refactor(elements): split edges and nodes
This commit is contained in:
+20
-28
@@ -146,35 +146,31 @@ export const onLoadProject = (currentStore: Store<ReactFlowState>) => {
|
||||
};
|
||||
};
|
||||
|
||||
export const parseElement = (element: Node | Edge, nodeExtent: NodeExtent): Node | Edge => {
|
||||
if (!element.id) {
|
||||
throw new Error('All nodes and edges need to have an id.');
|
||||
}
|
||||
|
||||
if (isEdge(element)) {
|
||||
return {
|
||||
...element,
|
||||
source: element.source.toString(),
|
||||
target: element.target.toString(),
|
||||
sourceHandle: element.sourceHandle ? element.sourceHandle.toString() : null,
|
||||
targetHandle: element.targetHandle ? element.targetHandle.toString() : null,
|
||||
id: element.id.toString(),
|
||||
type: element.type || 'default',
|
||||
};
|
||||
}
|
||||
|
||||
export const parseNode = (node: Node, nodeExtent: NodeExtent): Node => {
|
||||
return {
|
||||
...element,
|
||||
id: element.id.toString(),
|
||||
type: element.type || 'default',
|
||||
...node,
|
||||
id: node.id.toString(),
|
||||
type: node.type || 'default',
|
||||
__rf: {
|
||||
position: clampPosition(element.position, nodeExtent),
|
||||
position: clampPosition(node.position, nodeExtent),
|
||||
width: null,
|
||||
height: null,
|
||||
handleBounds: {},
|
||||
isDragging: false,
|
||||
},
|
||||
} as Node;
|
||||
};
|
||||
};
|
||||
|
||||
export const parseEdge = (edge: Edge): Edge => {
|
||||
return {
|
||||
...edge,
|
||||
source: edge.source.toString(),
|
||||
target: edge.target.toString(),
|
||||
sourceHandle: edge.sourceHandle ? edge.sourceHandle.toString() : null,
|
||||
targetHandle: edge.targetHandle ? edge.targetHandle.toString() : null,
|
||||
id: edge.id.toString(),
|
||||
type: edge.type || 'default',
|
||||
};
|
||||
};
|
||||
|
||||
const getBoundsOfBoxes = (box1: Box, box2: Box): Box => ({
|
||||
@@ -272,9 +268,7 @@ const parseElements = (nodes: Node[], edges: Edge[]): Elements => {
|
||||
|
||||
export const onLoadGetElements = (currentStore: Store<ReactFlowState>) => {
|
||||
return (): Elements => {
|
||||
const { elements = [] } = currentStore.getState();
|
||||
const nodes = elements.filter(isNode);
|
||||
const edges = elements.filter(isEdge);
|
||||
const { nodes = [], edges = [] } = currentStore.getState();
|
||||
|
||||
return parseElements(nodes, edges);
|
||||
};
|
||||
@@ -282,9 +276,7 @@ export const onLoadGetElements = (currentStore: Store<ReactFlowState>) => {
|
||||
|
||||
export const onLoadToObject = (currentStore: Store<ReactFlowState>) => {
|
||||
return (): FlowExportObject => {
|
||||
const { elements = [], transform } = currentStore.getState();
|
||||
const nodes = elements.filter(isNode);
|
||||
const edges = elements.filter(isEdge);
|
||||
const { nodes = [], edges = [], transform } = currentStore.getState();
|
||||
|
||||
return {
|
||||
elements: parseElements(nodes, edges),
|
||||
|
||||
Reference in New Issue
Block a user