refactor(nodes): use-d3drag

This commit is contained in:
moklick
2022-04-13 11:19:17 +02:00
parent 3bd756d8a6
commit f2004fe45a
11 changed files with 154 additions and 181 deletions
+7 -3
View File
@@ -97,7 +97,7 @@ const createStore = () =>
}
},
updateNodePosition: ({ id, diff, dragging }: NodeDiffUpdate) => {
const { onNodesChange, nodeExtent, nodeInternals, hasDefaultNodes } = get();
const { onNodesChange, nodeExtent, nodeInternals, hasDefaultNodes, snapGrid, snapToGrid } = get();
if (hasDefaultNodes || onNodesChange) {
const changes: NodePositionChange[] = [];
@@ -105,10 +105,14 @@ const createStore = () =>
nodeInternals.forEach((node) => {
if (node.selected) {
if (!node.parentNode || !isParentSelected(node, nodeInternals)) {
changes.push(createPositionChange({ node, diff, dragging, nodeExtent, nodeInternals }));
changes.push(
createPositionChange({ node, diff, dragging, nodeExtent, nodeInternals, snapToGrid, snapGrid })
);
}
} else if (node.id === id) {
changes.push(createPositionChange({ node, diff, dragging, nodeExtent, nodeInternals }));
changes.push(
createPositionChange({ node, diff, dragging, nodeExtent, nodeInternals, snapToGrid, snapGrid })
);
}
});
+7
View File
@@ -1,3 +1,4 @@
// @ts-nocheck
import { zoomIdentity } from 'd3-zoom';
import { GetState } from 'zustand';
@@ -15,6 +16,7 @@ import {
XYPosition,
XYZPosition,
FitViewOptions,
SnapGrid,
} from '../types';
type ParentNodes = Record<string, boolean>;
@@ -111,6 +113,8 @@ type CreatePostionChangeParams = {
nodeInternals: NodeInternals;
diff?: XYPosition;
dragging?: boolean;
snapToGrid?: boolean;
snapGrid?: SnapGrid;
};
export function createPositionChange({
@@ -119,6 +123,8 @@ export function createPositionChange({
dragging,
nodeExtent,
nodeInternals,
snapToGrid,
snapGrid,
}: CreatePostionChangeParams): NodePositionChange {
const change: NodePositionChange = {
id: node.id,
@@ -128,6 +134,7 @@ export function createPositionChange({
if (diff) {
const nextPosition = { x: node.position.x + diff.x, y: node.position.y + diff.y };
let currentExtent = node.extent || nodeExtent;
if (node.extent === 'parent') {