refactor(types): add NodePositionChange type

This commit is contained in:
moklick
2022-01-25 13:54:33 +01:00
parent 2a4dfce61c
commit 067d681485
4 changed files with 27 additions and 15 deletions
+2 -1
View File
@@ -13,6 +13,7 @@ import {
NodeDimensionChange, NodeDimensionChange,
EdgeSelectionChange, EdgeSelectionChange,
NodeSelectionChange, NodeSelectionChange,
NodePositionChange,
} from '../types'; } from '../types';
import { getHandleBounds } from '../components/Nodes/utils'; import { getHandleBounds } from '../components/Nodes/utils';
import { createSelectionChange, getSelectionChanges } from '../utils/changes'; import { createSelectionChange, getSelectionChanges } from '../utils/changes';
@@ -96,7 +97,7 @@ const createStore = () =>
const { onNodesChange, nodeExtent, nodeInternals, hasDefaultNodes } = get(); const { onNodesChange, nodeExtent, nodeInternals, hasDefaultNodes } = get();
if (hasDefaultNodes || onNodesChange) { if (hasDefaultNodes || onNodesChange) {
const changes: NodeDimensionChange[] = []; const changes: NodePositionChange[] = [];
nodeInternals.forEach((node) => { nodeInternals.forEach((node) => {
if (node.selected) { if (node.selected) {
+5 -5
View File
@@ -8,8 +8,8 @@ import {
Edge, Edge,
EdgeSelectionChange, EdgeSelectionChange,
Node, Node,
NodeDimensionChange,
NodeInternals, NodeInternals,
NodePositionChange,
NodeSelectionChange, NodeSelectionChange,
ReactFlowState, ReactFlowState,
XYPosition, XYPosition,
@@ -103,7 +103,7 @@ export function isParentSelected(node: Node, nodeInternals: NodeInternals): bool
return isParentSelected(parentNode, nodeInternals); return isParentSelected(parentNode, nodeInternals);
} }
type CreatePostiionChangeParams = { type CreatePostionChangeParams = {
node: Node; node: Node;
nodeExtent: CoordinateExtent; nodeExtent: CoordinateExtent;
nodeInternals: NodeInternals; nodeInternals: NodeInternals;
@@ -117,10 +117,10 @@ export function createPositionChange({
dragging, dragging,
nodeExtent, nodeExtent,
nodeInternals, nodeInternals,
}: CreatePostiionChangeParams): NodeDimensionChange { }: CreatePostionChangeParams): NodePositionChange {
const change: NodeDimensionChange = { const change: NodePositionChange = {
id: node.id, id: node.id,
type: 'dimensions', type: 'position',
dragging: !!dragging, dragging: !!dragging,
}; };
+8 -3
View File
@@ -4,9 +4,14 @@ import { NodeHandleBounds } from './nodes';
export type NodeDimensionChange = { export type NodeDimensionChange = {
id: string; id: string;
type: 'dimensions'; type: 'dimensions';
dimensions?: Dimensions; dimensions: Dimensions;
position?: XYPosition;
handleBounds?: NodeHandleBounds; handleBounds?: NodeHandleBounds;
};
export type NodePositionChange = {
id: string;
type: 'position';
position?: XYPosition;
dragging?: boolean; dragging?: boolean;
}; };
@@ -21,7 +26,7 @@ export type NodeRemoveChange = {
type: 'remove'; type: 'remove';
}; };
export type NodeChange = NodeDimensionChange | NodeSelectionChange | NodeRemoveChange; export type NodeChange = NodeDimensionChange | NodePositionChange | NodeSelectionChange | NodeRemoveChange;
export type EdgeSelectionChange = NodeSelectionChange; export type EdgeSelectionChange = NodeSelectionChange;
export type EdgeRemoveChange = NodeRemoveChange; export type EdgeRemoveChange = NodeRemoveChange;
+12 -6
View File
@@ -12,14 +12,9 @@ function applyChanges(changes: NodeChange[] | EdgeChange[], elements: any[]): an
res.push({ ...item, selected: currentChange.selected }); res.push({ ...item, selected: currentChange.selected });
return res; return res;
} }
case 'dimensions': { case 'position': {
const updateItem = { ...item }; const updateItem = { ...item };
if (typeof currentChange.dimensions !== 'undefined') {
updateItem.width = currentChange.dimensions.width;
updateItem.height = currentChange.dimensions.height;
}
if (typeof currentChange.position !== 'undefined') { if (typeof currentChange.position !== 'undefined') {
updateItem.position = currentChange.position; updateItem.position = currentChange.position;
} }
@@ -69,6 +64,17 @@ function applyChanges(changes: NodeChange[] | EdgeChange[], elements: any[]): an
res.push(updateItem); res.push(updateItem);
return res; return res;
} }
case 'dimensions': {
const updateItem = { ...item };
if (typeof currentChange.dimensions !== 'undefined') {
updateItem.width = currentChange.dimensions.width;
updateItem.height = currentChange.dimensions.height;
}
res.push(updateItem);
return res;
}
case 'remove': { case 'remove': {
return res; return res;
} }