fix: 处理分组预览和动态引入的问题

This commit is contained in:
奔跑的面条 2022-08-12 20:39:20 +08:00
parent 57798b9000
commit e380ead651
6 changed files with 137 additions and 35 deletions

View File

@ -23,7 +23,9 @@
> >
<!-- 图表 --> <!-- 图表 -->
<div v-for="(item, index) in chartEditStore.getComponentList" :key="item.id"> <div v-for="(item, index) in chartEditStore.getComponentList" :key="item.id">
<EditGroup v-if="item.isGroup" :groupData="item" :groupIndex="index"> </EditGroup> <!-- 分组 -->
<edit-group v-if="item.isGroup" :groupData="item" :groupIndex="index"></edit-group>
<!-- 单组件 --> <!-- 单组件 -->
<edit-shape-box <edit-shape-box
v-if="!item.isGroup" v-if="!item.isGroup"

View File

@ -4,6 +4,7 @@ import { ChartEditStoreEnum, ChartEditStorage } from '@/store/modules/chartEditS
import { useChartHistoryStore } from '@/store/modules/chartHistoryStore/chartHistoryStore' import { useChartHistoryStore } from '@/store/modules/chartHistoryStore/chartHistoryStore'
import { fetchChartComponent, fetchConfigComponent, createComponent } from '@/packages/index' import { fetchChartComponent, fetchConfigComponent, createComponent } from '@/packages/index'
import { CreateComponentType, CreateComponentGroupType, ConfigType } from '@/packages/index.d' import { CreateComponentType, CreateComponentGroupType, ConfigType } from '@/packages/index.d'
import { PublicGroupConfigClass } from '@/packages/public/publicConfig'
// 请求处理 // 请求处理
export const useSync = () => { export const useSync = () => {
@ -14,7 +15,7 @@ export const useSync = () => {
* * * *
* @param projectData * @param projectData
* @param isSplace * @param isSplace
* @returns * @returns
*/ */
const updateComponent = async (projectData: ChartEditStorage, isSplace = false) => { const updateComponent = async (projectData: ChartEditStorage, isSplace = false) => {
if (isSplace) { if (isSplace) {
@ -26,18 +27,19 @@ export const useSync = () => {
} }
// 列表组件注册 // 列表组件注册
projectData.componentList.forEach(async (e: CreateComponentType | CreateComponentGroupType) => { projectData.componentList.forEach(async (e: CreateComponentType | CreateComponentGroupType) => {
// 排除分组 const intComponent = (target: CreateComponentType) => {
if (e.isGroup) return if (!window['$vue'].component(target.chartConfig.chartKey)) {
const target = e as CreateComponentType window['$vue'].component(target.chartConfig.chartKey, fetchChartComponent(target.chartConfig))
if (!window['$vue'].component(target.chartConfig.chartKey)) { window['$vue'].component(target.chartConfig.conKey, fetchConfigComponent(target.chartConfig))
window['$vue'].component( }
target.chartConfig.chartKey, }
fetchChartComponent(target.chartConfig)
) if (e.isGroup) {
window['$vue'].component( ;(e as CreateComponentGroupType).groupList.forEach(groupItem => {
target.chartConfig.conKey, intComponent(groupItem)
fetchConfigComponent(target.chartConfig) })
) } else {
intComponent(e as CreateComponentType)
} }
}) })
// 数据赋值 // 数据赋值
@ -45,15 +47,37 @@ export const useSync = () => {
// 组件 // 组件
if (key === ChartEditStoreEnum.COMPONENT_LIST) { if (key === ChartEditStoreEnum.COMPONENT_LIST) {
for (const comItem of projectData[key]) { for (const comItem of projectData[key]) {
// 补充 class 上的方法
let newComponent: CreateComponentType = await createComponent( // 重新创建是为了处理类种方法消失的问题
comItem.chartConfig as ConfigType const create = async (e: CreateComponentType, callBack?: (e: CreateComponentType) => void) => {
) // 补充 class 上的方法
chartEditStore.addComponentList( let newComponent: CreateComponentType = await createComponent(e.chartConfig as ConfigType)
Object.assign(newComponent, {...comItem, id: getUUID()}), if (callBack) {
false, callBack(Object.assign(newComponent, { ...e, id: getUUID() }))
true } else {
) chartEditStore.addComponentList(Object.assign(newComponent, { ...e, id: getUUID() }), false, true)
}
}
if (comItem.isGroup) {
// 创建分组
let groupClass = new PublicGroupConfigClass()
groupClass = Object.assign(groupClass, comItem)
// 注册子应用
const targetList: CreateComponentType[] = []
;(comItem as CreateComponentGroupType).groupList.forEach(groupItem => {
create(groupItem, e => {
targetList.push(e)
})
})
groupClass.groupList = targetList
// 分组插入到列表
chartEditStore.addComponentList(groupClass, false, true)
} else {
create(comItem as CreateComponentType)
}
} }
} else { } else {
// 非组件(顺便排除脏数据) // 非组件(顺便排除脏数据)
@ -66,4 +90,4 @@ export const useSync = () => {
return { return {
updateComponent updateComponent
} }
} }

View File

@ -0,0 +1,3 @@
import PreviewRenderGroup from './index.vue'
export { PreviewRenderGroup }

View File

@ -0,0 +1,55 @@
<template>
<div
class="chart-item"
v-for="item in groupData.groupList"
:class="animationsClass(item.styles.animations)"
:key="item.id"
:style="{
...getComponentAttrStyle(item.attr, groupIndex),
...getFilterStyle(item.styles),
...getTransformStyle(item.styles)
}"
>
<component
:is="item.chartConfig.chartKey"
:chartConfig="item"
:themeSetting="themeSetting"
:themeColor="themeColor"
:style="{...getSizeStyle(item.attr)}"
></component>
</div>
</template>
<script setup lang="ts">
import { PropType } from 'vue'
import { CreateComponentGroupType } from '@/packages/index.d'
import { animationsClass, getFilterStyle, getTransformStyle } from '@/utils'
import { getSizeStyle, getComponentAttrStyle } from '../../utils'
const props = defineProps({
groupData: {
type: Object as PropType<CreateComponentGroupType>,
required: true
},
themeSetting: {
type: Object,
required: true
},
themeColor: {
type: Object,
required: true
},
groupIndex: {
type: Number,
required: true
}
})
console.log(props.groupData)
</script>
<style lang="scss" scoped>
.chart-item {
position: absolute;
}
</style>

View File

@ -1,21 +1,31 @@
<template> <template>
<div <div
class="chart-item" class="chart-item"
:class="animationsClass(item.styles.animations)"
v-for="(item, index) in localStorageInfo.componentList" v-for="(item, index) in localStorageInfo.componentList"
:class="animationsClass(item.styles.animations)"
:key="item.id" :key="item.id"
:style="{ :style="{
...getComponentAttrStyle(item.attr, index), ...getComponentAttrStyle(item.attr, index),
...getFilterStyle(item.styles), ...getFilterStyle(item.styles),
...getTransformStyle(item.styles) ...getTransformStyle(item.styles)
}" }"
> >
<!-- 分组 -->
<preview-render-group
v-if="item.isGroup"
:groupData="item"
:groupIndex="index"
:themeSetting="themeSetting"
:themeColor="themeColor"
></preview-render-group>
<!-- 单组件 -->
<component <component
:is="item.chartConfig.chartKey" :is="item.chartConfig.chartKey"
:chartConfig="item" :chartConfig="item"
:themeSetting="themeSetting" :themeSetting="themeSetting"
:themeColor="themeColor" :themeColor="themeColor"
:style="{...getSizeStyle(item.attr)}" :style="{ ...getSizeStyle(item.attr) }"
></component> ></component>
</div> </div>
</template> </template>
@ -23,6 +33,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { PropType, computed } from 'vue' import { PropType, computed } from 'vue'
import { ChartEditStorageType } from '../../index.d' import { ChartEditStorageType } from '../../index.d'
import { PreviewRenderGroup } from '../PreviewRenderGroup/index'
import { chartColors } from '@/settings/chartThemes/index' import { chartColors } from '@/settings/chartThemes/index'
import { animationsClass, getFilterStyle, getTransformStyle } from '@/utils' import { animationsClass, getFilterStyle, getTransformStyle } from '@/utils'
import { getSizeStyle, getComponentAttrStyle } from '../../utils' import { getSizeStyle, getComponentAttrStyle } from '../../utils'
@ -45,7 +56,6 @@ const themeColor = computed(() => {
const chartThemeColor = props.localStorageInfo.editCanvasConfig.chartThemeColor const chartThemeColor = props.localStorageInfo.editCanvasConfig.chartThemeColor
return chartColors[chartThemeColor] return chartColors[chartThemeColor]
}) })
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -1,6 +1,6 @@
import { ref } from 'vue' import { ref } from 'vue'
import { ChartEditStorageType } from '../index.d' import { ChartEditStorageType } from '../index.d'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
import { fetchChartComponent } from '@/packages/index' import { fetchChartComponent } from '@/packages/index'
export const useComInstall = (localStorageInfo: ChartEditStorageType) => { export const useComInstall = (localStorageInfo: ChartEditStorageType) => {
@ -10,12 +10,20 @@ export const useComInstall = (localStorageInfo: ChartEditStorageType) => {
const intervalTiming = setInterval(() => { const intervalTiming = setInterval(() => {
if (window['$vue'].component) { if (window['$vue'].component) {
clearInterval(intervalTiming) clearInterval(intervalTiming)
localStorageInfo.componentList.forEach(async (e: CreateComponentType) => {
if (!window['$vue'].component(e.chartConfig.chartKey)) { const intComponent = (target: CreateComponentType) => {
window['$vue'].component( if (!window['$vue'].component(target.chartConfig.chartKey)) {
e.chartConfig.chartKey, window['$vue'].component(target.chartConfig.chartKey, fetchChartComponent(target.chartConfig))
fetchChartComponent(e.chartConfig) }
) }
localStorageInfo.componentList.forEach(async (e: CreateComponentType | CreateComponentGroupType) => {
if (e.isGroup) {
(e as CreateComponentGroupType).groupList.forEach(groupItem => {
intComponent(groupItem)
})
} else {
intComponent(e as CreateComponentType)
} }
}) })
show.value = true show.value = true