refactor(types): drop NodeInternalsItem type

This commit is contained in:
moklick
2022-01-19 22:17:07 +01:00
parent 08b2696e6b
commit e52c5eda19
7 changed files with 35 additions and 46 deletions
+4 -5
View File
@@ -5,7 +5,6 @@ import { useStore } from '../../store';
import { getBezierPath } from '../Edges/BezierEdge'; import { getBezierPath } from '../Edges/BezierEdge';
import { getSmoothStepPath } from '../Edges/SmoothStepEdge'; import { getSmoothStepPath } from '../Edges/SmoothStepEdge';
import { import {
NodeInternalsItem,
HandleElement, HandleElement,
ConnectionLineType, ConnectionLineType,
ConnectionLineComponent, ConnectionLineComponent,
@@ -29,7 +28,7 @@ interface ConnectionLineProps {
const selector = (s: ReactFlowState) => ({ nodeInternals: s.nodeInternals, transform: s.transform }); const selector = (s: ReactFlowState) => ({ nodeInternals: s.nodeInternals, transform: s.transform });
const getSourceHandle = (handleId: string | null, sourceNode: NodeInternalsItem, connectionHandleType: HandleType) => { const getSourceHandle = (handleId: string | null, sourceNode: Node, connectionHandleType: HandleType) => {
const handleTypeInverted = connectionHandleType === 'source' ? 'target' : 'source'; const handleTypeInverted = connectionHandleType === 'source' ? 'target' : 'source';
const handleBound = sourceNode.handleBounds?.[connectionHandleType] || sourceNode.handleBounds?.[handleTypeInverted]; const handleBound = sourceNode.handleBounds?.[connectionHandleType] || sourceNode.handleBounds?.[handleTypeInverted];
@@ -51,7 +50,7 @@ export default ({
const handleId = connectionHandleId; const handleId = connectionHandleId;
const { nodeInternals, transform } = useStore(selector, shallow); const { nodeInternals, transform } = useStore(selector, shallow);
const sourceNode = useRef<NodeInternalsItem | undefined>(nodeInternals.get(nodeId)); const sourceNode = useRef<Node | undefined>(nodeInternals.get(nodeId));
if ( if (
!sourceNode.current || !sourceNode.current ||
@@ -65,8 +64,8 @@ export default ({
const sourceHandle = getSourceHandle(handleId, sourceNode.current, connectionHandleType); const sourceHandle = getSourceHandle(handleId, sourceNode.current, connectionHandleType);
const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : (sourceNode.current?.width ?? 0) / 2; const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : (sourceNode.current?.width ?? 0) / 2;
const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.current?.height ?? 0; const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.current?.height ?? 0;
const sourceX = sourceNode.current.positionAbsolute.x + sourceHandleX; const sourceX = (sourceNode.current.positionAbsolute?.x || 0) + sourceHandleX;
const sourceY = sourceNode.current.positionAbsolute.y + sourceHandleY; const sourceY = (sourceNode.current.positionAbsolute?.y || 0) + sourceHandleY;
const targetX = (connectionPositionX - transform[0]) / transform[2]; const targetX = (connectionPositionX - transform[0]) / transform[2];
const targetY = (connectionPositionY - transform[1]) / transform[2]; const targetY = (connectionPositionY - transform[1]) / transform[2];
+4 -4
View File
@@ -172,13 +172,13 @@ export function getNodeData(nodeInternals: NodeInternals, nodeId: string): [Rect
!node.handleBounds || !node.handleBounds ||
!node.width || !node.width ||
!node.height || !node.height ||
typeof node.positionAbsolute.x === 'undefined' || typeof node.positionAbsolute?.x === 'undefined' ||
typeof node.positionAbsolute.y === 'undefined'; typeof node.positionAbsolute?.y === 'undefined';
return [ return [
{ {
x: node?.positionAbsolute.x || 0, x: node?.positionAbsolute?.x || 0,
y: node?.positionAbsolute.y || 0, y: node?.positionAbsolute?.y || 0,
width: node?.width || 0, width: node?.width || 0,
height: node?.height || 0, height: node?.height || 0,
}, },
+3 -3
View File
@@ -92,8 +92,8 @@ const NodeRenderer = (props: NodeRendererProps) => {
sourcePosition={node.sourcePosition} sourcePosition={node.sourcePosition}
targetPosition={node.targetPosition} targetPosition={node.targetPosition}
hidden={node.hidden} hidden={node.hidden}
xPos={node.positionAbsolute.x} xPos={node.positionAbsolute?.x ?? 0}
yPos={node.positionAbsolute.y} yPos={node.positionAbsolute?.y ?? 0}
dragging={!!node.dragging} dragging={!!node.dragging}
isInitialized={!!isInitialized} isInitialized={!!isInitialized}
snapGrid={snapGrid} snapGrid={snapGrid}
@@ -115,7 +115,7 @@ const NodeRenderer = (props: NodeRendererProps) => {
isConnectable={isConnectable} isConnectable={isConnectable}
resizeObserver={resizeObserver} resizeObserver={resizeObserver}
dragHandle={node.dragHandle} dragHandle={node.dragHandle}
zIndex={node.z} zIndex={node.z ?? 0}
isParent={!!node.isParent} isParent={!!node.isParent}
noDragClassName={props.noDragClassName} noDragClassName={props.noDragClassName}
noPanClassName={props.noPanClassName} noPanClassName={props.noPanClassName}
+1
View File
@@ -1,4 +1,5 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import useViewportHelper from './useViewportHelper'; import useViewportHelper from './useViewportHelper';
import { useStoreApi } from '../store'; import { useStoreApi } from '../store';
import { ReactFlowInstance, Instance } from '../types'; import { ReactFlowInstance, Instance } from '../types';
+10 -10
View File
@@ -1,5 +1,8 @@
import { zoomIdentity } from 'd3-zoom'; import { zoomIdentity } from 'd3-zoom';
import { GetState } from 'zustand'; import { GetState } from 'zustand';
import { clampPosition, isNumeric } from '../utils';
import { getRectOfNodes, getTransformForBounds } from '../utils/graph';
import { import {
CoordinateExtent, CoordinateExtent,
Edge, Edge,
@@ -7,19 +10,16 @@ import {
Node, Node,
NodeDimensionChange, NodeDimensionChange,
NodeInternals, NodeInternals,
NodeInternalsItem,
NodeSelectionChange, NodeSelectionChange,
ReactFlowState, ReactFlowState,
XYPosition, XYPosition,
XYZPosition, XYZPosition,
} from '../types'; } from '../types';
import { clampPosition, isNumeric } from '../utils';
import { getRectOfNodes, getTransformForBounds } from '../utils/graph';
type ParentNodes = Record<string, boolean>; type ParentNodes = Record<string, boolean>;
function calculateXYZPosition( function calculateXYZPosition(
node: NodeInternalsItem, node: Node,
nodeInternals: NodeInternals, nodeInternals: NodeInternals,
parentNodes: ParentNodes, parentNodes: ParentNodes,
result: XYZPosition result: XYZPosition
@@ -32,17 +32,17 @@ function calculateXYZPosition(
return calculateXYZPosition(parentNode, nodeInternals, parentNodes, { return calculateXYZPosition(parentNode, nodeInternals, parentNodes, {
x: (result.x ?? 0) + (parentNode.position?.x ?? 0), x: (result.x ?? 0) + (parentNode.position?.x ?? 0),
y: (result.y ?? 0) + (parentNode.position?.y ?? 0), y: (result.y ?? 0) + (parentNode.position?.y ?? 0),
z: parentNode.z > node.z ? parentNode.z : node.z, z: (parentNode.z ?? 0) > (node.z ?? 0) ? parentNode.z ?? 0 : node.z ?? 0,
}); });
} }
export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals): NodeInternals { export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals): NodeInternals {
const nextNodeInternals = new Map<string, NodeInternalsItem>(); const nextNodeInternals = new Map<string, Node>();
const parentNodes: ParentNodes = {}; const parentNodes: ParentNodes = {};
nodes.forEach((node) => { nodes.forEach((node) => {
const z = isNumeric(node.zIndex) ? node.zIndex : node.dragging || node.selected ? 1000 : 0; const z = isNumeric(node.zIndex) ? node.zIndex : node.dragging || node.selected ? 1000 : 0;
const internals: NodeInternalsItem = { const internals: Node = {
...nodeInternals.get(node.id), ...nodeInternals.get(node.id),
...node, ...node,
positionAbsolute: { positionAbsolute: {
@@ -66,7 +66,7 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
if (node.parentNode || parentNodes[node.id]) { if (node.parentNode || parentNodes[node.id]) {
const { x, y, z } = calculateXYZPosition(node, nextNodeInternals, parentNodes, { const { x, y, z } = calculateXYZPosition(node, nextNodeInternals, parentNodes, {
...node.position, ...node.position,
z: node.z, z: node.z ?? 0,
}); });
node.positionAbsolute = { node.positionAbsolute = {
@@ -85,7 +85,7 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
return nextNodeInternals; return nextNodeInternals;
} }
export function isParentSelected(node: NodeInternalsItem, nodeInternals: NodeInternals): boolean { export function isParentSelected(node: Node, nodeInternals: NodeInternals): boolean {
if (!node.parentNode) { if (!node.parentNode) {
return false; return false;
} }
@@ -104,7 +104,7 @@ export function isParentSelected(node: NodeInternalsItem, nodeInternals: NodeInt
} }
type CreatePostiionChangeParams = { type CreatePostiionChangeParams = {
node: NodeInternalsItem; node: Node;
nodeExtent: CoordinateExtent; nodeExtent: CoordinateExtent;
nodeInternals: NodeInternals; nodeInternals: NodeInternals;
diff?: XYPosition; diff?: XYPosition;
+7 -8
View File
@@ -27,6 +27,12 @@ export interface Node<T = any> {
zIndex?: number; zIndex?: number;
extent?: 'parent' | CoordinateExtent; extent?: 'parent' | CoordinateExtent;
expandParent?: boolean; expandParent?: boolean;
// only used internally
positionAbsolute?: XYPosition;
z?: number;
handleBounds?: NodeHandleBounds;
isParent?: boolean;
} }
// props that get passed to a custom node // props that get passed to a custom node
@@ -104,14 +110,7 @@ export type NodeDimensionUpdate = {
forceUpdate?: boolean; forceUpdate?: boolean;
}; };
export type NodeInternalsItem = Node & { export type NodeInternals = Map<string, Node>;
positionAbsolute: XYPosition;
z: number;
handleBounds?: NodeHandleBounds;
isParent?: boolean;
};
export type NodeInternals = Map<string, NodeInternalsItem>;
export type NodeBounds = XYPosition & { export type NodeBounds = XYPosition & {
width: number | null; width: number | null;
+6 -16
View File
@@ -1,16 +1,6 @@
import { boxToRect, clamp, getBoundsOfBoxes, rectToBox } from '../utils'; import { boxToRect, clamp, getBoundsOfBoxes, rectToBox } from '../utils';
import { import { Node, Edge, Connection, EdgeMarkerType, Transform, XYPosition, Rect, NodeInternals } from '../types';
Node,
Edge,
Connection,
EdgeMarkerType,
Transform,
XYPosition,
Rect,
NodeInternals,
NodeInternalsItem,
} from '../types';
export const isEdge = (element: Node | Connection | Edge): element is Edge => export const isEdge = (element: Node | Connection | Edge): element is Edge =>
'id' in element && 'source' in element && 'target' in element; 'id' in element && 'source' in element && 'target' in element;
@@ -145,10 +135,10 @@ export const getRectOfNodes = (nodes: Node[]): Rect => {
return boxToRect(box); return boxToRect(box);
}; };
export const getRectOfNodeInternals = (nodes: NodeInternalsItem[]): Rect => { export const getRectOfNodeInternals = (nodes: Node[]): Rect => {
const box = nodes.reduce( const box = nodes.reduce(
(currBox, { positionAbsolute, width, height }) => (currBox, { positionAbsolute, width, height }) =>
getBoundsOfBoxes(currBox, rectToBox({ ...positionAbsolute, width: width || 0, height: height || 0 })), getBoundsOfBoxes(currBox, rectToBox({ ...positionAbsolute!, width: width || 0, height: height || 0 })),
{ x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity } { x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity }
); );
@@ -162,7 +152,7 @@ export const getNodesInside = (
partially: boolean = false, partially: boolean = false,
// set excludeNonSelectableNodes if you want to pay attention to the nodes "selectable" attribute // set excludeNonSelectableNodes if you want to pay attention to the nodes "selectable" attribute
excludeNonSelectableNodes: boolean = false excludeNonSelectableNodes: boolean = false
): NodeInternalsItem[] => { ): Node[] => {
const rBox = rectToBox({ const rBox = rectToBox({
x: (rect.x - tx) / tScale, x: (rect.x - tx) / tScale,
y: (rect.y - ty) / tScale, y: (rect.y - ty) / tScale,
@@ -170,7 +160,7 @@ export const getNodesInside = (
height: rect.height / tScale, height: rect.height / tScale,
}); });
const visibleNodes: NodeInternalsItem[] = []; const visibleNodes: Node[] = [];
nodeInternals.forEach((node) => { nodeInternals.forEach((node) => {
const { positionAbsolute, width, height, dragging, selectable = true } = node; const { positionAbsolute, width, height, dragging, selectable = true } = node;
@@ -179,7 +169,7 @@ export const getNodesInside = (
return false; return false;
} }
const nBox = rectToBox({ ...positionAbsolute, width: width || 0, height: height || 0 }); const nBox = rectToBox({ ...positionAbsolute!, width: width || 0, height: height || 0 });
const xOverlap = Math.max(0, Math.min(rBox.x2, nBox.x2) - Math.max(rBox.x, nBox.x)); const xOverlap = Math.max(0, Math.min(rBox.x2, nBox.x2) - Math.max(rBox.x, nBox.x));
const yOverlap = Math.max(0, Math.min(rBox.y2, nBox.y2) - Math.max(rBox.y, nBox.y)); const yOverlap = Math.max(0, Math.min(rBox.y2, nBox.y2) - Math.max(rBox.y, nBox.y));
const overlappingArea = Math.ceil(xOverlap * yOverlap); const overlappingArea = Math.ceil(xOverlap * yOverlap);