fix(diagram): virtualize large relationship graphs
This commit is contained in:
3 files changed
+245
-73
No files matched your search
@@ -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<HTMLElement | null>(null);
|
||||
const engineeringViewportRef = ref<HTMLElement | null>(null);
|
||||
const edgeWaypoints = ref<Record<string, Point[]>>({});
|
||||
const edgeHandleHints = ref<Record<string, { sourceHandle?: string; targetHandle?: string }>>({});
|
||||
const highlightEdgeId = ref<string | null>(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<EngineeringDiagramViewport>({
|
||||
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(() => {
|
||||
</VueFlow>
|
||||
<ZoomControls :can-undo="graphStore.canUndo" :can-redo="graphStore.canRedo" @undo="handleUndo" @redo="handleRedo" />
|
||||
</div>
|
||||
<div v-else class="min-h-0 h-full overflow-auto">
|
||||
<div v-else ref="engineeringViewportRef" class="min-h-0 h-full overflow-auto" @scroll.passive="scheduleEngineeringViewportSync">
|
||||
<div class="relative" :style="{ width: `${activeCanvasSize.width}px`, height: `${activeCanvasSize.height}px` }">
|
||||
<svg class="absolute inset-0 h-full w-full overflow-visible pointer-events-none">
|
||||
<g class="stroke-foreground/70">
|
||||
<line
|
||||
v-for="attribute in engineeringDiagram.attributes"
|
||||
:key="attribute.id"
|
||||
:x1="engineeringDiagram.entities.find((e) => e.name === attribute.tableName) ? engineeringDiagram.entities.find((e) => e.name === attribute.tableName)!.x + engineeringDiagram.entities.find((e) => e.name === attribute.tableName)!.width / 2 : 0"
|
||||
:y1="engineeringDiagram.entities.find((e) => e.name === attribute.tableName) ? engineeringDiagram.entities.find((e) => e.name === attribute.tableName)!.y + engineeringDiagram.entities.find((e) => e.name === attribute.tableName)!.height / 2 : 0"
|
||||
:x2="attribute.x + attribute.width / 2"
|
||||
:y2="attribute.y + attribute.height / 2"
|
||||
stroke-width="1.2"
|
||||
/>
|
||||
<template v-for="relationship in engineeringDiagram.relationships" :key="relationship.id">
|
||||
<line
|
||||
:x1="
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.sourceTable && a.columnName === relationship.sourceColumn)
|
||||
? engineeringDiagram.attributes.find((a) => a.tableName === relationship.sourceTable && a.columnName === relationship.sourceColumn)!.x +
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.sourceTable && a.columnName === relationship.sourceColumn)!.width / 2
|
||||
: engineeringDiagram.entities.find((e) => e.name === relationship.sourceTable)
|
||||
? engineeringDiagram.entities.find((e) => e.name === relationship.sourceTable)!.x + engineeringDiagram.entities.find((e) => e.name === relationship.sourceTable)!.width / 2
|
||||
: 0
|
||||
"
|
||||
:y1="
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.sourceTable && a.columnName === relationship.sourceColumn)
|
||||
? engineeringDiagram.attributes.find((a) => a.tableName === relationship.sourceTable && a.columnName === relationship.sourceColumn)!.y +
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.sourceTable && a.columnName === relationship.sourceColumn)!.height / 2
|
||||
: engineeringDiagram.entities.find((e) => e.name === relationship.sourceTable)
|
||||
? engineeringDiagram.entities.find((e) => e.name === relationship.sourceTable)!.y + engineeringDiagram.entities.find((e) => e.name === relationship.sourceTable)!.height / 2
|
||||
: 0
|
||||
"
|
||||
:x2="relationship.x + relationship.width / 2"
|
||||
:y2="relationship.y + relationship.height / 2"
|
||||
stroke-width="1.4"
|
||||
/>
|
||||
<line
|
||||
:x1="relationship.x + relationship.width / 2"
|
||||
:y1="relationship.y + relationship.height / 2"
|
||||
:x2="
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.targetTable && a.columnName === relationship.targetColumn)
|
||||
? engineeringDiagram.attributes.find((a) => a.tableName === relationship.targetTable && a.columnName === relationship.targetColumn)!.x +
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.targetTable && a.columnName === relationship.targetColumn)!.width / 2
|
||||
: engineeringDiagram.entities.find((e) => e.name === relationship.targetTable)
|
||||
? engineeringDiagram.entities.find((e) => e.name === relationship.targetTable)!.x + engineeringDiagram.entities.find((e) => e.name === relationship.targetTable)!.width / 2
|
||||
: 0
|
||||
"
|
||||
:y2="
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.targetTable && a.columnName === relationship.targetColumn)
|
||||
? engineeringDiagram.attributes.find((a) => a.tableName === relationship.targetTable && a.columnName === relationship.targetColumn)!.y +
|
||||
engineeringDiagram.attributes.find((a) => a.tableName === relationship.targetTable && a.columnName === relationship.targetColumn)!.height / 2
|
||||
: engineeringDiagram.entities.find((e) => e.name === relationship.targetTable)
|
||||
? engineeringDiagram.entities.find((e) => e.name === relationship.targetTable)!.y + engineeringDiagram.entities.find((e) => e.name === relationship.targetTable)!.height / 2
|
||||
: 0
|
||||
"
|
||||
stroke-width="1.4"
|
||||
/>
|
||||
</template>
|
||||
<line v-for="line in engineeringRenderSlice.attributeLines" :key="line.id" :x1="line.x1" :y1="line.y1" :x2="line.x2" :y2="line.y2" stroke-width="1.2" />
|
||||
<line v-for="line in engineeringRenderSlice.relationshipLines" :key="line.id" :x1="line.x1" :y1="line.y1" :x2="line.x2" :y2="line.y2" stroke-width="1.4" />
|
||||
</g>
|
||||
</svg>
|
||||
<div
|
||||
v-for="attribute in engineeringDiagram.attributes"
|
||||
v-for="attribute in engineeringRenderSlice.attributes"
|
||||
:key="attribute.id"
|
||||
class="absolute flex items-center justify-center rounded-full border border-green-600/55 bg-green-100/80 px-3 text-center text-xs text-green-950 shadow-sm dark:bg-green-950/35 dark:text-green-100"
|
||||
:class="attribute.primaryKey ? 'font-semibold underline underline-offset-2' : ''"
|
||||
@@ -2248,7 +2262,7 @@ onUnmounted(() => {
|
||||
<span class="truncate">{{ attribute.label }}</span>
|
||||
</div>
|
||||
<div
|
||||
v-for="relationship in engineeringDiagram.relationships"
|
||||
v-for="relationship in engineeringRenderSlice.relationships"
|
||||
:key="relationship.id"
|
||||
class="absolute flex items-center justify-center text-center text-xs font-medium text-red-950 dark:text-red-100"
|
||||
:style="{ width: `${relationship.width}px`, height: `${relationship.height}px`, transform: `translate(${relationship.x}px, ${relationship.y}px)` }"
|
||||
@@ -2258,7 +2272,7 @@ onUnmounted(() => {
|
||||
<span class="relative max-w-[70px] truncate">{{ relationship.label }}</span>
|
||||
</div>
|
||||
<div
|
||||
v-for="entity in engineeringDiagram.entities"
|
||||
v-for="entity in engineeringRenderSlice.entities"
|
||||
:key="entity.id"
|
||||
class="absolute flex cursor-pointer items-center justify-center border border-blue-500/70 bg-blue-100/80 px-3 text-center text-sm font-semibold text-blue-950 shadow-sm dark:bg-blue-950/35 dark:text-blue-100"
|
||||
:class="entity.name === focusTableName ? 'ring-2 ring-primary/40' : ''"
|
||||
|
||||
@@ -86,6 +86,32 @@ export interface EngineeringDiagram {
|
||||
};
|
||||
}
|
||||
|
||||
export interface EngineeringDiagramLine {
|
||||
id: string;
|
||||
x1: number;
|
||||
y1: number;
|
||||
x2: number;
|
||||
y2: number;
|
||||
}
|
||||
|
||||
export interface EngineeringDiagramConnections {
|
||||
attributeLines: EngineeringDiagramLine[];
|
||||
relationshipLines: EngineeringDiagramLine[];
|
||||
}
|
||||
|
||||
export interface EngineeringDiagramViewport {
|
||||
left: number;
|
||||
top: number;
|
||||
right: number;
|
||||
bottom: number;
|
||||
}
|
||||
|
||||
export interface EngineeringDiagramRenderSlice extends EngineeringDiagramConnections {
|
||||
entities: EngineeringEntityNode[];
|
||||
attributes: EngineeringAttributeNode[];
|
||||
relationships: EngineeringRelationshipNode[];
|
||||
}
|
||||
|
||||
function attributeWidth(label: string): number {
|
||||
return Math.min(ATTRIBUTE_MAX_WIDTH, Math.max(ATTRIBUTE_MIN_WIDTH, label.length * 10 + 30));
|
||||
}
|
||||
@@ -250,8 +276,8 @@ function orderedTableRows(tables: DiagramTable[], positions: Record<string, Diag
|
||||
}
|
||||
|
||||
function normalizeDiagram(diagram: Omit<EngineeringDiagram, "canvas">): EngineeringDiagram {
|
||||
const rects = [...diagram.entities, ...diagram.attributes, ...diagram.relationships];
|
||||
if (rects.length === 0) {
|
||||
const groups: ReadonlyArray<Array<{ x: number; y: number; width: number; height: number }>> = [diagram.entities, diagram.attributes, diagram.relationships];
|
||||
if (groups.every((group) => group.length === 0)) {
|
||||
return {
|
||||
...diagram,
|
||||
canvas: {
|
||||
@@ -261,23 +287,30 @@ function normalizeDiagram(diagram: Omit<EngineeringDiagram, "canvas">): 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 = <T extends { x: number; y: number }>(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<EngineeringDiagram, "canvas">): 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,
|
||||
|
||||
Reference in new issue
Block a user