Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(diagram): keep auto layout at a readable 75% zoom

tianma-if8584a4a850c6
diff --git a/apps/web/src/components/DiagramEditorPane.test.mjs b/apps/web/src/components/DiagramEditorPane.test.mjs
index 3520ce57..027c8552 100644
--- a/apps/web/src/components/DiagramEditorPane.test.mjs
+++ b/apps/web/src/components/DiagramEditorPane.test.mjs
@@ -199,13 +199,17 @@ describe("diagram editor canvas surface", () => {
expect(source).toContain("bindDiagramScrollerFit(graph)");
expect(source).toContain("applyDiagramScrollerFitOptions(");
expect(source).toContain("diagramNodeBounds(graph)");
- expect(source).toContain("graph.scale().sx < policy.minScale");
- expect(source).toContain("readFlowchart(graph, document, container)");
+ expect(source).toContain("flowchartFitsReadableViewport(bounds, size, padding, minScale, policy.maxScale)");
+ expect(source).toContain("readDiagramContent(graph, document)");
+ expect(source).toContain("diagramReaderFocusNode(document)");
expect(source).toContain("scroller.positionPoint({ x: box.x + box.width / 2, y: box.y }, \"50%\", 48)");
+ expect(source).toContain('policy.anchor === "leftmost"');
+ expect(source).toContain("scroller.positionPoint({ x: origin.x, y: origin.y }, 40, 48)");
expect(source).toContain("fitDiagramRect(graph, bounds, { padding, maxScale: policy.maxScale })");
expect(source).toContain("scroller.zoomToRect(bounds, options)");
expect(source).toContain("getDiagramLayoutViewport(document.kind)");
expect(source).toContain("fitDiagramContent(graph, document, containerRef.current);");
+ expect(source).toContain("fitDiagramContent(graph, document, containerRef.current, 40, layout.viewport, { readable: true })");
});
test("labels auto layout directly instead of relying on an ambiguous icon", () => {
@@ -219,7 +223,7 @@ describe("diagram editor canvas surface", () => {
expect(toolbarSource).toContain("onFit");
expect(toolbarSource).toContain('t("diagram.fit")');
expect(source).toContain("onFit={() =>");
- expect(source).toContain("fitDiagramContent(graph, document, containerRef.current, 40, layout.viewport);");
+ expect(source).toContain("fitDiagramContent(graph, document, containerRef.current, 40, layout.viewport, { readable: true });");
});
test("delegates every diagram kind to one shared toolbar shell", () => {
diff --git a/apps/web/src/components/DiagramEditorPane.tsx b/apps/web/src/components/DiagramEditorPane.tsx
index be3795a4..87b4f80a 100644
--- a/apps/web/src/components/DiagramEditorPane.tsx
+++ b/apps/web/src/components/DiagramEditorPane.tsx
@@ -90,9 +90,11 @@ import {
architectureNodeVisual,
isArchitectureNodeShape,
resolveArchitectureSurface,
+ diagramReaderFocusNode,
FLOWCHART_EDGE_ROUTER,
flowchartEdgeIsStraight,
flowchartEdgePorts,
+ flowchartFitsReadableViewport,
flowchartNodeVisual,
resolveFlowchartSurface,
type ArchitectureResourceIcon,
@@ -1129,21 +1131,39 @@ const removeGraphSelection = (graph: Graph) => {
};
-const readFlowchart = (graph: Graph, document: DiagramDocument, container: HTMLElement | null) => {
- if (!document.nodes.length) return;
- const incoming = new Set(document.edges.map((edge) => edge.target));
- const start = document.nodes.find((node) => !incoming.has(node.id)) ?? document.nodes[0];
- const cell = graph.getCellById(start.id);
- if (!cell?.isNode()) return;
+const diagramViewportSize = (graph: Graph, container: HTMLElement | null) => {
+ const host = getDiagramScroller(graph)?.container ?? container;
+ if (!host) return null;
+ return { width: host.clientWidth, height: host.clientHeight };
+};
+
+const readDiagramContent = (graph: Graph, document: DiagramDocument) => {
+ const policy = getDiagramLayoutViewport(document.kind);
+ const minScale = policy.minScale ?? 1;
+ const focus = diagramReaderFocusNode(document);
+ const cell = focus ? graph.getCellById(focus.id) : null;
ensureDiagramPaperContainsNodes(graph);
- zoomDiagram(graph, 1, true);
+ zoomDiagram(graph, document.kind === "flowchart" ? 1 : minScale, true);
ensureDiagramPaperContainsNodes(graph);
+ if (!cell?.isNode()) {
+ centerDiagramContent(graph);
+ ensureDiagramPaperContainsNodes(graph);
+ return;
+ }
const box = cell.getBBox();
const scroller = getDiagramScroller(graph);
- if (scroller) {
- scroller.positionPoint({ x: box.x + box.width / 2, y: box.y }, "50%", 48);
+ if (document.kind === "flowchart") {
+ if (scroller) scroller.positionPoint({ x: box.x + box.width / 2, y: box.y }, "50%", 48);
+ else graph.centerPoint(box.x + box.width / 2, box.y);
+ } else if (policy.anchor === "leftmost") {
+ const bounds = diagramNodeBounds(graph);
+ const origin = bounds ?? { x: box.x, y: box.y, width: box.width, height: box.height };
+ if (scroller) scroller.positionPoint({ x: origin.x, y: origin.y }, 40, 48);
+ else graph.centerPoint(origin.x + origin.width / 2, origin.y);
+ } else if (scroller) {
+ scroller.centerPoint(box.x + box.width / 2, box.y + box.height / 2);
} else {
- graph.centerPoint(box.x + box.width / 2, box.y);
+ graph.centerPoint(box.x + box.width / 2, box.y + box.height / 2);
}
ensureDiagramPaperContainsNodes(graph);
};
@@ -1154,22 +1174,24 @@ const fitDiagramContent = (
container: HTMLElement | null,
padding = 32,
viewport?: DiagramLayoutViewport,
+ options: { readable?: boolean } = {},
) => {
const policy = viewport ?? getDiagramLayoutViewport(document.kind);
const bounds = diagramNodeBounds(graph);
if (!bounds) return;
ensureDiagramPaperContainsNodes(graph);
+ if (options.readable) {
+ const size = diagramViewportSize(graph, container);
+ const minScale = policy.minScale ?? 1;
+ if (size && !flowchartFitsReadableViewport(bounds, size, padding, minScale, policy.maxScale)) {
+ readDiagramContent(graph, document);
+ return;
+ }
+ }
// Fit every node, including mind-map branches left of the root. Zooming to a
// visible subset or to edge paths lets Scroller shrink the paper and clip.
fitDiagramRect(graph, bounds, { padding, maxScale: policy.maxScale });
ensureDiagramPaperContainsNodes(graph);
- if (
- document.kind === "flowchart"
- && policy.minScale != null
- && graph.scale().sx < policy.minScale
- ) {
- readFlowchart(graph, document, container);
- }
};
const applyMindMapHierarchy = (graph: Graph, theme: DiagramTheme, appearance: DiagramAppearance, structure?: DiagramStructure) => {
@@ -1659,7 +1681,7 @@ export const DiagramEditorPane = ({
if (graphRef.current !== graph) return false;
if (!diagramCanvasIsReady(canvasSurfaceRef.current)) return false;
ensureDiagramPaperContainsNodes(graph);
- fitDiagramContent(graph, document, containerRef.current);
+ fitDiagramContent(graph, document, containerRef.current, 32, undefined, { readable: true });
return true;
};
settleLoadedViewport();
@@ -2422,7 +2444,7 @@ export const DiagramEditorPane = ({
if (document.kind === "mind-map") applyMindMapHierarchy(graph, themeRef.current, appearanceRef.current, structureRef.current);
graph.stopBatch("layout");
ensureDiagramPaperContainsNodes(graph);
- fitDiagramContent(graph, document, containerRef.current, 40, layout.viewport);
+ fitDiagramContent(graph, document, containerRef.current, 40, layout.viewport, { readable: true });
if (changed) {
setDirty(savedSnapshotRef.current !== diagramEditorSnapshot(
titleRef.current,
@@ -2888,7 +2910,7 @@ export const DiagramEditorPane = ({
structure={structure}
onUndo={() => runHistoryAction("undo")}
zoomPercent={zoomPercent}
- onRead={document.kind === "flowchart" ? () => { if (graphRef.current) readFlowchart(graphRef.current, document, containerRef.current); } : undefined}
+ onRead={document.kind === "flowchart" ? () => { if (graphRef.current) readDiagramContent(graphRef.current, document); } : undefined}
onFit={() => { const graph = graphRef.current; if (graph) fitDiagramContent(graph, document, containerRef.current); }}
onResetZoom={() => {
const graph = graphRef.current;
diff --git a/apps/web/src/lib/diagram-layout.test.mjs b/apps/web/src/lib/diagram-layout.test.mjs
index 6fc3fa38..46158667 100644
--- a/apps/web/src/lib/diagram-layout.test.mjs
+++ b/apps/web/src/lib/diagram-layout.test.mjs
@@ -7,6 +7,7 @@ import {
compactMindMapNodeSize,
computeDiagramLayout,
computeDiagramLayoutResult,
+ DIAGRAM_READABLE_MIN_SCALE,
getDiagramLayoutViewport,
} from "./diagram-layout.ts";
@@ -235,7 +236,8 @@ describe("diagram auto layout", () => {
expect(child.x + child.width).toBeLessThan(boundary.x + boundary.width);
expect(child.y + child.height).toBeLessThan(boundary.y + boundary.height);
}
- expect(result.viewport.minScale).toBe(0.64);
+ expect(result.viewport.minScale).toBe(DIAGRAM_READABLE_MIN_SCALE);
+ expect(result.viewport.maxScale).toBe(1);
});
test("keeps ungrouped architecture terminals next to the groups they connect to", () => {
@@ -316,7 +318,7 @@ describe("diagram auto layout", () => {
expect(getDiagramLayoutViewport("flowchart")).toEqual({
anchor: "center",
maxScale: 1,
- minScale: 0.85,
+ minScale: DIAGRAM_READABLE_MIN_SCALE,
});
});
@@ -324,7 +326,15 @@ describe("diagram auto layout", () => {
expect(getDiagramLayoutViewport("mind-map")).toEqual({
anchor: "root",
maxScale: 1,
- minScale: 0.85,
+ minScale: DIAGRAM_READABLE_MIN_SCALE,
+ });
+ });
+
+ test("keeps architecture viewports at reading size instead of shrinking the whole graph", () => {
+ expect(getDiagramLayoutViewport("architecture")).toEqual({
+ anchor: "leftmost",
+ maxScale: 1,
+ minScale: DIAGRAM_READABLE_MIN_SCALE,
});
});
});
diff --git a/apps/web/src/lib/diagram-layout.ts b/apps/web/src/lib/diagram-layout.ts
index 4de1e860..707eacfa 100644
--- a/apps/web/src/lib/diagram-layout.ts
+++ b/apps/web/src/lib/diagram-layout.ts
@@ -6,6 +6,7 @@ export {
compactMindMapNodeSize,
computeDiagramLayout,
computeDiagramLayoutResult,
+ DIAGRAM_READABLE_MIN_SCALE,
getDiagramLayoutViewport,
} from "@edgeever/shared/diagram-layout";
export type {
diff --git a/packages/shared/src/diagram-flowchart-style.test.mjs b/packages/shared/src/diagram-flowchart-style.test.mjs
index 4871cb10..e7d1185a 100644
--- a/packages/shared/src/diagram-flowchart-style.test.mjs
+++ b/packages/shared/src/diagram-flowchart-style.test.mjs
@@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test";
import { flowchartNodePresentation } from "./diagram-node-presentation.ts";
import {
+ DIAGRAM_READABLE_MIN_SCALE,
FLOWCHART_READABLE_MIN_SCALE,
FLOWCHART_SELECTABLE_THEMES,
FLOWCHART_SURFACES,
@@ -120,6 +121,7 @@ describe("flowchart readable viewport", () => {
test("refuses to shrink a tall flow below reading size", () => {
expect(flowchartFitsReadableViewport({ width: 220, height: 1680 }, viewport)).toBe(false);
- expect(FLOWCHART_READABLE_MIN_SCALE).toBe(0.85);
+ expect(FLOWCHART_READABLE_MIN_SCALE).toBe(DIAGRAM_READABLE_MIN_SCALE);
+ expect(DIAGRAM_READABLE_MIN_SCALE).toBe(0.75);
});
});
diff --git a/packages/shared/src/diagram-flowchart-style.ts b/packages/shared/src/diagram-flowchart-style.ts
index 843315ae..42c5198f 100644
--- a/packages/shared/src/diagram-flowchart-style.ts
+++ b/packages/shared/src/diagram-flowchart-style.ts
@@ -41,9 +41,10 @@ export const flowchartEdgeIsStraight = (source: FlowchartBox, target: FlowchartB
export const FLOWCHART_LAYOUT_SPACING = { rank: 56, node: 36 };
-// Tall flows should stay at reading size. Shrinking the whole graph into the
-// viewport makes 12-step notes unreadable; show the start and let the canvas scroll.
-export const FLOWCHART_READABLE_MIN_SCALE = 0.85;
+// Auto layout and opening stay at reading size. Shrinking the whole graph into
+// the viewport makes large notes unreadable; show the start and let the canvas scroll.
+export const DIAGRAM_READABLE_MIN_SCALE = 0.75;
+export const FLOWCHART_READABLE_MIN_SCALE = DIAGRAM_READABLE_MIN_SCALE;
export const flowchartFitsReadableViewport = (
bounds: { width: number; height: number },
diff --git a/packages/shared/src/diagram-layout.ts b/packages/shared/src/diagram-layout.ts
index 65d379f8..efab1732 100644
--- a/packages/shared/src/diagram-layout.ts
+++ b/packages/shared/src/diagram-layout.ts
@@ -1,9 +1,11 @@
import { compactFlowchartNodeSize, flowchartNodePresentation } from "./diagram-node-presentation";
export { compactFlowchartNodeSize, flowchartNodePresentation } from "./diagram-node-presentation";
-import { FLOWCHART_LAYOUT_SPACING, FLOWCHART_READABLE_MIN_SCALE } from "./diagram-flowchart-style";
+import { DIAGRAM_READABLE_MIN_SCALE, FLOWCHART_LAYOUT_SPACING } from "./diagram-flowchart-style";
export {
+ DIAGRAM_READABLE_MIN_SCALE,
FLOWCHART_EDGE_ROUTER,
FLOWCHART_LAYOUT_SPACING,
+ FLOWCHART_READABLE_MIN_SCALE,
flowchartEdgeIsStraight,
flowchartEdgePorts,
flowchartFitsReadableViewport,
@@ -903,18 +905,18 @@ const DIAGRAM_LAYOUT_STRATEGIES: Record<DiagramKind, DiagramLayoutStrategy> = {
"mind-map": {
kind: "mind-map",
layout: computeMindMapLayout,
- viewport: { anchor: "root", maxScale: 1, minScale: FLOWCHART_READABLE_MIN_SCALE },
+ viewport: { anchor: "root", maxScale: 1, minScale: DIAGRAM_READABLE_MIN_SCALE },
},
flowchart: {
kind: "flowchart",
layout: computeFlowchartLayout,
- viewport: { anchor: "center", maxScale: 1, minScale: FLOWCHART_READABLE_MIN_SCALE },
+ viewport: { anchor: "center", maxScale: 1, minScale: DIAGRAM_READABLE_MIN_SCALE },
},
architecture: {
kind: "architecture",
layout: computeArchitectureLayout,
finalize: finalizeArchitectureLayout,
- viewport: { anchor: "leftmost", maxScale: 0.84, minScale: 0.64 },
+ viewport: { anchor: "leftmost", maxScale: 1, minScale: DIAGRAM_READABLE_MIN_SCALE },
},
};
diff --git a/packages/shared/src/diagram-reader.ts b/packages/shared/src/diagram-reader.ts
index ae1888d3..ad4c3686 100644
--- a/packages/shared/src/diagram-reader.ts
+++ b/packages/shared/src/diagram-reader.ts
@@ -1,6 +1,6 @@
import { attachDiagramScroll } from "./diagram-scroll";
import type { DiagramDocument, DiagramKind } from "./diagram";
-import { FLOWCHART_READABLE_MIN_SCALE } from "./diagram-flowchart-style";
+import { DIAGRAM_READABLE_MIN_SCALE } from "./diagram-flowchart-style";
type ReaderBox = { x: number; y: number; width: number; height: number };
type ReaderNode = {
@@ -20,7 +20,7 @@ type ReaderGraph = {
enablePanning?: () => unknown;
};
-export const DIAGRAM_READER_MIN_SCALE = FLOWCHART_READABLE_MIN_SCALE;
+export const DIAGRAM_READER_MIN_SCALE = DIAGRAM_READABLE_MIN_SCALE;
export const DIAGRAM_READER_MAX_SCALE = 2.5;
export const DIAGRAM_READER_FIT_PADDING = 28;