AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(diagram): improve architecture auto layout
tianma-if1b50b864f182
diff --git a/apps/web/src/components/DiagramEditorPane.tsx b/apps/web/src/components/DiagramEditorPane.tsxindex ab46b598..aa4f0b61 100644--- a/apps/web/src/components/DiagramEditorPane.tsx+++ b/apps/web/src/components/DiagramEditorPane.tsx@@ -875,8 +875,14 @@ const diagramEditorSnapshot = (title: string, document: DiagramDocument) => JSON},});-const fitDiagramContent = (graph: Graph, document: DiagramDocument, container: HTMLElement | null, padding = 32) => {- graph.zoomToFit({ padding, maxScale: document.kind === "mind-map" ? 1 : 0.84 });+const fitDiagramContent = (+ graph: Graph,+ document: DiagramDocument,+ container: HTMLElement | null,+ padding = 32,+ minScale?: number,+) => {+ graph.zoomToFit({ padding, maxScale: document.kind === "mind-map" ? 1 : 0.84, ...(minScale ? { minScale } : {}) });if (!container) return;const anchor = document.kind === "mind-map"? graph.getNodes().find((node) => !node.getData<NodeData>()?.parentId)@@ -1810,7 +1816,7 @@ export const DiagramEditorPane = ({}if (document.kind === "architecture") fitArchitectureBoundaries(graph);graph.stopBatch("layout");- fitDiagramContent(graph, document, containerRef.current, 40);+ fitDiagramContent(graph, document, containerRef.current, 40, document.kind === "architecture" ? 0.64 : undefined);if (changed) {setDirty(savedSnapshotRef.current !== diagramEditorSnapshot(titleRef.current,diff --git a/apps/web/src/lib/diagram-layout.test.mjs b/apps/web/src/lib/diagram-layout.test.mjsindex 72dace23..f122bff9 100644--- a/apps/web/src/lib/diagram-layout.test.mjs+++ b/apps/web/src/lib/diagram-layout.test.mjs@@ -97,4 +97,26 @@ describe("diagram auto layout", () => {expect(child.y + child.height).toBeLessThan(boundary.y + boundary.height);}});++ test("wraps a long architecture pipeline by boundary instead of shrinking it into one row", () => {+ const nodes = [];+ const edges = [];+ let previousId;+ for (let groupIndex = 0; groupIndex < 4; groupIndex += 1) {+ const boundaryId = `stage-${groupIndex}`;+ nodes.push({ id: boundaryId, label: `Stage ${groupIndex}`, type: "boundary" });+ for (let nodeIndex = 0; nodeIndex < 3; nodeIndex += 1) {+ const id = `${boundaryId}-node-${nodeIndex}`;+ nodes.push({ id, label: id, type: "service", parentId: boundaryId });+ if (previousId) edges.push({ source: previousId, target: id });+ previousId = id;+ }+ }+ const document = compileDiagramIr({ kind: "architecture", nodes, edges });+ const boundaries = document.nodes.filter((node) => node.shape === "boundary");+ const contentWidth = Math.max(...boundaries.map((node) => node.x + node.width))+ - Math.min(...boundaries.map((node) => node.x));+ expect(contentWidth).toBeLessThanOrEqual(1480);+ expect(new Set(boundaries.map((node) => node.y)).size).toBeGreaterThan(1);+ });});diff --git a/packages/shared/src/diagram-layout.ts b/packages/shared/src/diagram-layout.tsindex 6d4b12cd..0ba39117 100644--- a/packages/shared/src/diagram-layout.ts+++ b/packages/shared/src/diagram-layout.ts@@ -56,6 +56,9 @@ export type DiagramIr = {const MIND_MAP_HORIZONTAL_GAP = 72;const MIND_MAP_VERTICAL_GAP = 16;+const ARCHITECTURE_LAYOUT_ROW_WIDTH = 1480;+const ARCHITECTURE_GROUP_HORIZONTAL_GAP = 72;+const ARCHITECTURE_GROUP_VERTICAL_GAP = 88;const visualTextUnits = (label: string) => Array.from(label).reduce((total, character) => total + (/[^\u0000-\u00ff]/.test(character) ? 1 : 0.55),@@ -173,6 +176,75 @@ const computeMindMapLayout = (return positions;};+const wrapArchitectureGroups = (+ document: DiagramDocument,+ positions: DiagramLayoutPositions,+) => {+ const nodeById = new Map(document.nodes.map((node) => [node.id, node]));+ const topLevelBoundaryId = (nodeId: string) => {+ let current = nodeById.get(nodeId);+ let boundaryId: string | undefined;+ const visited = new Set<string>();+ while (current?.parentId && !visited.has(current.parentId)) {+ visited.add(current.parentId);+ const parent = nodeById.get(current.parentId);+ if (!parent) break;+ if (parent.shape === "boundary") boundaryId = parent.id;+ current = parent;+ }+ return boundaryId;+ };++ const topLevelBoundaries = document.nodes.filter((node) => (+ node.shape === "boundary" && (!node.parentId || nodeById.get(node.parentId)?.shape !== "boundary")+ ));+ if (topLevelBoundaries.length < 2) return positions;++ const groups = topLevelBoundaries.flatMap((boundary) => {+ const members = document.nodes.filter((node) => (+ node.shape !== "boundary" && topLevelBoundaryId(node.id) === boundary.id && positions[node.id]+ ));+ if (members.length === 0) return [];+ const left = Math.min(...members.map((node) => positions[node.id].x));+ const top = Math.min(...members.map((node) => positions[node.id].y));+ const right = Math.max(...members.map((node) => positions[node.id].x + node.width));+ const bottom = Math.max(...members.map((node) => positions[node.id].y + node.height));+ return [{+ boundary,+ members: document.nodes.filter((node) => node.id === boundary.id || topLevelBoundaryId(node.id) === boundary.id),+ left,+ top,+ width: Math.max(260, right - left + 72),+ height: Math.max(180, bottom - top + 92),+ }];+ }).sort((left, right) => left.left - right.left || left.top - right.top || left.boundary.id.localeCompare(right.boundary.id));+ if (groups.length < 2) return positions;++ const contentLeft = Math.min(...groups.map((group) => group.left - 36));+ const contentRight = Math.max(...groups.map((group) => group.left - 36 + group.width));+ if (contentRight - contentLeft <= ARCHITECTURE_LAYOUT_ROW_WIDTH) return positions;++ let cursorX = 32;+ let cursorY = 32;+ let rowHeight = 0;+ for (const group of groups) {+ if (cursorX > 32 && cursorX + group.width > 32 + ARCHITECTURE_LAYOUT_ROW_WIDTH) {+ cursorX = 32;+ cursorY += rowHeight + ARCHITECTURE_GROUP_VERTICAL_GAP;+ rowHeight = 0;+ }+ const deltaX = cursorX + 36 - group.left;+ const deltaY = cursorY + 56 - group.top;+ for (const node of group.members) {+ const position = positions[node.id] ?? { x: node.x, y: node.y };+ positions[node.id] = { x: position.x + deltaX, y: position.y + deltaY };+ }+ cursorX += group.width + ARCHITECTURE_GROUP_HORIZONTAL_GAP;+ rowHeight = Math.max(rowHeight, group.height);+ }+ return positions;+};+export const computeDiagramLayout = (document: DiagramDocument,options: DiagramLayoutOptions = {},@@ -198,7 +270,7 @@ export const computeDiagramLayout = (}runDagreLayout(layoutGraph);- return Object.fromEntries(document.nodes.flatMap((node) => {+ const positions = Object.fromEntries(document.nodes.flatMap((node) => {if (node.shape === "boundary") return [[node.id, { x: node.x, y: node.y }]];const position = layoutGraph.node(node.id) as { x: number; y: number } | undefined;if (!position) return [];@@ -207,6 +279,7 @@ export const computeDiagramLayout = (y: Math.round(position.y - node.height / 2),}]];}));+ return document.kind === "architecture" ? wrapArchitectureGroups(document, positions) : positions;};const irNodeShape = (kind: DiagramKind, type: DiagramIrNodeType | undefined): DiagramNodeShape => {