fix(editor): match replace shortcut modifiers strictly

This commit is contained in:
jearhe authored and GitHub committed 2026-08-26 17:46:50 +08:00
1 parent 95537957cc
commit d15605cee9
3 files changed
+100 -3

No files matched your search

@@ -1,3 +1,6 @@
import { matchesShortcut } from "@/lib/editor/keyboardShortcuts";
import { shortcutToCodeMirrorKey } from "@/lib/editor/shortcutRegistry";
// pi-lens-ignore: typescript:2307
import type { KeyBinding } from "@codemirror/view";
interface QueryEditorSearchKeymapOptions {
@@ -21,3 +24,31 @@ export function createQueryEditorSearchKeymap(options: QueryEditorSearchKeymapOp
},
];
}
export function createQueryEditorReplaceShortcutBindings(shortcut: string, openReplace: () => boolean): KeyBinding[] {
const normalizedShortcut = shortcut.trim();
return normalizedShortcut && /\s/.test(normalizedShortcut) ? [{ key: shortcutToCodeMirrorKey(normalizedShortcut), preventDefault: true, run: openReplace }] : [];
}
/**
* Handle the configurable replace shortcut outside CodeMirror's character
* keymap matching. CodeMirror intentionally lets `Mod-r` match a shifted
* character event too, so a `Mod+R` replace binding can consume a custom
* `Shift+Mod+R` editor action before its exact binding is considered.
*
* Multi-stroke shortcuts stay in the regular keymap because this handler only
* handles one keyboard event at a time.
*/
export function createQueryEditorReplaceShortcutHandler(options: { shortcut: string; openReplace: () => boolean; isReadOnly: () => boolean }): (event: KeyboardEvent) => boolean {
const shortcut = options.shortcut.trim();
if (!shortcut || /\s/.test(shortcut)) return () => false;
return (event) => {
if (!matchesShortcut(event, shortcut)) return false;
// Match the existing keymap's preventDefault behavior even when the
// read-only guard prevents the panel from opening.
if (options.isReadOnly()) return true;
options.openReplace();
return true;
};
}