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): smooth sql zoom interactions

t8y23ad19107d832
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index a79c15680d..758ab3241e 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -24,7 +24,19 @@ import {
} from "@/lib/sqlCompletion";
import { extractIdentifierAt, isSqlKeyword, matchTable } from "@/lib/sqlNavigation";
import { lineColumnToOffset, parseSqlErrorLocation } from "@/lib/sqlDiagnostics";
-import { loadEditorTheme, editorFontTheme, sqlCompletionTheme } from "@/lib/editorThemes";
+import {
+ EDITOR_FONT_FAMILY_CSS_VAR,
+ EDITOR_FONT_SIZE_CSS_VAR,
+ loadEditorTheme,
+ editorFontTheme,
+ sqlCompletionTheme,
+} from "@/lib/editorThemes";
+import {
+ clampEditorFontSize,
+ createEditorZoomCommitScheduler,
+ fontSizeFromGestureScale,
+ fontSizeFromWheelDelta,
+} from "@/lib/editorZoom";
import { shortcutToCodeMirrorKey } from "@/lib/shortcutRegistry";
import type { SqlCompletionColumn } from "@/lib/sqlCompletion";
@@ -56,9 +68,10 @@ const editorRef = ref<HTMLDivElement>();
const view = shallowRef<EditorViewType | null>(null);
const connectionStore = useConnectionStore();
const settingsStore = useSettingsStore();
-const MIN_FONT_SIZE = 10;
-const MAX_FONT_SIZE = 24;
const MAX_COMPLETION_TABLES = 200;
+const liveFontSize = ref(settingsStore.editorSettings.fontSize);
+const gestureStartFontSize = ref(settingsStore.editorSettings.fontSize);
+const isGestureZooming = ref(false);
const searchVisible = ref(false);
const searchText = ref("");
@@ -70,6 +83,10 @@ const matchCount = ref(0);
const currentMatchIndex = ref(0);
const searchInputRef = ref<HTMLInputElement>();
+interface EditorGestureEvent extends Event {
+ scale?: number;
+}
+
let editorViewModule: typeof import("@codemirror/view") | null = null;
let fontThemeComp: import("@codemirror/state").Compartment | null = null;
let codeMirrorTheme: import("@codemirror/state").Compartment | null = null;
@@ -86,35 +103,64 @@ let cachedTables: Array<{ name: string; schema?: string; type?: "table" | "view"
// Persistent column cache keyed by "schema.table" or "table"
const cachedColumnsByTable = new Map<string, SqlCompletionColumn[]>();
-function setFontSize(size: number) {
- const next = Math.min(MAX_FONT_SIZE, Math.max(MIN_FONT_SIZE, size));
- const ss = settingsStore.editorSettings;
- ss.fontSize = next;
+const zoomCommitScheduler = createEditorZoomCommitScheduler((fontSize) => {
+ if (settingsStore.editorSettings.fontSize === fontSize) return;
+ settingsStore.updateEditorSettings({ fontSize });
+});
+
+function syncEditorFontCssVars(fontSize = liveFontSize.value, fontFamily = settingsStore.editorSettings.fontFamily) {
+ if (!editorRef.value) return;
+ editorRef.value.style.setProperty(EDITOR_FONT_SIZE_CSS_VAR, `${clampEditorFontSize(fontSize)}px`);
+ editorRef.value.style.setProperty(EDITOR_FONT_FAMILY_CSS_VAR, fontFamily);
+}
+
+function applyLiveFontSize(size: number) {
+ const next = clampEditorFontSize(size);
+ if (liveFontSize.value === next) return;
+ liveFontSize.value = next;
+ syncEditorFontCssVars(next);
+ view.value?.requestMeasure();
+}
+
+function commitFontSize(size: number) {
+ const next = clampEditorFontSize(size);
+ applyLiveFontSize(next);
+ if (settingsStore.editorSettings.fontSize === next) return;
settingsStore.updateEditorSettings({ fontSize: next });
- if (view.value && fontThemeComp && editorViewModule) {
- view.value.dispatch({
- effects: [
- fontThemeComp.reconfigure(
- editorFontTheme(editorViewModule.EditorView, next, ss.fontFamily, {
- fixedHeight: true,
- scrollable: true,
- }),
- ),
- ],
- });
- }
+}
+
+function scheduleFontSizeCommit(size: number) {
+ zoomCommitScheduler.schedule(size);
}
function zoomIn() {
- setFontSize(settingsStore.editorSettings.fontSize + 1);
+ commitFontSize(liveFontSize.value + 1);
}
function zoomOut() {
- setFontSize(settingsStore.editorSettings.fontSize - 1);
+ commitFontSize(liveFontSize.value - 1);
}
function resetZoom() {
- setFontSize(13);
+ commitFontSize(13);
+}
+
+function onEditorGestureStart(event: EditorGestureEvent) {
+ event.preventDefault();
+ isGestureZooming.value = true;
+ gestureStartFontSize.value = liveFontSize.value;
+}
+
+function onEditorGestureChange(event: EditorGestureEvent) {
+ if (typeof event.scale !== "number") return;
+ event.preventDefault();
+ applyLiveFontSize(fontSizeFromGestureScale(gestureStartFontSize.value, event.scale));
+}
+
+function onEditorGestureEnd(event: Event) {
+ event.preventDefault();
+ isGestureZooming.value = false;
+ zoomCommitScheduler.flush(liveFontSize.value);
}
function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view"))["keymap"]) {
@@ -654,7 +700,7 @@ onMounted(async () => {
}
}),
fontThemeComp.of(
- editorFontTheme(EditorView, ss.fontSize, ss.fontFamily, {
+ editorFontTheme(EditorView, liveFontSize.value, ss.fontFamily, {
fixedHeight: true,
scrollable: true,
}),
@@ -663,8 +709,9 @@ onMounted(async () => {
wheel(event) {
if (!event.metaKey && !event.ctrlKey) return false;
event.preventDefault();
- if (event.deltaY < 0) zoomIn();
- else if (event.deltaY > 0) zoomOut();
+ const next = fontSizeFromWheelDelta(liveFontSize.value, event.deltaY);
+ applyLiveFontSize(next);
+ scheduleFontSizeCommit(next);
return true;
},
mousedown: (event: MouseEvent) => {
@@ -802,6 +849,7 @@ onMounted(async () => {
});
view.value = new EditorView({ state, parent: editorRef.value });
+ syncEditorFontCssVars(liveFontSize.value, ss.fontFamily);
cachedTables = [];
});
@@ -865,18 +913,16 @@ watch(
if (!view.value || !codeMirrorTheme || !fontThemeComp || !wordWrapComp || !runKeymapComp || !editorViewModule) {
return;
}
+ if (!isGestureZooming.value && !zoomCommitScheduler.hasPendingCommit() && liveFontSize.value !== ss.fontSize) {
+ liveFontSize.value = ss.fontSize;
+ }
+ syncEditorFontCssVars(liveFontSize.value, ss.fontFamily);
const themeExt = await loadEditorTheme(ss.theme);
view.value.dispatch({
effects: [
codeMirrorTheme.reconfigure(themeExt),
wordWrapComp.reconfigure(props.forceWordWrap || ss.wordWrap ? editorViewModule.EditorView.lineWrapping : []),
runKeymapComp.reconfigure(runKeymapExtension(editorViewModule.keymap)),
- fontThemeComp.reconfigure(
- editorFontTheme(editorViewModule.EditorView, ss.fontSize, ss.fontFamily, {
- fixedHeight: true,
- scrollable: true,
- }),
- ),
],
});
},
@@ -884,6 +930,7 @@ watch(
);
onBeforeUnmount(() => {
+ zoomCommitScheduler.dispose();
view.value?.destroy();
});
@@ -1013,7 +1060,12 @@ defineExpose({ openSearch });
</script>
<template>
- <div class="h-full w-full overflow-hidden relative">
+ <div
+ class="h-full w-full overflow-hidden relative"
+ @gesturestart="onEditorGestureStart"
+ @gesturechange="onEditorGestureChange"
+ @gestureend="onEditorGestureEnd"
+ >
<div ref="editorRef" data-query-editor-root class="h-full w-full overflow-hidden" />
<Transition
enter-active-class="transition-all duration-150"
diff --git a/apps/desktop/src/lib/editorThemes.ts b/apps/desktop/src/lib/editorThemes.ts
index 1bba0dc6e2..7d1510742d 100644
--- a/apps/desktop/src/lib/editorThemes.ts
+++ b/apps/desktop/src/lib/editorThemes.ts
@@ -3,6 +3,9 @@ import type { EditorTheme } from "@/stores/settingsStore";
type CodeMirrorStyleSpec = Parameters<typeof import("@codemirror/view").EditorView.theme>[0];
+export const EDITOR_FONT_SIZE_CSS_VAR = "--dbx-editor-font-size";
+export const EDITOR_FONT_FAMILY_CSS_VAR = "--dbx-editor-font-family";
+
/** Load a CodeMirror theme extension by theme name. */
export async function loadEditorTheme(theme: EditorTheme): Promise<Extension> {
switch (theme) {
@@ -29,22 +32,22 @@ export async function loadEditorTheme(theme: EditorTheme): Promise<Extension> {
}
}
-/** Build a CodeMirror theme extension for font size + font family. */
-export function editorFontTheme(
- EditorView: typeof import("@codemirror/view").EditorView,
- size: number,
- family: string,
+export function buildEditorFontThemeRules(
opts?: { fixedHeight?: boolean; scrollable?: boolean },
-): Extension {
- return EditorView.theme({
+ defaults?: { size?: number; family?: string },
+): CodeMirrorStyleSpec {
+ return {
"&": {
...(opts?.fixedHeight ? { height: "100%" } : {}),
- fontSize: `${size}px`,
+ fontSize: `var(${EDITOR_FONT_SIZE_CSS_VAR}, ${defaults?.size ?? 13}px)`,
},
...(opts?.scrollable ? { ".cm-scroller": { overflow: "auto" } } : {}),
- ".cm-content": { fontFamily: family },
+ ".cm-content": {
+ fontFamily: `var(${EDITOR_FONT_FAMILY_CSS_VAR}, ${defaults?.family ?? "monospace"})`,
+ },
".cm-gutters": {
borderRight: "0 !important",
+ fontSize: `var(${EDITOR_FONT_SIZE_CSS_VAR}, ${defaults?.size ?? 13}px)`,
position: "relative",
},
".cm-gutters:after": {
@@ -61,7 +64,17 @@ export function editorFontTheme(
".cm-lineNumbers .cm-gutterElement": {
paddingRight: "16px",
},
- });
+ };
+}
+
+/** Build a CodeMirror theme extension for font size + font family. */
+export function editorFontTheme(
+ EditorView: typeof import("@codemirror/view").EditorView,
+ size: number,
+ family: string,
+ opts?: { fixedHeight?: boolean; scrollable?: boolean },
+): Extension {
+ return EditorView.theme(buildEditorFontThemeRules(opts, { size, family }));
}
export function buildSqlCompletionThemeRules(): CodeMirrorStyleSpec {
diff --git a/apps/desktop/src/lib/editorZoom.ts b/apps/desktop/src/lib/editorZoom.ts
new file mode 100644
index 0000000000..e609d33999
--- /dev/null
+++ b/apps/desktop/src/lib/editorZoom.ts
@@ -0,0 +1,52 @@
+export const EDITOR_MIN_FONT_SIZE = 10;
+export const EDITOR_MAX_FONT_SIZE = 24;
+const WHEEL_FONT_ZOOM_SENSITIVITY = 0.0015;
+
+export function clampEditorFontSize(value: number): number {
+ const clamped = Math.min(EDITOR_MAX_FONT_SIZE, Math.max(EDITOR_MIN_FONT_SIZE, value));
+ return Number(clamped.toFixed(2));
+}
+
+export function fontSizeFromWheelDelta(currentFontSize: number, deltaY: number): number {
+ return clampEditorFontSize(currentFontSize * Math.exp(-deltaY * WHEEL_FONT_ZOOM_SENSITIVITY));
+}
+
+export function fontSizeFromGestureScale(startFontSize: number, scale: number): number {
+ return clampEditorFontSize(startFontSize * scale);
+}
+
+export function createEditorZoomCommitScheduler(commit: (fontSize: number) => void, delayMs = 160) {
+ let timer: ReturnType<typeof setTimeout> | null = null;
+ let pendingFontSize: number | null = null;
+
+ const clearPending = () => {
+ if (timer) clearTimeout(timer);
+ timer = null;
+ };
+
+ return {
+ schedule(fontSize: number) {
+ pendingFontSize = clampEditorFontSize(fontSize);
+ clearPending();
+ timer = setTimeout(() => {
+ timer = null;
+ const next = pendingFontSize;
+ pendingFontSize = null;
+ if (next !== null) commit(next);
+ }, delayMs);
+ },
+ flush(fontSize?: number) {
+ const next = clampEditorFontSize(fontSize ?? pendingFontSize ?? NaN);
+ clearPending();
+ pendingFontSize = null;
+ if (!Number.isNaN(next)) commit(next);
+ },
+ hasPendingCommit() {
+ return timer !== null;
+ },
+ dispose() {
+ clearPending();
+ pendingFontSize = null;
+ },
+ };
+}
diff --git a/packages/app-tests/editorCompletionTheme.test.ts b/packages/app-tests/editorCompletionTheme.test.ts
index e5ba2daa3b..80f9b66b73 100644
--- a/packages/app-tests/editorCompletionTheme.test.ts
+++ b/packages/app-tests/editorCompletionTheme.test.ts
@@ -1,6 +1,11 @@
import { strict as assert } from "node:assert";
import test from "node:test";
-import { buildSqlCompletionThemeRules } from "../../apps/desktop/src/lib/editorThemes.ts";
+import {
+ buildEditorFontThemeRules,
+ buildSqlCompletionThemeRules,
+ EDITOR_FONT_FAMILY_CSS_VAR,
+ EDITOR_FONT_SIZE_CSS_VAR,
+} from "../../apps/desktop/src/lib/editorThemes.ts";
test("sql completion theme styles the autocomplete popup", () => {
const rules = buildSqlCompletionThemeRules();
@@ -26,3 +31,15 @@ test("sql completion theme styles the autocomplete popup", () => {
"rgba(70, 75, 84, 0.86) !important",
);
});
+
+test("editor font theme reads size and family from CSS variables", () => {
+ const rules = buildEditorFontThemeRules({ fixedHeight: true, scrollable: true });
+
+ assert.equal(rules["&"]?.height, "100%");
+ assert.equal(rules["&"]?.fontSize, `var(${EDITOR_FONT_SIZE_CSS_VAR}, 13px)`);
+ assert.deepEqual(rules[".cm-content"], {
+ fontFamily: `var(${EDITOR_FONT_FAMILY_CSS_VAR}, monospace)`,
+ });
+ assert.equal(rules[".cm-gutters"]?.fontSize, `var(${EDITOR_FONT_SIZE_CSS_VAR}, 13px)`);
+ assert.deepEqual(rules[".cm-scroller"], { overflow: "auto" });
+});
diff --git a/packages/app-tests/editorZoom.test.ts b/packages/app-tests/editorZoom.test.ts
new file mode 100644
index 0000000000..b3b7596700
--- /dev/null
+++ b/packages/app-tests/editorZoom.test.ts
@@ -0,0 +1,47 @@
+import { strict as assert } from "node:assert";
+import test from "node:test";
+import {
+ clampEditorFontSize,
+ createEditorZoomCommitScheduler,
+ fontSizeFromGestureScale,
+ fontSizeFromWheelDelta,
+} from "../../apps/desktop/src/lib/editorZoom.ts";
+
+test("clamps editor font size to supported bounds", () => {
+ assert.equal(clampEditorFontSize(8), 10);
+ assert.equal(clampEditorFontSize(30), 24);
+ assert.equal(clampEditorFontSize(13.257), 13.26);
+});
+
+test("maps trackpad pinch wheel delta to smooth font size changes", () => {
+ assert.ok(fontSizeFromWheelDelta(13, -120) > 13);
+ assert.ok(fontSizeFromWheelDelta(13, 120) < 13);
+});
+
+test("maps WebKit gesture scale from the gesture start font size", () => {
+ assert.equal(fontSizeFromGestureScale(13, 1.25), 16.25);
+ assert.equal(fontSizeFromGestureScale(13, 4), 24);
+});
+
+test("debounces editor zoom commits and keeps only the latest font size", async () => {
+ const committed: number[] = [];
+ const scheduler = createEditorZoomCommitScheduler((fontSize) => committed.push(fontSize), 10);
+
+ scheduler.schedule(13.5);
+ scheduler.schedule(14.25);
+
+ await new Promise((resolve) => setTimeout(resolve, 25));
+
+ assert.deepEqual(committed, [14.25]);
+});
+
+test("flushes a pending editor zoom commit immediately", () => {
+ const committed: number[] = [];
+ const scheduler = createEditorZoomCommitScheduler((fontSize) => committed.push(fontSize), 50);
+
+ scheduler.schedule(15.75);
+ scheduler.flush();
+
+ assert.deepEqual(committed, [15.75]);
+ assert.equal(scheduler.hasPendingCommit(), false);
+});
diff --git a/packages/app-tests/queryEditorZoomMeasure.test.ts b/packages/app-tests/queryEditorZoomMeasure.test.ts
new file mode 100644
index 0000000000..84e2491ea8
--- /dev/null
+++ b/packages/app-tests/queryEditorZoomMeasure.test.ts
@@ -0,0 +1,9 @@
+import { readFileSync } from "node:fs";
+import { strict as assert } from "node:assert";
+import test from "node:test";
+
+const source = readFileSync("apps/desktop/src/components/editor/QueryEditor.vue", "utf8");
+
+test("query editor requests a fresh CodeMirror measure after live zoom updates", () => {
+ assert.match(source, /requestMeasure\(\)/);
+});