Skip to content

Commit

Permalink
refactor(types): add NodePositionChange type
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Jan 25, 2022
1 parent 2a4dfce commit 067d681
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/store/index.ts
Expand Up @@ -13,6 +13,7 @@ import {
NodeDimensionChange,
EdgeSelectionChange,
NodeSelectionChange,
NodePositionChange,
} from '../types';
import { getHandleBounds } from '../components/Nodes/utils';
import { createSelectionChange, getSelectionChanges } from '../utils/changes';
Expand Down Expand Up @@ -96,7 +97,7 @@ const createStore = () =>
const { onNodesChange, nodeExtent, nodeInternals, hasDefaultNodes } = get();

if (hasDefaultNodes || onNodesChange) {
const changes: NodeDimensionChange[] = [];
const changes: NodePositionChange[] = [];

nodeInternals.forEach((node) => {
if (node.selected) {
Expand Down
10 changes: 5 additions & 5 deletions src/store/utils.ts
Expand Up @@ -8,8 +8,8 @@ import {
Edge,
EdgeSelectionChange,
Node,
NodeDimensionChange,
NodeInternals,
NodePositionChange,
NodeSelectionChange,
ReactFlowState,
XYPosition,
Expand Down Expand Up @@ -103,7 +103,7 @@ export function isParentSelected(node: Node, nodeInternals: NodeInternals): bool
return isParentSelected(parentNode, nodeInternals);
}

type CreatePostiionChangeParams = {
type CreatePostionChangeParams = {
node: Node;
nodeExtent: CoordinateExtent;
nodeInternals: NodeInternals;
Expand All @@ -117,10 +117,10 @@ export function createPositionChange({
dragging,
nodeExtent,
nodeInternals,
}: CreatePostiionChangeParams): NodeDimensionChange {
const change: NodeDimensionChange = {
}: CreatePostionChangeParams): NodePositionChange {
const change: NodePositionChange = {
id: node.id,
type: 'dimensions',
type: 'position',
dragging: !!dragging,
};

Expand Down
11 changes: 8 additions & 3 deletions src/types/changes.ts
Expand Up @@ -4,9 +4,14 @@ import { NodeHandleBounds } from './nodes';
export type NodeDimensionChange = {
id: string;
type: 'dimensions';
dimensions?: Dimensions;
position?: XYPosition;
dimensions: Dimensions;
handleBounds?: NodeHandleBounds;
};

export type NodePositionChange = {
id: string;
type: 'position';
position?: XYPosition;
dragging?: boolean;
};

Expand All @@ -21,7 +26,7 @@ export type NodeRemoveChange = {
type: 'remove';
};

export type NodeChange = NodeDimensionChange | NodeSelectionChange | NodeRemoveChange;
export type NodeChange = NodeDimensionChange | NodePositionChange | NodeSelectionChange | NodeRemoveChange;

export type EdgeSelectionChange = NodeSelectionChange;
export type EdgeRemoveChange = NodeRemoveChange;
Expand Down
18 changes: 12 additions & 6 deletions src/utils/changes.ts
Expand Up @@ -12,14 +12,9 @@ function applyChanges(changes: NodeChange[] | EdgeChange[], elements: any[]): an
res.push({ ...item, selected: currentChange.selected });
return res;
}
case 'dimensions': {
case 'position': {
const updateItem = { ...item };

if (typeof currentChange.dimensions !== 'undefined') {
updateItem.width = currentChange.dimensions.width;
updateItem.height = currentChange.dimensions.height;
}

if (typeof currentChange.position !== 'undefined') {
updateItem.position = currentChange.position;
}
Expand Down Expand Up @@ -69,6 +64,17 @@ function applyChanges(changes: NodeChange[] | EdgeChange[], elements: any[]): an
res.push(updateItem);
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': {
return res;
}
Expand Down

0 comments on commit 067d681

Please sign in to comment.