index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <div class="go-preview">
  3. <!-- 缩放层 -->
  4. <div ref="previewRef">
  5. <!-- 展示层 -->
  6. <div :style="previewRefStyle" v-if="show">
  7. <!-- 渲染层 -->
  8. <render-list :localStorageInfo="localStorageInfo"></render-list>
  9. </div>
  10. </div>
  11. </div>
  12. </template>
  13. <script setup lang="ts">
  14. import { onUnmounted, ref, nextTick, computed } from 'vue'
  15. import { usePreviewScale } from '@/hooks/index'
  16. import { RenderList } from './components/RenderList/index'
  17. import { ChartEditStorageType } from './index.d'
  18. import { useEditCanvasConfigStyle, getSessionStorageInfo } from './utils'
  19. import { CreateComponentType } from '@/packages/index.d'
  20. import { fetchChartComponent } from '@/packages/index'
  21. const previewRef = ref()
  22. const localStorageInfo: ChartEditStorageType = getSessionStorageInfo() as ChartEditStorageType
  23. const width = ref(localStorageInfo?.editCanvasConfig.width)
  24. const height = ref(localStorageInfo?.editCanvasConfig.height)
  25. const show = ref(false)
  26. const previewRefStyle: any = computed(() => {
  27. return useEditCanvasConfigStyle(localStorageInfo.editCanvasConfig)
  28. })
  29. // 注册组件(一开始无法获取window['$vue'])
  30. const intervalTiming = setInterval(() => {
  31. if (window['$vue'].component) {
  32. clearInterval(intervalTiming)
  33. show.value = true
  34. localStorageInfo.componentList.forEach(async (e: CreateComponentType) => {
  35. if (!window['$vue'].component(e.chartConfig.chartKey)) {
  36. window['$vue'].component(
  37. e.chartConfig.chartKey,
  38. fetchChartComponent(e.chartConfig)
  39. )
  40. }
  41. })
  42. }
  43. }, 200)
  44. // 屏幕适配
  45. nextTick(() => {
  46. const { calcRate, windowResize, unWindowResize } = usePreviewScale(
  47. width.value as number,
  48. height.value as number,
  49. previewRef.value
  50. )
  51. calcRate()
  52. windowResize()
  53. onUnmounted(() => {
  54. unWindowResize()
  55. })
  56. })
  57. </script>
  58. <style lang="scss" scoped>
  59. @include go('preview') {
  60. position: relative;
  61. display: flex;
  62. align-items: center;
  63. justify-content: center;
  64. height: 100vh;
  65. width: 100vw;
  66. overflow: hidden;
  67. @include background-image('background-image');
  68. }
  69. </style>