fix(editor): match replace shortcut modifiers strictly
This commit is contained in:
1 parent
95537957cc
commit
d15605cee9
3 files changed
+100
-3
No files matched your search
@@ -108,7 +108,7 @@ import {
|
||||
import type { SqlHighlighter } from "@/lib/sql/sqlHighlighter";
|
||||
import { EDITOR_FONT_FAMILY_CSS_VAR, EDITOR_FONT_SIZE_CSS_VAR, editorDiagnosticColors, editorThemeAppearanceFor, loadEditorTheme, editorFontTheme, shellLineCommentTheme, sqlCompletionTheme, sqlSemanticHighlightTheme } from "@/lib/editor/editorThemes";
|
||||
import { createStatementGutterMarkerDom, shouldShowStatementGutter } from "@/lib/editor/codemirrorStatementGutter";
|
||||
import { createQueryEditorSearchKeymap } from "@/lib/editor/queryEditorSearchKeymap";
|
||||
import { createQueryEditorReplaceShortcutBindings, createQueryEditorReplaceShortcutHandler, createQueryEditorSearchKeymap } from "@/lib/editor/queryEditorSearchKeymap";
|
||||
import { buildQueryEditorLineNumbersExtension } from "@/lib/editor/queryEditorLineNumbers";
|
||||
import { searchKeymapWithoutModD } from "@/lib/editor/codemirrorSearchKeymap";
|
||||
import { defaultKeymapForGlobalShortcuts } from "@/lib/editor/codemirrorDefaultKeymap";
|
||||
@@ -1937,7 +1937,20 @@ function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view")
|
||||
(currentView) => shouldBlockExecutionShortcut(undefined, currentView),
|
||||
);
|
||||
const executeInNewResultTabBindings = props.hideExecutionControls ? [] : createQueryEditorExecutionShortcutBindings(shortcuts.executeSqlInNewResultTab, requestExecuteInNewResultTab, (currentView) => shouldBlockExecutionShortcut(undefined, currentView));
|
||||
const replaceShortcutBindings = createQueryEditorReplaceShortcutBindings(shortcuts.replace, openReplace);
|
||||
const replaceShortcutHandler = createQueryEditorReplaceShortcutHandler({
|
||||
shortcut: shortcuts.replace,
|
||||
openReplace,
|
||||
isReadOnly: () => !!props.readOnly,
|
||||
});
|
||||
return [
|
||||
editorViewModule
|
||||
? (Prec?.high(
|
||||
editorViewModule.EditorView.domEventHandlers({
|
||||
keydown: replaceShortcutHandler,
|
||||
}),
|
||||
) ?? [])
|
||||
: [],
|
||||
Prec?.high(
|
||||
codeMirrorKeymap.of([
|
||||
{
|
||||
@@ -1945,7 +1958,7 @@ function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view")
|
||||
run: handleEnter,
|
||||
},
|
||||
...binding(shortcuts.find, openSearch),
|
||||
...binding(shortcuts.replace, openReplace),
|
||||
...replaceShortcutBindings,
|
||||
...executeInNewResultTabBindings,
|
||||
...executeBindings,
|
||||
...binding(shortcuts.saveSql, () => {
|
||||
|
||||
+54
-1
@@ -1,9 +1,15 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
// pi-lens-ignore: typescript:2307
|
||||
import { EditorState, Prec } from "@codemirror/state";
|
||||
// pi-lens-ignore: typescript:2307
|
||||
import { EditorView, keymap, runScopeHandlers } from "@codemirror/view";
|
||||
// pi-lens-ignore: typescript:2307
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createQueryEditorSearchKeymap } from "@/lib/editor/queryEditorSearchKeymap";
|
||||
// pi-lens-ignore: typescript:2307
|
||||
import { createQueryEditorExecutionShortcutBindings } from "@/lib/editor/queryEditorExecutionShortcut";
|
||||
// pi-lens-ignore: typescript:2307
|
||||
import { createQueryEditorReplaceShortcutBindings, createQueryEditorReplaceShortcutHandler, createQueryEditorSearchKeymap } from "@/lib/editor/queryEditorSearchKeymap";
|
||||
|
||||
describe("QueryEditor search keymap precedence", () => {
|
||||
it("runs configured editor actions before the built-in search fallback", () => {
|
||||
@@ -22,6 +28,53 @@ describe("QueryEditor search keymap precedence", () => {
|
||||
view.destroy();
|
||||
});
|
||||
|
||||
it("executes a custom Shift+Mod+R shortcut instead of the Mod+R replace binding", () => {
|
||||
const openReplace = vi.fn(() => true);
|
||||
const executeSql = vi.fn(() => true);
|
||||
const view = new EditorView({
|
||||
parent: document.createElement("div"),
|
||||
state: EditorState.create({
|
||||
extensions: [
|
||||
Prec.high(
|
||||
EditorView.domEventHandlers({
|
||||
keydown: createQueryEditorReplaceShortcutHandler({ shortcut: "Mod+R", openReplace, isReadOnly: () => false }),
|
||||
}),
|
||||
),
|
||||
Prec.high(keymap.of([...createQueryEditorReplaceShortcutBindings("Mod+R", openReplace), ...createQueryEditorExecutionShortcutBindings("Shift+Mod+R", executeSql, () => false)])),
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
const event = new KeyboardEvent("keydown", { key: "r", ctrlKey: true, shiftKey: true, bubbles: true, cancelable: true });
|
||||
view.contentDOM.dispatchEvent(event);
|
||||
expect(executeSql).toHaveBeenCalledOnce();
|
||||
expect(openReplace).not.toHaveBeenCalled();
|
||||
expect(event.defaultPrevented).toBe(true);
|
||||
view.destroy();
|
||||
});
|
||||
|
||||
it("keeps the default Mod+R replace action working", () => {
|
||||
const openReplace = vi.fn(() => true);
|
||||
const view = new EditorView({
|
||||
parent: document.createElement("div"),
|
||||
state: EditorState.create({
|
||||
extensions: [
|
||||
Prec.high(
|
||||
EditorView.domEventHandlers({
|
||||
keydown: createQueryEditorReplaceShortcutHandler({ shortcut: "Mod+R", openReplace, isReadOnly: () => false }),
|
||||
}),
|
||||
),
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
const event = new KeyboardEvent("keydown", { key: "r", ctrlKey: true, bubbles: true, cancelable: true });
|
||||
view.contentDOM.dispatchEvent(event);
|
||||
expect(openReplace).toHaveBeenCalledOnce();
|
||||
expect(event.defaultPrevented).toBe(true);
|
||||
view.destroy();
|
||||
});
|
||||
|
||||
it("keeps the search fallback above lower-priority CodeMirror bindings", () => {
|
||||
const openSearch = vi.fn(() => true);
|
||||
const lowerPrioritySearch = vi.fn(() => true);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user