wangrong
2026-08-30 4c949c0dc9bcae785c281c9d8125295f09810b15
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
import { deepCopy } from './utils'
import { divide, multiply } from 'mathjs'
import { dvMainStoreWithOut } from '@/store/modules/data-visualization/dvMain'
import { storeToRefs } from 'pinia'
import { groupItemStyleAdaptor, groupSizeStyleAdaptor } from '@/utils/style'
import { nextTick } from 'vue'
 
const dvMainStore = dvMainStoreWithOut()
const { componentData, curComponentIndex, canvasStyleData } = storeToRefs(dvMainStore)
 
const needToChangeAttrs = [
  'top',
  'left',
  'width',
  'height',
  'fontSize',
  'activeFontSize',
  'letterSpacing'
]
const needToChangeDirectionAttrs = {
  width: ['left', 'width', 'fontSize', 'activeFontSize', 'letterSpacing'],
  height: ['top', 'height']
}
 
export function changeSizeWithScaleAdaptor(scale) {
  return changeComponentsSizeWithScale(scale)
}
 
export function changeSizeWithScale(scale) {
  return changeComponentsSizeWithScale(scale)
}
 
function changeComponentsSizeWithScaleCircle(componentDataCopy, scale) {
  if (!componentDataCopy || !Array.isArray(componentDataCopy)) {
    return
  }
  componentDataCopy?.forEach(component => {
    if (component.style) {
      Object.keys(component.style)?.forEach(key => {
        if (needToChangeDirectionAttrs.width.includes(key)) {
          // 根据原来的比例获取样式原来的尺寸
          // 再用原来的尺寸 * 现在的比例得出新的尺寸
          if (!!component.style[key]) {
            component.style[key] = format(
              getOriginStyle(component.style[key], canvasStyleData.value.scale),
              scale
            )
          }
        } else if (needToChangeDirectionAttrs.height.includes(key)) {
          // 根据原来的比例获取样式原来的尺寸
          // 再用原来的尺寸 * 现在的比例得出新的尺寸
          component.style[key] = format(
            getOriginStyle(component.style[key], canvasStyleData.value.scaleHeight),
            scale
          )
        }
      })
    }
    if (['Group'].includes(component.component)) {
      groupSizeStyleAdaptor(component)
      const parentStyle = component.style
      component.propValue.forEach(componentInner => {
        if (['DeTabs'].includes(componentInner.component)) {
          componentInner.propValue.forEach(tabItem => {
            changeComponentsSizeWithScaleCircle(tabItem.componentData, scale)
          })
        } else {
          changeComponentsSizeWithScaleCircle(componentInner.propValue, scale)
          groupItemStyleAdaptor(componentInner, parentStyle)
        }
      })
    } else if (['DeTabs'].includes(component.component)) {
      component.propValue.forEach(tabItem => {
        changeComponentsSizeWithScaleCircle(tabItem.componentData, scale)
      })
    }
    // 如果是分组组件 则要进行分组内部组件groupStyle进行深度计算
    // 计算逻辑 Group 中样式 * groupComponent.groupStyle[sonKey].
    if (['Group', 'DeTabs'].includes(component.component)) {
      try {
        nextTick(() => groupSizeStyleAdaptor(component))
      } catch (e) {
        // 旧Group适配
        console.error('group adaptor error:' + e)
      }
    }
  })
}
 
function changeComponentsSizeWithScale(scale) {
  const componentDataCopy = deepCopy(componentData.value)
  changeComponentsSizeWithScaleCircle(componentDataCopy, scale)
  dvMainStore.setComponentData(componentDataCopy)
  // 更新画布数组后,需要重新设置当前组件,否则在改变比例后,直接拖动圆点改变组件大小不会生效
  dvMainStore.setCurComponent({
    component: componentData.value[curComponentIndex.value],
    index: curComponentIndex.value
  })
 
  // 分开保存初始化宽高比例
  dvMainStore.setCanvasStyle({
    ...canvasStyleData.value,
    scaleWidth: scale,
    scaleHeight: scale,
    scale
  })
}
export function changeRefComponentsSizeWithScale(componentDataRef, canvasStyleDataRef, scale) {
  componentDataRef.forEach(component => {
    Object.keys(component.style).forEach(key => {
      if (needToChangeAttrs.includes(key)) {
        if (key === 'fontSize' && component.style[key] === '') return
        // 根据原来的比例获取样式原来的尺寸
        // 再用原来的尺寸 * 现在的比例得出新的尺寸
        component.style[key] = format(
          getOriginStyle(component.style[key], canvasStyleDataRef.scale),
          scale
        )
      }
    })
  })
  canvasStyleDataRef.scale = scale
}
 
export function changeRefComponentsSizeWithScalePointCircle(
  componentDataRef,
  canvasStyleDataRef,
  scaleWidth,
  scaleHeight,
  outScale
) {
  componentDataRef.forEach(component => {
    Object.keys(component.style)?.forEach(key => {
      if (needToChangeDirectionAttrs.width.includes(key)) {
        // 根据原来的比例获取样式原来的尺寸
        // 再用原来的尺寸 * 现在的比例得出新的尺寸
        component.style[key] = format(
          getOriginStyle(component.style[key], canvasStyleDataRef.scale || outScale),
          scaleWidth
        )
      } else if (needToChangeDirectionAttrs.height.includes(key)) {
        // 根据原来的比例获取样式原来的尺寸
        // 再用原来的尺寸 * 现在的比例得出新的尺寸
        component.style[key] = format(
          getOriginStyle(component.style[key], canvasStyleDataRef.scaleHeight || outScale),
          scaleHeight
        )
      }
    })
    if (component.component === 'Group') {
      changeRefComponentsSizeWithScalePointCircle(
        component.propValue,
        canvasStyleDataRef,
        scaleWidth,
        scaleHeight,
        outScale
      )
    }
  })
}
 
export function changeRefComponentsSizeWithScalePoint(
  componentDataRef,
  canvasStyleDataRef,
  scaleWidth,
  scaleHeight,
  outScale
) {
  changeRefComponentsSizeWithScalePointCircle(
    componentDataRef,
    canvasStyleDataRef,
    scaleWidth,
    scaleHeight,
    outScale
  )
  canvasStyleDataRef.scale = scaleWidth
  canvasStyleDataRef.scaleWidth = scaleWidth
  canvasStyleDataRef.scaleHeight = scaleHeight
}
 
const needToChangeAttrs2 = ['width', 'height', 'fontSize']
export function changeComponentSizeWithScale(component, scale = canvasStyleData.value.scale) {
  Object.keys(component.style).forEach(key => {
    if (needToChangeAttrs2.includes(key)) {
      if (key === 'fontSize' && component.style[key] === '') return
      component.style[key] = format(component.style[key], scale)
    }
  })
}
 
function format(value, scale) {
  return multiply(value, divide(parseFloat(scale), 100))
}
 
function getOriginStyle(value = 0, scale) {
  if (!value) {
    value = 0
  }
  return divide(value, divide(parseFloat(scale), 100))
}