From 864fc4db40c4db0e76600f0e35b8ad04014f9a3e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 27 Jan 2026 18:52:05 +0800 Subject: [PATCH] Refactor playground settings (#18762) --- website/playground/Playground.jsx | 56 +++++++++---------- website/playground/PrettierFormat.js | 16 +++--- .../playground/composables/editor-state.js | 38 ------------- .../composables/playground-settings.js | 45 +++++++++++++++ 4 files changed, 81 insertions(+), 74 deletions(-) delete mode 100644 website/playground/composables/editor-state.js create mode 100644 website/playground/composables/playground-settings.js diff --git a/website/playground/Playground.jsx b/website/playground/Playground.jsx index b34da37c6..53a762d45 100644 --- a/website/playground/Playground.jsx +++ b/website/playground/Playground.jsx @@ -1,7 +1,7 @@ import { reactive, watch } from "vue"; import { Button, ClipboardButton } from "./buttons.jsx"; import getCodeSample from "./codeSamples.mjs"; -import { editorState } from "./composables/editor-state.js"; +import { settings, togglers } from "./composables/playground-settings.js"; import { worker } from "./composables/prettier-worker.js"; import generateDummyId from "./dummyId.js"; import formatMarkdown from "./markdown.js"; @@ -253,7 +253,7 @@ function setup(props) { return ( <>
- + {isDocExplorer ? null : ( )} {isDocExplorer ? null : ( )} {isDocExplorer ? null : ( )} {isDocExplorer ? null : ( )} {isDocExplorer ? null : ( )} - {editorState.showDoc && !isDocExplorer ? ( + {settings.showDoc && !isDocExplorer ? ( getMarkdown({ doc: debug.doc })} disabled={!debug.doc} @@ -396,7 +396,7 @@ function setup(props) {
- {editorState.showInput ? ( + {settings.showInput ? ( ) : null} - {editorState.showAst ? ( + {settings.showAst ? ( ) : null} - {editorState.showPreprocessedAst && !isDocExplorer ? ( + {settings.showPreprocessedAst && !isDocExplorer ? ( ) : null} - {editorState.showDoc && !isDocExplorer ? ( + {settings.showDoc && !isDocExplorer ? ( ) : null} - {editorState.showComments && !isDocExplorer ? ( + {settings.showComments && !isDocExplorer ? ( ) : null} - {editorState.showOutput ? ( + {settings.showOutput ? ( ) : null} - {editorState.showSecondFormat && !isDocExplorer ? ( + {settings.showSecondFormat && !isDocExplorer ? (
- [ props.code, props.options, - editorState.showAst, - editorState.showPreprocessedAst, - editorState.showDoc, - editorState.showComments, - editorState.showSecondFormat, - editorState.rethrowEmbedErrors, + settings.showAst, + settings.showPreprocessedAst, + settings.showDoc, + settings.showComments, + settings.showSecondFormat, + settings.rethrowEmbedErrors, ], () => { format(); diff --git a/website/playground/composables/editor-state.js b/website/playground/composables/editor-state.js deleted file mode 100644 index 421192995..000000000 --- a/website/playground/composables/editor-state.js +++ /dev/null @@ -1,38 +0,0 @@ -import { reactive, watch } from "vue"; -import * as storage from "../storage.js"; - -const stateToggler = (property) => () => { - state[property] = !state[property]; -}; - -const state = reactive({ - showSidebar: window.innerWidth > window.innerHeight, - showAst: false, - showPreprocessedAst: false, - showDoc: false, - showComments: false, - showSecondFormat: false, - showInput: true, - showOutput: true, - rethrowEmbedErrors: false, - toggleSidebar: stateToggler("showSidebar"), - toggleAst: stateToggler("showAst"), - togglePreprocessedAst: stateToggler("showPreprocessedAst"), - toggleDoc: stateToggler("showDoc"), - toggleComments: stateToggler("showComments"), - toggleSecondFormat: stateToggler("showSecondFormat"), - toggleInput: stateToggler("showInput"), - toggleOutput: stateToggler("showOutput"), - toggleEmbedErrors: stateToggler("rethrowEmbedErrors"), - ...storage.get("editor_state"), -}); - -watch( - () => state, - () => { - storage.set("editor_state", state); - }, - { deep: true }, -); - -export { state as editorState }; diff --git a/website/playground/composables/playground-settings.js b/website/playground/composables/playground-settings.js new file mode 100644 index 000000000..211f79b91 --- /dev/null +++ b/website/playground/composables/playground-settings.js @@ -0,0 +1,45 @@ +import { reactive, watch } from "vue"; +import * as storage from "../storage.js"; + +/** +@typedef {typeof defaultSettings} PlaygroundSettings +*/ + +const defaultSettings = { + showSidebar: window.innerWidth > window.innerHeight, + showAst: false, + showPreprocessedAst: false, + showDoc: false, + showComments: false, + showSecondFormat: false, + showInput: true, + showOutput: true, + rethrowEmbedErrors: false, +}; + +const togglers = Object.fromEntries( + Object.keys(defaultSettings).map((property) => [ + property, + () => { + settings[property] = !settings[property]; + }, + ]), +); + +/** @type {PlaygroundSettings} */ +const initialSettings = { + ...defaultSettings, + ...storage.get("editor_state"), +}; + +const settings = reactive(initialSettings); + +watch( + () => settings, + () => { + storage.set("editor_state", settings); + }, + { deep: true }, +); + +export { settings, togglers };