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(plugins): 关闭标签快捷键改为冒泡阶段并尊重已消费事件 (#13664)

Evvvvvvvan87d757dfe473
diff --git a/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts b/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts
index 299cc064d7..8a532079ec 100644
--- a/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts
+++ b/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts
@@ -1,4 +1,5 @@
-import { describe, expect, it, vi } from "vitest";
+// @vitest-environment happy-dom
+import { beforeEach, describe, expect, it, vi } from "vitest";
import { reactive, readonly } from "vue";
import { MAX_CONCURRENT_PLUGIN_DATA_QUERIES, PLUGIN_CLIPBOARD_AUDIT_CAPACITY, PluginHostBridge, clipboardReadGateAllows, createClipboardReadGate, pluginSandboxDocument, pluginSdkSource, recordClipboardRead } from "./pluginHostBridge";
import type { InstalledPlugin, PluginResultViewContribution, PluginWorkbenchContribution } from "@/types/database";
@@ -2445,3 +2446,99 @@ describe("AI completion bridge", () => {
expect(messages.some((m) => m.type === "init" && m.capabilities?.connectionsApi === true)).toBe(true);
});
});
+
+// The plugin-side close shortcut has to cooperate with whatever the plugin's
+// focused control already did with the key. A terminal (xterm) turns Ctrl+W
+// into a control byte and claims the event; while the host registered its
+// listener in the CAPTURE phase it closed the tab before the terminal ever saw
+// the key. These tests boot the real injected SDK inside the DOM and dispatch a
+// bubbling keydown on a descendant node, because only real event propagation
+// shows the capture/bubble difference: a capture-phase listener runs before the
+// terminal textarea, a bubble-phase listener after it.
+describe("plugin close-tab shortcut vs. terminal key handling", () => {
+ const posted: unknown[] = [];
+ let booted = false;
+
+ // Boot once: every boot registers another window keydown listener, and stale
+ // listeners would answer the next test's keystrokes.
+ function bootSdk(): void {
+ if (booted) return;
+ booted = true;
+ const parent = { postMessage: (message: unknown) => posted.push(message) };
+ new Function("window", "parent", "addEventListener", "document", pluginSdkSource())(window, parent, window.addEventListener.bind(window), document);
+ }
+
+ function shortcuts(): Array<{ source?: string; type?: string; shortcut?: string }> {
+ return posted.filter((message) => (message as { type?: string }).type === "shortcut") as Array<{ source?: string; type?: string; shortcut?: string }>;
+ }
+
+ function mountTerminal(consume?: (event: { preventDefault: () => void; stopPropagation: () => void }) => void): HTMLTextAreaElement {
+ const container = document.createElement("div");
+ const textarea = document.createElement("textarea");
+ container.appendChild(textarea);
+ document.body.appendChild(container);
+ if (consume) textarea.addEventListener("keydown", consume);
+ return textarea;
+ }
+
+ function press(target: EventTarget, init: KeyboardEventInit, overrides?: Record<string, unknown>): KeyboardEvent {
+ const event = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init });
+ for (const [name, value] of Object.entries(overrides || {})) Object.defineProperty(event, name, { value, configurable: true });
+ target.dispatchEvent(event);
+ return event;
+ }
+
+ beforeEach(() => {
+ bootSdk();
+ posted.length = 0;
+ });
+
+ it("keeps the tab open when the focused terminal consumes Ctrl+W", () => {
+ // xterm's shape: claim the key so the browser and the host both back off.
+ const textarea = mountTerminal((event) => {
+ event.preventDefault();
+ event.stopPropagation();
+ });
+
+ const event = press(textarea, { key: "w", ctrlKey: true });
+
+ expect(event.defaultPrevented).toBe(true);
+ expect(shortcuts()).toEqual([]);
+ textarea.parentElement?.remove();
+ });
+
+ it("keeps the tab open when a consumer only calls preventDefault", () => {
+ const textarea = mountTerminal((event) => event.preventDefault());
+
+ press(textarea, { key: "w", ctrlKey: true });
+
+ expect(shortcuts()).toEqual([]);
+ textarea.parentElement?.remove();
+ });
+
+ it("still closes the tab exactly once per unconsumed Ctrl+W or Cmd+W", () => {
+ const textarea = mountTerminal();
+
+ press(textarea, { key: "w", ctrlKey: true });
+ expect(shortcuts()).toHaveLength(1);
+ expect(shortcuts()[0]).toMatchObject({ source: "dbx-plugin", type: "shortcut", shortcut: "closeTab" });
+
+ press(textarea, { key: "w", metaKey: true });
+ expect(shortcuts()).toHaveLength(2);
+ expect(shortcuts()[1]).toMatchObject({ shortcut: "closeTab" });
+ textarea.parentElement?.remove();
+ });
+
+ it("leaves Tab, IME composition, and excluded modifier combinations alone", () => {
+ const textarea = mountTerminal();
+
+ press(textarea, { key: "Tab", ctrlKey: true });
+ press(textarea, { key: "w", ctrlKey: true, shiftKey: true });
+ press(textarea, { key: "w", ctrlKey: true, altKey: true });
+ press(textarea, { key: "w", ctrlKey: true }, { isComposing: true });
+ press(textarea, { key: "w" });
+
+ expect(shortcuts()).toEqual([]);
+ textarea.parentElement?.remove();
+ });
+});
diff --git a/apps/desktop/src/lib/plugins/pluginHostBridge.ts b/apps/desktop/src/lib/plugins/pluginHostBridge.ts
index c71fe279de..b9a1c1cb96 100644
--- a/apps/desktop/src/lib/plugins/pluginHostBridge.ts
+++ b/apps/desktop/src/lib/plugins/pluginHostBridge.ts
@@ -1661,13 +1661,21 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
});
}
});
+ // The close-tab shortcut listens in the BUBBLE phase and yields to events a
+ // plugin input already consumed. A capture-phase listener runs before the
+ // focused control, so a terminal (xterm) handling Ctrl+W as "delete word"
+ // had the tab closed underneath it: the host posted closeTab before the
+ // terminal ever saw the key. Bubbling lets the focused control run first;
+ // xterm calls preventDefault()+stopPropagation() for the keys it sends, and
+ // any other consumer only needs preventDefault() to keep the tab open.
addEventListener('keydown', (event) => {
+ if (event.defaultPrevented) return;
const key = typeof event.key === 'string' ? event.key.toLowerCase() : '';
if (key !== 'w' || (!event.metaKey && !event.ctrlKey) || event.altKey || event.shiftKey || event.isComposing) return;
event.preventDefault();
event.stopPropagation();
parent.postMessage({ source: '${PLUGIN_MESSAGE_SOURCE}', version: ${BRIDGE_VERSION}, type: 'shortcut', shortcut: 'closeTab' }, '*');
- }, true);
+ });
parent.postMessage({ source: '${PLUGIN_MESSAGE_SOURCE}', version: ${BRIDGE_VERSION}, type: 'ready', features: ['workbench.close'] }, '*');
})();`;
}