wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { defineStore, storeToRefs } from 'pinia'
import { store } from '../../index'
import { dvMainStoreWithOut } from './dvMain'
import { $, deepCopy } from '@/utils/utils'
import decomposeComponent from '@/utils/decomposeComponent'
import { generateID } from '@/utils/generateID'
import {
  commonStyle,
  commonAttr,
  COMMON_COMPONENT_BACKGROUND_MAP
} from '@/custom-component/component-list'
import { createGroupStyle, getComponentRotatedStyle } from '@/utils/style'
import eventBus from '@/utils/eventBus'
import { canvasIdMapCheck, checkJoinGroup, isTabCanvas } from '@/utils/canvasUtils'
import { useI18n } from '@/hooks/web/useI18n'
const { t } = useI18n()
 
const dvMainStore = dvMainStoreWithOut()
const { curComponent, componentData, curOriginThemes } = storeToRefs(dvMainStore)
 
export const composeStore = defineStore('compose', {
  state: () => {
    return {
      areaData: {
        // 选中区域包含的组件以及区域位移信息
        style: {
          top: 0,
          left: 0,
          width: 0,
          height: 0
        },
        components: []
      },
      editorMap: {},
      isCtrlOrCmdDown: false,
      isSpaceDown: false,
      isShiftDown: false,
      laterIndex: null, //最后点击组件的索引
      editor: null
    }
  },
  actions: {
    getEditor(canvasId = 'canvas-main') {
      this.editorMap[canvasId] = $('#editor-' + canvasId)
    },
 
    setLaterIndex(value) {
      this.laterIndex = value
    },
    setSpaceDownStatus(value) {
      this.isSpaceDown = value
    },
    setIsCtrlOrCmdDownStatus(value) {
      this.isCtrlOrCmdDown = value
    },
    setIsShiftDownStatus(value) {
      this.isShiftDown = value
    },
    setAreaData(data) {
      this.areaData = data
    },
    updateGroupBorder(canvasId) {
      if (canvasId) {
        // 1.查找所属分组
        const groupId = canvasId.replace('Group-', '')
        const sourceGroupComponent = componentData.value.filter(ele => ele.id === groupId)[0]
        const sourceSubComponents = sourceGroupComponent.propValue
        // 2. 还原分组内部组件再主画布位置
        const sourceParentStyle = { ...sourceGroupComponent.style }
        sourceSubComponents.forEach(subcomponent => {
          decomposeComponent(subcomponent, null, sourceParentStyle)
        })
        const newAreaData = {
          // 选中区域包含的组件以及区域位移信息
          style: {
            top: 0,
            left: 0,
            width: 0,
            height: 0
          },
          components: sourceSubComponents
        }
        // 3.重新计算分组区域边界
        this.calcComposeArea(newAreaData)
        sourceGroupComponent.style = {
          ...sourceGroupComponent.style,
          ...newAreaData.style
        }
        sourceSubComponents.forEach(component => {
          component.canvasId = canvasId
        })
        // 4.计算内部子组件位置
        createGroupStyle(sourceGroupComponent)
      }
    },
 
    alignment: function (params) {
      const { areaData } = this
      if (areaData.components.length === 1) {
        // 一个组件不进行组合直接释放
        areaData.components = []
        return
      }
      if (areaData.components.length > 0 && areaData.style.width === 0) {
        // 计算组合区域
        this.calcComposeArea()
      }
      const { left, top, width, height } = areaData.style
      const areaRight = left + width
      const areaTransverseCenter = left + width / 2 // 横向中心点
      const areaBottom = top + height
      const areaDirectionCenter = top + height / 2 // 纵向中心点
 
      areaData.components.forEach(component => {
        if (params === 'left') {
          // 居左
          component.style.left = left
        } else if (params === 'right') {
          // 居右
          component.style.left = areaRight - component.style.width
        } else if (params === 'top') {
          // 居上
          component.style.top = top
        } else if (params === 'bottom') {
          // 居下
          component.style.top = areaBottom - component.style.height
        } else if (params === 'transverse') {
          // 横向居中
          component.style.left = areaTransverseCenter - component.style.width / 2
        } else if (params === 'direction') {
          // 纵向
          component.style.top = areaDirectionCenter - component.style.height / 2
        }
      })
    },
 
    compose: function (canvasId = 'canvas-main') {
      const editor = this.editorMap[canvasId]
      const { areaData } = this
      if (areaData.components.length === 1) {
        // 一个组件不进行组合直接释放
        areaData.components = []
        return
      }
      if (areaData.components.length > 0) {
        // 计算组合区域
        this.calcComposeArea()
      }
 
      const components = []
      areaData.components.forEach(component => {
        if (['Group'].includes(component.component)) {
          // 如果要组合的组件中,已经存在组合数据,则需要提前拆分
          const parentStyle = { ...component.style }
          const subComponents = component.propValue
          const editorRect = editor.getBoundingClientRect()
 
          subComponents.forEach(component => {
            decomposeComponent(component, editorRect, parentStyle)
          })
 
          components.push(...component.propValue)
        } else if (['GroupArea'].includes(component.component)) {
          // do nothing GroupAreas组合视阔区 DeTabs 均不加入分组中
        } else if (checkJoinGroup(component)) {
          components.push(component)
        }
      })
 
      const newId = generateID()
      components.forEach(component => {
        component.canvasId = 'Group-' + newId
      })
      const groupComponent = deepCopy({
        id: newId,
        component: 'Group',
        canvasActive: false,
        name: t('visualization.view_group'),
        label: t('visualization.view_group'),
        icon: 'group',
        expand: true,
        commonBackground: {
          ...COMMON_COMPONENT_BACKGROUND_MAP[curOriginThemes.value],
          backgroundColorSelect: false,
          innerPadding: 0
        },
        ...commonAttr,
        style: {
          ...commonStyle,
          ...areaData.style
        },
        propValue: components
      })
 
      createGroupStyle(groupComponent)
      dvMainStore.addComponent({
        component: groupComponent,
        index: undefined
      })
 
      eventBus.emit('hideArea-canvas-main')
      this.batchDeleteComponent(areaData.components)
      dvMainStore.setCurComponent({
        component: componentData.value[componentData.value.length - 1],
        index: componentData.value.length - 1
      })
 
      this.areaData.components = []
    },
 
    // 将已经放到 Group 组件数据删除,也就是在 componentData 中删除,因为它们已经从 componentData 挪到 Group 组件中了
    batchDeleteComponent(deleteData) {
      deleteData.forEach(component => {
        if (!['GroupArea'].includes(component.component)) {
          for (let i = 0, len = componentData.value.length; i < len; i++) {
            if (component.id == componentData.value[i].id) {
              componentData.value.splice(i, 1)
              break
            }
          }
        }
      })
    },
 
    decompose() {
      const canvasId = curComponent.value.canvasId
      const editor = this.editorMap[canvasId]
      if (!editor) {
        return
      }
      const parentStyle = { ...curComponent.value.style }
      const components = curComponent.value.propValue
      const editorRect = editor.getBoundingClientRect()
      const isInTab = isTabCanvas(canvasId)
      let decomposeComponentData = componentData.value
      let parentGroupStyle = null
      if (isInTab) {
        const pathMap = {}
        componentData.value.forEach(componentItem => {
          canvasIdMapCheck(componentItem, null, pathMap)
        })
        const pComponent = pathMap[curComponent.value.id]
        const pComponentTarget = pComponent.propValue.filter(
          item => canvasId.indexOf(item.name) > -1
        )
        if (pComponentTarget && pComponentTarget.length > 0) {
          decomposeComponentData = pComponentTarget[0].componentData
        }
        parentGroupStyle = curComponent.value.groupStyle
      }
      dvMainStore.deleteComponentById(curComponent.value.id, decomposeComponentData)
      components.forEach(component => {
        decomposeComponent(component, editorRect, parentStyle, canvasId, parentGroupStyle)
        dvMainStore.addComponent({
          component: component,
          index: undefined,
          isFromGroup: true,
          componentData: decomposeComponentData
        })
      })
    },
    calcComposeArea(areaDataValue = this.areaData) {
      if (areaDataValue.components <= 1) {
        return
      }
      // 根据选中区域和区域中每个组件的位移信息来创建 Group 组件
      // 要遍历选择区域的每个组件,获取它们的 left top right bottom 信息来进行比较
      let top = Infinity,
        left = Infinity
      let right = -Infinity,
        bottom = -Infinity
      areaDataValue.components.forEach(component => {
        let style = { left: 0, top: 0, right: 0, bottom: 0 }
        style = getComponentRotatedStyle(component.style)
 
        if (style.left < left) left = style.left
        if (style.top < top) top = style.top
        if (style.right > right) right = style.right
        if (style.bottom > bottom) bottom = style.bottom
      })
 
      // 设置选中区域位移大小信息和区域内的组件数据
      areaDataValue.style = {
        left,
        top,
        width: right - left,
        height: bottom - top
      }
    }
  }
})
 
export const composeStoreWithOut = () => {
  return composeStore(store)
}