@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,7 @@
1
+ /**
2
+ * 画布交互领域能力:几何纯函数 + 空间索引
3
+ */
4
+ export * from './geometry'
5
+ export * from './quadtree'
6
+ export * from './layer'
7
+ export { default } from './quadtree'
@@ -0,0 +1,130 @@
1
+ /**
2
+ * 图层操作纯函数:置顶/置底、上移一层/下移一层
3
+ * 输入组件列表与目标 id 列表,输出 zIndex 更新和新的列表顺序
4
+ */
5
+
6
+ export type LayerMoveType = 'up' | 'down'
7
+
8
+ /**
9
+ * 判断选中的组件是否已经处于最顶层(上方没有未被选中的组件)
10
+ * @param renderList 渲染组件列表
11
+ * @param setList 选中的组件 id 列表
12
+ */
13
+ export const isLayerSelectedAtTop = (renderList: any[], setList: string[]) => {
14
+ const selectedSet = new Set(setList)
15
+ const indexMap = new Map<string, number>()
16
+ renderList.forEach((item, index) => indexMap.set(item.id, index))
17
+ for (const item of renderList) {
18
+ if (selectedSet.has(item.id)) {
19
+ continue
20
+ }
21
+ for (const id of setList) {
22
+ const current = renderList.find((v: any) => v.id === id)
23
+ if (!current) {
24
+ continue
25
+ }
26
+ const itemIndex = indexMap.get(item.id) as number
27
+ const currentIndex = indexMap.get(current.id) as number
28
+ // 同层级时排在后面的页面元素才算处于上方
29
+ const isAbove = (item.zIndex || 0) > (current.zIndex || 0) ||
30
+ (item.zIndex === current.zIndex && itemIndex > currentIndex)
31
+ if (isAbove) {
32
+ return false
33
+ }
34
+ }
35
+ }
36
+ return true
37
+ }
38
+
39
+ /**
40
+ * 上移一层/下移一层:选中组件整体 zIndex ± 1,再调整同层级内的顺序
41
+ * @param renderList 渲染组件列表
42
+ * @param setList 选中的组件 id 列表
43
+ * @param type up | down
44
+ */
45
+ export const getLayerOneStepResult = (renderList: any[], setList: string[], type: LayerMoveType) => {
46
+ const movedSet = new Set(setList)
47
+ const updates = setList
48
+ .map(id => {
49
+ const cpnt = renderList.find((v: any) => v.id === id)
50
+ if (!cpnt) {
51
+ return null
52
+ }
53
+ return {
54
+ id,
55
+ zIndex: Math.max(1, (cpnt.zIndex || 0) + (type === 'up' ? 1 : -1))
56
+ }
57
+ })
58
+ .filter((v: any) => v)
59
+ const updateMap = new Map(updates.map((update: any) => [update.id, update.zIndex]))
60
+ const updatedList = renderList.map((item: any) =>
61
+ updateMap.has(item.id) ? { ...item, zIndex: updateMap.get(item.id) } : item)
62
+
63
+ // 按 zIndex 分组,同一层级内调整顺序:上移的排在后面,下移的排在前面
64
+ const groupMap = new Map<number, any[]>()
65
+ for (const item of updatedList) {
66
+ const zIndex = item.zIndex
67
+ if (!groupMap.has(zIndex)) {
68
+ groupMap.set(zIndex, [])
69
+ }
70
+ groupMap.get(zIndex)?.push(item)
71
+ }
72
+ for (const [zIndex, list] of groupMap) {
73
+ const movedList = list.filter((v: any) => movedSet.has(v.id))
74
+ const unmovedList = list.filter((v: any) => !movedSet.has(v.id))
75
+ groupMap.set(zIndex, type === 'up' ? [...unmovedList, ...movedList] : [...movedList, ...unmovedList])
76
+ }
77
+
78
+ // 保持不同层级页面元素之间的相对顺序,只替换同层级内的顺序
79
+ const groupIndexMap = new Map<number, number>()
80
+ const orderedIds: string[] = []
81
+ for (const item of updatedList) {
82
+ const zIndex = item.zIndex
83
+ const list = groupMap.get(zIndex) || []
84
+ const index = groupIndexMap.get(zIndex) || 0
85
+ orderedIds.push(list[index].id)
86
+ groupIndexMap.set(zIndex, index + 1)
87
+ }
88
+ return {
89
+ updates,
90
+ orderedIds
91
+ }
92
+ }
93
+
94
+ /**
95
+ * 置于顶层:选中组件 zIndex 设为当前最大 zIndex + 1
96
+ * @param renderList 渲染组件列表
97
+ * @param setList 选中的组件 id 列表
98
+ */
99
+ export const getLayerTopResult = (renderList: any[], setList: string[]) => {
100
+ const allzIndex = renderList.filter((v: any) => v.zIndex).map((v: any) => v.zIndex)
101
+ const topZIndex = allzIndex?.length ? Math.max(...allzIndex) : 0
102
+ return {
103
+ updates: setList.map(id => ({
104
+ id,
105
+ zIndex: topZIndex + 1
106
+ }))
107
+ }
108
+ }
109
+
110
+ /**
111
+ * 置于底层:选中组件 zIndex 设为 1,并移到列表最前(保持选中项原有相对顺序)
112
+ * @param renderList 渲染组件列表
113
+ * @param setList 选中的组件 id 列表
114
+ */
115
+ export const getLayerBottomResult = (renderList: any[], setList: string[]) => {
116
+ const sortedSetList = setList.slice().sort((a, b) =>
117
+ renderList.findIndex((v: any) => v.id === a) - renderList.findIndex((v: any) => v.id === b))
118
+ const set = new Set(sortedSetList)
119
+ const orderedIds = [
120
+ ...renderList.filter((v: any) => set.has(v.id)).map((v: any) => v.id),
121
+ ...renderList.filter((v: any) => !set.has(v.id)).map((v: any) => v.id)
122
+ ]
123
+ return {
124
+ updates: sortedSetList.map(id => ({
125
+ id,
126
+ zIndex: 1
127
+ })),
128
+ orderedIds
129
+ }
130
+ }
@@ -0,0 +1,181 @@
1
+ /**
2
+ * 通用四叉树空间索引,用于画布框选等相交查询
3
+ */
4
+ export class QuadTree {
5
+ private boundary: { [key: string]:any } // 节点边界
6
+ private capacity: number // 每个节点的容量
7
+ private depth: number // 节点整体深度
8
+ private maxDepth: number // 节点最大深度
9
+ private elements: { [key: string]:any }[] // 节点内元素
10
+ private isDivided: boolean // 分裂标识
11
+ private nodes: { [key: string]:any }[] // 子节点
12
+ private scale: number // 画布缩放比例
13
+ constructor (boundary: { [key: string]:any }, capacity = 4, depth = 0, maxDepth = 8, scale = 1) {
14
+ this.boundary = boundary
15
+ this.capacity = capacity
16
+ this.depth = depth
17
+ this.maxDepth = maxDepth
18
+ this.elements = []
19
+ this.isDivided = false
20
+ this.nodes = []
21
+ this.scale = scale
22
+ }
23
+
24
+ isLargeElement (element: { [key: string]:any }) {
25
+ const largeSize: number = 300 * this.scale // 元素长或宽达到此数值后认为是大尺寸的元素,将会被留在第一层的节点
26
+ if (element.width >= largeSize || element.height >= largeSize) {
27
+ element.large = true
28
+ return true
29
+ } else {
30
+ return false
31
+ }
32
+ }
33
+
34
+ // 插入元素
35
+ insert (element: { [key: string]:any }) {
36
+ // 如果元素不在边界内且与边界不相交,不插入
37
+ if (!this.containsElement(element)) {
38
+ return false
39
+ }
40
+
41
+ // 如果是 大尺寸元素 或者 节点未满 或者 当前深度达到最大深度 直接插入
42
+ if (
43
+ this.isLargeElement(element) ||
44
+ this.elements.length < this.capacity ||
45
+ this.depth >= this.maxDepth
46
+ ) {
47
+ this.elements.push(element)
48
+ return true
49
+ }
50
+
51
+ // 如果节点已满且未分裂,则分裂
52
+ if (!this.isDivided) {
53
+ this.subdivide()
54
+ }
55
+
56
+ // 尝试插入到子节点
57
+ for (const node of this.nodes) {
58
+ if (node.insert(element)) {
59
+ return true
60
+ }
61
+ }
62
+
63
+ // 如果无法插入到子节点,留在当前节点
64
+ this.elements.push(element)
65
+ return true
66
+ }
67
+
68
+ // 分裂节点
69
+ subdivide () {
70
+ const { x, y, width, height } = this.boundary
71
+ const halfWidth = width / 2
72
+ const halfHeight = height / 2
73
+ const nextDepth = this.depth + 1
74
+
75
+ // 创建四个子节点
76
+ this.nodes = [
77
+ new QuadTree({ x: x, y: y, width: halfWidth, height: halfHeight },
78
+ this.capacity, nextDepth, this.maxDepth), // 西北
79
+ new QuadTree({ x: x + halfWidth, y: y, width: halfWidth, height: halfHeight },
80
+ this.capacity, nextDepth, this.maxDepth), // 东北
81
+ new QuadTree({ x: x, y: y + halfHeight, width: halfWidth, height: halfHeight },
82
+ this.capacity, nextDepth, this.maxDepth), // 西南
83
+ new QuadTree({ x: x + halfWidth, y: y + halfHeight, width: halfWidth, height: halfHeight },
84
+ this.capacity, nextDepth, this.maxDepth) // 东南
85
+ ]
86
+
87
+ this.isDivided = true
88
+
89
+ // 将当前节点的元素重新分配到子节点
90
+ for (const element of this.elements) {
91
+ if (element.large) {
92
+ continue
93
+ }
94
+ for (const node of this.nodes) {
95
+ if (node.insert(element)) {
96
+ break
97
+ }
98
+ }
99
+ }
100
+
101
+ // 清空当前节点的元素
102
+ this.elements = this.elements.filter(v => v.large)
103
+ }
104
+
105
+ // 查询与指定区域相交的元素
106
+ query (range: { [key: string]:any }, found: { [key: string]:any }[] = []) {
107
+ // 如果查询区域与节点边界不相交,返回空数组
108
+ if (!this.intersects(range)) {
109
+ return found
110
+ }
111
+
112
+ // 检查当前节点的元素
113
+ for (const element of this.elements) {
114
+ if (this.elementIntersectsRange(element, range)) {
115
+ found.push({ id: element.id, combinationId: element.combinationId })
116
+ }
117
+ }
118
+
119
+ // 如果有子节点,递归查询
120
+ if (this.isDivided) {
121
+ for (const node of this.nodes) {
122
+ node.query(range, found)
123
+ }
124
+ }
125
+
126
+ return found
127
+ }
128
+
129
+ // 检查元素是否属于该节点
130
+ containsElement (element: { [key: string]:any }) {
131
+ let result = false
132
+ if (
133
+ element.x >= this.boundary.x &&
134
+ element.y >= this.boundary.y &&
135
+ element.x + element.width <= this.boundary.x + this.boundary.width &&
136
+ element.y + element.height <= this.boundary.y + this.boundary.height
137
+ ) {
138
+ // 元素完全包裹于节点边界内
139
+ result = true
140
+ } else if (this.intersects(element)) {
141
+ // 元素与节点边界相交,则判断元素与节点区域相交面积是否大于元素面积的1/4,若大于则认为元素属于该节点
142
+ const intersectLeft = Math.max(element.x, this.boundary.x)
143
+ const intersectRight = Math.min(element.x + element.width, this.boundary.x + this.boundary.width)
144
+ const intersectTop = Math.max(element.y, this.boundary.y)
145
+ const intersectBottom = Math.min(element.y + element.height, this.boundary.y + this.boundary.height)
146
+ if ((intersectRight - intersectLeft) * (intersectBottom - intersectTop) >= (element.width * element.height / 4)) {
147
+ result = true
148
+ }
149
+ }
150
+ return result
151
+ }
152
+
153
+ // 检查节点边界是否与传入的区域相交
154
+ intersects (range: { [key: string]:any }) {
155
+ return !(
156
+ range.x > this.boundary.x + this.boundary.width ||
157
+ range.x + range.width < this.boundary.x ||
158
+ range.y > this.boundary.y + this.boundary.height ||
159
+ range.y + range.height < this.boundary.y
160
+ )
161
+ }
162
+
163
+ // 检查元素是否与查询区域相交
164
+ elementIntersectsRange (element: { [key: string]:any }, range: { [key: string]:any }) {
165
+ return !(
166
+ element.x > range.x + range.width ||
167
+ element.x + element.width < range.x ||
168
+ element.y > range.y + range.height ||
169
+ element.y + element.height < range.y
170
+ )
171
+ }
172
+
173
+ // 清空四叉树
174
+ clear () {
175
+ this.elements = []
176
+ this.nodes = []
177
+ this.isDivided = false
178
+ }
179
+ }
180
+
181
+ export default QuadTree
package/src/index.ts CHANGED
@@ -7,13 +7,32 @@ import useTransformChartDataByAttrKey from './hooks/useTransformChartDataByAttrK
7
7
  import useTransformChartDataByAttrValue from './hooks/useTransformChartDataByAttrValue'
8
8
  import useValueFormatter from './hooks/useValueFormatter'
9
9
  import useVariablesInText from './hooks/useVariablesInText'
10
- import transformProps from './utils/transformProps'
11
- import { getIndicatorParams, lowerCaseIncludes, hasOwn } from './utils/util'
12
- import { getFieldConfig, setAxisExtremum } from './utils/chartTransProps/scatter'
13
-
14
- export {
15
- formatIndicatorParams,
16
- runCode,
10
+ import transformProps from './utils/transformProps'
11
+ import { getIndicatorParams, lowerCaseIncludes, hasOwn } from './utils/util'
12
+ import { getFieldConfig, setAxisExtremum } from './utils/chartTransProps/scatter'
13
+ import {
14
+ formatWidthHeight,
15
+ isNearby,
16
+ getFormatWh,
17
+ getLimitMoveArea,
18
+ getComponentsArea,
19
+ getAlignItems,
20
+ getAlignedItems,
21
+ getAdsorptionPosition,
22
+ getMarkerLines,
23
+ getResizeRect,
24
+ expandSelectionIds,
25
+ getSelectedIdsByRect,
26
+ QuadTree,
27
+ isLayerSelectedAtTop,
28
+ getLayerOneStepResult,
29
+ getLayerTopResult,
30
+ getLayerBottomResult
31
+ } from './canvas'
32
+
33
+ export {
34
+ formatIndicatorParams,
35
+ runCode,
17
36
  useTransformChartDataByAttrKey,
18
37
  useTransformChartDataByAttrValue,
19
38
  useValueFormatter,
@@ -21,7 +40,24 @@ export {
21
40
  transformProps,
22
41
  getIndicatorParams,
23
42
  lowerCaseIncludes,
24
- hasOwn,
25
- getFieldConfig,
26
- setAxisExtremum
27
- }
43
+ hasOwn,
44
+ getFieldConfig,
45
+ setAxisExtremum,
46
+ formatWidthHeight,
47
+ isNearby,
48
+ getFormatWh,
49
+ getLimitMoveArea,
50
+ getComponentsArea,
51
+ getAlignItems,
52
+ getAlignedItems,
53
+ getAdsorptionPosition,
54
+ getMarkerLines,
55
+ getResizeRect,
56
+ expandSelectionIds,
57
+ getSelectedIdsByRect,
58
+ QuadTree,
59
+ isLayerSelectedAtTop,
60
+ getLayerOneStepResult,
61
+ getLayerTopResult,
62
+ getLayerBottomResult
63
+ }