@ecan-bi/tools 1.0.80 → 1.0.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,721 @@
1
+ /**
2
+ * 画布交互纯函数
3
+ * 不依赖 DOM / Vue / 业务 store,可被任意大屏设计器项目复用
4
+ */
5
+
6
+ export interface CanvasSizeValue {
7
+ value: number
8
+ suffix: string
9
+ }
10
+
11
+ export interface CanvasComponent {
12
+ id?: string
13
+ left?: string | number | CanvasSizeValue
14
+ top?: string | number | CanvasSizeValue
15
+ width?: string | number | CanvasSizeValue
16
+ height?: string | number | CanvasSizeValue
17
+ combinationId?: string
18
+ parentId?: string
19
+ isLimitMoveArea?: boolean
20
+ isForbidContain?: boolean
21
+ [key: string]: any
22
+ }
23
+
24
+ export interface CanvasRect {
25
+ left: number
26
+ top: number
27
+ width: number
28
+ height: number
29
+ }
30
+
31
+ export interface AlignItem {
32
+ id: string
33
+ type: 'normal' | 'combinate'
34
+ left: number
35
+ top: number
36
+ width: number
37
+ height: number
38
+ }
39
+
40
+ export type AlignType =
41
+ | 'left-align'
42
+ | 'horizontal-center'
43
+ | 'right-align'
44
+ | 'top-align'
45
+ | 'vertical-center'
46
+ | 'bottom-align'
47
+ | 'horizontal-avg'
48
+ | 'vertical-avg'
49
+
50
+ export type ResizePointType =
51
+ | 'top-center'
52
+ | 'top-left'
53
+ | 'top-right'
54
+ | 'bottom-center'
55
+ | 'bottom-left'
56
+ | 'bottom-right'
57
+ | 'left-center'
58
+ | 'right-center'
59
+
60
+ export interface MarkerLine {
61
+ left: string
62
+ top: string
63
+ width: string
64
+ height: string
65
+ background: string
66
+ }
67
+
68
+ export interface AdsorptionTarget {
69
+ id?: string
70
+ left: number
71
+ top: number
72
+ width: number
73
+ height: number
74
+ }
75
+
76
+ /**
77
+ * 格式化宽高,兼容字符串与 { value, suffix } 对象
78
+ * @param value 宽高值
79
+ * @param type width | height
80
+ * @param data 页面上下文
81
+ */
82
+ export const formatWidthHeight = (value: any, type: 'width' | 'height', data: any) => {
83
+ let result
84
+ if (typeof value !== 'string') {
85
+ const { selectedCanvasId, pageConfig, usedComponentList } = data
86
+ if (value?.suffix === 'px') {
87
+ result = value.value
88
+ } else if (value?.suffix === '%') {
89
+ if (selectedCanvasId === 'PAGE') {
90
+ // 页面
91
+ result = (+value.value / 100) * parseFloat(pageConfig[type])
92
+ } else {
93
+ // 弹窗
94
+ const modal = usedComponentList.find((v: any) => v.id === selectedCanvasId)
95
+ if (modal?.value) {
96
+ result = (+value.value / 100) * parseFloat(modal[type])
97
+ } else {
98
+ result = +value.value
99
+ }
100
+ }
101
+ }
102
+ } else if (typeof value === 'string') {
103
+ result = value
104
+ }
105
+ return result
106
+ }
107
+
108
+ /**
109
+ * 是否相近
110
+ * @param source 源数
111
+ * @param target 目标数
112
+ * @param diff 相差
113
+ */
114
+ export const isNearby = (source: number, target: number, diff: number = 3) => {
115
+ return Math.abs(source - target) <= diff
116
+ }
117
+
118
+ // 格式化宽度/高度为数值
119
+ export function getFormatWh (target: any, pageTarget: any) {
120
+ if (typeof target === 'string') {
121
+ return parseFloat(target)
122
+ } else {
123
+ if (target?.suffix === 'px') {
124
+ return +target.value
125
+ } else if (target?.suffix === '%') {
126
+ return parseFloat(pageTarget) * (target.value / 100)
127
+ } else {
128
+ return 0
129
+ }
130
+ }
131
+ }
132
+
133
+ // 获取限制移动区域
134
+ export const getLimitMoveArea = (component: any, renderComponents: any, pageConfig: any) => {
135
+ const { isLimitMoveArea, parentId, width: currentWidth, height: currentHeight } = component
136
+ const limitArea: any = [] // x1, y1, x2, y2
137
+ if (isLimitMoveArea && parentId) {
138
+ const parentCpnt = renderComponents.find((v: any) => v.id === parentId)
139
+ if (parentCpnt) {
140
+ limitArea[0] = parseFloat(parentCpnt.left)
141
+ limitArea[1] = parseFloat(parentCpnt.top)
142
+ limitArea[2] = parseFloat(parentCpnt.left) + getFormatWh(parentCpnt.width, pageConfig?.width) - getFormatWh(currentWidth, pageConfig?.width)
143
+ limitArea[3] = parseFloat(parentCpnt.top) + getFormatWh(parentCpnt.height, pageConfig?.height) - getFormatWh(currentHeight, pageConfig?.height)
144
+ }
145
+ }
146
+ return limitArea
147
+ }
148
+
149
+ // 获取多个组件的区域
150
+ export const getComponentsArea = (componentList: any, pageConfig: any) => {
151
+ let result: any = null
152
+ if (componentList?.length) {
153
+ const leftList = []
154
+ const topList = []
155
+ const widthList = []
156
+ const heightList = []
157
+ for (const component of componentList) {
158
+ const leftValue = parseFloat(component.left)
159
+ leftList.push(leftValue)
160
+ topList.push(parseFloat(component.top))
161
+ widthList.push(leftValue + getFormatWh(component.width, pageConfig?.width))
162
+ heightList.push(parseFloat(component.top) + getFormatWh(component.height, pageConfig?.height))
163
+ }
164
+ const minLeft = Math.min(...leftList)
165
+ const minTop = Math.min(...topList)
166
+ result = {
167
+ left: minLeft,
168
+ top: minTop,
169
+ width: Math.max(...widthList) - minLeft,
170
+ height: Math.max(...heightList) - minTop
171
+ }
172
+ }
173
+ return result
174
+ }
175
+
176
+ // 获取需要做对齐操作的数据
177
+ // 1、容器组件里面的子组件不需要获取
178
+ // 2、组合里面的组件不需要获取
179
+ // 3、指标面板内的组件不需要获取
180
+ export const getAlignItems = (list: any[], pageConfig: any, containerChilds: any) => {
181
+ const result: any = []
182
+ const info: any = []
183
+ // 是否操作的都是容器内同一父节点的组件
184
+ let isContainerInner = false
185
+ // 是否操作的都是同一个指标面板内的组件
186
+ let isIndexPanelInner = false
187
+ // 容器内的组件
188
+ let cpntParentIds: any = []
189
+ list.forEach((v: any) => {
190
+ const target = containerChilds.find((c: any) => c.id === v.id)
191
+ if (target) {
192
+ cpntParentIds.push(target.parentId)
193
+ }
194
+ })
195
+ if (cpntParentIds?.length === list?.length && Array.from(new Set(cpntParentIds))?.length === 1) {
196
+ // 这些组件都是同一个父级
197
+ isContainerInner = true
198
+ }
199
+ cpntParentIds = list.map((v: any) => v.parentId).filter((v: any) => v)
200
+ if (cpntParentIds?.length === list?.length && Array.from(new Set(cpntParentIds))?.length === 1) {
201
+ // 这些组件都是同一个指标面板内的组件
202
+ isIndexPanelInner = true
203
+ }
204
+ for (const item of list) {
205
+ // 非容器下的子组件、非指标面板下的子组件
206
+ if ((isContainerInner || !containerChilds.find((c: any) => c.id === item.id)) && (isIndexPanelInner || !item.isForbidContain)) {
207
+ if (item.combinationId) {
208
+ // 组合中的组件,只记录组合id就可以
209
+ if (!info.find((v: any) => v.id === item.combinationId)) {
210
+ info.push({
211
+ id: item.combinationId,
212
+ type: 'combinate'
213
+ })
214
+ }
215
+ } else {
216
+ info.push({
217
+ data: item,
218
+ type: 'normal'
219
+ })
220
+ }
221
+ }
222
+ }
223
+ if (info?.length) {
224
+ for (const item of info) {
225
+ if (item.type === 'combinate') {
226
+ // 需要算整个组合所占的区域
227
+ const { combinationMap = {} } = pageConfig
228
+ const cpntIds = combinationMap[item.id] || []
229
+ const combinateCpnts = list.filter((v: any) => cpntIds.includes(v.id))
230
+ const combinateArea = getComponentsArea(combinateCpnts, pageConfig)
231
+ if (combinateArea) {
232
+ result.push({
233
+ id: item.id,
234
+ type: item.type,
235
+ ...combinateArea
236
+ })
237
+ }
238
+ } else {
239
+ const { data } = item
240
+ result.push({
241
+ id: data.id,
242
+ type: item.type,
243
+ width: getFormatWh(data.width, pageConfig?.width),
244
+ height: getFormatWh(data.height, pageConfig?.height),
245
+ left: parseFloat(data.left),
246
+ top: parseFloat(data.top)
247
+ })
248
+ }
249
+ }
250
+ }
251
+ return result
252
+ }
253
+
254
+ /**
255
+ * 多选对齐/分布,输入对齐项,输出对齐后的新列表
256
+ * @param items 对齐项
257
+ * @param alignType 对齐类型
258
+ */
259
+ export const getAlignedItems = (items: AlignItem[], alignType: AlignType): AlignItem[] => {
260
+ const result = items.map((item) => ({ ...item }))
261
+ if (!result.length) return result
262
+ const leftList = []
263
+ const topList = []
264
+ const widthList = []
265
+ const heightList = []
266
+ for (const item of result) {
267
+ leftList.push(item.left)
268
+ topList.push(item.top)
269
+ widthList.push(item.left + item.width)
270
+ heightList.push(item.top + item.height)
271
+ }
272
+ switch (alignType) {
273
+ case 'left-align': {
274
+ // 左对齐
275
+ const minLeft = Math.min(...leftList)
276
+ result.forEach((v) => {
277
+ v.left = minLeft
278
+ })
279
+ break
280
+ }
281
+ case 'horizontal-center': {
282
+ // 水平居中
283
+ const minLeft = Math.min(...leftList)
284
+ const maxWidth = Math.max(...widthList)
285
+ const centerValue = +((minLeft + maxWidth) / 2).toFixed(2)
286
+ result.forEach((v) => {
287
+ v.left = centerValue - +(v.width / 2).toFixed(2)
288
+ })
289
+ break
290
+ }
291
+ case 'right-align': {
292
+ // 右对齐
293
+ const maxWidth = Math.max(...widthList)
294
+ result.forEach((v) => {
295
+ v.left = maxWidth - v.width
296
+ })
297
+ break
298
+ }
299
+ case 'top-align': {
300
+ // 顶端对齐
301
+ const minTop = Math.min(...topList)
302
+ result.forEach((v) => {
303
+ v.top = minTop
304
+ })
305
+ break
306
+ }
307
+ case 'vertical-center': {
308
+ // 垂直居中
309
+ const minTop = Math.min(...topList)
310
+ const maxHeight = Math.max(...heightList)
311
+ const centerValue = +((minTop + maxHeight) / 2).toFixed(2)
312
+ result.forEach((v) => {
313
+ v.top = centerValue - +(v.height / 2).toFixed(2)
314
+ })
315
+ break
316
+ }
317
+ case 'bottom-align': {
318
+ // 底端对齐
319
+ const maxHeight = Math.max(...heightList)
320
+ result.forEach((v) => {
321
+ v.top = maxHeight - v.height
322
+ })
323
+ break
324
+ }
325
+ case 'horizontal-avg': {
326
+ // 横向分布
327
+ const minLeft = Math.min(...leftList)
328
+ const areaWidth = Math.max(...widthList) - minLeft
329
+ let widthSum = 0
330
+ result.sort((a, b) => a.left - b.left)
331
+ result.forEach((v) => {
332
+ widthSum += v.width
333
+ })
334
+ if (result.length > 1) {
335
+ const itemOffset = +((areaWidth - widthSum) / (result.length - 1)).toFixed(2)
336
+ let baseLeft = 0
337
+ result.forEach((v, index) => {
338
+ if (index > 0) {
339
+ v.left = baseLeft + itemOffset
340
+ }
341
+ baseLeft = v.left + v.width
342
+ })
343
+ }
344
+ break
345
+ }
346
+ case 'vertical-avg': {
347
+ // 纵向分布
348
+ const minTop = Math.min(...topList)
349
+ const areaHeight = Math.max(...heightList) - minTop
350
+ let heightSum = 0
351
+ result.sort((a, b) => a.top - b.top)
352
+ result.forEach((v) => {
353
+ heightSum += v.height
354
+ })
355
+ if (result.length > 1) {
356
+ const itemOffset = +((areaHeight - heightSum) / (result.length - 1)).toFixed(2)
357
+ let baseTop = 0
358
+ result.forEach((v, index) => {
359
+ if (index > 0) {
360
+ v.top = baseTop + itemOffset
361
+ }
362
+ baseTop = v.top + v.height
363
+ })
364
+ }
365
+ break
366
+ }
367
+ }
368
+ return result
369
+ }
370
+
371
+ /**
372
+ * 计算拖拽吸附后的位置
373
+ * @param sourceLeft 源组件左
374
+ * @param sourceTop 源组件上
375
+ * @param sourceWidth 源组件宽
376
+ * @param sourceHeight 源组件高
377
+ * @param sourceId 源组件id
378
+ * @param targets 吸附目标列表(含画布边界)
379
+ * @param diff 吸附阈值
380
+ */
381
+ export const getAdsorptionPosition = (
382
+ sourceLeft: number,
383
+ sourceTop: number,
384
+ sourceWidth: number,
385
+ sourceHeight: number,
386
+ sourceId: string,
387
+ targets: AdsorptionTarget[],
388
+ diff: number = 5
389
+ ) => {
390
+ let x = sourceLeft
391
+ let y = sourceTop
392
+ const sourceRight = sourceLeft + sourceWidth
393
+ const sourceXCenter = (sourceLeft + sourceRight) / 2
394
+ const sourceBottom = sourceTop + sourceHeight
395
+ const sourceYCenter = (sourceTop + sourceBottom) / 2
396
+ targets.forEach((target) => {
397
+ const { left, top, width: targetWidth, height: targetHeight, id: targetId } = target
398
+ if (sourceId !== targetId) {
399
+ const targetLeft = left
400
+ const targetRight = targetLeft + targetWidth
401
+ const targetXCenter = (targetLeft + targetRight) / 2
402
+ const targetTop = top
403
+ const targetBottom = targetTop + targetHeight
404
+ const targetYCenter = (targetTop + targetBottom) / 2
405
+ const targetXPoints = [targetLeft, targetRight]
406
+ const targetYPoints = [targetTop, targetBottom]
407
+ for (let i = 0; i < targetXPoints.length; i++) {
408
+ const targetXPoint = targetXPoints[i]
409
+ if (isNearby(sourceLeft, targetXPoint, diff)) {
410
+ x = targetXPoint
411
+ }
412
+ }
413
+ for (let i = 0; i < targetXPoints.length; i++) {
414
+ const targetXPoint = targetXPoints[i]
415
+ if (isNearby(sourceRight, targetXPoint, diff)) {
416
+ x = targetXPoint - sourceWidth
417
+ }
418
+ }
419
+ for (let i = 0; i < targetYPoints.length; i++) {
420
+ const targetYPoint = targetYPoints[i]
421
+ if (isNearby(sourceTop, targetYPoint, diff)) {
422
+ y = targetYPoint
423
+ }
424
+ }
425
+ for (let i = 0; i < targetYPoints.length; i++) {
426
+ const targetYPoint = targetYPoints[i]
427
+ if (isNearby(sourceBottom, targetYPoint, diff)) {
428
+ y = targetYPoint - sourceHeight
429
+ }
430
+ }
431
+ if (isNearby(sourceXCenter, targetXCenter)) {
432
+ x = targetXCenter - sourceWidth / 2
433
+ }
434
+ if (isNearby(sourceYCenter, targetYCenter)) {
435
+ y = targetYCenter - sourceHeight / 2
436
+ }
437
+ }
438
+ })
439
+ return {
440
+ x,
441
+ y
442
+ }
443
+ }
444
+
445
+ /**
446
+ * 计算拖拽时的对齐线
447
+ */
448
+ export const getMarkerLines = (options: {
449
+ source: { left?: any; top?: any; width?: any; height?: any; id?: string }
450
+ components: any[]
451
+ excludeIds?: string[]
452
+ pageWidth: number
453
+ pageHeight: number
454
+ scale: number
455
+ canvasPositionLeft: number
456
+ canvasPositionTop: number
457
+ }): { lines: MarkerLine[]; highlightedIds: string[] } => {
458
+ const {
459
+ source,
460
+ components,
461
+ excludeIds = [],
462
+ pageWidth,
463
+ pageHeight,
464
+ scale,
465
+ canvasPositionLeft,
466
+ canvasPositionTop
467
+ } = options
468
+ const sourceLeft = parseFloat(source.left)
469
+ const sourceRight = sourceLeft + parseFloat(source.width)
470
+ const sourceXCenter = (sourceLeft + sourceRight) / 2
471
+ const sourceTop = parseFloat(source.top)
472
+ const sourceBottom = sourceTop + parseFloat(source.height)
473
+ const sourceYCenter = (sourceTop + sourceBottom) / 2
474
+ const sourceXPoints = [sourceLeft, sourceRight, sourceXCenter]
475
+ const sourceYPoints = [sourceTop, sourceBottom, sourceYCenter]
476
+ const lines: MarkerLine[] = []
477
+ const highlightedIds: string[] = []
478
+ components.forEach((usedComponent) => {
479
+ const { left, top, width, height, id: targetId } = usedComponent
480
+ const targetLeft = parseFloat(left)
481
+ const targetRight = targetLeft + parseFloat(width)
482
+ const targetXCenter = (targetLeft + targetRight) / 2
483
+ const targetTop = parseFloat(top)
484
+ const targetBottom = targetTop + parseFloat(height)
485
+ const targetYCenter = (targetTop + targetBottom) / 2
486
+ const targetXPoints = [targetLeft, targetRight, targetXCenter]
487
+ const targetYPoints = [targetTop, targetBottom, targetYCenter]
488
+ let isShowBorder = false
489
+ if (source.id !== targetId && !excludeIds.includes(targetId)) {
490
+ for (let i = 0; i < sourceXPoints.length; i++) {
491
+ for (let j = 0; j < targetXPoints.length; j++) {
492
+ if (isNearby(sourceXPoints[i], targetXPoints[j])) {
493
+ lines.push({
494
+ left: `${targetXPoints[j] * scale + canvasPositionLeft}px`,
495
+ top: `${canvasPositionTop}px`,
496
+ height: `${pageHeight * scale}px`,
497
+ width: '1px',
498
+ background: '#3f8ce8'
499
+ })
500
+ isShowBorder = true
501
+ }
502
+ }
503
+ }
504
+ for (let i = 0; i < sourceYPoints.length; i++) {
505
+ for (let j = 0; j < targetYPoints.length; j++) {
506
+ if (isNearby(sourceYPoints[i], targetYPoints[j])) {
507
+ lines.push({
508
+ left: `${canvasPositionLeft}px`,
509
+ top: `${targetYPoints[j] * scale + canvasPositionTop}px`,
510
+ width: `${pageWidth * scale}px`,
511
+ height: '1px',
512
+ background: '#3f8ce8'
513
+ })
514
+ isShowBorder = true
515
+ }
516
+ }
517
+ }
518
+ if (isShowBorder) {
519
+ highlightedIds.push(targetId)
520
+ }
521
+ }
522
+ })
523
+ return {
524
+ lines,
525
+ highlightedIds
526
+ }
527
+ }
528
+
529
+ /**
530
+ * 计算拖拽缩放后的宽高/位置
531
+ */
532
+ export const getResizeRect = (options: {
533
+ rect: { top: number; bottom: number; left: number; right: number }
534
+ clientX: number
535
+ clientY: number
536
+ container: { scrollTop: number; scrollLeft: number; offsetTop: number; offsetLeft: number }
537
+ canvasPosition: { left: number; top: number }
538
+ scale: number
539
+ pointType: ResizePointType
540
+ minWidth?: number
541
+ minHeight?: number
542
+ }): { width?: string; height?: string; left?: string; top?: string } => {
543
+ const {
544
+ rect,
545
+ clientX,
546
+ clientY,
547
+ container,
548
+ canvasPosition,
549
+ scale,
550
+ pointType,
551
+ minWidth = 12,
552
+ minHeight = 12
553
+ } = options
554
+ const calculateScaleSize = (num: number) => Number((num / scale).toFixed(0))
555
+ const { top, bottom, left, right } = rect
556
+ const { scrollTop, scrollLeft, offsetTop, offsetLeft } = container
557
+ let rw = clientX - left
558
+ if (minWidth >= rw) rw = minWidth
559
+ const rightWidth = `${calculateScaleSize(rw)}px`
560
+ let lw = right - clientX
561
+ if (minWidth >= lw) lw = minWidth
562
+ const leftWidth = `${calculateScaleSize(lw)}px`
563
+ let bh = scrollTop + clientY - top - scrollTop
564
+ if (minHeight >= bh) bh = minHeight
565
+ const bottomHeight = `${calculateScaleSize(bh)}px`
566
+ let th = bottom - clientY
567
+ if (minHeight >= th) th = minHeight
568
+ const topHeight = `${calculateScaleSize(th)}px`
569
+ const topOffset = `${calculateScaleSize(scrollTop + clientY - offsetTop - canvasPosition.top)}px`
570
+ const leftOffset = `${calculateScaleSize(scrollLeft + clientX - offsetLeft - canvasPosition.left)}px`
571
+ let size: any = {}
572
+ switch (pointType) {
573
+ case 'top-center':
574
+ if (th > minHeight) {
575
+ size = {
576
+ height: topHeight,
577
+ top: topOffset
578
+ }
579
+ }
580
+ break
581
+ case 'top-left':
582
+ if (lw > minWidth && th > minHeight) {
583
+ size = {
584
+ width: leftWidth,
585
+ left: leftOffset,
586
+ height: topHeight,
587
+ top: topOffset
588
+ }
589
+ } else if (lw <= minWidth && th > minHeight) {
590
+ size = {
591
+ height: topHeight,
592
+ top: topOffset
593
+ }
594
+ } else if (lw > minWidth && th <= minHeight) {
595
+ size = {
596
+ width: leftWidth,
597
+ left: leftOffset
598
+ }
599
+ }
600
+ break
601
+ case 'top-right':
602
+ if (rw >= minWidth && th >= minHeight) {
603
+ size = {
604
+ width: rightWidth,
605
+ height: topHeight,
606
+ top: topOffset
607
+ }
608
+ } else if (rw < minWidth && th >= minHeight) {
609
+ size = {
610
+ height: topHeight,
611
+ top: topOffset
612
+ }
613
+ } else if (rw >= minWidth && th < minHeight) {
614
+ size = {
615
+ width: rightWidth
616
+ }
617
+ }
618
+ break
619
+ case 'bottom-left':
620
+ if (lw >= minWidth && bh >= minHeight) {
621
+ size = {
622
+ width: leftWidth,
623
+ left: leftOffset,
624
+ height: bottomHeight
625
+ }
626
+ } else if (lw < minWidth && bh >= minHeight) {
627
+ size = {
628
+ height: bottomHeight
629
+ }
630
+ } else if (lw > minWidth && bh <= minHeight) {
631
+ size = {
632
+ width: leftWidth,
633
+ left: leftOffset
634
+ }
635
+ }
636
+ break
637
+ case 'bottom-right':
638
+ size = {
639
+ width: rightWidth,
640
+ height: bottomHeight
641
+ }
642
+ break
643
+ case 'bottom-center':
644
+ size = {
645
+ height: bottomHeight
646
+ }
647
+ break
648
+ case 'left-center':
649
+ if (lw > minWidth) {
650
+ size = {
651
+ width: leftWidth,
652
+ left: leftOffset
653
+ }
654
+ }
655
+ break
656
+ case 'right-center':
657
+ size = {
658
+ width: rightWidth
659
+ }
660
+ break
661
+ }
662
+ return size
663
+ }
664
+
665
+ /**
666
+ * 展开多选结果中的组合
667
+ * @param items 命中项
668
+ * @param combinationMap 组合映射
669
+ */
670
+ export const expandSelectionIds = (items: Array<{ id: string; combinationId?: string }>, combinationMap: any = {}) => {
671
+ const selectedIds: string[] = []
672
+ for (const item of items) {
673
+ if (item.combinationId && combinationMap[item.combinationId]) {
674
+ selectedIds.push(...combinationMap[item.combinationId])
675
+ } else {
676
+ selectedIds.push(item.id)
677
+ }
678
+ }
679
+ return Array.from(new Set(selectedIds))
680
+ }
681
+
682
+ /**
683
+ * 框选命中组件,并展开组合
684
+ * @param components 画布组件列表
685
+ * @param range 框选范围(画布像素坐标)
686
+ * @param options 缩放、画布偏移、页面尺寸、组合映射
687
+ */
688
+ export const getSelectedIdsByRect = (
689
+ components: CanvasComponent[],
690
+ range: CanvasRect,
691
+ options: {
692
+ scale: number
693
+ offsetLeft: number
694
+ offsetTop: number
695
+ pageWidth?: any
696
+ pageHeight?: any
697
+ combinationMap?: any
698
+ }
699
+ ) => {
700
+ const { scale, offsetLeft, offsetTop, pageWidth, pageHeight, combinationMap = {} } = options
701
+ const selectedIds: string[] = []
702
+ for (const component of components) {
703
+ const x = parseFloat(component.left as any) * scale + offsetLeft
704
+ const y = parseFloat(component.top as any) * scale + offsetTop
705
+ const width = getFormatWh(component.width, pageWidth) * scale
706
+ const height = getFormatWh(component.height, pageHeight) * scale
707
+ const isIntersect =
708
+ !(range.left + range.width <= x ||
709
+ x + width <= range.left ||
710
+ range.top + range.height <= y ||
711
+ y + height <= range.top)
712
+ if (isIntersect) {
713
+ if (component.combinationId && combinationMap[component.combinationId]) {
714
+ selectedIds.push(...combinationMap[component.combinationId])
715
+ } else {
716
+ selectedIds.push(component.id)
717
+ }
718
+ }
719
+ }
720
+ return Array.from(new Set(selectedIds))
721
+ }