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

@@ -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, () => {
@@ -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);