Stubbing out parsing and starting on rendering flow

This commit is contained in:
Jeff Avallone 2019-01-26 16:36:43 -05:00
parent 3378c68aed
commit 21c392752e
6 changed files with 83 additions and 21 deletions

View File

@ -21,9 +21,9 @@ exports[`App removing rendered expression 1`] = `
> >
<LoadNamespace(FormActions) /> <LoadNamespace(FormActions) />
</LoadNamespace(Form)> </LoadNamespace(Form)>
<RenderJS <Render
expr="test expression"
onRender={[Function]} onRender={[Function]}
parsed="PARSED(test expression)"
/> />
</Fragment> </Fragment>
`; `;
@ -138,9 +138,9 @@ exports[`App rendering an expression 3`] = `
> >
<LoadNamespace(FormActions) /> <LoadNamespace(FormActions) />
</LoadNamespace(Form)> </LoadNamespace(Form)>
<RenderJS <Render
expr="test expression"
onRender={[Function]} onRender={[Function]}
parsed="PARSED(test expression)"
/> />
</Fragment> </Fragment>
`; `;
@ -166,9 +166,9 @@ exports[`App rendering image details 1`] = `
> >
<LoadNamespace(FormActions) /> <LoadNamespace(FormActions) />
</LoadNamespace(Form)> </LoadNamespace(Form)>
<RenderJS <Render
expr="test expression"
onRender={[Function]} onRender={[Function]}
parsed="PARSED(test expression)"
/> />
</Fragment> </Fragment>
`; `;
@ -200,9 +200,9 @@ exports[`App rendering image details 2`] = `
} }
/> />
</LoadNamespace(Form)> </LoadNamespace(Form)>
<RenderJS <Render
expr="test expression"
onRender={[Function]} onRender={[Function]}
parsed="PARSED(test expression)"
/> />
</Fragment> </Fragment>
`; `;

View File

@ -68,16 +68,19 @@ class App extends React.PureComponent {
}); });
try { try {
const Component = await import( const syntaxModule = await import(
/* webpackChunkName: "render-[index]" */ /* webpackChunkName: "render-[index]" */
`syntax/${ syntax }` `syntax/${ syntax }`
); );
const parsed = syntaxModule.parse(expr);
this.setState({ this.setState({
loading: false, loading: false,
render: { render: {
syntax, syntax,
Component: Component.default parsed,
Component: syntaxModule.Render
} }
}); });
} }
@ -116,6 +119,7 @@ class App extends React.PureComponent {
imageDetails, imageDetails,
render: { render: {
syntax: renderSyntax, syntax: renderSyntax,
parsed,
Component Component
} }
} = this.state; } = this.state;
@ -133,7 +137,7 @@ class App extends React.PureComponent {
}; };
const renderProps = { const renderProps = {
onRender: this.handleSvg, onRender: this.handleSvg,
expr parsed
}; };
const doRender = renderSyntax === syntax; const doRender = renderSyntax === syntax;

View File

@ -4,6 +4,11 @@ import { shallow } from 'enzyme';
import { mockT } from 'i18n'; import { mockT } from 'i18n';
import { App } from 'components/App'; import { App } from 'components/App';
jest.mock('syntax/js', () => ({
parse: expr => `PARSED(${ expr })`,
Render: () => ''
}));
const syntaxList = [ const syntaxList = [
{ id: 'testJS', label: 'Testing JS' }, { id: 'testJS', label: 'Testing JS' },
{ id: 'other', label: 'Other' } { id: 'other', label: 'Other' }

View File

@ -6,9 +6,29 @@ import Text from 'rendering/Text';
import style from './style.module.css'; 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 { class Render extends React.PureComponent {
static propTypes = { static propTypes = {
expr: PropTypes.string, parsed: PropTypes.object.isRequired,
onRender: PropTypes.func.isRequired onRender: PropTypes.func.isRequired
} }
@ -28,13 +48,10 @@ class Render extends React.PureComponent {
} }
render() { render() {
const { expr } = this.props; const { parsed } = this.props;
// Demo rendering for now
return <div className={ style.render } ref={ this.svgContainer }> return <div className={ style.render } ref={ this.svgContainer }>
<SVG onReflow={ this.provideSVGData }> { render(parsed, { onReflow: this.provideSVGData }) }
<Text>{ this.constructor.name } =&gt; { expr }</Text>
</SVG>
</div>; </div>;
} }
} }

View File

@ -1,5 +1,23 @@
import Render from 'components/Render'; 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
};

View File

@ -1,5 +1,23 @@
import Render from 'components/Render'; 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
};