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。

3.9 KB
Raw Blame
import type { Command, EditorView, KeyBinding } from "@codemirror/view";
import { shortcutToCodeMirrorKey, type ShortcutSettings } from "@/lib/editor/shortcutRegistry";
import { isExecuteSqlShortcut, isExecuteAllSqlShortcut, isExecuteSqlInNewResultTabShortcut, isExplainSqlShortcut } from "@/lib/editor/keyboardShortcuts";

const SAFARI_POST_COMPOSITION_KEY_WINDOW_MS = 100;

type BrowserNavigator = Pick<Navigator, "maxTouchPoints" | "userAgent" | "vendor">;

function isDesktopSafari(currentNavigator: BrowserNavigator | undefined): boolean {
  if (!currentNavigator) return false;
  return /Apple Computer/.test(currentNavigator.vendor) && !/Mobile\/\w+/.test(currentNavigator.userAgent) && currentNavigator.maxTouchPoints <= 2;
}

export function createQueryEditorPostCompositionKeyGuard(options: { navigator?: BrowserNavigator; now?: () => number } = {}) {
  const currentNavigator = options.navigator ?? (typeof navigator === "undefined" ? undefined : navigator);
  const now = options.now ?? Date.now;
  const desktopSafari = isDesktopSafari(currentNavigator);
  const blockedEvents = new WeakSet<KeyboardEvent>();
  let compositionEndedAt = 0;
  let compositionPendingKey = false;

  function handleCompositionStart() {
    compositionPendingKey = false;
  }

  function handleCompositionEnd() {
    compositionPendingKey = desktopSafari;
    compositionEndedAt = desktopSafari ? now() : 0;
  }

  function handleKeydown(event: KeyboardEvent) {
    if (!compositionPendingKey) return;
    compositionPendingKey = false;
    if (now() - compositionEndedAt < SAFARI_POST_COMPOSITION_KEY_WINDOW_MS) blockedEvents.add(event);
  }

  return {
    attach(element: HTMLElement) {
      element.addEventListener("compositionstart", handleCompositionStart, true);
      element.addEventListener("compositionend", handleCompositionEnd, true);
      element.addEventListener("keydown", handleKeydown, true);
      return () => {
        element.removeEventListener("compositionstart", handleCompositionStart, true);
        element.removeEventListener("compositionend", handleCompositionEnd, true);
        element.removeEventListener("keydown", handleKeydown, true);
        compositionPendingKey = false;
      };
    },
    blocks(event: KeyboardEvent) {
      return blockedEvents.has(event);
    },
  };
}

/**
 * Create a query-editor execution binding that consumes the shortcut while an
 * IME composition is active, without invoking the execution callback.
 *
 * CodeMirror may ignore keydown events after a composition has changed the
 * document, so the app-level shortcut fallback also checks the editor state.
 * This guard covers the keymap path when CodeMirror still dispatches it.
 */
export function createQueryEditorExecutionShortcutBindings(shortcut: string, run: Command, isComposing: (view: EditorView) => boolean, isEnabled: () => boolean = () => true): KeyBinding[] {
  if (!shortcut) return [];
  return [
    {
      key: shortcutToCodeMirrorKey(shortcut),
      preventDefault: true,
      run(view) {
        if (isComposing(view) || !isEnabled()) return true;
        return run(view);
      },
    },
  ];
}

/**
 * Intercept DOM keydown events for execution shortcuts to prevent duplicate
 * executions caused by key repeat or while a query is already running.
 */
export function createQueryEditorExecutionShortcutHandler(options: { getShortcuts: () => Partial<ShortcutSettings>; isExecuting?: () => boolean; enableExplainShortcut?: () => boolean }): (event: KeyboardEvent) => boolean {
  return (event: KeyboardEvent) => {
    if (!event.repeat && !options.isExecuting?.()) return false;
    const shortcuts = options.getShortcuts();
    const isExecution = isExecuteSqlShortcut(event, shortcuts) || isExecuteAllSqlShortcut(event, shortcuts) || isExecuteSqlInNewResultTabShortcut(event, shortcuts) || (options.enableExplainShortcut?.() && isExplainSqlShortcut(event, shortcuts));
    if (!isExecution) return false;
    event.preventDefault();
    event.stopPropagation();
    return true;
  };
}