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。

2.6 KB
Raw Blame
import { EditorView } from "@codemirror/view";
import type { DatabaseType } from "@/types/database";

export const QUERY_EDITOR_ROOT_SELECTOR = "[data-query-editor-root]";
export const STRUCTURE_PEEK_PANEL_SELECTOR = "[data-structure-peek-panel]";

/** Dialect / connection metadata for inserting into a specific query editor view. */
export interface QueryEditorInsertContext {
  connectionId?: string;
  database?: string;
  schema?: string;
  databaseType?: DatabaseType;
}

let lastFocusedQueryEditorView: EditorView | null = null;
const insertContextByView = new WeakMap<EditorView, QueryEditorInsertContext>();

export function rememberFocusedQueryEditorView(view: EditorView | null | undefined): void {
  if (view) lastFocusedQueryEditorView = view;
}

/** Clear the remembered view when it is destroyed (or clear unconditionally). */
export function clearRememberedFocusedQueryEditorView(view?: EditorView | null): void {
  if (view == null || lastFocusedQueryEditorView === view) {
    lastFocusedQueryEditorView = null;
  }
}

export function registerQueryEditorInsertContext(view: EditorView, context: QueryEditorInsertContext): void {
  insertContextByView.set(view, context);
}

export function unregisterQueryEditorInsertContext(view: EditorView): void {
  insertContextByView.delete(view);
}

export function queryEditorInsertContext(view: EditorView): QueryEditorInsertContext | undefined {
  return insertContextByView.get(view);
}

/**
 * Resolve the CodeMirror view that should receive peek inserts.
 * - Focus inside a query editor → that view (must still report hasFocus).
 * - Focus inside a structure peek panel → most recently focused query editor.
 * - Focus elsewhere (sidebar, etc.) → null.
 */
export function focusedQueryEditorView(active: Element | null = typeof document !== "undefined" ? document.activeElement : null): EditorView | null {
  if (!(active instanceof Element)) return null;

  const root = active.closest(QUERY_EDITOR_ROOT_SELECTOR);
  if (root instanceof HTMLElement) {
    const view = EditorView.findFromDOM(root);
    if (!view?.hasFocus) return null;
    rememberFocusedQueryEditorView(view);
    return view;
  }

  if (active.closest(STRUCTURE_PEEK_PANEL_SELECTOR)) {
    const remembered = lastFocusedQueryEditorView;
    if (remembered?.dom.isConnected) return remembered;
    return null;
  }

  return null;
}

/** Keep editor focus when interacting with non-editable peek chrome (palette-style). */
export function shouldPreserveEditorFocusOnPeekPointerDown(target: EventTarget | null): boolean {
  if (!(target instanceof Element)) return false;
  return !target.closest("input, textarea, button, select, a, [contenteditable='true']");
}