From 48a93aa24f53d3e1b28e94899935139435540698 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 21 Oct 2019 15:43:35 +0200 Subject: [PATCH] JS editor fixes (#1752) --- packages/app-js/src/ActionButtons.tsx | 16 ++--- packages/app-js/src/Playground.tsx | 96 ++++++++++++++++----------- 2 files changed, 60 insertions(+), 52 deletions(-) diff --git a/packages/app-js/src/ActionButtons.tsx b/packages/app-js/src/ActionButtons.tsx index eba865510d..a514f409db 100644 --- a/packages/app-js/src/ActionButtons.tsx +++ b/packages/app-js/src/ActionButtons.tsx @@ -13,6 +13,7 @@ import translate from './translate'; interface Props extends BareProps, I18nProps { isCustomExample: boolean; + isRunning: boolean; generateLink: () => void; removeSnippet: () => void; runJs: () => void; @@ -21,8 +22,7 @@ interface Props extends BareProps, I18nProps { stopJs: () => void; } -function ActionButtons ({ className, generateLink, isCustomExample, removeSnippet, runJs, saveSnippet, stopJs, t }: Props): React.ReactElement { - const [isRunning, setIsRunning] = useState(false); +function ActionButtons ({ className, generateLink, isCustomExample, isRunning, removeSnippet, runJs, saveSnippet, stopJs, t }: Props): React.ReactElement { const [isOpen, setIsOpen] = useState(false); const [shareText, setShareText] = useState(t('Generate link to share code example')); const [snippetName, setSnippetName] = useState(''); @@ -48,14 +48,6 @@ function ActionButtons ({ className, generateLink, isCustomExample, removeSnippe saveSnippet(snippetName); _onPopupClose(); }; - const _runJs = (): void => { - setIsRunning(true); - runJs(); - }; - const _stopJs = (): void => { - stopJs(); - setIsRunning(false); - }; return (
@@ -129,7 +121,7 @@ function ActionButtons ({ className, generateLink, isCustomExample, removeSnippe icon='close' isCircular isNegative - onClick={_stopJs} + onClick={stopJs} /> ) : ( @@ -138,7 +130,7 @@ function ActionButtons ({ className, generateLink, isCustomExample, removeSnippe icon='play' isCircular isPrimary - onClick={_runJs} + onClick={runJs} /> ) } diff --git a/packages/app-js/src/Playground.tsx b/packages/app-js/src/Playground.tsx index 72e60c2610..d525529ad2 100644 --- a/packages/app-js/src/Playground.tsx +++ b/packages/app-js/src/Playground.tsx @@ -55,6 +55,7 @@ interface Props extends AppProps, I18nProps, RouteComponentProps<{}> { } const snippets: Snippet[] = JSON.parse(JSON.stringify(allSnippets)); +let hasSnippetWrappers = false; function decodeBase64 (base64: string): Snippet { const sharedExample: Snippet = { @@ -91,14 +92,53 @@ function decodeBase64 (base64: string): Snippet { function Playground ({ className, history, match: { params: { base64 } }, t }: Props): React.ReactElement { const { api, isDevelopment } = useContext(ApiContext); const injectedRef = useRef(null); + const [code, setCode] = useState(''); const [isAnimated, setIsAnimated] = useState(true); const [isCustomExample, setIsCustomExample] = useState(false); + const [isRunning, setIsRunning] = useState(false); const [customExamples, setCustomExamples] = useState([]); const [logs, setLogs] = useState([]); const [options, setOptions] = useState([]); const [selected, setSelected] = useState(snippets[0]); const [sharedExample, setSharedExample] = useState(); + // add snippet wrappers + useEffect((): void => { + if (!hasSnippetWrappers) { + snippets.forEach((snippet): void => { + snippet.code = `${makeWrapper(isDevelopment)}${snippet.code}`; + }); + + hasSnippetWrappers = true; + } + }, []); + + // initialize all options + useEffect((): void => { + const sharedExample = base64 ? decodeBase64(base64) : undefined; + const localData = { + examples: localStorage.getItem(STORE_EXAMPLES), + selectedValue: localStorage.getItem(STORE_SELECTED) + }; + const customExamples = localData.examples ? JSON.parse(localData.examples) : []; + + const options: Snippet[] = sharedExample + ? [sharedExample, ...customExamples, ...snippets] + : [...customExamples, ...snippets]; + + const selected = options.find((option): boolean => option.value === localData.selectedValue); + + setCustomExamples(customExamples); + setIsCustomExample((selected && selected.type === 'custom') || false); + setOptions(options); + setSelected(sharedExample || selected || snippets[0]); + setSharedExample(sharedExample); + }, []); + + useEffect((): void => { + setCode(selected.code); + }, [selected]); + const _clearConsole = (): void => setLogs([]); const _hookConsole = (type: LogType, args: any[]): void => { logs.push({ args, type }); @@ -135,8 +175,11 @@ function Playground ({ className, history, match: { params: { base64 } }, t }: P injectedRef.current.api.disconnect(); injectedRef.current = null; } + + setIsRunning(false); }; const _runJs = async (): Promise => { + setIsRunning(true); _stopJs(); _clearConsole(); @@ -158,27 +201,30 @@ function Playground ({ className, history, match: { params: { base64 } }, t }: P await injectedRef.current.api.isReady; - // squash into a single line so exceptions (with linenumbers) maps to the same line/origin + // squash into a single line so exceptions (with line numbers) maps to the same line/origin // as we have in the editor view (TODO: Make the console.error here actually return the full stack) - const exec = `(async ({${Object.keys(injectedRef.current).join(',')}}) => { try { ${selected.code} \n } catch (error) { console.error(error); } })(injected);`; + const exec = `(async ({${Object.keys(injectedRef.current).join(',')}}) => { try { ${code} \n } catch (error) { console.error(error); } })(injected);`; // eslint-disable-next-line no-new-func new Function('injected', exec)(injectedRef.current); }; const _selectExample = (value: string): void => { + _stopJs(); + if (value.length) { const option = options.find((option): boolean => option.value === value); if (option) { localStorage.setItem(STORE_SELECTED, value); - setLogs([]); + + _clearConsole(); setIsCustomExample(option.type === 'custom'); setSelected(option); } } }; const _generateLink = (): void => { - const u8a = util.stringToU8a(selected.code); + const u8a = util.stringToU8a(code); const compU8a = snappy.compress(u8a); const compStr = compU8a.reduce((str: string, ch: number): string => { return str + String.fromCharCode(ch); @@ -197,12 +243,6 @@ function Playground ({ className, history, match: { params: { base64 } }, t }: P _copyToClipboard(`${basePath}/#${path}`); }; - const _onEdit = (code: string): void => { - if (code !== selected.code) { - setIsCustomExample(false); - setSelected({ ...selected, code }); - } - }; const _removeSnippet = (): void => { const filtered = customExamples.filter((value): boolean => value.value !== selected.value); const nextOptions = [...filtered, ...snippets]; @@ -218,7 +258,7 @@ function Playground ({ className, history, match: { params: { base64 } }, t }: P // The component doesn't take boolean custom props and no // camelCase keys, that's why 'custom' is passed as a string here const snapshot: Snippet = { - code: selected.code, + code, label: CUSTOM_LABEL, text: snippetName, type: 'custom', @@ -240,31 +280,6 @@ function Playground ({ className, history, match: { params: { base64 } }, t }: P setSharedExample(selected.type === 'shared' ? undefined : sharedExample); }; - useEffect((): void => { - snippets.forEach((snippet): void => { - snippet.code = `${makeWrapper(isDevelopment)}${snippet.code}`; - }); - - const sharedExample = base64 ? decodeBase64(base64) : undefined; - const localData = { - examples: localStorage.getItem(STORE_EXAMPLES), - selectedValue: localStorage.getItem(STORE_SELECTED) - }; - const customExamples = localData.examples ? JSON.parse(localData.examples) : []; - - const options: Snippet[] = sharedExample - ? [sharedExample, ...customExamples, ...snippets] - : [...customExamples, ...snippets]; - - const selected = options.find((option): boolean => option.value === localData.selectedValue); - - setCustomExamples(customExamples); - setIsCustomExample((selected && selected.type === 'custom') || false); - setOptions(options); - setSelected(sharedExample || selected || snippets[0]); - setSharedExample(sharedExample); - }, []); - const snippetName = selected.type === 'custom' ? selected.text : undefined; return ( @@ -272,10 +287,10 @@ function Playground ({ className, history, match: { params: { base64 } }, t }: P
@@ -288,6 +303,7 @@ function Playground ({ className, history, match: { params: { base64 } }, t }: P