refactor(react): use size instead of dimensions, cleanup defaul node handling

This commit is contained in:
moklick
2023-10-10 13:24:02 +02:00
parent 93a180b19d
commit ed5606d870
7 changed files with 740 additions and 42 deletions

View File

@@ -26,7 +26,7 @@ function Wrapper({
}
return (
<ReactFlowProvider nodes={nodes} edges={edges} width={width} height={height}>
<ReactFlowProvider initialNodes={nodes} initialEdges={edges} initialWidth={width} initialHeight={height}>
{children}
</ReactFlowProvider>
);

View File

@@ -1,3 +1,5 @@
'use client';
import { forwardRef, type CSSProperties } from 'react';
import cc from 'classcat';
import {

View File

@@ -55,15 +55,27 @@ const createRFStore = ({
const hasDefaultNodes = typeof nodes !== 'undefined';
const hasDefaultEdges = typeof edges !== 'undefined';
const nextNodes = hasDefaultNodes
? updateNodes(nodes, [], {
nodeOrigin: get().nodeOrigin,
elevateNodesOnSelect: get().elevateNodesOnSelect,
})
: [];
const nextEdges = hasDefaultEdges ? edges : [];
const nextState: {
nodes?: Node[];
edges?: Edge[];
hasDefaultNodes: boolean;
hasDefaultEdges: boolean;
} = {
hasDefaultNodes,
hasDefaultEdges,
};
set({ nodes: nextNodes, edges: nextEdges, hasDefaultNodes, hasDefaultEdges });
if (hasDefaultNodes) {
nextState.nodes = updateNodes(nodes, [], {
nodeOrigin: get().nodeOrigin,
elevateNodesOnSelect: get().elevateNodesOnSelect,
});
}
if (hasDefaultEdges) {
nextState.edges = edges;
}
set(nextState);
},
updateNodeDimensions: (updates) => {
const { onNodesChange, fitView, nodes, fitViewOnInit, fitViewDone, fitViewOnInitOptions, domNode, nodeOrigin } =
@@ -289,7 +301,13 @@ const createRFStore = ({
set(currentConnection);
},
reset: () => set({ ...getInitialState({ nodes: [] }) }),
reset: () => {
// @todo: what should we do about this? Do we still need it?
// if you are on a SPA with multiple flows, we want to make sure that the store gets resetted
// when you switch pages. Does this reset solves this? Currently it always gets called. This
// leads to an emtpy nodes array at the beginning.
// set({ ...getInitialState() });
},
}),
Object.is
);

View File

@@ -20,7 +20,7 @@ const getInitialState = ({
edges?: Edge[];
width?: number;
height?: number;
}): ReactFlowStore => {
} = {}): ReactFlowStore => {
const nextNodes = updateNodes(nodes, [], { nodeOrigin: [0, 0], elevateNodesOnSelect: false });
let transform: Transform = [0, 0, 1];
@@ -28,8 +28,8 @@ const getInitialState = ({
if (width && height) {
const nodesWithDimensions = nextNodes.map((node) => ({
...node,
width: node.dimensions?.width,
height: node.dimensions?.height,
width: node.size?.width,
height: node.size?.height,
}));
const bounds = getRectOfNodes(nodesWithDimensions, [0, 0]);
transform = getTransformForBounds(bounds, width, height, 0.5, 2, 0.1);

View File

@@ -42,7 +42,10 @@ export type OnSelectionChangeFunc = (params: OnSelectionChangeParams) => void;
export type FitViewParams = FitViewParamsBase<Node>;
export type FitViewOptions = FitViewOptionsBase<Node>;
export type FitView = (fitViewOptions?: FitViewOptions) => boolean;
export type OnInit<NodeData = any, EdgeData = any> = (reactFlowInstance: ReactFlowInstance<NodeData, EdgeData>) => void;
export type OnInit<NodeData = any, EdgeData = any> = (
reactFlowInstance: ReactFlowInstance<NodeData, EdgeData>,
{ nodes, edges }: { nodes: Node<NodeData>[]; edges: Edge<EdgeData>[] }
) => void;
export type ViewportHelperFunctions = {
zoomIn: ZoomInOut;

730
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -30,6 +30,7 @@ export const esmConfig = defineConfig({
output: {
file: pkg.module,
format: 'esm',
banner: '"use client"',
},
onwarn,
plugins: [