From 44e6dae289df953610bc1413ef0043a5becdc7ff Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sat, 17 Feb 2018 16:28:46 -0500 Subject: [PATCH] Simplifying "pre-reflow" logic --- src/components/SVG/Base.js | 22 +++++----------------- src/components/SVG/Box.js | 8 ++------ src/components/SVG/HorizontalLayout.js | 4 ---- src/components/SVG/Image.js | 8 ++------ src/components/SVG/Loop.js | 8 ++------ src/components/SVG/VerticalLayout.js | 4 ---- 6 files changed, 11 insertions(+), 43 deletions(-) diff --git a/src/components/SVG/Base.js b/src/components/SVG/Base.js index ea01c58..7be6cc6 100644 --- a/src/components/SVG/Base.js +++ b/src/components/SVG/Base.js @@ -46,28 +46,20 @@ class Base extends React.PureComponent { }; } - async doPreReflow() { - const components = this.preReflow(); - + async reflowChildren() { // No child components - if (components === undefined) { + if (this.children === undefined) { return true; } - // List of child components - if (components.map) { - const componentsReflowed = await Promise.all(components.map(c => c.doReflow())); + const reflowed = await Promise.all(this.children.map(c => c.doReflow())); - return componentsReflowed.reduce((memo, value) => memo || value, false); - } - - // One child component - return components.doReflow(); + return reflowed.reduce((memo, value) => memo || value, false); } async doReflow() { const oldBBox = this._currentBBox(); - const shouldReflow = await this.doPreReflow(); + const shouldReflow = await this.reflowChildren(); if (shouldReflow) { this.reflow(); @@ -76,10 +68,6 @@ class Base extends React.PureComponent { return this._currentBBox() !== oldBBox; } - preReflow() { - // Implemented in subclass, return array of components to reflow before this - } - reflow() { // Implemented in subclasses } diff --git a/src/components/SVG/Box.js b/src/components/SVG/Box.js index dcf76c0..478f97d 100644 --- a/src/components/SVG/Box.js +++ b/src/components/SVG/Box.js @@ -11,13 +11,9 @@ class Box extends Base { radius: 3 } - preReflow() { - return this.contained; - } - reflow() { const { padding, useAnchors } = this.props; - const box = this.contained.getBBox(); + const box = this.children[0].getBBox(); const labelBox = this.label ? this.label.getBBox() : { width: 0, height: 0}; this.setBBox({ @@ -37,7 +33,7 @@ class Box extends Base { }); } - containedRef = contained => this.contained = contained + containedRef = contained => this.children = [contained] labelRef = label => this.label = label diff --git a/src/components/SVG/HorizontalLayout.js b/src/components/SVG/HorizontalLayout.js index 579ded6..688ce88 100644 --- a/src/components/SVG/HorizontalLayout.js +++ b/src/components/SVG/HorizontalLayout.js @@ -40,10 +40,6 @@ class HorizontalLayout extends Base { }, new Path()).toString(); } - preReflow() { - return this.children; - } - reflow() { const { spacing, withConnectors } = this.props; diff --git a/src/components/SVG/Image.js b/src/components/SVG/Image.js index b2c4e7c..0dd7344 100644 --- a/src/components/SVG/Image.js +++ b/src/components/SVG/Image.js @@ -30,13 +30,9 @@ class Image extends Base { height: 0 } - preReflow() { - return this.contained; - } - reflow() { const { padding } = this.props; - const box = this.contained.getBBox(); + const box = this.children[0].getBBox(); return this.setStateAsync({ width: Math.round(box.width + 2 * padding), @@ -44,7 +40,7 @@ class Image extends Base { }); } - containedRef = contained => this.contained = contained + containedRef = contained => this.children = [contained] svgRef = svg => this.svg = svg diff --git a/src/components/SVG/Loop.js b/src/components/SVG/Loop.js index d228402..7ffd882 100644 --- a/src/components/SVG/Loop.js +++ b/src/components/SVG/Loop.js @@ -68,13 +68,9 @@ class Loop extends Base { } } - preReflow() { - return this.contained; - } - reflow() { const { skip, repeat, greedy } = this.props; - const box = this.contained.getBBox(); + const box = this.children[0].getBBox(); const labelBox = this.label ? this.label.getBBox() : { width: 0, height: 0 }; let height = box.height + labelBox.height; @@ -105,7 +101,7 @@ class Loop extends Base { }); } - containedRef = contained => this.contained = contained + containedRef = contained => this.children = [contained] labelRef = label => this.label = label diff --git a/src/components/SVG/VerticalLayout.js b/src/components/SVG/VerticalLayout.js index d40174e..3b43892 100644 --- a/src/components/SVG/VerticalLayout.js +++ b/src/components/SVG/VerticalLayout.js @@ -78,10 +78,6 @@ class VerticalLayout extends Base { } } - preReflow() { - return this.children; - } - reflow() { const { spacing, withConnectors } = this.props;