vp-demo.vue 5.0 KB

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