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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { sin, cos, toPercent } from '@/utils/translate'
import { imgUrlTrans } from '@/utils/imgUtils'
import { hexColorToRGBA } from '@/views/chart/components/js/util'
import { dvMainStoreWithOut } from '@/store/modules/data-visualization/dvMain'
import { isMainCanvas, isTabCanvas } from '@/utils/canvasUtils'
const dvMainStore = dvMainStoreWithOut()
export function getShapeStyle(style) {
  const result = {}
  ;['width', 'height', 'top', 'left', 'rotate'].forEach(attr => {
    if (attr != 'rotate') {
      result[attr] = style[attr] + 'px'
    } else {
      result['transform'] = 'rotate(' + style[attr] + 'deg)'
    }
  })
 
  return result
}
 
export function getShapeItemStyle(
  item,
  { dvModel, cellWidth, cellHeight, curGap, showPosition = 'preview' }
) {
  let result = {}
  if (dvModel === 'dashboard' && !item['isPlayer']) {
    result = {
      padding: curGap + 'px!important',
      width: cellWidth * item.sizeX + 'px',
      height: cellHeight * item.sizeY + 'px',
      left: cellWidth * (item.x - 1) + 'px',
      top: cellHeight * (item.y - 1) + 'px'
    }
  } else if (dvModel === 'dataV' && isTabCanvas(item.canvasId) && showPosition === 'preview') {
    result = {
      padding: curGap + 'px!important',
      width: toPercent(item.groupStyle.width),
      height: toPercent(item.groupStyle.height),
      top: toPercent(item.groupStyle.top),
      left: toPercent(item.groupStyle.left)
    }
  } else {
    result = {
      padding: curGap + 'px!important',
      width: item.style.width + 'px',
      height: item.style.height + 'px',
      left: item.style.left + 'px',
      top: item.style.top + 'px'
    }
  }
 
  return result
}
 
export function syncShapeItemStyle(item, cellWidth, cellHeight) {
  item.style.left = cellWidth * (item.x - 1)
  item.style.top = cellHeight * (item.y - 1)
  item.style.width = cellWidth * item.sizeX
  item.style.height = cellHeight * item.sizeY
}
 
const needUnit = [
  'fontSize',
  'width',
  'height',
  'top',
  'left',
  'borderWidth',
  'letterSpacing',
  'borderRadius'
]
 
export function getSVGStyle(style, filter = []) {
  const result = {}
 
  ;[
    'opacity',
    'width',
    'height',
    'top',
    'left',
    'rotate',
    'fontSize',
    'fontWeight',
    'lineHeight',
    'letterSpacing',
    'textAlign',
    'color'
  ].forEach(key => {
    if (!filter.includes(key)) {
      if (key != 'rotate') {
        if (style[key] !== '') {
          result[key] = style[key]
 
          if (needUnit.includes(key)) {
            result[key] += 'px'
          }
        }
      } else {
        result['transform'] = key + '(' + style[key] + 'deg)'
      }
    }
  })
 
  return result
}
 
export function getItemAllStyle(item, filter = []) {
  const style = item.style
  const commonBackground = item.commonBackground
  const result = {}
  Object.keys(style).forEach(key => {
    if (!filter.includes(key)) {
      if (key != 'rotate') {
        if (style[key] !== '') {
          result[key] = style[key]
 
          if (needUnit.includes(key)) {
            result[key] += 'px'
          }
        }
      } else {
        result['transform'] = key + '(' + style[key] + 'deg)'
      }
    }
 
    if (commonBackground) {
      //附加背景样式
      let colorRGBA = ''
      if (commonBackground.backgroundColorSelect) {
        colorRGBA = hexColorToRGBA(commonBackground.backgroundColor, commonBackground.alpha)
      }
      if (commonBackground.backgroundImageEnable) {
        if (
          commonBackground.backgroundType === 'outerImage' &&
          typeof commonBackground.outerImage === 'string'
        ) {
          result['background'] = `url(${imgUrlTrans(
            commonBackground.outerImage
          )}) no-repeat ${colorRGBA}`
        } else {
          result['background-color'] = colorRGBA
        }
      } else {
        result['background-color'] = colorRGBA
      }
    }
  })
 
  return result
}
 
export function getStyle(style, filter = []) {
  const result = {}
  Object.keys(style).forEach(key => {
    if (!filter.includes(key)) {
      if (key != 'rotate') {
        if (style[key] !== '') {
          result[key] = style[key]
 
          if (needUnit.includes(key)) {
            result[key] += 'px'
          }
        }
      } else {
        result['transform'] = key + '(' + style[key] + 'deg)'
      }
    }
  })
  return result
}
 
// 获取一个组件旋转 rotate 后的样式
export function getComponentRotatedStyle(style) {
  style = { ...style }
  if (style.rotate != 0) {
    const newWidth = style.width * cos(style.rotate) + style.height * sin(style.rotate)
    const diffX = (style.width - newWidth) / 2 // 旋转后范围变小是正值,变大是负值
    style.left += diffX
    style.right = style.left + newWidth
 
    const newHeight = style.height * cos(style.rotate) + style.width * sin(style.rotate)
    const diffY = (newHeight - style.height) / 2 // 始终是正
    style.top -= diffY
    style.bottom = style.top + newHeight
 
    style.width = newWidth
    style.height = newHeight
  } else {
    style.bottom = style.top + style.height
    style.right = style.left + style.width
  }
 
  return style
}
 
export function getCanvasStyle(canvasStyleData, canvasId = 'canvas-main') {
  const {
    backgroundColorSelect,
    background,
    backgroundColor,
    backgroundImageEnable,
    fontSize,
    mobileSetting,
    fontFamily
  } = canvasStyleData
  const style = { fontSize: fontSize + 'px', color: canvasStyleData.color }
  if (isMainCanvas(canvasId)) {
    // 仪表板默认色#f5f6f7 大屏默认配色 #1a1a1a
    let colorRGBA = dvMainStore.dvInfo.type === 'dashboard' ? '#f5f6f7' : '#1a1a1a'
    if (backgroundColorSelect && backgroundColor) {
      colorRGBA = backgroundColor
    }
    if (backgroundImageEnable) {
      style['background'] = `url(${imgUrlTrans(background)}) no-repeat ${colorRGBA}`
    } else {
      style['background-color'] = colorRGBA
    }
 
    if (dvMainStore.mobileInPc && mobileSetting?.customSetting) {
      const { backgroundColorSelect, color, backgroundImageEnable, background } = mobileSetting
      if (backgroundColorSelect && backgroundImageEnable && typeof background === 'string') {
        style['background'] = `url(${imgUrlTrans(background)}) no-repeat ${color}`
      } else if (backgroundColorSelect) {
        style['background-color'] = color
      } else if (backgroundImageEnable) {
        style['background'] = `url(${imgUrlTrans(background)}) no-repeat`
      }
    }
    style['font-family'] = fontFamily + '!important'
  }
 
  return style
}
 
export function createGroupStyle(groupComponent) {
  const parentStyle = groupComponent.style
  groupComponent.propValue.forEach(component => {
    // 分组计算逻辑
    // 1.groupStyle记录left top width height 在出现分组缩放的时候进行等比例变更(缩放来源有两种a.整个大屏的缩放 b.分组尺寸的调整)
    // 2.component 内部进行位移或者尺寸的变更 要同步到这个比例中
    const style = { ...component.style }
    component.groupStyle.left = (style.left - parentStyle.left) / parentStyle.width
    component.groupStyle.top = (style.top - parentStyle.top) / parentStyle.height
    component.groupStyle.width = style.width / parentStyle.width
    component.groupStyle.height = style.height / parentStyle.height
 
    component.style.left = component.style.left - parentStyle.left
    component.style.top = component.style.top - parentStyle.top
  })
}
 
function dataVTabSizeStyleAdaptor(tabComponent) {
  const parentStyleAdaptor = { ...tabComponent.style }
  const offset = parentStyleAdaptor.showTabTitle ? 46 : 0
  const domId =
    dvMainStore.editMode === 'edit'
      ? 'component' + tabComponent.id
      : 'enlarge-inner-content' + tabComponent.id
  const tabDom = document.getElementById(domId)
  if (tabDom) {
    parentStyleAdaptor.height = tabDom.clientHeight - offset
    parentStyleAdaptor.width = tabDom.clientWidth
  } else {
    parentStyleAdaptor.height = parentStyleAdaptor.height - offset
  }
 
  tabComponent.propValue?.forEach(tabItem => {
    tabItem.componentData?.forEach(tabComponent => {
      groupItemStyleAdaptor(tabComponent, parentStyleAdaptor)
      if (['Group'].includes(tabComponent.component)) {
        groupSizeStyleAdaptor(tabComponent)
      }
    })
  })
}
 
export function groupItemStyleAdaptor(component, parentStyle) {
  // 分组还原逻辑
  // 当发上分组缩放是,要将内部组件按照比例转换
  const styleScale = component.groupStyle
  component.style.left = parentStyle.width * styleScale.left
  component.style.top = parentStyle.height * styleScale.top
  component.style.width = parentStyle.width * styleScale.width
  component.style.height = parentStyle.height * styleScale.height
}
 
export function groupStyleRevertBatch(groupComponent, parentStyle) {
  if (groupComponent.component === 'DeTabs') {
    groupComponent.propValue?.forEach(tabItem => {
      tabItem.componentData?.forEach(tabComponent => {
        groupStyleRevert(tabComponent, parentStyle)
      })
    })
  }
}
 
export function tabInnerStyleRevert(tabOuterComponent) {
  const parentStyle = {
    width: tabOuterComponent.style.width,
    height: tabOuterComponent.style.height - (tabOuterComponent.style.showTabTitle ? 46 : 0)
  }
  tabOuterComponent.propValue?.forEach(tabItem => {
    tabItem.componentData?.forEach(tabComponent => {
      groupStyleRevert(tabComponent, parentStyle)
    })
  })
}
 
export function groupStyleRevert(innerComponent, parentStyle) {
  const innerStyle = { ...innerComponent.style }
  innerComponent.groupStyle.left = innerStyle.left / parentStyle.width
  innerComponent.groupStyle.top = innerStyle.top / parentStyle.height
  innerComponent.groupStyle.width = innerStyle.width / parentStyle.width
  innerComponent.groupStyle.height = innerStyle.height / parentStyle.height
}
 
export function groupSizeStyleAdaptor(groupComponent) {
  if (groupComponent.component === 'Group') {
    const parentStyle = groupComponent.style
    groupComponent.propValue.forEach(component => {
      groupItemStyleAdaptor(component, parentStyle)
    })
  } else {
    dataVTabSizeStyleAdaptor(groupComponent)
  }
}
 
export function dataVTabComponentAdd(innerComponent, parentComponent) {
  //do dataVTabComponentAdd
  innerComponent.style.top = 0
  innerComponent.style.left = 0
  const parentStyleAdaptor = { ...parentComponent.style }
  // 去掉tab头部高度
  parentStyleAdaptor.height = parentStyleAdaptor.height - (parentComponent.showTabTitle ? 46 : 0)
  groupStyleRevert(innerComponent, parentStyleAdaptor)
}