From f096bfc04eae1d34a1629924cfcaf04af57d3674 Mon Sep 17 00:00:00 2001 From: olfedias Date: Tue, 13 Nov 2018 17:47:33 +0100 Subject: [PATCH 01/10] chore(Editor): fix editor --- .../ComponentExample/ComponentExample.tsx | 152 ++++++++---------- docs/src/index.ejs | 2 + package.json | 2 + yarn.lock | 27 +++- 4 files changed, 93 insertions(+), 90 deletions(-) diff --git a/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx b/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx index dbf0a29314..6a19b7cf49 100644 --- a/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx +++ b/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx @@ -2,14 +2,14 @@ import * as _ from 'lodash' import PropTypes from 'prop-types' import * as React from 'react' import { RouteComponentProps, withRouter } from 'react-router' -import { renderToStaticMarkup } from 'react-dom/server' import { html } from 'js-beautify' import * as copyToClipboard from 'copy-to-clipboard' +import SourceRender from 'react-source-render' import { Divider, Form, Grid, Menu, Segment, Visibility } from 'semantic-ui-react' import { Provider, themes } from '@stardust-ui/react' +import * as Stardust from '@stardust-ui/react' import { examplePathToHash, getFormattedHash, knobsContext, scrollToAnchor } from 'docs/src/utils' -import evalTypeScript from 'docs/src/utils/evalTypeScript' import { callable, doesNodeContainClick, pxToRem, constants } from 'src/lib' import Editor, { EDITOR_BACKGROUND_COLOR, EDITOR_GUTTER_COLOR } from 'docs/src/components/Editor' import ComponentControls from '../ComponentControls' @@ -31,7 +31,6 @@ interface ComponentExampleState { knobs: Object themeName: string componentVariables: Object - exampleElement?: JSX.Element handleMouseLeave?: () => void handleMouseMove?: () => void sourceCode: string @@ -56,6 +55,14 @@ const codeTypeApiButtonLabels: { [key in SourceCodeType]: string } = { shorthand: 'Shorthand API', } +const imports = { + lodash: _, + react: React, + '@stardust-ui/react': Stardust, +} + +const importResolver = importName => imports[importName] + /** * Renders a `component` and the raw `code` that produced it. * Allows toggling the the raw `code` code block. @@ -101,16 +108,13 @@ class ComponentExample extends React.Component { - this.renderSourceCode() - }) + this.setState({ showRtl: !showRtl }) } private handleShowCodeClick = e => { @@ -264,7 +266,6 @@ class ComponentExample extends React.Component { if (this.sourceCodeMgr.originalCodeHasChanged && confirm('Lose your changes?')) { this.sourceCodeMgr.resetToOriginalCode() - this.updateAndRenderSourceCode() } } @@ -278,15 +279,43 @@ class ComponentExample extends React.Component _.includes(knobsContext.keys(), this.getKnobsFilename()) - private setErrorDebounced = _.debounce(error => { - this.setState({ error }) - }, 800) + private renderExampleFromCode = (): JSX.Element => { + const { sourceCode } = this.state + + console.log('TEST', { + sourceCode, + }) - private renderExampleFromCode = (code: string): JSX.Element => { - const Example = code != null ? evalTypeScript(code) : this.renderMissingExample() - return _.isFunction(Example) ? this.renderWithProvider(Example) : Example + if (sourceCode == null) { + return this.renderMissingExample() + } + + const { showRtl, componentVariables, themeName } = this.state + const theme = themes[themeName] + + const newTheme: ThemeInput = { + componentVariables: mergeThemeVariables(theme.componentVariables, { + [this.getDisplayName()]: componentVariables, + }), + rtl: showRtl, + } + + return ( + + + + ) } + private onSuccess = (e, { markup }) => this.setState({ markup, error: null }) + private onError = error => this.setState({ error: error.toString() }) + private renderMissingExample = (): JSX.Element => { const missingExamplePath = `./docs/src/examples/${this.sourceCodeMgr.currentPath}.tsx` return ( @@ -297,40 +326,13 @@ class ComponentExample extends React.Component { - try { - const exampleElement = this.renderExampleFromCode(this.state.sourceCode) - - if (!React.isValidElement(exampleElement)) { - this.setErrorDebounced( - `Default export is not a valid React element. Check the example syntax.`, - ) - } else { - // immediately render a null error - // but also ensure the last debounced error call is a null error - const error = undefined - this.setErrorDebounced(error) - this.setState({ - error, - exampleElement, - markup: renderToStaticMarkup(exampleElement), - }) - } - } catch (err) { - this.setErrorDebounced(err.message) - } - }, 250) - private handleKnobChange = knobs => { - this.setState( - prevState => ({ - knobs: { - ...prevState.knobs, - ...knobs, - }, - }), - this.renderSourceCode, - ) + this.setState(prevState => ({ + knobs: { + ...prevState.knobs, + ...knobs, + }, + })) } private getKnobsComponent = () => { @@ -357,36 +359,12 @@ class ComponentExample extends React.Component this.props.examplePath.split('/')[1] - private renderWithProvider(ExampleComponent) { - const { showRtl, componentVariables, themeName } = this.state - const theme = themes[themeName] - - const newTheme: ThemeInput = { - componentVariables: mergeThemeVariables(theme.componentVariables, { - [this.getDisplayName()]: componentVariables, - }), - rtl: showRtl, - } - - return ( - - - - ) - } - private handleChangeCode = (sourceCode: string) => { - this.sourceCodeMgr.currentCode = sourceCode - this.updateAndRenderSourceCode() - } - - private updateAndRenderSourceCode = () => { - this.setState({ sourceCode: this.sourceCodeMgr.currentCode }, this.renderSourceCode) + this.setState({ sourceCode }) } private setApiCodeType = (codeType: SourceCodeType) => { - this.sourceCodeMgr.codeType = codeType - this.updateAndRenderSourceCode() + // TODO: } private renderApiCodeMenu = (): JSX.Element => { @@ -395,8 +373,8 @@ class ComponentExample extends React.Component { // we disable the menu button for Children API in case we don't have the example for it - const disabled = - codeType === SourceCodeType.normal && !this.sourceCodeMgr.isCodeValidForType(codeType) + const disabled = true + codeType === SourceCodeType.normal && !this.sourceCodeMgr.isCodeValidForType(codeType) return { active: this.sourceCodeMgr.codeType === codeType, @@ -596,15 +574,12 @@ class ComponentExample extends React.Component (e, { value }) => { - this.setState( - state => ({ - componentVariables: { - ...state.componentVariables, - [variable]: value, - }, - }), - this.renderSourceCode, - ) + this.setState(state => ({ + componentVariables: { + ...state.componentVariables, + [variable]: value, + }, + })) } public render() { @@ -612,7 +587,6 @@ class ComponentExample extends React.Component - {exampleElement} + {this.renderExampleFromCode()} ) }} diff --git a/docs/src/index.ejs b/docs/src/index.ejs index e5f209d11f..7e19b48165 100644 --- a/docs/src/index.ejs +++ b/docs/src/index.ejs @@ -33,6 +33,8 @@ + +