@dnd-kit/geometry 0.0.1
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/README.md +15 -0
- package/dist/index.d.ts +157 -0
- package/dist/index.js +280 -0
- package/dist/index.mjs +252 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @dnd-kit/geometry
|
|
2
|
+
|
|
3
|
+
[](https://npm.im/@dnd-kit/geometry)
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To get started, install the `@dnd-kit/geometry` package via npm or yarn:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install @dnd-kit/geometry
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Visit [docs.dndkit.com](https://docs.dndkit.com) to learn how to get started with @dnd-kit.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
declare enum Axis {
|
|
2
|
+
Horizontal = "x",
|
|
3
|
+
Vertical = "y"
|
|
4
|
+
}
|
|
5
|
+
declare const Axes: Axis[];
|
|
6
|
+
|
|
7
|
+
type Coordinates = Record<Axis, number>;
|
|
8
|
+
|
|
9
|
+
interface BoundingRectangle {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
left: number;
|
|
13
|
+
right: number;
|
|
14
|
+
top: number;
|
|
15
|
+
bottom: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A Point represents a location in a two-dimensional coordinate system.
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
declare class Point implements Coordinates {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
/**
|
|
26
|
+
* @param {number} Coordinate of the point on the horizontal axis
|
|
27
|
+
* @param {number} Coordinate of the point on the vertical axis
|
|
28
|
+
*/
|
|
29
|
+
constructor(x: number, y: number);
|
|
30
|
+
/**
|
|
31
|
+
* Returns the delta between this point and another point.
|
|
32
|
+
*
|
|
33
|
+
* @param {Point} a - A point
|
|
34
|
+
* @param {Point} b - Another point
|
|
35
|
+
*/
|
|
36
|
+
static delta(a: Point, b: Point): Point;
|
|
37
|
+
/**
|
|
38
|
+
* Returns the distance (hypotenuse) between this point and another point.
|
|
39
|
+
*
|
|
40
|
+
* @param {Point} a - A point
|
|
41
|
+
* @param {Point} b - Another point
|
|
42
|
+
*/
|
|
43
|
+
static distance(a: Point, b: Point): number;
|
|
44
|
+
/**
|
|
45
|
+
* Returns true if both points are equal.
|
|
46
|
+
*
|
|
47
|
+
* @param {Point} a - A point
|
|
48
|
+
* @param {Point} b - Another point
|
|
49
|
+
*/
|
|
50
|
+
static equals(a: Point, b: Point): boolean;
|
|
51
|
+
static from({ x, y }: Coordinates): Point;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* An abstract class representing a 2D geometric shape, such as
|
|
56
|
+
* a polygon or circle. Shapes are used for collision detection
|
|
57
|
+
* during drag and drop operations.
|
|
58
|
+
*/
|
|
59
|
+
declare abstract class Shape {
|
|
60
|
+
/**
|
|
61
|
+
* Get the bounding rectangle of the 2D shape.
|
|
62
|
+
* @returns The bounding rectangle of the shape.
|
|
63
|
+
*/
|
|
64
|
+
abstract get boundingRectangle(): BoundingRectangle;
|
|
65
|
+
/**
|
|
66
|
+
* Get the center point of the 2D shape.
|
|
67
|
+
* @returns The center point of the shape.
|
|
68
|
+
*/
|
|
69
|
+
abstract get center(): Point;
|
|
70
|
+
/**
|
|
71
|
+
* Get the total space taken up by the 2D shape.
|
|
72
|
+
* @returns The area of the shape.
|
|
73
|
+
*/
|
|
74
|
+
abstract get area(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Get the scale transformation of the shape on the 2D plane.
|
|
77
|
+
* @returns The scale of the shape.
|
|
78
|
+
*/
|
|
79
|
+
abstract get scale(): {
|
|
80
|
+
x: number;
|
|
81
|
+
y: number;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Get the inverse scale transformation of the shape on the 2D plane.
|
|
85
|
+
* @returns The inverse scale of the shape.
|
|
86
|
+
*/
|
|
87
|
+
abstract get inverseScale(): {
|
|
88
|
+
x: number;
|
|
89
|
+
y: number;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Returns whether or not this shape is equal to another shape.
|
|
93
|
+
*
|
|
94
|
+
* @param shape The other shape to compare with.
|
|
95
|
+
* @returns Whether or not the two shapes are equal.
|
|
96
|
+
*/
|
|
97
|
+
abstract equals(shape: Shape): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Returns the intersection area between this shape and another shape.
|
|
100
|
+
*
|
|
101
|
+
* @param shape The other shape to calculate the intersection area with.
|
|
102
|
+
* @returns The intersection area between the two shapes.
|
|
103
|
+
*/
|
|
104
|
+
abstract intersectionArea(shape: Shape): number;
|
|
105
|
+
/**
|
|
106
|
+
* Test a point for containment within this shape.
|
|
107
|
+
*
|
|
108
|
+
* @param point A point in world coordinates.
|
|
109
|
+
*/
|
|
110
|
+
abstract containsPoint(point: Point): boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
declare class Rectangle implements Shape {
|
|
114
|
+
left: number;
|
|
115
|
+
top: number;
|
|
116
|
+
width: number;
|
|
117
|
+
height: number;
|
|
118
|
+
constructor(left: number, top: number, width: number, height: number);
|
|
119
|
+
scale: {
|
|
120
|
+
x: number;
|
|
121
|
+
y: number;
|
|
122
|
+
};
|
|
123
|
+
get inverseScale(): {
|
|
124
|
+
x: number;
|
|
125
|
+
y: number;
|
|
126
|
+
};
|
|
127
|
+
translate(x: number, y: number): Rectangle;
|
|
128
|
+
get boundingRectangle(): BoundingRectangle;
|
|
129
|
+
get center(): Point;
|
|
130
|
+
get area(): number;
|
|
131
|
+
equals(shape: Shape): boolean;
|
|
132
|
+
containsPoint(point: Point): boolean;
|
|
133
|
+
intersectionArea(shape: Shape): number;
|
|
134
|
+
intersectionRatio(shape: Shape): number;
|
|
135
|
+
get bottom(): number;
|
|
136
|
+
get right(): number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
declare class Position {
|
|
140
|
+
constructor(initialValue: Coordinates);
|
|
141
|
+
initial: Point;
|
|
142
|
+
previous: Point;
|
|
143
|
+
current: Point;
|
|
144
|
+
get delta(): Point;
|
|
145
|
+
get direction(): "right" | "left" | "down" | "up" | null;
|
|
146
|
+
reset(coordinates: Coordinates): void;
|
|
147
|
+
update(coordinates: Coordinates): void;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
type Distance = number | Coordinates | Pick<Coordinates, Axis.Horizontal> | Pick<Coordinates, Axis.Vertical>;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Returns true if a set of relative coordinates exceeds a given distance.
|
|
154
|
+
*/
|
|
155
|
+
declare function exceedsDistance({ x, y }: Coordinates, distance: Distance): boolean;
|
|
156
|
+
|
|
157
|
+
export { Axes, Axis, BoundingRectangle, Coordinates, Distance, Point, Position, Rectangle, Shape, exceedsDistance };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __pow = Math.pow;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __export = (target, all) => {
|
|
22
|
+
for (var name in all)
|
|
23
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
24
|
+
};
|
|
25
|
+
var __copyProps = (to, from, except, desc) => {
|
|
26
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
27
|
+
for (let key of __getOwnPropNames(from))
|
|
28
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
29
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
30
|
+
}
|
|
31
|
+
return to;
|
|
32
|
+
};
|
|
33
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
34
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
35
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
36
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
37
|
+
if (decorator = decorators[i])
|
|
38
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
39
|
+
if (kind && result)
|
|
40
|
+
__defProp(target, key, result);
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/index.ts
|
|
45
|
+
var src_exports = {};
|
|
46
|
+
__export(src_exports, {
|
|
47
|
+
Axes: () => Axes,
|
|
48
|
+
Axis: () => Axis,
|
|
49
|
+
Point: () => Point,
|
|
50
|
+
Position: () => Position,
|
|
51
|
+
Rectangle: () => Rectangle,
|
|
52
|
+
exceedsDistance: () => exceedsDistance
|
|
53
|
+
});
|
|
54
|
+
module.exports = __toCommonJS(src_exports);
|
|
55
|
+
|
|
56
|
+
// src/point/Point.ts
|
|
57
|
+
var Point = class {
|
|
58
|
+
/**
|
|
59
|
+
* @param {number} Coordinate of the point on the horizontal axis
|
|
60
|
+
* @param {number} Coordinate of the point on the vertical axis
|
|
61
|
+
*/
|
|
62
|
+
constructor(x, y) {
|
|
63
|
+
this.x = x;
|
|
64
|
+
this.y = y;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Returns the delta between this point and another point.
|
|
68
|
+
*
|
|
69
|
+
* @param {Point} a - A point
|
|
70
|
+
* @param {Point} b - Another point
|
|
71
|
+
*/
|
|
72
|
+
static delta(a, b) {
|
|
73
|
+
return new Point(a.x - b.x, a.y - b.y);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Returns the distance (hypotenuse) between this point and another point.
|
|
77
|
+
*
|
|
78
|
+
* @param {Point} a - A point
|
|
79
|
+
* @param {Point} b - Another point
|
|
80
|
+
*/
|
|
81
|
+
static distance(a, b) {
|
|
82
|
+
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns true if both points are equal.
|
|
86
|
+
*
|
|
87
|
+
* @param {Point} a - A point
|
|
88
|
+
* @param {Point} b - Another point
|
|
89
|
+
*/
|
|
90
|
+
static equals(a, b) {
|
|
91
|
+
return a.x === b.x && a.y === b.y;
|
|
92
|
+
}
|
|
93
|
+
static from({ x, y }) {
|
|
94
|
+
return new Point(x, y);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/shapes/Rectangle.ts
|
|
99
|
+
var Rectangle = class {
|
|
100
|
+
constructor(left, top, width, height) {
|
|
101
|
+
this.left = left;
|
|
102
|
+
this.top = top;
|
|
103
|
+
this.width = width;
|
|
104
|
+
this.height = height;
|
|
105
|
+
this.scale = {
|
|
106
|
+
x: 1,
|
|
107
|
+
y: 1
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
get inverseScale() {
|
|
111
|
+
return {
|
|
112
|
+
x: 1 / this.scale.x,
|
|
113
|
+
y: 1 / this.scale.y
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
translate(x, y) {
|
|
117
|
+
const { top, left, width, height, scale } = this;
|
|
118
|
+
const newShape = new Rectangle(left + x, top + y, width, height);
|
|
119
|
+
newShape.scale = __spreadValues({}, scale);
|
|
120
|
+
return newShape;
|
|
121
|
+
}
|
|
122
|
+
get boundingRectangle() {
|
|
123
|
+
const { width, height, left, top, right, bottom } = this;
|
|
124
|
+
return { width, height, left, top, right, bottom };
|
|
125
|
+
}
|
|
126
|
+
get center() {
|
|
127
|
+
const { left, top, right, bottom } = this;
|
|
128
|
+
return new Point((left + right) / 2, (top + bottom) / 2);
|
|
129
|
+
}
|
|
130
|
+
get area() {
|
|
131
|
+
const { width, height } = this;
|
|
132
|
+
return width * height;
|
|
133
|
+
}
|
|
134
|
+
equals(shape) {
|
|
135
|
+
if (!(shape instanceof Rectangle)) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
const { left, top, width, height } = this;
|
|
139
|
+
return left === shape.left && top === shape.top && width === shape.width && height === shape.height;
|
|
140
|
+
}
|
|
141
|
+
containsPoint(point) {
|
|
142
|
+
const { top, left, bottom, right } = this;
|
|
143
|
+
return top <= point.y && point.y <= bottom && left <= point.x && point.x <= right;
|
|
144
|
+
}
|
|
145
|
+
intersectionArea(shape) {
|
|
146
|
+
if (shape instanceof Rectangle) {
|
|
147
|
+
return rectangleRectangleIntersection(this, shape);
|
|
148
|
+
}
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
intersectionRatio(shape) {
|
|
152
|
+
const { area } = this;
|
|
153
|
+
const intersectionArea = this.intersectionArea(shape);
|
|
154
|
+
const intersectionRatio = intersectionArea / (shape.area + area - intersectionArea);
|
|
155
|
+
return intersectionRatio;
|
|
156
|
+
}
|
|
157
|
+
get bottom() {
|
|
158
|
+
const { top, height } = this;
|
|
159
|
+
return top + height;
|
|
160
|
+
}
|
|
161
|
+
get right() {
|
|
162
|
+
const { left, width } = this;
|
|
163
|
+
return left + width;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
function rectangleRectangleIntersection(a, b) {
|
|
167
|
+
const top = Math.max(b.top, a.top);
|
|
168
|
+
const left = Math.max(b.left, a.left);
|
|
169
|
+
const right = Math.min(b.left + b.width, a.left + a.width);
|
|
170
|
+
const bottom = Math.min(b.top + b.height, a.top + a.height);
|
|
171
|
+
const width = right - left;
|
|
172
|
+
const height = bottom - top;
|
|
173
|
+
if (left < right && top < bottom) {
|
|
174
|
+
const intersectionArea = width * height;
|
|
175
|
+
return intersectionArea;
|
|
176
|
+
}
|
|
177
|
+
return 0;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/position/position.ts
|
|
181
|
+
var import_state = require("@dnd-kit/state");
|
|
182
|
+
var SENSITIVITY = 10;
|
|
183
|
+
var Position = class {
|
|
184
|
+
constructor(initialValue) {
|
|
185
|
+
const point = Point.from(initialValue);
|
|
186
|
+
this.initial = point;
|
|
187
|
+
this.current = point;
|
|
188
|
+
this.previous = point;
|
|
189
|
+
}
|
|
190
|
+
get delta() {
|
|
191
|
+
return Point.delta(this.current, this.initial);
|
|
192
|
+
}
|
|
193
|
+
get direction() {
|
|
194
|
+
const delta = {
|
|
195
|
+
x: this.current.x - this.previous.x,
|
|
196
|
+
y: this.current.y - this.previous.y
|
|
197
|
+
};
|
|
198
|
+
if (!delta.x && !delta.y) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
if (Math.abs(delta.x) > Math.abs(delta.y)) {
|
|
202
|
+
return delta.x > 0 ? "right" : "left";
|
|
203
|
+
}
|
|
204
|
+
return delta.y > 0 ? "down" : "up";
|
|
205
|
+
}
|
|
206
|
+
reset(coordinates) {
|
|
207
|
+
const point = Point.from(coordinates);
|
|
208
|
+
(0, import_state.batch)(() => {
|
|
209
|
+
this.current = point;
|
|
210
|
+
this.previous = point;
|
|
211
|
+
this.initial = point;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
update(coordinates) {
|
|
215
|
+
const { current } = this;
|
|
216
|
+
const point = Point.from(coordinates);
|
|
217
|
+
if (Point.equals(current, point)) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const delta = {
|
|
221
|
+
x: point.x - current.x,
|
|
222
|
+
y: point.y - current.y
|
|
223
|
+
};
|
|
224
|
+
if (Math.abs(delta.x) < SENSITIVITY || Math.abs(delta.y) < SENSITIVITY) {
|
|
225
|
+
this.previous = current;
|
|
226
|
+
}
|
|
227
|
+
this.current = point;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
__decorateClass([
|
|
231
|
+
import_state.reactive
|
|
232
|
+
], Position.prototype, "initial", 2);
|
|
233
|
+
__decorateClass([
|
|
234
|
+
import_state.reactive
|
|
235
|
+
], Position.prototype, "previous", 2);
|
|
236
|
+
__decorateClass([
|
|
237
|
+
import_state.reactive
|
|
238
|
+
], Position.prototype, "current", 2);
|
|
239
|
+
__decorateClass([
|
|
240
|
+
import_state.derived
|
|
241
|
+
], Position.prototype, "delta", 1);
|
|
242
|
+
__decorateClass([
|
|
243
|
+
import_state.derived
|
|
244
|
+
], Position.prototype, "direction", 1);
|
|
245
|
+
|
|
246
|
+
// src/distance/distance.ts
|
|
247
|
+
function exceedsDistance({ x, y }, distance) {
|
|
248
|
+
const dx = Math.abs(x);
|
|
249
|
+
const dy = Math.abs(y);
|
|
250
|
+
if (typeof distance === "number") {
|
|
251
|
+
return Math.sqrt(__pow(dx, 2) + __pow(dy, 2)) > distance;
|
|
252
|
+
}
|
|
253
|
+
if ("x" in distance && "y" in distance) {
|
|
254
|
+
return dx > distance.x && dy > distance.y;
|
|
255
|
+
}
|
|
256
|
+
if ("x" in distance) {
|
|
257
|
+
return dx > distance.x;
|
|
258
|
+
}
|
|
259
|
+
if ("y" in distance) {
|
|
260
|
+
return dy > distance.y;
|
|
261
|
+
}
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// src/types/axis.ts
|
|
266
|
+
var Axis = /* @__PURE__ */ ((Axis2) => {
|
|
267
|
+
Axis2["Horizontal"] = "x";
|
|
268
|
+
Axis2["Vertical"] = "y";
|
|
269
|
+
return Axis2;
|
|
270
|
+
})(Axis || {});
|
|
271
|
+
var Axes = Object.values(Axis);
|
|
272
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
273
|
+
0 && (module.exports = {
|
|
274
|
+
Axes,
|
|
275
|
+
Axis,
|
|
276
|
+
Point,
|
|
277
|
+
Position,
|
|
278
|
+
Rectangle,
|
|
279
|
+
exceedsDistance
|
|
280
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
6
|
+
var __pow = Math.pow;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
20
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
21
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
22
|
+
if (decorator = decorators[i])
|
|
23
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
24
|
+
if (kind && result)
|
|
25
|
+
__defProp(target, key, result);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/point/Point.ts
|
|
30
|
+
var Point = class {
|
|
31
|
+
/**
|
|
32
|
+
* @param {number} Coordinate of the point on the horizontal axis
|
|
33
|
+
* @param {number} Coordinate of the point on the vertical axis
|
|
34
|
+
*/
|
|
35
|
+
constructor(x, y) {
|
|
36
|
+
this.x = x;
|
|
37
|
+
this.y = y;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns the delta between this point and another point.
|
|
41
|
+
*
|
|
42
|
+
* @param {Point} a - A point
|
|
43
|
+
* @param {Point} b - Another point
|
|
44
|
+
*/
|
|
45
|
+
static delta(a, b) {
|
|
46
|
+
return new Point(a.x - b.x, a.y - b.y);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns the distance (hypotenuse) between this point and another point.
|
|
50
|
+
*
|
|
51
|
+
* @param {Point} a - A point
|
|
52
|
+
* @param {Point} b - Another point
|
|
53
|
+
*/
|
|
54
|
+
static distance(a, b) {
|
|
55
|
+
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns true if both points are equal.
|
|
59
|
+
*
|
|
60
|
+
* @param {Point} a - A point
|
|
61
|
+
* @param {Point} b - Another point
|
|
62
|
+
*/
|
|
63
|
+
static equals(a, b) {
|
|
64
|
+
return a.x === b.x && a.y === b.y;
|
|
65
|
+
}
|
|
66
|
+
static from({ x, y }) {
|
|
67
|
+
return new Point(x, y);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/shapes/Rectangle.ts
|
|
72
|
+
var Rectangle = class {
|
|
73
|
+
constructor(left, top, width, height) {
|
|
74
|
+
this.left = left;
|
|
75
|
+
this.top = top;
|
|
76
|
+
this.width = width;
|
|
77
|
+
this.height = height;
|
|
78
|
+
this.scale = {
|
|
79
|
+
x: 1,
|
|
80
|
+
y: 1
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
get inverseScale() {
|
|
84
|
+
return {
|
|
85
|
+
x: 1 / this.scale.x,
|
|
86
|
+
y: 1 / this.scale.y
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
translate(x, y) {
|
|
90
|
+
const { top, left, width, height, scale } = this;
|
|
91
|
+
const newShape = new Rectangle(left + x, top + y, width, height);
|
|
92
|
+
newShape.scale = __spreadValues({}, scale);
|
|
93
|
+
return newShape;
|
|
94
|
+
}
|
|
95
|
+
get boundingRectangle() {
|
|
96
|
+
const { width, height, left, top, right, bottom } = this;
|
|
97
|
+
return { width, height, left, top, right, bottom };
|
|
98
|
+
}
|
|
99
|
+
get center() {
|
|
100
|
+
const { left, top, right, bottom } = this;
|
|
101
|
+
return new Point((left + right) / 2, (top + bottom) / 2);
|
|
102
|
+
}
|
|
103
|
+
get area() {
|
|
104
|
+
const { width, height } = this;
|
|
105
|
+
return width * height;
|
|
106
|
+
}
|
|
107
|
+
equals(shape) {
|
|
108
|
+
if (!(shape instanceof Rectangle)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const { left, top, width, height } = this;
|
|
112
|
+
return left === shape.left && top === shape.top && width === shape.width && height === shape.height;
|
|
113
|
+
}
|
|
114
|
+
containsPoint(point) {
|
|
115
|
+
const { top, left, bottom, right } = this;
|
|
116
|
+
return top <= point.y && point.y <= bottom && left <= point.x && point.x <= right;
|
|
117
|
+
}
|
|
118
|
+
intersectionArea(shape) {
|
|
119
|
+
if (shape instanceof Rectangle) {
|
|
120
|
+
return rectangleRectangleIntersection(this, shape);
|
|
121
|
+
}
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
intersectionRatio(shape) {
|
|
125
|
+
const { area } = this;
|
|
126
|
+
const intersectionArea = this.intersectionArea(shape);
|
|
127
|
+
const intersectionRatio = intersectionArea / (shape.area + area - intersectionArea);
|
|
128
|
+
return intersectionRatio;
|
|
129
|
+
}
|
|
130
|
+
get bottom() {
|
|
131
|
+
const { top, height } = this;
|
|
132
|
+
return top + height;
|
|
133
|
+
}
|
|
134
|
+
get right() {
|
|
135
|
+
const { left, width } = this;
|
|
136
|
+
return left + width;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
function rectangleRectangleIntersection(a, b) {
|
|
140
|
+
const top = Math.max(b.top, a.top);
|
|
141
|
+
const left = Math.max(b.left, a.left);
|
|
142
|
+
const right = Math.min(b.left + b.width, a.left + a.width);
|
|
143
|
+
const bottom = Math.min(b.top + b.height, a.top + a.height);
|
|
144
|
+
const width = right - left;
|
|
145
|
+
const height = bottom - top;
|
|
146
|
+
if (left < right && top < bottom) {
|
|
147
|
+
const intersectionArea = width * height;
|
|
148
|
+
return intersectionArea;
|
|
149
|
+
}
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/position/position.ts
|
|
154
|
+
import { batch, derived, reactive } from "@dnd-kit/state";
|
|
155
|
+
var SENSITIVITY = 10;
|
|
156
|
+
var Position = class {
|
|
157
|
+
constructor(initialValue) {
|
|
158
|
+
const point = Point.from(initialValue);
|
|
159
|
+
this.initial = point;
|
|
160
|
+
this.current = point;
|
|
161
|
+
this.previous = point;
|
|
162
|
+
}
|
|
163
|
+
get delta() {
|
|
164
|
+
return Point.delta(this.current, this.initial);
|
|
165
|
+
}
|
|
166
|
+
get direction() {
|
|
167
|
+
const delta = {
|
|
168
|
+
x: this.current.x - this.previous.x,
|
|
169
|
+
y: this.current.y - this.previous.y
|
|
170
|
+
};
|
|
171
|
+
if (!delta.x && !delta.y) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
if (Math.abs(delta.x) > Math.abs(delta.y)) {
|
|
175
|
+
return delta.x > 0 ? "right" : "left";
|
|
176
|
+
}
|
|
177
|
+
return delta.y > 0 ? "down" : "up";
|
|
178
|
+
}
|
|
179
|
+
reset(coordinates) {
|
|
180
|
+
const point = Point.from(coordinates);
|
|
181
|
+
batch(() => {
|
|
182
|
+
this.current = point;
|
|
183
|
+
this.previous = point;
|
|
184
|
+
this.initial = point;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
update(coordinates) {
|
|
188
|
+
const { current } = this;
|
|
189
|
+
const point = Point.from(coordinates);
|
|
190
|
+
if (Point.equals(current, point)) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const delta = {
|
|
194
|
+
x: point.x - current.x,
|
|
195
|
+
y: point.y - current.y
|
|
196
|
+
};
|
|
197
|
+
if (Math.abs(delta.x) < SENSITIVITY || Math.abs(delta.y) < SENSITIVITY) {
|
|
198
|
+
this.previous = current;
|
|
199
|
+
}
|
|
200
|
+
this.current = point;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
__decorateClass([
|
|
204
|
+
reactive
|
|
205
|
+
], Position.prototype, "initial", 2);
|
|
206
|
+
__decorateClass([
|
|
207
|
+
reactive
|
|
208
|
+
], Position.prototype, "previous", 2);
|
|
209
|
+
__decorateClass([
|
|
210
|
+
reactive
|
|
211
|
+
], Position.prototype, "current", 2);
|
|
212
|
+
__decorateClass([
|
|
213
|
+
derived
|
|
214
|
+
], Position.prototype, "delta", 1);
|
|
215
|
+
__decorateClass([
|
|
216
|
+
derived
|
|
217
|
+
], Position.prototype, "direction", 1);
|
|
218
|
+
|
|
219
|
+
// src/distance/distance.ts
|
|
220
|
+
function exceedsDistance({ x, y }, distance) {
|
|
221
|
+
const dx = Math.abs(x);
|
|
222
|
+
const dy = Math.abs(y);
|
|
223
|
+
if (typeof distance === "number") {
|
|
224
|
+
return Math.sqrt(__pow(dx, 2) + __pow(dy, 2)) > distance;
|
|
225
|
+
}
|
|
226
|
+
if ("x" in distance && "y" in distance) {
|
|
227
|
+
return dx > distance.x && dy > distance.y;
|
|
228
|
+
}
|
|
229
|
+
if ("x" in distance) {
|
|
230
|
+
return dx > distance.x;
|
|
231
|
+
}
|
|
232
|
+
if ("y" in distance) {
|
|
233
|
+
return dy > distance.y;
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/types/axis.ts
|
|
239
|
+
var Axis = /* @__PURE__ */ ((Axis2) => {
|
|
240
|
+
Axis2["Horizontal"] = "x";
|
|
241
|
+
Axis2["Vertical"] = "y";
|
|
242
|
+
return Axis2;
|
|
243
|
+
})(Axis || {});
|
|
244
|
+
var Axes = Object.values(Axis);
|
|
245
|
+
export {
|
|
246
|
+
Axes,
|
|
247
|
+
Axis,
|
|
248
|
+
Point,
|
|
249
|
+
Position,
|
|
250
|
+
Rectangle,
|
|
251
|
+
exceedsDistance
|
|
252
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dnd-kit/geometry",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/**"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --external react",
|
|
14
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts --external react",
|
|
15
|
+
"lint": "TIMING=1 eslint src/**/*.ts* --fix",
|
|
16
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@dnd-kit/state": "*",
|
|
20
|
+
"tslib": "^2.6.2"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@dnd-kit/eslint-config": "*",
|
|
24
|
+
"eslint": "^8.38.0",
|
|
25
|
+
"tsup": "^6.7.0",
|
|
26
|
+
"typescript": "^5.0.4"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
}
|
|
31
|
+
}
|