Skip to content

Commit

Permalink
feat(playground): load prettier before playground (#340)
Browse files Browse the repository at this point in the history
* Load prettier before loading playground

* Remove old prettier handling code

* Tweak layout

* Error handling during playground load

* Lint fix
  • Loading branch information
benjie committed Jul 16, 2020
1 parent 2fb2c5d commit 71f27e1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 73 deletions.
74 changes: 63 additions & 11 deletions packages/@remirror/playground/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const Playground: FC = () => {
const [Component, setPlayground] = useState<FC | null>(null);

const [hasBabel, setHasBabel] = useState(false);
const [hasPrettier, setHasPrettier] = useState(false);
const [error, setError] = useState<Error | null>(null);

useEffect(() => {
if (hasBabel) {
Expand All @@ -23,20 +25,47 @@ export const Playground: FC = () => {
}, [hasBabel]);

useEffect(() => {
if (!hasBabel) {
if (hasPrettier) {
return;
}

import('./playground').then((playground) => {
// This has to be a function, otherwise React breaks (not unexpectedly)
setPlayground(() => playground.Playground);
});
}, [hasBabel]);
const checkForBabel = () => {
if (typeof window['prettier'] !== 'undefined') {
setHasPrettier(true);
}
};
const int = setInterval(checkForBabel, 100);

return Component ? <Component /> : <Loading hasBabel={hasBabel} />;
return () => {
clearInterval(int);
};
}, [hasPrettier]);

useEffect(() => {
if (!hasBabel || !hasPrettier) {
return;
}

import('./playground')
.then((playground) => {
// This has to be a function, otherwise React breaks (not unexpectedly)
setPlayground(() => playground.Playground);
})
.catch((error_) => setError(error_));
}, [hasBabel, hasPrettier]);

return Component ? (
<Component />
) : (
<Loading hasBabel={hasBabel} hasPrettier={hasPrettier} error={error} />
);
};

const Loading: FC<{ hasBabel: boolean }> = ({ hasBabel }) => {
const Loading: FC<{ hasBabel: boolean; hasPrettier: boolean; error: Error | null }> = ({
error,
hasBabel,
hasPrettier,
}) => {
const [numberOfDots, setNumberOfDots] = useState(3);
useEffect(() => {
const dotsPlusPlus = () => {
Expand Down Expand Up @@ -85,9 +114,32 @@ const Loading: FC<{ hasBabel: boolean }> = ({ hasBabel }) => {
alt='animated remirror logo'
/>
</div>
<div style={{ flex: '1 0 16rem', padding: '0 0 0 2rem', textAlign: 'center' }}>
Loading {!hasBabel ? 'babel' : 'monaco'}
{dots}
<div
style={{
flex: '1 0 16rem',
padding: '0 0 1rem 2rem',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
}}
>
{error ? (
<div>
An error occurred, please refresh the page and try again. Details: {error.message}
</div>
) : (
<>
<div style={{ textAlign: 'center' }}>Loading{dots}</div>
<div>
Babel: {!hasBabel ? '🏃' : '👍'}
<br />
Prettier: {!hasPrettier ? '🏃' : '👍'}
<br />
Playground: {!hasBabel || !hasPrettier ? '✋' : '🏃'}
</div>
</>
)}
</div>
</div>
);
Expand Down
62 changes: 0 additions & 62 deletions packages/@remirror/playground/src/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ function cleanse(moduleName: string, moduleExports: Exports): Exports {
return cleansedExports;
}

const PRETTIER_SCRIPTS = [
'https://unpkg.com/prettier@2.0.5/standalone.js',
'https://unpkg.com/prettier@2.0.5/parser-typescript.js',
];

export const Playground: FC = () => {
const [value, setValue] = useState('// Add some code here\n');
const [advanced, setAdvanced] = useState(false);
Expand Down Expand Up @@ -105,63 +100,6 @@ export const Playground: FC = () => {
presets: [],
} as CodeOptions);

// Load prettier for formatting
const handleFormat = useCallback(() => {
setValue(makeCode(options));
}, [options]);
const handleFormatRef = useRef(handleFormat);
useEffect(() => {
handleFormatRef.current = handleFormat;
}, [handleFormat]);
useEffect(() => {
const loadedScripts: string[] = [];

for (let i = 0, l = document.head.childNodes.length; i < l; i++) {
const child = document.head.childNodes[i];

if (child.nodeType === 1 && child.nodeName === 'SCRIPT') {
const scriptEl = child as HTMLScriptElement;

if (scriptEl.src) {
loadedScripts.push(scriptEl.src);
}
}
}

const unlisten: Array<() => void> = [];

const format = (e: Event) => {
const element = e.target ? (e.target as HTMLScriptElement) : null;

if (!element) {
return;
}

loadedScripts.push(element.src);

if (PRETTIER_SCRIPTS.every((script) => loadedScripts.includes(script))) {
handleFormatRef.current();
}
};

PRETTIER_SCRIPTS.forEach((script) => {
if (!loadedScripts.includes(script)) {
const scriptEl = document.createElement('script');
scriptEl.addEventListener('load', format, false);
unlisten.push(() => {
scriptEl.removeEventListener('load', format, false);
});
scriptEl.src = script;
document.head.append(scriptEl);
}
});
return () => {
for (const cb of unlisten) {
cb();
}
};
}, []);

const handleToggleAdvanced = useCallback(() => {
if (
confirm(
Expand Down
2 changes: 2 additions & 0 deletions support/website/src/pages/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const PlaygroundPage = (props: any) => {
{/* TODO: Do not assume that it is in english language */}
<html lang='en' />
<script src='https://unpkg.com/@babel/standalone/babel.min.js'></script>
<script src='https://unpkg.com/prettier@2.0.5/standalone.js'></script>
<script src='https://unpkg.com/prettier@2.0.5/parser-typescript.js'></script>
{metaTitle ?? <title>{metaTitle}</title>}
{metaTitle ?? <meta property='og:title' content={metaTitle} />}
{favicon ?? <link rel='shortcut icon' href={faviconUrl} />}
Expand Down

1 comment on commit 71f27e1

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://remirror.io as production
🚀 Deployed on https://5f106a3c10d00e4c52dcdc28--remirror.netlify.app

Please sign in to comment.