From cdb77255a7beb188132398576c25215fdbcd745d Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Tue, 29 Jan 2019 17:47:22 -0500 Subject: [PATCH] Cleaning up HorizontalLayout path calculations --- src/rendering/HorizontalLayout/index.js | 43 +++++++++++++------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/rendering/HorizontalLayout/index.js b/src/rendering/HorizontalLayout/index.js index 9336c22..3548f35 100644 --- a/src/rendering/HorizontalLayout/index.js +++ b/src/rendering/HorizontalLayout/index.js @@ -3,14 +3,17 @@ import PropTypes from 'prop-types'; import * as style from 'rendering/style'; -const HorizontalLayout = ({ connectorPath, transforms, children }) => { - return <> - - { React.Children.map(children, (child, i) => ( - { child } - )) } - ; -}; +const HorizontalLayout = ({ + withConnectors, + connectorPath, + transforms, + children +}) => <> + { withConnectors && } + { React.Children.map(children, (child, i) => ( + { child } + )) } +; HorizontalLayout.defaultProps = { withConnectors: false, @@ -29,35 +32,35 @@ HorizontalLayout.propTypes = { }; const generateOffsetBoxes = (boxes, axisY, spacing) => { - let offset = 0; + let x = 0; return boxes.map(box => { try { + const y = axisY - box.axisY; return { ...box, - offsetX: offset, - offsetY: axisY - box.axisY + x, y, + axisX1: x + box.axisX1, + axisX2: x + box.axisX2, + axisY: y + box.axisY }; } finally { - offset += box.width + spacing; + x += box.width + spacing; } }); }; const calculateChildTransforms = boxes => - boxes.map(box => `translate(${ box.offsetX } ${ box.offsetY })`); -const calculateConnectorPath = (boxes, axisY) => { + boxes.map(box => `translate(${ box.x } ${ box.y })`); +const calculateConnectorPath = boxes => { let last = boxes[0]; return boxes.slice(1).map(box => { try { - return ` - M${ last.offsetX + last.axisX2 },${ axisY } - H${ box.offsetX + box.axisX1 } - `; + return `M${ last.axisX2 },${ box.axisY } H${ box.axisX1 }`; } finally { last = box; } - }).join(''); + }).join('\n'); }; const layout = data => { const { withConnectors, spacing } = { @@ -79,7 +82,7 @@ const layout = data => { ...data.props, transforms: calculateChildTransforms(offsetBoxes), connectorPath: withConnectors - ? calculateConnectorPath(offsetBoxes, data.box.axisY) + ? calculateConnectorPath(offsetBoxes) : undefined };