vp-example.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <script setup lang="ts">
  2. import { computed, unref, watchEffect } from 'vue'
  3. import { File, Repl, ReplStore } from '@vue/repl'
  4. import mainCode from './main.vue?raw'
  5. import deptCode from './dept.js?raw'
  6. import { loadKanKanThemeChalkStyle } from './dept-home'
  7. import type { SFCOptions } from '@vue/repl'
  8. const sfcOptions: SFCOptions = {
  9. script: {
  10. reactivityTransform: true,
  11. },
  12. }
  13. const props = defineProps({
  14. file: {
  15. type: String,
  16. required: true,
  17. },
  18. raw: {
  19. type: String,
  20. required: true,
  21. },
  22. demo: {
  23. type: Object,
  24. required: true,
  25. },
  26. isRepl: {
  27. type: Boolean,
  28. required: false,
  29. default: () => false,
  30. },
  31. })
  32. const isDev = computed(() => {
  33. return import.meta.env.MODE === 'development'
  34. })
  35. const serverLink = computed(() => {
  36. return unref(isDev) ? '/demoServer' : 'https://test.4dkankan.com/'
  37. })
  38. const store = new ReplStore({
  39. // initialize repl with previously serialized state
  40. // serializedState: unref(loadSingleData),
  41. // starts on the output pane (mobile only) if the URL has a showOutput query
  42. showOutput: true,
  43. // starts on a different tab on the output pane if the URL has a outputMode query
  44. // and default to the "preview" tab
  45. outputMode: 'preview',
  46. // specify the default URL to import Vue runtime from in the sandbox
  47. // default is the CDN link from unpkg.com with version matching Vue's version
  48. // from peerDependency
  49. defaultVueRuntimeURL:
  50. 'https://cdn.jsdelivr.net/npm/@vue/runtime-dom@latest/dist/runtime-dom.esm-browser.js',
  51. })
  52. watchEffect(async () => {
  53. if (!unref(props.isRepl)) {
  54. await loadKanKanThemeChalkStyle()
  55. } else {
  56. if (unref(props.raw)) {
  57. store.setImportMap({
  58. imports: {
  59. vue: 'https://cdn.jsdelivr.net/npm/@vue/runtime-dom@latest/dist/runtime-dom.esm-browser.js',
  60. '@vue/shared':
  61. 'https://cdn.jsdelivr.net/npm/@vue/shared@latest/dist/shared.esm-bundler.js',
  62. 'kankan-components':
  63. 'https://4dkk.4dage.com/npm_test/kankan-components/dist/index.full.min.mjs',
  64. },
  65. })
  66. const mainFile = new File('PlaygroundMain.vue', mainCode)
  67. const deptFile = new File('dept.js', deptCode)
  68. const appFile = new File(
  69. 'App.vue',
  70. decodeURIComponent(props.raw).replace('#DEMOSEVER#', unref(serverLink))
  71. )
  72. store.addFile(mainFile)
  73. store.addFile(appFile)
  74. store.addFile(deptFile)
  75. store.state.mainFile = 'PlaygroundMain.vue'
  76. store.state.activeFile = appFile
  77. store.init()
  78. }
  79. }
  80. })
  81. </script>
  82. <template>
  83. <div class="example-showcase" antialiased>
  84. <ClientOnly>
  85. <template v-if="demo">
  86. <component :is="demo" v-if="!isRepl" v-bind="$attrs" />
  87. <Repl
  88. v-else
  89. ref="repl"
  90. :store="store"
  91. v-bind="$attrs"
  92. :sfc-options="sfcOptions"
  93. :clear-console="false"
  94. :show-compile-output="true"
  95. auto-resize
  96. />
  97. </template>
  98. </ClientOnly>
  99. </div>
  100. </template>
  101. <style lang="scss" scoped>
  102. .example-showcase {
  103. padding: 1.5rem;
  104. margin: 0.5px;
  105. background-color: var(--bg-color);
  106. }
  107. </style>
  108. <style lang="scss">
  109. .vue-repl {
  110. height: 650px;
  111. .left {
  112. display: none;
  113. }
  114. .right {
  115. width: 100% !important;
  116. }
  117. // .tab-buttons {
  118. // display: none;
  119. // }
  120. }
  121. </style>