useSync.hook.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { getUUID } from '@/utils'
  2. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  3. import { ChartEditStoreEnum, ChartEditStorage } from '@/store/modules/chartEditStore/chartEditStore.d'
  4. import { useChartHistoryStore } from '@/store/modules/chartHistoryStore/chartHistoryStore'
  5. import { fetchChartComponent, fetchConfigComponent, createComponent } from '@/packages/index'
  6. import { CreateComponentType, CreateComponentGroupType, ConfigType } from '@/packages/index.d'
  7. import { PublicGroupConfigClass } from '@/packages/public/publicConfig'
  8. // 请求处理
  9. export const useSync = () => {
  10. const chartEditStore = useChartEditStore()
  11. const chartHistoryStore = useChartHistoryStore()
  12. /**
  13. * * 组件动态注册
  14. * @param projectData 项目数据
  15. * @param isSplace 是否替换数据
  16. * @returns
  17. */
  18. const updateComponent = async (projectData: ChartEditStorage, isSplace = false) => {
  19. if (isSplace) {
  20. // 清除列表
  21. chartEditStore.componentList = []
  22. // 清除历史记录
  23. chartHistoryStore.clearBackStack()
  24. chartHistoryStore.clearForwardStack()
  25. }
  26. // 列表组件注册
  27. projectData.componentList.forEach(async (e: CreateComponentType | CreateComponentGroupType) => {
  28. const intComponent = (target: CreateComponentType) => {
  29. if (!window['$vue'].component(target.chartConfig.chartKey)) {
  30. window['$vue'].component(target.chartConfig.chartKey, fetchChartComponent(target.chartConfig))
  31. window['$vue'].component(target.chartConfig.conKey, fetchConfigComponent(target.chartConfig))
  32. }
  33. }
  34. if (e.isGroup) {
  35. ;(e as CreateComponentGroupType).groupList.forEach(groupItem => {
  36. intComponent(groupItem)
  37. })
  38. } else {
  39. intComponent(e as CreateComponentType)
  40. }
  41. })
  42. // 数据赋值
  43. for (const key in projectData) {
  44. // 组件
  45. if (key === ChartEditStoreEnum.COMPONENT_LIST) {
  46. for (const comItem of projectData[key]) {
  47. // 重新创建是为了处理类种方法消失的问题
  48. const create = async (e: CreateComponentType, callBack?: (e: CreateComponentType) => void) => {
  49. // 补充 class 上的方法
  50. let newComponent: CreateComponentType = await createComponent(e.chartConfig)
  51. if (callBack) {
  52. callBack(Object.assign(newComponent, { ...e, id: getUUID() }))
  53. } else {
  54. chartEditStore.addComponentList(Object.assign(newComponent, { ...e, id: getUUID() }), false, true)
  55. }
  56. }
  57. if (comItem.isGroup) {
  58. // 创建分组
  59. let groupClass = new PublicGroupConfigClass()
  60. groupClass = Object.assign(groupClass, comItem)
  61. // 注册子应用
  62. const targetList: CreateComponentType[] = []
  63. ;(comItem as CreateComponentGroupType).groupList.forEach(groupItem => {
  64. create(groupItem, e => {
  65. targetList.push(e)
  66. })
  67. })
  68. groupClass.groupList = targetList
  69. // 分组插入到列表
  70. chartEditStore.addComponentList(groupClass, false, true)
  71. } else {
  72. create(comItem as CreateComponentType)
  73. }
  74. }
  75. } else {
  76. // 非组件(顺便排除脏数据)
  77. if (key !== 'editCanvasConfig' && key !== 'requestGlobalConfig') return
  78. Object.assign(chartEditStore[key], projectData[key])
  79. }
  80. }
  81. }
  82. return {
  83. updateComponent
  84. }
  85. }