From d323e2a1e9101f5462e496941fe5d0ce2383b0b6 Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Wed, 22 Jan 2025 09:28:06 +0800 Subject: [PATCH] feat(plugin-md-power): improve `normal demo` sandbox --- .../demo/normal-lib.html" | 4 +- .../demo/normal.html" | 2 +- ...15\347\253\257\346\274\224\347\244\272.md" | 24 +-- .../src/client/components/VPDemoBasic.vue | 7 +- .../src/client/components/VPDemoNormal.vue | 136 +++----------- .../src/client/composables/demo.ts | 173 ++++++++++++++++++ .../src/client/styles/demo.css | 7 + .../src/client/utils/shared.ts | 39 ---- .../plugin-md-power/src/node/demo/watcher.ts | 8 +- plugins/plugin-md-power/tsup.config.ts | 4 +- 10 files changed, 226 insertions(+), 178 deletions(-) create mode 100644 plugins/plugin-md-power/src/client/composables/demo.ts delete mode 100644 plugins/plugin-md-power/src/client/utils/shared.ts diff --git "a/docs/notes/theme/guide/\344\273\243\347\240\201\346\274\224\347\244\272/demo/normal-lib.html" "b/docs/notes/theme/guide/\344\273\243\347\240\201\346\274\224\347\244\272/demo/normal-lib.html" index ff84aa82..05bec0bf 100644 --- "a/docs/notes/theme/guide/\344\273\243\347\240\201\346\274\224\347\244\272/demo/normal-lib.html" +++ "b/docs/notes/theme/guide/\344\273\243\347\240\201\346\274\224\347\244\272/demo/normal-lib.html" @@ -5,8 +5,8 @@

vuepress-theme-plume

+ + + ${html} + + + +` +} + +export function parseData(data: any) { + try { + if (typeof data === 'string') { + return JSON.parse(data) + } + else if (isPlainObject(data)) { + return data + } + return {} + } + catch { + return {} + } +} diff --git a/plugins/plugin-md-power/src/client/styles/demo.css b/plugins/plugin-md-power/src/client/styles/demo.css index 152195a3..7721417a 100644 --- a/plugins/plugin-md-power/src/client/styles/demo.css +++ b/plugins/plugin-md-power/src/client/styles/demo.css @@ -9,6 +9,13 @@ padding: 24px; } +.vp-demo-wrapper .demo-draw .draw-iframe { + width: 100%; + padding: 0; + margin: 0; + border: none; +} + .vp-demo-wrapper .demo-info .title { display: flex; align-items: center; diff --git a/plugins/plugin-md-power/src/client/utils/shared.ts b/plugins/plugin-md-power/src/client/utils/shared.ts deleted file mode 100644 index 2026fd29..00000000 --- a/plugins/plugin-md-power/src/client/utils/shared.ts +++ /dev/null @@ -1,39 +0,0 @@ -const cache: { - [src: string]: Promise | undefined -} = {} -export function loadScript(src: string) { - if (__VUEPRESS_SSR__) - return Promise.resolve() - - if (document.querySelector(`script[src="${src}"]`)) { - if (cache[src]) - return cache[src] - return Promise.resolve() - } - - const script = document.createElement('script') - script.src = src - document.body.appendChild(script) - - cache[src] = new Promise((resolve, reject) => { - script.onload = () => { - resolve() - delete cache[src] - } - script.onerror = reject - }) - return cache[src] -} - -export function loadStyle(href: string, target: ShadowRoot) { - if (__VUEPRESS_SSR__) - return - - if (target.querySelector(`link[href="${href}"]`)) - return - - const link = document.createElement('link') - link.rel = 'stylesheet' - link.href = href - target.appendChild(link) -} diff --git a/plugins/plugin-md-power/src/node/demo/watcher.ts b/plugins/plugin-md-power/src/node/demo/watcher.ts index daf52d4a..b606aa1f 100644 --- a/plugins/plugin-md-power/src/node/demo/watcher.ts +++ b/plugins/plugin-md-power/src/node/demo/watcher.ts @@ -80,9 +80,11 @@ export function demoWatcher(app: App, watchers: any[]) { watcher!.unwatch(path) }) - watchers.push(() => { - watcher!.close() - watcher = null + watchers.push({ + close: () => { + watcher!.close() + watcher = null + }, }) } diff --git a/plugins/plugin-md-power/tsup.config.ts b/plugins/plugin-md-power/tsup.config.ts index 37af6301..c4b7a02d 100644 --- a/plugins/plugin-md-power/tsup.config.ts +++ b/plugins/plugin-md-power/tsup.config.ts @@ -2,8 +2,8 @@ import { defineConfig, type Options } from 'tsup' import { argv } from '../../scripts/tsup-args.js' const config = [ - { dir: 'composables', files: ['codeRepl.ts', 'pdf.ts', 'rustRepl.ts', 'size.ts', 'audio.ts'] }, - { dir: 'utils', files: ['http.ts', 'is.ts', 'link.ts', 'sleep.ts', 'shared.ts'] }, + { dir: 'composables', files: ['codeRepl.ts', 'pdf.ts', 'rustRepl.ts', 'size.ts', 'audio.ts', 'demo.ts'] }, + { dir: 'utils', files: ['http.ts', 'is.ts', 'link.ts', 'sleep.ts'] }, { dir: '', files: ['index.ts', 'options.ts'] }, ]