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

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