|
| 1 | +<script lang="ts" setup> |
| 2 | +import { ref, onMounted, watch, computed } from "vue"; |
| 3 | +import CodeOpen from "../icons/code-open.vue"; |
| 4 | +import CodeClose from "../icons/code-close.vue"; |
| 5 | +import CodeCopy from "../icons/code-copy.vue"; |
| 6 | +import { useCodeFold } from "../hooks/use-codefold"; |
| 7 | +import { useCodeCopy } from "../hooks/use-codecopy"; |
| 8 | +import { useMessage } from "naive-ui"; |
| 9 | +
|
| 10 | +interface DemoBlockProps { |
| 11 | + code: string; |
| 12 | + showCode: string; |
| 13 | + title: string; |
| 14 | + description: string; |
| 15 | + onlyRender?: boolean; |
| 16 | +} |
| 17 | +
|
| 18 | +const props = withDefaults(defineProps<DemoBlockProps>(), { |
| 19 | + title: "默认标题", |
| 20 | + description: "描述内容", |
| 21 | + onlyRender: false, |
| 22 | +}); |
| 23 | +
|
| 24 | +const { isCodeFold, setCodeFold } = useCodeFold(); |
| 25 | +const { clickCopy } = useCodeCopy(); |
| 26 | +
|
| 27 | +const sourceCode = ref(decodeURIComponent(props.code)); |
| 28 | +const showSourceCode = ref(decodeURIComponent(props.showCode)); |
| 29 | +const sourceCodeArea = ref<any>(null); |
| 30 | +const message = useMessage(); |
| 31 | +
|
| 32 | +const clickCodeCopy = () => { |
| 33 | + clickCopy(sourceCode.value); |
| 34 | + message.success("复制成功"); |
| 35 | +}; |
| 36 | +
|
| 37 | +const sourceCodeContainerHeight = computed(() => { |
| 38 | + if (sourceCodeArea.value) return sourceCodeArea.value?.clientHeight; |
| 39 | + return 0; |
| 40 | +}); |
| 41 | +const setContainerHeight = (value: number) => { |
| 42 | + if (isCodeFold.value) sourceCodeArea.value.style.height = "0px"; |
| 43 | + else sourceCodeArea.value.style.height = `${value}px`; |
| 44 | +}; |
| 45 | +onMounted(() => { |
| 46 | + // 组件挂载时,先获取代码块容器为折叠前的容器高度 |
| 47 | + if (props.onlyRender) return; |
| 48 | + const currentContainerHeight = sourceCodeContainerHeight.value; |
| 49 | + setContainerHeight(currentContainerHeight); |
| 50 | +}); |
| 51 | +watch(isCodeFold, () => { |
| 52 | + const container = sourceCodeContainerHeight.value; |
| 53 | + setContainerHeight(container); |
| 54 | +}); |
| 55 | +</script> |
| 56 | + |
| 57 | +<template> |
| 58 | + <ClientOnly> |
| 59 | + <div class="vitepress-demo-preview__custom-ui__container"> |
| 60 | + <section v-if="!onlyRender || props.title" class="vitepress-demo-preview-name_handle"> |
| 61 | + <div v-if="props.title" class="vitepress-demo-preview-component__name">{{ title }}</div> |
| 62 | + <div v-if="!onlyRender" class="vitepress-demo-preview-description__btns"> |
| 63 | + <CodeCopy @click="clickCodeCopy" /> |
| 64 | + <CodeClose v-if="!isCodeFold" @click="setCodeFold(true)" /> |
| 65 | + <CodeOpen v-else @click="setCodeFold(false)" /> |
| 66 | + </div> |
| 67 | + </section> |
| 68 | + |
| 69 | + <section v-if="props.description" class="vitepress-demo-preview-description"> |
| 70 | + <span> |
| 71 | + {{ description }} |
| 72 | + </span> |
| 73 | + </section> |
| 74 | + |
| 75 | + <section class="vitepress-demo-preview-preview"> |
| 76 | + <slot></slot> |
| 77 | + </section> |
| 78 | + |
| 79 | + <section v-if="!onlyRender" ref="sourceCodeArea" class="vitepress-demo-preview-source"> |
| 80 | + <n-divider /> |
| 81 | + <div class="language-vue" v-html="showSourceCode"></div> |
| 82 | + </section> |
| 83 | + </div> |
| 84 | + </ClientOnly> |
| 85 | +</template> |
| 86 | + |
| 87 | +<style lang="scss" scoped> |
| 88 | +:root { |
| 89 | + --component-preview-bg: var(--vp-c-bg); |
| 90 | + --component-preview-soft: var(--vp-c-bg-soft); |
| 91 | + --component-preview-mute: var(--vp-c-bg-mute); |
| 92 | + --component-preview-border: rgb(240, 240, 240); |
| 93 | + --component-preview-text-1: var(--vp-c-text-1); |
| 94 | + --component-preview-text-2: var(--vp-c-text-2); |
| 95 | + --component-preview-text-3: var(--vp-c-text-3); |
| 96 | + --component-preview-text-4: var(--vp-c-text-4); |
| 97 | + --component-preview-code-block-bg: #343030; |
| 98 | + --component-preview-primary-color: var(--vp-c-brand); |
| 99 | +} |
| 100 | +
|
| 101 | +.dark:root { |
| 102 | + --component-preview-bg: var(--vp-c-bg); |
| 103 | + --component-preview-soft: var(--vp-c-bg-soft); |
| 104 | + --component-preview-mute: var(--vp-c-bg-mute); |
| 105 | + --component-preview-border: rgb(240, 240, 240, 0.1); |
| 106 | + --component-preview-text-1: var(--vp-c-text-1); |
| 107 | + --component-preview-text-2: var(--vp-c-text-2); |
| 108 | + --component-preview-text-3: var(--vp-c-text-3); |
| 109 | + --component-preview-text-4: var(--vp-c-text-4); |
| 110 | + --component-preview-code-block-bg: #282626; |
| 111 | + --component-preview-primary-color: var(--vp-c-brand); |
| 112 | +} |
| 113 | +
|
| 114 | +$defaultPrefix: "vitepress-demo-preview"; |
| 115 | +
|
| 116 | +$componentPrefix: "custom-ui"; |
| 117 | +$containerPrefix: #{$defaultPrefix}__#{$componentPrefix}; |
| 118 | +
|
| 119 | +.#{$containerPrefix}__container > * { |
| 120 | + font-size: 14px; |
| 121 | +} |
| 122 | +
|
| 123 | +.#{$containerPrefix}__container { |
| 124 | + div[class*="language-"] { |
| 125 | + margin-top: 0; |
| 126 | + margin-bottom: 0; |
| 127 | + border-radius: 0; |
| 128 | + background-color: var(--component-preview-bg); |
| 129 | + } |
| 130 | +} |
| 131 | +
|
| 132 | +.#{$containerPrefix}__container { |
| 133 | + width: 100%; |
| 134 | + border-radius: 4px; |
| 135 | + border: 1px solid var(--component-preview-border); |
| 136 | + box-shadow: 0px 0px 10px var(--component-preview-border); |
| 137 | + margin: 10px 0; |
| 138 | + overflow: hidden; |
| 139 | +
|
| 140 | + .#{$defaultPrefix}-name_handle, |
| 141 | + .#{$defaultPrefix}-description, |
| 142 | + .#{$defaultPrefix}-source { |
| 143 | + width: 100%; |
| 144 | + } |
| 145 | +} |
| 146 | +
|
| 147 | +.#{$containerPrefix}__container > .#{$defaultPrefix}-name_handle { |
| 148 | + padding: 20px 20px 20px 20px; |
| 149 | + display: flex; |
| 150 | + align-items: center; |
| 151 | +
|
| 152 | + & > .#{$defaultPrefix}-component__name { |
| 153 | + font-size: 20px; |
| 154 | + } |
| 155 | +
|
| 156 | + & > .#{$defaultPrefix}-description__btns { |
| 157 | + display: flex; |
| 158 | + align-items: center; |
| 159 | + margin-left: auto; |
| 160 | +
|
| 161 | + svg { |
| 162 | + width: 16px; |
| 163 | + height: 16px; |
| 164 | + fill: currentColor; |
| 165 | + color: var(--component-preview-text-1); |
| 166 | + cursor: pointer; |
| 167 | + } |
| 168 | +
|
| 169 | + svg:not(:last-of-type) { |
| 170 | + margin-right: 8px; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | +
|
| 175 | +.#{$containerPrefix}__container > .#{$defaultPrefix}-description { |
| 176 | + padding: 5px 20px 5px 20px; |
| 177 | +} |
| 178 | +
|
| 179 | +.#{$containerPrefix}__container > .#{$defaultPrefix}-preview { |
| 180 | + padding: 10px 10px 10px 20px; |
| 181 | +
|
| 182 | + & > p { |
| 183 | + margin: 0; |
| 184 | + padding: 0; |
| 185 | + } |
| 186 | +} |
| 187 | +
|
| 188 | +.#{$containerPrefix}__container > .#{$defaultPrefix}-source { |
| 189 | + overflow: hidden; |
| 190 | + transition: all 0.3s ease-in-out; |
| 191 | +} |
| 192 | +</style> |
0 commit comments