-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #696 from tone-row/dev
v1.53.0
- Loading branch information
Showing
26 changed files
with
1,137 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { NodePositions } from "../components/getNodePositionsFromCy"; | ||
import { useDoc } from "./useDoc"; | ||
|
||
/** | ||
* This function tries to align nodes vertical and horiontal on their center | ||
* axis by iterating over all the nodes, finding their centers, looking for other | ||
* nodes that are close to them, and then moving them to the center of the | ||
* closest node. | ||
*/ | ||
export function alignNodes() { | ||
const meta = useDoc.getState().meta; | ||
const nodePositions = meta.nodePositions as NodePositions; | ||
if (!nodePositions) return; | ||
|
||
const threshold = 20; // Adjust this value to change the alignment sensitivity | ||
const alignedPositions: NodePositions = {}; | ||
|
||
// Iterate through all nodes | ||
Object.entries(nodePositions).forEach(([nodeId, position]) => { | ||
let closestHorizontal: cytoscape.Position | null = null; | ||
let closestVertical: cytoscape.Position | null = null; | ||
let minHorizontalDiff = Infinity; | ||
let minVerticalDiff = Infinity; | ||
|
||
// Compare with other nodes | ||
for (const [otherNodeId, otherPosition] of Object.entries(nodePositions)) { | ||
if (nodeId !== otherNodeId) { | ||
const horizontalDiff = Math.abs(position.x - otherPosition.x); | ||
const verticalDiff = Math.abs(position.y - otherPosition.y); | ||
|
||
// Check for horizontal alignment | ||
if (horizontalDiff < threshold && horizontalDiff < minHorizontalDiff) { | ||
closestHorizontal = otherPosition; | ||
minHorizontalDiff = horizontalDiff; | ||
} | ||
|
||
// Check for vertical alignment | ||
if (verticalDiff < threshold && verticalDiff < minVerticalDiff) { | ||
closestVertical = otherPosition; | ||
minVerticalDiff = verticalDiff; | ||
} | ||
} | ||
} | ||
|
||
// Align the node | ||
alignedPositions[nodeId] = { | ||
x: closestHorizontal ? closestHorizontal.x : position.x, | ||
y: closestVertical ? closestVertical.y : position.y, | ||
}; | ||
}); | ||
|
||
// Update the node positions in the document state | ||
useDoc.setState((state) => ({ | ||
meta: { | ||
...state.meta, | ||
nodePositions: alignedPositions, | ||
}, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.