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。

feat(grid): JSON value editor node folding with consistent SVG markers

lxk9556452f18c3689
diff --git a/apps/desktop/src/components/grid/__tests__/DataGridCellDetailFolding.spec.ts b/apps/desktop/src/components/grid/__tests__/DataGridCellDetailFolding.spec.ts
index 4f4040788a..aa4d4193fb 100644
--- a/apps/desktop/src/components/grid/__tests__/DataGridCellDetailFolding.spec.ts
+++ b/apps/desktop/src/components/grid/__tests__/DataGridCellDetailFolding.spec.ts
@@ -58,6 +58,9 @@ describe("DataGrid cell detail code folding and line numbers (#10431)", () => {
const foldGutter = host.querySelector(".cm-foldGutter");
expect(foldGutter).not.toBeNull();
+ const foldMarkers = foldGutter?.querySelectorAll(".cm-foldMarker-svg");
+ expect(foldMarkers?.length).toBeGreaterThan(0);
+ expect(foldGutter?.querySelector(".cm-foldMarker-svg svg")).not.toBeNull();
// Verify folding interaction on the CodeMirror EditorView
const cmView = editor.view.value;
@@ -74,4 +77,82 @@ describe("DataGrid cell detail code folding and line numbers (#10431)", () => {
editor.destroy();
host.remove();
});
+
+ it("enables code folding and mounts SVG fold markers for JSON in non-json column types like nvarchar(max) (#11834)", async () => {
+ const pinia = createPinia();
+ setActivePinia(pinia);
+
+ const host = document.createElement("div");
+ document.body.append(host);
+
+ const editor = useCellDetailEditor({
+ folding: true,
+ lineNumbers: true,
+ editorTheme: () => "default",
+ appAppearance: () => "light",
+ appPalette: () => ({}) as any,
+ fontSize: () => 13,
+ fontFamily: () => "monospace",
+ });
+
+ const jsonDoc = `{\n "data": {\n "pens": [\n { "text": "Begin" }\n ]\n }\n}`;
+ await editor.create(host, jsonDoc, "nvarchar(max)");
+
+ const foldGutter = host.querySelector(".cm-foldGutter");
+ expect(foldGutter).not.toBeNull();
+ const foldMarkers = foldGutter?.querySelectorAll(".cm-foldMarker-svg");
+ expect(foldMarkers?.length).toBeGreaterThan(0);
+
+ const cmView = editor.view.value;
+ expect(cmView).not.toBeNull();
+ if (cmView) {
+ expect(foldCode(cmView)).toBe(true);
+ expect(unfoldCode(cmView)).toBe(true);
+ }
+
+ editor.destroy();
+ host.remove();
+ });
+
+ it("supports code folding on large JSON data exceeding 50KB (#11834)", async () => {
+ const pinia = createPinia();
+ setActivePinia(pinia);
+
+ const host = document.createElement("div");
+ document.body.append(host);
+
+ const editor = useCellDetailEditor({
+ folding: true,
+ lineNumbers: true,
+ editorTheme: () => "default",
+ appAppearance: () => "light",
+ appPalette: () => ({}) as any,
+ fontSize: () => 13,
+ fontFamily: () => "monospace",
+ });
+
+ // Generate JSON > 50KB
+ const items = Array.from({ length: 1500 }, (_, i) => ({ id: i, label: `item-${i}` }));
+ const largeJson = JSON.stringify({ items }, null, 2);
+ expect(largeJson.length).toBeGreaterThan(50_000);
+
+ await editor.create(host, largeJson, "nvarchar(max)");
+
+ const foldGutter = host.querySelector(".cm-foldGutter");
+ expect(foldGutter).not.toBeNull();
+ const foldMarkers = foldGutter?.querySelectorAll(".cm-foldMarker-svg");
+ expect(foldMarkers?.length).toBeGreaterThan(0);
+
+ const cmView = editor.view.value;
+ expect(cmView).not.toBeNull();
+ if (cmView) {
+ const line3 = cmView.state.doc.line(3);
+ cmView.dispatch({ selection: { anchor: line3.from } });
+ expect(foldCode(cmView)).toBe(true);
+ expect(unfoldCode(cmView)).toBe(true);
+ }
+
+ editor.destroy();
+ host.remove();
+ });
});
diff --git a/apps/desktop/src/composables/useCellDetailEditor.ts b/apps/desktop/src/composables/useCellDetailEditor.ts
index e7ad019993..4936372982 100644
--- a/apps/desktop/src/composables/useCellDetailEditor.ts
+++ b/apps/desktop/src/composables/useCellDetailEditor.ts
@@ -4,14 +4,15 @@ import { EditorView, keymap, drawSelection, dropCursor, highlightSpecialChars, h
import { json } from "@codemirror/lang-json";
import { search as cmSearch } from "@codemirror/search";
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
-import { bracketMatching, foldGutter, foldKeymap } from "@codemirror/language";
+import { bracketMatching, foldKeymap } from "@codemirror/language";
+import { createConsistentFoldGutter } from "@/lib/editor/codemirrorFoldGutter";
import { trimmedSelectionLayer } from "@/lib/editor/codemirrorTrimmedSelectionLayer";
import { EDITOR_FONT_FAMILY_CSS_VAR, EDITOR_FONT_SIZE_CSS_VAR, cellDetailActiveLineColor, loadEditorTheme, editorFontTheme } from "@/lib/editor/editorThemes";
import { editorClipboardLineEndingsExtension } from "@/lib/editor/editorClipboardLineEndings";
import { matchesShortcut } from "@/lib/editor/keyboardShortcuts";
import { shortcutToCodeMirrorKey } from "@/lib/editor/shortcutRegistry";
import { useSettingsStore } from "@/stores/settingsStore";
-import { CELL_DETAIL_JSON_FORMAT_MAX_LENGTH, isJsonColumnType } from "@/lib/dataGrid/cellDetailPresentation";
+import { isJsonColumnType } from "@/lib/dataGrid/cellDetailPresentation";
import { clampEditorFontSize, createEditorWheelZoomGestureGuard, createEditorZoomCommitScheduler, fontSizeFromGestureScale, fontSizeFromWheelDelta } from "@/lib/editor/editorZoom";
import i18n from "@/i18n";
import EditorSearchPanel from "@/components/editor/EditorSearchPanel.vue";
@@ -66,8 +67,10 @@ function looksLikeJsonString(text: string): boolean {
return trimmed.startsWith("{") || trimmed.startsWith("[");
}
+export const CELL_DETAIL_JSON_MODE_MAX_LENGTH = 2_000_000;
+
function shouldUseJsonMode(columnType?: string, value?: string): boolean {
- if (value && value.length > CELL_DETAIL_JSON_FORMAT_MAX_LENGTH) return false;
+ if (value && value.length > CELL_DETAIL_JSON_MODE_MAX_LENGTH) return false;
if (isJsonColumnType(columnType)) return true;
if (value && looksLikeJsonString(value)) return true;
return false;
@@ -230,7 +233,7 @@ export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCel
}),
// Keep the compact detail-editor baseline; gutters and wrapping are opt-in.
...(options.lineNumbers ? [lineNumbers(), highlightActiveLineGutter()] : []),
- ...(options.folding ? [foldGutter()] : []),
+ ...(options.folding ? [createConsistentFoldGutter()] : []),
highlightSpecialChars(),
history(),
drawSelection(),
diff --git a/apps/desktop/src/lib/__tests__/editorThemes.spec.ts b/apps/desktop/src/lib/__tests__/editorThemes.spec.ts
index 7e0fb054c3..c5fb876cc2 100644
--- a/apps/desktop/src/lib/__tests__/editorThemes.spec.ts
+++ b/apps/desktop/src/lib/__tests__/editorThemes.spec.ts
@@ -318,6 +318,24 @@ describe("editor gutters", () => {
justifyContent: "flex-end",
});
});
+
+ it("styles the fold gutter and SVG fold markers consistently (#11834)", () => {
+ const rules = buildEditorFontThemeRules();
+
+ expect(rules[".cm-foldGutter"]).toMatchObject({
+ width: "16px",
+ });
+ expect(rules[".cm-foldGutter .cm-gutterElement"]).toMatchObject({
+ alignItems: "center",
+ display: "flex",
+ justifyContent: "center",
+ });
+ expect(rules[".cm-foldMarker-svg"]).toMatchObject({
+ display: "inline-flex",
+ width: "16px",
+ height: "16px",
+ });
+ });
});
describe("editor font theme", () => {
diff --git a/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts b/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts
index e831bfe7fc..4f54d5d190 100644
--- a/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts
+++ b/apps/desktop/src/lib/dataGrid/cellDetailPresentation.ts
@@ -3,7 +3,7 @@ import { safeJsonFormat } from "@/lib/common/safeJsonFormat";
export type CellDetailTab = "details" | "hexViewer" | "valueEditor";
export type ValueEditorAction = "formatJson" | "compactJson" | "setNull" | "restoreOriginal";
-export const CELL_DETAIL_JSON_FORMAT_MAX_LENGTH = 50_000;
+export const CELL_DETAIL_JSON_FORMAT_MAX_LENGTH = 500_000;
export interface CellDetailPresentationOptions {
isEditable: boolean;
diff --git a/apps/desktop/src/lib/editor/codemirrorFoldGutter.ts b/apps/desktop/src/lib/editor/codemirrorFoldGutter.ts
new file mode 100644
index 0000000000..f061f2a51a
--- /dev/null
+++ b/apps/desktop/src/lib/editor/codemirrorFoldGutter.ts
@@ -0,0 +1,20 @@
+import { foldGutter } from "@codemirror/language";
+import type { Extension } from "@codemirror/state";
+
+export function createFoldMarkerDom(open: boolean): HTMLElement {
+ const span = document.createElement("span");
+ span.className = "cm-foldMarker-svg";
+ span.setAttribute("role", "button");
+ span.setAttribute("aria-label", open ? "Fold line" : "Unfold line");
+ span.innerHTML = open
+ ? '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M4.5 6.5l3.5 3.5 3.5-3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>'
+ : '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M6.5 4.5l3.5 3.5-3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>';
+ return span;
+}
+
+export function createConsistentFoldGutter(options?: Parameters<typeof foldGutter>[0]): Extension {
+ return foldGutter({
+ markerDOM: createFoldMarkerDom,
+ ...options,
+ });
+}
diff --git a/apps/desktop/src/lib/editor/editorThemes.ts b/apps/desktop/src/lib/editor/editorThemes.ts
index 418e934548..28a3c2eed4 100644
--- a/apps/desktop/src/lib/editor/editorThemes.ts
+++ b/apps/desktop/src/lib/editor/editorThemes.ts
@@ -867,6 +867,36 @@ export function buildEditorFontThemeRules(opts?: { fixedHeight?: boolean; scroll
paddingRight: "8px",
userSelect: "none",
},
+ ".cm-foldGutter": {
+ width: "16px",
+ },
+ ".cm-foldGutter .cm-gutterElement": {
+ alignItems: "center",
+ cursor: "pointer",
+ display: "flex",
+ justifyContent: "center",
+ padding: "0",
+ userSelect: "none",
+ },
+ ".cm-foldMarker-svg": {
+ alignItems: "center",
+ color: "var(--muted-foreground)",
+ display: "inline-flex",
+ height: "16px",
+ justifyContent: "center",
+ opacity: "0.65",
+ transition: "opacity 0.15s",
+ verticalAlign: "middle",
+ width: "16px",
+ },
+ ".cm-foldMarker-svg:hover": {
+ opacity: "0.95",
+ },
+ ".cm-foldMarker-svg svg": {
+ display: "block",
+ height: "16px",
+ width: "16px",
+ },
".cm-run-statement-gutter": {
minWidth: "28px",
},
diff --git a/packages/app-tests/dataGridDetail.test.ts b/packages/app-tests/dataGridDetail.test.ts
index da0234ab99..885ae3312b 100644
--- a/packages/app-tests/dataGridDetail.test.ts
+++ b/packages/app-tests/dataGridDetail.test.ts
@@ -1,7 +1,18 @@
import assert from "node:assert/strict";
import { test } from "vitest";
-import { buildDataGridCellDetail, buildDataGridColumnDetail, dataGridColumnDetailJson, dataGridColumnDetailTsv, buildDataGridRowDetail, dataGridRowDetailJson, dataGridRowDetailTsv, filterDataGridDetailFields, jsonDetailDisplayValue, type DataGridCellDetail } from "../../apps/desktop/src/lib/dataGrid/dataGridDetail.ts";
+import {
+ buildDataGridCellDetail,
+ buildDataGridColumnDetail,
+ dataGridColumnDetailJson,
+ dataGridColumnDetailTsv,
+ buildDataGridRowDetail,
+ dataGridRowDetailJson,
+ dataGridRowDetailTsv,
+ filterDataGridDetailFields,
+ jsonDetailDisplayValue,
+ type DataGridCellDetail,
+} from "../../apps/desktop/src/lib/dataGrid/dataGridDetail.ts";
import type { CellValue } from "../../apps/desktop/src/lib/dataGrid/cellValue.ts";
test("buildDataGridCellDetail returns null for an invalid column", () => {
@@ -144,7 +155,7 @@ test("aggregate details defer binary image preview generation", () => {
});
test("buildDataGridCellDetail limits rendered previews for huge text values", () => {
- const hugeJson = `{"body":"${"x".repeat(120_000)}"}`;
+ const hugeJson = `{"body":"${"x".repeat(600_000)}"}`;
const detail = buildDataGridCellDetail({
rowIndex: 0,
rowId: 1,
@@ -164,6 +175,24 @@ test("buildDataGridCellDetail limits rendered previews for huge text values", ()
assert.equal(detail.formattedJson, "");
});
+test("buildDataGridCellDetail formats large JSON values within the format limit", () => {
+ const largeJson = `{"body":"${"x".repeat(120_000)}"}`;
+ const detail = buildDataGridCellDetail({
+ rowIndex: 0,
+ rowId: 1,
+ row: [largeJson],
+ columns: ["payload"],
+ columnIndex: 0,
+ typeByColumn: new Map([["payload", "nvarchar(max)"]]),
+ displayValue: (value) => String(value),
+ isEditable: false,
+ });
+
+ assert.ok(detail);
+ assert.notEqual(detail.formattedJson, "");
+ assert.ok(detail.formattedJson.startsWith('{\n "body":'));
+});
+
test("buildDataGridRowDetail maps requested column indexes in order", () => {
const row: CellValue[] = ["Ada", null, true];
const detail = buildDataGridRowDetail({
@@ -273,10 +302,7 @@ test("dataGridRowDetailJson uses the original MongoDB document for nested values
});
test("jsonDetailDisplayValue expands JSON stored in MongoDB string fields", () => {
- assert.deepEqual(
- jsonDetailDisplayValue({ imparts: '{"insImpart":[{"impartId":"40-936","impartAnswer":"N,,"}]}' }),
- { imparts: { insImpart: [{ impartId: "40-936", impartAnswer: "N,," }] } },
- );
+ assert.deepEqual(jsonDetailDisplayValue({ imparts: '{"insImpart":[{"impartId":"40-936","impartAnswer":"N,,"}]}' }), { imparts: { insImpart: [{ impartId: "40-936", impartAnswer: "N,," }] } });
});
test("buildDataGridColumnDetail maps a whole column across rows", () => {
const typeByColumn = new Map([["name", "varchar"]]);