Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(editor): support line comment shortcuts under IME and numpad divide

lxk955d9755f0632a5
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index 4b1c265995..7baaf72d78 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -142,6 +142,7 @@ import { scrollEditorLine } from "@/lib/editor/scrollEditorLine";
import { createQueryEditorStringMouseSelection } from "@/lib/editor/queryEditorStringMouseSelection";
import { createQueryEditorCompletionShortcutBindings } from "@/lib/editor/queryEditorCompletionShortcut";
import { handleQueryEditorCompletionNavigation } from "@/lib/editor/queryEditorCompletionNavigation";
+import { createQueryEditorLineCommentShortcutBindings } from "@/lib/editor/queryEditorLineComment";
import { createQueryEditorSelectionCaseShortcutBindings } from "@/lib/editor/queryEditorSelectionCaseShortcut";
import { createQueryEditorExecutionShortcutBindings, createQueryEditorExecutionShortcutHandler, createQueryEditorPostCompositionKeyGuard } from "@/lib/editor/queryEditorExecutionShortcut";
import { supportsPlDebugRoutine, supportsQueryEditorBlockComments, supportsSqlInListPaste } from "@/lib/database/databaseFeatureSupport";
@@ -1560,7 +1561,7 @@ function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view")
...createQueryEditorSelectionCaseShortcutBindings(shortcuts.lowercaseSelection, () => convertSelectedSqlCase("lower")),
...createQueryEditorSelectionCaseShortcutBindings(shortcuts.toggleCaseSelection, () => convertSelectedSqlCase("toggle")),
...createQueryEditorSelectionCaseShortcutBindings(shortcuts.convertNamingStyle, () => convertSelectedNamingStyle()),
- ...binding(shortcuts.toggleLineComment, (view) => codeMirrorRuntime.codeMirrorToggleLineComment?.(view) ?? false),
+ ...createQueryEditorLineCommentShortcutBindings(shortcuts.toggleLineComment, (view) => codeMirrorRuntime.codeMirrorToggleLineComment?.(view) ?? false),
...binding(shortcuts.toggleBlockComment, (view) => {
if (!supportsQueryEditorBlockComments(props.databaseType)) return false;
return codeMirrorRuntime.codeMirrorToggleBlockComment?.(view) ?? false;
@@ -2143,7 +2144,9 @@ async function flushImeComposition() {
function defaultKeymapExtension() {
if (!codeMirrorRuntime.editorViewModule || !codeMirrorRuntime.codeMirrorDefaultKeymap || !codeMirrorRuntime.codeMirrorToggleBlockComment) return [];
- return codeMirrorRuntime.editorViewModule.keymap.of(defaultKeymapForGlobalShortcuts(codeMirrorRuntime.codeMirrorDefaultKeymap, settingsStore.editorSettings.shortcuts).filter((item) => item.run !== codeMirrorRuntime.codeMirrorToggleBlockComment));
+ return codeMirrorRuntime.editorViewModule.keymap.of(
+ defaultKeymapForGlobalShortcuts(codeMirrorRuntime.codeMirrorDefaultKeymap, settingsStore.editorSettings.shortcuts).filter((item) => item.run !== codeMirrorRuntime.codeMirrorToggleBlockComment && item.run !== codeMirrorRuntime.codeMirrorToggleLineComment),
+ );
}
const codeMirrorLifecycle = useQueryEditorCodeMirror({
diff --git a/apps/desktop/src/lib/__tests__/editor/keyboardShortcuts.spec.ts b/apps/desktop/src/lib/__tests__/editor/keyboardShortcuts.spec.ts
index ffc845646e..1186e01109 100644
--- a/apps/desktop/src/lib/__tests__/editor/keyboardShortcuts.spec.ts
+++ b/apps/desktop/src/lib/__tests__/editor/keyboardShortcuts.spec.ts
@@ -119,6 +119,25 @@ describe("keyboard shortcut matching", () => {
expect(eventToShortcut({ key: "A", code: "KeyA", ctrlKey: true, shiftKey: true }, "Win32")).toBe("Shift+Mod+A");
});
+ it("records Mod+/ from Windows Chinese IME punctuation, Process keys, and numpad divide", () => {
+ // Chinese IME converts "/" to "、" (Ideographic comma)
+ expect(eventToShortcut({ key: "、", code: "Slash", ctrlKey: true }, "Win32")).toBe("Mod+/");
+ // Windows IME keydown with "Process" key
+ expect(eventToShortcut({ key: "Process", code: "Slash", ctrlKey: true }, "Win32")).toBe("Mod+/");
+ expect(eventToShortcut({ key: "Process", code: "Slash", ctrlKey: true, isComposing: true }, "Win32")).toBe("Mod+/");
+ // Numpad divide key (which may report "Divide" or "/")
+ expect(eventToShortcut({ key: "Divide", code: "NumpadDivide", ctrlKey: true }, "Win32")).toBe("Mod+/");
+ expect(eventToShortcut({ key: "/", code: "NumpadDivide", ctrlKey: true }, "Win32")).toBe("Mod+/");
+ });
+
+ it("matches Mod+/ against Chinese IME punctuation, Process keys, and numpad divide", () => {
+ expect(matchesShortcut({ key: "、", code: "Slash", ctrlKey: true }, "Mod+/", "Win32")).toBe(true);
+ expect(matchesShortcut({ key: "Process", code: "Slash", ctrlKey: true }, "Mod+/", "Win32")).toBe(true);
+ expect(matchesShortcut({ key: "Process", code: "Slash", ctrlKey: true, isComposing: true }, "Mod+/", "Win32")).toBe(true);
+ expect(matchesShortcut({ key: "Divide", code: "NumpadDivide", ctrlKey: true }, "Mod+/", "Win32")).toBe(true);
+ expect(matchesShortcut({ key: "/", code: "NumpadDivide", ctrlKey: true }, "Mod+/", "Win32")).toBe(true);
+ });
+
it.each([
["¨", "KeyU", "Shift+Alt+U"],
["Ò", "KeyL", "Shift+Alt+L"],
diff --git a/apps/desktop/src/lib/__tests__/editor/queryEditorLineComment.spec.ts b/apps/desktop/src/lib/__tests__/editor/queryEditorLineComment.spec.ts
index 00fb59e5f9..fe55692c9a 100644
--- a/apps/desktop/src/lib/__tests__/editor/queryEditorLineComment.spec.ts
+++ b/apps/desktop/src/lib/__tests__/editor/queryEditorLineComment.spec.ts
@@ -1,8 +1,11 @@
+// @vitest-environment happy-dom
+
import { toggleLineComment } from "@codemirror/commands";
import { sql } from "@codemirror/lang-sql";
import { EditorState, Prec, type Transaction } from "@codemirror/state";
+import { EditorView, keymap, runScopeHandlers } from "@codemirror/view";
import { describe, expect, it, vi } from "vitest";
-import { queryEditorCommentTokens, queryEditorLineCommentToken, queryEditorWordLanguageData } from "@/lib/editor/queryEditorLineComment";
+import { createQueryEditorLineCommentShortcutBindings, queryEditorCommentTokens, queryEditorLineCommentToken, queryEditorWordLanguageData } from "@/lib/editor/queryEditorLineComment";
function runToggleLineComment(doc: string, commentToken: string) {
let state = EditorState.create({
@@ -85,3 +88,73 @@ describe("QueryEditor line comment", () => {
expect(result.state.doc.toString()).toBe("-- SELECT 1");
});
});
+
+describe("createQueryEditorLineCommentShortcutBindings", () => {
+ function createView(shortcut: string, run: (view: EditorView) => boolean, platform = "Win32") {
+ return new EditorView({
+ parent: document.createElement("div"),
+ state: EditorState.create({
+ extensions: [keymap.of(createQueryEditorLineCommentShortcutBindings(shortcut, run, platform))],
+ }),
+ });
+ }
+
+ it.each([
+ ["standard slash", { key: "/", code: "Slash", ctrlKey: true }],
+ ["Chinese IME punctuation (、)", { key: "、", code: "Slash", ctrlKey: true }],
+ ["IME Process key", { key: "Process", code: "Slash", ctrlKey: true }],
+ ["IME Process key while isComposing", { key: "Process", code: "Slash", ctrlKey: true, isComposing: true }],
+ ["numpad divide key (Divide)", { key: "Divide", code: "NumpadDivide", ctrlKey: true }],
+ ["numpad divide key (/)", { key: "/", code: "NumpadDivide", ctrlKey: true }],
+ ])("dispatches line comment for %s on Windows", (_name, eventInit) => {
+ const run = vi.fn(() => true);
+ const view = createView("Mod+/", run, "Win32");
+ const event = new KeyboardEvent("keydown", {
+ ...eventInit,
+ bubbles: true,
+ cancelable: true,
+ });
+
+ expect(runScopeHandlers(view, event, "editor")).toBe(true);
+ expect(run).toHaveBeenCalledOnce();
+ expect(run).toHaveBeenCalledWith(view);
+ view.destroy();
+ });
+
+ it("leaves line comment unhandled when a different key is pressed", () => {
+ const run = vi.fn(() => true);
+ const view = createView("Mod+/", run, "Win32");
+ const event = new KeyboardEvent("keydown", {
+ key: "z",
+ code: "KeyZ",
+ ctrlKey: true,
+ bubbles: true,
+ cancelable: true,
+ });
+
+ expect(runScopeHandlers(view, event, "editor")).toBe(false);
+ expect(run).not.toHaveBeenCalled();
+ view.destroy();
+ });
+
+ it("handles custom configured shortcut", () => {
+ const run = vi.fn(() => true);
+ const view = createView("Shift+Mod+C", run, "Win32");
+ const event = new KeyboardEvent("keydown", {
+ key: "C",
+ code: "KeyC",
+ ctrlKey: true,
+ shiftKey: true,
+ bubbles: true,
+ cancelable: true,
+ });
+
+ expect(runScopeHandlers(view, event, "editor")).toBe(true);
+ expect(run).toHaveBeenCalledOnce();
+ view.destroy();
+ });
+
+ it("keeps disabled when shortcut is empty", () => {
+ expect(createQueryEditorLineCommentShortcutBindings("", vi.fn())).toEqual([]);
+ });
+});
diff --git a/apps/desktop/src/lib/editor/keyboardShortcuts.ts b/apps/desktop/src/lib/editor/keyboardShortcuts.ts
index 299e3a303b..4fa5ede634 100644
--- a/apps/desktop/src/lib/editor/keyboardShortcuts.ts
+++ b/apps/desktop/src/lib/editor/keyboardShortcuts.ts
@@ -44,32 +44,111 @@ const BASE_KEY_BY_CODE: Record<string, string> = {
Comma: ",",
Period: ".",
Slash: "/",
+ NumpadDivide: "/",
+ NumpadMultiply: "*",
+ NumpadSubtract: "-",
+ NumpadAdd: "+",
+ NumpadDecimal: ".",
+ Numpad0: "0",
+ Numpad1: "1",
+ Numpad2: "2",
+ Numpad3: "3",
+ Numpad4: "4",
+ Numpad5: "5",
+ Numpad6: "6",
+ Numpad7: "7",
+ Numpad8: "8",
+ Numpad9: "9",
+};
+
+const CJK_OR_MUTATED_PUNCTUATION: Record<string, string> = {
+ "、": "/",
+ "·": "`",
+ "【": "[",
+ "】": "]",
+ "。": ".",
+ ",": ",",
+ ";": ";",
+ ":": ";",
+ "“": "'",
+ "”": "'",
+ "‘": "'",
+ "’": "'",
+ Divide: "/",
+ Multiply: "*",
+ Subtract: "-",
+ Add: "+",
+ Decimal: ".",
+ "÷": "/",
};
/**
- * The unshifted character for a shifted punctuation/digit press, or null when
- * the event is not one. Mirrors CodeMirror's `base[event.keyCode]` table, which
- * is what its keymap resolves `Shift-` bindings against.
+ * Physical unshifted character for a key press, or null when not applicable.
+ * Handles shifted punctuation/digit presses (mirroring CodeMirror keymap resolution),
+ * physical Slash / NumpadDivide, IME placeholders (Process / Unidentified), and
+ * CJK punctuation emitted by active input methods on Windows.
*/
-function baseKeyForShiftedEvent(event: ShortcutLikeEvent): string | null {
- if (!event.shiftKey || event.key.length !== 1) return null;
- const baseKey = BASE_KEY_BY_CODE[event.code ?? ""];
- return baseKey && baseKey !== event.key ? baseKey : null;
+function baseKeyForEvent(event: ShortcutLikeEvent): string | null {
+ const code = event.code ?? "";
+
+ // Shifted punctuation/digit press (e.g. "?" on Slash -> "/", "!" on Digit1 -> "1")
+ if (event.shiftKey && event.key && event.key.length === 1) {
+ const baseKey = BASE_KEY_BY_CODE[code];
+ if (baseKey && baseKey !== event.key) return baseKey;
+ }
+
+ // Physical slash or numpad divide always resolves to "/"
+ if (code === "Slash" || code === "NumpadDivide") {
+ return "/";
+ }
+
+ // Known deformed or CJK punctuation produced by the physical key
+ if (event.key && CJK_OR_MUTATED_PUNCTUATION[event.key]) {
+ return CJK_OR_MUTATED_PUNCTUATION[event.key];
+ }
+
+ // IME placeholder key (Process / Unidentified) with recognized code
+ if (event.key === "Process" || event.key === "Unidentified") {
+ if (BASE_KEY_BY_CODE[code]) return BASE_KEY_BY_CODE[code];
+ }
+
+ // Numpad named keys like "Divide", "Multiply", "Add", etc.
+ if (code.startsWith("Numpad") && BASE_KEY_BY_CODE[code]) {
+ return BASE_KEY_BY_CODE[code];
+ }
+
+ return null;
}
function matchesShortcutKey(event: ShortcutLikeEvent, key: string, platform = globalThis.navigator?.platform || ""): boolean {
- if (normalizeKey(event.key) === normalizeKey(key)) return true;
- // Shortcuts store the unshifted character for shifted punctuation/digits (see
- // baseKeyForShiftedEvent), so match those against the physical key too.
- if (baseKeyForShiftedEvent(event) === normalizeKey(key)) return true;
+ const normalizedTarget = normalizeKey(key);
+ if (normalizeKey(event.key) === normalizedTarget) return true;
+
+ // Match against physical key / unshifted base key (shifted punctuation,
+ // Slash / NumpadDivide, IME Process / CJK punctuation, numpad keys)
+ const baseKey = baseKeyForEvent(event);
+ if (baseKey && normalizeKey(baseKey) === normalizedTarget) return true;
+
+ // Slash / NumpadDivide physical key match for "/"
+ if (normalizedTarget === "/" && (event.code === "Slash" || event.code === "NumpadDivide")) {
+ return true;
+ }
+
// KeyboardEvent.code 回退:event.key 随布局/修饰键变形时按物理键位匹配
// (macOS Option+字母 → 变形字符如 ⌥W="∑";俄文等非拉丁布局 → "Ц")。
// 仅限 Alt 组合键场景
- if (!event.altKey || !/^Key[A-Z]$/.test(event.code ?? "") || !/^[A-Z]$/i.test(key)) return false;
- // 非 macOS 的 Ctrl+Alt 是 AltGr 特征:用户可能在输入字符(如波兰语
- // AltGr+W → "ł"),按 code 强制匹配会让全局快捷键在打字时误触发
- if (!isMacShortcutPlatform(platform) && event.ctrlKey) return false;
- return event.code!.slice(3).toLowerCase() === key.toLowerCase();
+ if (event.altKey && /^Key[A-Z]$/.test(event.code ?? "") && /^[A-Z]$/i.test(key)) {
+ // 非 macOS 的 Ctrl+Alt 是 AltGr 特征:用户可能在输入字符(如波兰语
+ // AltGr+W → "ł"),按 code 强制匹配会让全局快捷键在打字时误触发
+ if (!isMacShortcutPlatform(platform) && event.ctrlKey) return false;
+ return event.code!.slice(3).toLowerCase() === key.toLowerCase();
+ }
+
+ if ((event.key === "Process" || event.key === "Unidentified") && /^Key[A-Z]$/.test(event.code ?? "") && /^[A-Z]$/i.test(key)) {
+ return event.code!.slice(3).toLowerCase() === key.toLowerCase();
+ }
+
+ return false;
}
function shortcutKeyName(key: string): string | null {
@@ -84,15 +163,17 @@ function shortcutKeyNameFromEvent(event: ShortcutLikeEvent, platform: string): s
if (event.altKey && isMacShortcutPlatform(platform) && /^Key[A-Z]$/.test(event.code ?? "")) {
return event.code!.slice(3);
}
- // Record "Shift+Mod+/" rather than "Shift+Mod+?" so the CodeMirror binding
- // ("Shift-Ctrl-/") is one the keymap actually resolves for this key press.
- const baseKey = baseKeyForShiftedEvent(event);
+ const baseKey = baseKeyForEvent(event);
if (baseKey) return shortcutKeyName(baseKey);
+ if ((event.key === "Process" || event.key === "Unidentified") && /^Key[A-Z]$/.test(event.code ?? "")) {
+ return event.code!.slice(3).toUpperCase();
+ }
return shortcutKeyName(event.key);
}
export function eventToShortcut(event: ShortcutLikeEvent, platform = globalThis.navigator?.platform || ""): string | null {
- if (event.isComposing) return null;
+ const isSlashShortcut = (event.code === "Slash" || event.code === "NumpadDivide") && (!!event.ctrlKey || !!event.metaKey);
+ if (event.isComposing && !isSlashShortcut) return null;
const key = shortcutKeyNameFromEvent(event, platform);
if (!key) return null;
@@ -140,7 +221,8 @@ export function matchesModifierOnlyShortcut(event: Omit<ShortcutLikeEvent, "key"
}
export function matchesShortcut(event: ShortcutLikeEvent, shortcut: string, platform = globalThis.navigator?.platform || ""): boolean {
- if (event.isComposing || !shortcut) return false;
+ const isSlashShortcut = (event.code === "Slash" || event.code === "NumpadDivide") && (!!event.ctrlKey || !!event.metaKey);
+ if ((event.isComposing && !isSlashShortcut) || !shortcut) return false;
const parts = parseShortcutParts(shortcut);
const key = parts[parts.length - 1] ?? "";
const modifiers = new Set(parts.slice(0, -1));
diff --git a/apps/desktop/src/lib/editor/queryEditorLineComment.ts b/apps/desktop/src/lib/editor/queryEditorLineComment.ts
index 8aadb06f94..34e301b35c 100644
--- a/apps/desktop/src/lib/editor/queryEditorLineComment.ts
+++ b/apps/desktop/src/lib/editor/queryEditorLineComment.ts
@@ -1,4 +1,8 @@
+import type { Command, KeyBinding } from "@codemirror/view";
import type { DatabaseType } from "@/types/database";
+import { matchesShortcut } from "@/lib/editor/keyboardShortcuts";
+import { parseShortcutStrokes } from "@/lib/editor/shortcutDisplay";
+import { shortcutToCodeMirrorKey } from "@/lib/editor/shortcutRegistry";
import { graphQueryProfile, isGraphQueryDatabase } from "@/lib/graph/graphQueryProfiles";
const SQL_LINE_COMMENT_TOKEN = "--";
@@ -24,3 +28,32 @@ export function queryEditorCommentTokens(dbType?: DatabaseType) {
export function queryEditorWordLanguageData(dbType?: DatabaseType) {
return dbType === "sqlserver" ? [{ wordChars: "@#" }] : [];
}
+
+/**
+ * Creates CodeMirror keybindings for toggling line comments.
+ * Single-stroke shortcuts use an `any` handler backed by `matchesShortcut`
+ * so that physical key fallback (Slash / NumpadDivide) works seamlessly
+ * even when active IMEs (e.g. Windows Chinese IME producing "、" or "Process"),
+ * numpad keys ("Divide"), or layout shifts alter `KeyboardEvent.key`.
+ */
+export function createQueryEditorLineCommentShortcutBindings(shortcut: string, run: Command, platform = globalThis.navigator?.platform || ""): KeyBinding[] {
+ if (!shortcut) return [];
+
+ if (parseShortcutStrokes(shortcut).length === 1) {
+ return [
+ {
+ any(view, event) {
+ if (event.isComposing && event.code !== "Slash" && event.code !== "NumpadDivide") return false;
+ if (!matchesShortcut(event, shortcut, platform)) return false;
+ if (run(view)) {
+ event.preventDefault();
+ return true;
+ }
+ return false;
+ },
+ },
+ ];
+ }
+
+ return [{ key: shortcutToCodeMirrorKey(shortcut), preventDefault: true, run }];
+}