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: reflow mind map siblings on insert
tianma-if2124866a788e
diff --git a/apps/web/src/components/DiagramEditorPane.test.mjs b/apps/web/src/components/DiagramEditorPane.test.mjsindex 42ef3408..3f7156d1 100644--- a/apps/web/src/components/DiagramEditorPane.test.mjs+++ b/apps/web/src/components/DiagramEditorPane.test.mjs@@ -15,6 +15,7 @@ describe("diagram editor keyboard workflow", () => {expect(source).toContain('insertNodeRef.current("sibling"');expect(source).toContain('graph.bindKey("tab"');expect(source).toContain('insertNodeRef.current("child"');+ expect(source).toContain("graph.cleanSelection();\n graph.select(node);");});});diff --git a/apps/web/src/components/DiagramEditorPane.tsx b/apps/web/src/components/DiagramEditorPane.tsxindex 8106587c..7f1e7b31 100644--- a/apps/web/src/components/DiagramEditorPane.tsx+++ b/apps/web/src/components/DiagramEditorPane.tsx@@ -645,7 +645,20 @@ export const DiagramEditorPane = ({ memo, repository, readOnly, onBackToList, onif (isMindMap && parent?.isNode()) {graph.addEdge(edgeMetadata({ id: createId("branch"), source: parent.id, target: id }, document.kind, themeRef.current));}+ if (isMindMap) {+ const positions = computeDiagramLayout(+ graphToDocument(graph, document.kind, themeRef.current),+ requestedSibling && selected?.isNode()+ ? { insertedNodeId: id, insertAfterNodeId: selected.id }+ : {},+ );+ for (const graphNode of graph.getNodes()) {+ const position = positions[graphNode.id];+ if (position) graphNode.position(position.x, position.y);+ }+ }graph.stopBatch("add");+ graph.cleanSelection();graph.select(node);setSelectedNodeId(id);setSelectedNodeLabel(node.getData<NodeData>().label);diff --git a/apps/web/src/lib/diagram-layout.test.mjs b/apps/web/src/lib/diagram-layout.test.mjsindex 8d14ab02..7dd6adad 100644--- a/apps/web/src/lib/diagram-layout.test.mjs+++ b/apps/web/src/lib/diagram-layout.test.mjs@@ -16,6 +16,39 @@ describe("diagram auto layout", () => {expect(compactMindMapNodeSize("A much longer topic label", false).width).toBeLessThanOrEqual(156);});+ test("inserts a sibling after the selected branch and pushes following subtrees down", () => {+ const document = createDefaultDiagramDocument("mind-map");+ document.nodes.push(+ { id: "topic-1-child", parentId: "topic-1", label: "Child 1", x: 400, y: 60, width: 92, height: 36, shape: "topic" },+ { id: "topic-1-child-2", parentId: "topic-1", label: "Child 2", x: 400, y: 112, width: 92, height: 36, shape: "topic" },+ { id: "topic-new", parentId: "topic-root", label: "New", x: 256, y: 89, width: 92, height: 36, shape: "topic" },+ );+ document.edges.push(+ { id: "branch-child", source: "topic-1", target: "topic-1-child" },+ { id: "branch-child-2", source: "topic-1", target: "topic-1-child-2" },+ { id: "branch-new", source: "topic-root", target: "topic-new" },+ );++ const positions = computeDiagramLayout(document, {+ insertedNodeId: "topic-new",+ insertAfterNodeId: "topic-1",+ });+ const orderedSiblings = ["topic-1", "topic-new", "topic-2", "topic-3"];+ for (let index = 1; index < orderedSiblings.length; index += 1) {+ const previous = document.nodes.find((node) => node.id === orderedSiblings[index - 1]);+ expect(positions[orderedSiblings[index]].y).toBeGreaterThanOrEqual(+ positions[orderedSiblings[index - 1]].y + previous.height + 16,+ );+ }+ expect(positions["topic-1-child"].x).toBeGreaterThan(positions["topic-1"].x);+ const selectedSubtreeBottom = Math.max(+ positions["topic-1"].y + 36,+ positions["topic-1-child"].y + 36,+ positions["topic-1-child-2"].y + 36,+ );+ expect(positions["topic-new"].y).toBeGreaterThanOrEqual(selectedSubtreeBottom + 16);+ });+test("orders a connected flow from left to right and keeps detached nodes finite", () => {const document = createDefaultDiagramDocument("flowchart");document.nodes.push({ id: "detached", label: "Detached", x: 0, y: 0, width: 140, height: 52, shape: "process" });diff --git a/apps/web/src/lib/diagram-layout.ts b/apps/web/src/lib/diagram-layout.tsindex a5128275..6ac2fc68 100644--- a/apps/web/src/lib/diagram-layout.ts+++ b/apps/web/src/lib/diagram-layout.ts@@ -3,6 +3,14 @@ import type { DiagramDocument } from "@edgeever/shared";export type DiagramLayoutPositions = Record<string, { x: number; y: number }>;+export type DiagramLayoutOptions = {+ insertedNodeId?: string;+ insertAfterNodeId?: string;+};++const MIND_MAP_HORIZONTAL_GAP = 72;+const MIND_MAP_VERTICAL_GAP = 16;+const visualTextUnits = (label: string) => Array.from(label).reduce((total, character) => total + (/[^\u0000-\u00ff]/.test(character) ? 1 : 0.55),0,@@ -13,12 +21,105 @@ export const compactMindMapNodeSize = (label: string, isRoot: boolean) => ({height: isRoot ? 42 : 36,});-export const computeDiagramLayout = (document: DiagramDocument): DiagramLayoutPositions => {+const computeMindMapLayout = (+ document: DiagramDocument,+ options: DiagramLayoutOptions,+): DiagramLayoutPositions => {+ const nodeById = new Map(document.nodes.map((node) => [node.id, node]));+ const childrenByParent = new Map<string, string[]>();+ for (const node of document.nodes) {+ if (!node.parentId || !nodeById.has(node.parentId)) continue;+ const children = childrenByParent.get(node.parentId) ?? [];+ children.push(node.id);+ childrenByParent.set(node.parentId, children);+ }++ for (const children of childrenByParent.values()) {+ children.sort((leftId, rightId) => {+ const left = nodeById.get(leftId)!;+ const right = nodeById.get(rightId)!;+ return left.y - right.y || left.x - right.x || left.id.localeCompare(right.id);+ });+ }++ if (options.insertedNodeId && options.insertAfterNodeId) {+ const inserted = nodeById.get(options.insertedNodeId);+ const target = nodeById.get(options.insertAfterNodeId);+ if (inserted?.parentId && inserted.parentId === target?.parentId) {+ const siblings = childrenByParent.get(inserted.parentId);+ if (siblings) {+ const withoutInserted = siblings.filter((id) => id !== inserted.id);+ const targetIndex = withoutInserted.indexOf(target.id);+ withoutInserted.splice(targetIndex < 0 ? withoutInserted.length : targetIndex + 1, 0, inserted.id);+ childrenByParent.set(inserted.parentId, withoutInserted);+ }+ }+ }++ const subtreeHeights = new Map<string, number>();+ const measureSubtree = (nodeId: string, ancestors: Set<string>): number => {+ const cached = subtreeHeights.get(nodeId);+ if (cached !== undefined) return cached;+ const node = nodeById.get(nodeId);+ if (!node || ancestors.has(nodeId)) return 0;+ const nextAncestors = new Set(ancestors).add(nodeId);+ const childHeights = (childrenByParent.get(nodeId) ?? [])+ .map((childId) => measureSubtree(childId, nextAncestors))+ .filter((height) => height > 0);+ const childrenHeight = childHeights.reduce((total, height) => total + height, 0)+ + Math.max(0, childHeights.length - 1) * MIND_MAP_VERTICAL_GAP;+ const height = Math.max(node.height, childrenHeight);+ subtreeHeights.set(nodeId, height);+ return height;+ };++ const positions: DiagramLayoutPositions = Object.fromEntries(+ document.nodes.map((node) => [node.id, { x: node.x, y: node.y }]),+ );+ const placeChildren = (nodeId: string, subtreeTop: number, ancestors: Set<string>) => {+ const node = nodeById.get(nodeId);+ if (!node || ancestors.has(nodeId)) return;+ const nextAncestors = new Set(ancestors).add(nodeId);+ const childIds = (childrenByParent.get(nodeId) ?? [])+ .filter((childId) => !nextAncestors.has(childId));+ const childHeights = childIds.map((childId) => measureSubtree(childId, nextAncestors));+ const childrenHeight = childHeights.reduce((total, height) => total + height, 0)+ + Math.max(0, childHeights.length - 1) * MIND_MAP_VERTICAL_GAP;+ let cursor = subtreeTop + (measureSubtree(nodeId, ancestors) - childrenHeight) / 2;+ for (let index = 0; index < childIds.length; index += 1) {+ const childId = childIds[index];+ const child = nodeById.get(childId)!;+ const childSubtreeHeight = childHeights[index];+ positions[childId] = {+ x: positions[nodeId].x + node.width + MIND_MAP_HORIZONTAL_GAP,+ y: Math.round(cursor + (childSubtreeHeight - child.height) / 2),+ };+ placeChildren(childId, cursor, nextAncestors);+ cursor += childSubtreeHeight + MIND_MAP_VERTICAL_GAP;+ }+ };++ const roots = document.nodes+ .filter((node) => !node.parentId || !nodeById.has(node.parentId))+ .sort((left, right) => left.y - right.y || left.id.localeCompare(right.id));+ for (const root of roots) {+ const subtreeHeight = measureSubtree(root.id, new Set());+ const subtreeTop = root.y + root.height / 2 - subtreeHeight / 2;+ placeChildren(root.id, subtreeTop, new Set());+ }+ return positions;+};++export const computeDiagramLayout = (+ document: DiagramDocument,+ options: DiagramLayoutOptions = {},+): DiagramLayoutPositions => {+ if (document.kind === "mind-map") return computeMindMapLayout(document, options);const layoutGraph = new graphlib.Graph();layoutGraph.setGraph({rankdir: "LR",- ranksep: document.kind === "mind-map" ? 72 : 130,- nodesep: document.kind === "mind-map" ? 24 : 56,+ ranksep: 130,+ nodesep: 56,marginx: 32,marginy: 32,});