diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue index 6449231601..f3eee4e0f2 100644 --- a/apps/desktop/src/components/grid/DataGrid.vue +++ b/apps/desktop/src/components/grid/DataGrid.vue @@ -124,6 +124,7 @@ import { dataGridScrollPosition, isDataGridNearScrollBottom, shouldCheckInfinite import { CANVAS_DATA_GRID_ROW_HEIGHT, drawCanvasDataGrid } from "@/lib/canvasDataGridRenderer"; import { dataGridSaveActionMode, dataGridSaveToolbarState } from "@/lib/dataGridSaveUi"; import { EDITOR_FONT_FAMILY_CSS_VAR } from "@/lib/editorThemes"; +import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/safeStorage"; import { appendColumnValueFilterCondition, buildColumnValueFilterCondition, combineWhereInputs, filterModeNeedsValue, parseFilterValue } from "@/lib/dataGridColumnFilter"; import { clampSearchSplitWidth } from "@/lib/dataGridSearchSplit"; import { MAX_RESULT_PAGE_SIZE, MIN_RESULT_PAGE_SIZE, normalizeResultPageSize, resultPageSizeMenuOptions } from "@/lib/paginationPageSize"; @@ -152,6 +153,7 @@ import { effectiveDatabaseTypeForConnection } from "@/lib/jdbcDialect"; import { isMacOS } from "@/lib/platform"; const SqlPreviewPanel = defineAsyncComponent(() => import("@/components/editor/SqlPreviewPanel.vue")); +const FORMATTED_JSON_EDIT_WARNING_STORAGE_KEY = "dbx-cell-detail-formatted-json-edit-warning-shown"; const { t } = useI18n(); const slots = useSlots(); @@ -3721,8 +3723,12 @@ const detailTemporalEditorKind = computed(() => { // CodeMirror-based cell detail editors const detailsEditorContainer = ref(); const valueEditorContainer = ref(); +const sideJsonPreviewContainer = ref(); +const dialogJsonPreviewContainer = ref(); let detailsDetailEditor: UseCellDetailEditorReturn | null = null; let valueDetailEditor: UseCellDetailEditorReturn | null = null; +let sideJsonPreviewEditor: UseCellDetailEditorReturn | null = null; +let dialogJsonPreviewEditor: UseCellDetailEditorReturn | null = null; const editorThemeAccessor = () => settingsStore.editorSettings.theme; const editorAppAppearance = () => (isDark.value ? "dark" : "light") as import("@/lib/appTheme").AppThemeAppearance; @@ -3782,6 +3788,54 @@ watch(valueEditorContainer, async (el) => { } }); +watch(sideJsonPreviewContainer, async (el) => { + if (el && !sideJsonPreviewEditor) { + sideJsonPreviewEditor = useCellDetailEditor({ + language: "json", + readOnly: true, + editorTheme: editorThemeAccessor, + appAppearance: editorAppAppearance, + fontSize: editorFontSize, + fontFamily: editorFontFamily, + }); + await sideJsonPreviewEditor.create(el, activeCellDetail.value?.formattedJson ?? "", "json"); + } else if (!el && sideJsonPreviewEditor) { + sideJsonPreviewEditor.destroy(); + sideJsonPreviewEditor = null; + } +}); + +watch(dialogJsonPreviewContainer, async (el) => { + if (el && !dialogJsonPreviewEditor) { + dialogJsonPreviewEditor = useCellDetailEditor({ + language: "json", + readOnly: true, + editorTheme: editorThemeAccessor, + appAppearance: editorAppAppearance, + fontSize: editorFontSize, + fontFamily: editorFontFamily, + }); + await dialogJsonPreviewEditor.create(el, dialogCellDetail.value?.formattedJson ?? "", "json"); + } else if (!el && dialogJsonPreviewEditor) { + dialogJsonPreviewEditor.destroy(); + dialogJsonPreviewEditor = null; + } +}); + +watch( + () => activeCellDetail.value?.formattedJson ?? "", + (value) => { + sideJsonPreviewEditor?.setValue(value, "json"); + }, +); + +watch( + () => dialogCellDetail.value?.formattedJson ?? "", + (value) => { + dialogJsonPreviewEditor?.setValue(value, "json"); + }, +); + function resetDetailEdit() { isEditingDetail.value = false; detailEditValue.value = ""; @@ -3793,14 +3847,27 @@ function closeCellDetails() { detailCell.value = null; } -function startDetailEdit() { - const detail = activeCellDetail.value; - if (!detail || !detail.isEditable) return; - detailEditValue.value = dataGridCellEditorText({ +function cellDetailEditText(detail: DataGridCellDetail): string { + if (sideDetailJsonView.value && detail.formattedJson) return detail.formattedJson; + return dataGridCellEditorText({ value: detail.value, databaseType: props.databaseType, columnInfo: tableColumnForGridColumn(detail.colIndex), }); +} + +function warnFormattedJsonEditIfNeeded(detail: DataGridCellDetail, force = false) { + if (!force && (!sideDetailJsonView.value || !detail.formattedJson)) return; + if (safeLocalStorageGet(FORMATTED_JSON_EDIT_WARNING_STORAGE_KEY) === "1") return; + toast(t("grid.formattedJsonEditWarning"), 10000); + safeLocalStorageSet(FORMATTED_JSON_EDIT_WARNING_STORAGE_KEY, "1"); +} + +function startDetailEdit() { + const detail = activeCellDetail.value; + if (!detail || !detail.isEditable) return; + warnFormattedJsonEditIfNeeded(detail); + detailEditValue.value = cellDetailEditText(detail); isEditingDetail.value = true; } @@ -3881,6 +3948,7 @@ function formatValueEditorJson() { if (!detail || !canFormatCellDetailJson(detailEditValue.value, detail.type)) return; detailEditValue.value = formatJsonText(detailEditValue.value) ?? detailEditValue.value; syncEditorFromDetailEdit(); + warnFormattedJsonEditIfNeeded(detail, true); } function setDetailNull() { @@ -5558,11 +5626,14 @@ function copyDialogCellColumnName() { copyText(detail.column); } -function openDialogCellInSidePanel() { +async function openDialogCellInSidePanel() { const detail = dialogCellDetail.value; if (!detail) return; showCellDetails(detail.rowNumber - 1, detail.colIndex); cellDetailDialogOpen.value = false; + await nextTick(); + sideDetailJsonView.value = cellDetailJsonView.value && !!detail.formattedJson; + if (detail.isEditable) startDetailEdit(); } function copyRowDetailJson() { @@ -8425,12 +8496,20 @@ const gridContextMenuItems = computed(() => { +
{{ sideDetailJsonView && activeCellDetail.formattedJson ? activeCellDetail.formattedJson : activeCellDetail.rawValuePreview }}
{{ activeCellDetail.rawValuePreview }}
{{ @@ -8707,9 +8786,8 @@ const gridContextMenuItems = computed(() => { -
{{
-              cellDetailJsonView && dialogCellDetail.formattedJson ? dialogCellDetail.formattedJson : dialogCellDetail.rawValuePreview
-            }}
+
+
{{ dialogCellDetail.rawValuePreview }}
{{ t("grid.largeValuePreviewHint", { count: dialogCellDetail.rawValuePreview.length }) }}
diff --git a/apps/desktop/src/composables/useCellDetailEditor.ts b/apps/desktop/src/composables/useCellDetailEditor.ts index 2d5bdd1562..e3eb749b7d 100644 --- a/apps/desktop/src/composables/useCellDetailEditor.ts +++ b/apps/desktop/src/composables/useCellDetailEditor.ts @@ -1,4 +1,4 @@ -import { shallowRef, onBeforeUnmount, type ShallowRef, createApp, watch } from "vue"; +import { shallowRef, onBeforeUnmount, getCurrentInstance, type ShallowRef, createApp, watch } from "vue"; import { EditorState, Compartment } from "@codemirror/state"; import { EditorView, keymap, drawSelection, dropCursor, highlightSpecialChars, highlightActiveLine } from "@codemirror/view"; import { json } from "@codemirror/lang-json"; @@ -20,6 +20,7 @@ export interface UseCellDetailEditorOptions { onChange?: (value: string) => void; onEscape?: () => void; onBlur?: () => void; + language?: "auto" | "json"; readOnly?: boolean; editorTheme: () => EditorTheme; appAppearance: () => AppThemeAppearance; @@ -147,7 +148,7 @@ export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCel if (destroyed) return; const doc = initialValue ?? ""; - currentIsJson = shouldUseJsonMode(columnType, doc); + currentIsJson = options.language === "json" || shouldUseJsonMode(columnType, doc); const theme = await loadEditorTheme(options.editorTheme(), options.appAppearance()); liveFontSize = clampEditorFontSize(options.fontSize()); @@ -251,7 +252,7 @@ export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCel if (!editor || destroyed) return; const text = value ?? ""; - const newIsJson = shouldUseJsonMode(columnType, text); + const newIsJson = options.language === "json" || shouldUseJsonMode(columnType, text); const effects: ReturnType[] = []; if (newIsJson !== currentIsJson) { @@ -297,9 +298,11 @@ export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCel wrapperEl = null; } - onBeforeUnmount(() => { - destroy(); - }); + if (getCurrentInstance()) { + onBeforeUnmount(() => { + destroy(); + }); + } return { create, setValue, getValue, openSearch, openReplace, destroy, view }; } diff --git a/apps/desktop/src/composables/useToast.ts b/apps/desktop/src/composables/useToast.ts index 60754a5dfa..c30eef271f 100644 --- a/apps/desktop/src/composables/useToast.ts +++ b/apps/desktop/src/composables/useToast.ts @@ -1,18 +1,32 @@ -import { ref } from "vue"; +import { ref, type Ref } from "vue"; -const message = ref(""); -const visible = ref(false); -let timer = 0; +type ToastState = { + message: Ref; + visible: Ref; + timer: number; +}; + +declare global { + var __DBX_TOAST_STATE__: ToastState | undefined; +} + +const toastState = + globalThis.__DBX_TOAST_STATE__ ?? + (globalThis.__DBX_TOAST_STATE__ = { + message: ref(""), + visible: ref(false), + timer: 0, + }); export function useToast() { function toast(msg: string, duration = 2000) { - message.value = msg; - visible.value = true; - clearTimeout(timer); - timer = window.setTimeout(() => { - visible.value = false; + toastState.message.value = msg; + toastState.visible.value = true; + clearTimeout(toastState.timer); + toastState.timer = window.setTimeout(() => { + toastState.visible.value = false; }, duration); } - return { message, visible, toast }; + return { message: toastState.message, visible: toastState.visible, toast }; } diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts index 140ee287bc..da00dcc4a2 100644 --- a/apps/desktop/src/i18n/locales/en.ts +++ b/apps/desktop/src/i18n/locales/en.ts @@ -779,6 +779,7 @@ export default { copyColumnTsv: "Copy Column (TSV)", editValue: "Edit Value", formatJson: "Format JSON", + formattedJsonEditWarning: "You are editing formatted JSON. Saving writes back formatted text and may change the original whitespace and layout", setNull: "Set NULL", restoreOriginalValue: "Restore Original", copyColumnName: "Copy Column Name", diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts index 6333c9e0d1..67a9550acc 100644 --- a/apps/desktop/src/i18n/locales/zh-CN.ts +++ b/apps/desktop/src/i18n/locales/zh-CN.ts @@ -782,6 +782,7 @@ export default withEnglishFallback({ copyColumnTsv: "复制列 (TSV)", editValue: "编辑值", formatJson: "格式化 JSON", + formattedJsonEditWarning: "正在编辑格式化后的 JSON。保存后会写回格式化文本,原始空白和排版可能变化", setNull: "设为 NULL", restoreOriginalValue: "恢复原值", copyColumnName: "复制列名", diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts index 6e42c818e5..246dee8faa 100644 --- a/apps/desktop/src/i18n/locales/zh-TW.ts +++ b/apps/desktop/src/i18n/locales/zh-TW.ts @@ -781,6 +781,7 @@ export default withEnglishFallback({ copyColumnTsv: "複製欄 (TSV)", editValue: "編輯值", formatJson: "格式化 JSON", + formattedJsonEditWarning: "正在編輯格式化後的 JSON。儲存後會寫回格式化文字,原始空白和排版可能改變", setNull: "設為 NULL", restoreOriginalValue: "復原資料", copyColumnName: "複製欄位名稱",