feat(grid): support word wrap in cell detail and value editors

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-01 23:49:29 +08:00
1 parent 51f9dfc854
commit 3d6a5088f4
8 files changed
+272 -6

No files matched your search

@@ -6480,6 +6480,7 @@ watch(valueEditorContainer, async (el) => {
appPalette: editorAppPalette,
fontSize: editorFontSize,
fontFamily: detailEditorFontFamily,
lineWrapping: () => settingsStore.editorSettings.wordWrap,
});
const editor = valueDetailEditor;
await editor.create(el, detailEditValue.value, activeCellDetail.value?.type);
@@ -2,7 +2,7 @@
import DataGridCellDetailTextPreview from "@/components/grid/DataGridCellDetailTextPreview.vue";
import DataGridValueTransform from "@/components/grid/DataGridValueTransform.vue";
import { computed, nextTick, ref, watch } from "vue";
import { Code2, Copy, Download, Eye, FileUp, Info, Pencil } from "@lucide/vue";
import { Code2, Copy, Download, Eye, FileUp, Info, Pencil, WrapText } from "@lucide/vue";
import { useI18n } from "vue-i18n";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
@@ -46,9 +46,14 @@ let jsonPreviewEditor: UseCellDetailEditorReturn | null = null;
const jsonFormatted = computed(() => settingsStore.editorSettings.cellDetailJsonFormatted);
const jsonView = computed(() => jsonFormatted.value && !!props.detail?.formattedJson);
const wordWrapEnabled = computed(() => settingsStore.editorSettings.wordWrap);
const binaryTextPreview = computed(() => (props.detail && isBlobCellColumnType(props.detail.type) ? binaryCellUtf8Text(props.detail.value, props.detail.type, props.databaseType) : null));
const presentedValuePreview = computed(() => (binaryTextPreview.value === null ? props.detail?.rawValuePreview : props.detail?.displayValuePreview) ?? "");
function toggleWordWrap() {
settingsStore.updateEditorSettings({ wordWrap: !wordWrapEnabled.value });
}
function toggleJsonFormatted() {
settingsStore.updateEditorSettings({ cellDetailJsonFormatted: !jsonFormatted.value });
}
@@ -98,6 +103,7 @@ watch(jsonPreviewContainer, async (element) => {
appPalette: () => themePalette.value,
fontSize: () => settingsStore.editorSettings.fontSize,
fontFamily: () => settingsStore.editorSettings.tableFontFamily,
lineWrapping: () => settingsStore.editorSettings.wordWrap,
});
await jsonPreviewEditor.create(element, props.detail?.formattedJson ?? "", "json");
} else if (!element && jsonPreviewEditor) {
@@ -166,6 +172,18 @@ watch(
<Code2 class="h-3 w-3" />
{{ t("grid.formattedJson") }}
</Button>
<Button
variant="ghost"
size="icon"
class="h-6 w-6"
:class="wordWrapEnabled ? 'bg-sky-500/10 text-sky-700 hover:bg-sky-500/20 hover:text-sky-800 dark:text-sky-300 dark:hover:text-sky-200' : 'text-muted-foreground/70 hover:bg-muted hover:text-muted-foreground'"
:title="t('settings.wordWrap')"
:aria-label="t('settings.wordWrap')"
:aria-pressed="wordWrapEnabled"
@click="toggleWordWrap"
>
<WrapText class="h-3 w-3" />
</Button>
<Button v-if="detail.isEditable" variant="ghost" size="icon" class="h-6 w-6" :title="t('grid.editValue')" @click="emit('edit')">
<Pencil class="h-3 w-3" />
</Button>
@@ -1,8 +1,10 @@
<script setup lang="ts">
import { ChevronDown, ChevronRight, ListTree, Maximize2, PanelBottom, PanelRight, TableProperties, X } from "@lucide/vue";
import { ChevronDown, ChevronRight, ListTree, Maximize2, PanelBottom, PanelRight, TableProperties, WrapText, X } from "@lucide/vue";
import { computed } from "vue";
import { useI18n } from "vue-i18n";
import { Button } from "@/components/ui/button";
import { TabsList, TabsTrigger } from "@/components/ui/tabs";
import { useSettingsStore } from "@/stores/settingsStore";
import type { CellDetailTab } from "@/lib/dataGrid/cellDetailPresentation";
interface DataGridCellDetailHeaderProps {
@@ -23,6 +25,12 @@ const emit = defineEmits<{
}>();
const { t } = useI18n();
const settingsStore = useSettingsStore();
const wordWrapEnabled = computed(() => settingsStore.editorSettings.wordWrap);
function toggleWordWrap() {
settingsStore.updateEditorSettings({ wordWrap: !wordWrapEnabled.value });
}
</script>
<template>
@@ -51,6 +59,18 @@ const { t } = useI18n();
</TabsList>
</div>
<div class="ml-auto flex shrink-0 items-center gap-1">
<Button
variant="ghost"
size="icon"
class="h-5 w-5"
:class="wordWrapEnabled ? 'bg-sky-500/10 text-sky-700 hover:bg-sky-500/20 hover:text-sky-800 dark:text-sky-300 dark:hover:text-sky-200' : 'text-muted-foreground/70 hover:bg-muted hover:text-muted-foreground'"
:title="t('settings.wordWrap')"
:aria-label="t('settings.wordWrap')"
:aria-pressed="wordWrapEnabled"
@click="toggleWordWrap"
>
<WrapText class="w-3 h-3" />
</Button>
<Button variant="ghost" size="icon" class="h-5 w-5" :title="props.panelIsBottom ? t('grid.cellDetailLayoutRight') : t('grid.cellDetailLayoutBottom')" @click="emit('toggleLayout')">
<PanelRight v-if="props.panelIsBottom" class="w-3 h-3" />
<PanelBottom v-else class="w-3 h-3" />
@@ -39,6 +39,7 @@ watch(previewContainer, async (element) => {
appPalette: () => themePalette.value,
fontSize: () => settingsStore.editorSettings.fontSize,
fontFamily: () => settingsStore.editorSettings.tableFontFamily,
lineWrapping: () => settingsStore.editorSettings.wordWrap,
});
await previewEditor.create(element, props.text, "json");
} else if (!element && previewEditor) {
@@ -0,0 +1,217 @@
// @vitest-environment happy-dom
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { createApp, defineComponent, h, nextTick, type App } from "vue";
import { createPinia, setActivePinia } from "pinia";
import { createI18n } from "vue-i18n";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import DataGridCellDetailHeader from "../DataGridCellDetailHeader.vue";
import DataGridCellDetailDialog from "../DataGridCellDetailDialog.vue";
import { Tabs } from "@/components/ui/tabs";
import { useSettingsStore } from "@/stores/settingsStore";
import { useCellDetailEditor } from "@/composables/useCellDetailEditor";
import type { DataGridCellDetail } from "@/lib/dataGrid/dataGridDetail";
const dataGridSource = readFileSync(resolve(import.meta.dirname, "../DataGrid.vue"), "utf8");
const useDataGridCellDetailSource = readFileSync(resolve(import.meta.dirname, "../../../composables/useDataGridCellDetail.ts"), "utf8");
const cellDetailDialogSource = readFileSync(resolve(import.meta.dirname, "../DataGridCellDetailDialog.vue"), "utf8");
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
const i18n = createI18n({
legacy: false,
locale: "en",
messages: {
en: {
settings: {
wordWrap: "Word Wrap",
},
grid: {
cellDetails: "Cell Details",
hexViewer: "Hex Viewer",
valueEditor: "Value Editor",
openCellDetailsDialog: "Maximize",
openRowDetailsDialog: "Row details",
openColumnDetailsDialog: "Column details",
cellDetailLayoutRight: "Layout right",
cellDetailLayoutBottom: "Layout bottom",
collapseCellDetailMetadata: "Collapse",
expandCellDetailMetadata: "Expand",
editValue: "Edit",
copyValue: "Copy",
copyColumnName: "Copy column name",
cellValue: "Value",
formattedJson: "JSON",
},
common: {
close: "Close",
},
},
},
});
function createMockDetail(): DataGridCellDetail {
return {
rowNumber: 1,
rowId: 1,
colIndex: 0,
column: "description",
type: "TEXT",
comment: "",
value: "Line 1 that is very long and needs word wrap to be read comfortably without scrolling\nLine 2",
rawValue: "Line 1 that is very long and needs word wrap to be read comfortably without scrolling\nLine 2",
rawValuePreview: "Line 1...",
displayValue: "Line 1...",
displayValuePreview: "Line 1...",
isValuePreviewTruncated: false,
imagePreviewUrl: null,
length: 90,
formattedJson: null,
isEditable: true,
};
}
beforeEach(() => {
const pinia = createPinia();
setActivePinia(pinia);
});
afterEach(() => {
for (const { app, host } of mountedApps.splice(0)) {
app.unmount();
host.remove();
}
});
describe("DataGrid cell editor and value editor word wrap (#10506)", () => {
it("binds lineWrapping in useDataGridCellDetail, DataGrid valueDetailEditor, and cell detail dialog", () => {
// #10506: useDataGridCellDetail, DataGrid.vue (valueDetailEditor), and DataGridCellDetailDialog
// must connect CodeMirror lineWrapping to settingsStore.editorSettings.wordWrap
expect(useDataGridCellDetailSource).toMatch(/lineWrapping:\s*\(\)\s*=>\s*settingsStore\.editorSettings\.wordWrap/);
expect(dataGridSource).toMatch(/lineWrapping:\s*\(\)\s*=>\s*settingsStore\.editorSettings\.wordWrap/);
expect(cellDetailDialogSource).toMatch(/lineWrapping:\s*\(\)\s*=>\s*settingsStore\.editorSettings\.wordWrap/);
});
it("dynamically toggles CodeMirror cm-lineWrapping in useCellDetailEditor based on wordWrap setting", async () => {
const settingsStore = useSettingsStore();
settingsStore.editorSettings.wordWrap = false;
const host = document.createElement("div");
document.body.append(host);
const editor = useCellDetailEditor({
editorTheme: () => "default",
appAppearance: () => "light",
appPalette: () => ({}) as any,
fontSize: () => 13,
fontFamily: () => "monospace",
});
await editor.create(host, "A very long line of text that should wrap when wordWrap is enabled");
const cmContent = host.querySelector(".cm-content");
expect(cmContent).not.toBeNull();
expect(cmContent?.classList.contains("cm-lineWrapping")).toBe(false);
// Toggle wordWrap to true
settingsStore.editorSettings.wordWrap = true;
await nextTick();
expect(cmContent?.classList.contains("cm-lineWrapping")).toBe(true);
// Toggle wordWrap back to false
settingsStore.editorSettings.wordWrap = false;
await nextTick();
expect(cmContent?.classList.contains("cm-lineWrapping")).toBe(false);
editor.destroy();
host.remove();
});
it("renders word wrap toggle button in DataGridCellDetailHeader and updates settingsStore", async () => {
const settingsStore = useSettingsStore();
settingsStore.editorSettings.wordWrap = false;
const host = document.createElement("div");
document.body.append(host);
const Root = defineComponent({
setup() {
return () =>
h(Tabs, { defaultValue: "details" }, () =>
h(DataGridCellDetailHeader, {
metadataCollapsed: false,
panelIsBottom: false,
activeTabs: ["details", "valueEditor"],
}),
);
},
});
const app = createApp(Root);
app.use(i18n);
app.mount(host);
mountedApps.push({ app, host });
const wrapButton = host.querySelector<HTMLButtonElement>('button[aria-label="Word Wrap"]');
expect(wrapButton).not.toBeNull();
expect(wrapButton?.getAttribute("aria-pressed")).toBe("false");
wrapButton?.click();
await nextTick();
expect(settingsStore.editorSettings.wordWrap).toBe(true);
expect(wrapButton?.getAttribute("aria-pressed")).toBe("true");
wrapButton?.click();
await nextTick();
expect(settingsStore.editorSettings.wordWrap).toBe(false);
expect(wrapButton?.getAttribute("aria-pressed")).toBe("false");
});
it("renders word wrap toggle button in DataGridCellDetailDialog and updates settingsStore", async () => {
const settingsStore = useSettingsStore();
settingsStore.editorSettings.wordWrap = false;
const host = document.createElement("div");
document.body.append(host);
const detail = createMockDetail();
const Root = defineComponent({
setup() {
return () =>
h(DataGridCellDetailDialog, {
open: true,
detail,
typeColorClass: () => "",
openImagePreview: vi.fn(),
copyText: vi.fn(),
canDownloadBinaryValue: () => false,
downloadBinaryValue: vi.fn(),
canImportBinaryValue: () => false,
importBinaryValue: vi.fn(),
});
},
});
const app = createApp(Root);
app.use(i18n);
app.mount(host);
mountedApps.push({ app, host });
await nextTick();
const wrapButton = document.body.querySelector<HTMLButtonElement>('button[aria-label="Word Wrap"]');
expect(wrapButton).not.toBeNull();
expect(wrapButton?.getAttribute("aria-pressed")).toBe("false");
wrapButton?.click();
await nextTick();
expect(settingsStore.editorSettings.wordWrap).toBe(true);
expect(wrapButton?.getAttribute("aria-pressed")).toBe("true");
wrapButton?.click();
await nextTick();
expect(settingsStore.editorSettings.wordWrap).toBe(false);
expect(wrapButton?.getAttribute("aria-pressed")).toBe("false");
});
});
@@ -14,13 +14,15 @@ const mocks = vi.hoisted(() => ({
onChange: undefined as undefined | ((value: string) => void),
onSaveShortcut: undefined as undefined | ((event: KeyboardEvent) => boolean),
fontFamily: undefined as undefined | (() => string),
lineWrapping: undefined as undefined | (() => boolean),
}));
vi.mock("@/composables/useCellDetailEditor", () => ({
useCellDetailEditor: (options: { onChange?: (value: string) => void; onSaveShortcut?: (event: KeyboardEvent) => boolean; fontFamily: () => string }) => {
useCellDetailEditor: (options: { onChange?: (value: string) => void; onSaveShortcut?: (event: KeyboardEvent) => boolean; fontFamily: () => string; lineWrapping?: () => boolean }) => {
mocks.onChange = options.onChange;
mocks.onSaveShortcut = options.onSaveShortcut;
mocks.fontFamily = options.fontFamily;
mocks.lineWrapping = options.lineWrapping;
return {
create: mocks.create,
destroy: mocks.destroy,
@@ -33,7 +35,7 @@ vi.mock("@/composables/useCellDetailEditor", () => ({
}));
vi.mock("@/composables/useTheme", () => ({ useTheme: () => ({ isDark: ref(false), themePalette: ref({}) }) }));
vi.mock("@/stores/settingsStore", () => ({
useSettingsStore: () => ({ editorSettings: { theme: "default", fontSize: 13, fontFamily: "monospace", tableFontFamily: "'Grid Font', sans-serif" } }),
useSettingsStore: () => ({ editorSettings: { theme: "default", fontSize: 13, fontFamily: "monospace", tableFontFamily: "'Grid Font', sans-serif", wordWrap: true } }),
}));
vi.mock("@/lib/dataGrid/geometryPreview", () => ({ renderWktOnCanvas: vi.fn() }));
@@ -65,6 +67,7 @@ beforeEach(() => {
mocks.onChange = undefined;
mocks.onSaveShortcut = undefined;
mocks.fontFamily = undefined;
mocks.lineWrapping = undefined;
mocks.getValue.mockReturnValue("");
});
@@ -79,6 +82,7 @@ describe("useDataGridCellDetail", () => {
expect(mocks.focus).toHaveBeenCalledOnce();
expect(mocks.fontFamily?.()).toBe("'Grid Font', sans-serif");
expect(mocks.lineWrapping?.()).toBe(true);
composable.detailsEditorContainer.value = undefined;
await nextTick();
@@ -107,6 +107,10 @@ export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCel
return typeof options.readOnly === "function" ? options.readOnly() : Boolean(options.readOnly);
}
function isLineWrapping(): boolean {
return typeof options.lineWrapping === "function" ? options.lineWrapping() : settingsStore.editorSettings.wordWrap;
}
function readOnlyExtensions(readOnly: boolean) {
return [EditorState.readOnly.of(readOnly), EditorView.editable.of(!readOnly), EditorView.contentAttributes.of(readOnly ? { tabindex: "0" } : {})];
}
@@ -173,7 +177,7 @@ export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCel
});
watch(
() => options.lineWrapping?.() ?? false,
() => isLineWrapping(),
(lineWrapping) => {
const editor = view.value;
if (!editor || destroyed) return;
@@ -250,7 +254,7 @@ export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCel
]),
]),
languageComp.of(currentIsJson ? json() : []),
lineWrappingComp.of(options.lineWrapping?.() ? EditorView.lineWrapping : []),
lineWrappingComp.of(isLineWrapping() ? EditorView.lineWrapping : []),
readOnlyComp.of(readOnlyExtensions(isReadOnly())),
themeComp.of(theme),
fontThemeComp.of(fontTheme),
@@ -22,6 +22,7 @@ export function useDataGridCellDetail(options: { detail: Ref<DataGridCellDetail>
appPalette: () => themePalette.value,
fontSize: () => settingsStore.editorSettings.fontSize,
fontFamily: () => settingsStore.editorSettings.tableFontFamily,
lineWrapping: () => settingsStore.editorSettings.wordWrap,
});
watch(geometryPreviewOpen, async (open) => {