Merge pull request #255 from wbkd/develop

Develop
This commit is contained in:
Moritz
2020-05-28 11:28:02 +02:00
committed by GitHub
8 changed files with 61 additions and 29 deletions
+1
View File
@@ -132,6 +132,7 @@ Node example: `{ id: '1', type: 'input', data: { label: 'Node 1' }, position: {
- `data`: {} *(required if you are using a standard type, otherwise depends on your implementation)* - `data`: {} *(required if you are using a standard type, otherwise depends on your implementation)*
- `type`: 'input' | 'output' | 'default' or a custom one you implemented - `type`: 'input' | 'output' | 'default' or a custom one you implemented
- `style`: css properties - `style`: css properties
- `className`: additional class name
- `targetPosition`: 'left' | 'right' | 'top' | 'bottom' handle position - default: 'top' - `targetPosition`: 'left' | 'right' | 'top' | 'bottom' handle position - default: 'top'
- `sourcePosition`: 'left' | 'right' | 'top' | 'bottom' handle position - default: 'bottom' - `sourcePosition`: 'left' | 'right' | 'top' | 'bottom' handle position - default: 'bottom'
+18 -2
View File
@@ -7,7 +7,7 @@ const onLoad = (graph) => {
}; };
const initialElements = [ const initialElements = [
{ id: '1', sourcePosition: 'right', type: 'input', data: { label: 'Input' }, position: { x: 0, y: 80 } }, { id: '1', sourcePosition: 'right', type: 'input', className: 'dark-node', data: { label: 'Input' }, position: { x: 0, y: 80 } },
{ id: '2', sourcePosition: 'right', targetPosition: 'left', data: { label: 'A Node' }, position: { x: 250, y: 0 } }, { id: '2', sourcePosition: 'right', targetPosition: 'left', data: { label: 'A Node' }, position: { x: 250, y: 0 } },
{ id: '3', sourcePosition: 'right', targetPosition: 'left', data: { label: 'Another node' }, position: { x: 250, y: 160 } }, { id: '3', sourcePosition: 'right', targetPosition: 'left', data: { label: 'Another node' }, position: { x: 250, y: 160 } },
{ id: '4', sourcePosition: 'right', targetPosition: 'left', data: { label: 'Node 4' }, position: { x: 500, y: 80 } }, { id: '4', sourcePosition: 'right', targetPosition: 'left', data: { label: 'Node 4' }, position: { x: 500, y: 80 } },
@@ -21,6 +21,15 @@ const HorizontalFlow = () => {
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 changeClassName = () => {
setElements(elms => elms.map(el => {
if (el.type === 'input') {
el.className = el.className ? '' : 'dark-node';
}
return {...el};
}))
}
return ( return (
<ReactFlow <ReactFlow
@@ -29,7 +38,14 @@ const HorizontalFlow = () => {
onConnect={onConnect} onConnect={onConnect}
onLoad={onLoad} onLoad={onLoad}
selectNodesOnDrag={false} selectNodesOnDrag={false}
/> >
<button
onClick={changeClassName}
style={{ position: 'absolute', right: 10, top: 30, zIndex: 4 }}
>
change class name
</button>
</ReactFlow>
); );
} }
+5
View File
@@ -136,6 +136,11 @@ nav a.active:before {
color: #111; color: #111;
} }
.dark-node>div {
background: #333 !important;
color: #f8f8f8 !important;
}
@media screen and (min-width: 768px) { @media screen and (min-width: 768px) {
nav { nav {
position: relative; position: relative;
+10 -4
View File
@@ -1,7 +1,7 @@
import React, { useEffect, useState, CSSProperties } from 'react'; import React, { useEffect, useState, CSSProperties } from 'react';
import cx from 'classnames'; import cx from 'classnames';
import { ElementId, Node, Transform, HandleElement } from '../../types'; import { ElementId, Node, Transform, HandleElement, Position } from '../../types';
interface ConnectionLineProps { interface ConnectionLineProps {
connectionSourceId: ElementId; connectionSourceId: ElementId;
@@ -61,9 +61,15 @@ export default ({
let dAttr: string = ''; let dAttr: string = '';
if (connectionLineType === 'bezier') { if (connectionLineType === 'bezier') {
const yOffset = Math.abs(targetY - sourceY) / 2; if (sourceHandle?.position === Position.Left || sourceHandle?.position === Position.Right) {
const centerY = targetY < sourceY ? targetY + yOffset : targetY - yOffset; const xOffset = Math.abs(targetX - sourceX) / 2;
dAttr = `M${sourceX},${sourceY} C${sourceX},${centerY} ${targetX},${centerY} ${targetX},${targetY}`; const centerX = targetX < sourceX ? targetX + xOffset : targetX - xOffset;
dAttr = `M${sourceX},${sourceY} C${centerX},${sourceY} ${centerX},${targetY} ${targetX},${targetY}`;
} else {
const yOffset = Math.abs(targetY - sourceY) / 2;
const centerY = targetY < sourceY ? targetY + yOffset : targetY - yOffset;
dAttr = `M${sourceX},${sourceY} C${sourceX},${centerY} ${targetX},${centerY} ${targetX},${targetY}`;
}
} else { } else {
dAttr = `M${sourceX},${sourceY} ${targetX},${targetY}`; dAttr = `M${sourceX},${sourceY} ${targetX},${targetY}`;
} }
+3 -1
View File
@@ -142,6 +142,7 @@ export default (NodeComponent: ComponentType<NodeComponentProps>) => {
onNodeDragStart, onNodeDragStart,
onNodeDragStop, onNodeDragStop,
style, style,
className,
isInteractive, isInteractive,
selectNodesOnDrag, selectNodesOnDrag,
sourcePosition, sourcePosition,
@@ -151,7 +152,8 @@ export default (NodeComponent: ComponentType<NodeComponentProps>) => {
const [offset, setOffset] = useState({ x: 0, y: 0 }); const [offset, setOffset] = useState({ x: 0, y: 0 });
const [isDragging, setDragging] = useState(false); const [isDragging, setDragging] = useState(false);
const position = { x: xPos, y: yPos }; const position = { x: xPos, y: yPos };
const nodeClasses = cx('react-flow__node', { selected }); const nodeClasses = cx('react-flow__node', `react-flow__node-${type}`, className, { selected });
const nodeStyle: CSSProperties = { const nodeStyle: CSSProperties = {
zIndex: selected ? 10 : 3, zIndex: selected ? 10 : 3,
transform: `translate(${xPos}px,${yPos}px)`, transform: `translate(${xPos}px,${yPos}px)`,
+1
View File
@@ -42,6 +42,7 @@ function renderNode(
transform={transform} transform={transform}
selected={isSelected} selected={isSelected}
style={node.style} style={node.style}
className={node.className}
isInteractive={isInteractive} isInteractive={isInteractive}
sourcePosition={node.sourcePosition} sourcePosition={node.sourcePosition}
targetPosition={node.targetPosition} targetPosition={node.targetPosition}
+21 -22
View File
@@ -28,34 +28,33 @@ const useElementUpdater = (elements: Elements): void => {
? { ...existingNode.style, ...propNode.style } ? { ...existingNode.style, ...propNode.style }
: existingNode.style; : existingNode.style;
const className = existingNode.className === propNode.className ? existingNode.className : propNode.className;
const positionChanged = const positionChanged =
existingNode.position.x !== propNode.position.x || existingNode.position.y !== propNode.position.y; existingNode.position.x !== propNode.position.x || existingNode.position.y !== propNode.position.y;
if (positionChanged) { const nodeProps = {
return {
...existingNode,
__rg: {
...existingNode.__rg,
position: propNode.position,
},
position: propNode.position,
data,
style,
};
}
if (style) {
return {
...existingNode,
data,
style,
};
}
return {
...existingNode, ...existingNode,
data, data,
}; };
if (positionChanged) {
nodeProps.__rg = {
...existingNode.__rg,
position: propNode.position,
};
nodeProps.position = propNode.position;
}
if (typeof style !== 'undefined') {
nodeProps.style = style;
}
if (typeof className !== 'undefined') {
nodeProps.className = className;
}
return nodeProps;
} }
return parseElement(propNode) as Node; return parseElement(propNode) as Node;
+2
View File
@@ -37,6 +37,7 @@ export interface Node {
__rg?: any; __rg?: any;
data?: any; data?: any;
style?: CSSProperties; style?: CSSProperties;
className?: string;
targetPosition?: Position; targetPosition?: Position;
sourcePosition?: Position; sourcePosition?: Position;
} }
@@ -128,6 +129,7 @@ export interface WrapNodeProps {
onNodeDragStart?: (node: Node) => void; onNodeDragStart?: (node: Node) => void;
onNodeDragStop?: (node: Node) => void; onNodeDragStop?: (node: Node) => void;
style?: CSSProperties; style?: CSSProperties;
className?: string;
sourcePosition?: Position; sourcePosition?: Position;
targetPosition?: Position; targetPosition?: Position;
} }