Skip to content

Commit

Permalink
chore: 优化预览方案
Browse files Browse the repository at this point in the history
  • Loading branch information
ckvv committed Aug 10, 2023
1 parent bde9409 commit 4c46728
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 131 deletions.
5 changes: 3 additions & 2 deletions docs/.vitepress/nav.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $t } from './i18n';

const playgroundLink = 'https://sfc.vuejs.org/#eNp9kMtugzAQRX/F8iYbwHvkVn38QBdVVt5QmCRO4ofGA1WF+PcOmFYJlbqbx9W5M3eUzzFWQw+yljq1aCOJBNTHR+OtiwFJjGL/0hMFX4j9a/AHe3zDMNgOsBB0AgdJTOKAwYndU3uJTXtRQ9nbnfFaZSKzuCFw8doQcCeE3qBEnVEPRubCyEVnSK/u2Yv3Ea1r8IsF75BIq3WfsWrD5alWv86ykPmp0jWxOqfg+e1x8VkX7FuLZTLPOJe5N/JEFFOtVDq0c1jnVAU8Kq4q7D1ZBxUkV35g+EyADDay+GHcZnIPazvP0g6udsDKAykfnbqVq84mWqp7JPsq9hkASwTPXwL+d+ZG+ufUGTsZP8npGx3Vt+c=';
// const playgroundLink = 'https://sfc.vuejs.org/#eNp9kMtugzAQRX/F8iYbwHvkVn38QBdVVt5QmCRO4ofGA1WF+PcOmFYJlbqbx9W5M3eUzzFWQw+yljq1aCOJBNTHR+OtiwFJjGL/0hMFX4j9a/AHe3zDMNgOsBB0AgdJTOKAwYndU3uJTXtRQ9nbnfFaZSKzuCFw8doQcCeE3qBEnVEPRubCyEVnSK/u2Yv3Ea1r8IsF75BIq3WfsWrD5alWv86ykPmp0jWxOqfg+e1x8VkX7FuLZTLPOJe5N/JEFFOtVDq0c1jnVAU8Kq4q7D1ZBxUkV35g+EyADDay+GHcZnIPazvP0g6udsDKAykfnbqVq84mWqp7JPsq9hkASwTPXwL+d+ZG+ufUGTsZP8npGx3Vt+c=';

export function getSidebarCompoents(lang = '') {
const t = $t(lang || 'zh');
Expand Down Expand Up @@ -74,7 +74,8 @@ export function getNav(lang = '') {
activeMatch: `^${lang}/compoents`,
}, {
text: t('playground'),
link: playgroundLink,
link: '/play',
activeMatch: '/play',
}, {
text: t('changelog'),
link: `${lang}/CHANGELOG`,
Expand Down
61 changes: 36 additions & 25 deletions docs/.vitepress/plugins/demo-block.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
<script setup>
import { computed, ref } from 'vue';
import ReplWrapper from './repl-wrapper.vue';
import { ReplStore } from '@vue/repl';
const props = defineProps({
description: {
Expand All @@ -15,18 +15,21 @@ const props = defineProps({
type: String,
default: '',
},
demo: {
type: Object,
default: () => {},
},
});
const replWrapperRef = ref();
const isShowCode = ref(false);
const description = computed(() => decodeURIComponent(props.description));
const code = computed(() => decodeURIComponent(props.code));
const editLink = computed(() => decodeURIComponent(props.editLink));
const openlink = (url: string) => window.open(url);
const openlink = url => window.open(url);
function reset() {
replWrapperRef?.value.setAppFile(decodeURIComponent(props.code));
}
const store = new ReplStore();
store.setFiles({ 'src/App.vue': decodeURIComponent(code.value) });
</script>

<template>
Expand All @@ -36,31 +39,39 @@ function reset() {
{{ description }}
</slot>
</div>
<div class="control">
<button
v-if="editLink"
class="control-btn"
@click="reset"
>
Reset
</button>
<button
v-if="editLink"
class="control-btn"
@click="openlink(editLink)"
>
Edit
</button>
<div v-if="demo" class="preview">
<slot name="preview" :compoent="demo">
<component :is="demo" />
</slot>
</div>
<button
class="control-btn" :style="{
color: isShowCode ? 'var(--vp-c-brand)' : 'var(--vp-c-text-2)',
}" @click="isShowCode = !isShowCode"
>
{{ '</>' }}
</button>
<button v-if="editLink" class="control-btn" @click="openlink(editLink)">
Edit
</button>
<button class="control-btn" @click="openlink(`/v-ui/play${store.serialize()}`)">
Playground
</button>

<div v-show="isShowCode && code">
<slot name="code">
<pre>
<code>{{ code }}</code>
</pre>
</slot>
</div>
<slot name="code">
<ReplWrapper ref="replWrapperRef" :code="code" />
</slot>
</div>
</template>

<style scoped>
.demo-block {
border: 1px solid var(--vp-c-divider-light);
text-align: right;
}
.demo-block .description {
Expand Down
9 changes: 8 additions & 1 deletion docs/.vitepress/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ export function mdDemoPlugin(options?: {
env.sfcBlocks.scriptSetup = scriptSetup;
env.sfcBlocks.scripts = [scriptSetup];

// return `${md.render(description)}
// <${demoCompoentPrefix} code="${encodeCode}" demo="${compoentName}" edit-link="${encodeEditLint}" />`;

return `${md.render(description)}
<${demoCompoentPrefix} code="${encodeCode}" demo="${compoentName}" edit-link="${encodeEditLint}" />`;
<${demoCompoentPrefix} code="${encodeCode}" :demo="${compoentName}" edit-link="${encodeEditLint}">
<template #code>
${defaultFenceRender(tokens, idx, options, env, self)}
</template>
</${demoCompoentPrefix}>`;
};
};
}
75 changes: 37 additions & 38 deletions docs/.vitepress/plugins/repl-wrapper.vue
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
<script setup lang="ts">
<script setup>
import '@vue/repl/style.css';
import { File, Repl, ReplStore } from '@vue/repl';
import Monaco from '@vue/repl/monaco-editor';
import { shallowRef, watch } from 'vue';
const props = defineProps({
code: {
type: String,
default: '<template>\n</template>',
},
import { watchEffect } from 'vue';
import { Repl, ReplStore } from '@vue/repl';
import CodeMirror from '@vue/repl/codemirror-editor';
// import Monaco from '@vue/repl/monaco-editor';
function setVH() {
document.documentElement.style.setProperty('--vh', `${window.innerHeight}px`);
};
window.addEventListener('resize', setVH);
setVH();
const query = new URLSearchParams(location.search);
const defaultSerializedState = 'eNp9kc1uwyAQhF8l4pyYQ26RFfUvh/bQVm2PXFy8cYjxgmBxLUV+92LcuG4V5YZmhuHb5cRurc3aAGzDcoLG6oJgK3CxyNvVZyAyuH2AfRE05XxSBOZ8FmZLphprHK2awmZHbzC2nYYS8WN4wTaLpAzajaxtIevYF9RgCHYgsn7DuSwx3i9Bq9ZlCMTRNnwe56XylE4xJ9jQ2AvsIwF5aXCvqn/vS9NYpcG9WFIG/3IUWpuvp6SRC7A86/IAsr6gH3034r468OBaEGzyqHAV0Gjv3p+hi+fJbEwZdExfMd/AGx0GxjF2F7CM2LNcon1M61RYffhdR4D+PNQAmraR8oLFL72/Mvov7jpbT1vsvwGBTq3o';
const store = new ReplStore({
// initialize repl with previously serialized state
serializedState: location.hash.slice(1) || defaultSerializedState,
// starts on the output pane (mobile only) if the URL has a showOutput query
showOutput: query.has('showOutput'),
// starts on a different tab on the output pane if the URL has a outputMode query
// and default to the "preview" tab
outputMode: query.get('outputMode') || 'preview',
// specify the default URL to import Vue runtime from in the sandbox
// default is the CDN link from jsdelivr.com with version matching Vue's version
// from peerDependency
// defaultVueRuntimeURL: 'cdn link to vue.runtime.esm-browser.js',
});
const store = shallowRef(new ReplStore());
// persist state to URL hash
watchEffect(() => history.replaceState({}, '', store.serialize()));
store.value.setImportMap({
// pre-set import map
store.setImportMap({
imports: {
'@ckpack/v-ui': 'https://cdn.jsdelivr.net/npm/@ckpack/v-ui/dist/v-ui.js',
},
});
watch(() => props.code, (v) => {
setAppFile(v);
}, {
immediate: true,
});
const previewOptions = {
customCode: {
importCode: 'import VUI from \'@ckpack/v-ui\';',
useCode: 'app.use(VUI);',
},
};
function setAppFile(code: string) {
if (store.value) {
store.value.addFile(new File('src/App.vue', code));
}
}
defineExpose({ setAppFile });
</script>

<template>
<Repl
:auto-resize="true"
:editor="Monaco" :store="store" :preview-options="previewOptions"
layout="vertical"
:show-import-map="false"
:show-compile-output="false"
:show-ts-config="false"
/>
<Repl :store="store" :editor="CodeMirror" :show-compile-output="true" :preview-options="previewOptions" />
</template>

<style>
.editor-container {
.editor {
min-height: 300px !important;
}
.vue-repl {
height: calc(var(--vh) - var(--vp-nav-height)) !important;
}
</style>
4 changes: 2 additions & 2 deletions docs/.vitepress/theme/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.vp-doc div[class*='language-'] {
/* .vp-doc div[class*='language-'] {
margin: 0;
}
} */
13 changes: 13 additions & 0 deletions docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import DefaultTheme from 'vitepress/theme';
import './index.css';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import VUI from '@/index';

export default {
...DefaultTheme,
async enhanceApp({ app }) {
app.use(VUI);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (!import.meta.env.SSR) {
const ReplWrapper = (await import('../plugins/repl-wrapper.vue')).default;
app.component('ReplWrapper', ReplWrapper);
}
},
};
4 changes: 4 additions & 0 deletions docs/play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
layout: home
---
<repl-wrapper />
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@
"async-validator": "^4.2.5"
},
"devDependencies": {
"@vue/repl": "^2.5.6",
"@vue/repl": "^2.5.7",
"eslint": "^8.46.0",
"@antfu/eslint-config": "0.40.0",
"@antfu/eslint-config": "0.40.2",
"tsx": "^3.12.7",
"@types/node": "^18.x",
"typescript": "5.1.6",
"vite": "^4.4.8",
"vite": "^4.4.9",
"@vitejs/plugin-vue": "^4.2.3",
"vite-plugin-dts": "3.5.1",
"@vue/tsconfig": "^0.4.0",
"fast-glob": "^3.3.1",
"simple-git-hooks": "^2.9.0",
"lint-staged": "^13.2.3",
"standard-version": "^9.5.0",
"vitepress": "1.0.0-beta.7",
"vitepress": "1.0.0-rc.4",
"vue": "^3.3.4"
},
"peerDependencies": {
Expand Down
Loading

0 comments on commit 4c46728

Please sign in to comment.