MCPcopy
hub / github.com/tone-row/flowchart-fun / alignNodes

Function alignNodes

app/src/lib/alignNodes.ts:11–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9 * closest node.
10 */
11export function alignNodes() {
12 const meta = useDoc.getState().meta;
13 const nodePositions = meta.nodePositions as NodePositions;
14 if (!nodePositions) return;
15
16 const threshold = 40; // Adjust this value to change the alignment sensitivity
17 const alignedPositions: NodePositions = {};
18
19 // Store the original positions for undo
20 const originalPositions = { ...nodePositions };
21
22 // Iterate through all nodes
23 Object.entries(nodePositions).forEach(([nodeId, position]) => {
24 let closestHorizontal: cytoscape.Position | null = null;
25 let closestVertical: cytoscape.Position | null = null;
26 let minHorizontalDiff = Infinity;
27 let minVerticalDiff = Infinity;
28
29 // Compare with other nodes
30 for (const [otherNodeId, otherPosition] of Object.entries(nodePositions)) {
31 if (nodeId !== otherNodeId) {
32 const horizontalDiff = Math.abs(position.x - otherPosition.x);
33 const verticalDiff = Math.abs(position.y - otherPosition.y);
34
35 // Check for horizontal alignment
36 if (horizontalDiff < threshold && horizontalDiff < minHorizontalDiff) {
37 closestHorizontal = otherPosition;
38 minHorizontalDiff = horizontalDiff;
39 }
40
41 // Check for vertical alignment
42 if (verticalDiff < threshold && verticalDiff < minVerticalDiff) {
43 closestVertical = otherPosition;
44 minVerticalDiff = verticalDiff;
45 }
46 }
47 }
48
49 // Align the node
50 alignedPositions[nodeId] = {
51 x: closestHorizontal ? closestHorizontal.x : position.x,
52 y: closestVertical ? closestVertical.y : position.y,
53 };
54 });
55
56 // Update the node positions in the document state
57 useDoc.setState((state) => ({
58 meta: {
59 ...state.meta,
60 nodePositions: alignedPositions,
61 },
62 }));
63
64 // Add the action to the undo stack
65 addToUndoStack({
66 undo: () => {
67 useDoc.setState((state) => ({
68 meta: {

Callers

nothing calls this directly

Calls 1

addToUndoStackFunction · 0.90

Tested by

no test coverage detected