@thienvu18/designable-shared 2.0.1 → 2.0.2

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 (73) hide show
  1. package/esm/animation.d.ts +8 -2
  2. package/esm/animation.js +29 -30
  3. package/esm/array.d.ts +163 -37
  4. package/esm/array.js +104 -87
  5. package/esm/clone.d.ts +4 -4
  6. package/esm/clone.js +79 -86
  7. package/esm/compose.d.ts +3 -1
  8. package/esm/compose.js +6 -6
  9. package/esm/coordinate.d.ts +120 -76
  10. package/esm/coordinate.js +480 -411
  11. package/esm/element.d.ts +11 -6
  12. package/esm/element.js +123 -125
  13. package/esm/event.d.ts +162 -75
  14. package/esm/event.js +197 -197
  15. package/esm/globalThisPolyfill.d.ts +1 -1
  16. package/esm/globalThisPolyfill.js +14 -17
  17. package/esm/index.d.ts +17 -17
  18. package/esm/index.js +17 -17
  19. package/esm/instanceof.d.ts +1 -1
  20. package/esm/instanceof.js +9 -10
  21. package/esm/keycode.d.ts +145 -145
  22. package/esm/keycode.js +149 -149
  23. package/esm/lru.d.ts +50 -47
  24. package/esm/lru.js +236 -245
  25. package/esm/observer.d.ts +7 -7
  26. package/esm/observer.js +37 -40
  27. package/esm/request-idle.d.ts +9 -6
  28. package/esm/request-idle.js +6 -6
  29. package/esm/scroller.d.ts +24 -8
  30. package/esm/scroller.js +78 -75
  31. package/esm/subscribable.d.ts +10 -10
  32. package/esm/subscribable.js +39 -40
  33. package/esm/types.d.ts +13 -13
  34. package/esm/types.js +19 -20
  35. package/esm/uid.d.ts +1 -1
  36. package/esm/uid.js +7 -7
  37. package/lib/animation.d.ts +8 -2
  38. package/lib/animation.js +34 -35
  39. package/lib/array.d.ts +163 -37
  40. package/lib/array.js +129 -101
  41. package/lib/clone.d.ts +4 -4
  42. package/lib/clone.js +84 -91
  43. package/lib/compose.d.ts +3 -1
  44. package/lib/compose.js +10 -10
  45. package/lib/coordinate.d.ts +120 -76
  46. package/lib/coordinate.js +538 -440
  47. package/lib/element.d.ts +11 -6
  48. package/lib/element.js +139 -133
  49. package/lib/event.d.ts +162 -75
  50. package/lib/event.js +206 -202
  51. package/lib/globalThisPolyfill.d.ts +1 -1
  52. package/lib/globalThisPolyfill.js +17 -20
  53. package/lib/index.d.ts +17 -17
  54. package/lib/index.js +49 -33
  55. package/lib/instanceof.d.ts +1 -1
  56. package/lib/instanceof.js +13 -14
  57. package/lib/keycode.d.ts +145 -145
  58. package/lib/keycode.js +153 -153
  59. package/lib/lru.d.ts +50 -47
  60. package/lib/lru.js +240 -249
  61. package/lib/observer.d.ts +7 -7
  62. package/lib/observer.js +41 -44
  63. package/lib/request-idle.d.ts +9 -6
  64. package/lib/request-idle.js +14 -11
  65. package/lib/scroller.d.ts +24 -8
  66. package/lib/scroller.js +89 -81
  67. package/lib/subscribable.d.ts +10 -10
  68. package/lib/subscribable.js +43 -44
  69. package/lib/types.d.ts +13 -13
  70. package/lib/types.js +40 -28
  71. package/lib/uid.d.ts +1 -1
  72. package/lib/uid.js +11 -11
  73. package/package.json +1 -1
package/lib/coordinate.js CHANGED
@@ -1,502 +1,600 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calcClosestEdges = exports.calcCombineSnapLineSegment = exports.calcDistanceOfSnapLineToEdges = exports.calcOffsetOfSnapLineSegmentToEdge = exports.calcExtendsLineSegmentOfRect = exports.calcSpaceBlockOfRect = exports.calcRectOfAxisLineSegment = exports.calcEdgeLinesOfRect = exports.calcRectByStartEndPoint = exports.calcBoundingRect = exports.calcRelativeOfPointToRect = exports.isNearAfter = exports.calcDistancePointToEdge = exports.calcDistanceOfPointToRect = exports.calcQuadrantOfPointToRect = exports.isCrossRectInRect = exports.isRectInRect = exports.getRectPoints = exports.isEqualRect = exports.isPointInRect = exports.RectQuadrant = exports.LineSegment = exports.Rect = exports.Point = exports.isLineSegment = exports.isPoint = exports.isRect = void 0;
4
- const types_1 = require("./types");
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.calcClosestEdges =
4
+ exports.calcCombineSnapLineSegment =
5
+ exports.calcDistanceOfSnapLineToEdges =
6
+ exports.calcOffsetOfSnapLineSegmentToEdge =
7
+ exports.calcExtendsLineSegmentOfRect =
8
+ exports.calcSpaceBlockOfRect =
9
+ exports.calcRectOfAxisLineSegment =
10
+ exports.calcEdgeLinesOfRect =
11
+ exports.calcRectByStartEndPoint =
12
+ exports.calcBoundingRect =
13
+ exports.calcRelativeOfPointToRect =
14
+ exports.isNearAfter =
15
+ exports.calcDistancePointToEdge =
16
+ exports.calcDistanceOfPointToRect =
17
+ exports.calcQuadrantOfPointToRect =
18
+ exports.isCrossRectInRect =
19
+ exports.isRectInRect =
20
+ exports.getRectPoints =
21
+ exports.isEqualRect =
22
+ exports.isPointInRect =
23
+ exports.RectQuadrant =
24
+ exports.LineSegment =
25
+ exports.Rect =
26
+ exports.Point =
27
+ exports.isLineSegment =
28
+ exports.isPoint =
29
+ exports.isRect =
30
+ void 0
31
+ const types_1 = require('./types')
5
32
  function isRect(rect) {
6
- return rect?.x && rect?.y && rect?.width && rect?.height;
33
+ return rect?.x && rect?.y && rect?.width && rect?.height
7
34
  }
8
- exports.isRect = isRect;
35
+ exports.isRect = isRect
9
36
  function isPoint(val) {
10
- return (0, types_1.isValidNumber)(val?.x) && (0, types_1.isValidNumber)(val?.y);
37
+ return (
38
+ (0, types_1.isValidNumber)(val?.x) && (0, types_1.isValidNumber)(val?.y)
39
+ )
11
40
  }
12
- exports.isPoint = isPoint;
41
+ exports.isPoint = isPoint
13
42
  function isLineSegment(val) {
14
- return isPoint(val?.start) && isPoint(val?.end);
43
+ return isPoint(val?.start) && isPoint(val?.end)
15
44
  }
16
- exports.isLineSegment = isLineSegment;
45
+ exports.isLineSegment = isLineSegment
17
46
  class Point {
18
- constructor(x, y) {
19
- this.x = x;
20
- this.y = y;
21
- }
47
+ constructor(x, y) {
48
+ this.x = x
49
+ this.y = y
50
+ }
22
51
  }
23
- exports.Point = Point;
52
+ exports.Point = Point
24
53
  class Rect {
25
- constructor(x, y, width, height) {
26
- this.x = 0;
27
- this.y = 0;
28
- this.width = 0;
29
- this.height = 0;
30
- this.x = x;
31
- this.y = y;
32
- this.width = width;
33
- this.height = height;
34
- }
35
- get left() {
36
- return this.x;
37
- }
38
- get right() {
39
- return this.x + this.width;
40
- }
41
- get top() {
42
- return this.y;
43
- }
44
- get bottom() {
45
- return this.y + this.height;
46
- }
54
+ constructor(x, y, width, height) {
55
+ this.x = 0
56
+ this.y = 0
57
+ this.width = 0
58
+ this.height = 0
59
+ this.x = x
60
+ this.y = y
61
+ this.width = width
62
+ this.height = height
63
+ }
64
+ get left() {
65
+ return this.x
66
+ }
67
+ get right() {
68
+ return this.x + this.width
69
+ }
70
+ get top() {
71
+ return this.y
72
+ }
73
+ get bottom() {
74
+ return this.y + this.height
75
+ }
47
76
  }
48
- exports.Rect = Rect;
77
+ exports.Rect = Rect
49
78
  class LineSegment {
50
- constructor(start, end) {
51
- this.start = { ...start };
52
- this.end = { ...end };
53
- }
79
+ constructor(start, end) {
80
+ this.start = { ...start }
81
+ this.end = { ...end }
82
+ }
54
83
  }
55
- exports.LineSegment = LineSegment;
56
- var RectQuadrant;
57
- (function (RectQuadrant) {
58
- RectQuadrant["Inner1"] = "I1";
59
- RectQuadrant["Inner2"] = "I2";
60
- RectQuadrant["Inner3"] = "I3";
61
- RectQuadrant["Inner4"] = "I4";
62
- RectQuadrant["Outer1"] = "O1";
63
- RectQuadrant["Outer2"] = "O2";
64
- RectQuadrant["Outer3"] = "O3";
65
- RectQuadrant["Outer4"] = "O4";
66
- })(RectQuadrant || (exports.RectQuadrant = RectQuadrant = {}));
84
+ exports.LineSegment = LineSegment
85
+ var RectQuadrant
86
+ ;(function (RectQuadrant) {
87
+ RectQuadrant['Inner1'] = 'I1'
88
+ RectQuadrant['Inner2'] = 'I2'
89
+ RectQuadrant['Inner3'] = 'I3'
90
+ RectQuadrant['Inner4'] = 'I4'
91
+ RectQuadrant['Outer1'] = 'O1'
92
+ RectQuadrant['Outer2'] = 'O2'
93
+ RectQuadrant['Outer3'] = 'O3'
94
+ RectQuadrant['Outer4'] = 'O4'
95
+ })(RectQuadrant || (exports.RectQuadrant = RectQuadrant = {}))
67
96
  function isPointInRect(point, rect, sensitive = true) {
68
- const boundSensor = (value) => {
69
- if (!sensitive)
70
- return 0;
71
- const sensor = value * 0.1;
72
- if (sensor > 20)
73
- return 20;
74
- if (sensor < 10)
75
- return 10;
76
- return sensor;
77
- };
78
- return (point.x >= rect.x + boundSensor(rect.width) &&
79
- point.x <= rect.x + rect.width - boundSensor(rect.width) &&
80
- point.y >= rect.y + boundSensor(rect.height) &&
81
- point.y <= rect.y + rect.height - boundSensor(rect.height));
97
+ const boundSensor = (value) => {
98
+ if (!sensitive) return 0
99
+ const sensor = value * 0.1
100
+ if (sensor > 20) return 20
101
+ if (sensor < 10) return 10
102
+ return sensor
103
+ }
104
+ return (
105
+ point.x >= rect.x + boundSensor(rect.width) &&
106
+ point.x <= rect.x + rect.width - boundSensor(rect.width) &&
107
+ point.y >= rect.y + boundSensor(rect.height) &&
108
+ point.y <= rect.y + rect.height - boundSensor(rect.height)
109
+ )
82
110
  }
83
- exports.isPointInRect = isPointInRect;
111
+ exports.isPointInRect = isPointInRect
84
112
  function isEqualRect(target, source) {
85
- return (target?.x === source?.x &&
86
- target.y === source.y &&
87
- target.width === source.width &&
88
- target.height === source.height);
113
+ return (
114
+ target?.x === source?.x &&
115
+ target.y === source.y &&
116
+ target.width === source.width &&
117
+ target.height === source.height
118
+ )
89
119
  }
90
- exports.isEqualRect = isEqualRect;
120
+ exports.isEqualRect = isEqualRect
91
121
  function getRectPoints(source) {
92
- const p1 = new Point(source.x, source.y);
93
- const p2 = new Point(source.x + source.width, source.y);
94
- const p3 = new Point(source.x + source.width, source.y + source.height);
95
- const p4 = new Point(source.x, source.y + source.height);
96
- return [p1, p2, p3, p4];
122
+ const p1 = new Point(source.x, source.y)
123
+ const p2 = new Point(source.x + source.width, source.y)
124
+ const p3 = new Point(source.x + source.width, source.y + source.height)
125
+ const p4 = new Point(source.x, source.y + source.height)
126
+ return [p1, p2, p3, p4]
97
127
  }
98
- exports.getRectPoints = getRectPoints;
128
+ exports.getRectPoints = getRectPoints
99
129
  function isRectInRect(target, source) {
100
- const [p1, p2, p3, p4] = getRectPoints(target);
101
- return (isPointInRect(p1, source, false) &&
102
- isPointInRect(p2, source, false) &&
103
- isPointInRect(p3, source, false) &&
104
- isPointInRect(p4, source, false));
130
+ const [p1, p2, p3, p4] = getRectPoints(target)
131
+ return (
132
+ isPointInRect(p1, source, false) &&
133
+ isPointInRect(p2, source, false) &&
134
+ isPointInRect(p3, source, false) &&
135
+ isPointInRect(p4, source, false)
136
+ )
105
137
  }
106
- exports.isRectInRect = isRectInRect;
138
+ exports.isRectInRect = isRectInRect
107
139
  function isCrossRectInRect(target, source) {
108
- const targetCenterPoint = new Point(target.x + target.width / 2, target.y + target.height / 2);
109
- const sourceCenterPoint = new Point(source.x + source.width / 2, source.y + source.height / 2);
110
- return (Math.abs(targetCenterPoint.x - sourceCenterPoint.x) <=
111
- target.width / 2 + source.width / 2 &&
112
- Math.abs(targetCenterPoint.y - sourceCenterPoint.y) <=
113
- target.height / 2 + source.height / 2);
140
+ const targetCenterPoint = new Point(
141
+ target.x + target.width / 2,
142
+ target.y + target.height / 2
143
+ )
144
+ const sourceCenterPoint = new Point(
145
+ source.x + source.width / 2,
146
+ source.y + source.height / 2
147
+ )
148
+ return (
149
+ Math.abs(targetCenterPoint.x - sourceCenterPoint.x) <=
150
+ target.width / 2 + source.width / 2 &&
151
+ Math.abs(targetCenterPoint.y - sourceCenterPoint.y) <=
152
+ target.height / 2 + source.height / 2
153
+ )
114
154
  }
115
- exports.isCrossRectInRect = isCrossRectInRect;
155
+ exports.isCrossRectInRect = isCrossRectInRect
116
156
  /**
117
157
  * 计算点在矩形的哪个象限
118
158
  * @param point
119
159
  * @param rect
120
160
  */
121
161
  function calcQuadrantOfPointToRect(point, rect) {
122
- const isInner = isPointInRect(point, rect);
123
- if (point.x <= rect.x + rect.width / 2) {
124
- if (point.y <= rect.y + rect.height / 2) {
125
- if (isInner) {
126
- return RectQuadrant.Inner1;
127
- }
128
- else {
129
- return RectQuadrant.Outer1;
130
- }
131
- }
132
- else {
133
- if (isInner) {
134
- return RectQuadrant.Inner4;
135
- }
136
- else {
137
- return RectQuadrant.Outer4;
138
- }
139
- }
140
- }
141
- else {
142
- if (point.y <= rect.y + rect.height / 2) {
143
- if (isInner) {
144
- return RectQuadrant.Inner2;
145
- }
146
- else {
147
- return RectQuadrant.Outer2;
148
- }
149
- }
150
- else {
151
- if (isInner) {
152
- return RectQuadrant.Inner3;
153
- }
154
- else {
155
- return RectQuadrant.Outer3;
156
- }
157
- }
158
- }
162
+ const isInner = isPointInRect(point, rect)
163
+ if (point.x <= rect.x + rect.width / 2) {
164
+ if (point.y <= rect.y + rect.height / 2) {
165
+ if (isInner) {
166
+ return RectQuadrant.Inner1
167
+ } else {
168
+ return RectQuadrant.Outer1
169
+ }
170
+ } else {
171
+ if (isInner) {
172
+ return RectQuadrant.Inner4
173
+ } else {
174
+ return RectQuadrant.Outer4
175
+ }
176
+ }
177
+ } else {
178
+ if (point.y <= rect.y + rect.height / 2) {
179
+ if (isInner) {
180
+ return RectQuadrant.Inner2
181
+ } else {
182
+ return RectQuadrant.Outer2
183
+ }
184
+ } else {
185
+ if (isInner) {
186
+ return RectQuadrant.Inner3
187
+ } else {
188
+ return RectQuadrant.Outer3
189
+ }
190
+ }
191
+ }
159
192
  }
160
- exports.calcQuadrantOfPointToRect = calcQuadrantOfPointToRect;
193
+ exports.calcQuadrantOfPointToRect = calcQuadrantOfPointToRect
161
194
  function calcDistanceOfPointToRect(point, rect) {
162
- let minX = Math.min(Math.abs(point.x - rect.x), Math.abs(point.x - (rect.x + rect.width)));
163
- let minY = Math.min(Math.abs(point.y - rect.y), Math.abs(point.y - (rect.y + rect.height)));
164
- if (point.x >= rect.x && point.x <= rect.x + rect.width) {
165
- minX = 0;
166
- }
167
- if (point.y >= rect.y && point.y <= rect.y + rect.height) {
168
- minY = 0;
169
- }
170
- return Math.sqrt(minX ** 2 + minY ** 2);
195
+ let minX = Math.min(
196
+ Math.abs(point.x - rect.x),
197
+ Math.abs(point.x - (rect.x + rect.width))
198
+ )
199
+ let minY = Math.min(
200
+ Math.abs(point.y - rect.y),
201
+ Math.abs(point.y - (rect.y + rect.height))
202
+ )
203
+ if (point.x >= rect.x && point.x <= rect.x + rect.width) {
204
+ minX = 0
205
+ }
206
+ if (point.y >= rect.y && point.y <= rect.y + rect.height) {
207
+ minY = 0
208
+ }
209
+ return Math.sqrt(minX ** 2 + minY ** 2)
171
210
  }
172
- exports.calcDistanceOfPointToRect = calcDistanceOfPointToRect;
211
+ exports.calcDistanceOfPointToRect = calcDistanceOfPointToRect
173
212
  function calcDistancePointToEdge(point, rect) {
174
- const distanceTop = Math.abs(point.y - rect.y);
175
- const distanceBottom = Math.abs(point.y - (rect.y + rect.height));
176
- const distanceLeft = Math.abs(point.x - rect.x);
177
- const distanceRight = Math.abs(point.x - (rect.x + rect.width));
178
- return Math.min(distanceTop, distanceBottom, distanceLeft, distanceRight);
213
+ const distanceTop = Math.abs(point.y - rect.y)
214
+ const distanceBottom = Math.abs(point.y - (rect.y + rect.height))
215
+ const distanceLeft = Math.abs(point.x - rect.x)
216
+ const distanceRight = Math.abs(point.x - (rect.x + rect.width))
217
+ return Math.min(distanceTop, distanceBottom, distanceLeft, distanceRight)
179
218
  }
180
- exports.calcDistancePointToEdge = calcDistancePointToEdge;
219
+ exports.calcDistancePointToEdge = calcDistancePointToEdge
181
220
  function isNearAfter(point, rect, inline = false) {
182
- if (inline) {
183
- return (Math.abs(point.x - rect.x) + Math.abs(point.y - rect.y) >
184
- Math.abs(point.x - (rect.x + rect.width)) +
185
- Math.abs(point.y - (rect.y + rect.height)));
186
- }
187
- return Math.abs(point.y - rect.y) > Math.abs(point.y - (rect.y + rect.height));
221
+ if (inline) {
222
+ return (
223
+ Math.abs(point.x - rect.x) + Math.abs(point.y - rect.y) >
224
+ Math.abs(point.x - (rect.x + rect.width)) +
225
+ Math.abs(point.y - (rect.y + rect.height))
226
+ )
227
+ }
228
+ return Math.abs(point.y - rect.y) > Math.abs(point.y - (rect.y + rect.height))
188
229
  }
189
- exports.isNearAfter = isNearAfter;
230
+ exports.isNearAfter = isNearAfter
190
231
  /**
191
232
  * 计算点鱼矩形的相对位置信息
192
233
  * @param point
193
234
  * @param rect
194
235
  */
195
236
  function calcRelativeOfPointToRect(point, rect) {
196
- const distance = calcDistanceOfPointToRect(point, rect);
197
- const quadrant = calcQuadrantOfPointToRect(point, rect);
198
- return {
199
- quadrant,
200
- distance,
201
- };
237
+ const distance = calcDistanceOfPointToRect(point, rect)
238
+ const quadrant = calcQuadrantOfPointToRect(point, rect)
239
+ return {
240
+ quadrant,
241
+ distance,
242
+ }
202
243
  }
203
- exports.calcRelativeOfPointToRect = calcRelativeOfPointToRect;
244
+ exports.calcRelativeOfPointToRect = calcRelativeOfPointToRect
204
245
  function calcBoundingRect(rects) {
205
- if (!rects?.length)
206
- return;
207
- if (rects?.length === 1 && !rects[0])
208
- return;
209
- let minTop = Infinity;
210
- let maxBottom = -Infinity;
211
- let minLeft = Infinity;
212
- let maxRight = -Infinity;
213
- rects.forEach((item) => {
214
- const rect = new Rect(item.x, item.y, item.width, item.height);
215
- if (rect.top <= minTop) {
216
- minTop = rect.top;
217
- }
218
- if (rect.bottom >= maxBottom) {
219
- maxBottom = rect.bottom;
220
- }
221
- if (rect.left <= minLeft) {
222
- minLeft = rect.left;
223
- }
224
- if (rect.right >= maxRight) {
225
- maxRight = rect.right;
226
- }
227
- });
228
- return new Rect(minLeft, minTop, maxRight - minLeft, maxBottom - minTop);
246
+ if (!rects?.length) return
247
+ if (rects?.length === 1 && !rects[0]) return
248
+ let minTop = Infinity
249
+ let maxBottom = -Infinity
250
+ let minLeft = Infinity
251
+ let maxRight = -Infinity
252
+ rects.forEach((item) => {
253
+ const rect = new Rect(item.x, item.y, item.width, item.height)
254
+ if (rect.top <= minTop) {
255
+ minTop = rect.top
256
+ }
257
+ if (rect.bottom >= maxBottom) {
258
+ maxBottom = rect.bottom
259
+ }
260
+ if (rect.left <= minLeft) {
261
+ minLeft = rect.left
262
+ }
263
+ if (rect.right >= maxRight) {
264
+ maxRight = rect.right
265
+ }
266
+ })
267
+ return new Rect(minLeft, minTop, maxRight - minLeft, maxBottom - minTop)
229
268
  }
230
- exports.calcBoundingRect = calcBoundingRect;
231
- function calcRectByStartEndPoint(startPoint, endPoint, scrollX = 0, scrollY = 0) {
232
- let drawStartX = 0, drawStartY = 0;
233
- if (endPoint.x + scrollX >= startPoint.x &&
234
- endPoint.y + scrollY >= startPoint.y) {
235
- //4象限
236
- drawStartX = startPoint.x;
237
- drawStartY = startPoint.y;
238
- return new Rect(drawStartX - scrollX, drawStartY - scrollY, Math.abs(endPoint.x - startPoint.x + scrollX), Math.abs(endPoint.y - startPoint.y + scrollY));
239
- }
240
- else if (endPoint.x + scrollX < startPoint.x &&
241
- endPoint.y + scrollY < startPoint.y) {
242
- //1象限
243
- drawStartX = endPoint.x;
244
- drawStartY = endPoint.y;
245
- return new Rect(drawStartX, drawStartY, Math.abs(endPoint.x - startPoint.x + scrollX), Math.abs(endPoint.y - startPoint.y + scrollY));
246
- }
247
- else if (endPoint.x + scrollX < startPoint.x &&
248
- endPoint.y + scrollY >= startPoint.y) {
249
- //3象限
250
- drawStartX = endPoint.x;
251
- drawStartY = startPoint.y;
252
- return new Rect(drawStartX - scrollX, drawStartY - scrollY, Math.abs(endPoint.x - startPoint.x + scrollX), Math.abs(endPoint.y - startPoint.y + scrollY));
253
- }
254
- else {
255
- //2象限
256
- drawStartX = startPoint.x;
257
- drawStartY = endPoint.y;
258
- return new Rect(drawStartX, drawStartY, Math.abs(endPoint.x - startPoint.x + scrollX), Math.abs(endPoint.y - startPoint.y + scrollY));
259
- }
269
+ exports.calcBoundingRect = calcBoundingRect
270
+ function calcRectByStartEndPoint(
271
+ startPoint,
272
+ endPoint,
273
+ scrollX = 0,
274
+ scrollY = 0
275
+ ) {
276
+ let drawStartX = 0,
277
+ drawStartY = 0
278
+ if (
279
+ endPoint.x + scrollX >= startPoint.x &&
280
+ endPoint.y + scrollY >= startPoint.y
281
+ ) {
282
+ //4象限
283
+ drawStartX = startPoint.x
284
+ drawStartY = startPoint.y
285
+ return new Rect(
286
+ drawStartX - scrollX,
287
+ drawStartY - scrollY,
288
+ Math.abs(endPoint.x - startPoint.x + scrollX),
289
+ Math.abs(endPoint.y - startPoint.y + scrollY)
290
+ )
291
+ } else if (
292
+ endPoint.x + scrollX < startPoint.x &&
293
+ endPoint.y + scrollY < startPoint.y
294
+ ) {
295
+ //1象限
296
+ drawStartX = endPoint.x
297
+ drawStartY = endPoint.y
298
+ return new Rect(
299
+ drawStartX,
300
+ drawStartY,
301
+ Math.abs(endPoint.x - startPoint.x + scrollX),
302
+ Math.abs(endPoint.y - startPoint.y + scrollY)
303
+ )
304
+ } else if (
305
+ endPoint.x + scrollX < startPoint.x &&
306
+ endPoint.y + scrollY >= startPoint.y
307
+ ) {
308
+ //3象限
309
+ drawStartX = endPoint.x
310
+ drawStartY = startPoint.y
311
+ return new Rect(
312
+ drawStartX - scrollX,
313
+ drawStartY - scrollY,
314
+ Math.abs(endPoint.x - startPoint.x + scrollX),
315
+ Math.abs(endPoint.y - startPoint.y + scrollY)
316
+ )
317
+ } else {
318
+ //2象限
319
+ drawStartX = startPoint.x
320
+ drawStartY = endPoint.y
321
+ return new Rect(
322
+ drawStartX,
323
+ drawStartY,
324
+ Math.abs(endPoint.x - startPoint.x + scrollX),
325
+ Math.abs(endPoint.y - startPoint.y + scrollY)
326
+ )
327
+ }
260
328
  }
261
- exports.calcRectByStartEndPoint = calcRectByStartEndPoint;
329
+ exports.calcRectByStartEndPoint = calcRectByStartEndPoint
262
330
  function calcEdgeLinesOfRect(rect) {
263
- return {
264
- v: [
265
- new LineSegment(new Point(rect.x, rect.y), new Point(rect.x, rect.y + rect.height)),
266
- new LineSegment(new Point(rect.x + rect.width / 2, rect.y), new Point(rect.x + rect.width / 2, rect.y + rect.height)),
267
- new LineSegment(new Point(rect.x + rect.width, rect.y), new Point(rect.x + rect.width, rect.y + rect.height)),
268
- ],
269
- h: [
270
- new LineSegment(new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y)),
271
- new LineSegment(new Point(rect.x, rect.y + rect.height / 2), new Point(rect.x + rect.width, rect.y + rect.height / 2)),
272
- new LineSegment(new Point(rect.x, rect.y + rect.height), new Point(rect.x + rect.width, rect.y + rect.height)),
273
- ],
274
- };
331
+ return {
332
+ v: [
333
+ new LineSegment(
334
+ new Point(rect.x, rect.y),
335
+ new Point(rect.x, rect.y + rect.height)
336
+ ),
337
+ new LineSegment(
338
+ new Point(rect.x + rect.width / 2, rect.y),
339
+ new Point(rect.x + rect.width / 2, rect.y + rect.height)
340
+ ),
341
+ new LineSegment(
342
+ new Point(rect.x + rect.width, rect.y),
343
+ new Point(rect.x + rect.width, rect.y + rect.height)
344
+ ),
345
+ ],
346
+ h: [
347
+ new LineSegment(
348
+ new Point(rect.x, rect.y),
349
+ new Point(rect.x + rect.width, rect.y)
350
+ ),
351
+ new LineSegment(
352
+ new Point(rect.x, rect.y + rect.height / 2),
353
+ new Point(rect.x + rect.width, rect.y + rect.height / 2)
354
+ ),
355
+ new LineSegment(
356
+ new Point(rect.x, rect.y + rect.height),
357
+ new Point(rect.x + rect.width, rect.y + rect.height)
358
+ ),
359
+ ],
360
+ }
275
361
  }
276
- exports.calcEdgeLinesOfRect = calcEdgeLinesOfRect;
362
+ exports.calcEdgeLinesOfRect = calcEdgeLinesOfRect
277
363
  function calcRectOfAxisLineSegment(line) {
278
- if (!isLineSegment(line))
279
- return;
280
- const isXAxis = line.start.x === line.end.x;
281
- return new Rect(line.start.x, line.start.y, isXAxis ? 0 : line.end.x - line.start.x, isXAxis ? line.end.y - line.start.y : 0);
364
+ if (!isLineSegment(line)) return
365
+ const isXAxis = line.start.x === line.end.x
366
+ return new Rect(
367
+ line.start.x,
368
+ line.start.y,
369
+ isXAxis ? 0 : line.end.x - line.start.x,
370
+ isXAxis ? line.end.y - line.start.y : 0
371
+ )
282
372
  }
283
- exports.calcRectOfAxisLineSegment = calcRectOfAxisLineSegment;
373
+ exports.calcRectOfAxisLineSegment = calcRectOfAxisLineSegment
284
374
  function calcSpaceBlockOfRect(target, source, type) {
285
- const targetRect = new Rect(target.x, target.y, target.width, target.height);
286
- const sourceRect = new Rect(source.x, source.y, source.width, source.height);
287
- if (sourceRect.bottom < targetRect.top && sourceRect.left > targetRect.right)
288
- return;
289
- if (sourceRect.top > targetRect.bottom && sourceRect.left > targetRect.right)
290
- return;
291
- if (sourceRect.bottom < targetRect.top && sourceRect.right < targetRect.left)
292
- return;
293
- if (sourceRect.top > targetRect.bottom && sourceRect.right < targetRect.left)
294
- return;
295
- if (sourceRect.bottom < targetRect.top) {
296
- const distance = targetRect.top - sourceRect.bottom;
297
- const left = Math.min(sourceRect.left, targetRect.left);
298
- const right = Math.max(sourceRect.right, targetRect.right);
299
- if (type && type !== 'top')
300
- return;
301
- return {
302
- type: 'top',
303
- distance,
304
- rect: new Rect(left, sourceRect.bottom, right - left, distance),
305
- };
306
- }
307
- else if (sourceRect.top > targetRect.bottom) {
308
- const distance = sourceRect.top - targetRect.bottom;
309
- const left = Math.min(sourceRect.left, targetRect.left);
310
- const right = Math.max(sourceRect.right, targetRect.right);
311
- if (type && type !== 'bottom')
312
- return;
313
- return {
314
- type: 'bottom',
315
- distance,
316
- rect: new Rect(left, targetRect.bottom, right - left, distance),
317
- };
318
- }
319
- else if (sourceRect.right < targetRect.left) {
320
- const distance = targetRect.left - sourceRect.right;
321
- const top = Math.min(sourceRect.top, targetRect.top);
322
- const bottom = Math.max(sourceRect.bottom, targetRect.bottom);
323
- if (type && type !== 'left')
324
- return;
325
- return {
326
- type: 'left',
327
- distance,
328
- rect: new Rect(sourceRect.right, top, distance, bottom - top),
329
- };
330
- }
331
- else if (sourceRect.left > targetRect.right) {
332
- const distance = sourceRect.left - targetRect.right;
333
- const top = Math.min(sourceRect.top, targetRect.top);
334
- const bottom = Math.max(sourceRect.bottom, targetRect.bottom);
335
- if (type && type !== 'right')
336
- return;
337
- return {
338
- type: 'right',
339
- distance,
340
- rect: new Rect(targetRect.right, top, distance, bottom - top),
341
- };
375
+ const targetRect = new Rect(target.x, target.y, target.width, target.height)
376
+ const sourceRect = new Rect(source.x, source.y, source.width, source.height)
377
+ if (sourceRect.bottom < targetRect.top && sourceRect.left > targetRect.right)
378
+ return
379
+ if (sourceRect.top > targetRect.bottom && sourceRect.left > targetRect.right)
380
+ return
381
+ if (sourceRect.bottom < targetRect.top && sourceRect.right < targetRect.left)
382
+ return
383
+ if (sourceRect.top > targetRect.bottom && sourceRect.right < targetRect.left)
384
+ return
385
+ if (sourceRect.bottom < targetRect.top) {
386
+ const distance = targetRect.top - sourceRect.bottom
387
+ const left = Math.min(sourceRect.left, targetRect.left)
388
+ const right = Math.max(sourceRect.right, targetRect.right)
389
+ if (type && type !== 'top') return
390
+ return {
391
+ type: 'top',
392
+ distance,
393
+ rect: new Rect(left, sourceRect.bottom, right - left, distance),
394
+ }
395
+ } else if (sourceRect.top > targetRect.bottom) {
396
+ const distance = sourceRect.top - targetRect.bottom
397
+ const left = Math.min(sourceRect.left, targetRect.left)
398
+ const right = Math.max(sourceRect.right, targetRect.right)
399
+ if (type && type !== 'bottom') return
400
+ return {
401
+ type: 'bottom',
402
+ distance,
403
+ rect: new Rect(left, targetRect.bottom, right - left, distance),
404
+ }
405
+ } else if (sourceRect.right < targetRect.left) {
406
+ const distance = targetRect.left - sourceRect.right
407
+ const top = Math.min(sourceRect.top, targetRect.top)
408
+ const bottom = Math.max(sourceRect.bottom, targetRect.bottom)
409
+ if (type && type !== 'left') return
410
+ return {
411
+ type: 'left',
412
+ distance,
413
+ rect: new Rect(sourceRect.right, top, distance, bottom - top),
414
+ }
415
+ } else if (sourceRect.left > targetRect.right) {
416
+ const distance = sourceRect.left - targetRect.right
417
+ const top = Math.min(sourceRect.top, targetRect.top)
418
+ const bottom = Math.max(sourceRect.bottom, targetRect.bottom)
419
+ if (type && type !== 'right') return
420
+ return {
421
+ type: 'right',
422
+ distance,
423
+ rect: new Rect(targetRect.right, top, distance, bottom - top),
342
424
  }
425
+ }
343
426
  }
344
- exports.calcSpaceBlockOfRect = calcSpaceBlockOfRect;
427
+ exports.calcSpaceBlockOfRect = calcSpaceBlockOfRect
345
428
  function calcExtendsLineSegmentOfRect(targetRect, referRect) {
346
- if (referRect.right < targetRect.right &&
347
- targetRect.left <= referRect.right) {
348
- //右侧
349
- if (referRect.bottom < targetRect.top) {
350
- //上方
351
- return {
352
- start: { x: referRect.right, y: referRect.bottom },
353
- end: { x: targetRect.right, y: referRect.bottom },
354
- };
355
- }
356
- else if (referRect.top > targetRect.bottom) {
357
- //下方
358
- return {
359
- start: { x: referRect.right, y: referRect.top },
360
- end: { x: targetRect.right, y: referRect.top },
361
- };
362
- }
363
- }
364
- else if (referRect.left > targetRect.left &&
365
- targetRect.right >= referRect.left) {
366
- //左侧
367
- if (referRect.bottom < targetRect.top) {
368
- //上方
369
- return {
370
- start: { x: targetRect.left, y: referRect.bottom },
371
- end: { x: referRect.left, y: referRect.bottom },
372
- };
373
- }
374
- else if (referRect.top > targetRect.bottom) {
375
- //下方
376
- return {
377
- start: { x: targetRect.left, y: referRect.top },
378
- end: { x: referRect.left, y: referRect.top },
379
- };
380
- }
381
- }
382
- if (referRect.top < targetRect.top && targetRect.bottom >= referRect.top) {
383
- //refer在上方
384
- if (referRect.right < targetRect.left) {
385
- //右侧
386
- return {
387
- start: { x: referRect.right, y: referRect.bottom },
388
- end: { x: referRect.right, y: targetRect.bottom },
389
- };
390
- }
391
- else if (referRect.left > targetRect.right) {
392
- //左侧
393
- return {
394
- start: { x: referRect.left, y: referRect.bottom },
395
- end: { x: referRect.left, y: targetRect.bottom },
396
- };
397
- }
398
- }
399
- else if (referRect.bottom > targetRect.bottom &&
400
- referRect.top <= targetRect.bottom) {
401
- //refer下方
402
- if (referRect.right < targetRect.left) {
403
- //右侧
404
- return {
405
- start: { x: referRect.right, y: targetRect.top },
406
- end: { x: referRect.right, y: referRect.top },
407
- };
408
- }
409
- else if (referRect.left > targetRect.right) {
410
- //左侧
411
- return {
412
- start: { x: referRect.left, y: targetRect.top },
413
- end: { x: referRect.left, y: referRect.top },
414
- };
415
- }
416
- }
429
+ if (
430
+ referRect.right < targetRect.right &&
431
+ targetRect.left <= referRect.right
432
+ ) {
433
+ //右侧
434
+ if (referRect.bottom < targetRect.top) {
435
+ //上方
436
+ return {
437
+ start: { x: referRect.right, y: referRect.bottom },
438
+ end: { x: targetRect.right, y: referRect.bottom },
439
+ }
440
+ } else if (referRect.top > targetRect.bottom) {
441
+ //下方
442
+ return {
443
+ start: { x: referRect.right, y: referRect.top },
444
+ end: { x: targetRect.right, y: referRect.top },
445
+ }
446
+ }
447
+ } else if (
448
+ referRect.left > targetRect.left &&
449
+ targetRect.right >= referRect.left
450
+ ) {
451
+ //左侧
452
+ if (referRect.bottom < targetRect.top) {
453
+ //上方
454
+ return {
455
+ start: { x: targetRect.left, y: referRect.bottom },
456
+ end: { x: referRect.left, y: referRect.bottom },
457
+ }
458
+ } else if (referRect.top > targetRect.bottom) {
459
+ //下方
460
+ return {
461
+ start: { x: targetRect.left, y: referRect.top },
462
+ end: { x: referRect.left, y: referRect.top },
463
+ }
464
+ }
465
+ }
466
+ if (referRect.top < targetRect.top && targetRect.bottom >= referRect.top) {
467
+ //refer在上方
468
+ if (referRect.right < targetRect.left) {
469
+ //右侧
470
+ return {
471
+ start: { x: referRect.right, y: referRect.bottom },
472
+ end: { x: referRect.right, y: targetRect.bottom },
473
+ }
474
+ } else if (referRect.left > targetRect.right) {
475
+ //左侧
476
+ return {
477
+ start: { x: referRect.left, y: referRect.bottom },
478
+ end: { x: referRect.left, y: targetRect.bottom },
479
+ }
480
+ }
481
+ } else if (
482
+ referRect.bottom > targetRect.bottom &&
483
+ referRect.top <= targetRect.bottom
484
+ ) {
485
+ //refer下方
486
+ if (referRect.right < targetRect.left) {
487
+ //右侧
488
+ return {
489
+ start: { x: referRect.right, y: targetRect.top },
490
+ end: { x: referRect.right, y: referRect.top },
491
+ }
492
+ } else if (referRect.left > targetRect.right) {
493
+ //左侧
494
+ return {
495
+ start: { x: referRect.left, y: targetRect.top },
496
+ end: { x: referRect.left, y: referRect.top },
497
+ }
498
+ }
499
+ }
417
500
  }
418
- exports.calcExtendsLineSegmentOfRect = calcExtendsLineSegmentOfRect;
501
+ exports.calcExtendsLineSegmentOfRect = calcExtendsLineSegmentOfRect
419
502
  function calcOffsetOfSnapLineSegmentToEdge(line, current) {
420
- const edges = calcEdgeLinesOfRect(current);
421
- const isVerticalLine = line.start.x === line.end.x;
422
- if (isVerticalLine) {
423
- return { x: calcMinDistanceValue(edges.x, line.start.x) - current.x, y: 0 };
424
- }
425
- function calcEdgeLinesOfRect(rect) {
426
- return {
427
- x: [rect.x, rect.x + rect.width / 2, rect.x + rect.width],
428
- y: [rect.y, rect.y + rect.height / 2, rect.y + rect.height],
429
- };
430
- }
431
- function calcMinDistanceValue(edges, targetValue) {
432
- let minDistance = Infinity, minDistanceIndex = -1;
433
- for (let i = 0; i < edges.length; i++) {
434
- const distance = Math.abs(edges[i] - targetValue);
435
- if (minDistance > distance) {
436
- minDistance = distance;
437
- minDistanceIndex = i;
438
- }
439
- }
440
- return edges[minDistanceIndex];
441
- }
442
- return { x: 0, y: calcMinDistanceValue(edges.y, line.start.y) - current.y };
503
+ const edges = calcEdgeLinesOfRect(current)
504
+ const isVerticalLine = line.start.x === line.end.x
505
+ if (isVerticalLine) {
506
+ return { x: calcMinDistanceValue(edges.x, line.start.x) - current.x, y: 0 }
507
+ }
508
+ function calcEdgeLinesOfRect(rect) {
509
+ return {
510
+ x: [rect.x, rect.x + rect.width / 2, rect.x + rect.width],
511
+ y: [rect.y, rect.y + rect.height / 2, rect.y + rect.height],
512
+ }
513
+ }
514
+ function calcMinDistanceValue(edges, targetValue) {
515
+ let minDistance = Infinity,
516
+ minDistanceIndex = -1
517
+ for (let i = 0; i < edges.length; i++) {
518
+ const distance = Math.abs(edges[i] - targetValue)
519
+ if (minDistance > distance) {
520
+ minDistance = distance
521
+ minDistanceIndex = i
522
+ }
523
+ }
524
+ return edges[minDistanceIndex]
525
+ }
526
+ return { x: 0, y: calcMinDistanceValue(edges.y, line.start.y) - current.y }
443
527
  }
444
- exports.calcOffsetOfSnapLineSegmentToEdge = calcOffsetOfSnapLineSegmentToEdge;
528
+ exports.calcOffsetOfSnapLineSegmentToEdge = calcOffsetOfSnapLineSegmentToEdge
445
529
  function calcDistanceOfSnapLineToEdges(line, edges) {
446
- let distance = Infinity;
447
- if (line?.start?.y === line?.end?.y) {
448
- edges.h.forEach((target) => {
449
- const _distance = Math.abs(target.start.y - line.start.y);
450
- if (_distance < distance) {
451
- distance = _distance;
452
- }
453
- });
454
- }
455
- else if (line?.start?.x === line?.end?.x) {
456
- edges.v.forEach((target) => {
457
- const _distance = Math.abs(target.start.x - line.start.x);
458
- if (_distance < distance) {
459
- distance = _distance;
460
- }
461
- });
462
- }
463
- else {
464
- throw new Error('can not calculate slash distance');
465
- }
466
- return distance;
530
+ let distance = Infinity
531
+ if (line?.start?.y === line?.end?.y) {
532
+ edges.h.forEach((target) => {
533
+ const _distance = Math.abs(target.start.y - line.start.y)
534
+ if (_distance < distance) {
535
+ distance = _distance
536
+ }
537
+ })
538
+ } else if (line?.start?.x === line?.end?.x) {
539
+ edges.v.forEach((target) => {
540
+ const _distance = Math.abs(target.start.x - line.start.x)
541
+ if (_distance < distance) {
542
+ distance = _distance
543
+ }
544
+ })
545
+ } else {
546
+ throw new Error('can not calculate slash distance')
547
+ }
548
+ return distance
467
549
  }
468
- exports.calcDistanceOfSnapLineToEdges = calcDistanceOfSnapLineToEdges;
550
+ exports.calcDistanceOfSnapLineToEdges = calcDistanceOfSnapLineToEdges
469
551
  function calcCombineSnapLineSegment(target, source) {
470
- if (target.start.x === target.end.x) {
471
- return new LineSegment(new Point(target.start.x, target.start.y > source.start.y ? source.start.y : target.start.y), new Point(target.start.x, target.end.y > source.end.y ? target.end.y : source.end.y));
472
- }
473
- return new LineSegment(new Point(target.start.x > source.start.x ? source.start.x : target.start.x, target.start.y), new Point(target.end.x > source.end.x ? target.end.x : source.end.x, target.end.y));
552
+ if (target.start.x === target.end.x) {
553
+ return new LineSegment(
554
+ new Point(
555
+ target.start.x,
556
+ target.start.y > source.start.y ? source.start.y : target.start.y
557
+ ),
558
+ new Point(
559
+ target.start.x,
560
+ target.end.y > source.end.y ? target.end.y : source.end.y
561
+ )
562
+ )
563
+ }
564
+ return new LineSegment(
565
+ new Point(
566
+ target.start.x > source.start.x ? source.start.x : target.start.x,
567
+ target.start.y
568
+ ),
569
+ new Point(
570
+ target.end.x > source.end.x ? target.end.x : source.end.x,
571
+ target.end.y
572
+ )
573
+ )
474
574
  }
475
- exports.calcCombineSnapLineSegment = calcCombineSnapLineSegment;
575
+ exports.calcCombineSnapLineSegment = calcCombineSnapLineSegment
476
576
  function calcClosestEdges(line, edges) {
477
- let result;
478
- let distance = Infinity;
479
- if (line?.start?.y === line?.end?.y) {
480
- edges.h.forEach((target) => {
481
- const _distance = Math.abs(target.start.y - line.start.y);
482
- if (_distance < distance) {
483
- distance = _distance;
484
- result = target;
485
- }
486
- });
487
- }
488
- else if (line?.start?.x === line?.end?.x) {
489
- edges.v.forEach((target) => {
490
- const _distance = Math.abs(target.start.x - line.start.x);
491
- if (_distance < distance) {
492
- distance = _distance;
493
- result = target;
494
- }
495
- });
496
- }
497
- else {
498
- throw new Error('can not calculate slash distance');
499
- }
500
- return [distance, result];
577
+ let result
578
+ let distance = Infinity
579
+ if (line?.start?.y === line?.end?.y) {
580
+ edges.h.forEach((target) => {
581
+ const _distance = Math.abs(target.start.y - line.start.y)
582
+ if (_distance < distance) {
583
+ distance = _distance
584
+ result = target
585
+ }
586
+ })
587
+ } else if (line?.start?.x === line?.end?.x) {
588
+ edges.v.forEach((target) => {
589
+ const _distance = Math.abs(target.start.x - line.start.x)
590
+ if (_distance < distance) {
591
+ distance = _distance
592
+ result = target
593
+ }
594
+ })
595
+ } else {
596
+ throw new Error('can not calculate slash distance')
597
+ }
598
+ return [distance, result]
501
599
  }
502
- exports.calcClosestEdges = calcClosestEdges;
600
+ exports.calcClosestEdges = calcClosestEdges