refactor(useDragNode): cleanup, refactoring
This commit is contained in:
+10
-13
@@ -9,21 +9,19 @@ import {
|
||||
Node,
|
||||
Edge,
|
||||
NodeDimensionUpdate,
|
||||
NodeDiffUpdate,
|
||||
CoordinateExtent,
|
||||
NodeDimensionChange,
|
||||
EdgeSelectionChange,
|
||||
NodeSelectionChange,
|
||||
NodePositionChange,
|
||||
NodeDragItem,
|
||||
} from '../types';
|
||||
import { getHandleBounds } from '../components/Nodes/utils';
|
||||
import { createSelectionChange, getSelectionChanges } from '../utils/changes';
|
||||
import {
|
||||
createNodeInternals,
|
||||
createPositionChange,
|
||||
handleControlledEdgeSelectionChange,
|
||||
handleControlledNodeSelectionChange,
|
||||
isParentSelected,
|
||||
fitView,
|
||||
} from './utils';
|
||||
import initialState from './initialState';
|
||||
@@ -96,20 +94,19 @@ const createStore = () =>
|
||||
onNodesChange?.(changes);
|
||||
}
|
||||
},
|
||||
updateNodePosition: ({ id, diff }: NodeDiffUpdate) => {
|
||||
const { onNodesChange, nodeExtent, nodeInternals, hasDefaultNodes, snapGrid, snapToGrid } = get();
|
||||
updateNodePositions: (nodeDragItems: NodeDragItem[]) => {
|
||||
const { onNodesChange, nodeInternals, hasDefaultNodes } = get();
|
||||
|
||||
if (hasDefaultNodes || onNodesChange) {
|
||||
const changes: NodePositionChange[] = [];
|
||||
|
||||
nodeInternals.forEach((node) => {
|
||||
if (node.selected) {
|
||||
if (!node.parentNode || !isParentSelected(node, nodeInternals)) {
|
||||
changes.push(createPositionChange({ node, diff, nodeExtent, nodeInternals, snapToGrid, snapGrid }));
|
||||
}
|
||||
} else if (node.id === id) {
|
||||
changes.push(createPositionChange({ node, diff, nodeExtent, nodeInternals, snapToGrid, snapGrid }));
|
||||
}
|
||||
nodeDragItems.forEach((node) => {
|
||||
const change: NodePositionChange = {
|
||||
id: node.id,
|
||||
type: 'position',
|
||||
position: node.position,
|
||||
};
|
||||
changes.push(change);
|
||||
});
|
||||
|
||||
if (changes?.length) {
|
||||
|
||||
@@ -15,7 +15,6 @@ const initialState: ReactFlowStore = {
|
||||
onEdgesChange: null,
|
||||
hasDefaultNodes: false,
|
||||
hasDefaultEdges: false,
|
||||
selectedNodesBbox: { x: 0, y: 0, width: 0, height: 0 },
|
||||
d3Zoom: null,
|
||||
d3Selection: null,
|
||||
d3ZoomHandler: undefined,
|
||||
|
||||
@@ -92,24 +92,6 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
|
||||
return nextNodeInternals;
|
||||
}
|
||||
|
||||
export function isParentSelected(node: Node, nodeInternals: NodeInternals): boolean {
|
||||
if (!node.parentNode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const parentNode = nodeInternals.get(node.parentNode);
|
||||
|
||||
if (!parentNode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parentNode.selected) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return isParentSelected(parentNode, nodeInternals);
|
||||
}
|
||||
|
||||
type CreatePostionChangeParams = {
|
||||
node: Node;
|
||||
nodeExtent: CoordinateExtent;
|
||||
@@ -119,49 +101,6 @@ type CreatePostionChangeParams = {
|
||||
snapGrid?: SnapGrid;
|
||||
};
|
||||
|
||||
export function createPositionChange({
|
||||
node,
|
||||
diff,
|
||||
nodeExtent,
|
||||
nodeInternals,
|
||||
snapToGrid,
|
||||
snapGrid,
|
||||
}: CreatePostionChangeParams): NodePositionChange {
|
||||
const change: NodePositionChange = {
|
||||
id: node.id,
|
||||
type: 'position',
|
||||
};
|
||||
|
||||
if (diff) {
|
||||
const nextPosition = { x: diff.x, y: diff.y };
|
||||
|
||||
let currentExtent = node.extent || nodeExtent;
|
||||
|
||||
if (node.extent === 'parent') {
|
||||
if (node.parentNode && node.width && node.height) {
|
||||
const parent = nodeInternals.get(node.parentNode);
|
||||
currentExtent =
|
||||
parent?.width && parent?.height
|
||||
? [
|
||||
[0, 0],
|
||||
[parent.width - node.width, parent.height - node.height],
|
||||
]
|
||||
: currentExtent;
|
||||
} else {
|
||||
// @ts-ignore
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.warn('[React Flow]: Only child nodes can use a parent extent. Help: https://reactflow.dev/error#500');
|
||||
}
|
||||
currentExtent = nodeExtent;
|
||||
}
|
||||
}
|
||||
|
||||
change.position = currentExtent ? clampPosition(nextPosition, currentExtent as CoordinateExtent) : nextPosition;
|
||||
}
|
||||
|
||||
return change;
|
||||
}
|
||||
|
||||
type InternalFitViewOptions = {
|
||||
initial?: boolean;
|
||||
} & FitViewOptions;
|
||||
|
||||
Reference in New Issue
Block a user