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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
import { shallowRef, onBeforeUnmount, getCurrentInstance, type ShallowRef, createApp, watch } from "vue";
import { EditorSelection, EditorState, Compartment } from "@codemirror/state";
import { EditorView, keymap, drawSelection, dropCursor, highlightSpecialChars, highlightActiveLine, highlightActiveLineGutter, lineNumbers } from "@codemirror/view";
import { json } from "@codemirror/lang-json";
import { search as cmSearch } from "@codemirror/search";
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
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 { isJsonColumnType } from "@/lib/dataGrid/cellDetailPresentation";
import { clampEditorFontSize, createEditorWheelZoomController, createEditorZoomCommitScheduler, fontSizeFromGestureScale } from "@/lib/editor/editorZoom";
import i18n from "@/i18n";
import EditorSearchPanel from "@/components/editor/EditorSearchPanel.vue";
import type { EditorTheme } from "@/stores/settingsStore";
import type { AppThemeAppearance, AppThemePalette } from "@/lib/app/appTheme";
import { selectAllCellDetailText } from "@/lib/dataGrid/cellDetailSelection";
import { shouldAutoWrapLongSingleLine } from "@/lib/dataGrid/cellDetailWordWrap";
import { selectLineEnds } from "@/lib/editor/selectLineEnds";
import { preserveSourceHistory, rawSourceField, restoreRawSource } from "@/lib/editor/preserveTextLineEndings";
export interface UseCellDetailEditorOptions {
onChange?: (value: string) => void;
onEscape?: () => void;
onBlur?: () => void;
/** Return true after handling a save shortcut so CodeMirror consumes it. */
onSaveShortcut?: (event: KeyboardEvent) => boolean;
language?: "auto" | "json" | "text";
/** Keep source line endings and suppress change events during external text synchronization. */
preserveLineEndings?: boolean;
readOnly?: boolean | (() => boolean);
/** Keep cell detail editors gutter-free unless a caller explicitly opts in. */
lineNumbers?: boolean;
/** A reactive source for opting into CodeMirror line wrapping. */
lineWrapping?: () => boolean;
/** Add CodeMirror fold controls and keyboard bindings for structured source. */
folding?: boolean;
/**
* When false, Mod+F / replace are not bound so a parent (e.g. Redis value viewer)
* can own the unified find surface. Default true for grid cell editors.
*/
enableBuiltinFind?: boolean;
editorTheme: () => EditorTheme;
appAppearance: () => AppThemeAppearance;
appPalette: () => AppThemePalette;
fontSize: () => number;
fontFamily: () => string;
}
export interface UseCellDetailEditorReturn {
create: (parent: HTMLElement, initialValue: string, columnType?: string) => Promise<void>;
setValue: (value: string, columnType?: string) => void;
getValue: () => string;
openSearch: () => boolean;
openReplace: () => boolean;
selectRange: (from: number, to: number, options?: { focus?: boolean }) => boolean;
destroy: () => void;
view: Readonly<ShallowRef<EditorView | null>>;
}
interface CellDetailEditorGestureEvent extends Event {
scale?: number;
}
function looksLikeJsonString(text: string): boolean {
const trimmed = text.trim();
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_MODE_MAX_LENGTH) return false;
if (isJsonColumnType(columnType)) return true;
if (value && looksLikeJsonString(value)) return true;
return false;
}
export function useCellDetailEditor(options: UseCellDetailEditorOptions): UseCellDetailEditorReturn {
const view = shallowRef<EditorView | null>(null) as ShallowRef<EditorView | null>;
const settingsStore = useSettingsStore();
const languageComp = new Compartment();
const themeComp = new Compartment();
const fontThemeComp = new Compartment();
const lineWrappingComp = new Compartment();
const readOnlyComp = new Compartment();
const shortcutComp = new Compartment();
let destroyed = false;
let synchronizingValue = false;
let currentIsJson = false;
let appliedLineWrapping: boolean | null = null;
let liveFontSize = clampEditorFontSize(options.fontSize());
let gestureStartFontSize = liveFontSize;
let isGestureZooming = false;
let pendingFontReconfig: { size: number; family: string } | null = null;
let fontReconfigScheduled = false;
let searchApp: ReturnType<typeof createApp> | null = null;
let searchInstance: InstanceType<typeof EditorSearchPanel> | null = null;
let wrapperEl: HTMLDivElement | null = null;
const zoomCommitScheduler = createEditorZoomCommitScheduler((fontSize) => {
if (settingsStore.editorSettings.fontSize === fontSize) return;
settingsStore.updateEditorSettings({ fontSize });
});
const wheelZoom = createEditorWheelZoomController();
function syncEditorFontCssVars(fontSize = liveFontSize, fontFamily = options.fontFamily()) {
if (!wrapperEl) return;
wrapperEl.style.setProperty(EDITOR_FONT_SIZE_CSS_VAR, `${clampEditorFontSize(fontSize)}px`);
wrapperEl.style.setProperty(EDITOR_FONT_FAMILY_CSS_VAR, fontFamily);
}
function isReadOnly(): boolean {
return typeof options.readOnly === "function" ? options.readOnly() : Boolean(options.readOnly);
}
function isLineWrapping(): boolean {
return typeof options.lineWrapping === "function" ? options.lineWrapping() : settingsStore.editorSettings.wordWrap;
}
/**
* Effective wrapping for the current document: the caller's explicit wrap
* choice, plus automatic folding of super long single-line values (#12320)
* so they are readable without dragging the horizontal scrollbar.
*/
function shouldWrapDoc(doc: string): boolean {
return isLineWrapping() || shouldAutoWrapLongSingleLine(doc);
}
function applyLineWrapping(doc: string): boolean {
const wrap = shouldWrapDoc(doc);
if (wrap === appliedLineWrapping) return false;
appliedLineWrapping = wrap;
return true;
}
function readOnlyExtensions(readOnly: boolean) {
return [EditorState.readOnly.of(readOnly), EditorView.editable.of(!readOnly), EditorView.contentAttributes.of(readOnly ? { tabindex: "0" } : {})];
}
function reconfigureFontTheme(size: number, family: string) {
const editor = view.value;
if (!editor) return;
editor.dispatch({
effects: fontThemeComp.reconfigure(editorFontTheme(EditorView, size, family, { fixedHeight: true, scrollable: true })),
});
}
function scheduleFontThemeReconfig(size: number, family: string) {
pendingFontReconfig = { size, family };
if (fontReconfigScheduled) return;
fontReconfigScheduled = true;
requestAnimationFrame(() => {
fontReconfigScheduled = false;
const pending = pendingFontReconfig;
if (!pending || destroyed) return;
pendingFontReconfig = null;
reconfigureFontTheme(pending.size, pending.family);
});
}
function applyLiveFontSize(size: number) {
const next = clampEditorFontSize(size);
if (liveFontSize === next) return;
liveFontSize = next;
syncEditorFontCssVars(next);
scheduleFontThemeReconfig(next, options.fontFamily());
}
function onEditorGestureStart(event: CellDetailEditorGestureEvent) {
event.preventDefault();
isGestureZooming = true;
gestureStartFontSize = liveFontSize;
}
function onEditorGestureChange(event: CellDetailEditorGestureEvent) {
if (typeof event.scale !== "number") return;
event.preventDefault();
applyLiveFontSize(fontSizeFromGestureScale(gestureStartFontSize, event.scale));
}
function onEditorGestureEnd(event: Event) {
event.preventDefault();
isGestureZooming = false;
wheelZoom.holdScrollGesture(event.timeStamp);
zoomCommitScheduler.flush(liveFontSize);
}
watch([() => options.fontSize(), () => options.fontFamily(), () => options.editorTheme(), () => options.appAppearance(), () => options.appPalette()], async ([fontSize, fontFamily, editorTheme, appearance, palette]) => {
const editor = view.value;
if (!editor || destroyed) return;
if (!isGestureZooming && !zoomCommitScheduler.hasPendingCommit()) {
const nextFontSize = clampEditorFontSize(fontSize);
if (nextFontSize !== liveFontSize) wheelZoom.rebase();
liveFontSize = nextFontSize;
}
syncEditorFontCssVars(liveFontSize, fontFamily);
const theme = await loadEditorTheme(editorTheme, appearance, undefined, palette);
if (!view.value || destroyed) return;
view.value.dispatch({
effects: [themeComp.reconfigure(theme), fontThemeComp.reconfigure(editorFontTheme(EditorView, liveFontSize, fontFamily, { fixedHeight: true, scrollable: true }))],
});
});
watch(
() => isLineWrapping(),
() => {
const editor = view.value;
if (!editor || destroyed) return;
const doc = editor.state.doc.toString();
if (!applyLineWrapping(doc)) return;
editor.dispatch({ effects: lineWrappingComp.reconfigure(appliedLineWrapping ? EditorView.lineWrapping : []) });
},
);
watch(
() => settingsStore.editorSettings.shortcuts.selectLineEnds,
(shortcut) => {
const editor = view.value;
if (!editor || destroyed) return;
editor.dispatch({ effects: shortcutComp.reconfigure(keymap.of([{ key: shortcutToCodeMirrorKey(shortcut), preventDefault: true, run: selectLineEnds }])) });
},
);
watch(
() => isReadOnly(),
(readOnly) => {
const editor = view.value;
if (!editor || destroyed) return;
editor.dispatch({ effects: readOnlyComp.reconfigure(readOnlyExtensions(readOnly)) });
},
);
async function create(parent: HTMLElement, initialValue: string, columnType?: string): Promise<void> {
if (destroyed) return;
const doc = initialValue ?? "";
currentIsJson = options.language !== "text" && (options.language === "json" || shouldUseJsonMode(columnType, doc));
const theme = await loadEditorTheme(options.editorTheme(), options.appAppearance(), undefined, options.appPalette());
if (destroyed) return;
liveFontSize = clampEditorFontSize(options.fontSize());
const fontTheme = editorFontTheme(EditorView, liveFontSize, options.fontFamily(), { fixedHeight: true, scrollable: true });
const shortcuts = settingsStore.editorSettings.shortcuts;
appliedLineWrapping = shouldWrapDoc(doc);
const state = EditorState.create({
doc,
extensions: [
cmSearch({
createPanel: () => {
const dom = document.createElement("span");
dom.style.display = "none";
return { dom };
},
}),
// Keep the compact detail-editor baseline; gutters and wrapping are opt-in.
...(options.lineNumbers ? [lineNumbers(), highlightActiveLineGutter()] : []),
...(options.folding ? [createConsistentFoldGutter()] : []),
highlightSpecialChars(),
history(),
...(options.preserveLineEndings ? preserveSourceHistory(doc) : []),
drawSelection(),
editorClipboardLineEndingsExtension(EditorView),
trimmedSelectionLayer(),
dropCursor(),
highlightActiveLine(),
EditorView.theme({
".cm-activeLine": {
backgroundColor: cellDetailActiveLineColor(),
},
}),
EditorState.allowMultipleSelections.of(true),
bracketMatching(),
keymap.of([
...defaultKeymap,
...historyKeymap,
...(options.folding ? foldKeymap : []),
// Callers may disable find so a parent surface (e.g. RedisValueViewer) owns Mod+F.
...(options.enableBuiltinFind === false
? []
: [
{
key: shortcutToCodeMirrorKey(shortcuts.find),
preventDefault: true,
run: () => openSearch(),
},
{
key: shortcutToCodeMirrorKey(shortcuts.replace),
preventDefault: true,
run: () => openReplace(),
},
]),
]),
languageComp.of(currentIsJson ? json() : []),
lineWrappingComp.of(shouldWrapDoc(doc) ? EditorView.lineWrapping : []),
readOnlyComp.of(readOnlyExtensions(isReadOnly())),
themeComp.of(theme),
fontThemeComp.of(fontTheme),
shortcutComp.of(keymap.of([{ key: shortcutToCodeMirrorKey(shortcuts.selectLineEnds), preventDefault: true, run: selectLineEnds }])),
keymap.of([
{
key: "Mod-a",
run: selectAllCellDetailText,
},
{
key: "Escape",
run: () => {
if (searchInstance && (searchInstance as any).closeSearch()) return true;
options.onEscape?.();
return true;
},
},
]),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
const nextDoc = update.state.doc.toString();
// Auto-wrap (#12320) flips with the document itself: a single long
// line folds, then un-folds again as soon as the user breaks it up.
if (applyLineWrapping(nextDoc)) {
update.view.dispatch({ effects: lineWrappingComp.reconfigure(appliedLineWrapping ? EditorView.lineWrapping : []) });
}
if (options.preserveLineEndings) {
if (synchronizingValue) return;
options.onChange?.(update.state.field(rawSourceField));
} else {
options.onChange?.(nextDoc);
}
}
}),
EditorView.domEventHandlers({
keydown(event, eventView) {
if (matchesShortcut(event, settingsStore.editorSettings.shortcuts.selectLineEnds)) {
event.preventDefault();
return selectLineEnds(eventView);
}
if (!options.onSaveShortcut?.(event)) return false;
event.preventDefault();
event.stopPropagation();
return true;
},
wheel(event) {
if (isGestureZooming) {
if (event.ctrlKey || event.metaKey) {
event.preventDefault();
return true;
}
return false;
}
const next = wheelZoom.nextFontSize(liveFontSize, event);
if (next === null) return false;
event.preventDefault();
applyLiveFontSize(next);
zoomCommitScheduler.schedule(next);
return true;
},
blur: () => {
options.onBlur?.();
},
}),
],
});
// Re-check after the async theme load: a fast v-if unmount can destroy this
// instance while create is still in flight.
if (destroyed) return;
wrapperEl = document.createElement("div");
wrapperEl.style.cssText = "position: relative; width: 100%; height: 100%;";
wrapperEl.addEventListener("gesturestart", onEditorGestureStart);
wrapperEl.addEventListener("gesturechange", onEditorGestureChange);
wrapperEl.addEventListener("gestureend", onEditorGestureEnd);
parent.appendChild(wrapperEl);
syncEditorFontCssVars(liveFontSize, options.fontFamily());
view.value = new EditorView({ state, parent: wrapperEl });
// Mount search panel component
const searchMount = document.createElement("div");
wrapperEl.appendChild(searchMount);
searchApp = createApp(EditorSearchPanel, { view: view.value });
searchApp.use(i18n);
searchInstance = searchApp.mount(searchMount) as any;
// If unmounted during the last sync steps, drop the late-mounted editor.
if (destroyed) {
destroy();
}
}
function setValue(value: string, columnType?: string) {
const editor = view.value;
if (!editor || destroyed) return;
const text = value ?? "";
const newIsJson = options.language !== "text" && (options.language === "json" || shouldUseJsonMode(columnType, text));
const effects: ReturnType<typeof Compartment.prototype.reconfigure>[] = [];
if (newIsJson !== currentIsJson) {
effects.push(languageComp.reconfigure(newIsJson ? json() : []));
currentIsJson = newIsJson;
}
synchronizingValue = true;
try {
editor.dispatch({
changes: { from: 0, to: editor.state.doc.length, insert: text },
effects: options.preserveLineEndings ? [...effects, restoreRawSource.of(text)] : effects,
});
} finally {
synchronizingValue = false;
}
}
function getValue(): string {
if (options.preserveLineEndings && view.value) return view.value.state.field(rawSourceField);
return view.value?.state.doc.toString() ?? "";
}
function openSearch(): boolean {
return (searchInstance as any)?.openSearch?.() ?? false;
}
function openReplace(): boolean {
return (searchInstance as any)?.openReplace?.() ?? false;
}
function selectRange(from: number, to: number, options?: { focus?: boolean }): boolean {
const editor = view.value;
if (!editor || destroyed) return false;
const max = editor.state.doc.length;
const start = Math.max(0, Math.min(from, max));
const end = Math.max(0, Math.min(to, max));
editor.dispatch({
selection: EditorSelection.range(start, end),
scrollIntoView: true,
});
// Default focus for explicit navigation; callers can pass focus:false while typing in a find box.
if (options?.focus !== false) editor.focus();
return true;
}
function destroy() {
const alreadyDestroyed = destroyed;
destroyed = true;
searchApp?.unmount();
searchApp = null;
searchInstance = null;
view.value?.destroy();
view.value = null;
if (!alreadyDestroyed) zoomCommitScheduler.dispose();
if (wrapperEl) {
wrapperEl.removeEventListener("gesturestart", onEditorGestureStart);
wrapperEl.removeEventListener("gesturechange", onEditorGestureChange);
wrapperEl.removeEventListener("gestureend", onEditorGestureEnd);
}
if (wrapperEl?.parentNode) {
wrapperEl.parentNode.removeChild(wrapperEl);
}
wrapperEl = null;
}
if (getCurrentInstance()) {
onBeforeUnmount(() => {
destroy();
});
}
return { create, setValue, getValue, openSearch, openReplace, selectRange, destroy, view };
}