@affino/diagram-core 0.1.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/README.md +133 -0
- package/dist/DiagramEngine.d.ts +72 -0
- package/dist/DiagramEngine.d.ts.map +1 -0
- package/dist/DiagramEngine.js +1545 -0
- package/dist/geometry.d.ts +14 -0
- package/dist/geometry.d.ts.map +1 -0
- package/dist/geometry.js +131 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/interaction.d.ts +48 -0
- package/dist/interaction.d.ts.map +1 -0
- package/dist/interaction.js +462 -0
- package/dist/internal/DiagramClipboardService.d.ts +10 -0
- package/dist/internal/DiagramClipboardService.d.ts.map +1 -0
- package/dist/internal/DiagramClipboardService.js +103 -0
- package/dist/internal/DiagramGeometryService.d.ts +9 -0
- package/dist/internal/DiagramGeometryService.d.ts.map +1 -0
- package/dist/internal/DiagramGeometryService.js +38 -0
- package/dist/internal/DiagramHistory.d.ts +15 -0
- package/dist/internal/DiagramHistory.d.ts.map +1 -0
- package/dist/internal/DiagramHistory.js +53 -0
- package/dist/internal/DiagramSceneStore.d.ts +18 -0
- package/dist/internal/DiagramSceneStore.d.ts.map +1 -0
- package/dist/internal/DiagramSceneStore.js +159 -0
- package/dist/internal/DiagramSpatialIndex.d.ts +13 -0
- package/dist/internal/DiagramSpatialIndex.d.ts.map +1 -0
- package/dist/internal/DiagramSpatialIndex.js +51 -0
- package/dist/internal/DiagramViewportService.d.ts +5 -0
- package/dist/internal/DiagramViewportService.d.ts.map +1 -0
- package/dist/internal/DiagramViewportService.js +16 -0
- package/dist/internal/model.d.ts +43 -0
- package/dist/internal/model.d.ts.map +1 -0
- package/dist/internal/model.js +2 -0
- package/dist/spatialIndex.d.ts +12 -0
- package/dist/spatialIndex.d.ts.map +1 -0
- package/dist/spatialIndex.js +71 -0
- package/dist/types.d.ts +337 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DiagramEdge, DiagramEdgeEndpoint, DiagramEntities, DiagramGeometry, DiagramId, DiagramPoint, DiagramPort, DiagramRect } from "./types.js";
|
|
2
|
+
export declare function rectIntersects(a: DiagramRect, b: DiagramRect): boolean;
|
|
3
|
+
export declare function rectContainsPoint(rect: DiagramRect, point: DiagramPoint, radius?: number): boolean;
|
|
4
|
+
export declare function rectFromPoints(points: ReadonlyArray<DiagramPoint>): DiagramRect;
|
|
5
|
+
export declare function inflateRect(rect: DiagramRect, amount: number): DiagramRect;
|
|
6
|
+
export declare function rectCenter(rect: DiagramRect): DiagramPoint;
|
|
7
|
+
export declare function distance(a: DiagramPoint, b: DiagramPoint): number;
|
|
8
|
+
export declare function createEntityGeometry(id: DiagramId, entities: DiagramEntities): DiagramGeometry | null;
|
|
9
|
+
export declare function resolveEdgePath(edge: DiagramEdge, entities: DiagramEntities): ReadonlyArray<DiagramPoint>;
|
|
10
|
+
export declare function resolveEndpoint(endpoint: DiagramEdgeEndpoint, entities: DiagramEntities): DiagramPoint;
|
|
11
|
+
export declare function resolvePortPoint(port: DiagramPort, entities: DiagramEntities): DiagramPoint;
|
|
12
|
+
export declare function rotatedRectCorners(rect: DiagramRect, rotation: number): ReadonlyArray<DiagramPoint>;
|
|
13
|
+
export declare function rotatePoint(point: DiagramPoint, center: DiagramPoint, rotation: number): DiagramPoint;
|
|
14
|
+
//# sourceMappingURL=geometry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geometry.d.ts","sourceRoot":"","sources":["../src/geometry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,eAAe,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAEvJ,wBAAgB,cAAc,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAAG,OAAO,CAEtE;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,SAAI,GAAG,OAAO,CAK7F;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,CAAC,YAAY,CAAC,GAAG,WAAW,CAe/E;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,CAO1E;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,WAAW,GAAG,YAAY,CAE1D;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,MAAM,CAEjE;AAED,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,SAAS,EAAE,QAAQ,EAAE,eAAe,GAAG,eAAe,GAAG,IAAI,CA6BrG;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,eAAe,GAAG,aAAa,CAAC,YAAY,CAAC,CAMzG;AAED,wBAAgB,eAAe,CAAC,QAAQ,EAAE,mBAAmB,EAAE,QAAQ,EAAE,eAAe,GAAG,YAAY,CAUtG;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,eAAe,GAAG,YAAY,CAO3F;AAYD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,aAAa,CAAC,YAAY,CAAC,CAQnG;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,GAAG,YAAY,CASrG"}
|
package/dist/geometry.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
export function rectIntersects(a, b) {
|
|
2
|
+
return a.x <= b.x + b.width && a.x + a.width >= b.x && a.y <= b.y + b.height && a.y + a.height >= b.y;
|
|
3
|
+
}
|
|
4
|
+
export function rectContainsPoint(rect, point, radius = 0) {
|
|
5
|
+
return point.x >= rect.x - radius
|
|
6
|
+
&& point.x <= rect.x + rect.width + radius
|
|
7
|
+
&& point.y >= rect.y - radius
|
|
8
|
+
&& point.y <= rect.y + rect.height + radius;
|
|
9
|
+
}
|
|
10
|
+
export function rectFromPoints(points) {
|
|
11
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
12
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
13
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
14
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
15
|
+
for (const point of points) {
|
|
16
|
+
minX = Math.min(minX, point.x);
|
|
17
|
+
minY = Math.min(minY, point.y);
|
|
18
|
+
maxX = Math.max(maxX, point.x);
|
|
19
|
+
maxY = Math.max(maxY, point.y);
|
|
20
|
+
}
|
|
21
|
+
if (!points.length) {
|
|
22
|
+
return { x: 0, y: 0, width: 0, height: 0 };
|
|
23
|
+
}
|
|
24
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
25
|
+
}
|
|
26
|
+
export function inflateRect(rect, amount) {
|
|
27
|
+
return {
|
|
28
|
+
x: rect.x - amount,
|
|
29
|
+
y: rect.y - amount,
|
|
30
|
+
width: rect.width + amount * 2,
|
|
31
|
+
height: rect.height + amount * 2,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function rectCenter(rect) {
|
|
35
|
+
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
36
|
+
}
|
|
37
|
+
export function distance(a, b) {
|
|
38
|
+
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
39
|
+
}
|
|
40
|
+
export function createEntityGeometry(id, entities) {
|
|
41
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
42
|
+
const node = entities.nodesById.get(id);
|
|
43
|
+
if (node) {
|
|
44
|
+
return rectGeometry(id, "node", { x: node.x, y: node.y, width: node.width, height: node.height }, (_a = node.rotation) !== null && _a !== void 0 ? _a : 0, 4);
|
|
45
|
+
}
|
|
46
|
+
const port = entities.portsById.get(id);
|
|
47
|
+
if (port) {
|
|
48
|
+
const point = resolvePortPoint(port, entities);
|
|
49
|
+
const radius = (_b = port.radius) !== null && _b !== void 0 ? _b : 4;
|
|
50
|
+
const bounds = { x: point.x - radius, y: point.y - radius, width: radius * 2, height: radius * 2 };
|
|
51
|
+
return { id, kind: "port", bounds, hitBounds: inflateRect(bounds, 4), point };
|
|
52
|
+
}
|
|
53
|
+
const edge = entities.edgesById.get(id);
|
|
54
|
+
if (edge) {
|
|
55
|
+
const path = resolveEdgePath(edge, entities);
|
|
56
|
+
const bounds = rectFromPoints(path);
|
|
57
|
+
return { id, kind: "edge", bounds, hitBounds: inflateRect(bounds, 6), path };
|
|
58
|
+
}
|
|
59
|
+
const text = entities.textsById.get(id);
|
|
60
|
+
if (text) {
|
|
61
|
+
const width = (_c = text.width) !== null && _c !== void 0 ? _c : Math.max(1, text.text.length) * ((_d = text.fontSize) !== null && _d !== void 0 ? _d : 12) * 0.6;
|
|
62
|
+
const height = (_e = text.height) !== null && _e !== void 0 ? _e : ((_f = text.fontSize) !== null && _f !== void 0 ? _f : 12) * 1.2;
|
|
63
|
+
return rectGeometry(id, "text", { x: text.x, y: text.y, width, height }, (_g = text.rotation) !== null && _g !== void 0 ? _g : 0, 3);
|
|
64
|
+
}
|
|
65
|
+
const shape = entities.shapesById.get(id);
|
|
66
|
+
if (shape) {
|
|
67
|
+
return rectGeometry(id, "shape", { x: shape.x, y: shape.y, width: shape.width, height: shape.height }, (_h = shape.rotation) !== null && _h !== void 0 ? _h : 0, 3);
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
export function resolveEdgePath(edge, entities) {
|
|
72
|
+
var _a;
|
|
73
|
+
return [
|
|
74
|
+
resolveEndpoint(edge.source, entities),
|
|
75
|
+
...((_a = edge.points) !== null && _a !== void 0 ? _a : []),
|
|
76
|
+
resolveEndpoint(edge.target, entities),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
export function resolveEndpoint(endpoint, entities) {
|
|
80
|
+
if (endpoint.kind === "point") {
|
|
81
|
+
return endpoint.point;
|
|
82
|
+
}
|
|
83
|
+
if (endpoint.kind === "port") {
|
|
84
|
+
const port = entities.portsById.get(endpoint.portId);
|
|
85
|
+
return port ? resolvePortPoint(port, entities) : { x: 0, y: 0 };
|
|
86
|
+
}
|
|
87
|
+
const node = entities.nodesById.get(endpoint.nodeId);
|
|
88
|
+
return node ? rectCenter({ x: node.x, y: node.y, width: node.width, height: node.height }) : { x: 0, y: 0 };
|
|
89
|
+
}
|
|
90
|
+
export function resolvePortPoint(port, entities) {
|
|
91
|
+
var _a;
|
|
92
|
+
const node = entities.nodesById.get(port.nodeId);
|
|
93
|
+
if (!node) {
|
|
94
|
+
return { x: port.x, y: port.y };
|
|
95
|
+
}
|
|
96
|
+
const point = { x: node.x + port.x, y: node.y + port.y };
|
|
97
|
+
return rotatePoint(point, rectCenter({ x: node.x, y: node.y, width: node.width, height: node.height }), (_a = node.rotation) !== null && _a !== void 0 ? _a : 0);
|
|
98
|
+
}
|
|
99
|
+
function rectGeometry(id, kind, bounds, rotation, hitPadding) {
|
|
100
|
+
const normalizedRotation = normalizeRotation(rotation);
|
|
101
|
+
if (!normalizedRotation) {
|
|
102
|
+
return { id, kind, bounds, hitBounds: inflateRect(bounds, hitPadding), unrotatedBounds: bounds, rotation: 0 };
|
|
103
|
+
}
|
|
104
|
+
const corners = rotatedRectCorners(bounds, normalizedRotation);
|
|
105
|
+
const rotatedBounds = rectFromPoints(corners);
|
|
106
|
+
return { id, kind, bounds: rotatedBounds, hitBounds: inflateRect(rotatedBounds, hitPadding), unrotatedBounds: bounds, corners, rotation: normalizedRotation };
|
|
107
|
+
}
|
|
108
|
+
export function rotatedRectCorners(rect, rotation) {
|
|
109
|
+
const center = rectCenter(rect);
|
|
110
|
+
return [
|
|
111
|
+
{ x: rect.x, y: rect.y },
|
|
112
|
+
{ x: rect.x + rect.width, y: rect.y },
|
|
113
|
+
{ x: rect.x + rect.width, y: rect.y + rect.height },
|
|
114
|
+
{ x: rect.x, y: rect.y + rect.height },
|
|
115
|
+
].map((point) => rotatePoint(point, center, rotation));
|
|
116
|
+
}
|
|
117
|
+
export function rotatePoint(point, center, rotation) {
|
|
118
|
+
const normalizedRotation = normalizeRotation(rotation);
|
|
119
|
+
if (!normalizedRotation)
|
|
120
|
+
return point;
|
|
121
|
+
const angle = normalizedRotation * (Math.PI / 180);
|
|
122
|
+
const cos = Math.cos(angle);
|
|
123
|
+
const sin = Math.sin(angle);
|
|
124
|
+
const dx = point.x - center.x;
|
|
125
|
+
const dy = point.y - center.y;
|
|
126
|
+
return { x: center.x + dx * cos - dy * sin, y: center.y + dx * sin + dy * cos };
|
|
127
|
+
}
|
|
128
|
+
function normalizeRotation(rotation) {
|
|
129
|
+
const normalized = rotation % 360;
|
|
130
|
+
return normalized < 0 ? normalized + 360 : normalized;
|
|
131
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { DiagramEngine, createDiagramEngine, deserializeScene, serializeScene } from "./DiagramEngine.js";
|
|
2
|
+
export { DiagramInteractionController, createDiagramInteractionController } from "./interaction.js";
|
|
3
|
+
export { UniformGridIndex } from "./spatialIndex.js";
|
|
4
|
+
export * from "./geometry.js";
|
|
5
|
+
export type * from "./types.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACzG,OAAO,EAAE,4BAA4B,EAAE,kCAAkC,EAAE,MAAM,kBAAkB,CAAA;AACnG,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,cAAc,eAAe,CAAA;AAC7B,mBAAmB,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { DiagramEngine, createDiagramEngine, deserializeScene, serializeScene } from "./DiagramEngine.js";
|
|
2
|
+
export { DiagramInteractionController, createDiagramInteractionController } from "./interaction.js";
|
|
3
|
+
export { UniformGridIndex } from "./spatialIndex.js";
|
|
4
|
+
export * from "./geometry.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { DiagramEngine } from "./DiagramEngine.js";
|
|
2
|
+
import type { DiagramId, DiagramInteractionSnapshot, DiagramInteractionTool, DiagramMarqueeMode, DiagramPointerEvent, DiagramResizeHandle } from "./types.js";
|
|
3
|
+
type ScheduleFrame = (callback: () => void) => void;
|
|
4
|
+
export type DiagramInteractionOptions = Readonly<{
|
|
5
|
+
scheduleFrame?: ScheduleFrame;
|
|
6
|
+
marqueeMode?: DiagramMarqueeMode;
|
|
7
|
+
}>;
|
|
8
|
+
export declare class DiagramInteractionController {
|
|
9
|
+
private readonly engine;
|
|
10
|
+
private tool;
|
|
11
|
+
private activePointer;
|
|
12
|
+
private startPoint;
|
|
13
|
+
private latestPoint;
|
|
14
|
+
private previewDelta;
|
|
15
|
+
private marquee;
|
|
16
|
+
private resizeOwnerId;
|
|
17
|
+
private resizeSelectionIds;
|
|
18
|
+
private resizeHandle;
|
|
19
|
+
private resizeStartBounds;
|
|
20
|
+
private resizeStartRotation;
|
|
21
|
+
private resizePreview;
|
|
22
|
+
private resizePreviewEntries;
|
|
23
|
+
private framePending;
|
|
24
|
+
private pendingMoveCount;
|
|
25
|
+
private commitCount;
|
|
26
|
+
private readonly scheduleFrame;
|
|
27
|
+
private readonly marqueeMode;
|
|
28
|
+
constructor(engine: DiagramEngine, options?: DiagramInteractionOptions);
|
|
29
|
+
setTool(tool: DiagramInteractionTool): void;
|
|
30
|
+
pointerDown(event: DiagramPointerEvent): void;
|
|
31
|
+
beginResizeHandle(ownerId: DiagramId, handle: DiagramResizeHandle, event: DiagramPointerEvent): boolean;
|
|
32
|
+
beginResizeSelectionHandle(handle: DiagramResizeHandle, event: DiagramPointerEvent): boolean;
|
|
33
|
+
pointerMove(event: DiagramPointerEvent): void;
|
|
34
|
+
pointerUp(event: DiagramPointerEvent): void;
|
|
35
|
+
cancel(): void;
|
|
36
|
+
getSnapshot(): DiagramInteractionSnapshot;
|
|
37
|
+
getPendingMoveCount(): number;
|
|
38
|
+
getCommitCount(): number;
|
|
39
|
+
private flushPointerMove;
|
|
40
|
+
private createResizePreviewEntries;
|
|
41
|
+
private createSelectionResizePreviewEntries;
|
|
42
|
+
private createResizePreview;
|
|
43
|
+
private getMarqueeSelectionIds;
|
|
44
|
+
private clearGesture;
|
|
45
|
+
}
|
|
46
|
+
export declare function createDiagramInteractionController(engine: DiagramEngine, options?: DiagramInteractionOptions): DiagramInteractionController;
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=interaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interaction.d.ts","sourceRoot":"","sources":["../src/interaction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,0BAA0B,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,mBAAmB,EAAiD,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAE5M,KAAK,aAAa,GAAG,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAA;AAEnD,MAAM,MAAM,yBAAyB,GAAG,QAAQ,CAAC;IAC/C,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B,WAAW,CAAC,EAAE,kBAAkB,CAAA;CACjC,CAAC,CAAA;AAEF,qBAAa,4BAA4B;IAoB3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAnBnC,OAAO,CAAC,IAAI,CAAmC;IAC/C,OAAO,CAAC,aAAa,CAAmC;IACxD,OAAO,CAAC,UAAU,CAA4B;IAC9C,OAAO,CAAC,WAAW,CAA4B;IAC/C,OAAO,CAAC,YAAY,CAA4B;IAChD,OAAO,CAAC,OAAO,CAA2B;IAC1C,OAAO,CAAC,aAAa,CAAyB;IAC9C,OAAO,CAAC,kBAAkB,CAA+B;IACzD,OAAO,CAAC,YAAY,CAAmC;IACvD,OAAO,CAAC,iBAAiB,CAA2B;IACpD,OAAO,CAAC,mBAAmB,CAAI;IAC/B,OAAO,CAAC,aAAa,CAAkC;IACvD,OAAO,CAAC,oBAAoB,CAAuD;IACnF,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,gBAAgB,CAAI;IAC5B,OAAO,CAAC,WAAW,CAAI;IACvB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;gBAEnB,MAAM,EAAE,aAAa,EAAE,OAAO,GAAE,yBAA8B;IAK3F,OAAO,CAAC,IAAI,EAAE,sBAAsB,GAAG,IAAI;IAK3C,WAAW,CAAC,KAAK,EAAE,mBAAmB,GAAG,IAAI;IAuB7C,iBAAiB,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,mBAAmB,EAAE,KAAK,EAAE,mBAAmB,GAAG,OAAO;IAiBvG,0BAA0B,CAAC,MAAM,EAAE,mBAAmB,EAAE,KAAK,EAAE,mBAAmB,GAAG,OAAO;IAwB5F,WAAW,CAAC,KAAK,EAAE,mBAAmB,GAAG,IAAI;IAY7C,SAAS,CAAC,KAAK,EAAE,mBAAmB,GAAG,IAAI;IA+B3C,MAAM,IAAI,IAAI;IAId,WAAW,IAAI,0BAA0B;IAWzC,mBAAmB,IAAI,MAAM;IAI7B,cAAc,IAAI,MAAM;IAIxB,OAAO,CAAC,gBAAgB;IAoBxB,OAAO,CAAC,0BAA0B;IAQlC,OAAO,CAAC,mCAAmC;IAqB3C,OAAO,CAAC,mBAAmB;IAiC3B,OAAO,CAAC,sBAAsB;IAgB9B,OAAO,CAAC,YAAY;CAgBrB;AAED,wBAAgB,kCAAkC,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,GAAE,yBAA8B,GAAG,4BAA4B,CAE/I"}
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { createEntityGeometry } from "./geometry.js";
|
|
2
|
+
export class DiagramInteractionController {
|
|
3
|
+
constructor(engine, options = {}) {
|
|
4
|
+
var _a, _b;
|
|
5
|
+
Object.defineProperty(this, "engine", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: engine
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(this, "tool", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: "select"
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "activePointer", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: null
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "startPoint", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: null
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "latestPoint", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: null
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "previewDelta", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: null
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "marquee", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: null
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(this, "resizeOwnerId", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: null
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(this, "resizeSelectionIds", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: []
|
|
58
|
+
});
|
|
59
|
+
Object.defineProperty(this, "resizeHandle", {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: true,
|
|
62
|
+
writable: true,
|
|
63
|
+
value: null
|
|
64
|
+
});
|
|
65
|
+
Object.defineProperty(this, "resizeStartBounds", {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
value: null
|
|
70
|
+
});
|
|
71
|
+
Object.defineProperty(this, "resizeStartRotation", {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
configurable: true,
|
|
74
|
+
writable: true,
|
|
75
|
+
value: 0
|
|
76
|
+
});
|
|
77
|
+
Object.defineProperty(this, "resizePreview", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: null
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "resizePreviewEntries", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: Object.freeze([])
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(this, "framePending", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: true,
|
|
92
|
+
writable: true,
|
|
93
|
+
value: false
|
|
94
|
+
});
|
|
95
|
+
Object.defineProperty(this, "pendingMoveCount", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: 0
|
|
100
|
+
});
|
|
101
|
+
Object.defineProperty(this, "commitCount", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: 0
|
|
106
|
+
});
|
|
107
|
+
Object.defineProperty(this, "scheduleFrame", {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: true,
|
|
111
|
+
value: void 0
|
|
112
|
+
});
|
|
113
|
+
Object.defineProperty(this, "marqueeMode", {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
configurable: true,
|
|
116
|
+
writable: true,
|
|
117
|
+
value: void 0
|
|
118
|
+
});
|
|
119
|
+
this.scheduleFrame = (_a = options.scheduleFrame) !== null && _a !== void 0 ? _a : defaultScheduleFrame;
|
|
120
|
+
this.marqueeMode = (_b = options.marqueeMode) !== null && _b !== void 0 ? _b : "contain";
|
|
121
|
+
}
|
|
122
|
+
setTool(tool) {
|
|
123
|
+
this.cancel();
|
|
124
|
+
this.tool = tool;
|
|
125
|
+
}
|
|
126
|
+
pointerDown(event) {
|
|
127
|
+
this.activePointer = event;
|
|
128
|
+
this.startPoint = event.point;
|
|
129
|
+
this.latestPoint = event.point;
|
|
130
|
+
this.previewDelta = null;
|
|
131
|
+
this.marquee = null;
|
|
132
|
+
if (this.tool === "select") {
|
|
133
|
+
const hit = this.engine.hitTest(event.point, { radius: 2 });
|
|
134
|
+
if (hit) {
|
|
135
|
+
const selection = this.engine.getScene().selection;
|
|
136
|
+
if (event.shiftKey) {
|
|
137
|
+
this.engine.dispatch({ type: "setSelection", selection: { ids: [hit.id], primaryId: hit.id }, mode: "toggle" });
|
|
138
|
+
}
|
|
139
|
+
else if (!selection.ids.includes(hit.id)) {
|
|
140
|
+
this.engine.dispatch({ type: "setSelection", selection: { ids: [hit.id], primaryId: hit.id } });
|
|
141
|
+
}
|
|
142
|
+
this.tool = "drag-selection";
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
this.engine.dispatch({ type: "setSelection", selection: { ids: [], primaryId: null } });
|
|
146
|
+
this.tool = "marquee";
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
beginResizeHandle(ownerId, handle, event) {
|
|
151
|
+
var _a, _b;
|
|
152
|
+
const geometry = this.engine.getGeometrySnapshot(ownerId);
|
|
153
|
+
if (!geometry || geometry.kind === "edge" || geometry.kind === "port" || !this.engine.canResize([ownerId])) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
this.cancel();
|
|
157
|
+
this.tool = "resize-selection";
|
|
158
|
+
this.activePointer = event;
|
|
159
|
+
this.startPoint = event.point;
|
|
160
|
+
this.latestPoint = event.point;
|
|
161
|
+
this.resizeOwnerId = ownerId;
|
|
162
|
+
this.resizeHandle = handle;
|
|
163
|
+
this.resizeStartBounds = (_a = geometry.unrotatedBounds) !== null && _a !== void 0 ? _a : geometry.bounds;
|
|
164
|
+
this.resizeStartRotation = (_b = geometry.rotation) !== null && _b !== void 0 ? _b : 0;
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
beginResizeSelectionHandle(handle, event) {
|
|
168
|
+
const ids = this.engine.getScene().selection.ids.filter((id) => {
|
|
169
|
+
const geometry = this.engine.getGeometrySnapshot(id);
|
|
170
|
+
return geometry && geometry.kind !== "edge" && geometry.kind !== "port" && this.engine.canResize([id]);
|
|
171
|
+
});
|
|
172
|
+
if (ids.length < 2) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
const bounds = unionRects(ids.map((id) => { var _a, _b, _c; return (_b = (_a = this.engine.getGeometrySnapshot(id)) === null || _a === void 0 ? void 0 : _a.unrotatedBounds) !== null && _b !== void 0 ? _b : (_c = this.engine.getGeometrySnapshot(id)) === null || _c === void 0 ? void 0 : _c.bounds; }).filter((rect) => Boolean(rect)));
|
|
176
|
+
if (!bounds) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
this.cancel();
|
|
180
|
+
this.tool = "resize-selection";
|
|
181
|
+
this.activePointer = event;
|
|
182
|
+
this.startPoint = event.point;
|
|
183
|
+
this.latestPoint = event.point;
|
|
184
|
+
this.resizeSelectionIds = ids;
|
|
185
|
+
this.resizeHandle = handle;
|
|
186
|
+
this.resizeStartBounds = bounds;
|
|
187
|
+
this.resizeStartRotation = 0;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
pointerMove(event) {
|
|
191
|
+
if (!this.activePointer || this.activePointer.id !== event.id) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.latestPoint = event.point;
|
|
195
|
+
this.pendingMoveCount += 1;
|
|
196
|
+
if (!this.framePending) {
|
|
197
|
+
this.framePending = true;
|
|
198
|
+
this.scheduleFrame(() => this.flushPointerMove());
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
pointerUp(event) {
|
|
202
|
+
var _a, _b, _c, _d;
|
|
203
|
+
if (!this.activePointer || this.activePointer.id !== event.id) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
this.latestPoint = event.point;
|
|
207
|
+
this.flushPointerMove();
|
|
208
|
+
if (this.tool === "drag-selection" && this.previewDelta) {
|
|
209
|
+
const selection = this.engine.getScene().selection;
|
|
210
|
+
this.engine.dispatch({ type: "moveEntities", ids: selection.ids, delta: this.previewDelta, historyKey: `drag:${(_a = selection.primaryId) !== null && _a !== void 0 ? _a : "selection"}` });
|
|
211
|
+
this.commitCount += 1;
|
|
212
|
+
}
|
|
213
|
+
if (this.tool === "pan" && this.previewDelta) {
|
|
214
|
+
const viewport = this.engine.getScene().viewport;
|
|
215
|
+
this.engine.dispatch({ type: "setViewport", viewport: { x: viewport.x - this.previewDelta.x, y: viewport.y - this.previewDelta.y }, historyKey: "pan" });
|
|
216
|
+
this.commitCount += 1;
|
|
217
|
+
}
|
|
218
|
+
if (this.tool === "resize-selection" && this.resizePreviewEntries.length) {
|
|
219
|
+
this.engine.dispatch({ type: "resizeEntities", entries: this.resizePreviewEntries, historyKey: `resize:${(_c = (_b = this.resizePreview) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : "selection"}` });
|
|
220
|
+
this.commitCount += 1;
|
|
221
|
+
}
|
|
222
|
+
if (this.tool === "marquee" && this.marquee) {
|
|
223
|
+
const ids = this.getMarqueeSelectionIds(this.marquee);
|
|
224
|
+
this.engine.dispatch({ type: "setSelection", selection: { ids, primaryId: (_d = ids[0]) !== null && _d !== void 0 ? _d : null }, mode: this.activePointer.shiftKey ? "add" : "replace" });
|
|
225
|
+
this.commitCount += 1;
|
|
226
|
+
}
|
|
227
|
+
this.clearGesture();
|
|
228
|
+
if (this.tool === "drag-selection" || this.tool === "marquee" || this.tool === "resize-selection") {
|
|
229
|
+
this.tool = "select";
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
cancel() {
|
|
233
|
+
this.clearGesture();
|
|
234
|
+
}
|
|
235
|
+
getSnapshot() {
|
|
236
|
+
return {
|
|
237
|
+
tool: this.tool,
|
|
238
|
+
active: this.activePointer !== null,
|
|
239
|
+
previewDelta: this.previewDelta,
|
|
240
|
+
resizePreview: this.resizePreview,
|
|
241
|
+
resizePreviewEntries: this.resizePreviewEntries,
|
|
242
|
+
marquee: this.marquee,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
getPendingMoveCount() {
|
|
246
|
+
return this.pendingMoveCount;
|
|
247
|
+
}
|
|
248
|
+
getCommitCount() {
|
|
249
|
+
return this.commitCount;
|
|
250
|
+
}
|
|
251
|
+
flushPointerMove() {
|
|
252
|
+
var _a;
|
|
253
|
+
this.framePending = false;
|
|
254
|
+
if (!this.startPoint || !this.latestPoint) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const delta = { x: this.latestPoint.x - this.startPoint.x, y: this.latestPoint.y - this.startPoint.y };
|
|
258
|
+
if (this.tool === "drag-selection" || this.tool === "pan") {
|
|
259
|
+
this.previewDelta = delta;
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (this.tool === "resize-selection") {
|
|
263
|
+
this.resizePreviewEntries = this.createResizePreviewEntries(delta);
|
|
264
|
+
this.resizePreview = (_a = this.resizePreviewEntries[0]) !== null && _a !== void 0 ? _a : null;
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (this.tool === "marquee") {
|
|
268
|
+
this.marquee = rectFromPoints(this.startPoint, this.latestPoint);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
createResizePreviewEntries(delta) {
|
|
272
|
+
if (this.resizeSelectionIds.length) {
|
|
273
|
+
return this.createSelectionResizePreviewEntries(delta);
|
|
274
|
+
}
|
|
275
|
+
const entry = this.createResizePreview(delta);
|
|
276
|
+
return entry ? Object.freeze([entry]) : Object.freeze([]);
|
|
277
|
+
}
|
|
278
|
+
createSelectionResizePreviewEntries(delta) {
|
|
279
|
+
var _a;
|
|
280
|
+
if (!this.resizeHandle || !this.resizeStartBounds)
|
|
281
|
+
return Object.freeze([]);
|
|
282
|
+
const nextBounds = resizeBoundsByHandle(this.resizeStartBounds, this.resizeHandle, delta, 8);
|
|
283
|
+
const scaleX = this.resizeStartBounds.width ? nextBounds.width / this.resizeStartBounds.width : 1;
|
|
284
|
+
const scaleY = this.resizeStartBounds.height ? nextBounds.height / this.resizeStartBounds.height : 1;
|
|
285
|
+
const entries = [];
|
|
286
|
+
for (const id of this.resizeSelectionIds) {
|
|
287
|
+
const geometry = this.engine.getGeometrySnapshot(id);
|
|
288
|
+
const bounds = (_a = geometry === null || geometry === void 0 ? void 0 : geometry.unrotatedBounds) !== null && _a !== void 0 ? _a : geometry === null || geometry === void 0 ? void 0 : geometry.bounds;
|
|
289
|
+
if (!bounds)
|
|
290
|
+
continue;
|
|
291
|
+
entries.push({
|
|
292
|
+
id,
|
|
293
|
+
x: nextBounds.x + (bounds.x - this.resizeStartBounds.x) * scaleX,
|
|
294
|
+
y: nextBounds.y + (bounds.y - this.resizeStartBounds.y) * scaleY,
|
|
295
|
+
width: Math.max(8, bounds.width * scaleX),
|
|
296
|
+
height: Math.max(8, bounds.height * scaleY),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
return Object.freeze(entries);
|
|
300
|
+
}
|
|
301
|
+
createResizePreview(delta) {
|
|
302
|
+
if (!this.resizeOwnerId || !this.resizeHandle || !this.resizeStartBounds)
|
|
303
|
+
return null;
|
|
304
|
+
const bounds = this.resizeStartBounds;
|
|
305
|
+
const localDelta = rotateDelta(delta, -this.resizeStartRotation);
|
|
306
|
+
const minSize = 8;
|
|
307
|
+
let x = bounds.x;
|
|
308
|
+
let y = bounds.y;
|
|
309
|
+
let width = bounds.width;
|
|
310
|
+
let height = bounds.height;
|
|
311
|
+
if (this.resizeHandle.includes("e"))
|
|
312
|
+
width = Math.max(minSize, bounds.width + localDelta.x);
|
|
313
|
+
if (this.resizeHandle.includes("s"))
|
|
314
|
+
height = Math.max(minSize, bounds.height + localDelta.y);
|
|
315
|
+
if (this.resizeHandle.includes("w")) {
|
|
316
|
+
width = Math.max(minSize, bounds.width - localDelta.x);
|
|
317
|
+
x = bounds.x + (bounds.width - width);
|
|
318
|
+
}
|
|
319
|
+
if (this.resizeHandle.includes("n")) {
|
|
320
|
+
height = Math.max(minSize, bounds.height - localDelta.y);
|
|
321
|
+
y = bounds.y + (bounds.height - height);
|
|
322
|
+
}
|
|
323
|
+
const entry = { id: this.resizeOwnerId, width, height };
|
|
324
|
+
if (this.resizeStartRotation) {
|
|
325
|
+
const anchored = anchorRotatedResize(bounds, this.resizeHandle, width, height, this.resizeStartRotation);
|
|
326
|
+
entry.x = anchored.x;
|
|
327
|
+
entry.y = anchored.y;
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
if (x !== bounds.x)
|
|
331
|
+
entry.x = x;
|
|
332
|
+
if (y !== bounds.y)
|
|
333
|
+
entry.y = y;
|
|
334
|
+
}
|
|
335
|
+
return entry;
|
|
336
|
+
}
|
|
337
|
+
getMarqueeSelectionIds(rect) {
|
|
338
|
+
const visible = new Set(this.engine.queryVisible(rect));
|
|
339
|
+
const scene = this.engine.getScene();
|
|
340
|
+
return [
|
|
341
|
+
...scene.order.nodeIds,
|
|
342
|
+
...scene.order.shapeIds,
|
|
343
|
+
...scene.order.textIds,
|
|
344
|
+
...scene.order.edgeIds,
|
|
345
|
+
].filter((id) => {
|
|
346
|
+
if (!visible.has(id))
|
|
347
|
+
return false;
|
|
348
|
+
if (this.marqueeMode === "intersect")
|
|
349
|
+
return true;
|
|
350
|
+
const geometry = createEntityGeometry(id, scene.entities);
|
|
351
|
+
return geometry ? rectContainsRect(rect, geometry.bounds) : false;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
clearGesture() {
|
|
355
|
+
this.activePointer = null;
|
|
356
|
+
this.startPoint = null;
|
|
357
|
+
this.latestPoint = null;
|
|
358
|
+
this.previewDelta = null;
|
|
359
|
+
this.marquee = null;
|
|
360
|
+
this.resizeOwnerId = null;
|
|
361
|
+
this.resizeSelectionIds = [];
|
|
362
|
+
this.resizeHandle = null;
|
|
363
|
+
this.resizeStartBounds = null;
|
|
364
|
+
this.resizeStartRotation = 0;
|
|
365
|
+
this.resizePreview = null;
|
|
366
|
+
this.resizePreviewEntries = Object.freeze([]);
|
|
367
|
+
this.framePending = false;
|
|
368
|
+
this.pendingMoveCount = 0;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
export function createDiagramInteractionController(engine, options = {}) {
|
|
372
|
+
return new DiagramInteractionController(engine, options);
|
|
373
|
+
}
|
|
374
|
+
function defaultScheduleFrame(callback) {
|
|
375
|
+
if (typeof globalThis.requestAnimationFrame === "function") {
|
|
376
|
+
globalThis.requestAnimationFrame(() => callback());
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
callback();
|
|
380
|
+
}
|
|
381
|
+
function rotateDelta(delta, rotation) {
|
|
382
|
+
const angle = rotation * (Math.PI / 180);
|
|
383
|
+
const cos = Math.cos(angle);
|
|
384
|
+
const sin = Math.sin(angle);
|
|
385
|
+
return { x: delta.x * cos - delta.y * sin, y: delta.x * sin + delta.y * cos };
|
|
386
|
+
}
|
|
387
|
+
function resizeBoundsByHandle(bounds, handle, delta, minSize) {
|
|
388
|
+
let x = bounds.x;
|
|
389
|
+
let y = bounds.y;
|
|
390
|
+
let width = bounds.width;
|
|
391
|
+
let height = bounds.height;
|
|
392
|
+
if (handle.includes("e"))
|
|
393
|
+
width = Math.max(minSize, bounds.width + delta.x);
|
|
394
|
+
if (handle.includes("s"))
|
|
395
|
+
height = Math.max(minSize, bounds.height + delta.y);
|
|
396
|
+
if (handle.includes("w")) {
|
|
397
|
+
width = Math.max(minSize, bounds.width - delta.x);
|
|
398
|
+
x = bounds.x + (bounds.width - width);
|
|
399
|
+
}
|
|
400
|
+
if (handle.includes("n")) {
|
|
401
|
+
height = Math.max(minSize, bounds.height - delta.y);
|
|
402
|
+
y = bounds.y + (bounds.height - height);
|
|
403
|
+
}
|
|
404
|
+
return { x, y, width, height };
|
|
405
|
+
}
|
|
406
|
+
function unionRects(rects) {
|
|
407
|
+
if (!rects.length)
|
|
408
|
+
return null;
|
|
409
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
410
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
411
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
412
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
413
|
+
for (const rect of rects) {
|
|
414
|
+
minX = Math.min(minX, rect.x);
|
|
415
|
+
minY = Math.min(minY, rect.y);
|
|
416
|
+
maxX = Math.max(maxX, rect.x + rect.width);
|
|
417
|
+
maxY = Math.max(maxY, rect.y + rect.height);
|
|
418
|
+
}
|
|
419
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
420
|
+
}
|
|
421
|
+
function anchorRotatedResize(bounds, handle, width, height, rotation) {
|
|
422
|
+
const oldAnchor = anchorPointForHandle(bounds, handle);
|
|
423
|
+
const newAnchorOffset = oppositeCornerOffset(handle, width, height);
|
|
424
|
+
const oldAnchorWorld = rotatePoint(oldAnchor, rectCenter(bounds), rotation);
|
|
425
|
+
const newCenterOffset = { x: width / 2, y: height / 2 };
|
|
426
|
+
const rotatedAnchorOffset = rotateDelta({ x: newAnchorOffset.x - newCenterOffset.x, y: newAnchorOffset.y - newCenterOffset.y }, rotation);
|
|
427
|
+
return {
|
|
428
|
+
x: oldAnchorWorld.x - newCenterOffset.x - rotatedAnchorOffset.x,
|
|
429
|
+
y: oldAnchorWorld.y - newCenterOffset.y - rotatedAnchorOffset.y,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function anchorPointForHandle(bounds, handle) {
|
|
433
|
+
const offset = oppositeCornerOffset(handle, bounds.width, bounds.height);
|
|
434
|
+
return { x: bounds.x + offset.x, y: bounds.y + offset.y };
|
|
435
|
+
}
|
|
436
|
+
function oppositeCornerOffset(handle, width, height) {
|
|
437
|
+
return {
|
|
438
|
+
x: handle.includes("w") ? width : 0,
|
|
439
|
+
y: handle.includes("n") ? height : 0,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function rectCenter(rect) {
|
|
443
|
+
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
444
|
+
}
|
|
445
|
+
function rotatePoint(point, center, rotation) {
|
|
446
|
+
const delta = rotateDelta({ x: point.x - center.x, y: point.y - center.y }, rotation);
|
|
447
|
+
return { x: center.x + delta.x, y: center.y + delta.y };
|
|
448
|
+
}
|
|
449
|
+
function rectFromPoints(a, b) {
|
|
450
|
+
return {
|
|
451
|
+
x: Math.min(a.x, b.x),
|
|
452
|
+
y: Math.min(a.y, b.y),
|
|
453
|
+
width: Math.abs(a.x - b.x),
|
|
454
|
+
height: Math.abs(a.y - b.y),
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
function rectContainsRect(outer, inner) {
|
|
458
|
+
return inner.x >= outer.x
|
|
459
|
+
&& inner.y >= outer.y
|
|
460
|
+
&& inner.x + inner.width <= outer.x + outer.width
|
|
461
|
+
&& inner.y + inner.height <= outer.y + outer.height;
|
|
462
|
+
}
|