vp-example.vue 2.8 KB

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