Skip to content

Commit

Permalink
perf: optimize instance preview.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed May 5, 2023
1 parent 679d07c commit 2ad9807
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ export default React.forwardRef<HTMLIFrameElement, RunWebProps>((props, ref) =>

useImperativeHandle(ref, () => frameRef.current as HTMLIFrameElement, [frameRef]);

const srcDoc = useMemo(() => {
useMemo(() => {
setIsLoaded(false);
const jsString = js ? `<script type="text/javascript">${js}</script>` : '';
const cssString = css ? `<style>${css}</style>` : '';
return `<!DOCTYPE html><html><head>${cssString}</head><body>${html}</body>${jsString}</html>`;
const result = `<!DOCTYPE html><html><head>${cssString}</head><body>${html}</body>${jsString}</html>`;
const blob = new Blob([result], { type: 'text/html' });
if (frameRef.current) {
frameRef.current.src = URL.createObjectURL(blob);
}
}, [css, html, js]);

function renderFrameContents() {
Expand All @@ -40,7 +44,6 @@ export default React.forwardRef<HTMLIFrameElement, RunWebProps>((props, ref) =>
onLoad && onLoad(evn);
}}
ref={frameRef}
srcDoc={srcDoc}
>
{isLoaded && renderFrameContents()}
</iframe>
Expand Down
2 changes: 1 addition & 1 deletion website/pages/run/index.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}

.editor {
font-size: 12px;
font-size: 16px;
background-color: #fff;
position: relative;
overflow: hidden;
Expand Down
6 changes: 3 additions & 3 deletions website/pages/run/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default function Run() {
<div className={styles.editorCode}>
<CodeEditor
value={htmlStr}
style={{ minHeight: '100%' }}
style={{ fontSize: 16, minHeight: '100%' }}
placeholder="Please enter HTML code."
onChange={(evn) => handleChange(evn.target.value, 'html')}
language="html"
Expand All @@ -96,7 +96,7 @@ export default function Run() {
<div className={styles.editorCode}>
<CodeEditor
value={jsStr}
style={{ minHeight: '100%' }}
style={{ fontSize: 16, minHeight: '100%' }}
placeholder="Please enter JavaScript code."
onChange={(evn) => handleChange(evn.target.value, 'js')}
language="js"
Expand All @@ -114,7 +114,7 @@ export default function Run() {
<div className={styles.editorCode}>
<CodeEditor
value={cssStr}
style={{ minHeight: '100%' }}
style={{ fontSize: 16, minHeight: '100%' }}
placeholder="Please enter CSS code."
onChange={(evn) => handleChange(evn.target.value, 'css')}
language="css"
Expand Down

0 comments on commit 2ad9807

Please sign in to comment.