feat(grid): compare original and edited JSON values

Closes #1638
This commit is contained in:
t8y2 committed 2026-08-24 23:25:24 +00:00
1 parent b3e8709272
commit efba56c8c0
22 files changed
+814 -339

No files matched your search

+40 -1
View File
@@ -151,6 +151,7 @@ import {
} from "@/lib/dataGrid/binaryCellDownload";
import { buildBinaryHexViewRows } from "@/lib/dataGrid/binaryHexViewer";
import { canFormatCellDetailJson, cellDetailEditorText, compactJsonText, defaultCellDetailTab, formatJsonText, isGeometryColumnType, linkedCellDetailTarget, looksLikeJsonContainerText, valueEditorActions, visibleCellDetailTabs, type CellDetailTab } from "@/lib/dataGrid/cellDetailPresentation";
import { createJsonValueDiffSnapshot, isJsonValueDiffAvailable, type JsonValueDiffContext, type JsonValueDiffSnapshot } from "@/lib/dataGrid/jsonValueDiff";
import {
buildDataGridCellDetail,
buildDataGridColumnDetail,
@@ -361,6 +362,7 @@ dayjs.extend(timezone);
const SqlPreviewPanel = defineAsyncComponent(() => import("@/components/editor/SqlPreviewPanel.vue"));
const ImagePreviewDialog = defineAsyncComponent(() => import("@/components/grid/ImagePreviewDialog.vue"));
const DataGridCellDetailDialog = defineAsyncComponent(() => import("@/components/grid/DataGridCellDetailDialog.vue"));
const DataGridValueDiffDialog = defineAsyncComponent(() => import("@/components/grid/DataGridValueDiffDialog.vue"));
const DataGridMongoJsonPreview = defineAsyncComponent(() => import("@/components/grid/DataGridMongoJsonPreview.vue"));
const DataGridDetailDialogs = defineAsyncComponent(() => import("@/components/grid/DataGridDetailDialogs.vue"));
const DataGridBulkEditDialog = defineAsyncComponent(() => import("@/components/grid/DataGridBulkEditDialog.vue"));
@@ -5972,7 +5974,25 @@ watch(activeCellDetailTab, (tab) => {
const detailEditValue = ref("");
const detailEditOriginalValue = ref("");
const isEditingDetail = ref(false);
const detailValueDiffOpen = ref(false);
const detailValueDiffSnapshot = ref<Readonly<JsonValueDiffSnapshot> | null>(null);
const hasPendingDetailEditorDraft = computed(() => isEditingDetail.value && detailEditValue.value !== detailEditOriginalValue.value);
const detailJsonDiffContext = computed<JsonValueDiffContext | null>(() => {
const detail = activeCellDetail.value;
if (!detail) return null;
return {
columnName: detail.column,
columnType: detail.type,
originalValue: detailEditOriginalValue.value,
isEditable: detail.isEditable,
isEditing: isEditingDetail.value,
};
});
const showDetailJsonCompare = computed(() => {
const context = detailJsonDiffContext.value;
return !!context && isJsonValueDiffAvailable(context);
});
const canCompareDetailJson = computed(() => showDetailJsonCompare.value && hasPendingDetailEditorDraft.value);
const hasPendingInlineEditorDraft = computed(() => {
const cell = editingCell.value;
if (!cell) return false;
@@ -6141,7 +6161,9 @@ watch(valueEditorContainer, async (el) => {
detailEditValue.value = v;
},
onEscape: () => restoreDetailOriginalValue(),
onBlur: () => commitValueEditorEdit(),
onBlur: () => {
if (!detailValueDiffOpen.value) commitValueEditorEdit();
},
editorTheme: editorThemeAccessor,
appAppearance: editorAppAppearance,
appPalette: editorAppPalette,
@@ -6316,6 +6338,15 @@ function compactDetailJson() {
syncEditorFromDetailEdit();
}
function openDetailJsonCompare() {
const context = detailJsonDiffContext.value;
if (!context) return;
const snapshot = createJsonValueDiffSnapshot({ ...context, currentValue: detailEditValue.value });
if (!snapshot) return;
detailValueDiffSnapshot.value = snapshot;
detailValueDiffOpen.value = true;
}
function setDetailNull() {
const detail = activeCellDetail.value;
if (!detail || !detail.isEditable) return;
@@ -13420,6 +13451,8 @@ function currentGridContextMenuItems(): ContextMenuItem[] {
:side-json-view="sideDetailJsonView"
:show-compact-json="showCompactDetailJson"
:can-compact-json="canCompactDetailJson"
:show-compare-json="showDetailJsonCompare"
:can-compare-json="canCompareDetailJson"
:type-color-class="typeColorClass"
:can-download-binary-value="canDownloadDetailBinaryValue"
:download-binary-value="downloadDetailBinaryValue"
@@ -13430,6 +13463,7 @@ function currentGridContextMenuItems(): ContextMenuItem[] {
:database-type="resolvedDatabaseType"
@start-edit="startDetailEdit"
@compact-json="compactDetailJson"
@compare-json="openDetailJsonCompare"
@toggle-formatted="toggleCellDetailJsonFormatted"
@copy-value="copyDetailCurrentValue"
@commit="commitDetailEdit"
@@ -13506,6 +13540,9 @@ function currentGridContextMenuItems(): ContextMenuItem[] {
<Button v-if="activeValueEditorActions.includes('compactJson')" variant="outline" size="sm" class="h-6 text-xs" @mousedown.prevent @click="compactDetailJson">
{{ t("grid.compactJson") }}
</Button>
<Button v-if="showDetailJsonCompare" variant="outline" size="sm" class="h-6 text-xs" :disabled="!canCompareDetailJson" @mousedown.prevent @click="openDetailJsonCompare">
{{ t("grid.compareJson") }}
</Button>
<Button v-if="activeValueEditorActions.includes('setNull')" variant="outline" size="sm" class="h-6 text-xs" @mousedown.prevent @click="setValueEditorNull">
{{ t("grid.setNull") }}
</Button>
@@ -13622,6 +13659,8 @@ function currentGridContextMenuItems(): ContextMenuItem[] {
@edit="openDialogCellInSidePanel"
/>
<DataGridValueDiffDialog v-if="detailValueDiffSnapshot" v-model:open="detailValueDiffOpen" :snapshot="detailValueDiffSnapshot" />
<DataGridDetailDialogs
v-if="detailDialogsMounted"
v-model:row-open="rowDetailDialogOpen"
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { computed, toRef } from "vue";
import { Code2, Copy, Download, Eye, FileUp, Pencil, X } from "@lucide/vue";
import { Code2, Copy, Download, Eye, FileDiff, FileUp, Pencil, X } from "@lucide/vue";
import { useI18n } from "vue-i18n";
import { Button } from "@/components/ui/button";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
@@ -28,6 +28,8 @@ const props = defineProps<{
sideJsonView: boolean;
showCompactJson: boolean;
canCompactJson: boolean;
showCompareJson?: boolean;
canCompareJson?: boolean;
typeColorClass: (type: string) => string;
canDownloadBinaryValue: (detail: DataGridCellDetail | null) => boolean;
downloadBinaryValue: (detail: DataGridCellDetail | null, mode: BinaryCellDownloadMode) => void | Promise<void>;
@@ -44,6 +46,7 @@ const detailEditValue = defineModel<string>("value", { default: "" });
const emit = defineEmits<{
startEdit: [];
compactJson: [];
compareJson: [];
toggleFormatted: [];
copyValue: [];
commit: [];
@@ -138,6 +141,7 @@ defineExpose({ openSearch });
<div class="flex min-h-5 items-center justify-between gap-2">
<div class="text-muted-foreground">{{ t("grid.cellValue") }}</div>
<div class="flex items-center gap-1">
<Button v-if="editing && showCompareJson" variant="ghost" size="sm" class="h-5 gap-1 px-1.5 text-xs" :disabled="!canCompareJson" :title="t('grid.compareJson')" @mousedown.prevent @click="emit('compareJson')"><FileDiff class="h-3 w-3" />{{ t("grid.compareJson") }}</Button>
<Button v-if="showCompactJson" variant="ghost" size="sm" class="h-5 gap-1 px-1.5 text-xs" :disabled="!canCompactJson" :title="t('grid.compactJson')" @click="emit('compactJson')"><Code2 class="h-3 w-3" />{{ t("grid.compactJson") }}</Button>
<Button v-if="!editing && detail.formattedJson" :variant="sideJsonView ? 'secondary' : 'ghost'" size="sm" class="h-5 gap-1 px-1.5 text-xs" :title="t('grid.formattedJson')" @click="emit('toggleFormatted')"><Code2 class="h-3 w-3" />{{ t("grid.formattedJson") }}</Button>
<Button v-if="!editing && detail.isEditable" variant="ghost" size="icon" class="h-5 w-5" :title="t('grid.editValue')" @click="emit('startEdit')"><Pencil class="h-3 w-3" /></Button>
@@ -0,0 +1,85 @@
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { AlertCircle, FileDiff } from "@lucide/vue";
import { useI18n } from "vue-i18n";
import SideBySideTextDiff, { type TextDiffSide } from "@/components/common/SideBySideTextDiff.vue";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { useToast } from "@/composables/useToast";
import { copyToClipboard } from "@/lib/common/clipboard";
import { formatJsonValueDiffText, type JsonValueDiffFormatError, type JsonValueDiffMode, type JsonValueDiffSnapshot } from "@/lib/dataGrid/jsonValueDiff";
const props = defineProps<{
snapshot: Readonly<JsonValueDiffSnapshot> | null;
}>();
const open = defineModel<boolean>("open", { default: false });
const { t } = useI18n();
const { toast } = useToast();
const formatMode = ref<JsonValueDiffMode>("json");
const originalFormatted = computed(() => formatJsonValueDiffText(props.snapshot?.originalValue ?? "", formatMode.value));
const currentFormatted = computed(() => formatJsonValueDiffText(props.snapshot?.currentValue ?? "", formatMode.value));
function formatError(error: JsonValueDiffFormatError): string {
if (error.kind === "too-large") return t("grid.valueDiffTooLarge", { limit: error.limit.toLocaleString() });
return t("grid.valueDiffInvalidJson", { message: error.message });
}
async function copyValue(side: TextDiffSide) {
const snapshot = props.snapshot;
if (!snapshot) return;
try {
await copyToClipboard(side === "before" ? snapshot.originalValue : snapshot.currentValue);
toast(t("grid.cellValueCopied"), 2000);
} catch (error) {
toast(error instanceof Error ? error.message : String(error), 5000);
}
}
watch(open, (isOpen) => {
if (isOpen) formatMode.value = "json";
});
</script>
<template>
<Dialog v-model:open="open">
<DialogContent v-if="snapshot" class="flex h-[min(82vh,760px)] min-w-0 max-w-[min(1180px,calc(100vw-32px))] flex-col gap-0 overflow-hidden p-0 sm:max-w-[min(1180px,calc(100vw-32px))]">
<DialogHeader class="shrink-0 border-b px-4 py-3 pr-11">
<DialogTitle class="flex min-w-0 items-center gap-2 text-sm">
<FileDiff class="h-4 w-4 shrink-0 text-blue-600 dark:text-blue-400" />
<span class="min-w-0 truncate">{{ t("grid.valueDiffTitle", { column: snapshot.columnName }) }}</span>
</DialogTitle>
</DialogHeader>
<div class="flex h-9 shrink-0 items-center gap-3 border-b px-3">
<div class="inline-flex h-7 items-center rounded border bg-muted/20 p-0.5" role="group" :aria-label="t('grid.valueDiffFormat')">
<Button type="button" size="sm" :variant="formatMode === 'json' ? 'secondary' : 'ghost'" class="h-6 rounded-sm px-2 text-[11px] shadow-none" @click="formatMode = 'json'">JSON</Button>
<Button type="button" size="sm" :variant="formatMode === 'raw' ? 'secondary' : 'ghost'" class="h-6 rounded-sm px-2 text-[11px] shadow-none" @click="formatMode = 'raw'">{{ t("grid.valueDiffRaw") }}</Button>
</div>
</div>
<div v-if="originalFormatted.error || currentFormatted.error" class="grid shrink-0 grid-cols-2 border-b bg-amber-500/10 text-[11px] text-amber-700 dark:text-amber-300">
<div class="flex min-w-0 items-start gap-1.5 border-r px-2 py-1">
<template v-if="originalFormatted.error"
><AlertCircle class="mt-0.5 h-3 w-3 shrink-0" /><span class="min-w-0 break-words">{{ formatError(originalFormatted.error) }}</span></template
>
</div>
<div class="flex min-w-0 items-start gap-1.5 px-2 py-1">
<template v-if="currentFormatted.error"
><AlertCircle class="mt-0.5 h-3 w-3 shrink-0" /><span class="min-w-0 break-words">{{ formatError(currentFormatted.error) }}</span></template
>
</div>
</div>
<SideBySideTextDiff
:before-text="originalFormatted.text"
:after-text="currentFormatted.text"
:before-label="t('grid.valueDiffOriginal')"
:after-label="t('grid.valueDiffCurrent')"
:copy-before-title="t('grid.copyOriginalValue')"
:copy-after-title="t('grid.copyCurrentValue')"
@copy="copyValue"
/>
</DialogContent>
</Dialog>
</template>
@@ -28,6 +28,7 @@ vi.mock("@lucide/vue", async () => {
ChevronsRight: icon,
Download: icon,
Filter: icon,
FileDiff: icon,
Loader2: icon,
FileUp: icon,
Upload: icon,
@@ -1434,6 +1435,49 @@ describe("cell detail surfaces", () => {
doubleClickCapture({ target: { closest: () => null }, clientX: 60, clientY: 80 });
expect(startEdit).toHaveBeenCalledTimes(4);
});
it("only enables JSON comparison for changed drafts and prevents editor blur on pointer down", async () => {
const compareJson = vi.fn();
const mounted = mountComponent(DataGridCellDetailPanel, {
detail: detail(),
panelIsBottom: true,
metadataCollapsed: false,
valueFillsHeight: true,
editing: true,
sideJsonView: false,
showCompactJson: true,
canCompactJson: true,
showCompareJson: true,
canCompareJson: false,
typeColorClass: () => "",
canDownloadBinaryValue: () => false,
downloadBinaryValue: vi.fn(),
canImportBinaryValue: () => false,
importBinaryValue: vi.fn(),
openImagePreview: vi.fn(),
canCopySqlCondition: () => true,
onCompareJson: compareJson,
});
const disabledCompare = findOne(mounted.root, (node) => node.props.title === "grid.compareJson");
expect(disabledCompare.props.disabled).toBe(true);
dispatch(disabledCompare, "click");
expect(compareJson).not.toHaveBeenCalled();
await mounted.setProps({ canCompareJson: true });
const enabledCompare = findOne(mounted.root, (node) => node.props.title === "grid.compareJson");
expect(dispatch(enabledCompare, "mousedown").defaultPrevented).toBe(true);
dispatch(enabledCompare, "click");
expect(compareJson).toHaveBeenCalledOnce();
});
it("snapshots comparison values before opening and suppresses modal-induced blur commits", () => {
expect(dataGridSource).toContain("detailValueDiffSnapshot.value = snapshot;");
expect(dataGridSource).toContain("detailValueDiffOpen.value = true;");
expect(dataGridSource).toContain("if (!detailValueDiffOpen.value) commitValueEditorEdit();");
expect(dataGridSource).toContain(':disabled="!canCompareDetailJson" @mousedown.prevent @click="openDetailJsonCompare"');
expect(dataGridSource).toContain('v-model:open="detailValueDiffOpen" :snapshot="detailValueDiffSnapshot"');
});
});
describe("DataGridCopyColumnNamesDialog", () => {