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