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