fix(grid): Ctrl+A selects SQL preview text, not table data

This commit is contained in:
zipg authored and GitHub committed 2026-09-25 23:59:04 +08:00
1 parent e91340ee15
commit 7255a194cb
2 files changed
+169 -3

No files matched your search

@@ -6,7 +6,7 @@ import { Button } from "@/components/ui/button";
import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip"; import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip";
import { useTheme } from "@/composables/useTheme"; import { useTheme } from "@/composables/useTheme";
import { useToast } from "@/composables/useToast"; import { useToast } from "@/composables/useToast";
import { copyToClipboard } from "@/lib/common/clipboard"; import { copyToClipboard, isPlainClipboardShortcut } from "@/lib/common/clipboard";
import { formatSqlText, type SqlFormatDialect } from "@/lib/sql/sqlFormatter"; import { formatSqlText, type SqlFormatDialect } from "@/lib/sql/sqlFormatter";
import { createShikiSqlHighlighter, type SqlHighlighter } from "@/lib/sql/sqlHighlighter"; import { createShikiSqlHighlighter, type SqlHighlighter } from "@/lib/sql/sqlHighlighter";
import { useSettingsStore } from "@/stores/settingsStore"; import { useSettingsStore } from "@/stores/settingsStore";
@@ -47,6 +47,26 @@ const displaySql = computed(() => {
const hasSql = computed(() => props.sql.trim().length > 0); const hasSql = computed(() => props.sql.trim().length > 0);
const contentRef = ref<HTMLPreElement | null>(null);
function selectPreviewContent() {
const el = contentRef.value;
if (!el) return false;
const range = document.createRange();
range.selectNodeContents(el);
const selection = window.getSelection();
selection?.removeAllRanges();
selection?.addRange(range);
return true;
}
function onContentKeydown(event: KeyboardEvent) {
if (!isPlainClipboardShortcut(event, "a")) return;
// Claim the shortcut before it reaches the surrounding data grid, then select
// the preview text instead of the grid cells.
if (selectPreviewContent()) event.preventDefault();
}
async function initHighlighter() { async function initHighlighter() {
if (highlighter) return; if (highlighter) return;
try { try {
@@ -193,10 +213,10 @@ onMounted(() => {
</div> </div>
<!-- Shiki highlighted SQL --> <!-- Shiki highlighted SQL -->
<pre v-else-if="highlightedHtml" data-native-clipboard class="m-0 p-3 text-xs font-mono leading-relaxed whitespace-pre-wrap break-words select-text" v-html="highlightedHtml"></pre> <pre v-else-if="highlightedHtml" ref="contentRef" data-native-clipboard tabindex="0" class="m-0 p-3 text-xs font-mono leading-relaxed whitespace-pre-wrap break-words select-text outline-none" v-html="highlightedHtml" @keydown="onContentKeydown"></pre>
<!-- Plain text fallback --> <!-- Plain text fallback -->
<pre v-else data-native-clipboard class="p-3 text-xs font-mono whitespace-pre-wrap select-text">{{ displaySql }}</pre> <pre v-else ref="contentRef" data-native-clipboard tabindex="0" class="p-3 text-xs font-mono whitespace-pre-wrap select-text outline-none" @keydown="onContentKeydown">{{ displaySql }}</pre>
</div> </div>
</div> </div>
</template> </template>
@@ -0,0 +1,146 @@
// @vitest-environment happy-dom
import { createApp, nextTick, type App } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
highlight: vi.fn<(content: string) => string>(() => ""),
}));
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
vi.mock("@/composables/useTheme", () => ({ useTheme: () => ({ isDark: { value: false } }) }));
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: vi.fn() }) }));
vi.mock("@/stores/settingsStore", () => ({ useSettingsStore: () => ({ editorSettings: { sqlFormatter: {} } }) }));
vi.mock("@/lib/sql/sqlHighlighter", () => ({
createShikiSqlHighlighter: async () => (content: string) => mocks.highlight(content),
}));
vi.mock("@/components/ui/button", async () => {
const { defineComponent, h } = await import("vue");
return {
Button: defineComponent({
inheritAttrs: false,
setup(_, { attrs, slots }) {
return () => h("button", attrs, slots.default?.());
},
}),
};
});
vi.mock("@/components/ui/tooltip", async () => {
const { defineComponent, h } = await import("vue");
const passthrough = () =>
defineComponent({
setup:
(_, { slots }) =>
() =>
h("div", slots.default?.()),
});
return { Tooltip: passthrough(), TooltipTrigger: passthrough(), TooltipContent: passthrough() };
});
import SqlPreviewPanel from "@/components/editor/SqlPreviewPanel.vue";
const SQL = "UPDATE users SET name = 'Ada' WHERE id = 1;";
let app: App<Element> | null = null;
let host: HTMLDivElement | null = null;
async function flushUi() {
for (let index = 0; index < 4; index += 1) {
await Promise.resolve();
await nextTick();
}
}
async function mountPanel(sql = SQL) {
host = document.createElement("div");
document.body.append(host);
app = createApp(SqlPreviewPanel, { sql });
app.mount(host);
await flushUi();
return host;
}
function previewText(host: HTMLElement): HTMLPreElement {
const pre = host.querySelector("pre");
if (!pre) throw new Error("SQL preview text not found");
return pre;
}
function dispatchSelectAll(target: HTMLElement, modifiers: KeyboardEventInit = { ctrlKey: true }) {
const event = new KeyboardEvent("keydown", { key: "a", bubbles: true, cancelable: true, ...modifiers });
target.dispatchEvent(event);
return event;
}
beforeEach(() => {
mocks.highlight.mockImplementation(() => "");
});
afterEach(() => {
window.getSelection()?.removeAllRanges();
app?.unmount();
app = null;
host?.remove();
host = null;
mocks.highlight.mockClear();
});
describe("SqlPreviewPanel select all", () => {
it("selects the preview text on Ctrl+A instead of leaving the shortcut to the data grid", async () => {
const mounted = await mountPanel();
const pre = previewText(mounted);
expect(pre.getAttribute("data-native-clipboard")).toBe("");
expect(pre.tabIndex).toBe(0);
pre.focus();
expect(document.activeElement).toBe(pre);
const event = dispatchSelectAll(pre);
const selection = window.getSelection();
expect(event.defaultPrevented).toBe(true);
expect(selection?.rangeCount).toBe(1);
expect(selection?.toString()).toBe(SQL);
expect(selection?.anchorNode && pre.contains(selection.anchorNode)).toBe(true);
});
it("supports Cmd+A and selects the highlighted SQL", async () => {
mocks.highlight.mockImplementation((content) => `<span class="tok">${content}</span>`);
const mounted = await mountPanel();
const pre = previewText(mounted);
expect(pre.innerHTML).toContain('class="tok"');
const event = dispatchSelectAll(pre, { metaKey: true });
const selection = window.getSelection();
expect(event.defaultPrevented).toBe(true);
expect(selection?.rangeCount).toBe(1);
expect(selection?.toString()).toBe(SQL);
});
it("keeps the shortcut untouched for other modifiers and keys", async () => {
const mounted = await mountPanel();
const pre = previewText(mounted);
expect(dispatchSelectAll(pre, { ctrlKey: true, shiftKey: true }).defaultPrevented).toBe(false);
expect(dispatchSelectAll(pre, { ctrlKey: true, altKey: true }).defaultPrevented).toBe(false);
expect(dispatchSelectAll(pre, { ctrlKey: false }).defaultPrevented).toBe(false);
const other = new KeyboardEvent("keydown", { key: "c", bubbles: true, cancelable: true, ctrlKey: true });
pre.dispatchEvent(other);
expect(other.defaultPrevented).toBe(false);
expect(window.getSelection()?.rangeCount ?? 0).toBe(0);
});
it("lets the event keep bubbling so the surrounding grid can observe it", async () => {
const mounted = await mountPanel();
const pre = previewText(mounted);
const bubbled = vi.fn();
mounted.addEventListener("keydown", bubbled);
dispatchSelectAll(pre);
expect(bubbled).toHaveBeenCalledOnce();
});
});