From dd35871076dfe4444de37cc19a09c3479b22192c Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Fri, 9 Nov 2018 13:01:58 -0500 Subject: [PATCH] Revert "Wait for other callbacks before load SVG code (#755)" This reverts commit 2d277581b161319f4bffb901d4ee0c3fc55499ef. --- src/containers/paper-canvas.jsx | 105 +++++++++++++++----------------- 1 file changed, 49 insertions(+), 56 deletions(-) diff --git a/src/containers/paper-canvas.jsx b/src/containers/paper-canvas.jsx index 35ae89cf..05f9d99a 100644 --- a/src/containers/paper-canvas.jsx +++ b/src/containers/paper-canvas.jsx @@ -27,7 +27,6 @@ class PaperCanvas extends React.Component { bindAll(this, [ 'setCanvas', 'importSvg', - 'initializeSvg', 'maybeZoomToFit', 'switchCostume' ]); @@ -172,65 +171,59 @@ class PaperCanvas extends React.Component { performSnapshot(paperCanvas.props.undoSnapshot, Formats.VECTOR_SKIP_CONVERT); return; } - item.remove(); + const itemWidth = item.bounds.width; + const itemHeight = item.bounds.height; - // Without the callback, rasters' load function has not been called yet, and they are - // positioned incorrectly - window.setTimeout(() => { - paperCanvas.initializeSvg(item, rotationCenterX, rotationCenterY, viewBox); - }, 0); + // Remove viewbox + if (item.clipped) { + let mask; + for (const child of item.children) { + if (child.isClipMask()) { + mask = child; + break; + } + } + item.clipped = false; + mask.remove(); + } + + // Reduce single item nested in groups + if (item instanceof paper.Group && item.children.length === 1) { + item = item.reduce(); + } + + ensureClockwise(item); + scaleWithStrokes(item, 2, new paper.Point()); // Import at 2x + + if (typeof rotationCenterX !== 'undefined' && typeof rotationCenterY !== 'undefined') { + let rotationPoint = new paper.Point(rotationCenterX, rotationCenterY); + if (viewBox && viewBox.length >= 2 && !isNaN(viewBox[0]) && !isNaN(viewBox[1])) { + rotationPoint = rotationPoint.subtract(viewBox[0], viewBox[1]); + } + item.translate(new paper.Point(ART_BOARD_WIDTH / 2, ART_BOARD_HEIGHT / 2) + .subtract(rotationPoint.multiply(2))); + } else { + // Center + item.translate(new paper.Point(ART_BOARD_WIDTH / 2, ART_BOARD_HEIGHT / 2) + .subtract(itemWidth, itemHeight)); + } + if (isGroup(item)) { + // Fixes an issue where we may export empty groups + for (const child of item.children) { + if (isGroup(child) && child.children.length === 0) { + child.remove(); + } + } + ungroupItems([item]); + } + + // Without the callback, the transforms sometimes don't finish applying before the + // snapshot is taken. + window.setTimeout( + () => performSnapshot(paperCanvas.props.undoSnapshot, Formats.VECTOR_SKIP_CONVERT), 0); } }); } - initializeSvg (item, rotationCenterX, rotationCenterY, viewBox) { - const itemWidth = item.bounds.width; - const itemHeight = item.bounds.height; - - // Remove viewbox - if (item.clipped) { - let mask; - for (const child of item.children) { - if (child.isClipMask()) { - mask = child; - break; - } - } - item.clipped = false; - mask.remove(); - } - - // Reduce single item nested in groups - if (item instanceof paper.Group && item.children.length === 1) { - item = item.reduce(); - } - - ensureClockwise(item); - scaleWithStrokes(item, 2, new paper.Point()); // Import at 2x - - if (typeof rotationCenterX !== 'undefined' && typeof rotationCenterY !== 'undefined') { - let rotationPoint = new paper.Point(rotationCenterX, rotationCenterY); - if (viewBox && viewBox.length >= 2 && !isNaN(viewBox[0]) && !isNaN(viewBox[1])) { - rotationPoint = rotationPoint.subtract(viewBox[0], viewBox[1]); - } - item.translate(new paper.Point(ART_BOARD_WIDTH / 2, ART_BOARD_HEIGHT / 2) - .subtract(rotationPoint.multiply(2))); - } else { - // Center - item.translate(new paper.Point(ART_BOARD_WIDTH / 2, ART_BOARD_HEIGHT / 2) - .subtract(itemWidth, itemHeight)); - } - paper.project.activeLayer.insertChild(0, item); - if (isGroup(item)) { - // Fixes an issue where we may export empty groups - for (const child of item.children) { - if (isGroup(child) && child.children.length === 0) { - child.remove(); - } - } - ungroupItems([item]); - } - performSnapshot(this.props.undoSnapshot, Formats.VECTOR_SKIP_CONVERT); - } setCanvas (canvas) { this.canvas = canvas; if (this.props.canvasRef) {