From 633ecd2399409515221a1486976ddb7964990a1c Mon Sep 17 00:00:00 2001 From: DD Date: Wed, 30 Aug 2017 10:50:05 -0400 Subject: [PATCH] load an svg string --- src/components/paint-editor.jsx | 10 ++++++++- src/containers/paint-editor.jsx | 27 +++++++++++++++++++++-- src/containers/paper-canvas.jsx | 39 +++++++++++++++++++++------------ src/playground/playground.jsx | 9 +++++++- 4 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/components/paint-editor.jsx b/src/components/paint-editor.jsx index 1459fe35..faef8b9b 100644 --- a/src/components/paint-editor.jsx +++ b/src/components/paint-editor.jsx @@ -3,6 +3,7 @@ import React from 'react'; import PaperCanvas from '../containers/paper-canvas.jsx'; import BrushMode from '../containers/brush-mode.jsx'; import EraserMode from '../containers/eraser-mode.jsx'; +import PropTypes from 'prop-types'; class PaintEditorComponent extends React.Component { constructor (props) { @@ -20,7 +21,10 @@ class PaintEditorComponent extends React.Component { if (this.state.canvas) { return (
- +
@@ -34,4 +38,8 @@ class PaintEditorComponent extends React.Component { } } +PaintEditorComponent.propTypes = { + svg: PropTypes.string +}; + export default PaintEditorComponent; diff --git a/src/containers/paint-editor.jsx b/src/containers/paint-editor.jsx index 303546ba..219e3fe9 100644 --- a/src/containers/paint-editor.jsx +++ b/src/containers/paint-editor.jsx @@ -4,23 +4,46 @@ import PaintEditorComponent from '../components/paint-editor.jsx'; import {changeMode} from '../reducers/modes'; import Modes from '../modes/modes'; import {connect} from 'react-redux'; +import bindAll from 'lodash.bindall'; class PaintEditor extends React.Component { + constructor (props) { + super(props); + bindAll(this, [ + 'updateSvg' + ]); + } componentDidMount () { document.addEventListener('keydown', this.props.onKeyPress); } + shouldComponentUpdate (newProps) { + return newProps.assetId !== this.props.assetId; + } componentWillUnmount () { document.removeEventListener('keydown', this.props.onKeyPress); } + updateSvg (svg) { + if (!this.props.onUpdate) { + return; + } + this.props.onUpdate( + this.props.assetIndex, + svg + ); + } render () { return ( - + ); } } PaintEditor.propTypes = { - onKeyPress: PropTypes.func.isRequired + assetId: PropTypes.string, + assetIndex: PropTypes.number, + onKeyPress: PropTypes.func.isRequired, + onUpdate: PropTypes.func, + svg: PropTypes.string }; const mapDispatchToProps = dispatch => ({ diff --git a/src/containers/paper-canvas.jsx b/src/containers/paper-canvas.jsx index 4ccb0b58..f44bc10d 100644 --- a/src/containers/paper-canvas.jsx +++ b/src/containers/paper-canvas.jsx @@ -7,27 +7,37 @@ class PaperCanvas extends React.Component { constructor (props) { super(props); bindAll(this, [ - 'setCanvas' + 'setCanvas', + 'importSvg' ]); } componentDidMount () { paper.setup(this.canvas); - // Create a Paper.js Path to draw a line into it: - const path = new paper.Path(); - // Give the stroke a color - path.strokeColor = 'black'; - const start = new paper.Point(100, 100); - // Move to start and draw a line from there - path.moveTo(start); - // Note that the plus operator on Point objects does not work - // in JavaScript. Instead, we need to call the add() function: - path.lineTo(start.add([200, -50])); - // Draw the view now: - paper.view.draw(); + if (this.props.svg) { + this.importSvg(this.props.svg); + } + } + componentWillReceiveProps (newProps) { + if (newProps.svg !== this.props.svg) { + paper.project.activeLayer.removeChildren(); + this.importSvg(newProps.svg); + } } componentWillUnmount () { paper.remove(); } + importSvg (svg) { + paper.project.importSVG(svg, + { + expandShapes: true, + onLoad: function (item) { + while (item.reduce() !== item) { + item = item.reduce(); + } + } + }); + paper.project.view.update(); + } setCanvas (canvas) { this.canvas = canvas; if (this.props.canvasRef) { @@ -44,7 +54,8 @@ class PaperCanvas extends React.Component { } PaperCanvas.propTypes = { - canvasRef: PropTypes.func + canvasRef: PropTypes.func, + svg: PropTypes.string }; export default PaperCanvas; diff --git a/src/playground/playground.jsx b/src/playground/playground.jsx index 9f68704a..a3f2a15f 100644 --- a/src/playground/playground.jsx +++ b/src/playground/playground.jsx @@ -13,10 +13,17 @@ const store = createStore( intlInitialState, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() ); +const svgString = + '' + + '' + + '' + + '' + + '' + + ''; ReactDOM.render(( - + ), appTarget);