fix(frontend): 直前のパターンを記録するように
This commit is contained in:
parent
a5407131d4
commit
5372b25940
|
@ -44,6 +44,10 @@ const MODIFIER_KEYS = ['ctrl', 'alt', 'shift'];
|
||||||
const IGNORE_ELEMENTS = ['input', 'textarea'];
|
const IGNORE_ELEMENTS = ['input', 'textarea'];
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
|
//#region store
|
||||||
|
let latestHotkey: Pattern & { callback: CallbackFunction } | null = null;
|
||||||
|
//#endregion
|
||||||
|
|
||||||
//#region impl
|
//#region impl
|
||||||
export const makeHotkey = (keymap: Keymap) => {
|
export const makeHotkey = (keymap: Keymap) => {
|
||||||
const actions = parseKeymap(keymap);
|
const actions = parseKeymap(keymap);
|
||||||
|
@ -53,11 +57,12 @@ export const makeHotkey = (keymap: Keymap) => {
|
||||||
if (IGNORE_ELEMENTS.includes(document.activeElement.tagName.toLowerCase())) return;
|
if (IGNORE_ELEMENTS.includes(document.activeElement.tagName.toLowerCase())) return;
|
||||||
if ((document.activeElement as HTMLElement).isContentEditable) return;
|
if ((document.activeElement as HTMLElement).isContentEditable) return;
|
||||||
}
|
}
|
||||||
for (const { patterns, callback, options } of actions) {
|
for (const action of actions) {
|
||||||
if (matchPatterns(ev, patterns, options)) {
|
if (matchPatterns(ev, action)) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
callback(ev);
|
action.callback(ev);
|
||||||
|
storePattern(ev, action.callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -102,10 +107,21 @@ const parseOptions = (rawCallback: Keymap[keyof Keymap]) => {
|
||||||
return { ...defaultOptions } as const satisfies Action['options'];
|
return { ...defaultOptions } as const satisfies Action['options'];
|
||||||
};
|
};
|
||||||
|
|
||||||
const matchPatterns = (ev: KeyboardEvent, patterns: Action['patterns'], options: Action['options']) => {
|
const matchPatterns = (ev: KeyboardEvent, action: Action) => {
|
||||||
|
const { patterns, options, callback } = action;
|
||||||
if (ev.repeat && !options.allowRepeat) return false;
|
if (ev.repeat && !options.allowRepeat) return false;
|
||||||
const key = ev.key.toLowerCase();
|
const key = ev.key.toLowerCase();
|
||||||
return patterns.some(({ which, ctrl, shift, alt }) => {
|
return patterns.some(({ which, ctrl, shift, alt }) => {
|
||||||
|
if (
|
||||||
|
latestHotkey != null &&
|
||||||
|
latestHotkey.which.includes(key) &&
|
||||||
|
latestHotkey.ctrl === ctrl &&
|
||||||
|
latestHotkey.alt === alt &&
|
||||||
|
latestHotkey.shift === shift &&
|
||||||
|
latestHotkey.callback === callback
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!which.includes(key)) return false;
|
if (!which.includes(key)) return false;
|
||||||
if (ctrl !== (ev.ctrlKey || ev.metaKey)) return false;
|
if (ctrl !== (ev.ctrlKey || ev.metaKey)) return false;
|
||||||
if (alt !== ev.altKey) return false;
|
if (alt !== ev.altKey) return false;
|
||||||
|
@ -114,6 +130,16 @@ const matchPatterns = (ev: KeyboardEvent, patterns: Action['patterns'], options:
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const storePattern = (ev: KeyboardEvent, callback: CallbackFunction) => {
|
||||||
|
latestHotkey = {
|
||||||
|
which: [ev.key.toLowerCase()],
|
||||||
|
ctrl: ev.ctrlKey || ev.metaKey,
|
||||||
|
alt: ev.altKey,
|
||||||
|
shift: ev.shiftKey,
|
||||||
|
callback,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const parseKeyCode = (input?: string | null) => {
|
const parseKeyCode = (input?: string | null) => {
|
||||||
if (input == null) return [];
|
if (input == null) return [];
|
||||||
const raw = getValueByKey(KEY_ALIASES, input);
|
const raw = getValueByKey(KEY_ALIASES, input);
|
||||||
|
|
Loading…
Reference in New Issue