vp-example.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 { loadSDK, loadSDKDep } 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@3.2.47/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@3.2.47/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. '@kankan-components/icons-vue':
  65. 'https://4dkk.4dage.com/npm_test/%40kankan-components/icon-vue/dist/index.min.js',
  66. },
  67. })
  68. const mainFile = new File('PlaygroundMain.vue', mainCode)
  69. const deptFile = new File('dept.js', deptCode)
  70. const appFile = new File(
  71. 'App.vue',
  72. decodeURIComponent(props.raw).replace('#DEMOSEVER#', unref(serverLink))
  73. )
  74. store.addFile(mainFile)
  75. store.addFile(appFile)
  76. store.addFile(deptFile)
  77. store.state.mainFile = 'PlaygroundMain.vue'
  78. store.state.activeFile = appFile
  79. store.init()
  80. }
  81. }
  82. })
  83. </script>
  84. <template>
  85. <div class="example-showcase" antialiased>
  86. <ClientOnly>
  87. <template v-if="demo">
  88. <component :is="demo" v-if="!isRepl" v-bind="$attrs" />
  89. <Repl
  90. v-else
  91. ref="repl"
  92. :store="store"
  93. v-bind="$attrs"
  94. :sfc-options="sfcOptions"
  95. :clear-console="false"
  96. :show-compile-output="true"
  97. auto-resize
  98. />
  99. </template>
  100. </ClientOnly>
  101. </div>
  102. </template>
  103. <style lang="scss" scoped>
  104. .example-showcase {
  105. padding: 1.5rem;
  106. margin: 0.5px;
  107. background-color: var(--bg-color);
  108. }
  109. </style>
  110. <style lang="scss">
  111. .vue-repl {
  112. height: 650px;
  113. .left {
  114. display: none;
  115. }
  116. .right {
  117. width: 100% !important;
  118. }
  119. // .tab-buttons {
  120. // display: none;
  121. // }
  122. }
  123. </style>