vp-demo.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <script setup lang="ts">
  2. import { computed, getCurrentInstance, toRef } from 'vue'
  3. import { isClient, useClipboard, useToggle } from '@vueuse/core'
  4. import { CaretTop } from '@element-plus/icons-vue'
  5. import { useLang } from '../composables/lang'
  6. import { useSourceCode } from '../composables/source-code'
  7. import { usePlayground } from '../composables/use-playground'
  8. import demoBlockLocale from '../../i18n/component/demo-block.json'
  9. import Example from './demo/vp-example.vue'
  10. import SourceCode from './demo/vp-source-code.vue'
  11. import '@vue/repl/style.css'
  12. const props = defineProps<{
  13. demos: object
  14. source: string
  15. path: string
  16. rawSource: string
  17. description?: string
  18. isRepl?: boolean
  19. }>()
  20. const vm = getCurrentInstance()!
  21. const { copy, isSupported } = useClipboard({
  22. source: decodeURIComponent(props.rawSource),
  23. read: false,
  24. })
  25. const [sourceVisible, toggleSourceVisible] = useToggle()
  26. const lang = useLang()
  27. const demoSourceUrl = useSourceCode(toRef(props, 'path'))
  28. const formatPathDemos = computed(() => {
  29. const demos = {}
  30. Object.keys(props.demos).forEach(key => {
  31. demos[key.replace('../../examples/', '').replace('.vue', '')] = props.demos[key].default
  32. })
  33. return demos
  34. })
  35. const locale = computed(() => demoBlockLocale[lang.value])
  36. const decodedDescription = computed(() => decodeURIComponent(props.description!))
  37. const onPlaygroundClick = () => {
  38. const productionServer = 'https://test.4dkankan.com'
  39. const data = decodeURIComponent(props.rawSource).replace('#DEMOSEVER#', productionServer)
  40. const { link } = usePlayground(encodeURIComponent(data))
  41. if (!isClient) return
  42. window.open(link)
  43. }
  44. const copyCode = async () => {
  45. const { $message } = vm.appContext.config.globalProperties
  46. if (!isSupported) {
  47. $message.error(locale.value['copy-error'])
  48. }
  49. try {
  50. await copy()
  51. $message.success(locale.value['copy-success'])
  52. } catch (e: any) {
  53. $message.error(e.message)
  54. }
  55. }
  56. </script>
  57. <template>
  58. <ClientOnly>
  59. <!-- danger here DO NOT USE INLINE SCRIPT TAG -->
  60. <p text="sm" v-html="decodedDescription" />
  61. <div class="example">
  62. <Example :file="path" :demo="formatPathDemos[path]" :raw="rawSource" :is-repl="isRepl" />
  63. <ElDivider class="m-0" />
  64. <div class="op-btns">
  65. <ElTooltip :content="locale['edit-in-editor']" :show-arrow="false">
  66. <ElIcon :size="16" class="op-btn">
  67. <i-ri-flask-line @click="onPlaygroundClick" />
  68. </ElIcon>
  69. </ElTooltip>
  70. <ElTooltip :content="locale['edit-on-github']" :show-arrow="false">
  71. <ElIcon :size="16" class="op-btn github" style="color: var(--text-color-light)">
  72. <a :href="demoSourceUrl" rel="noreferrer noopener" target="_blank">
  73. <i-ri-github-line />
  74. </a>
  75. </ElIcon>
  76. </ElTooltip>
  77. <ElTooltip :content="locale['copy-code']" :show-arrow="false">
  78. <ElIcon :size="16" class="op-btn" @click="copyCode">
  79. <i-ri-file-copy-line />
  80. </ElIcon>
  81. </ElTooltip>
  82. <ElTooltip :content="locale['view-source']" :show-arrow="false">
  83. <ElIcon :size="16" class="op-btn" @click="toggleSourceVisible()">
  84. <i-ri-code-line />
  85. </ElIcon>
  86. </ElTooltip>
  87. </div>
  88. <ElCollapseTransition>
  89. <SourceCode v-show="sourceVisible" :source="source" />
  90. </ElCollapseTransition>
  91. <Transition name="el-fade-in-linear">
  92. <div v-show="sourceVisible" class="example-float-control" @click="toggleSourceVisible(false)">
  93. <ElIcon :size="16">
  94. <CaretTop />
  95. </ElIcon>
  96. <span>{{ locale['hide-source'] }}</span>
  97. </div>
  98. </Transition>
  99. </div>
  100. </ClientOnly>
  101. </template>
  102. <style scoped lang="scss">
  103. .example {
  104. border: 1px solid var(--border-color);
  105. border-radius: var(--el-border-radius-base);
  106. .op-btns {
  107. padding: 0.5rem;
  108. display: flex;
  109. align-items: center;
  110. justify-content: flex-end;
  111. height: 2.5rem;
  112. .el-icon {
  113. &:hover {
  114. color: var(--text-color);
  115. }
  116. }
  117. .op-btn {
  118. margin: 0 0.5rem;
  119. cursor: pointer;
  120. color: var(--text-color-lighter);
  121. transition: 0.2s;
  122. &.github a {
  123. transition: 0.2s;
  124. color: var(--text-color-lighter);
  125. &:hover {
  126. color: var(--text-color);
  127. }
  128. }
  129. }
  130. }
  131. &-float-control {
  132. display: flex;
  133. align-items: center;
  134. justify-content: center;
  135. border-top: 1px solid var(--border-color);
  136. height: 44px;
  137. box-sizing: border-box;
  138. background-color: var(--bg-color, #fff);
  139. border-bottom-left-radius: 4px;
  140. border-bottom-right-radius: 4px;
  141. margin-top: -1px;
  142. color: var(--el-text-color-secondary);
  143. cursor: pointer;
  144. position: sticky;
  145. left: 0;
  146. right: 0;
  147. bottom: 0;
  148. z-index: 10;
  149. span {
  150. font-size: 14px;
  151. margin-left: 10px;
  152. }
  153. &:hover {
  154. color: var(--el-color-primary);
  155. }
  156. }
  157. }
  158. </style>