| 34 | * 多组件对齐功能类 |
| 35 | */ |
| 36 | export class Alignment { |
| 37 | constructor(readonly designer: Designer) {} |
| 38 | |
| 39 | get selection() { |
| 40 | return this.designer.selection |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * 获取选中节点(排除 root) |
| 45 | */ |
| 46 | private getSelectedNodes(): Node[] { |
| 47 | return this.selection.getTopNodes(false).filter(node => node && !node.isRoot) |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * 计算选中组件的包围盒 |
| 52 | */ |
| 53 | private calculateBoundingBox(nodes: Node[]): BoundingBox { |
| 54 | let [minX, minY, maxX, maxY] = [ |
| 55 | Number.POSITIVE_INFINITY, |
| 56 | Number.POSITIVE_INFINITY, |
| 57 | Number.NEGATIVE_INFINITY, |
| 58 | Number.NEGATIVE_INFINITY, |
| 59 | ] |
| 60 | |
| 61 | for (const node of nodes) { |
| 62 | const rect = node.getDashboardRect() |
| 63 | minX = Math.min(minX, rect.x) |
| 64 | minY = Math.min(minY, rect.y) |
| 65 | maxX = Math.max(maxX, rect.x + rect.width) |
| 66 | maxY = Math.max(maxY, rect.y + rect.height) |
| 67 | } |
| 68 | |
| 69 | return { |
| 70 | minX, |
| 71 | minY, |
| 72 | maxX, |
| 73 | maxY, |
| 74 | width: maxX - minX, |
| 75 | height: maxY - minY, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * 更新节点位置(支持 Group) |
| 81 | */ |
| 82 | private updateNodePosition(node: Node, x: number, y: number) { |
| 83 | if (node.isGroup) { |
| 84 | const nodeRect = node.getDashboardRect() |
| 85 | const deltaX = x - nodeRect.x |
| 86 | const deltaY = y - nodeRect.y |
| 87 | |
| 88 | for (const child of node.getAllNodesInGroup()) { |
| 89 | const childRect = child.getDashboardRect() |
| 90 | child.updateDashboardRect({ |
| 91 | x: childRect.x + deltaX, |
| 92 | y: childRect.y + deltaY, |
| 93 | }) |
nothing calls this directly
no test coverage detected