|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useClipboard } from '@vueuse/core' |
| 3 | +import { useData } from 'vitepress' |
| 4 | +import { computed, ref } from 'vue' |
| 5 | +import { GITHUB_EDIT_URL } from '../config/constants' |
| 6 | +import ExampleComponent from './ExampleComponent.vue' |
| 7 | +import ExampleOperate from './ExampleOperate.vue' |
| 8 | +import ExampleSourceCode from './ExampleSourceCode.vue' |
| 9 | +
|
| 10 | +const props = defineProps({ |
| 11 | + path: { |
| 12 | + type: String, |
| 13 | + required: true, |
| 14 | + }, |
| 15 | + source: { |
| 16 | + type: String, |
| 17 | + required: true, |
| 18 | + }, |
| 19 | + rawSource: { |
| 20 | + type: String, |
| 21 | + required: true, |
| 22 | + }, |
| 23 | +}) |
| 24 | +
|
| 25 | +const { isDark } = useData() |
| 26 | +
|
| 27 | +const components = import.meta.glob('../../examples/**/*.vue', { eager: true }) |
| 28 | +
|
| 29 | +const pathComponents = computed(() => { |
| 30 | + const _obj = {} |
| 31 | + Object.keys(components).forEach((key) => { |
| 32 | + _obj[key.replace('../../components/', '').replace('.vue', '')] |
| 33 | + = components[key].default |
| 34 | + }) |
| 35 | + return _obj |
| 36 | +}) |
| 37 | +
|
| 38 | +const showCode = ref(false) |
| 39 | +
|
| 40 | +function toggleHandle() { |
| 41 | + showCode.value = !showCode.value |
| 42 | +} |
| 43 | +
|
| 44 | +const { copy } = useClipboard({ |
| 45 | + source: decodeURIComponent(props.rawSource), |
| 46 | + read: false, |
| 47 | +}) |
| 48 | +
|
| 49 | +function copyHandle() { |
| 50 | + copy() |
| 51 | +} |
| 52 | +
|
| 53 | +const path = computed(() => { |
| 54 | + return props.path.replace(/\/\//g, '/') |
| 55 | +}) |
| 56 | +
|
| 57 | +function openHandle() { |
| 58 | + window.open(`${GITHUB_EDIT_URL}/docs/${path.value}`) |
| 59 | +} |
| 60 | +</script> |
| 61 | + |
| 62 | +<template> |
| 63 | + <ClientOnly> |
| 64 | + <div class="example" :class="{ dark: isDark }"> |
| 65 | + <div class="example-component-wrapper"> |
| 66 | + <ExampleComponent :file="path" :comp="pathComponents[path]" /> |
| 67 | + </div> |
| 68 | + <div class="example-operate-wrapper"> |
| 69 | + <ExampleOperate @toggle="toggleHandle" @copy="copyHandle" @open="openHandle" /> |
| 70 | + </div> |
| 71 | + <div v-show="showCode" class="example-source-code-wrapper"> |
| 72 | + <ExampleSourceCode :source="source" /> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + </ClientOnly> |
| 76 | +</template> |
| 77 | + |
| 78 | +<style scoped> |
| 79 | +.example { |
| 80 | + border: 1px solid; |
| 81 | + border-color: var(--vp-c-divider); |
| 82 | + border-radius: 4px; |
| 83 | + padding: 0; |
| 84 | +} |
| 85 | +
|
| 86 | +.example-component-wrapper, |
| 87 | +.example-operate-wrapper { |
| 88 | + padding: 0.5rem; |
| 89 | +} |
| 90 | +
|
| 91 | +.example-source-code-wrapper, |
| 92 | +.example-operate-wrapper { |
| 93 | + border-top: 1px solid; |
| 94 | + border-color: var(--vp-c-divider); |
| 95 | +} |
| 96 | +
|
| 97 | +.example-operate-wrapper { |
| 98 | + background-color: var(--vp-c-bg-soft); |
| 99 | +} |
| 100 | +</style> |
0 commit comments