@@ -40,16 +40,18 @@ const BasicGraph = () => (
|
|||||||
- `onMove`: move handler
|
- `onMove`: move handler
|
||||||
- `nodeTypes`: object with node types
|
- `nodeTypes`: object with node types
|
||||||
- `edgeTypes`: object with node types
|
- `edgeTypes`: object with node types
|
||||||
|
- `style`: css style passed to the wrapper
|
||||||
- `connectionLineType`: connection line type = `straight` or `bezier`
|
- `connectionLineType`: connection line type = `straight` or `bezier`
|
||||||
- `connectionLineStyle`: connection style as svg attributes
|
- `connectionLineStyle`: connection style as svg attributes
|
||||||
- `deleteKeyCode`: delete key code
|
- `deleteKeyCode`: default: `16`
|
||||||
- `selectionKeyCode`: selection key code
|
- `selectionKeyCode`: default: `false`
|
||||||
- `showBackground`: boolean
|
- `showBackground`: default: `true`
|
||||||
- `backgroundGap`: gap size
|
- `backgroundGap`: gap size - default: `16`
|
||||||
- `backgroundColor`: color of dots or lines
|
- `backgroundColor`: color of dots or lines - default: `#eee`
|
||||||
- `backgroundType`: background type = `dots` or `lines`
|
- `backgroundType`: background type = `dots` or `lines` - default: `dots`
|
||||||
- `snapToGrid`: boolean
|
- `snapToGrid`: default: `false`
|
||||||
- `snapGrid`: [x, y] array
|
- `snapGrid`: [x, y] array - default: `[16, 16]`
|
||||||
|
- `onlyRenderVisibleNodes`: default: `true`
|
||||||
|
|
||||||
# Plugins
|
# Plugins
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
describe('Basic Flow Rendering', () => {
|
describe('Basic Graph Rendering', () => {
|
||||||
it('renders a flow with three nodes', () => {
|
it('renders a flow with three nodes', () => {
|
||||||
cy.visit('/basic');
|
cy.visit('/basic');
|
||||||
|
|
||||||
|
|||||||
+5
-5
@@ -1,11 +1,11 @@
|
|||||||
describe('Advanced Flow Rendering', () => {
|
describe('Custom Nodes Graph Rendering', () => {
|
||||||
it('renders a flow with sone nodes', () => {
|
it('renders a flow with sone nodes', () => {
|
||||||
cy.visit('/');
|
cy.visit('/');
|
||||||
|
|
||||||
cy.get('.react-flow__renderer');
|
cy.get('.react-flow__renderer');
|
||||||
|
|
||||||
cy.get('.react-flow__node').should('have.length', 9);
|
cy.get('.react-flow__node').should('have.length', 8);
|
||||||
cy.get('.react-flow__edge').should('have.length', 9);
|
cy.get('.react-flow__edge').should('have.length', 8);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders a grid', () => {
|
it('renders a grid', () => {
|
||||||
@@ -31,7 +31,7 @@ describe('Advanced Flow Rendering', () => {
|
|||||||
.should('not.have.class', 'valid')
|
.should('not.have.class', 'valid')
|
||||||
.should('not.have.class', 'connecting');
|
.should('not.have.class', 'connecting');
|
||||||
|
|
||||||
cy.get('.react-flow__edge').should('have.length', 10);
|
cy.get('.react-flow__edge').should('have.length', 9);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('tries to make invalid connection', () => {
|
it('tries to make invalid connection', () => {
|
||||||
@@ -50,6 +50,6 @@ describe('Advanced Flow Rendering', () => {
|
|||||||
.trigger('mouseup', { force: true })
|
.trigger('mouseup', { force: true })
|
||||||
.should('not.have.class', 'connecting');
|
.should('not.have.class', 'connecting');
|
||||||
|
|
||||||
cy.get('.react-flow__edge').should('have.length', 10);
|
cy.get('.react-flow__edge').should('have.length', 9);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
describe('Empty Flow Rendering', () => {
|
||||||
|
it('renders an empty graph', () => {
|
||||||
|
cy.visit('/empty');
|
||||||
|
|
||||||
|
cy.get('.react-flow__renderer');
|
||||||
|
cy.get('.react-flow__node').should('not.exist');
|
||||||
|
cy.get('.react-flow__edge').should('not.exist');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders a control panel', () => {
|
||||||
|
cy.get('.react-flow__controls');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders a mini map', () => {
|
||||||
|
cy.get('.react-flow__minimap');
|
||||||
|
});
|
||||||
|
});
|
||||||
Vendored
+28
-36
@@ -5804,6 +5804,11 @@ var storeModel = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var selectedNodes = getNodesInside(state.nodes, selection, state.transform);
|
var selectedNodes = getNodesInside(state.nodes, selection, state.transform);
|
||||||
|
if (!selectedNodes.length) {
|
||||||
|
state.nodesSelectionActive = false;
|
||||||
|
state.selectedElements = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
var selectedNodesBbox = getRectOfNodes(selectedNodes);
|
var selectedNodesBbox = getRectOfNodes(selectedNodes);
|
||||||
state.selection = selection;
|
state.selection = selection;
|
||||||
state.nodesSelectionActive = true;
|
state.nodesSelectionActive = true;
|
||||||
@@ -5966,7 +5971,9 @@ var getNodesInside = function (nodes, rect, _a, partially) {
|
|||||||
return nodes.filter(function (_a) {
|
return nodes.filter(function (_a) {
|
||||||
var _b = _a.__rg, position = _b.position, width = _b.width, height = _b.height;
|
var _b = _a.__rg, position = _b.position, width = _b.width, height = _b.height;
|
||||||
var nBox = rectToBox(__assign(__assign({}, position), { width: width, height: height }));
|
var nBox = rectToBox(__assign(__assign({}, position), { width: width, height: height }));
|
||||||
var overlappingArea = (Math.max(rBox.x, nBox.x) - Math.min(rBox.x2, nBox.x2)) * (Math.max(rBox.y, nBox.y) - Math.min(rBox.y2, nBox.y2));
|
var xOverlap = Math.max(0, Math.min(rBox.x2, nBox.x2) - Math.max(rBox.x, nBox.x));
|
||||||
|
var yOverlap = Math.max(0, Math.min(rBox.y2, nBox.y2) - Math.max(rBox.y, nBox.y));
|
||||||
|
var overlappingArea = xOverlap * yOverlap;
|
||||||
if (partially) {
|
if (partially) {
|
||||||
return overlappingArea >= 0;
|
return overlappingArea >= 0;
|
||||||
}
|
}
|
||||||
@@ -6219,57 +6226,39 @@ function getEdgePositions(sourceNode, sourceHandle, sourcePosition, targetNode,
|
|||||||
targetY: targetY,
|
targetY: targetY,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
function renderEdge(edge, props, state) {
|
function renderEdge(edge, props, nodes, selectedElements) {
|
||||||
var edgeType = edge.type || 'default';
|
var _a = edge.source.split('__'), sourceId = _a[0], sourceHandleId = _a[1];
|
||||||
var hasSourceHandleId = edge.source.includes('__');
|
var _b = edge.target.split('__'), targetId = _b[0], targetHandleId = _b[1];
|
||||||
var hasTargetHandleId = edge.target.includes('__');
|
var sourceNode = nodes.find(function (n) { return n.id === sourceId; });
|
||||||
var sourceId = hasSourceHandleId ? edge.source.split('__')[0] : edge.source;
|
var targetNode = nodes.find(function (n) { return n.id === targetId; });
|
||||||
var targetId = hasTargetHandleId ? edge.target.split('__')[0] : edge.target;
|
|
||||||
var sourceHandleId = hasSourceHandleId ? edge.source.split('__')[1] : null;
|
|
||||||
var targetHandleId = hasTargetHandleId ? edge.target.split('__')[1] : null;
|
|
||||||
var sourceNode = state.nodes.find(function (n) { return n.id === sourceId; });
|
|
||||||
var targetNode = state.nodes.find(function (n) { return n.id === targetId; });
|
|
||||||
if (!sourceNode) {
|
if (!sourceNode) {
|
||||||
throw new Error("couldn't create edge for source id: " + sourceId);
|
throw new Error("couldn't create edge for source id: " + sourceId);
|
||||||
}
|
}
|
||||||
if (!targetNode) {
|
if (!targetNode) {
|
||||||
throw new Error("couldn't create edge for target id: " + targetId);
|
throw new Error("couldn't create edge for target id: " + targetId);
|
||||||
}
|
}
|
||||||
|
var edgeType = edge.type || 'default';
|
||||||
var EdgeComponent = props.edgeTypes[edgeType] || props.edgeTypes.default;
|
var EdgeComponent = props.edgeTypes[edgeType] || props.edgeTypes.default;
|
||||||
var sourceHandle = getHandle(sourceNode.__rg.handleBounds.source, sourceHandleId);
|
var sourceHandle = getHandle(sourceNode.__rg.handleBounds.source, sourceHandleId);
|
||||||
var targetHandle = getHandle(targetNode.__rg.handleBounds.target, targetHandleId);
|
var targetHandle = getHandle(targetNode.__rg.handleBounds.target, targetHandleId);
|
||||||
var sourcePosition = sourceHandle ? sourceHandle.position : Position.Bottom;
|
var sourcePosition = sourceHandle ? sourceHandle.position : Position.Bottom;
|
||||||
var targetPosition = targetHandle ? targetHandle.position : Position.Top;
|
var targetPosition = targetHandle ? targetHandle.position : Position.Top;
|
||||||
var _a = getEdgePositions(sourceNode, sourceHandle, sourcePosition, targetNode, targetHandle, targetPosition), sourceX = _a.sourceX, sourceY = _a.sourceY, targetX = _a.targetX, targetY = _a.targetY;
|
var _c = getEdgePositions(sourceNode, sourceHandle, sourcePosition, targetNode, targetHandle, targetPosition), sourceX = _c.sourceX, sourceY = _c.sourceY, targetX = _c.targetX, targetY = _c.targetY;
|
||||||
var selected = state.selectedElements
|
var isSelected = selectedElements.some(function (elm) { return isEdge(elm) && elm.source === sourceId && elm.target === targetId; });
|
||||||
.filter(isEdge)
|
return (React.createElement(EdgeComponent, { key: edge.id, id: edge.id, type: edge.type, onClick: props.onElementClick, selected: isSelected, animated: edge.animated, style: edge.style, source: sourceId, target: targetId, sourceHandleId: sourceHandleId, targetHandleId: targetHandleId, sourceX: sourceX, sourceY: sourceY, targetX: targetX, targetY: targetY, sourcePosition: sourcePosition, targetPosition: targetPosition }));
|
||||||
.find(function (elm) { return elm.source === sourceId && elm.target === targetId; });
|
|
||||||
return (React.createElement(EdgeComponent, { key: edge.id, id: edge.id, type: edge.type, onClick: props.onElementClick, selected: selected, animated: edge.animated, style: edge.style, source: sourceId, target: targetId, sourceHandleId: sourceHandleId, targetHandleId: targetHandleId, sourceX: sourceX, sourceY: sourceY, targetX: targetX, targetY: targetY, sourcePosition: sourcePosition, targetPosition: targetPosition }));
|
|
||||||
}
|
}
|
||||||
var EdgeRenderer = memo(function (_a) {
|
var EdgeRenderer = memo(function (props) {
|
||||||
var width = _a.width, height = _a.height, connectionLineStyle = _a.connectionLineStyle, connectionLineType = _a.connectionLineType, rest = __rest(_a, ["width", "height", "connectionLineStyle", "connectionLineType"]);
|
var _a = useStoreState$1(function (s) { return s; }), transform = _a.transform, edges = _a.edges, nodes = _a.nodes, connectionSourceId = _a.connectionSourceId, _b = _a.connectionPosition, x = _b.x, y = _b.y, selectedElements = _a.selectedElements;
|
||||||
var state = useStoreState$1(function (s) { return ({
|
var width = props.width, height = props.height, connectionLineStyle = props.connectionLineStyle, connectionLineType = props.connectionLineType;
|
||||||
nodes: s.nodes,
|
|
||||||
edges: s.edges,
|
|
||||||
transform: s.transform,
|
|
||||||
selectedElements: s.selectedElements,
|
|
||||||
connectionSourceId: s.connectionSourceId,
|
|
||||||
position: s.connectionPosition,
|
|
||||||
}); });
|
|
||||||
if (!width) {
|
if (!width) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var transform = state.transform, edges = state.edges, nodes = state.nodes, connectionSourceId = state.connectionSourceId, position = state.position;
|
var tx = transform[0], ty = transform[1], tScale = transform[2];
|
||||||
var transformStyle = "translate(" + transform[0] + "," + transform[1] + ") scale(" + transform[2] + ")";
|
var transformStyle = "translate(" + tx + "," + ty + ") scale(" + tScale + ")";
|
||||||
return (React.createElement("svg", { width: width, height: height, className: "react-flow__edges" },
|
return (React.createElement("svg", { width: width, height: height, className: "react-flow__edges" },
|
||||||
React.createElement("g", { transform: transformStyle },
|
React.createElement("g", { transform: transformStyle },
|
||||||
edges.map(function (e) {
|
edges.map(function (e) { return renderEdge(e, props, nodes, selectedElements); }),
|
||||||
return renderEdge(e, __assign({ width: width,
|
connectionSourceId && (React.createElement(ConnectionLine, { nodes: nodes, connectionSourceId: connectionSourceId, connectionPositionX: x, connectionPositionY: y, transform: transform, connectionLineStyle: connectionLineStyle, connectionLineType: connectionLineType })))));
|
||||||
height: height,
|
|
||||||
connectionLineStyle: connectionLineStyle,
|
|
||||||
connectionLineType: connectionLineType }, rest), state);
|
|
||||||
}),
|
|
||||||
connectionSourceId && (React.createElement(ConnectionLine, { nodes: nodes, connectionSourceId: connectionSourceId, connectionPositionX: position.x, connectionPositionY: position.y, transform: transform, connectionLineStyle: connectionLineStyle, connectionLineType: connectionLineType })))));
|
|
||||||
});
|
});
|
||||||
EdgeRenderer.displayName = 'EdgeRenderer';
|
EdgeRenderer.displayName = 'EdgeRenderer';
|
||||||
|
|
||||||
@@ -8041,10 +8030,13 @@ var NodesSelection = memo(function () {
|
|||||||
transform: s.transform,
|
transform: s.transform,
|
||||||
selectedNodesBbox: s.selectedNodesBbox,
|
selectedNodesBbox: s.selectedNodesBbox,
|
||||||
selectedElements: s.selectedElements,
|
selectedElements: s.selectedElements,
|
||||||
|
snapToGrid: s.snapToGrid,
|
||||||
|
snapGrid: s.snapGrid
|
||||||
}); });
|
}); });
|
||||||
var updateNodePos = useStoreActions$1(function (a) { return a.updateNodePos; });
|
var updateNodePos = useStoreActions$1(function (a) { return a.updateNodePos; });
|
||||||
var _c = state.transform, x = _c[0], y = _c[1], k = _c[2];
|
var _c = state.transform, x = _c[0], y = _c[1], k = _c[2];
|
||||||
var position = state.selectedNodesBbox;
|
var position = state.selectedNodesBbox;
|
||||||
|
var grid = (state.snapToGrid ? state.snapGrid : [1, 1]);
|
||||||
var onStart = function (evt) {
|
var onStart = function (evt) {
|
||||||
var scaledClient = {
|
var scaledClient = {
|
||||||
x: evt.clientX * (1 / k),
|
x: evt.clientX * (1 / k),
|
||||||
@@ -8082,7 +8074,7 @@ var NodesSelection = memo(function () {
|
|||||||
return (React.createElement("div", { className: "react-flow__nodesselection", style: {
|
return (React.createElement("div", { className: "react-flow__nodesselection", style: {
|
||||||
transform: "translate(" + x + "px," + y + "px) scale(" + k + ")",
|
transform: "translate(" + x + "px," + y + "px) scale(" + k + ")",
|
||||||
} },
|
} },
|
||||||
React.createElement(reactDraggable, { scale: k, onStart: function (evt) { return onStart(evt); }, onDrag: function (evt) { return onDrag(evt); } },
|
React.createElement(reactDraggable, { scale: k, grid: grid, onStart: function (evt) { return onStart(evt); }, onDrag: function (evt) { return onDrag(evt); } },
|
||||||
React.createElement("div", { className: "react-flow__nodesselection-rect", style: {
|
React.createElement("div", { className: "react-flow__nodesselection-rect", style: {
|
||||||
width: state.selectedNodesBbox.width,
|
width: state.selectedNodesBbox.width,
|
||||||
height: state.selectedNodesBbox.height,
|
height: state.selectedNodesBbox.height,
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+28
-36
@@ -5811,6 +5811,11 @@ var storeModel = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var selectedNodes = getNodesInside(state.nodes, selection, state.transform);
|
var selectedNodes = getNodesInside(state.nodes, selection, state.transform);
|
||||||
|
if (!selectedNodes.length) {
|
||||||
|
state.nodesSelectionActive = false;
|
||||||
|
state.selectedElements = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
var selectedNodesBbox = getRectOfNodes(selectedNodes);
|
var selectedNodesBbox = getRectOfNodes(selectedNodes);
|
||||||
state.selection = selection;
|
state.selection = selection;
|
||||||
state.nodesSelectionActive = true;
|
state.nodesSelectionActive = true;
|
||||||
@@ -5973,7 +5978,9 @@ var getNodesInside = function (nodes, rect, _a, partially) {
|
|||||||
return nodes.filter(function (_a) {
|
return nodes.filter(function (_a) {
|
||||||
var _b = _a.__rg, position = _b.position, width = _b.width, height = _b.height;
|
var _b = _a.__rg, position = _b.position, width = _b.width, height = _b.height;
|
||||||
var nBox = rectToBox(__assign(__assign({}, position), { width: width, height: height }));
|
var nBox = rectToBox(__assign(__assign({}, position), { width: width, height: height }));
|
||||||
var overlappingArea = (Math.max(rBox.x, nBox.x) - Math.min(rBox.x2, nBox.x2)) * (Math.max(rBox.y, nBox.y) - Math.min(rBox.y2, nBox.y2));
|
var xOverlap = Math.max(0, Math.min(rBox.x2, nBox.x2) - Math.max(rBox.x, nBox.x));
|
||||||
|
var yOverlap = Math.max(0, Math.min(rBox.y2, nBox.y2) - Math.max(rBox.y, nBox.y));
|
||||||
|
var overlappingArea = xOverlap * yOverlap;
|
||||||
if (partially) {
|
if (partially) {
|
||||||
return overlappingArea >= 0;
|
return overlappingArea >= 0;
|
||||||
}
|
}
|
||||||
@@ -6226,57 +6233,39 @@ function getEdgePositions(sourceNode, sourceHandle, sourcePosition, targetNode,
|
|||||||
targetY: targetY,
|
targetY: targetY,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
function renderEdge(edge, props, state) {
|
function renderEdge(edge, props, nodes, selectedElements) {
|
||||||
var edgeType = edge.type || 'default';
|
var _a = edge.source.split('__'), sourceId = _a[0], sourceHandleId = _a[1];
|
||||||
var hasSourceHandleId = edge.source.includes('__');
|
var _b = edge.target.split('__'), targetId = _b[0], targetHandleId = _b[1];
|
||||||
var hasTargetHandleId = edge.target.includes('__');
|
var sourceNode = nodes.find(function (n) { return n.id === sourceId; });
|
||||||
var sourceId = hasSourceHandleId ? edge.source.split('__')[0] : edge.source;
|
var targetNode = nodes.find(function (n) { return n.id === targetId; });
|
||||||
var targetId = hasTargetHandleId ? edge.target.split('__')[0] : edge.target;
|
|
||||||
var sourceHandleId = hasSourceHandleId ? edge.source.split('__')[1] : null;
|
|
||||||
var targetHandleId = hasTargetHandleId ? edge.target.split('__')[1] : null;
|
|
||||||
var sourceNode = state.nodes.find(function (n) { return n.id === sourceId; });
|
|
||||||
var targetNode = state.nodes.find(function (n) { return n.id === targetId; });
|
|
||||||
if (!sourceNode) {
|
if (!sourceNode) {
|
||||||
throw new Error("couldn't create edge for source id: " + sourceId);
|
throw new Error("couldn't create edge for source id: " + sourceId);
|
||||||
}
|
}
|
||||||
if (!targetNode) {
|
if (!targetNode) {
|
||||||
throw new Error("couldn't create edge for target id: " + targetId);
|
throw new Error("couldn't create edge for target id: " + targetId);
|
||||||
}
|
}
|
||||||
|
var edgeType = edge.type || 'default';
|
||||||
var EdgeComponent = props.edgeTypes[edgeType] || props.edgeTypes.default;
|
var EdgeComponent = props.edgeTypes[edgeType] || props.edgeTypes.default;
|
||||||
var sourceHandle = getHandle(sourceNode.__rg.handleBounds.source, sourceHandleId);
|
var sourceHandle = getHandle(sourceNode.__rg.handleBounds.source, sourceHandleId);
|
||||||
var targetHandle = getHandle(targetNode.__rg.handleBounds.target, targetHandleId);
|
var targetHandle = getHandle(targetNode.__rg.handleBounds.target, targetHandleId);
|
||||||
var sourcePosition = sourceHandle ? sourceHandle.position : Position.Bottom;
|
var sourcePosition = sourceHandle ? sourceHandle.position : Position.Bottom;
|
||||||
var targetPosition = targetHandle ? targetHandle.position : Position.Top;
|
var targetPosition = targetHandle ? targetHandle.position : Position.Top;
|
||||||
var _a = getEdgePositions(sourceNode, sourceHandle, sourcePosition, targetNode, targetHandle, targetPosition), sourceX = _a.sourceX, sourceY = _a.sourceY, targetX = _a.targetX, targetY = _a.targetY;
|
var _c = getEdgePositions(sourceNode, sourceHandle, sourcePosition, targetNode, targetHandle, targetPosition), sourceX = _c.sourceX, sourceY = _c.sourceY, targetX = _c.targetX, targetY = _c.targetY;
|
||||||
var selected = state.selectedElements
|
var isSelected = selectedElements.some(function (elm) { return isEdge(elm) && elm.source === sourceId && elm.target === targetId; });
|
||||||
.filter(isEdge)
|
return (React__default.createElement(EdgeComponent, { key: edge.id, id: edge.id, type: edge.type, onClick: props.onElementClick, selected: isSelected, animated: edge.animated, style: edge.style, source: sourceId, target: targetId, sourceHandleId: sourceHandleId, targetHandleId: targetHandleId, sourceX: sourceX, sourceY: sourceY, targetX: targetX, targetY: targetY, sourcePosition: sourcePosition, targetPosition: targetPosition }));
|
||||||
.find(function (elm) { return elm.source === sourceId && elm.target === targetId; });
|
|
||||||
return (React__default.createElement(EdgeComponent, { key: edge.id, id: edge.id, type: edge.type, onClick: props.onElementClick, selected: selected, animated: edge.animated, style: edge.style, source: sourceId, target: targetId, sourceHandleId: sourceHandleId, targetHandleId: targetHandleId, sourceX: sourceX, sourceY: sourceY, targetX: targetX, targetY: targetY, sourcePosition: sourcePosition, targetPosition: targetPosition }));
|
|
||||||
}
|
}
|
||||||
var EdgeRenderer = React.memo(function (_a) {
|
var EdgeRenderer = React.memo(function (props) {
|
||||||
var width = _a.width, height = _a.height, connectionLineStyle = _a.connectionLineStyle, connectionLineType = _a.connectionLineType, rest = __rest(_a, ["width", "height", "connectionLineStyle", "connectionLineType"]);
|
var _a = useStoreState$1(function (s) { return s; }), transform = _a.transform, edges = _a.edges, nodes = _a.nodes, connectionSourceId = _a.connectionSourceId, _b = _a.connectionPosition, x = _b.x, y = _b.y, selectedElements = _a.selectedElements;
|
||||||
var state = useStoreState$1(function (s) { return ({
|
var width = props.width, height = props.height, connectionLineStyle = props.connectionLineStyle, connectionLineType = props.connectionLineType;
|
||||||
nodes: s.nodes,
|
|
||||||
edges: s.edges,
|
|
||||||
transform: s.transform,
|
|
||||||
selectedElements: s.selectedElements,
|
|
||||||
connectionSourceId: s.connectionSourceId,
|
|
||||||
position: s.connectionPosition,
|
|
||||||
}); });
|
|
||||||
if (!width) {
|
if (!width) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var transform = state.transform, edges = state.edges, nodes = state.nodes, connectionSourceId = state.connectionSourceId, position = state.position;
|
var tx = transform[0], ty = transform[1], tScale = transform[2];
|
||||||
var transformStyle = "translate(" + transform[0] + "," + transform[1] + ") scale(" + transform[2] + ")";
|
var transformStyle = "translate(" + tx + "," + ty + ") scale(" + tScale + ")";
|
||||||
return (React__default.createElement("svg", { width: width, height: height, className: "react-flow__edges" },
|
return (React__default.createElement("svg", { width: width, height: height, className: "react-flow__edges" },
|
||||||
React__default.createElement("g", { transform: transformStyle },
|
React__default.createElement("g", { transform: transformStyle },
|
||||||
edges.map(function (e) {
|
edges.map(function (e) { return renderEdge(e, props, nodes, selectedElements); }),
|
||||||
return renderEdge(e, __assign({ width: width,
|
connectionSourceId && (React__default.createElement(ConnectionLine, { nodes: nodes, connectionSourceId: connectionSourceId, connectionPositionX: x, connectionPositionY: y, transform: transform, connectionLineStyle: connectionLineStyle, connectionLineType: connectionLineType })))));
|
||||||
height: height,
|
|
||||||
connectionLineStyle: connectionLineStyle,
|
|
||||||
connectionLineType: connectionLineType }, rest), state);
|
|
||||||
}),
|
|
||||||
connectionSourceId && (React__default.createElement(ConnectionLine, { nodes: nodes, connectionSourceId: connectionSourceId, connectionPositionX: position.x, connectionPositionY: position.y, transform: transform, connectionLineStyle: connectionLineStyle, connectionLineType: connectionLineType })))));
|
|
||||||
});
|
});
|
||||||
EdgeRenderer.displayName = 'EdgeRenderer';
|
EdgeRenderer.displayName = 'EdgeRenderer';
|
||||||
|
|
||||||
@@ -8048,10 +8037,13 @@ var NodesSelection = React.memo(function () {
|
|||||||
transform: s.transform,
|
transform: s.transform,
|
||||||
selectedNodesBbox: s.selectedNodesBbox,
|
selectedNodesBbox: s.selectedNodesBbox,
|
||||||
selectedElements: s.selectedElements,
|
selectedElements: s.selectedElements,
|
||||||
|
snapToGrid: s.snapToGrid,
|
||||||
|
snapGrid: s.snapGrid
|
||||||
}); });
|
}); });
|
||||||
var updateNodePos = useStoreActions$1(function (a) { return a.updateNodePos; });
|
var updateNodePos = useStoreActions$1(function (a) { return a.updateNodePos; });
|
||||||
var _c = state.transform, x = _c[0], y = _c[1], k = _c[2];
|
var _c = state.transform, x = _c[0], y = _c[1], k = _c[2];
|
||||||
var position = state.selectedNodesBbox;
|
var position = state.selectedNodesBbox;
|
||||||
|
var grid = (state.snapToGrid ? state.snapGrid : [1, 1]);
|
||||||
var onStart = function (evt) {
|
var onStart = function (evt) {
|
||||||
var scaledClient = {
|
var scaledClient = {
|
||||||
x: evt.clientX * (1 / k),
|
x: evt.clientX * (1 / k),
|
||||||
@@ -8089,7 +8081,7 @@ var NodesSelection = React.memo(function () {
|
|||||||
return (React__default.createElement("div", { className: "react-flow__nodesselection", style: {
|
return (React__default.createElement("div", { className: "react-flow__nodesselection", style: {
|
||||||
transform: "translate(" + x + "px," + y + "px) scale(" + k + ")",
|
transform: "translate(" + x + "px," + y + "px) scale(" + k + ")",
|
||||||
} },
|
} },
|
||||||
React__default.createElement(reactDraggable, { scale: k, onStart: function (evt) { return onStart(evt); }, onDrag: function (evt) { return onDrag(evt); } },
|
React__default.createElement(reactDraggable, { scale: k, grid: grid, onStart: function (evt) { return onStart(evt); }, onDrag: function (evt) { return onDrag(evt); } },
|
||||||
React__default.createElement("div", { className: "react-flow__nodesselection-rect", style: {
|
React__default.createElement("div", { className: "react-flow__nodesselection-rect", style: {
|
||||||
width: state.selectedNodesBbox.width,
|
width: state.selectedNodesBbox.width,
|
||||||
height: state.selectedNodesBbox.height,
|
height: state.selectedNodesBbox.height,
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -7,5 +7,5 @@ interface EdgeRendererProps {
|
|||||||
connectionLineType?: string;
|
connectionLineType?: string;
|
||||||
onElementClick?: () => void;
|
onElementClick?: () => void;
|
||||||
}
|
}
|
||||||
declare const EdgeRenderer: React.MemoExoticComponent<({ width, height, connectionLineStyle, connectionLineType, ...rest }: EdgeRendererProps) => JSX.Element | null>;
|
declare const EdgeRenderer: React.MemoExoticComponent<(props: EdgeRendererProps) => JSX.Element | null>;
|
||||||
export default EdgeRenderer;
|
export default EdgeRenderer;
|
||||||
|
|||||||
@@ -1,174 +0,0 @@
|
|||||||
import React, { PureComponent } from 'react';
|
|
||||||
|
|
||||||
import Graph, { isEdge, removeElements, addEdge, getOutgoers, MiniMap, Controls } from 'react-flow';
|
|
||||||
|
|
||||||
import SpecialNode from './SpecialNode';
|
|
||||||
import InputNode from './InputNode';
|
|
||||||
|
|
||||||
const onNodeDragStop = node => console.log('drag stop', node);
|
|
||||||
|
|
||||||
class App extends PureComponent {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
|
|
||||||
const onChange = (option, d) => {
|
|
||||||
this.setState(prevState => (
|
|
||||||
{elements: prevState.elements.map(e => {
|
|
||||||
if (isEdge(e) || e.id !== '6') {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...e,
|
|
||||||
data: {
|
|
||||||
...e.data,
|
|
||||||
label: `Option ${option} selected.`
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})}
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
const onChangeInput = (input, d) => {
|
|
||||||
this.setState(prevState => (
|
|
||||||
{elements: prevState.elements.map(e => {
|
|
||||||
if (isEdge(e) || e.id !== '8') {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.id === '8') {
|
|
||||||
return {
|
|
||||||
...e,
|
|
||||||
data: {
|
|
||||||
...e.data,
|
|
||||||
input: input || 'write something'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
})}
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.state = {
|
|
||||||
graphLoaded: false,
|
|
||||||
elements: [
|
|
||||||
{ id: '1', type: 'input', data: { label: '1 Tests' }, position: { x: 250, y: 5 } },
|
|
||||||
{ id: '2', data: { label: '2 This is a node This is a node This is a node This is a node' }, position: { x: 100, y: 100 } },
|
|
||||||
{ id: '3', data: { label: '3 I bring my own style' }, position: { x: 100, y: 200 }, style: { background: '#eee', color: '#222', border: '1px solid #bbb' } },
|
|
||||||
{ id: '4', type: 'output', data: { label: '4 nody nodes' }, position: { x: 50, y: 300 } },
|
|
||||||
{ id: '5', type: 'default', data: { label: '5 Another node'}, position: { x: 400, y: 300 } },
|
|
||||||
{ id: '6', type: 'special', data: { onChange, label: '6 no option selected' }, position: { x: 425, y: 375 } },
|
|
||||||
{ id: '7', type: 'output', data: { label: '7 output' }, position: { x: 250, y: 500 } },
|
|
||||||
{ id: '8', type: 'text', data: { onChange: onChangeInput, input: 'write something' }, position: { x: 350, y: 100 } },
|
|
||||||
{ id: '9', type: 'text', data: { label: 'right' }, position: { x: 600, y: 100 } },
|
|
||||||
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
|
||||||
{ id: 'e1-8', source: '1', target: '8', animated: true },
|
|
||||||
{ id: 'e8-9', source: '8', target: '9', animated: true },
|
|
||||||
{ id: 'e2-3', source: '2', target: '3' },
|
|
||||||
{ id: 'e3-4', source: '3', target: '4', type: 'step' },
|
|
||||||
{ id: 'e3-5', source: '3', target: '5' },
|
|
||||||
{ id: 'e5-6b', source: '5', target: '6__b' },
|
|
||||||
{ id: 'e5-6a', source: '5', target: '6__a', type: 'step', animated: true, style: { stroke: '#FFCC00' } },
|
|
||||||
{ id: 'e6-7', source: '6', target: '7', style: { stroke: '#FFCC00' }},
|
|
||||||
]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
onLoad(graphInstance) {
|
|
||||||
console.log('graph loaded:', graphInstance);
|
|
||||||
window.rg = graphInstance;
|
|
||||||
|
|
||||||
this.graphInstance = graphInstance;
|
|
||||||
this.graphInstance.fitView({ padding: 0.1 });
|
|
||||||
this.setState({
|
|
||||||
graphLoaded: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
onFitView() {
|
|
||||||
this.graphInstance.fitView();
|
|
||||||
}
|
|
||||||
|
|
||||||
onAdd() {
|
|
||||||
this.setState(prevState => ({
|
|
||||||
...prevState,
|
|
||||||
elements: prevState.elements.concat({
|
|
||||||
id: (prevState.elements.length + 1).toString(),
|
|
||||||
data: { label: 'Added node' },
|
|
||||||
position: { x: Math.random() * window.innerWidth, y: Math.random() * window.innerHeight }
|
|
||||||
})
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
onElementClick(element) {
|
|
||||||
console.log('click', element);
|
|
||||||
console.log('outgoers', getOutgoers(element, this.state.elements));
|
|
||||||
}
|
|
||||||
|
|
||||||
onZoomIn() {
|
|
||||||
this.graphInstance.zoomIn();
|
|
||||||
}
|
|
||||||
|
|
||||||
onZoomOut() {
|
|
||||||
this.graphInstance.zoomOut();
|
|
||||||
}
|
|
||||||
|
|
||||||
onElementsRemove(elementsToRemove) {
|
|
||||||
this.setState(prevState => ({
|
|
||||||
elements: removeElements(elementsToRemove, prevState.elements)
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
onConnect(params) {
|
|
||||||
this.setState(prevState => ({
|
|
||||||
elements: addEdge(params, prevState.elements)
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
return (
|
|
||||||
<Graph
|
|
||||||
elements={this.state.elements}
|
|
||||||
onElementClick={element => this.onElementClick(element)}
|
|
||||||
onElementsRemove={elements => this.onElementsRemove(elements)}
|
|
||||||
onConnect={params => this.onConnect(params)}
|
|
||||||
onNodeDragStop={onNodeDragStop}
|
|
||||||
style={{ width: '100%', height: '100%' }}
|
|
||||||
onLoad={graphInstance => this.onLoad(graphInstance)}
|
|
||||||
nodeTypes={{
|
|
||||||
special: SpecialNode,
|
|
||||||
text: InputNode
|
|
||||||
}}
|
|
||||||
connectionLineStyle={{ stroke: '#ddd', strokeWidth: 2 }}
|
|
||||||
connectionLineType="bezier"
|
|
||||||
backgroundColor="#888"
|
|
||||||
backgroundGap={16}
|
|
||||||
snapToGrid={true}
|
|
||||||
snapGrid={[16, 16]}
|
|
||||||
>
|
|
||||||
<MiniMap
|
|
||||||
style={{ position: 'absolute', right: 10, bottom: 10, backgroundColor: '#f8f8f8'}}
|
|
||||||
nodeColor={n => {
|
|
||||||
if (n.type === 'input') return 'blue';
|
|
||||||
if (n.type === 'output') return 'green';
|
|
||||||
if (n.type === 'default') return 'red';
|
|
||||||
|
|
||||||
return '#FFCC00';
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<Controls />
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => this.onAdd()}
|
|
||||||
style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}
|
|
||||||
>
|
|
||||||
add
|
|
||||||
</button>
|
|
||||||
</Graph>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default App;
|
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import React, { useState, useEffect } from 'react';
|
||||||
|
|
||||||
|
import Graph, { isEdge, removeElements, addEdge, MiniMap, Controls } from 'react-flow';
|
||||||
|
|
||||||
|
import SpecialNode from './SpecialNode';
|
||||||
|
import InputNode from './InputNode';
|
||||||
|
|
||||||
|
const onNodeDragStop = node => console.log('drag stop', node);
|
||||||
|
const onElementClick = element => console.log('click', element);
|
||||||
|
const onLoad = (graph) => console.log('graph loaded:', graph);
|
||||||
|
|
||||||
|
const CustomNodesGraph = () => {
|
||||||
|
const [elements, setElements] = useState([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onChange = (option, d) => {
|
||||||
|
setElements(els => els.map(e => {
|
||||||
|
if (isEdge(e) || e.id !== '6') {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...e,
|
||||||
|
data: {
|
||||||
|
...e.data,
|
||||||
|
label: `Option ${option} selected.`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
const onChangeInput = (input, d) => {
|
||||||
|
setElements(els => els.map(e => {
|
||||||
|
if (isEdge(e) || e.id !== '8') {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...e,
|
||||||
|
data: {
|
||||||
|
...e.data,
|
||||||
|
input: input || 'write something'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
setElements([
|
||||||
|
{ id: '1', type: 'input', data: { label: '1 Tests' }, position: { x: 250, y: 5 } },
|
||||||
|
{ id: '2', data: { label: '2 This is a node This is a node This is a node This is a node' }, position: { x: 100, y: 100 } },
|
||||||
|
{ id: '3', data: { label: '3 I bring my own style' }, position: { x: 100, y: 200 }, style: { background: '#eee', color: '#222', border: '1px solid #bbb' } },
|
||||||
|
{ id: '4', type: 'output', data: { label: '4 nody nodes' }, position: { x: 50, y: 300 } },
|
||||||
|
{ id: '5', type: 'default', data: { label: '5 Another node'}, position: { x: 400, y: 300 } },
|
||||||
|
{ id: '6', type: 'special', data: { onChange: onChange, label: '6 no option selected' }, position: { x: 425, y: 375 } },
|
||||||
|
{ id: '7', type: 'output', data: { label: '7 output' }, position: { x: 250, y: 500 } },
|
||||||
|
{ id: '8', type: 'text', data: { onChange: onChangeInput, input: 'write something' }, position: { x: 350, y: 100 } },
|
||||||
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
|
{ id: 'e1-8', source: '1', target: '8', animated: true },
|
||||||
|
{ id: 'e2-3', source: '2', target: '3', animated: true },
|
||||||
|
{ id: 'e3-4', source: '3', target: '4', animated: true },
|
||||||
|
{ id: 'e3-5', source: '3', target: '5', animated: true },
|
||||||
|
{ id: 'e5-6b', source: '5', target: '6__b', animated: true },
|
||||||
|
{ id: 'e5-6a', source: '5', target: '6__a', animated: true },
|
||||||
|
{ id: 'e6-7', source: '6', target: '7', animated: true },
|
||||||
|
])
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const addRandomNode = () => {
|
||||||
|
setElements(els => els.concat({
|
||||||
|
id: (els.length + 1).toString(),
|
||||||
|
data: { label: 'Added node' },
|
||||||
|
position: { x: Math.random() * window.innerWidth, y: Math.random() * window.innerHeight }
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
const onElementsRemove = (elementsToRemove) => setElements(els => removeElements(elementsToRemove, els));
|
||||||
|
const onConnect = (params) => setElements(els => addEdge(params, els));
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Graph
|
||||||
|
elements={elements}
|
||||||
|
onElementClick={onElementClick}
|
||||||
|
onElementsRemove={onElementsRemove}
|
||||||
|
onConnect={onConnect}
|
||||||
|
onNodeDragStop={onNodeDragStop}
|
||||||
|
style={{ width: '100%', height: '100%' }}
|
||||||
|
onLoad={onLoad}
|
||||||
|
nodeTypes={{
|
||||||
|
special: SpecialNode,
|
||||||
|
text: InputNode
|
||||||
|
}}
|
||||||
|
connectionLineStyle={{ stroke: '#ddd', strokeWidth: 2 }}
|
||||||
|
connectionLineType="bezier"
|
||||||
|
backgroundColor="#888"
|
||||||
|
backgroundGap={16}
|
||||||
|
snapToGrid={true}
|
||||||
|
snapGrid={[16, 16]}
|
||||||
|
>
|
||||||
|
<MiniMap
|
||||||
|
nodeColor={n => {
|
||||||
|
if (n.type === 'input') return 'blue';
|
||||||
|
if (n.type === 'output') return 'green';
|
||||||
|
if (n.type === 'default') return 'red';
|
||||||
|
|
||||||
|
return '#FFCC00';
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Controls />
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={addRandomNode}
|
||||||
|
style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}
|
||||||
|
>
|
||||||
|
add node
|
||||||
|
</button>
|
||||||
|
</Graph>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CustomNodesGraph;
|
||||||
@@ -11,7 +11,7 @@ const EmptyGraph = () => {
|
|||||||
const onElementsRemove = (elementsToRemove) =>
|
const onElementsRemove = (elementsToRemove) =>
|
||||||
setElements(els => removeElements(elementsToRemove, els));
|
setElements(els => removeElements(elementsToRemove, els));
|
||||||
const onConnect = (params) => setElements(els => addEdge(params, els));
|
const onConnect = (params) => setElements(els => addEdge(params, els));
|
||||||
const onAdd = () => {
|
const addRandomNode = () => {
|
||||||
const nodeId = (elements.length + 1).toString();
|
const nodeId = (elements.length + 1).toString();
|
||||||
const newNode = {
|
const newNode = {
|
||||||
id: nodeId,
|
id: nodeId,
|
||||||
@@ -36,10 +36,10 @@ const EmptyGraph = () => {
|
|||||||
<Controls />
|
<Controls />
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={onAdd}
|
onClick={addRandomNode}
|
||||||
style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}
|
style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}
|
||||||
>
|
>
|
||||||
add
|
add node
|
||||||
</button>
|
</button>
|
||||||
</Graph>
|
</Graph>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React from 'react';
|
|||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
|
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
|
||||||
|
|
||||||
import Advanced from './Advanced';
|
import CustomNodes from './CustomNodes';
|
||||||
import Basic from './Basic';
|
import Basic from './Basic';
|
||||||
import Empty from './Empty';
|
import Empty from './Empty';
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ ReactDOM.render((
|
|||||||
<Empty />
|
<Empty />
|
||||||
</Route>
|
</Route>
|
||||||
<Route path="/">
|
<Route path="/">
|
||||||
<Advanced />
|
<CustomNodes />
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
</Router>
|
</Router>
|
||||||
|
|||||||
@@ -29,10 +29,13 @@ export default memo(() => {
|
|||||||
transform: s.transform,
|
transform: s.transform,
|
||||||
selectedNodesBbox: s.selectedNodesBbox,
|
selectedNodesBbox: s.selectedNodesBbox,
|
||||||
selectedElements: s.selectedElements,
|
selectedElements: s.selectedElements,
|
||||||
|
snapToGrid: s.snapToGrid,
|
||||||
|
snapGrid: s.snapGrid
|
||||||
}));
|
}));
|
||||||
const updateNodePos = useStoreActions(a => a.updateNodePos);
|
const updateNodePos = useStoreActions(a => a.updateNodePos);
|
||||||
const [x, y, k] = state.transform;
|
const [x, y, k] = state.transform;
|
||||||
const position = state.selectedNodesBbox;
|
const position = state.selectedNodesBbox;
|
||||||
|
const grid = (state.snapToGrid ? state.snapGrid : [1, 1])! as [number, number];
|
||||||
|
|
||||||
const onStart = (evt: MouseEvent) => {
|
const onStart = (evt: MouseEvent) => {
|
||||||
const scaledClient: XYPosition = {
|
const scaledClient: XYPosition = {
|
||||||
@@ -84,6 +87,7 @@ export default memo(() => {
|
|||||||
>
|
>
|
||||||
<ReactDraggable
|
<ReactDraggable
|
||||||
scale={k}
|
scale={k}
|
||||||
|
grid={grid}
|
||||||
onStart={evt => onStart(evt as MouseEvent)}
|
onStart={evt => onStart(evt as MouseEvent)}
|
||||||
onDrag={evt => onDrag(evt as MouseEvent)}
|
onDrag={evt => onDrag(evt as MouseEvent)}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import React, { memo, SVGAttributes } from 'react';
|
|||||||
import { useStoreState } from '../../store/hooks';
|
import { useStoreState } from '../../store/hooks';
|
||||||
import ConnectionLine from '../../components/ConnectionLine/index';
|
import ConnectionLine from '../../components/ConnectionLine/index';
|
||||||
import { isEdge } from '../../utils/graph';
|
import { isEdge } from '../../utils/graph';
|
||||||
import { XYPosition, Position, Edge, Node, ElementId, Transform, HandleElement } from '../../types';
|
import { XYPosition, Position, Edge, Node, ElementId, HandleElement, Elements } from '../../types';
|
||||||
|
|
||||||
interface EdgeRendererProps {
|
interface EdgeRendererProps {
|
||||||
width: number;
|
width: number;
|
||||||
@@ -14,15 +14,6 @@ interface EdgeRendererProps {
|
|||||||
onElementClick?: () => void;
|
onElementClick?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EdgeRendererState {
|
|
||||||
nodes: Node[];
|
|
||||||
edges: Edge[];
|
|
||||||
transform: Transform;
|
|
||||||
selectedElements: any;
|
|
||||||
connectionSourceId: ElementId | null;
|
|
||||||
position: XYPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface EdgePositions {
|
interface EdgePositions {
|
||||||
sourceX: number;
|
sourceX: number;
|
||||||
sourceY: number;
|
sourceY: number;
|
||||||
@@ -122,20 +113,12 @@ function getEdgePositions(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderEdge(edge: Edge, props: EdgeRendererProps, state: EdgeRendererState) {
|
function renderEdge(edge: Edge, props: EdgeRendererProps, nodes: Node[], selectedElements: Elements) {
|
||||||
const edgeType = edge.type || 'default';
|
const [sourceId, sourceHandleId] = edge.source.split('__');
|
||||||
|
const [targetId, targetHandleId] = edge.target.split('__');
|
||||||
|
|
||||||
const hasSourceHandleId = edge.source.includes('__');
|
const sourceNode = nodes.find(n => n.id === sourceId);
|
||||||
const hasTargetHandleId = edge.target.includes('__');
|
const targetNode = nodes.find(n => n.id === targetId);
|
||||||
|
|
||||||
const sourceId = hasSourceHandleId ? edge.source.split('__')[0] : edge.source;
|
|
||||||
const targetId = hasTargetHandleId ? edge.target.split('__')[0] : edge.target;
|
|
||||||
|
|
||||||
const sourceHandleId = hasSourceHandleId ? edge.source.split('__')[1] : null;
|
|
||||||
const targetHandleId = hasTargetHandleId ? edge.target.split('__')[1] : null;
|
|
||||||
|
|
||||||
const sourceNode = state.nodes.find(n => n.id === sourceId);
|
|
||||||
const targetNode = state.nodes.find(n => n.id === targetId);
|
|
||||||
|
|
||||||
if (!sourceNode) {
|
if (!sourceNode) {
|
||||||
throw new Error(`couldn't create edge for source id: ${sourceId}`);
|
throw new Error(`couldn't create edge for source id: ${sourceId}`);
|
||||||
@@ -144,7 +127,7 @@ function renderEdge(edge: Edge, props: EdgeRendererProps, state: EdgeRendererSta
|
|||||||
if (!targetNode) {
|
if (!targetNode) {
|
||||||
throw new Error(`couldn't create edge for target id: ${targetId}`);
|
throw new Error(`couldn't create edge for target id: ${targetId}`);
|
||||||
}
|
}
|
||||||
|
const edgeType = edge.type || 'default';
|
||||||
const EdgeComponent = props.edgeTypes[edgeType] || props.edgeTypes.default;
|
const EdgeComponent = props.edgeTypes[edgeType] || props.edgeTypes.default;
|
||||||
const sourceHandle = getHandle(sourceNode.__rg.handleBounds.source, sourceHandleId);
|
const sourceHandle = getHandle(sourceNode.__rg.handleBounds.source, sourceHandleId);
|
||||||
const targetHandle = getHandle(targetNode.__rg.handleBounds.target, targetHandleId);
|
const targetHandle = getHandle(targetNode.__rg.handleBounds.target, targetHandleId);
|
||||||
@@ -159,9 +142,10 @@ function renderEdge(edge: Edge, props: EdgeRendererProps, state: EdgeRendererSta
|
|||||||
targetHandle,
|
targetHandle,
|
||||||
targetPosition
|
targetPosition
|
||||||
);
|
);
|
||||||
const selected = state.selectedElements
|
|
||||||
.filter(isEdge)
|
const isSelected = (selectedElements as Edge[]).some(
|
||||||
.find((elm: Edge) => elm.source === sourceId && elm.target === targetId);
|
elm => isEdge(elm) && elm.source === sourceId && elm.target === targetId
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<EdgeComponent
|
<EdgeComponent
|
||||||
@@ -169,7 +153,7 @@ function renderEdge(edge: Edge, props: EdgeRendererProps, state: EdgeRendererSta
|
|||||||
id={edge.id}
|
id={edge.id}
|
||||||
type={edge.type}
|
type={edge.type}
|
||||||
onClick={props.onElementClick}
|
onClick={props.onElementClick}
|
||||||
selected={selected}
|
selected={isSelected}
|
||||||
animated={edge.animated}
|
animated={edge.animated}
|
||||||
style={edge.style}
|
style={edge.style}
|
||||||
source={sourceId}
|
source={sourceId}
|
||||||
@@ -186,44 +170,35 @@ function renderEdge(edge: Edge, props: EdgeRendererProps, state: EdgeRendererSta
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const EdgeRenderer = memo(({ width, height, connectionLineStyle, connectionLineType, ...rest }: EdgeRendererProps) => {
|
const EdgeRenderer = memo((props: EdgeRendererProps) => {
|
||||||
const state: EdgeRendererState = useStoreState(s => ({
|
const {
|
||||||
nodes: s.nodes,
|
transform,
|
||||||
edges: s.edges,
|
edges,
|
||||||
transform: s.transform,
|
nodes,
|
||||||
selectedElements: s.selectedElements,
|
connectionSourceId,
|
||||||
connectionSourceId: s.connectionSourceId,
|
connectionPosition: { x, y },
|
||||||
position: s.connectionPosition,
|
selectedElements,
|
||||||
}));
|
} = useStoreState(s => s);
|
||||||
|
|
||||||
|
const { width, height, connectionLineStyle, connectionLineType } = props;
|
||||||
|
|
||||||
if (!width) {
|
if (!width) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { transform, edges, nodes, connectionSourceId, position } = state;
|
const [tx, ty, tScale] = transform;
|
||||||
const transformStyle = `translate(${transform[0]},${transform[1]}) scale(${transform[2]})`;
|
const transformStyle = `translate(${tx},${ty}) scale(${tScale})`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<svg width={width} height={height} className="react-flow__edges">
|
<svg width={width} height={height} className="react-flow__edges">
|
||||||
<g transform={transformStyle}>
|
<g transform={transformStyle}>
|
||||||
{edges.map((e: Edge) =>
|
{edges.map((e: Edge) => renderEdge(e, props, nodes, selectedElements))}
|
||||||
renderEdge(
|
|
||||||
e,
|
|
||||||
{
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
connectionLineStyle,
|
|
||||||
connectionLineType,
|
|
||||||
...rest,
|
|
||||||
},
|
|
||||||
state
|
|
||||||
)
|
|
||||||
)}
|
|
||||||
{connectionSourceId && (
|
{connectionSourceId && (
|
||||||
<ConnectionLine
|
<ConnectionLine
|
||||||
nodes={nodes}
|
nodes={nodes}
|
||||||
connectionSourceId={connectionSourceId}
|
connectionSourceId={connectionSourceId}
|
||||||
connectionPositionX={position.x}
|
connectionPositionX={x}
|
||||||
connectionPositionY={position.y}
|
connectionPositionY={y}
|
||||||
transform={transform}
|
transform={transform}
|
||||||
connectionLineStyle={connectionLineStyle}
|
connectionLineStyle={connectionLineStyle}
|
||||||
connectionLineType={connectionLineType}
|
connectionLineType={connectionLineType}
|
||||||
|
|||||||
@@ -200,6 +200,14 @@ const storeModel: StoreModel = {
|
|||||||
selection,
|
selection,
|
||||||
state.transform
|
state.transform
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!selectedNodes.length) {
|
||||||
|
state.nodesSelectionActive = false;
|
||||||
|
state.selectedElements = [];
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const selectedNodesBbox = getRectOfNodes(selectedNodes);
|
const selectedNodesBbox = getRectOfNodes(selectedNodes);
|
||||||
|
|
||||||
state.selection = selection;
|
state.selection = selection;
|
||||||
|
|||||||
+5
-2
@@ -152,10 +152,13 @@ export const getNodesInside = (
|
|||||||
width: rect.width / tScale,
|
width: rect.width / tScale,
|
||||||
height: rect.height / tScale,
|
height: rect.height / tScale,
|
||||||
});
|
});
|
||||||
|
|
||||||
return nodes.filter(({ __rg: { position, width, height } }) => {
|
return nodes.filter(({ __rg: { position, width, height } }) => {
|
||||||
const nBox = rectToBox({ ...position, width, height });
|
const nBox = rectToBox({ ...position, width, height });
|
||||||
const overlappingArea =
|
const xOverlap = Math.max(0, Math.min(rBox.x2, nBox.x2) - Math.max(rBox.x, nBox.x));
|
||||||
(Math.max(rBox.x, nBox.x) - Math.min(rBox.x2, nBox.x2)) * (Math.max(rBox.y, nBox.y) - Math.min(rBox.y2, nBox.y2));
|
const yOverlap = Math.max(0, Math.min(rBox.y2, nBox.y2) - Math.max(rBox.y, nBox.y));
|
||||||
|
const overlappingArea = xOverlap * yOverlap;
|
||||||
|
|
||||||
if (partially) {
|
if (partially) {
|
||||||
return overlappingArea >= 0;
|
return overlappingArea >= 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user