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。

1.3 KB
Raw Blame
/**
 * Focus logic for the query editor CodeMirror view.
 *
 * Extracted from QueryEditor.vue so the guard condition can be unit-tested
 * without mounting the full Vue component.
 */

export interface EditorViewLike {
  hasFocus: boolean;
  focus(): void;
}

const PRESERVED_EDITOR_FOCUS_SELECTOR = "[data-preserve-editor-focus]";

/**
 * A surrounding surface can temporarily own focus while a query editor is
 * mounting (for example, the SQL library's inline rename input). Query editor
 * autofocus must respect that handoff instead of immediately stealing focus.
 */
function focusPreservedElement(): boolean {
  if (typeof document === "undefined") return false;
  const target = document.querySelector<HTMLElement>(PRESERVED_EDITOR_FOCUS_SELECTOR);
  if (!target || document.activeElement !== target) return false;
  target.focus();
  return true;
}

/**
 * Focus the editor if it exists and does not already have focus, while
 * respecting a surrounding control that currently owns a focus handoff.
 * Returns `true` when `view.focus()` was actually called, `false` otherwise.
 */
export function focusEditorView(view: EditorViewLike | null | undefined): boolean {
  if (focusPreservedElement()) return false;
  if (!view || view.hasFocus) return false;
  view.focus();
  return true;
}