diff --git a/client/src/lit/play/editor.js b/client/src/lit/play/editor.js index ffc9a2ea65dd..c7192d76d17f 100644 --- a/client/src/lit/play/editor.js +++ b/client/src/lit/play/editor.js @@ -61,6 +61,10 @@ export class PlayEditor extends LitElement { return this._editor ? this._editor.state.doc.toString() : this._value; } + _dispatchUpdate() { + this.dispatchEvent(new Event("update", { bubbles: true, composed: true })); + } + _extensions() { const language = (() => { switch (this.language) { @@ -99,9 +103,7 @@ export class PlayEditor extends LitElement { } this._updateTimer = window?.setTimeout(() => { this._updateTimer = -1; - this.dispatchEvent( - new Event("update", { bubbles: true, composed: true }) - ); + this._dispatchUpdate(); }, 1000); } }), @@ -141,10 +143,17 @@ export class PlayEditor extends LitElement { })(); if (config) { const plugins = await Promise.all(config.plugins); - this.value = await prettier.format(this.value, { + const unformatted = this.value; + const formatted = await prettier.format(unformatted, { parser: config.parser, plugins: /** @type {import("prettier").Plugin[]} */ (plugins), }); + if (this.value === unformatted) { + if (unformatted !== formatted) { + this.value = formatted; + this._dispatchUpdate(); + } + } } } diff --git a/client/src/playground/index.tsx b/client/src/playground/index.tsx index 1244a910cae6..752fbbc727b9 100644 --- a/client/src/playground/index.tsx +++ b/client/src/playground/index.tsx @@ -67,7 +67,8 @@ export default function Playground() { let [shared, setShared] = useState(false); let [shareUrl, setShareUrl] = useState(null); let [state, setState] = useState(State.initial); - const [isEmpty, setIsEmpty] = useState(true); + const [isShareable, setIsShareable] = useState(true); + const [isClearable, setIsClearable] = useState(true); const [initialContent, setInitialContent] = useState( null ); @@ -120,14 +121,24 @@ export default function Playground() { }; }, [initialContent?.src, initialCode?.src]); - const setEditorContent = (content: EditorContent) => { - if (controller.current) { - controller.current.code = { ...content }; - if (content.src) { - controller.current.srcPrefix = content.src; + const setIsEmpty = useCallback((content: EditorContent) => { + const { html, css, js } = content; + setIsShareable(!html.trim() && !css.trim() && !js.trim()); + setIsClearable(!html && !css && !js); + }, []); + + const setEditorContent = useCallback( + (content: EditorContent) => { + if (controller.current) { + controller.current.code = { ...content }; + if (content.src) { + controller.current.srcPrefix = content.src; + } + setIsEmpty(content); } - } - }; + }, + [setIsEmpty] + ); useEffect(() => { (async () => { @@ -156,12 +167,11 @@ export default function Playground() { setState(State.ready); } })(); - }, [initialCode, state, gistId, stateParam]); + }, [initialCode, state, gistId, stateParam, setEditorContent]); const clear = async () => { setSearchParams([], { replace: true }); setInitialContent(null); - setIsEmpty(true); setEditorContent({ html: HTML_DEFAULT, css: CSS_DEFAULT, @@ -231,8 +241,7 @@ export default function Playground() { const onEditorUpdate = () => { const code = getEditorContent(); - const { html, css, js } = code; - setIsEmpty(!html && !css && !js); + setIsEmpty(code); store(SESSION_KEY, code); }; @@ -258,7 +267,7 @@ export default function Playground() {