From 73008347ca364649aa9c6191c38b996c6dd1691f Mon Sep 17 00:00:00 2001 From: Shawn Date: Wed, 14 Nov 2018 22:17:18 -0600 Subject: [PATCH] Add dynamic elements --- .../Creatify/RichDraggable/index.js | 2 +- client/src/components/Creatify/index.js | 71 ++++++++++++++++--- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/client/src/components/Creatify/RichDraggable/index.js b/client/src/components/Creatify/RichDraggable/index.js index a4097085..8ec02ede 100644 --- a/client/src/components/Creatify/RichDraggable/index.js +++ b/client/src/components/Creatify/RichDraggable/index.js @@ -48,7 +48,7 @@ export default class RichDraggable extends Component { }; return ( - +
{props.children} diff --git a/client/src/components/Creatify/index.js b/client/src/components/Creatify/index.js index 35248408..5715ce23 100644 --- a/client/src/components/Creatify/index.js +++ b/client/src/components/Creatify/index.js @@ -104,8 +104,10 @@ export default class Creatify extends Component { ); this.state = { + activeElement: false, bounds: {}, fontName: fontKeys[0], + elements: [], fontOptions, }; } @@ -125,7 +127,58 @@ export default class Creatify extends Component { */ } - renderContents() { + setActiveElement(activeElement) { + this.setState({ activeElement }); + } + + addElement() { + const { + state + } = this; + + const newElementKey = `element-${state.elements.length}-${Date.now()}`; + + const newElement = ( + this.setActiveElement(newElement)}> + + + ); + + this.setState({ + elements: [...state.elements, newElement], + activeElement: newElement, + }); + } + + removeElement() { + const { + state + } = this; + + const activeElementIndex = state.elements.indexOf(state.activeElement); + + if(state.elements.length === 0 || activeElementIndex === -1) { + return; + } + + const elements = [...state.elements]; + elements.splice(activeElementIndex, 1) + + this.setState({ + activeElement: false, + elements, + }); + } + + async onSave() { + const renderedCanvas = await this.renderContentsToCanvas(); + + if(this.props.onSave) { + this.props.onSave(renderedCanvas); + } + } + + async renderContentsToCanvas() { const me = this; const contentsElement = me.contents.current; @@ -140,9 +193,7 @@ export default class Creatify extends Component { // Fix the dimensions, fixes when flex is used. contents = `
${contents}
`; - getRasterizedCanvas(contents, contentsWidth, contentsHeight).then((element) => { - document.body.appendChild(element); - }); + return await getRasterizedCanvas(contents, contentsWidth, contentsHeight); } render() { @@ -157,22 +208,20 @@ export default class Creatify extends Component { return (
- + this.addElement()} />
- + this.removeElement()} />