Skip to content

Commit

Permalink
Revert "fix(frontend): 直前のパターンを記録するように"
Browse files Browse the repository at this point in the history
This reverts commit 5372b25.
  • Loading branch information
kakkokari-gtyih committed Jul 9, 2024
1 parent bdac342 commit a9bb52e
Showing 1 changed file with 4 additions and 30 deletions.
34 changes: 4 additions & 30 deletions packages/frontend/src/scripts/hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ const MODIFIER_KEYS = ['ctrl', 'alt', 'shift'];
const IGNORE_ELEMENTS = ['input', 'textarea'];
//#endregion

//#region store
let latestHotkey: Pattern & { callback: CallbackFunction } | null = null;
//#endregion

//#region impl
export const makeHotkey = (keymap: Keymap) => {
const actions = parseKeymap(keymap);
Expand All @@ -58,12 +54,11 @@ export const makeHotkey = (keymap: Keymap) => {
if (IGNORE_ELEMENTS.includes(document.activeElement.tagName.toLowerCase())) return;
if (getHTMLElementOrNull(document.activeElement)?.isContentEditable) return;
}
for (const action of actions) {
if (matchPatterns(ev, action)) {
for (const { patterns, callback, options } of actions) {
if (matchPatterns(ev, patterns, options)) {
ev.preventDefault();
ev.stopPropagation();
action.callback(ev);
storePattern(ev, action.callback);
callback(ev);
}
}
};
Expand Down Expand Up @@ -108,21 +103,10 @@ const parseOptions = (rawCallback: Keymap[keyof Keymap]) => {
return { ...defaultOptions } as const satisfies Action['options'];
};

const matchPatterns = (ev: KeyboardEvent, action: Action) => {
const { patterns, options, callback } = action;
const matchPatterns = (ev: KeyboardEvent, patterns: Action['patterns'], options: Action['options']) => {
if (ev.repeat && !options.allowRepeat) return false;
const key = ev.key.toLowerCase();
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 (ctrl !== (ev.ctrlKey || ev.metaKey)) return false;
if (alt !== ev.altKey) return false;
Expand All @@ -131,16 +115,6 @@ const matchPatterns = (ev: KeyboardEvent, action: Action) => {
});
};

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) => {
if (input == null) return [];
const raw = getValueByKey(KEY_ALIASES, input);
Expand Down

0 comments on commit a9bb52e

Please sign in to comment.