@antv/g6 5.0.0-beta.4 → 5.0.0-beta.5

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.
Files changed (168) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +3 -1
  3. package/esm/index.js +1 -1
  4. package/esm/item/edge.d.ts +1 -0
  5. package/esm/item/edge.js +3 -1
  6. package/esm/item/edge.js.map +1 -1
  7. package/esm/item/item.d.ts +3 -2
  8. package/esm/item/item.js +12 -6
  9. package/esm/item/item.js.map +1 -1
  10. package/esm/item/node.d.ts +1 -0
  11. package/esm/item/node.js +3 -1
  12. package/esm/item/node.js.map +1 -1
  13. package/esm/runtime/controller/item.js +252 -148
  14. package/esm/runtime/controller/item.js.map +1 -1
  15. package/esm/runtime/graph.d.ts +16 -14
  16. package/esm/runtime/graph.js +66 -28
  17. package/esm/runtime/graph.js.map +1 -1
  18. package/esm/stdlib/behavior/activate-relations.js +2 -2
  19. package/esm/stdlib/behavior/activate-relations.js.map +1 -1
  20. package/esm/stdlib/behavior/click-select.js +1 -1
  21. package/esm/stdlib/behavior/click-select.js.map +1 -1
  22. package/esm/stdlib/behavior/drag-canvas.js +50 -27
  23. package/esm/stdlib/behavior/drag-canvas.js.map +1 -1
  24. package/esm/stdlib/behavior/drag-combo.js +8 -8
  25. package/esm/stdlib/behavior/drag-combo.js.map +1 -1
  26. package/esm/stdlib/behavior/drag-node.js +10 -10
  27. package/esm/stdlib/behavior/drag-node.js.map +1 -1
  28. package/esm/stdlib/behavior/zoom-canvas.js +47 -24
  29. package/esm/stdlib/behavior/zoom-canvas.js.map +1 -1
  30. package/esm/stdlib/index.d.ts +2 -0
  31. package/esm/stdlib/index.js +10 -8
  32. package/esm/stdlib/index.js.map +1 -1
  33. package/esm/stdlib/item/edge/base.d.ts +1 -1
  34. package/esm/stdlib/item/edge/base.js +13 -4
  35. package/esm/stdlib/item/edge/base.js.map +1 -1
  36. package/esm/stdlib/item/node/base.d.ts +1 -1
  37. package/esm/stdlib/item/node/base.js +11 -12
  38. package/esm/stdlib/item/node/base.js.map +1 -1
  39. package/esm/stdlib/item/node/image.d.ts +1 -1
  40. package/esm/stdlib/item/node/image.js +1 -1
  41. package/esm/stdlib/item/node/image.js.map +1 -1
  42. package/esm/stdlib/plugin/history/combo-command.js +2 -2
  43. package/esm/stdlib/plugin/history/combo-command.js.map +1 -1
  44. package/esm/stdlib/plugin/history/index.d.ts +10 -10
  45. package/esm/stdlib/plugin/history/index.js +14 -14
  46. package/esm/stdlib/plugin/history/index.js.map +1 -1
  47. package/esm/stdlib/plugin/history/item-data-command.js +4 -4
  48. package/esm/stdlib/plugin/history/item-data-command.js.map +1 -1
  49. package/esm/stdlib/plugin/history/layer-updated-command.js +2 -2
  50. package/esm/stdlib/plugin/history/layer-updated-command.js.map +1 -1
  51. package/esm/stdlib/plugin/history/state-updated-command.js +1 -1
  52. package/esm/stdlib/plugin/history/state-updated-command.js.map +1 -1
  53. package/esm/stdlib/plugin/history/visibility-updated-command.js +2 -2
  54. package/esm/stdlib/plugin/history/visibility-updated-command.js.map +1 -1
  55. package/esm/stdlib/plugin/index.d.ts +1 -0
  56. package/esm/stdlib/plugin/index.js +1 -0
  57. package/esm/stdlib/plugin/index.js.map +1 -1
  58. package/esm/stdlib/plugin/snapline/index.d.ts +163 -0
  59. package/esm/stdlib/plugin/snapline/index.js +1052 -0
  60. package/esm/stdlib/plugin/snapline/index.js.map +1 -0
  61. package/esm/stdlib/plugin/types.d.ts +1 -0
  62. package/esm/types/graph.d.ts +14 -14
  63. package/esm/types/hook.d.ts +6 -0
  64. package/esm/types/spec.d.ts +13 -0
  65. package/esm/util/bbox.d.ts +16 -1
  66. package/esm/util/bbox.js +29 -0
  67. package/esm/util/bbox.js.map +1 -1
  68. package/esm/util/shape.js +2 -2
  69. package/esm/util/shape.js.map +1 -1
  70. package/lib/index.d.ts +3 -1
  71. package/lib/index.js +1 -1
  72. package/lib/item/edge.d.ts +1 -0
  73. package/lib/item/edge.js +3 -1
  74. package/lib/item/edge.js.map +1 -1
  75. package/lib/item/item.d.ts +3 -2
  76. package/lib/item/item.js +12 -6
  77. package/lib/item/item.js.map +1 -1
  78. package/lib/item/node.d.ts +1 -0
  79. package/lib/item/node.js +3 -1
  80. package/lib/item/node.js.map +1 -1
  81. package/lib/runtime/controller/item.js +251 -147
  82. package/lib/runtime/controller/item.js.map +1 -1
  83. package/lib/runtime/graph.d.ts +16 -14
  84. package/lib/runtime/graph.js +66 -28
  85. package/lib/runtime/graph.js.map +1 -1
  86. package/lib/stdlib/behavior/activate-relations.js +2 -2
  87. package/lib/stdlib/behavior/activate-relations.js.map +1 -1
  88. package/lib/stdlib/behavior/click-select.js +1 -1
  89. package/lib/stdlib/behavior/click-select.js.map +1 -1
  90. package/lib/stdlib/behavior/drag-canvas.js +50 -27
  91. package/lib/stdlib/behavior/drag-canvas.js.map +1 -1
  92. package/lib/stdlib/behavior/drag-combo.js +8 -8
  93. package/lib/stdlib/behavior/drag-combo.js.map +1 -1
  94. package/lib/stdlib/behavior/drag-node.js +10 -10
  95. package/lib/stdlib/behavior/drag-node.js.map +1 -1
  96. package/lib/stdlib/behavior/zoom-canvas.js +47 -24
  97. package/lib/stdlib/behavior/zoom-canvas.js.map +1 -1
  98. package/lib/stdlib/index.d.ts +2 -0
  99. package/lib/stdlib/index.js +10 -8
  100. package/lib/stdlib/index.js.map +1 -1
  101. package/lib/stdlib/item/edge/base.d.ts +1 -1
  102. package/lib/stdlib/item/edge/base.js +13 -4
  103. package/lib/stdlib/item/edge/base.js.map +1 -1
  104. package/lib/stdlib/item/node/base.d.ts +1 -1
  105. package/lib/stdlib/item/node/base.js +11 -12
  106. package/lib/stdlib/item/node/base.js.map +1 -1
  107. package/lib/stdlib/item/node/image.d.ts +1 -1
  108. package/lib/stdlib/item/node/image.js +1 -1
  109. package/lib/stdlib/item/node/image.js.map +1 -1
  110. package/lib/stdlib/plugin/history/combo-command.js +2 -2
  111. package/lib/stdlib/plugin/history/combo-command.js.map +1 -1
  112. package/lib/stdlib/plugin/history/index.d.ts +10 -10
  113. package/lib/stdlib/plugin/history/index.js +14 -14
  114. package/lib/stdlib/plugin/history/index.js.map +1 -1
  115. package/lib/stdlib/plugin/history/item-data-command.js +4 -4
  116. package/lib/stdlib/plugin/history/item-data-command.js.map +1 -1
  117. package/lib/stdlib/plugin/history/layer-updated-command.js +2 -2
  118. package/lib/stdlib/plugin/history/layer-updated-command.js.map +1 -1
  119. package/lib/stdlib/plugin/history/state-updated-command.js +1 -1
  120. package/lib/stdlib/plugin/history/state-updated-command.js.map +1 -1
  121. package/lib/stdlib/plugin/history/visibility-updated-command.js +2 -2
  122. package/lib/stdlib/plugin/history/visibility-updated-command.js.map +1 -1
  123. package/lib/stdlib/plugin/index.d.ts +1 -0
  124. package/lib/stdlib/plugin/index.js +1 -0
  125. package/lib/stdlib/plugin/index.js.map +1 -1
  126. package/lib/stdlib/plugin/snapline/index.d.ts +163 -0
  127. package/lib/stdlib/plugin/snapline/index.js +1056 -0
  128. package/lib/stdlib/plugin/snapline/index.js.map +1 -0
  129. package/lib/stdlib/plugin/types.d.ts +1 -0
  130. package/lib/types/graph.d.ts +14 -14
  131. package/lib/types/hook.d.ts +6 -0
  132. package/lib/types/spec.d.ts +13 -0
  133. package/lib/util/bbox.d.ts +16 -1
  134. package/lib/util/bbox.js +32 -1
  135. package/lib/util/bbox.js.map +1 -1
  136. package/lib/util/shape.js +2 -2
  137. package/lib/util/shape.js.map +1 -1
  138. package/package.json +2 -2
  139. package/src/index.ts +1 -1
  140. package/src/item/edge.ts +4 -1
  141. package/src/item/item.ts +11 -6
  142. package/src/item/node.ts +10 -7
  143. package/src/runtime/controller/item.ts +212 -66
  144. package/src/runtime/graph.ts +91 -29
  145. package/src/stdlib/behavior/activate-relations.ts +2 -2
  146. package/src/stdlib/behavior/click-select.ts +1 -1
  147. package/src/stdlib/behavior/drag-canvas.ts +65 -27
  148. package/src/stdlib/behavior/drag-combo.ts +8 -8
  149. package/src/stdlib/behavior/drag-node.ts +10 -10
  150. package/src/stdlib/behavior/zoom-canvas.ts +60 -24
  151. package/src/stdlib/index.ts +10 -7
  152. package/src/stdlib/item/edge/base.ts +13 -5
  153. package/src/stdlib/item/node/base.ts +15 -14
  154. package/src/stdlib/item/node/image.ts +1 -1
  155. package/src/stdlib/plugin/history/combo-command.ts +2 -2
  156. package/src/stdlib/plugin/history/index.ts +14 -14
  157. package/src/stdlib/plugin/history/item-data-command.ts +4 -4
  158. package/src/stdlib/plugin/history/layer-updated-command.ts +2 -2
  159. package/src/stdlib/plugin/history/state-updated-command.ts +1 -1
  160. package/src/stdlib/plugin/history/visibility-updated-command.ts +2 -2
  161. package/src/stdlib/plugin/index.ts +1 -0
  162. package/src/stdlib/plugin/snapline/index.ts +1277 -0
  163. package/src/stdlib/plugin/types.ts +1 -0
  164. package/src/types/graph.ts +22 -17
  165. package/src/types/hook.ts +6 -0
  166. package/src/types/spec.ts +13 -0
  167. package/src/util/bbox.ts +38 -1
  168. package/src/util/shape.ts +2 -2
@@ -0,0 +1,1277 @@
1
+ import { ID } from '@antv/graphlib';
2
+ import { AABB, DisplayObject } from '@antv/g';
3
+ import { throttle } from '@antv/util';
4
+ import { ITEM_TYPE, ShapeStyle } from '../../../types/item';
5
+ import { IG6GraphEvent, IGraph } from '../../../types';
6
+ import { Plugin as Base, IPluginBaseConfig } from '../../../types/plugin';
7
+ import { Point } from '../../../types/common';
8
+
9
+ export interface SnapLineConfig extends IPluginBaseConfig {
10
+ // snapline type
11
+ line?: ShapeStyle;
12
+ // snapline align type. true means 'horizontal' and 'vertical'
13
+ itemAlignType?: boolean | 'horizontal' | 'vertical' | 'center';
14
+ }
15
+
16
+ /**
17
+ * draw line position
18
+ */
19
+ type LinePosition = 'top' | 'hcenter' | 'bottom' | 'left' | 'vcenter' | 'right';
20
+
21
+ /**
22
+ * Line for draw
23
+ * first point: point on constractItem
24
+ * second point: point on draggingItem
25
+ */
26
+ type DrawLine = { line: [Point, Point]; lp: LinePosition };
27
+
28
+ /**
29
+ * dl: draw line for choose
30
+ * offset: offset that selects the drawline with the smallest gap
31
+ */
32
+ type DrawLineForChoose = { dl: DrawLine; offset: Point };
33
+
34
+ export class Snapline extends Base {
35
+ //#region state: historyPoints
36
+ private historyPoints: [Point | undefined, Point | undefined] = [
37
+ undefined,
38
+ undefined,
39
+ ];
40
+
41
+ /**
42
+ * when start drag, offset of cursor and node center (event.canvas - draggingnode.BBox)
43
+ */
44
+ private initialOffset: [
45
+ offsetX: number | undefined,
46
+ offsetY: number | undefined,
47
+ ] = [undefined, undefined];
48
+
49
+ /**
50
+ * Gets the current cursor node and center offset and then with initialOffset difference
51
+ */
52
+ private getCurOffsetCompareToInitialOffset = (
53
+ e: IG6GraphEvent,
54
+ ): [number | undefined, number | undefined] => {
55
+ if (
56
+ this.initialOffset[0] === undefined ||
57
+ this.initialOffset[1] === undefined
58
+ ) {
59
+ return [undefined, undefined];
60
+ }
61
+
62
+ const curOffset: [curOffsetX: number, curOffsetY: number] = [
63
+ e.canvas.x - this.draggingBBox.center[0],
64
+ e.canvas.y - this.draggingBBox.center[1],
65
+ ];
66
+
67
+ return [
68
+ curOffset[0] - this.initialOffset[0],
69
+ curOffset[1] - this.initialOffset[1],
70
+ ];
71
+ };
72
+
73
+ //#endregion
74
+
75
+ private dragging = false;
76
+ private draggingBBox: AABB = undefined;
77
+
78
+ // the offset(cursor and draggingbox) when start to drag
79
+ private nonAbosorbOffset: Point = undefined;
80
+
81
+ // Gets the offset between the cursor and draggingItem
82
+ private getPointerOffsetWithItem = (pointer: {
83
+ x: number;
84
+ y: number;
85
+ }): { x: number; y: number } => {
86
+ return {
87
+ x: Math.abs(pointer.x - this.draggingBBox.center[0]),
88
+ y: Math.abs(pointer.x - this.draggingBBox.center[1]),
89
+ };
90
+ };
91
+
92
+ //#region state: tolerance
93
+ public tolerance = 4;
94
+ public toleranceFactor = 2;
95
+ public setTolerance(tolerance: number) {
96
+ this.tolerance = tolerance;
97
+ }
98
+ public getTolerance(): number {
99
+ return this.tolerance;
100
+ }
101
+ //#endregion
102
+
103
+ // All the lines in the vertical direction
104
+ private vContrastPoints: Point[] = [];
105
+ private hContrastPoints: Point[] = [];
106
+
107
+ /**
108
+ * alignLinesForChoose container
109
+ */
110
+ private alignLinesForChoose: {
111
+ v: DrawLineForChoose[];
112
+ h: DrawLineForChoose[];
113
+ } = { v: [], h: [] };
114
+
115
+ /**
116
+ * getDrawLineId via DrawLine
117
+ * Principle: the lines on the drawing board is the only load-point
118
+ * @param dl drawLine
119
+ */
120
+ getDrawLineIdByDrawLine(dl: DrawLine): ID {
121
+ return `${dl.line[0].x}-${dl.line[0].y}-${dl.line[1].x}-${dl.line[1].y}`;
122
+ }
123
+ /**
124
+ *
125
+ * @param cp based on constractBBox's point
126
+ * @param dp based on draggingBBox point
127
+ * @returns
128
+ */
129
+ getDrawLineIdByPoints([cp, dp]: [Point, Point]): ID {
130
+ return `${cp.x}-${cp.y}-${dp.x}-${dp.y}`;
131
+ }
132
+
133
+ /**
134
+ * all AlignLines satisfy conditions: in the tolerance range
135
+ */
136
+ addAlignLinesForChoose() {
137
+ function isInToleranceRange(
138
+ xOrY: number,
139
+ contrast: number,
140
+ tolerance: number,
141
+ ): boolean {
142
+ return xOrY >= contrast - tolerance && xOrY <= contrast + tolerance;
143
+ }
144
+
145
+ this.vContrastPoints.forEach((vpoint) => {
146
+ if (
147
+ isInToleranceRange(this.draggingBBox.min[0], vpoint.x, this.tolerance)
148
+ )
149
+ this.alignLinesForChoose.v.push({
150
+ dl: {
151
+ line: [vpoint, { x: vpoint.x, y: this.draggingBBox.center[1] }],
152
+ lp: 'left',
153
+ },
154
+ offset: { x: vpoint.x - this.draggingBBox.min[0], y: 0 },
155
+ });
156
+ if (
157
+ isInToleranceRange(
158
+ this.draggingBBox.center[0],
159
+ vpoint.x,
160
+ this.tolerance,
161
+ )
162
+ )
163
+ this.alignLinesForChoose.v.push({
164
+ dl: {
165
+ line: [vpoint, { x: vpoint.x, y: this.draggingBBox.center[1] }],
166
+ lp: 'vcenter',
167
+ },
168
+ offset: { x: vpoint.x - this.draggingBBox.center[0], y: 0 },
169
+ });
170
+ if (
171
+ isInToleranceRange(this.draggingBBox.max[0], vpoint.x, this.tolerance)
172
+ )
173
+ this.alignLinesForChoose.v.push({
174
+ dl: {
175
+ line: [vpoint, { x: vpoint.x, y: this.draggingBBox.center[1] }],
176
+ lp: 'right',
177
+ },
178
+ offset: { x: vpoint.x - this.draggingBBox.max[0], y: 0 },
179
+ });
180
+ });
181
+
182
+ this.hContrastPoints.forEach((hpoint) => {
183
+ if (
184
+ isInToleranceRange(this.draggingBBox.min[1], hpoint.y, this.tolerance)
185
+ )
186
+ this.alignLinesForChoose.v.push({
187
+ dl: {
188
+ line: [hpoint, { x: this.draggingBBox.min[0], y: hpoint.y }],
189
+ lp: 'top',
190
+ },
191
+ offset: { x: 0, y: hpoint.y - this.draggingBBox.min[1] },
192
+ });
193
+ if (
194
+ isInToleranceRange(
195
+ this.draggingBBox.center[1],
196
+ hpoint.y,
197
+ this.tolerance,
198
+ )
199
+ )
200
+ this.alignLinesForChoose.v.push({
201
+ dl: {
202
+ line: [hpoint, { x: this.draggingBBox.center[0], y: hpoint.y }],
203
+ lp: 'hcenter',
204
+ },
205
+ offset: { x: 0, y: hpoint.y - this.draggingBBox.center[1] },
206
+ });
207
+ if (
208
+ isInToleranceRange(this.draggingBBox.max[1], hpoint.y, this.tolerance)
209
+ )
210
+ this.alignLinesForChoose.v.push({
211
+ dl: {
212
+ line: [hpoint, { x: this.draggingBBox.max[0], y: hpoint.y }],
213
+ lp: 'bottom',
214
+ },
215
+ offset: { x: 0, y: hpoint.y - this.draggingBBox.max[1] },
216
+ });
217
+ });
218
+ }
219
+
220
+ initAlignLinesForChoose() {
221
+ this.alignLinesForChoose.v = [];
222
+ this.alignLinesForChoose.h = [];
223
+
224
+ let allIds: ID[] = [];
225
+
226
+ this.graph.getAllCombosData().forEach((item) => {
227
+ allIds.push(item.id);
228
+ });
229
+ this.graph.getAllNodesData().forEach((item) => {
230
+ allIds.push(item.id);
231
+ });
232
+ allIds = allIds.filter((item) => item !== this.dragItemId); // 排除drag item
233
+
234
+ allIds.forEach((id) => {
235
+ const cb = this.graph.getRenderBBox(id, true) as AABB; // contrast bbox
236
+ // 判断2个方向,6条线的对比
237
+
238
+ /* draggingBBox left */
239
+ if (Math.abs(this.draggingBBox.min[0] - cb.min[0]) <= this.tolerance) {
240
+ this.alignLinesForChoose.v.push({
241
+ dl: {
242
+ line: [
243
+ { x: cb.min[0], y: cb.center[1] },
244
+ { x: cb.min[0], y: this.draggingBBox.center[1] },
245
+ ],
246
+ lp: 'left',
247
+ },
248
+ offset: {
249
+ x: this.draggingBBox.min[0] - cb.min[0],
250
+ y: this.draggingBBox.center[1] - cb.center[1],
251
+ },
252
+ });
253
+ }
254
+ if (Math.abs(this.draggingBBox.min[0] - cb.center[0]) <= this.tolerance) {
255
+ this.alignLinesForChoose.v.push({
256
+ dl: {
257
+ line: [
258
+ { x: cb.center[0], y: cb.center[1] },
259
+ { x: cb.center[0], y: this.draggingBBox.center[1] },
260
+ ],
261
+ lp: 'left',
262
+ },
263
+ offset: {
264
+ x: this.draggingBBox.min[0] - cb.center[0],
265
+ y: this.draggingBBox.center[1] - cb.center[1],
266
+ },
267
+ });
268
+ }
269
+ if (Math.abs(this.draggingBBox.min[0] - cb.max[0]) <= this.tolerance) {
270
+ this.alignLinesForChoose.v.push({
271
+ dl: {
272
+ line: [
273
+ { x: cb.max[0], y: cb.center[1] },
274
+ { x: cb.max[0], y: this.draggingBBox.center[1] },
275
+ ],
276
+ lp: 'left',
277
+ },
278
+ offset: {
279
+ x: this.draggingBBox.min[0] - cb.max[0],
280
+ y: this.draggingBBox.center[1] - cb.center[1],
281
+ },
282
+ });
283
+ }
284
+
285
+ /* draggingBBox vcenter */
286
+ if (Math.abs(this.draggingBBox.center[0] - cb.min[0]) <= this.tolerance) {
287
+ this.alignLinesForChoose.v.push({
288
+ dl: {
289
+ line: [
290
+ { x: cb.min[0], y: cb.center[1] },
291
+ { x: cb.min[0], y: this.draggingBBox.center[1] },
292
+ ],
293
+ lp: 'vcenter',
294
+ },
295
+ offset: {
296
+ x: this.draggingBBox.center[0] - cb.min[0],
297
+ y: this.draggingBBox.center[1] - cb.center[1],
298
+ },
299
+ });
300
+ }
301
+ if (
302
+ Math.abs(this.draggingBBox.center[0] - cb.center[0]) <= this.tolerance
303
+ ) {
304
+ this.alignLinesForChoose.v.push({
305
+ dl: {
306
+ line: [
307
+ { x: cb.center[0], y: cb.center[1] },
308
+ { x: cb.center[0], y: this.draggingBBox.center[1] },
309
+ ],
310
+ lp: 'vcenter',
311
+ },
312
+ offset: {
313
+ x: this.draggingBBox.center[0] - cb.center[0],
314
+ y: this.draggingBBox.center[1] - cb.center[1],
315
+ },
316
+ });
317
+ }
318
+ if (Math.abs(this.draggingBBox.center[0] - cb.max[0]) <= this.tolerance) {
319
+ this.alignLinesForChoose.v.push({
320
+ dl: {
321
+ line: [
322
+ { x: cb.max[0], y: cb.center[1] },
323
+ { x: cb.max[0], y: this.draggingBBox.center[1] },
324
+ ],
325
+ lp: 'vcenter',
326
+ },
327
+ offset: {
328
+ x: this.draggingBBox.center[0] - cb.max[0],
329
+ y: this.draggingBBox.center[1] - cb.center[1],
330
+ },
331
+ });
332
+ }
333
+
334
+ /* draggingBBox right */
335
+ if (Math.abs(this.draggingBBox.max[0] - cb.min[0]) <= this.tolerance) {
336
+ this.alignLinesForChoose.v.push({
337
+ dl: {
338
+ line: [
339
+ { x: cb.min[0], y: cb.center[1] },
340
+ { x: cb.min[0], y: this.draggingBBox.center[1] },
341
+ ],
342
+ lp: 'right',
343
+ },
344
+ offset: {
345
+ x: this.draggingBBox.max[0] - cb.min[0],
346
+ y: this.draggingBBox.center[1] - cb.center[1],
347
+ },
348
+ });
349
+ }
350
+ if (Math.abs(this.draggingBBox.max[0] - cb.center[0]) <= this.tolerance) {
351
+ this.alignLinesForChoose.v.push({
352
+ dl: {
353
+ line: [
354
+ { x: cb.center[0], y: cb.center[1] },
355
+ { x: cb.center[0], y: this.draggingBBox.center[1] },
356
+ ],
357
+ lp: 'right',
358
+ },
359
+ offset: {
360
+ x: this.draggingBBox.max[0] - cb.center[0],
361
+ y: this.draggingBBox.center[1] - cb.center[1],
362
+ },
363
+ });
364
+ }
365
+ if (Math.abs(this.draggingBBox.max[0] - cb.max[0]) <= this.tolerance) {
366
+ this.alignLinesForChoose.v.push({
367
+ dl: {
368
+ line: [
369
+ { x: cb.max[0], y: cb.center[1] },
370
+ { x: cb.max[0], y: this.draggingBBox.center[1] },
371
+ ],
372
+ lp: 'right',
373
+ },
374
+ offset: {
375
+ x: this.draggingBBox.max[0] - cb.max[0],
376
+ y: this.draggingBBox.center[1] - cb.center[1],
377
+ },
378
+ });
379
+ }
380
+
381
+ /* draggingBBox top */
382
+ if (Math.abs(this.draggingBBox.min[1] - cb.max[1]) <= this.tolerance) {
383
+ this.alignLinesForChoose.h.push({
384
+ dl: {
385
+ line: [
386
+ { x: cb.center[0], y: cb.max[1] },
387
+ { x: this.draggingBBox.center[0], y: cb.max[1] },
388
+ ],
389
+ lp: 'top',
390
+ },
391
+ offset: {
392
+ x: this.draggingBBox.center[0] - cb.center[0],
393
+ y: this.draggingBBox.min[1] - cb.max[1],
394
+ },
395
+ });
396
+ }
397
+ if (Math.abs(this.draggingBBox.min[1] - cb.center[1]) <= this.tolerance) {
398
+ this.alignLinesForChoose.h.push({
399
+ dl: {
400
+ line: [
401
+ { x: cb.center[0], y: cb.center[1] },
402
+ { x: this.draggingBBox.center[0], y: cb.center[1] },
403
+ ],
404
+ lp: 'top',
405
+ },
406
+ offset: {
407
+ x: this.draggingBBox.center[0] - cb.center[0],
408
+ y: this.draggingBBox.min[1] - cb.center[1],
409
+ },
410
+ });
411
+ }
412
+ if (Math.abs(this.draggingBBox.min[1] - cb.min[1]) <= this.tolerance) {
413
+ this.alignLinesForChoose.h.push({
414
+ dl: {
415
+ line: [
416
+ { x: cb.center[0], y: cb.max[1] },
417
+ { x: this.draggingBBox.center[0], y: cb.max[1] },
418
+ ],
419
+ lp: 'top',
420
+ },
421
+ offset: {
422
+ x: this.draggingBBox.center[0] - cb.center[0],
423
+ y: this.draggingBBox.min[1] - cb.min[1],
424
+ },
425
+ });
426
+ }
427
+
428
+ /* draggingBBox hcenter */
429
+ if (Math.abs(this.draggingBBox.center[1] - cb.max[1]) <= this.tolerance) {
430
+ this.alignLinesForChoose.h.push({
431
+ dl: {
432
+ line: [
433
+ { x: cb.center[0], y: cb.max[1] },
434
+ { x: this.draggingBBox.center[0], y: cb.max[1] },
435
+ ],
436
+ lp: 'hcenter',
437
+ },
438
+ offset: {
439
+ x: this.draggingBBox.center[0] - cb.center[0],
440
+ y: this.draggingBBox.center[1] - cb.max[1],
441
+ },
442
+ });
443
+ }
444
+ if (
445
+ Math.abs(this.draggingBBox.center[1] - cb.center[1]) <= this.tolerance
446
+ ) {
447
+ this.alignLinesForChoose.h.push({
448
+ dl: {
449
+ line: [
450
+ { x: cb.center[0], y: cb.center[1] },
451
+ { x: this.draggingBBox.center[0], y: cb.center[1] },
452
+ ],
453
+ lp: 'hcenter',
454
+ },
455
+ offset: {
456
+ x: this.draggingBBox.center[0] - cb.center[0],
457
+ y: this.draggingBBox.center[1] - cb.center[1],
458
+ },
459
+ });
460
+ }
461
+ if (Math.abs(this.draggingBBox.center[1] - cb.min[1]) <= this.tolerance) {
462
+ this.alignLinesForChoose.h.push({
463
+ dl: {
464
+ line: [
465
+ { x: cb.center[0], y: cb.min[1] },
466
+ { x: this.draggingBBox.center[0], y: cb.min[1] },
467
+ ],
468
+ lp: 'hcenter',
469
+ },
470
+ offset: {
471
+ x: this.draggingBBox.center[0] - cb.center[0],
472
+ y: this.draggingBBox.center[1] - cb.min[1],
473
+ },
474
+ });
475
+ }
476
+
477
+ /* draggingBBox bottom */
478
+ if (Math.abs(this.draggingBBox.max[1] - cb.max[1]) <= this.tolerance) {
479
+ this.alignLinesForChoose.h.push({
480
+ dl: {
481
+ line: [
482
+ { x: cb.center[0], y: cb.max[1] },
483
+ { x: this.draggingBBox.center[0], y: cb.max[1] },
484
+ ],
485
+ lp: 'bottom',
486
+ },
487
+ offset: {
488
+ x: this.draggingBBox.center[0] - cb.center[0],
489
+ y: this.draggingBBox.max[1] - cb.max[1],
490
+ },
491
+ });
492
+ }
493
+ if (Math.abs(this.draggingBBox.max[1] - cb.center[1]) <= this.tolerance) {
494
+ this.alignLinesForChoose.h.push({
495
+ dl: {
496
+ line: [
497
+ { x: cb.center[0], y: cb.center[1] },
498
+ { x: this.draggingBBox.center[0], y: cb.center[1] },
499
+ ],
500
+ lp: 'bottom',
501
+ },
502
+ offset: {
503
+ x: this.draggingBBox.center[0] - cb.center[0],
504
+ y: this.draggingBBox.max[1] - cb.center[1],
505
+ },
506
+ });
507
+ }
508
+ if (Math.abs(this.draggingBBox.max[1] - cb.min[1]) <= this.tolerance) {
509
+ this.alignLinesForChoose.h.push({
510
+ dl: {
511
+ line: [
512
+ { x: cb.center[0], y: cb.min[1] },
513
+ { x: this.draggingBBox.center[0], y: cb.min[1] },
514
+ ],
515
+ lp: 'bottom',
516
+ },
517
+ offset: {
518
+ x: this.draggingBBox.center[0] - cb.center[0],
519
+ y: this.draggingBBox.max[1] - cb.min[1],
520
+ },
521
+ });
522
+ }
523
+ });
524
+ }
525
+
526
+ chooseAlignLine(): { v: DrawLine | undefined; h: DrawLine | undefined } {
527
+ // Principles:
528
+ // 1. Smaller difference from tolerance is more important
529
+ // 2. Same tolerance, (if multiple lines in the same direction are within the tolerance range) Importance: hcenter > top > bottom; vcenter > left > right
530
+ // 3. At most: vertical one count; horizon one count
531
+ // 4. Repeat line, will be subject to the longest (note: can't go to heavy, for performance, in the array of selected again)
532
+
533
+ // v direction line selection rule: draggingBBox's left/vcenter/right compares to vContrastPoints
534
+ // h direction line selection rule: draggingBBox's top/hcenter/bottom compares to hContrastPoints
535
+
536
+ if (
537
+ this.alignLinesForChoose.v.length + this.alignLinesForChoose.h.length ===
538
+ 0
539
+ ) {
540
+ return { v: undefined, h: undefined };
541
+ }
542
+
543
+ let vTmpLines: DrawLineForChoose[] = [];
544
+ let hTmpLines: DrawLineForChoose[] = [];
545
+
546
+ // 1.v,h direction: select the line with minimum offset
547
+ const vMinOffset = Math.min(
548
+ ...this.alignLinesForChoose.v.map((dlFc) => dlFc.offset.x),
549
+ );
550
+ const hMinOffset = Math.min(
551
+ ...this.alignLinesForChoose.h.map((dlFc) => dlFc.offset.y),
552
+ );
553
+
554
+ vTmpLines = this.alignLinesForChoose.v.filter(
555
+ (dlFc) => dlFc.offset.x === vMinOffset,
556
+ );
557
+ hTmpLines = this.alignLinesForChoose.h.filter(
558
+ (dlFc) => dlFc.offset.y === hMinOffset,
559
+ );
560
+
561
+ // 2.same tolerance,hcenter > top > bottom; vcenter > left > right. (The principle of selecting only one direction)
562
+ {
563
+ const vcenter_hasdata = vTmpLines.some(
564
+ (dlFc) => dlFc.dl.lp === 'vcenter',
565
+ );
566
+ const left_hasdata = vTmpLines.some((dlFc) => dlFc.dl.lp === 'left');
567
+
568
+ vTmpLines = vTmpLines.filter((dlFc) => {
569
+ if (dlFc.dl.lp === 'vcenter') {
570
+ return true;
571
+ }
572
+
573
+ if (dlFc.dl.lp === 'left') {
574
+ if (!vcenter_hasdata && left_hasdata) {
575
+ return true;
576
+ }
577
+ return false;
578
+ }
579
+
580
+ if (dlFc.dl.lp === 'right') {
581
+ if (!vcenter_hasdata && !left_hasdata) {
582
+ return true;
583
+ }
584
+ return false;
585
+ }
586
+
587
+ return false;
588
+ });
589
+
590
+ const hcenter_hasdata = hTmpLines.some(
591
+ (dlFc) => dlFc.dl.lp === 'hcenter',
592
+ );
593
+ const top_hasdata = hTmpLines.some((dlFc) => dlFc.dl.lp === 'top');
594
+
595
+ hTmpLines = hTmpLines.filter((dlFc) => {
596
+ if (dlFc.dl.lp === 'hcenter') {
597
+ return true;
598
+ }
599
+
600
+ if (dlFc.dl.lp === 'top') {
601
+ if (!hcenter_hasdata && top_hasdata) {
602
+ return true;
603
+ }
604
+ return false;
605
+ }
606
+
607
+ if (dlFc.dl.lp === 'bottom') {
608
+ if (!hcenter_hasdata && !top_hasdata) {
609
+ return true;
610
+ }
611
+ return false;
612
+ }
613
+
614
+ return false;
615
+ });
616
+ }
617
+
618
+ // 3.remove duplicates
619
+ const uniVTmpLines = Array.from(
620
+ vTmpLines
621
+ .reduce((map, item) => {
622
+ if (
623
+ !map.has(item.dl.lp) ||
624
+ Math.abs(item.offset.x) < Math.abs(map.get(item.dl.lp)?.offset.x)
625
+ ) {
626
+ map.set(item.dl.lp, item);
627
+ }
628
+ return map;
629
+ }, new Map<string, (typeof vTmpLines)[0]>())
630
+ .values(),
631
+ );
632
+
633
+ const uniHTmpLines = Array.from(
634
+ hTmpLines
635
+ .reduce((map, item) => {
636
+ if (
637
+ !map.has(item.dl.lp) ||
638
+ Math.abs(item.offset.y) < Math.abs(map.get(item.dl.lp)?.offset.y)
639
+ ) {
640
+ map.set(item.dl.lp, item);
641
+ }
642
+ return map;
643
+ }, new Map<string, (typeof vTmpLines)[0]>())
644
+ .values(),
645
+ );
646
+
647
+ // 4.choose a maximum offset one of DrawLineForChoose
648
+ const vMaxDL = uniVTmpLines.length
649
+ ? uniVTmpLines.reduce(
650
+ (maxItem: DrawLineForChoose, curItem: DrawLineForChoose) => {
651
+ if (curItem.offset.x > maxItem.offset.x) {
652
+ return curItem;
653
+ } else {
654
+ return maxItem;
655
+ }
656
+ },
657
+ )
658
+ : undefined;
659
+
660
+ const hMaxDL = uniHTmpLines.length
661
+ ? uniHTmpLines.reduce(
662
+ (maxItem: DrawLineForChoose, curItem: DrawLineForChoose) => {
663
+ if (curItem.offset.y > maxItem.offset.y) {
664
+ return curItem;
665
+ } else {
666
+ return maxItem;
667
+ }
668
+ },
669
+ )
670
+ : undefined;
671
+
672
+ return {
673
+ v: vMaxDL ? vMaxDL.dl : undefined,
674
+ h: hMaxDL ? hMaxDL.dl : undefined,
675
+ };
676
+ }
677
+
678
+ /**
679
+ * v and h direction adsorption state
680
+ */
681
+ private isAdsorbed: [boolean, boolean] = [false, false]; // whether the adsorption state
682
+
683
+ private dragEvent: IG6GraphEvent = undefined; // drag event
684
+
685
+ // use dragItemId and dragItemType to avoid object changing when dragging
686
+ private dragItemId: ID = undefined;
687
+ private dragItemType: ITEM_TYPE | 'canvas' = undefined;
688
+
689
+ /**
690
+ * Historical adsorption region: v and h directions
691
+ * x0 ~ x1: Represents the starting and ending line in the v direction
692
+ * y0 ~ y1: Represents the starting and ending line in the h direction
693
+ *
694
+ * When the cursor moves out of this region && non-adsorbed state => set to undefined
695
+ */
696
+ private historyAbsorbArea: [
697
+ [x0: number, x1: number] | undefined,
698
+ [y0: number, y1: number] | undefined,
699
+ ] = [undefined, undefined];
700
+
701
+ /**
702
+ * Unable to adsorb: countdown
703
+ */
704
+ private canAdsorbCount = 0;
705
+
706
+ /**
707
+ * Countdown method: Countdown
708
+ */
709
+ private canAdsorbCountDown = () => {
710
+ if (this.canAdsorbCount > 0) {
711
+ const intervalID = setInterval(() => {
712
+ this.canAdsorbCount--;
713
+
714
+ if (this.canAdsorbCount <= 0) {
715
+ clearTimeout(intervalID);
716
+ }
717
+ }, 100);
718
+ }
719
+ };
720
+
721
+ constructor(options?: SnapLineConfig) {
722
+ super(options);
723
+
724
+ this.init(options.graph);
725
+ }
726
+
727
+ public getEvents() {
728
+ return {
729
+ pointerdown: this.onPointerDown,
730
+ pointermove: this.onPointerMove,
731
+ pointerup: this.onPointerUp,
732
+ };
733
+ }
734
+
735
+ public onPointerDown(event: IG6GraphEvent) {
736
+ this.historyPoints[1] = { x: event.canvas.x, y: event.canvas.y };
737
+
738
+ this.dragItemId = event.itemId;
739
+ this.dragItemType = event.itemType;
740
+ }
741
+
742
+ public onPointerMove(event: IG6GraphEvent) {
743
+ if (!this.historyPoints[1]) return;
744
+ if (this.dragItemType !== 'node') {
745
+ return;
746
+ }
747
+
748
+ // pointerDown + first move = start drag
749
+ if (!this.dragging) {
750
+ this.onDragStart(event);
751
+ this.dragging = true;
752
+ } else {
753
+ this.onDrag(event);
754
+ }
755
+ }
756
+
757
+ public onPointerUp(event: IG6GraphEvent) {
758
+ // pointerUp + dragging = onDragEnd
759
+ if (this.dragging) {
760
+ this.onDragEnd();
761
+ } else {
762
+ this.historyPoints = [undefined, undefined];
763
+ this.dragging = false;
764
+ this.dragItemId = undefined;
765
+ this.dragItemType = undefined;
766
+ }
767
+ }
768
+
769
+ getGraph(event: IG6GraphEvent): IGraph {
770
+ return event.currentTarget;
771
+ }
772
+
773
+ //#region event handler
774
+ onDragStart(event: IG6GraphEvent) {
775
+ this.draggingBBox = this.graph.getRenderBBox(event.itemId, true) as AABB;
776
+ this.initialOffset = [
777
+ event.canvas.x - this.draggingBBox.center[0],
778
+ event.canvas.y - this.draggingBBox.center[1],
779
+ ];
780
+
781
+ this.nonAbosorbOffset = this.getPointerOffsetWithItem({
782
+ x: event.canvas.x,
783
+ y: event.canvas.y,
784
+ });
785
+ }
786
+
787
+ onDrag = throttle(
788
+ ((event: IG6GraphEvent) => {
789
+ this.dragEvent = event;
790
+
791
+ this.initAlignLinesForChoose();
792
+
793
+ // control layer:Check to delete existing line & drawed line & adsorption
794
+ // set historyPoints
795
+
796
+ this.historyPoints[0] = this.historyPoints[1];
797
+ this.historyPoints[1] = { x: event.canvas.x, y: event.canvas.y };
798
+
799
+ let vlp: LinePosition = undefined;
800
+ let vline: [Point, Point] = undefined;
801
+ let hlp: LinePosition = undefined;
802
+ let hline: [Point, Point] = undefined;
803
+
804
+ // v direction: whether keep adsorption
805
+ const checkVShouldHoldAbsorb = (vlp, vline) => {
806
+ // v direction: continue to adsorb
807
+ const vShouldAbsorb =
808
+ Math.abs(this.getCurOffsetCompareToInitialOffset(event)[0]) <=
809
+ this.tolerance * this.toleranceFactor;
810
+
811
+ if (vShouldAbsorb) {
812
+ if (vlp === 'left') {
813
+ this.doUpdatePosition({
814
+ x: vline[0].x + this.draggingBBox.halfExtents[0],
815
+ });
816
+ } else if (vlp === 'vcenter') {
817
+ this.doUpdatePosition({
818
+ x: vline[0].x,
819
+ });
820
+ } else if (vlp === 'right') {
821
+ this.doUpdatePosition({
822
+ fx: vline[0].x - this.draggingBBox.halfExtents[0],
823
+ });
824
+ }
825
+
826
+ // updarte align line when absorb
827
+ this.updateAlignLineWhenAbsorb([true, false]);
828
+ } else {
829
+ // cancel absorb
830
+ // 1.update positon
831
+ this.doUpdatePosition({ fx: undefined });
832
+ // 2.remove align line && update state
833
+ this.removeAlignLine([true, undefined]);
834
+
835
+ // count down to be adsorbable
836
+ this.canAdsorbCount = 10; // 1s
837
+ this.canAdsorbCountDown();
838
+ }
839
+ };
840
+ // h direction: whether keep adsorption
841
+ const checkHShouldHoldAbsorb = (hlp, hline) => {
842
+ // h direction: continue to adsorb
843
+ const hShouldAbsorb =
844
+ Math.abs(this.getCurOffsetCompareToInitialOffset(event)[1]) <=
845
+ this.tolerance * this.toleranceFactor;
846
+
847
+ if (hShouldAbsorb) {
848
+ if (hlp === 'top') {
849
+ this.doUpdatePosition({
850
+ fy: hline[0].y + this.draggingBBox.halfExtents[1],
851
+ });
852
+ } else if (hlp === 'hcenter') {
853
+ this.doUpdatePosition({
854
+ fy: hline[0].y,
855
+ });
856
+ } else if (hlp === 'bottom') {
857
+ this.doUpdatePosition({
858
+ fy: hline[0].y - this.draggingBBox.halfExtents[1],
859
+ });
860
+ }
861
+
862
+ // update align line
863
+ this.updateAlignLineWhenAbsorb([false, true]);
864
+ } else {
865
+ // 1.update position
866
+ this.doUpdatePosition({ fy: undefined });
867
+ // 2.remove align line && update state
868
+ this.removeAlignLine([undefined, true]);
869
+
870
+ // count down to be adsorbable
871
+ this.canAdsorbCount = 10; // 1s
872
+ this.canAdsorbCountDown();
873
+ }
874
+ };
875
+ // v direction: whether adsorbed
876
+ const checkVShouldAbsorb = () => {
877
+ const ret: { vdl: DrawLine; hdl: DrawLine } | false =
878
+ this.canDrawLine();
879
+
880
+ // If in recent from adsorption area, can't immediately draw lines (must be removed from the area to reline this area)
881
+ if (ret) {
882
+ // draw align lines
883
+ this.drawAlignLines({ vdl: ret.vdl, hdl: undefined });
884
+ // absorb dragging box
885
+ this.absorb({ vdl: ret.vdl, hdl: undefined });
886
+ }
887
+ };
888
+ // h direction: whether adsorbed
889
+ const checkHShouldAbsorb = () => {
890
+ const ret: { vdl: DrawLine; hdl: DrawLine } | false =
891
+ this.canDrawLine();
892
+
893
+ // If in recent from adsorption area, can't immediately draw lines (must be removed from the area to reline this area)
894
+ if (ret) {
895
+ // draw align lines
896
+ this.drawAlignLines({ vdl: undefined, hdl: ret.hdl });
897
+ // absorb dragging box
898
+ this.absorb({ vdl: undefined, hdl: ret.hdl });
899
+ }
900
+ };
901
+
902
+ // 1、the current state of the two directions are adsorption
903
+ if (this.isAdsorbed[0] && this.isAdsorbed[1]) {
904
+ vlp = this.drawedLines[0].dl?.lp;
905
+ vline = this.drawedLines[0].dl?.line;
906
+ hlp = this.drawedLines[1].dl?.lp;
907
+ hline = this.drawedLines[1].dl?.line;
908
+
909
+ checkVShouldHoldAbsorb(vlp, vline);
910
+ checkHShouldHoldAbsorb(hlp, hline);
911
+ } else if (this.isAdsorbed[0] && !this.isAdsorbed[1]) {
912
+ // 2、currently, only the v direction is adsorbed
913
+ vlp = this.drawedLines[0].dl?.lp;
914
+ vline = this.drawedLines[0].dl?.line;
915
+
916
+ checkVShouldHoldAbsorb(vlp, vline);
917
+ checkHShouldAbsorb();
918
+ } else if (!this.isAdsorbed[0] && this.isAdsorbed[1]) {
919
+ // 3、currently, only the h direction is adsorbed
920
+ hlp = this.drawedLines[1].dl?.lp;
921
+ hline = this.drawedLines[1].dl?.line;
922
+
923
+ checkHShouldHoldAbsorb(vlp, vline);
924
+ checkVShouldAbsorb();
925
+ } else if (!this.isAdsorbed[0] && !this.isAdsorbed[1]) {
926
+ // 4、currently, v and h directions are not adsorbed
927
+ const ret: { vdl: DrawLine; hdl: DrawLine } | false =
928
+ this.canDrawLine();
929
+
930
+ // If you have recently detached from the attachment area, you cannot draw the line immediately (you need to remove the detached area to redraw the area)
931
+ if (ret) {
932
+ // draw align lines
933
+ this.drawAlignLines(ret);
934
+ // absorb dragging box
935
+ this.absorb(ret);
936
+ }
937
+ }
938
+ }).bind(this),
939
+ 10,
940
+ {
941
+ leading: true,
942
+ trailing: false,
943
+ },
944
+ );
945
+
946
+ onDragEnd() {
947
+ this.dragEvent = undefined;
948
+ this.historyPoints = [undefined, undefined];
949
+ this.dragging = false;
950
+ this.dragItemId = undefined;
951
+ this.dragItemType = undefined;
952
+ }
953
+
954
+ //#region draw/remove lines
955
+ // first:v direction; second:h direction;
956
+ // ddo: drawed DisplayObject, dl: DrawLine
957
+ private drawedLines: [
958
+ { do: DisplayObject | undefined; dl: DrawLine | undefined } | undefined,
959
+ { do: DisplayObject | undefined; dl: DrawLine | undefined } | undefined,
960
+ ] = [undefined, undefined];
961
+
962
+ drawAlignLines({
963
+ vdl,
964
+ hdl,
965
+ }: {
966
+ vdl: DrawLine | undefined;
967
+ hdl: DrawLine | undefined;
968
+ }) {
969
+ // 1.draw align line
970
+ const vLineID: ID = vdl ? this.getDrawLineIdByDrawLine(vdl) : undefined;
971
+ const hLineID: ID = hdl ? this.getDrawLineIdByDrawLine(hdl) : undefined;
972
+
973
+ let vLine: DisplayObject = undefined;
974
+ if (vdl) {
975
+ vLine = this.graph.drawTransient('line', vLineID, {
976
+ style: {
977
+ stroke: 'red',
978
+ lineWidth: 1,
979
+ x1: vdl.line[0].x,
980
+ y1: vdl.line[0].y,
981
+ x2: vdl.line[1].x,
982
+ y2: vdl.line[1].y,
983
+ },
984
+ });
985
+ // add to alignLines
986
+ this.drawedLines[0] = { do: vLine, dl: vdl };
987
+ }
988
+
989
+ let hLine: DisplayObject = undefined;
990
+ if (hdl) {
991
+ hLine = this.graph.drawTransient('line', hLineID, {
992
+ style: {
993
+ stroke: 'red',
994
+ lineWidth: 1,
995
+ x1: hdl.line[0].x,
996
+ y1: hdl.line[0].y,
997
+ x2: hdl.line[1].x,
998
+ y2: hdl.line[1].y,
999
+ },
1000
+ });
1001
+ // add to alignLines
1002
+ this.drawedLines[1] = { do: hLine, dl: hdl };
1003
+ }
1004
+ }
1005
+
1006
+ /**
1007
+ * remove align line
1008
+ * @param rvdl: remove vertical drawed line
1009
+ * @param rhdl: remove horizon drawed line
1010
+ */
1011
+ removeAlignLine([rvdl, rhdl]: [
1012
+ rvdl: boolean | undefined,
1013
+ rhdl: boolean | undefined,
1014
+ ]) {
1015
+ if (rvdl) {
1016
+ const vlid = this.getDrawLineIdByDrawLine(this.drawedLines[0].dl);
1017
+ this.graph.drawTransient('line', vlid, { action: 'remove' });
1018
+ this.drawedLines[0] = undefined;
1019
+
1020
+ this.isAdsorbed[0] = false;
1021
+ }
1022
+
1023
+ if (rhdl) {
1024
+ const hlid = this.getDrawLineIdByDrawLine(this.drawedLines[1].dl);
1025
+
1026
+ this.graph.drawTransient('line', hlid, { action: 'remove' });
1027
+ this.drawedLines[1] = undefined;
1028
+
1029
+ this.isAdsorbed[1] = false;
1030
+ }
1031
+ }
1032
+
1033
+ /**
1034
+ * via ID: remove align line
1035
+ * @param rmLineID
1036
+ */
1037
+ removeAlignLineByID(rmLineID: ID) {
1038
+ // remove from DisplayObject
1039
+
1040
+ const [vTmpDrawedLine, hTmpDrawedLine] = this.drawedLines;
1041
+ this.drawedLines.forEach((line, index) => {
1042
+ if (line && line.do) {
1043
+ const lineID = this.getDrawLineIdByPoints(line.dl.line);
1044
+ if (lineID === rmLineID) {
1045
+ if (index === 0 && vTmpDrawedLine) {
1046
+ vTmpDrawedLine.dl = undefined;
1047
+ vTmpDrawedLine.do = undefined;
1048
+ } else if (index === 1 && hTmpDrawedLine) {
1049
+ hTmpDrawedLine.dl = undefined;
1050
+ hTmpDrawedLine.do = undefined;
1051
+ }
1052
+
1053
+ this.isAdsorbed[index] = false;
1054
+ }
1055
+ }
1056
+ });
1057
+
1058
+ this.drawedLines[0] = vTmpDrawedLine;
1059
+ this.drawedLines[1] = hTmpDrawedLine;
1060
+
1061
+ // remove drawed line
1062
+ this.graph.drawTransient('line', rmLineID, { action: 'remove' });
1063
+ }
1064
+
1065
+ /**
1066
+ * get draggingBBox Point based on LinePosition
1067
+ */
1068
+ getDraggingBBoxPointByLinePosition(lp: LinePosition): Point | undefined {
1069
+ if (!this.draggingBBox) {
1070
+ return undefined;
1071
+ }
1072
+
1073
+ if (lp === 'left') {
1074
+ return { x: this.draggingBBox.min[0], y: this.draggingBBox.center[1] };
1075
+ }
1076
+ if (lp === 'vcenter' || lp === 'hcenter') {
1077
+ return { x: this.draggingBBox.center[0], y: this.draggingBBox.center[1] };
1078
+ }
1079
+ if (lp === 'right') {
1080
+ return { x: this.draggingBBox.max[0], y: this.draggingBBox.center[1] };
1081
+ }
1082
+ if (lp === 'top') {
1083
+ return { x: this.draggingBBox.center[0], y: this.draggingBBox.min[1] };
1084
+ }
1085
+ if (lp === 'bottom') {
1086
+ return { x: this.draggingBBox.center[0], y: this.draggingBBox.max[1] };
1087
+ }
1088
+
1089
+ return undefined;
1090
+ }
1091
+
1092
+ /**
1093
+ * update align line under condition of absorbed
1094
+ */
1095
+ updateAlignLineWhenAbsorb([v, h]: [boolean, boolean]) {
1096
+ if (v) {
1097
+ if (this.drawedLines[0] && this.drawedLines[0].do) {
1098
+ const draggingBBoxDrawEndPoint: Point =
1099
+ this.getDraggingBBoxPointByLinePosition(this.drawedLines[0].dl.lp);
1100
+ const vdl: DrawLine = {
1101
+ line: [
1102
+ this.drawedLines[0].dl.line[0],
1103
+ {
1104
+ x: this.drawedLines[0].dl.line[0].x,
1105
+ y: draggingBBoxDrawEndPoint.y,
1106
+ },
1107
+ ],
1108
+ lp: this.drawedLines[0].dl.lp,
1109
+ };
1110
+ this.removeAlignLineByID(this.drawedLines[0].do.id);
1111
+ this.drawAlignLines({ vdl: vdl, hdl: undefined });
1112
+ this.isAdsorbed[0] = true;
1113
+ }
1114
+ }
1115
+
1116
+ if (h) {
1117
+ if (this.drawedLines[1] && this.drawedLines[1].do) {
1118
+ const draggingBBoxDrawEndPoint: Point =
1119
+ this.getDraggingBBoxPointByLinePosition(this.drawedLines[1].dl.lp);
1120
+ const hdl: DrawLine = {
1121
+ line: [
1122
+ this.drawedLines[1].dl.line[0],
1123
+ {
1124
+ x: draggingBBoxDrawEndPoint.x,
1125
+ y: this.drawedLines[1].dl.line[0].y,
1126
+ },
1127
+ ],
1128
+ lp: this.drawedLines[1].dl.lp,
1129
+ };
1130
+ this.removeAlignLineByID(this.drawedLines[1].do.id);
1131
+ this.drawAlignLines({ vdl: undefined, hdl: hdl });
1132
+ this.isAdsorbed[1] = true;
1133
+ }
1134
+ }
1135
+ }
1136
+
1137
+ /**
1138
+ * check whether can draw lines
1139
+ */
1140
+ canDrawLine(): { vdl: DrawLine; hdl: DrawLine } | false {
1141
+ const { v, h }: { v: DrawLine | undefined; h: DrawLine | undefined } =
1142
+ this.chooseAlignLine();
1143
+ if (!v && !h) {
1144
+ return false;
1145
+ }
1146
+
1147
+ return { vdl: v, hdl: h };
1148
+ }
1149
+
1150
+ /**
1151
+ * detect whether adsorption can be cancelled
1152
+ * two directions:v h
1153
+ * @return first: v direction ; second:h direction; undefine means no-adsorb
1154
+ */
1155
+ canCancelAbsorb(): [boolean | undefined, boolean | undefined] {
1156
+ // eslint-disable-next-line prefer-const
1157
+ let ret: [boolean | undefined, boolean | undefined] = [
1158
+ undefined,
1159
+ undefined,
1160
+ ];
1161
+
1162
+ const vdl = this.drawedLines[0].dl;
1163
+ const hdl = this.drawedLines[1].dl;
1164
+
1165
+ const offset = this.getPointerOffsetWithItem({
1166
+ x: this.dragEvent.canvas.x,
1167
+ y: this.dragEvent.canvas.y,
1168
+ });
1169
+
1170
+ if (vdl) {
1171
+ if (Math.abs(offset.x) > this.tolerance * this.toleranceFactor) {
1172
+ ret[0] = true;
1173
+ }
1174
+ }
1175
+ if (hdl) {
1176
+ if (Math.abs(offset.y) > this.tolerance * this.toleranceFactor) {
1177
+ ret[1] = true;
1178
+ }
1179
+ }
1180
+
1181
+ return ret;
1182
+ }
1183
+
1184
+ //#endregion
1185
+
1186
+ /**
1187
+ * absorb the dragging box
1188
+ */
1189
+ absorb(dls: { vdl: DrawLine; hdl: DrawLine }) {
1190
+ if (dls.vdl) {
1191
+ this.isAdsorbed[0] = true;
1192
+ this.historyAbsorbArea[0] = [
1193
+ dls.vdl.line[0].x - this.tolerance * this.toleranceFactor,
1194
+ dls.vdl.line[0].x + this.tolerance * this.toleranceFactor,
1195
+ ];
1196
+ this.updateDraggingItemPosition(this.drawedLines[0].dl);
1197
+ }
1198
+ if (dls.hdl) {
1199
+ this.isAdsorbed[1] = true;
1200
+ this.historyAbsorbArea[1] = [
1201
+ dls.hdl.line[0].y - this.tolerance * this.toleranceFactor,
1202
+ dls.hdl.line[0].y + this.tolerance * this.toleranceFactor,
1203
+ ];
1204
+ this.updateDraggingItemPosition(this.drawedLines[1].dl);
1205
+ }
1206
+ }
1207
+
1208
+ /**
1209
+ * If there is dl(draw line), then absorb to dl;
1210
+ * don't pass dl, means cancel absorb
1211
+ * @param dl drawline [point, point]
1212
+ */
1213
+ updateDraggingItemPosition(
1214
+ dl?: DrawLine,
1215
+ shouldChange?: { x?: number; y?: number },
1216
+ ) {
1217
+ // Determine if historyPoints do not exist do not continue
1218
+ if (!this.historyPoints[0] && !this.historyPoints[1]) {
1219
+ return;
1220
+ }
1221
+
1222
+ // absorb effect
1223
+ if (dl) {
1224
+ // calculate the diff and absorb
1225
+ if (dl.lp === 'left') {
1226
+ const tmpX = dl.line[0].x + this.draggingBBox.halfExtents[0];
1227
+ this.doUpdatePosition({ fx: tmpX });
1228
+ }
1229
+ if (dl.lp === 'vcenter') {
1230
+ this.doUpdatePosition({
1231
+ fx: dl.line[0].x,
1232
+ });
1233
+ }
1234
+ if (dl.lp === 'right') {
1235
+ this.doUpdatePosition({
1236
+ fx: dl.line[0].x - this.draggingBBox.halfExtents[0],
1237
+ });
1238
+ }
1239
+ if (dl.lp === 'top') {
1240
+ this.doUpdatePosition({
1241
+ fy: dl.line[0].y + this.draggingBBox.halfExtents[1],
1242
+ });
1243
+ }
1244
+ if (dl.lp === 'hcenter') {
1245
+ this.doUpdatePosition({
1246
+ fy: dl.line[0].y,
1247
+ });
1248
+ }
1249
+ if (dl.lp === 'bottom') {
1250
+ this.doUpdatePosition({
1251
+ fy: dl.line[0].y - this.draggingBBox.halfExtents[1],
1252
+ });
1253
+ }
1254
+ }
1255
+
1256
+ if (shouldChange) {
1257
+ if (shouldChange.x) {
1258
+ const changeX = this.draggingBBox.center[0] + shouldChange.x;
1259
+ this.doUpdatePosition({
1260
+ x: changeX,
1261
+ fx: undefined,
1262
+ });
1263
+ }
1264
+ if (shouldChange.y) {
1265
+ const changeY = this.draggingBBox.center[1] + shouldChange.y;
1266
+ this.doUpdatePosition({
1267
+ y: changeY,
1268
+ fy: undefined,
1269
+ });
1270
+ }
1271
+ }
1272
+ }
1273
+
1274
+ doUpdatePosition(data) {
1275
+ this.graph.updateNodePosition({ id: this.dragItemId, data }, false, true);
1276
+ }
1277
+ }