Bläddra i källkod

fix: 预览页改成hook写法

MTrun 4 år sedan
förälder
incheckning
a45036c935

+ 2 - 2
src/views/preview/components/PreviewRenderList/index.vue

@@ -4,7 +4,7 @@
     :class="animationsClass(item.styles.animations)"
     v-for="(item, index) in localStorageInfo.componentList"
     :key="item.id"
-    :style="{ ...useComponentAttrStyle(item.attr, index), ...useSizeStyle(item.attr), ...useStyle(item.styles)}"
+    :style="{ ...getComponentAttrStyle(item.attr, index), ...getSizeStyle(item.attr), ...getStyle(item.styles)}"
   >
     <component
       :is="item.chartConfig.chartKey"
@@ -19,7 +19,7 @@
 import { PropType, computed } from 'vue'
 import { ChartEditStorageType } from '../../index.d'
 import { chartColors } from '@/settings/chartThemes/index'
-import { useSizeStyle, useStyle, useComponentAttrStyle, animationsClass } from '../../utils'
+import { getSizeStyle, getStyle, getComponentAttrStyle, animationsClass } from '../../utils'
 
 const props = defineProps({
   localStorageInfo: {

+ 28 - 0
src/views/preview/hooks/useComInstall.hook.ts

@@ -0,0 +1,28 @@
+import { ref } from 'vue'
+import { ChartEditStorageType } from '../index.d'
+import { CreateComponentType } from '@/packages/index.d'
+import { fetchChartComponent } from '@/packages/index'
+
+export const useComInstall = (localStorageInfo: ChartEditStorageType) => {
+  const show = ref(false)
+
+  // 注册组件(一开始无法获取window['$vue'])
+  const intervalTiming = setInterval(() => {
+    if (window['$vue'].component) {
+      clearInterval(intervalTiming)
+      show.value = true
+      localStorageInfo.componentList.forEach(async (e: CreateComponentType) => {
+        if (!window['$vue'].component(e.chartConfig.chartKey)) {
+          window['$vue'].component(
+            e.chartConfig.chartKey,
+            fetchChartComponent(e.chartConfig)
+          )
+        }
+      })
+    }
+  }, 200)
+
+  return {
+    show
+  }
+}

+ 31 - 0
src/views/preview/hooks/useScale.hook.ts

@@ -0,0 +1,31 @@
+import { onUnmounted, ref, nextTick, computed } from 'vue'
+import { usePreviewScale } from '@/hooks/index'
+import type { ChartEditStorageType } from '..'
+
+export const useScale = (localStorageInfo: ChartEditStorageType) => {
+
+  const previewRef = ref()
+
+  const width = ref(localStorageInfo?.editCanvasConfig.width)
+  const height = ref(localStorageInfo?.editCanvasConfig.height)
+
+  // 屏幕适配
+  nextTick(() => {
+    const { calcRate, windowResize, unWindowResize } = usePreviewScale(
+      width.value as number,
+      height.value as number,
+      previewRef.value
+    )
+
+    calcRate()
+    windowResize()
+
+    onUnmounted(() => {
+      unWindowResize()
+    })
+  })
+
+  return {
+    previewRef
+  }
+}

+ 12 - 46
src/views/preview/index.vue

@@ -5,64 +5,30 @@
       <!-- 展示层 -->
       <div :style="previewRefStyle" v-if="show">
         <!-- 渲染层 -->
-        <preview-render-list :localStorageInfo="localStorageInfo"></preview-render-list>
+        <preview-render-list
+          :localStorageInfo="localStorageInfo"
+        ></preview-render-list>
       </div>
     </div>
   </div>
 </template>
 
 <script setup lang="ts">
-import { onUnmounted, ref, nextTick, computed } from 'vue'
-import { usePreviewScale } from '@/hooks/index'
-import { PreviewRenderList } from './components/PreviewRenderList/index'
-import { ChartEditStorageType } from './index.d'
-import { useEditCanvasConfigStyle, getSessionStorageInfo } from './utils'
-import { CreateComponentType } from '@/packages/index.d'
-import { fetchChartComponent } from '@/packages/index'
-
-const previewRef = ref()
+import { computed } from 'vue'
+import { PreviewRenderList } from './components/PreviewRenderList'
+import { getEditCanvasConfigStyle, getSessionStorageInfo } from './utils'
+import { useComInstall } from './hooks/useComInstall.hook'
+import { useScale } from './hooks/useScale.hook'
+import type { ChartEditStorageType } from './index.d'
 
 const localStorageInfo: ChartEditStorageType = getSessionStorageInfo() as ChartEditStorageType
 
-const width = ref(localStorageInfo?.editCanvasConfig.width)
-const height = ref(localStorageInfo?.editCanvasConfig.height)
-const show = ref(false)
-
 const previewRefStyle: any = computed(() => {
-  return useEditCanvasConfigStyle(localStorageInfo.editCanvasConfig)
+  return getEditCanvasConfigStyle(localStorageInfo.editCanvasConfig)
 })
 
-// 注册组件(一开始无法获取window['$vue'])
-const intervalTiming = setInterval(() => {
-  if (window['$vue'].component) {
-    clearInterval(intervalTiming)
-    show.value = true
-    localStorageInfo.componentList.forEach(async (e: CreateComponentType) => {
-      if (!window['$vue'].component(e.chartConfig.chartKey)) {
-        window['$vue'].component(
-          e.chartConfig.chartKey,
-          fetchChartComponent(e.chartConfig)
-        )
-      }
-    })
-  }
-}, 200)
-
-// 屏幕适配
-nextTick(() => {
-  const { calcRate, windowResize, unWindowResize } = usePreviewScale(
-    width.value as number,
-    height.value as number,
-    previewRef.value
-  )
-
-  calcRate()
-  windowResize()
-
-  onUnmounted(() => {
-    unWindowResize()
-  })
-})
+const { previewRef } = useScale(localStorageInfo)
+const { show } = useComInstall(localStorageInfo)
 </script>
 
 <style lang="scss" scoped>

+ 4 - 4
src/views/preview/utils/style.ts

@@ -4,7 +4,7 @@ import { EditCanvasConfigType } from '@/store/modules/chartEditStore/chartEditSt
 type AttrType = PickCreateComponentType<'attr'>
 type StylesType = PickCreateComponentType<'styles'>
 
-export const useComponentAttrStyle = (attr: AttrType, index: number) => {
+export const getComponentAttrStyle = (attr: AttrType, index: number) => {
   const componentStyle = {
     zIndex: index + 1,
     left: `${attr.x}px`,
@@ -13,7 +13,7 @@ export const useComponentAttrStyle = (attr: AttrType, index: number) => {
   return componentStyle
 }
 
-export const useSizeStyle = (attr: AttrType, scale?: number) => {
+export const getSizeStyle = (attr: AttrType, scale?: number) => {
   const sizeStyle = {
     width: `${scale ? scale * attr.w : attr.w}px`,
     height: `${scale ? scale * attr.h : attr.h}px`
@@ -21,7 +21,7 @@ export const useSizeStyle = (attr: AttrType, scale?: number) => {
   return sizeStyle
 }
 
-export const useEditCanvasConfigStyle = (canvas: EditCanvasConfigType) => {
+export const getEditCanvasConfigStyle = (canvas: EditCanvasConfigType) => {
   // 背景
   const computedBackground = canvas.selectColor
     ? { background: canvas.background }
@@ -44,7 +44,7 @@ export const animationsClass = (animations: string[]) => {
   return ''
 }
 
-export const useStyle = (styles: StylesType) => {
+export const getStyle = (styles: StylesType) => {
   return {
     // 透明度
     opacity: styles.opacity