feat(snap): snap initial nodes to grid

This commit is contained in:
moklick
2019-10-21 21:27:00 +02:00
parent 0fccc63601
commit 99dcc775b4
7 changed files with 89 additions and 36 deletions
+13 -2
View File
@@ -10,6 +10,8 @@ const useElementUpdater = (elements: Elements): void => {
nodes: s.nodes,
edges: s.edges,
transform: s.transform,
snapToGrid: s.snapToGrid,
snapGrid: s.snapGrid,
}));
const setNodes = useStoreActions(a => a.setNodes);
@@ -17,7 +19,11 @@ const useElementUpdater = (elements: Elements): void => {
useEffect(() => {
const nodes = elements.filter(isNode) as Node[];
const edges = elements.filter(isEdge).map(e => parseElement(e)) as Edge[];
const edges = elements
.filter(isEdge)
.map(e =>
parseElement(e, state.transform, state.snapToGrid, state.snapGrid)
) as Edge[];
const nextNodes = nodes.map(propNode => {
const existingNode = state.nodes.find(n => n.id === propNode.id);
@@ -33,7 +39,12 @@ const useElementUpdater = (elements: Elements): void => {
};
}
return parseElement(propNode, state.transform);
return parseElement(
propNode,
state.transform,
state.snapToGrid,
state.snapGrid
);
}) as Node[];
const nodesChanged: boolean = !isEqual(state.nodes, nextNodes);