/** * Commands and their inverses, design ยง4.5. * * An inverse-command journal, not snapshots. At 200 nodes a snapshot is ~70 KB * and a hundred of them is 7 MB of retained JSON for nothing. * * Two shapes carry the interesting properties. `moveNodes` carries DELTAS, so * coalescing two consecutive moves on the same ids is exact addition. * `setPositions` carries ABSOLUTES and is what auto-layout emits; its inverse * holds the prior absolutes, so one undo restores the whole pre-layout * arrangement โ€” the property Clay reaches for the first time a tidy scrambles a * graph he liked. */ import type { EdgeId, EdgeKindDefs, GraphDocument, GraphEdgeOf, GraphNodeOf, NodeId, NodeKindDefs, XY, } from "./document"; export type GraphCommand = | { readonly t: "addNodes"; readonly nodes: readonly GraphNodeOf[]; readonly at?: number } | { readonly t: "removeNodes"; readonly ids: readonly NodeId[] } | { readonly t: "moveNodes"; readonly deltas: Readonly> } | { readonly t: "setPositions"; readonly positions: Readonly> } | { readonly t: "setNodeData"; readonly id: NodeId; readonly data: unknown } | { readonly t: "addEdges"; readonly edges: readonly GraphEdgeOf[]; readonly at?: number } | { readonly t: "removeEdges"; readonly ids: readonly EdgeId[] } | { readonly t: "setEdgeData"; readonly id: EdgeId; readonly data: unknown } | { readonly t: "batch"; readonly commands: readonly GraphCommand[] }; const noop = (): GraphCommand => ({ t: "batch", commands: [], }); function insert(list: readonly T[], items: readonly T[], at: number | undefined): T[] { if (at === undefined || at >= list.length) return [...list, ...items]; const copy = [...list]; copy.splice(Math.max(0, at), 0, ...items); return copy; } export function applyCommand( doc: GraphDocument, command: GraphCommand, ): GraphDocument { switch (command.t) { case "addNodes": return { ...doc, nodes: insert(doc.nodes, command.nodes, command.at) }; case "removeNodes": { const gone = new Set(command.ids); return { ...doc, nodes: doc.nodes.filter((node) => !gone.has(node.id)), // An edge whose end has been deleted is not a partial graph, it is a // broken one, so a node takes its edges with it. `invertCommand` // restores both, in their original places. edges: doc.edges.filter( (edge) => !gone.has(edge.source.node) && !gone.has(edge.target.node), ), }; } case "moveNodes": { const { deltas } = command; return { ...doc, nodes: doc.nodes.map((node) => { const delta = deltas[node.id]; return delta ? { ...node, position: { x: node.position.x + delta.x, y: node.position.y + delta.y } } : node; }), }; } case "setPositions": { const { positions } = command; return { ...doc, nodes: doc.nodes.map((node) => { const position = positions[node.id]; return position ? { ...node, position } : node; }), }; } case "setNodeData": return { ...doc, nodes: doc.nodes.map((node) => node.id === command.id ? ({ ...node, data: command.data } as GraphNodeOf) : node, ), }; case "addEdges": return { ...doc, edges: insert(doc.edges, command.edges, command.at) }; case "removeEdges": { const gone = new Set(command.ids); return { ...doc, edges: doc.edges.filter((edge) => !gone.has(edge.id)) }; } case "setEdgeData": return { ...doc, edges: doc.edges.map((edge) => edge.id === command.id ? ({ ...edge, data: command.data } as GraphEdgeOf) : edge, ), }; case "batch": return command.commands.reduce>(applyCommand, doc); } } /** * `0 - v`, not `-v`. Negating a zero component gives -0, and `Object.is(-0, 0)` * is false โ€” so an inverse of a purely horizontal drag would not compare equal * to the same command written by hand, and two structurally identical journal * entries would diff. `0 - 0` is 0 and every other value negates as usual. */ function negate(deltas: Readonly>): Record { const out: Record = {}; for (const [id, delta] of Object.entries(deltas)) out[id] = { x: 0 - delta.x, y: 0 - delta.y }; return out; } export function invertCommand( doc: GraphDocument, command: GraphCommand, ): GraphCommand { switch (command.t) { case "addNodes": return { t: "removeNodes", ids: command.nodes.map((node) => node.id) }; case "removeNodes": { const gone = new Set(command.ids); // One insert per item, at its ORIGINAL index, ascending. Applied in that // order each insert shifts the ones after it into place, so the arrays // come back identical โ€” not merely with the same members. React Flow // paints later nodes on top, so order is a visible property. const restores: GraphCommand[] = []; doc.nodes.forEach((node, at) => { if (gone.has(node.id)) restores.push({ t: "addNodes", nodes: [node], at }); }); doc.edges.forEach((edge, at) => { if (gone.has(edge.source.node) || gone.has(edge.target.node)) { restores.push({ t: "addEdges", edges: [edge], at }); } }); return { t: "batch", commands: restores }; } case "moveNodes": return { t: "moveNodes", deltas: negate(command.deltas) }; case "setPositions": { const before: Record = {}; for (const node of doc.nodes) { if (node.id in command.positions) before[node.id] = node.position; } return { t: "setPositions", positions: before }; } case "setNodeData": { const node = doc.nodes.find((candidate) => candidate.id === command.id); // No prior data to restore, so no-op rather than inventing one. return node ? { t: "setNodeData", id: command.id, data: node.data } : noop(); } case "addEdges": return { t: "removeEdges", ids: command.edges.map((edge) => edge.id) }; case "removeEdges": { const gone = new Set(command.ids); const restores: GraphCommand[] = []; doc.edges.forEach((edge, at) => { if (gone.has(edge.id)) restores.push({ t: "addEdges", edges: [edge], at }); }); return { t: "batch", commands: restores }; } case "setEdgeData": { const edge = doc.edges.find((candidate) => candidate.id === command.id); return edge ? { t: "setEdgeData", id: command.id, data: edge.data } : noop(); } case "batch": { // Each step is inverted against the document it will actually meet, and // the list runs backwards. Inverting them all against `doc` would be // wrong the moment two steps touch the same node. const inverses: GraphCommand[] = []; let current = doc; for (const step of command.commands) { inverses.push(invertCommand(current, step)); current = applyCommand(current, step); } return { t: "batch", commands: inverses.reverse() }; } } }