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: highlight formatted json in cell detail

zipg7e5b6b8e77f6
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<HTMLElement>();
const valueEditorContainer = ref<HTMLElement>();
+const sideJsonPreviewContainer = ref<HTMLElement>();
+const dialogJsonPreviewContainer = ref<HTMLElement>();
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<ContextMenuItem[]>(() => {
</Button>
</div>
</template>
+ <div
+ v-else-if="sideDetailJsonView && activeCellDetail.formattedJson"
+ ref="sideJsonPreviewContainer"
+ data-cell-detail-editor-root
+ class="overflow-hidden rounded border bg-muted/20 p-2"
+ :class="[{ 'cursor-text': activeCellDetail.isEditable }, cellDetailPanelIsBottom ? 'min-h-0 flex-1' : 'h-72 max-h-[42vh]']"
+ @dblclick.capture="startDetailEdit"
+ />
<pre
v-else
class="overflow-auto rounded border bg-muted/20 p-2 font-mono text-xs whitespace-pre-wrap break-words cursor-pointer hover:border-primary/50"
:class="[{ 'cursor-text': activeCellDetail.isEditable }, cellDetailPanelIsBottom ? 'min-h-0 flex-1' : '']"
@dblclick="startDetailEdit"
- >{{ sideDetailJsonView && activeCellDetail.formattedJson ? activeCellDetail.formattedJson : activeCellDetail.rawValuePreview }}</pre
+ >{{ activeCellDetail.rawValuePreview }}</pre
>
<div v-if="activeCellDetail.isValuePreviewTruncated && !sideDetailJsonView" class="text-[11px] text-muted-foreground">
{{
@@ -8707,9 +8786,8 @@ const gridContextMenuItems = computed<ContextMenuItem[]>(() => {
<a v-if="dialogCellDetail.imagePreviewUrl" :href="dialogCellDetail.imagePreviewUrl" role="button" class="block max-h-72 overflow-hidden rounded border bg-muted/20" @click.prevent="openImagePreview(dialogCellDetail.imagePreviewUrl, dialogCellDetail.column)">
<img :src="dialogCellDetail.imagePreviewUrl" :alt="dialogCellDetail.column" loading="lazy" decoding="async" referrerpolicy="no-referrer" class="max-h-72 w-full object-contain" />
</a>
- <pre class="max-h-[44vh] overflow-auto rounded border bg-muted/20 p-3 font-mono text-xs whitespace-pre-wrap break-words" :class="{ 'italic text-muted-foreground': dialogCellDetail.value === null }">{{
- cellDetailJsonView && dialogCellDetail.formattedJson ? dialogCellDetail.formattedJson : dialogCellDetail.rawValuePreview
- }}</pre>
+ <div v-if="cellDetailJsonView && dialogCellDetail.formattedJson" ref="dialogJsonPreviewContainer" data-cell-detail-editor-root class="h-[44vh] min-h-60 overflow-hidden rounded border bg-muted/20 p-3" />
+ <pre v-else class="max-h-[44vh] overflow-auto rounded border bg-muted/20 p-3 font-mono text-xs whitespace-pre-wrap break-words" :class="{ 'italic text-muted-foreground': dialogCellDetail.value === null }">{{ dialogCellDetail.rawValuePreview }}</pre>
<div v-if="dialogCellDetail.isValuePreviewTruncated && !cellDetailJsonView" class="text-[11px] text-muted-foreground">
{{ t("grid.largeValuePreviewHint", { count: dialogCellDetail.rawValuePreview.length }) }}
</div>
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<typeof Compartment.prototype.reconfigure>[] = [];
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<string>;
+ visible: Ref<boolean>;
+ 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: "複製欄位名稱",