@formily-design/shared 1.0.0
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.
- package/LICENSE.md +20 -0
- package/README.md +1 -0
- package/dist/designable.shared.umd.production.js +1 -0
- package/dist/designable.shared.umd.production.min.js +2051 -0
- package/esm/animation.d.ts +2 -0
- package/esm/animation.js +33 -0
- package/esm/array.d.ts +37 -0
- package/esm/array.js +111 -0
- package/esm/clone.d.ts +4 -0
- package/esm/clone.js +97 -0
- package/esm/compose.d.ts +1 -0
- package/esm/compose.js +7 -0
- package/esm/coordinate.d.ts +106 -0
- package/esm/coordinate.js +475 -0
- package/esm/element.d.ts +6 -0
- package/esm/element.js +137 -0
- package/esm/event.d.ts +96 -0
- package/esm/event.js +213 -0
- package/esm/globalThisPolyfill.d.ts +1 -0
- package/esm/globalThisPolyfill.js +22 -0
- package/esm/index.d.ts +17 -0
- package/esm/index.js +17 -0
- package/esm/instanceof.d.ts +1 -0
- package/esm/instanceof.js +11 -0
- package/esm/keycode.d.ts +147 -0
- package/esm/keycode.js +150 -0
- package/esm/lru.d.ts +73 -0
- package/esm/lru.js +293 -0
- package/esm/observer.d.ts +9 -0
- package/esm/observer.js +29 -0
- package/esm/request-idle.d.ts +10 -0
- package/esm/request-idle.js +8 -0
- package/esm/scroller.d.ts +10 -0
- package/esm/scroller.js +82 -0
- package/esm/subscribable.d.ts +14 -0
- package/esm/subscribable.js +48 -0
- package/esm/types.d.ts +13 -0
- package/esm/types.js +21 -0
- package/esm/uid.d.ts +1 -0
- package/esm/uid.js +9 -0
- package/lib/animation.d.ts +2 -0
- package/lib/animation.js +38 -0
- package/lib/array.d.ts +37 -0
- package/lib/array.js +125 -0
- package/lib/clone.d.ts +4 -0
- package/lib/clone.js +102 -0
- package/lib/compose.d.ts +1 -0
- package/lib/compose.js +11 -0
- package/lib/coordinate.d.ts +106 -0
- package/lib/coordinate.js +504 -0
- package/lib/element.d.ts +6 -0
- package/lib/element.js +145 -0
- package/lib/event.d.ts +96 -0
- package/lib/event.js +218 -0
- package/lib/globalThisPolyfill.d.ts +1 -0
- package/lib/globalThisPolyfill.js +25 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +33 -0
- package/lib/instanceof.d.ts +1 -0
- package/lib/instanceof.js +15 -0
- package/lib/keycode.d.ts +147 -0
- package/lib/keycode.js +154 -0
- package/lib/lru.d.ts +73 -0
- package/lib/lru.js +297 -0
- package/lib/observer.d.ts +9 -0
- package/lib/observer.js +33 -0
- package/lib/request-idle.d.ts +10 -0
- package/lib/request-idle.js +13 -0
- package/lib/scroller.d.ts +10 -0
- package/lib/scroller.js +88 -0
- package/lib/subscribable.d.ts +14 -0
- package/lib/subscribable.js +52 -0
- package/lib/types.d.ts +13 -0
- package/lib/types.js +29 -0
- package/lib/uid.d.ts +1 -0
- package/lib/uid.js +12 -0
- package/package.json +32 -0
- package/rollup.config.js +3 -0
- package/src/animation.ts +38 -0
- package/src/array.ts +301 -0
- package/src/clone.ts +96 -0
- package/src/compose.ts +7 -0
- package/src/coordinate.ts +633 -0
- package/src/element.ts +144 -0
- package/src/event.ts +379 -0
- package/src/globalThisPolyfill.ts +19 -0
- package/src/index.ts +17 -0
- package/src/instanceof.ts +10 -0
- package/src/keycode.ts +150 -0
- package/src/lru.ts +322 -0
- package/src/observer.ts +38 -0
- package/src/request-idle.ts +22 -0
- package/src/scroller.ts +113 -0
- package/src/subscribable.ts +60 -0
- package/src/types.ts +27 -0
- package/src/uid.ts +10 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +5 -0
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { isValidNumber } from './types';
|
|
2
|
+
export function isRect(rect) {
|
|
3
|
+
return (rect === null || rect === void 0 ? void 0 : rect.x) && (rect === null || rect === void 0 ? void 0 : rect.y) && (rect === null || rect === void 0 ? void 0 : rect.width) && (rect === null || rect === void 0 ? void 0 : rect.height);
|
|
4
|
+
}
|
|
5
|
+
export function isPoint(val) {
|
|
6
|
+
return isValidNumber(val === null || val === void 0 ? void 0 : val.x) && isValidNumber(val === null || val === void 0 ? void 0 : val.y);
|
|
7
|
+
}
|
|
8
|
+
export function isLineSegment(val) {
|
|
9
|
+
return isPoint(val === null || val === void 0 ? void 0 : val.start) && isPoint(val === null || val === void 0 ? void 0 : val.end);
|
|
10
|
+
}
|
|
11
|
+
export class Point {
|
|
12
|
+
constructor(x, y) {
|
|
13
|
+
this.x = x;
|
|
14
|
+
this.y = y;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
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
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class LineSegment {
|
|
42
|
+
constructor(start, end) {
|
|
43
|
+
this.start = { ...start };
|
|
44
|
+
this.end = { ...end };
|
|
45
|
+
}
|
|
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 = {}));
|
|
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));
|
|
73
|
+
}
|
|
74
|
+
export function isEqualRect(target, source) {
|
|
75
|
+
return ((target === null || target === void 0 ? void 0 : target.x) === (source === null || source === void 0 ? void 0 : source.x) &&
|
|
76
|
+
target.y === source.y &&
|
|
77
|
+
target.width === source.width &&
|
|
78
|
+
target.height === source.height);
|
|
79
|
+
}
|
|
80
|
+
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];
|
|
86
|
+
}
|
|
87
|
+
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));
|
|
93
|
+
}
|
|
94
|
+
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);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 计算点在矩形的哪个象限
|
|
104
|
+
* @param point
|
|
105
|
+
* @param rect
|
|
106
|
+
*/
|
|
107
|
+
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
|
+
}
|
|
145
|
+
}
|
|
146
|
+
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);
|
|
156
|
+
}
|
|
157
|
+
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);
|
|
163
|
+
}
|
|
164
|
+
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));
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 计算点鱼矩形的相对位置信息
|
|
174
|
+
* @param point
|
|
175
|
+
* @param rect
|
|
176
|
+
*/
|
|
177
|
+
export function calcRelativeOfPointToRect(point, rect) {
|
|
178
|
+
const distance = calcDistanceOfPointToRect(point, rect);
|
|
179
|
+
const quadrant = calcQuadrantOfPointToRect(point, rect);
|
|
180
|
+
return {
|
|
181
|
+
quadrant,
|
|
182
|
+
distance,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
export function calcBoundingRect(rects) {
|
|
186
|
+
if (!(rects === null || rects === void 0 ? void 0 : rects.length))
|
|
187
|
+
return;
|
|
188
|
+
if ((rects === null || rects === void 0 ? void 0 : 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);
|
|
210
|
+
}
|
|
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
|
+
}
|
|
240
|
+
}
|
|
241
|
+
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
|
+
};
|
|
254
|
+
}
|
|
255
|
+
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);
|
|
260
|
+
}
|
|
261
|
+
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
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
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
|
+
}
|
|
393
|
+
}
|
|
394
|
+
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 };
|
|
418
|
+
}
|
|
419
|
+
export function calcDistanceOfSnapLineToEdges(line, edges) {
|
|
420
|
+
var _a, _b, _c, _d;
|
|
421
|
+
let distance = Infinity;
|
|
422
|
+
if (((_a = line === null || line === void 0 ? void 0 : line.start) === null || _a === void 0 ? void 0 : _a.y) === ((_b = line === null || line === void 0 ? void 0 : line.end) === null || _b === void 0 ? void 0 : _b.y)) {
|
|
423
|
+
edges.h.forEach((target) => {
|
|
424
|
+
const _distance = Math.abs(target.start.y - line.start.y);
|
|
425
|
+
if (_distance < distance) {
|
|
426
|
+
distance = _distance;
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
else if (((_c = line === null || line === void 0 ? void 0 : line.start) === null || _c === void 0 ? void 0 : _c.x) === ((_d = line === null || line === void 0 ? void 0 : line.end) === null || _d === void 0 ? void 0 : _d.x)) {
|
|
431
|
+
edges.v.forEach((target) => {
|
|
432
|
+
const _distance = Math.abs(target.start.x - line.start.x);
|
|
433
|
+
if (_distance < distance) {
|
|
434
|
+
distance = _distance;
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
throw new Error('can not calculate slash distance');
|
|
440
|
+
}
|
|
441
|
+
return distance;
|
|
442
|
+
}
|
|
443
|
+
export function calcCombineSnapLineSegment(target, source) {
|
|
444
|
+
if (target.start.x === target.end.x) {
|
|
445
|
+
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));
|
|
446
|
+
}
|
|
447
|
+
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));
|
|
448
|
+
}
|
|
449
|
+
export function calcClosestEdges(line, edges) {
|
|
450
|
+
var _a, _b, _c, _d;
|
|
451
|
+
let result;
|
|
452
|
+
let distance = Infinity;
|
|
453
|
+
if (((_a = line === null || line === void 0 ? void 0 : line.start) === null || _a === void 0 ? void 0 : _a.y) === ((_b = line === null || line === void 0 ? void 0 : line.end) === null || _b === void 0 ? void 0 : _b.y)) {
|
|
454
|
+
edges.h.forEach((target) => {
|
|
455
|
+
const _distance = Math.abs(target.start.y - line.start.y);
|
|
456
|
+
if (_distance < distance) {
|
|
457
|
+
distance = _distance;
|
|
458
|
+
result = target;
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
else if (((_c = line === null || line === void 0 ? void 0 : line.start) === null || _c === void 0 ? void 0 : _c.x) === ((_d = line === null || line === void 0 ? void 0 : line.end) === null || _d === void 0 ? void 0 : _d.x)) {
|
|
463
|
+
edges.v.forEach((target) => {
|
|
464
|
+
const _distance = Math.abs(target.start.x - line.start.x);
|
|
465
|
+
if (_distance < distance) {
|
|
466
|
+
distance = _distance;
|
|
467
|
+
result = target;
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
throw new Error('can not calculate slash distance');
|
|
473
|
+
}
|
|
474
|
+
return [distance, result];
|
|
475
|
+
}
|
package/esm/element.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Point } from './coordinate';
|
|
2
|
+
export declare const calcElementOuterWidth: (innerWidth: number, style: CSSStyleDeclaration) => number;
|
|
3
|
+
export declare const calcElementLayout: (element: Element) => "vertical" | "horizontal";
|
|
4
|
+
export declare const calcElementTranslate: (element: HTMLElement) => Point;
|
|
5
|
+
export declare const calcElementRotate: (element: HTMLElement) => number;
|
|
6
|
+
export declare const calcElementScale: (element: HTMLElement) => number;
|
package/esm/element.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Point } from './coordinate';
|
|
2
|
+
const InlineLayoutTagNames = new Set([
|
|
3
|
+
'A',
|
|
4
|
+
'ABBR',
|
|
5
|
+
'ACRONYM',
|
|
6
|
+
'AUDIO',
|
|
7
|
+
'B',
|
|
8
|
+
'BDI',
|
|
9
|
+
'BDO',
|
|
10
|
+
'BIG',
|
|
11
|
+
'BR',
|
|
12
|
+
'BUTTON',
|
|
13
|
+
'CANVAS',
|
|
14
|
+
'CITE',
|
|
15
|
+
'CODE',
|
|
16
|
+
'DATA',
|
|
17
|
+
'DATALIST',
|
|
18
|
+
'DEL',
|
|
19
|
+
'DFN',
|
|
20
|
+
'EM',
|
|
21
|
+
'EMBED',
|
|
22
|
+
'I',
|
|
23
|
+
'IFRAME',
|
|
24
|
+
'IMG',
|
|
25
|
+
'INS',
|
|
26
|
+
'KBD',
|
|
27
|
+
'LABEL',
|
|
28
|
+
'MAP',
|
|
29
|
+
'MARK',
|
|
30
|
+
'METER',
|
|
31
|
+
'NOSCRIPT',
|
|
32
|
+
'OBJECT',
|
|
33
|
+
'OUTPUT',
|
|
34
|
+
'PICTURE',
|
|
35
|
+
'PROGRESS',
|
|
36
|
+
'Q',
|
|
37
|
+
'RUBY',
|
|
38
|
+
'S',
|
|
39
|
+
'SAMP',
|
|
40
|
+
'SELECT',
|
|
41
|
+
'SLOT',
|
|
42
|
+
'SMALL',
|
|
43
|
+
'STRONG',
|
|
44
|
+
'SUB',
|
|
45
|
+
'SUP',
|
|
46
|
+
'SVG',
|
|
47
|
+
'TEMPLATE',
|
|
48
|
+
'TEXTAREA',
|
|
49
|
+
'TIME',
|
|
50
|
+
'U',
|
|
51
|
+
'TT',
|
|
52
|
+
'VAR',
|
|
53
|
+
'VIDEO',
|
|
54
|
+
'WBR',
|
|
55
|
+
'INPUT',
|
|
56
|
+
'SPAN',
|
|
57
|
+
]);
|
|
58
|
+
export const calcElementOuterWidth = (innerWidth, style) => {
|
|
59
|
+
return (innerWidth +
|
|
60
|
+
parseFloat(style.marginLeft) +
|
|
61
|
+
parseFloat(style.marginRight) +
|
|
62
|
+
parseFloat(style.paddingLeft) +
|
|
63
|
+
parseFloat(style.paddingRight) +
|
|
64
|
+
parseFloat(style.borderLeftWidth) +
|
|
65
|
+
parseFloat(style.borderRightWidth));
|
|
66
|
+
};
|
|
67
|
+
export const calcElementLayout = (element) => {
|
|
68
|
+
if (!element)
|
|
69
|
+
return 'vertical';
|
|
70
|
+
const parent = element.parentElement;
|
|
71
|
+
if (!parent)
|
|
72
|
+
return 'vertical';
|
|
73
|
+
const tagName = element.tagName;
|
|
74
|
+
const parentTagName = parent.tagName;
|
|
75
|
+
const style = getComputedStyle(element);
|
|
76
|
+
const parentStyle = getComputedStyle(parent);
|
|
77
|
+
const isNotFullWidth = () => {
|
|
78
|
+
const innerWidth = element.getBoundingClientRect().width;
|
|
79
|
+
const outerWidth = calcElementOuterWidth(innerWidth, style);
|
|
80
|
+
const parentInnerWidth = parent.getBoundingClientRect().width;
|
|
81
|
+
return outerWidth.toFixed(0) < parentInnerWidth.toFixed(0);
|
|
82
|
+
};
|
|
83
|
+
if (tagName === 'TH' || tagName === 'TD') {
|
|
84
|
+
if (parentTagName === 'TR')
|
|
85
|
+
return 'horizontal';
|
|
86
|
+
}
|
|
87
|
+
if (parentStyle.display === 'flex' && parentStyle.flexDirection === 'row')
|
|
88
|
+
return 'horizontal';
|
|
89
|
+
if (parentStyle.display === 'grid') {
|
|
90
|
+
if (isNotFullWidth()) {
|
|
91
|
+
return 'horizontal';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (InlineLayoutTagNames.has(tagName)) {
|
|
95
|
+
if (style.display === 'block') {
|
|
96
|
+
if (style.float === 'left' || style.float === 'right') {
|
|
97
|
+
if (isNotFullWidth()) {
|
|
98
|
+
return 'horizontal';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return 'vertical';
|
|
102
|
+
}
|
|
103
|
+
return 'horizontal';
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
export const calcElementTranslate = (element) => {
|
|
107
|
+
var _a, _b, _c;
|
|
108
|
+
const transform = (_a = element === null || element === void 0 ? void 0 : element.style) === null || _a === void 0 ? void 0 : _a.transform;
|
|
109
|
+
if (transform) {
|
|
110
|
+
const [x, y] = (_c = (_b = transform
|
|
111
|
+
.match(/translate(?:3d)?\(\s*([-\d.]+)[a-z]+?[\s,]+([-\d.]+)[a-z]+?(?:[\s,]+([-\d.]+))?[a-z]+?\s*\)/)) === null || _b === void 0 ? void 0 : _b.slice(1, 3)) !== null && _c !== void 0 ? _c : [0, 0];
|
|
112
|
+
return new Point(Number(x), Number(y));
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
return new Point(Number(element.offsetLeft), Number(element.offsetTop));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
export const calcElementRotate = (element) => {
|
|
119
|
+
var _a, _b, _c;
|
|
120
|
+
const transform = (_a = element === null || element === void 0 ? void 0 : element.style) === null || _a === void 0 ? void 0 : _a.transform;
|
|
121
|
+
if (transform) {
|
|
122
|
+
return Number((_c = (_b = transform.match(/rotate\(\s*([-\d.]+)/)) === null || _b === void 0 ? void 0 : _b[1]) !== null && _c !== void 0 ? _c : 0);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
export const calcElementScale = (element) => {
|
|
129
|
+
var _a, _b, _c;
|
|
130
|
+
const transform = (_a = element === null || element === void 0 ? void 0 : element.style) === null || _a === void 0 ? void 0 : _a.transform;
|
|
131
|
+
if (transform) {
|
|
132
|
+
return Number((_c = (_b = transform.match(/scale\(\s*([-\d.]+)/)) === null || _b === void 0 ? void 0 : _b[1]) !== null && _c !== void 0 ? _c : 0);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
137
|
+
};
|