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.7 KB
Raw Blame
import { ref, type Ref } from "vue";
import type { DiffHunk } from "@/components/diff/DiffHunkBuilder";

export interface UseDiffScrollSyncOptions {
  container: Ref<HTMLElement | undefined>;
  leftPane: Ref<HTMLElement | undefined>;
  rightPane: Ref<HTMLElement | undefined>;
  hunks: Ref<DiffHunk[]>;
}

export function useDiffScrollSync({ container, leftPane, rightPane, hunks }: UseDiffScrollSyncOptions) {
  const isSyncingScroll = ref(false);

  function syncScroll(from: "left" | "right") {
    if (isSyncingScroll.value) return;
    const source = from === "left" ? leftPane.value : rightPane.value;
    const target = from === "left" ? rightPane.value : leftPane.value;
    if (!source || !target) return;
    isSyncingScroll.value = true;
    target.scrollTop = source.scrollTop;
    target.scrollLeft = source.scrollLeft;
    isSyncingScroll.value = false;
  }

  function measureHunks() {
    const outer = container.value;
    const left = leftPane.value;
    const right = rightPane.value;
    if (!outer || !left || !right) return;

    const outerRect = outer.getBoundingClientRect();

    for (const hunk of hunks.value) {
      const leftEl = left.querySelector(`[data-hunk-id="${hunk.id}"]`) as HTMLElement | null;
      const rightEl = right.querySelector(`[data-hunk-id="${hunk.id}"]`) as HTMLElement | null;
      if (leftEl) {
        const rect = leftEl.getBoundingClientRect();
        hunk.leftTop = rect.top - outerRect.top;
        hunk.leftBottom = rect.bottom - outerRect.top;
      }
      if (rightEl) {
        const rect = rightEl.getBoundingClientRect();
        hunk.rightTop = rect.top - outerRect.top;
        hunk.rightBottom = rect.bottom - outerRect.top;
      }
    }
  }

  return {
    syncScroll,
    measureHunks,
  };
}