|
| 1 | +import React from 'react' |
| 2 | +import { isCompound } from './graphUtil' |
| 3 | + |
| 4 | +function isOutputPort ({ id }) { |
| 5 | + return /.+_out$/.test(id) |
| 6 | +} |
| 7 | + |
| 8 | +function Port ({ port }) { |
| 9 | + return ( |
| 10 | + <rect |
| 11 | + x={port.x} |
| 12 | + y={port.y} |
| 13 | + width={port.width} |
| 14 | + height={port.height} |
| 15 | + stroke='#000' |
| 16 | + fill={isOutputPort(port) ? 'black' : 'white'} |
| 17 | + /> |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +function getPath (e, padding, startsAtParent = null) { |
| 22 | + let path |
| 23 | + if (startsAtParent) { |
| 24 | + path = 'M ' + e.sourcePoint.x + ' ' + e.sourcePoint.y + ' ' |
| 25 | + } else { |
| 26 | + path = 'M ' + (e.sourcePoint.x + padding.left) + ' ' + (e.sourcePoint.y + padding.top) + ' ' |
| 27 | + } |
| 28 | + var bendPoints = e.bendPoints || [] |
| 29 | + bendPoints.forEach((bp, i) => { |
| 30 | + path += 'L ' + (bp.x + padding.left) + ' ' + (bp.y + padding.top) + ' ' |
| 31 | + }) |
| 32 | + path += 'L ' + (e.targetPoint.x + padding.left) + ' ' + (e.targetPoint.y + padding.top - 10) + ' ' |
| 33 | + return path |
| 34 | +} |
| 35 | + |
| 36 | +function Edge ({ parentNode, edge, padding = { top: 0, left: 0 }, ...other }) { |
| 37 | + let endPoint = edge.targetPoint |
| 38 | + let previousPoint = edge.bendPoints != null && edge.bendPoints.length > 0 ? edge.bendPoints[edge.bendPoints.length - 1] : edge.sourcePoint |
| 39 | + const angle = Math.atan2(endPoint.y - previousPoint.y, endPoint.x - previousPoint.x) * 180 / Math.PI |
| 40 | + const color = (edge.meta && edge.meta.style && edge.meta.style.color) || '#333333' |
| 41 | + |
| 42 | + return ( |
| 43 | + <g> |
| 44 | + <path |
| 45 | + strokeWidth={3} |
| 46 | + opacity={0.8} |
| 47 | + fill='none' |
| 48 | + stroke={color} |
| 49 | + d={getPath(edge, padding, edge.source === parentNode.id)} |
| 50 | + {...other} |
| 51 | + /> |
| 52 | + <path |
| 53 | + strokeWidth={3} |
| 54 | + opacity={0.8} |
| 55 | + fill={color} |
| 56 | + d='M0,0 L0,3 L3,1.5 L0,0' |
| 57 | + transform={`translate(${edge.targetPoint.x + padding.left + 4.5} ${edge.targetPoint.y + padding.top - 10}) rotate(${angle}) scale(3)`} |
| 58 | + /> |
| 59 | + </g> |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +function Node ({ graph, node, ...other }) { |
| 64 | + const transform = `translate(${(node.x || 0) + (node.padding ? node.padding.left : 0)} ${(node.y || 0) + (node.padding ? node.padding.top : 0)})` |
| 65 | + |
| 66 | + if (isCompound(node, graph)) { |
| 67 | + return ( |
| 68 | + <g |
| 69 | + transform={transform} |
| 70 | + > |
| 71 | + <rect |
| 72 | + {...other} |
| 73 | + strokeWidth={1} |
| 74 | + width={node.width} |
| 75 | + height={node.height} |
| 76 | + strokeOpacity={0.5} |
| 77 | + strokeDasharray='10 5' |
| 78 | + fillOpacity={0} |
| 79 | + transform='translate(0.5 0.5)' |
| 80 | + stroke={(node.meta && node.meta.style && node.meta.style.color) || '#000'} |
| 81 | + /> |
| 82 | + <text |
| 83 | + x={5} |
| 84 | + y={5} |
| 85 | + alignmentBaseline='hanging' |
| 86 | + fontFamily='sans-serif' |
| 87 | + fontSize={14} |
| 88 | + fill={(node.meta && node.meta.style && node.meta.style.color) || null} |
| 89 | + opacity={0.5} |
| 90 | + > |
| 91 | + {node.text} |
| 92 | + </text> |
| 93 | + {Array.isArray(node.edges) && node.edges.map((edge) => ( |
| 94 | + <Edge |
| 95 | + key={`e-${edge.source}-${edge.target}`} |
| 96 | + graph={graph} |
| 97 | + parentNode={node} |
| 98 | + edge={edge} |
| 99 | + padding={node.padding} |
| 100 | + /> |
| 101 | + ))} |
| 102 | + {Array.isArray(node.ports) && node.ports.map((port) => ( |
| 103 | + <Port |
| 104 | + key={`p-${port.id}`} |
| 105 | + graph={graph} |
| 106 | + port={port} |
| 107 | + /> |
| 108 | + ))} |
| 109 | + {Array.isArray(node.children) && node.children.map((child) => ( |
| 110 | + <Node |
| 111 | + key={`c-${child.id}`} |
| 112 | + graph={graph} |
| 113 | + node={child} |
| 114 | + /> |
| 115 | + ))} |
| 116 | + </g> |
| 117 | + ) |
| 118 | + } else { |
| 119 | + return ( |
| 120 | + <g |
| 121 | + transform={transform} |
| 122 | + > |
| 123 | + <rect |
| 124 | + {...other} |
| 125 | + strokeWidth={3} |
| 126 | + width={node.width} |
| 127 | + height={node.height} |
| 128 | + stroke={(node.meta && node.meta.style && node.meta.style.color) || '#000'} |
| 129 | + fill='#fff' |
| 130 | + /> |
| 131 | + <text |
| 132 | + x={node.width / 2} |
| 133 | + y={node.height / 2} |
| 134 | + textAnchor='middle' |
| 135 | + alignmentBaseline='central' |
| 136 | + fontFamily='sans-serif' |
| 137 | + fontSize={14} |
| 138 | + fill={(node.meta && node.meta.style && node.meta.style.color) || null} |
| 139 | + > |
| 140 | + {node.text} |
| 141 | + </text> |
| 142 | + {Array.isArray(node.ports) && node.ports.map((port) => ( |
| 143 | + <Port |
| 144 | + key={`p-${port.id}`} |
| 145 | + graph={graph} |
| 146 | + port={port} |
| 147 | + /> |
| 148 | + ))} |
| 149 | + </g> |
| 150 | + ) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +function RootNode ({ graph, node }) { |
| 155 | + return ( |
| 156 | + <g transform={`translate(${-node.padding.left} ${-node.padding.top})`}> |
| 157 | + {Array.isArray(node.edges) && node.edges.map((edge) => ( |
| 158 | + <Edge |
| 159 | + key={`e-${edge.source}-${edge.target}`} |
| 160 | + graph={graph} |
| 161 | + parentNode={node} |
| 162 | + edge={edge} |
| 163 | + /> |
| 164 | + ))} |
| 165 | + {Array.isArray(node.ports) && node.ports.map((port) => ( |
| 166 | + <Port |
| 167 | + key={`p-${port.id}`} |
| 168 | + graph={graph} |
| 169 | + port={port} |
| 170 | + /> |
| 171 | + ))} |
| 172 | + {Array.isArray(node.children) && node.children.map((child) => ( |
| 173 | + <Node |
| 174 | + key={`c-${child.id}`} |
| 175 | + graph={graph} |
| 176 | + node={child} |
| 177 | + /> |
| 178 | + ))} |
| 179 | + </g> |
| 180 | + ) |
| 181 | +} |
| 182 | + |
| 183 | +export default function Graph ({ graph }) { |
| 184 | + return ( |
| 185 | + <svg |
| 186 | + width={graph.width - graph.padding.left - graph.padding.right} |
| 187 | + height={graph.height - graph.padding.top - graph.padding.bottom} |
| 188 | + > |
| 189 | + <RootNode |
| 190 | + graph={graph} |
| 191 | + node={graph} |
| 192 | + /> |
| 193 | + </svg> |
| 194 | + ) |
| 195 | +} |
0 commit comments