76 lines
2.4 KiB
JavaScript
76 lines
2.4 KiB
JavaScript
'use client'
|
|
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/use-shortcut.ts
|
|
var use_shortcut_exports = {};
|
|
__export(use_shortcut_exports, {
|
|
useShortcut: () => useShortcut
|
|
});
|
|
module.exports = __toCommonJS(use_shortcut_exports);
|
|
var import_react = require("react");
|
|
function isPrintableCharacter(event) {
|
|
const { key } = event;
|
|
return key.length === 1 || key.length > 1 && /[^a-zA-Z0-9]/.test(key);
|
|
}
|
|
function useShortcut(props = {}) {
|
|
const { timeout = 300, preventDefault = () => true } = props;
|
|
const [keys, setKeys] = (0, import_react.useState)([]);
|
|
const timeoutRef = (0, import_react.useRef)();
|
|
const flush = () => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
timeoutRef.current = null;
|
|
}
|
|
};
|
|
const clearKeysAfterDelay = () => {
|
|
flush();
|
|
timeoutRef.current = setTimeout(() => {
|
|
setKeys([]);
|
|
timeoutRef.current = null;
|
|
}, timeout);
|
|
};
|
|
(0, import_react.useEffect)(() => flush, []);
|
|
function onKeyDown(fn) {
|
|
return (event) => {
|
|
if (event.key === "Backspace") {
|
|
const keysCopy = [...keys];
|
|
keysCopy.pop();
|
|
setKeys(keysCopy);
|
|
return;
|
|
}
|
|
if (isPrintableCharacter(event)) {
|
|
const keysCopy = keys.concat(event.key);
|
|
if (preventDefault(event)) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
setKeys(keysCopy);
|
|
fn(keysCopy.join(""));
|
|
clearKeysAfterDelay();
|
|
}
|
|
};
|
|
}
|
|
return onKeyDown;
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
useShortcut
|
|
});
|
|
//# sourceMappingURL=use-shortcut.js.map
|