diff --git a/apps/desktop/src/components/diagram/SchemaDiagramDialog.vue b/apps/desktop/src/components/diagram/SchemaDiagramDialog.vue index 39a5695879..29ae9a3eb4 100644 --- a/apps/desktop/src/components/diagram/SchemaDiagramDialog.vue +++ b/apps/desktop/src/components/diagram/SchemaDiagramDialog.vue @@ -27,7 +27,7 @@ import type { InspectorTarget } from "@/types/diagram"; import DiagramInspector from "./DiagramInspector.vue"; import DiagramSyncDialog from "./DiagramSyncDialog.vue"; import CreateDraftTableDialog from "./CreateDraftTableDialog.vue"; -import { buildEngineeringDiagram } from "@/lib/diagram/engineeringDiagram"; +import { buildEngineeringDiagram, buildEngineeringDiagramConnections, sliceEngineeringDiagramForViewport, type EngineeringDiagramViewport } from "@/lib/diagram/engineeringDiagram"; import { buildEngineeringDiagramSvg, buildTableDiagramSvg, buildTableRelationshipPolylines, computeTableDiagramCanvas } from "@/lib/export/diagramSvgExport"; import { pointsToSvgPath } from "@/lib/diagram/edge-obstacle-router"; import { buildDiagramDbml, buildDiagramJson, buildDiagramMermaid, diagramExportFileName, svgToPngBlob, type DiagramExportFormat } from "@/lib/export/diagramFormats"; @@ -64,6 +64,7 @@ const layerStore = useLayerStore(); const { nodes, edges, applyNodeChanges, applyEdgeChanges, setNodes, setEdges, zoomIn: vfZoomIn, zoomOut: vfZoomOut, fitView, getViewport, setViewport } = useVueFlow(); const diagramPaneRef = ref(null); +const engineeringViewportRef = ref(null); const edgeWaypoints = ref>({}); const edgeHandleHints = ref>({}); const highlightEdgeId = ref(null); @@ -291,6 +292,9 @@ const emit = defineEmits<{ }>(); const METADATA_BATCH_SIZE = 4; +const LARGE_TABLE_DIAGRAM_THRESHOLD = 200; +const LARGE_ENGINEERING_DIAGRAM_THRESHOLD = 2_000; +const ENGINEERING_VIEWPORT_OVERSCAN = 800; const connectionId = ref(""); const database = ref(""); @@ -327,6 +331,9 @@ async function setDiagramMode(mode: "table" | "engineering") { if (mode === "table") { await nextTick(); fitDiagramView(); + } else { + await nextTick(); + syncEngineeringViewport(); } } @@ -549,9 +556,51 @@ const canvasSize = computed(() => { }); const engineeringDiagram = computed(() => buildEngineeringDiagram(visibleTables.value, visibleRelationships.value, positions.value)); +const engineeringConnections = computed(() => buildEngineeringDiagramConnections(engineeringDiagram.value)); +const virtualizeTableDiagram = computed(() => visibleTables.value.length > LARGE_TABLE_DIAGRAM_THRESHOLD); +const virtualizeEngineeringDiagram = computed(() => engineeringDiagram.value.entities.length + engineeringDiagram.value.attributes.length + engineeringDiagram.value.relationships.length > LARGE_ENGINEERING_DIAGRAM_THRESHOLD); +const engineeringViewport = ref({ + left: -ENGINEERING_VIEWPORT_OVERSCAN, + top: -ENGINEERING_VIEWPORT_OVERSCAN, + right: 1920 + ENGINEERING_VIEWPORT_OVERSCAN, + bottom: 1080 + ENGINEERING_VIEWPORT_OVERSCAN, +}); +const engineeringRenderSlice = computed(() => { + const diagram = engineeringDiagram.value; + const connections = engineeringConnections.value; + if (!virtualizeEngineeringDiagram.value) { + return { + entities: diagram.entities, + attributes: diagram.attributes, + relationships: diagram.relationships, + ...connections, + }; + } + return sliceEngineeringDiagramForViewport(diagram, connections, engineeringViewport.value); +}); const activeCanvasSize = computed(() => (diagramMode.value === "engineering" ? engineeringDiagram.value.canvas : canvasSize.value)); +let engineeringViewportFrame: number | null = null; +let engineeringResizeObserver: ResizeObserver | null = null; + +function syncEngineeringViewport() { + engineeringViewportFrame = null; + const viewport = engineeringViewportRef.value; + if (!viewport) return; + engineeringViewport.value = { + left: viewport.scrollLeft - ENGINEERING_VIEWPORT_OVERSCAN, + top: viewport.scrollTop - ENGINEERING_VIEWPORT_OVERSCAN, + right: viewport.scrollLeft + viewport.clientWidth + ENGINEERING_VIEWPORT_OVERSCAN, + bottom: viewport.scrollTop + viewport.clientHeight + ENGINEERING_VIEWPORT_OVERSCAN, + }; +} + +function scheduleEngineeringViewportSync() { + if (engineeringViewportFrame !== null) return; + engineeringViewportFrame = window.requestAnimationFrame(syncEngineeringViewport); +} + function handleAddLayer() { recordHistory(); const placement = placeNewLayer(layerStore.layers, positions.value, tableHeightsMap()); @@ -2044,6 +2093,15 @@ watch( }, ); +watch(engineeringViewportRef, (viewport) => { + engineeringResizeObserver?.disconnect(); + engineeringResizeObserver = null; + if (!viewport || typeof ResizeObserver === "undefined") return; + engineeringResizeObserver = new ResizeObserver(scheduleEngineeringViewportSync); + engineeringResizeObserver.observe(viewport); + scheduleEngineeringViewportSync(); +}); + onMounted(() => { window.addEventListener("keydown", handleKeydown); window.addEventListener("keyup", handleKeyup); @@ -2052,6 +2110,12 @@ onMounted(() => { onUnmounted(() => { window.removeEventListener("keydown", handleKeydown); window.removeEventListener("keyup", handleKeyup); + if (engineeringViewportFrame !== null) { + window.cancelAnimationFrame(engineeringViewportFrame); + engineeringViewportFrame = null; + } + engineeringResizeObserver?.disconnect(); + engineeringResizeObserver = null; teardownFullscreenListeners(); if (diagramOwnedFullscreen) { void exitFullscreen(); @@ -2163,6 +2227,7 @@ onUnmounted(() => { :min-zoom="0.05" :max-zoom="2" :fit-view-options="{ padding: 0.15, minZoom: 0.05, maxZoom: 2 }" + :only-render-visible-elements="virtualizeTableDiagram" :pan-mode="isSpacePressed ? 'always' : undefined" class="diagram-flow w-full h-full" @nodes-change="handleNodesChange" @@ -2178,67 +2243,16 @@ onUnmounted(() => { -
+
- - + +
{ {{ attribute.label }}
{ {{ relationship.label }}
): EngineeringDiagram { - const rects = [...diagram.entities, ...diagram.attributes, ...diagram.relationships]; - if (rects.length === 0) { + const groups: ReadonlyArray> = [diagram.entities, diagram.attributes, diagram.relationships]; + if (groups.every((group) => group.length === 0)) { return { ...diagram, canvas: { @@ -261,23 +287,30 @@ function normalizeDiagram(diagram: Omit): Engineer }; } - const minX = Math.min(...rects.map((rect) => rect.x)); - const minY = Math.min(...rects.map((rect) => rect.y)); - const maxX = Math.max(...rects.map((rect) => rect.x + rect.width)); - const maxY = Math.max(...rects.map((rect) => rect.y + rect.height)); + let minX = Number.POSITIVE_INFINITY; + let minY = Number.POSITIVE_INFINITY; + let maxX = Number.NEGATIVE_INFINITY; + let maxY = Number.NEGATIVE_INFINITY; + for (const group of groups) { + for (const rect of group) { + minX = Math.min(minX, rect.x); + minY = Math.min(minY, rect.y); + maxX = Math.max(maxX, rect.x + rect.width); + maxY = Math.max(maxY, rect.y + rect.height); + } + } const dx = CANVAS_PADDING - minX; const dy = CANVAS_PADDING - minY; - const shift = (node: T): T => ({ - ...node, - x: node.x + dx, - y: node.y + dy, - }); + for (const group of groups) { + for (const rect of group) { + rect.x += dx; + rect.y += dy; + } + } return { - entities: diagram.entities.map(shift), - attributes: diagram.attributes.map(shift), - relationships: diagram.relationships.map(shift), + ...diagram, canvas: { width: maxX + dx + CANVAS_PADDING, height: maxY + dy + CANVAS_PADDING, @@ -285,6 +318,69 @@ function normalizeDiagram(diagram: Omit): Engineer }; } +function nodeIntersectsViewport(node: { x: number; y: number; width: number; height: number }, viewport: EngineeringDiagramViewport): boolean { + return node.x <= viewport.right && node.x + node.width >= viewport.left && node.y <= viewport.bottom && node.y + node.height >= viewport.top; +} + +function lineIntersectsViewport(line: EngineeringDiagramLine, viewport: EngineeringDiagramViewport): boolean { + return Math.min(line.x1, line.x2) <= viewport.right && Math.max(line.x1, line.x2) >= viewport.left && Math.min(line.y1, line.y2) <= viewport.bottom && Math.max(line.y1, line.y2) >= viewport.top; +} + +export function buildEngineeringDiagramConnections(diagram: EngineeringDiagram): EngineeringDiagramConnections { + const entityMap = new Map(diagram.entities.map((entity) => [entity.name, entity])); + const attributeMap = new Map(diagram.attributes.map((attribute) => [`${attribute.tableName}:${attribute.columnName}`, attribute])); + const attributeLines: EngineeringDiagramLine[] = []; + for (const attribute of diagram.attributes) { + const entity = entityMap.get(attribute.tableName); + if (!entity) continue; + attributeLines.push({ + id: `attribute:${attribute.id}`, + x1: entity.x + entity.width / 2, + y1: entity.y + entity.height / 2, + x2: attribute.x + attribute.width / 2, + y2: attribute.y + attribute.height / 2, + }); + } + const relationshipLines: EngineeringDiagramLine[] = []; + for (const relationship of diagram.relationships) { + const source = attributeMap.get(`${relationship.sourceTable}:${relationship.sourceColumn}`) ?? entityMap.get(relationship.sourceTable); + const target = attributeMap.get(`${relationship.targetTable}:${relationship.targetColumn}`) ?? entityMap.get(relationship.targetTable); + if (!source || !target) continue; + const relationshipCenter = { + x: relationship.x + relationship.width / 2, + y: relationship.y + relationship.height / 2, + }; + relationshipLines.push( + { + id: `relationship:${relationship.id}:source`, + x1: source.x + source.width / 2, + y1: source.y + source.height / 2, + x2: relationshipCenter.x, + y2: relationshipCenter.y, + }, + { + id: `relationship:${relationship.id}:target`, + x1: relationshipCenter.x, + y1: relationshipCenter.y, + x2: target.x + target.width / 2, + y2: target.y + target.height / 2, + }, + ); + } + + return { attributeLines, relationshipLines }; +} + +export function sliceEngineeringDiagramForViewport(diagram: EngineeringDiagram, connections: EngineeringDiagramConnections, viewport: EngineeringDiagramViewport): EngineeringDiagramRenderSlice { + return { + entities: diagram.entities.filter((entity) => nodeIntersectsViewport(entity, viewport)), + attributes: diagram.attributes.filter((attribute) => nodeIntersectsViewport(attribute, viewport)), + relationships: diagram.relationships.filter((relationship) => nodeIntersectsViewport(relationship, viewport)), + attributeLines: connections.attributeLines.filter((line) => lineIntersectsViewport(line, viewport)), + relationshipLines: connections.relationshipLines.filter((line) => lineIntersectsViewport(line, viewport)), + }; +} + function attributeCenter(attribute: EngineeringAttributeNode): DiagramPosition { return { x: attribute.x + attribute.width / 2, diff --git a/packages/app-tests/engineeringDiagram.test.ts b/packages/app-tests/engineeringDiagram.test.ts index a9679ff2c2..b13994d748 100644 --- a/packages/app-tests/engineeringDiagram.test.ts +++ b/packages/app-tests/engineeringDiagram.test.ts @@ -1,6 +1,6 @@ import { strict as assert } from "node:assert"; import { test } from "vitest"; -import { buildEngineeringDiagram } from "../../apps/desktop/src/lib/diagram/engineeringDiagram.ts"; +import { buildEngineeringDiagram, buildEngineeringDiagramConnections, sliceEngineeringDiagramForViewport } from "../../apps/desktop/src/lib/diagram/engineeringDiagram.ts"; import type { DiagramRelationship, DiagramTable } from "../../apps/desktop/src/lib/diagram/erDiagram.ts"; const tables: DiagramTable[] = [ @@ -102,3 +102,65 @@ test("keeps adjacent entity centers reasonably close", () => { assert.ok(Math.abs(orderCenter - userCenter) <= 560); }); + +test("precomputes engineering connection lines and slices them by viewport", () => { + const diagram = buildEngineeringDiagram(tables, relationships, { + users: { x: 40, y: 40 }, + orders: { x: 360, y: 40 }, + }); + const connections = buildEngineeringDiagramConnections(diagram); + const users = diagram.entities.find((entity) => entity.name === "users")!; + const viewport = { + left: users.x - 20, + top: users.y - 20, + right: users.x + users.width + 20, + bottom: users.y + users.height + 20, + }; + const slice = sliceEngineeringDiagramForViewport(diagram, connections, viewport); + + assert.equal(connections.attributeLines.length, diagram.attributes.length); + assert.equal(connections.relationshipLines.length, diagram.relationships.length * 2); + assert.ok(slice.entities.some((entity) => entity.name === "users")); + assert.ok(slice.entities.length < diagram.entities.length); + assert.ok(slice.attributeLines.length > 0); +}); + +test("builds a 1268-table engineering diagram without overflowing the call stack", () => { + const largeTables: DiagramTable[] = Array.from({ length: 1268 }, (_, tableIndex) => ({ + name: `table_${tableIndex}`, + columns: Array.from({ length: 100 }, (_, columnIndex) => ({ + name: `column_${columnIndex}`, + data_type: "varchar", + is_nullable: true, + column_default: null, + is_primary_key: columnIndex === 0, + extra: null, + })), + foreignKeys: [], + })); + const positions = Object.fromEntries( + largeTables.map((table, index) => [ + table.name, + { + x: (index % 4) * 440, + y: Math.floor(index / 4) * 600, + }, + ]), + ); + + const diagram = buildEngineeringDiagram(largeTables, [], positions); + const connections = buildEngineeringDiagramConnections(diagram); + const slice = sliceEngineeringDiagramForViewport(diagram, connections, { + left: 0, + top: 0, + right: 1920, + bottom: 1080, + }); + + assert.equal(diagram.entities.length, 1268); + assert.equal(diagram.attributes.length, 126_800); + assert.ok(Number.isFinite(diagram.canvas.width)); + assert.ok(Number.isFinite(diagram.canvas.height)); + assert.ok(slice.entities.length < diagram.entities.length); + assert.ok(slice.attributes.length < diagram.attributes.length); +});