From 21c392752e0670518910ff597c36746062ef5e16 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sat, 26 Jan 2019 16:36:43 -0500 Subject: [PATCH] Stubbing out parsing and starting on rendering flow --- src/components/App/__snapshots__/test.js.snap | 16 +++++----- src/components/App/index.js | 10 +++++-- src/components/App/test.js | 5 ++++ src/components/Render/index.js | 29 +++++++++++++++---- src/syntax/js.js | 22 ++++++++++++-- src/syntax/pcre.js | 22 ++++++++++++-- 6 files changed, 83 insertions(+), 21 deletions(-) diff --git a/src/components/App/__snapshots__/test.js.snap b/src/components/App/__snapshots__/test.js.snap index cf2c86c..c84d246 100644 --- a/src/components/App/__snapshots__/test.js.snap +++ b/src/components/App/__snapshots__/test.js.snap @@ -21,9 +21,9 @@ exports[`App removing rendered expression 1`] = ` > - `; @@ -138,9 +138,9 @@ exports[`App rendering an expression 3`] = ` > - `; @@ -166,9 +166,9 @@ exports[`App rendering image details 1`] = ` > - `; @@ -200,9 +200,9 @@ exports[`App rendering image details 2`] = ` } /> - `; diff --git a/src/components/App/index.js b/src/components/App/index.js index d7a951b..df11c78 100644 --- a/src/components/App/index.js +++ b/src/components/App/index.js @@ -68,16 +68,19 @@ class App extends React.PureComponent { }); try { - const Component = await import( + const syntaxModule = await import( /* webpackChunkName: "render-[index]" */ `syntax/${ syntax }` ); + const parsed = syntaxModule.parse(expr); + this.setState({ loading: false, render: { syntax, - Component: Component.default + parsed, + Component: syntaxModule.Render } }); } @@ -116,6 +119,7 @@ class App extends React.PureComponent { imageDetails, render: { syntax: renderSyntax, + parsed, Component } } = this.state; @@ -133,7 +137,7 @@ class App extends React.PureComponent { }; const renderProps = { onRender: this.handleSvg, - expr + parsed }; const doRender = renderSyntax === syntax; diff --git a/src/components/App/test.js b/src/components/App/test.js index 16667d6..3d66b22 100644 --- a/src/components/App/test.js +++ b/src/components/App/test.js @@ -4,6 +4,11 @@ import { shallow } from 'enzyme'; import { mockT } from 'i18n'; import { App } from 'components/App'; +jest.mock('syntax/js', () => ({ + parse: expr => `PARSED(${ expr })`, + Render: () => '' +})); + const syntaxList = [ { id: 'testJS', label: 'Testing JS' }, { id: 'other', label: 'Other' } diff --git a/src/components/Render/index.js b/src/components/Render/index.js index 291faad..1e1c790 100644 --- a/src/components/Render/index.js +++ b/src/components/Render/index.js @@ -6,9 +6,29 @@ import Text from 'rendering/Text'; import style from './style.module.css'; +const nodeTypes = { + SVG, + Text +}; + +const render = (data, extraProps) => { + if (typeof data === 'string') { + return data; + } + + const { type, props } = data; + const children = (data.children || []).map( + (data, key) => render(data, { key })); + + return React.createElement( + nodeTypes[type] || type, + { ...extraProps, ...props }, + children.length === 1 ? children[0] : children); +}; + class Render extends React.PureComponent { static propTypes = { - expr: PropTypes.string, + parsed: PropTypes.object.isRequired, onRender: PropTypes.func.isRequired } @@ -28,13 +48,10 @@ class Render extends React.PureComponent { } render() { - const { expr } = this.props; + const { parsed } = this.props; - // Demo rendering for now return
- - { this.constructor.name } => { expr } - + { render(parsed, { onReflow: this.provideSVGData }) }
; } } diff --git a/src/syntax/js.js b/src/syntax/js.js index aefec9a..7214bd3 100644 --- a/src/syntax/js.js +++ b/src/syntax/js.js @@ -1,5 +1,23 @@ import Render from 'components/Render'; -class RenderJS extends Render {} +const parse = expr => { + return { + type: 'SVG', + children: [ + { + type: 'Text', + props: { + quoted: true + }, + children: [ + `JS => ${ expr }` + ] + } + ] + }; +}; -export default RenderJS; +export { + parse, + Render +}; diff --git a/src/syntax/pcre.js b/src/syntax/pcre.js index 11c32d9..a078762 100644 --- a/src/syntax/pcre.js +++ b/src/syntax/pcre.js @@ -1,5 +1,23 @@ import Render from 'components/Render'; -class RenderPCRE extends Render {} +const parse = expr => { + return { + type: 'SVG', + children: [ + { + type: 'Text', + props: { + quoted: true + }, + children: [ + `PCRE => ${ expr }` + ] + } + ] + }; +}; -export default RenderPCRE; +export { + parse, + Render +};