@zag-js/rect-utils 2.0.0-next.3 → 2.0.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/align.d.mts +2 -5
  2. package/dist/align.d.ts +2 -5
  3. package/dist/align.js +27 -44
  4. package/dist/align.mjs +23 -19
  5. package/dist/angle.d.mts +2 -5
  6. package/dist/angle.d.ts +2 -5
  7. package/dist/angle.js +8 -32
  8. package/dist/angle.mjs +4 -7
  9. package/dist/clamp.d.mts +8 -11
  10. package/dist/clamp.d.ts +8 -11
  11. package/dist/clamp.js +23 -46
  12. package/dist/clamp.mjs +18 -20
  13. package/dist/closest.d.mts +4 -7
  14. package/dist/closest.d.ts +4 -7
  15. package/dist/closest.js +35 -62
  16. package/dist/closest.mjs +29 -35
  17. package/dist/compass.d.mts +1 -3
  18. package/dist/compass.d.ts +1 -3
  19. package/dist/compass.js +4 -18
  20. package/dist/compass.mjs +1 -0
  21. package/dist/constrain.d.mts +2 -5
  22. package/dist/constrain.d.ts +2 -5
  23. package/dist/constrain.js +12 -37
  24. package/dist/constrain.mjs +8 -12
  25. package/dist/contains.d.mts +4 -7
  26. package/dist/contains.d.ts +4 -7
  27. package/dist/contains.js +11 -36
  28. package/dist/contains.mjs +5 -9
  29. package/dist/distance.d.mts +7 -10
  30. package/dist/distance.d.ts +7 -10
  31. package/dist/distance.js +55 -60
  32. package/dist/distance.mjs +48 -32
  33. package/dist/equality.d.mts +4 -7
  34. package/dist/equality.d.ts +4 -7
  35. package/dist/equality.js +13 -38
  36. package/dist/equality.mjs +7 -11
  37. package/dist/from-element.d.mts +12 -15
  38. package/dist/from-element.d.ts +12 -15
  39. package/dist/from-element.js +39 -58
  40. package/dist/from-element.mjs +35 -33
  41. package/dist/from-points.d.mts +2 -5
  42. package/dist/from-points.d.ts +2 -5
  43. package/dist/from-points.js +18 -36
  44. package/dist/from-points.mjs +14 -11
  45. package/dist/from-range.d.mts +2 -5
  46. package/dist/from-range.d.ts +2 -5
  47. package/dist/from-range.js +25 -46
  48. package/dist/from-range.mjs +19 -19
  49. package/dist/from-rotation.d.mts +4 -7
  50. package/dist/from-rotation.d.ts +4 -7
  51. package/dist/from-rotation.js +31 -56
  52. package/dist/from-rotation.mjs +25 -29
  53. package/dist/from-window.d.mts +13 -16
  54. package/dist/from-window.d.ts +13 -16
  55. package/dist/from-window.js +29 -44
  56. package/dist/from-window.mjs +24 -18
  57. package/dist/index.d.mts +21 -21
  58. package/dist/index.d.ts +21 -21
  59. package/dist/index.js +91 -61
  60. package/dist/index.mjs +20 -21
  61. package/dist/interact.d.mts +34 -37
  62. package/dist/interact.d.ts +34 -37
  63. package/dist/interact.js +414 -350
  64. package/dist/interact.mjs +396 -311
  65. package/dist/intersection.d.mts +4 -7
  66. package/dist/intersection.d.ts +4 -7
  67. package/dist/intersection.js +34 -45
  68. package/dist/intersection.mjs +28 -18
  69. package/dist/operations.d.mts +6 -9
  70. package/dist/operations.d.ts +6 -9
  71. package/dist/operations.js +38 -57
  72. package/dist/operations.mjs +30 -28
  73. package/dist/polygon.d.mts +6 -9
  74. package/dist/polygon.d.ts +6 -9
  75. package/dist/polygon.js +73 -82
  76. package/dist/polygon.mjs +67 -55
  77. package/dist/rect.d.mts +55 -58
  78. package/dist/rect.d.ts +55 -58
  79. package/dist/rect.js +88 -117
  80. package/dist/rect.mjs +70 -78
  81. package/dist/types.d.mts +42 -46
  82. package/dist/types.d.ts +42 -46
  83. package/dist/types.js +4 -18
  84. package/dist/types.mjs +1 -0
  85. package/dist/union.d.mts +2 -5
  86. package/dist/union.d.ts +2 -5
  87. package/dist/union.js +16 -39
  88. package/dist/union.mjs +12 -14
  89. package/package.json +2 -2
package/dist/interact.js CHANGED
@@ -1,377 +1,441 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/interact.ts
21
- var interact_exports = {};
22
- __export(interact_exports, {
23
- applyMove: () => applyMove,
24
- applyResize: () => applyResize,
25
- centerInRect: () => centerInRect,
26
- centerOnPoint: () => centerOnPoint,
27
- clampPointInRange: () => clampPointInRange,
28
- getArrowKeyDelta: () => getArrowKeyDelta,
29
- getDirectionDelta: () => getDirectionDelta,
30
- getKeyboardResizeDelta: () => getKeyboardResizeDelta,
31
- isBottomHandle: () => isBottomHandle,
32
- isCornerHandle: () => isCornerHandle,
33
- isHorizontalHandle: () => isHorizontalHandle,
34
- isLeftHandle: () => isLeftHandle,
35
- isRightHandle: () => isRightHandle,
36
- isTopHandle: () => isTopHandle,
37
- isVerticalHandle: () => isVerticalHandle
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
38
4
  });
39
- module.exports = __toCommonJS(interact_exports);
40
- var isLeftHandle = (h) => h === "w" || h === "nw" || h === "sw";
41
- var isRightHandle = (h) => h === "e" || h === "ne" || h === "se";
42
- var isTopHandle = (h) => h === "n" || h === "nw" || h === "ne";
43
- var isBottomHandle = (h) => h === "s" || h === "sw" || h === "se";
44
- var isCornerHandle = (h) => (isLeftHandle(h) || isRightHandle(h)) && (isTopHandle(h) || isBottomHandle(h));
45
- var isHorizontalHandle = (h) => (isLeftHandle(h) || isRightHandle(h)) && !isTopHandle(h) && !isBottomHandle(h);
46
- var isVerticalHandle = (h) => (isTopHandle(h) || isBottomHandle(h)) && !isLeftHandle(h) && !isRightHandle(h);
47
- var { min, max, abs, round } = Math;
48
- var clamp = (v, lo, hi) => min(max(v, lo), hi);
5
+ // src/interact.ts
6
+ const isLeftHandle = (h) => h === "w" || h === "nw" || h === "sw";
7
+ const isRightHandle = (h) => h === "e" || h === "ne" || h === "se";
8
+ const isTopHandle = (h) => h === "n" || h === "nw" || h === "ne";
9
+ const isBottomHandle = (h) => h === "s" || h === "sw" || h === "se";
10
+ const isCornerHandle = (h) => (isLeftHandle(h) || isRightHandle(h)) && (isTopHandle(h) || isBottomHandle(h));
11
+ const isHorizontalHandle = (h) => (isLeftHandle(h) || isRightHandle(h)) && !isTopHandle(h) && !isBottomHandle(h);
12
+ const isVerticalHandle = (h) => (isTopHandle(h) || isBottomHandle(h)) && !isLeftHandle(h) && !isRightHandle(h);
13
+ const { min, max, abs, round } = Math;
14
+ const clamp = (v, lo, hi) => min(max(v, lo), hi);
49
15
  function resolveLimits(opts) {
50
- const minW = opts.minSize?.width ?? 0;
51
- const minH = opts.minSize?.height ?? 0;
52
- let maxW = opts.maxSize?.width ?? Infinity;
53
- let maxH = opts.maxSize?.height ?? Infinity;
54
- if (opts.boundary) {
55
- if (Number.isFinite(maxW)) maxW = min(maxW, opts.boundary.width);
56
- else maxW = opts.boundary.width;
57
- if (Number.isFinite(maxH)) maxH = min(maxH, opts.boundary.height);
58
- else maxH = opts.boundary.height;
59
- }
60
- return { minW: max(0, minW), minH: max(0, minH), maxW: max(minW, maxW), maxH: max(minH, maxH) };
16
+ const minW = opts.minSize?.width ?? 0;
17
+ const minH = opts.minSize?.height ?? 0;
18
+ let maxW = opts.maxSize?.width ?? 1e400;
19
+ let maxH = opts.maxSize?.height ?? 1e400;
20
+ if (opts.boundary) {
21
+ if (Number.isFinite(maxW)) maxW = min(maxW, opts.boundary.width);
22
+ else maxW = opts.boundary.width;
23
+ if (Number.isFinite(maxH)) maxH = min(maxH, opts.boundary.height);
24
+ else maxH = opts.boundary.height;
25
+ }
26
+ return {
27
+ minW: max(0, minW),
28
+ minH: max(0, minH),
29
+ maxW: max(minW, maxW),
30
+ maxH: max(minH, maxH)
31
+ };
61
32
  }
62
33
  function resolveLimitsWithAspect(limits, boundary, ar) {
63
- let { minW, minH, maxW, maxH } = limits;
64
- const bw = boundary?.width ?? Infinity;
65
- const bh = boundary?.height ?? Infinity;
66
- const minWa = max(minW, minH * ar);
67
- const minHa = minWa / ar;
68
- minW = min(minWa, bw);
69
- minH = min(minHa, bh);
70
- let cmw = min(maxW, maxH * ar, bw);
71
- let cmh = cmw / ar;
72
- if (cmh > maxH || cmh > bh) {
73
- cmh = min(maxH, bh);
74
- cmw = cmh * ar;
75
- }
76
- maxW = max(minW, min(cmw, bw));
77
- maxH = max(minH, min(cmh, bh));
78
- return { minW, minH, maxW, maxH };
34
+ let { minW, minH, maxW, maxH } = limits;
35
+ const bw = boundary?.width ?? 1e400;
36
+ const bh = boundary?.height ?? 1e400;
37
+ const minWa = max(minW, minH * ar);
38
+ const minHa = minWa / ar;
39
+ minW = min(minWa, bw);
40
+ minH = min(minHa, bh);
41
+ let cmw = min(maxW, maxH * ar, bw);
42
+ let cmh = cmw / ar;
43
+ if (cmh > maxH || cmh > bh) {
44
+ cmh = min(maxH, bh);
45
+ cmw = cmh * ar;
46
+ }
47
+ maxW = max(minW, min(cmw, bw));
48
+ maxH = max(minH, min(cmh, bh));
49
+ return {
50
+ minW,
51
+ minH,
52
+ maxW,
53
+ maxH
54
+ };
79
55
  }
80
56
  function applyDelta(edges, delta, handle, limits, boundary) {
81
- let { left, top, right, bottom } = edges;
82
- const { minW, minH, maxW, maxH } = limits;
83
- if (isLeftHandle(handle)) {
84
- const lo = boundary ? max(boundary.x, right - maxW) : right - maxW;
85
- const hi = right - minW;
86
- left = clamp(left + delta.x, lo, hi);
87
- }
88
- if (isRightHandle(handle)) {
89
- const lo = left + minW;
90
- const hi = boundary ? min(boundary.x + boundary.width, left + maxW) : left + maxW;
91
- right = clamp(right + delta.x, lo, hi);
92
- }
93
- if (isTopHandle(handle)) {
94
- const lo = boundary ? max(boundary.y, bottom - maxH) : bottom - maxH;
95
- const hi = bottom - minH;
96
- top = clamp(top + delta.y, lo, hi);
97
- }
98
- if (isBottomHandle(handle)) {
99
- const lo = top + minH;
100
- const hi = boundary ? min(boundary.y + boundary.height, top + maxH) : top + maxH;
101
- bottom = clamp(bottom + delta.y, lo, hi);
102
- }
103
- return { left, top, right, bottom };
57
+ let { left, top, right, bottom } = edges;
58
+ const { minW, minH, maxW, maxH } = limits;
59
+ if (isLeftHandle(handle)) {
60
+ const lo = boundary ? max(boundary.x, right - maxW) : right - maxW;
61
+ const hi = right - minW;
62
+ left = clamp(left + delta.x, lo, hi);
63
+ }
64
+ if (isRightHandle(handle)) {
65
+ const lo = left + minW;
66
+ const hi = boundary ? min(boundary.x + boundary.width, left + maxW) : left + maxW;
67
+ right = clamp(right + delta.x, lo, hi);
68
+ }
69
+ if (isTopHandle(handle)) {
70
+ const lo = boundary ? max(boundary.y, bottom - maxH) : bottom - maxH;
71
+ const hi = bottom - minH;
72
+ top = clamp(top + delta.y, lo, hi);
73
+ }
74
+ if (isBottomHandle(handle)) {
75
+ const lo = top + minH;
76
+ const hi = boundary ? min(boundary.y + boundary.height, top + maxH) : top + maxH;
77
+ bottom = clamp(bottom + delta.y, lo, hi);
78
+ }
79
+ return {
80
+ left,
81
+ top,
82
+ right,
83
+ bottom
84
+ };
104
85
  }
105
86
  function clampAspectSize(wHint, hHint, ar, lim, bw, bh) {
106
- const fromHeight = (h) => {
107
- const w = clamp(h * ar, lim.minW, min(lim.maxW, bw));
108
- return { width: w, height: w / ar };
109
- };
110
- const tryWidth = (value) => {
111
- let w = clamp(value, lim.minW, min(lim.maxW, bw));
112
- let h = w / ar;
113
- if (h < lim.minH) ({ width: w, height: h } = fromHeight(lim.minH));
114
- if (h > min(lim.maxH, bh)) ({ width: w, height: h } = fromHeight(min(lim.maxH, bh)));
115
- if (h > bh) {
116
- ;
117
- ({ width: w, height: h } = fromHeight(bh));
118
- if (h < lim.minH) ({ width: w, height: h } = fromHeight(lim.minH));
119
- }
120
- return { width: w, height: h };
121
- };
122
- const tryHeight = (value) => {
123
- let h = clamp(value, lim.minH, min(lim.maxH, bh));
124
- let w = clamp(h * ar, lim.minW, min(lim.maxW, bw));
125
- let ah = w / ar;
126
- if (ah < lim.minH) ({ width: w, height: ah } = fromHeight(lim.minH));
127
- if (ah > min(lim.maxH, bh)) ({ width: w, height: ah } = fromHeight(min(lim.maxH, bh)));
128
- if (w > bw) {
129
- w = bw;
130
- ah = w / ar;
131
- if (ah > min(lim.maxH, bh)) ({ width: w, height: ah } = fromHeight(min(lim.maxH, bh)));
132
- if (ah < lim.minH) ({ width: w, height: ah } = fromHeight(lim.minH));
133
- }
134
- return { width: w, height: ah };
135
- };
136
- const byW = tryWidth(wHint);
137
- const byH = tryHeight(hHint);
138
- const dw = abs(byW.width - wHint) + abs(byW.height - hHint);
139
- const dh = abs(byH.width - wHint) + abs(byH.height - hHint);
140
- return dh < dw ? byH : byW;
87
+ const fromHeight = (h) => {
88
+ const w = clamp(h * ar, lim.minW, min(lim.maxW, bw));
89
+ return {
90
+ width: w,
91
+ height: w / ar
92
+ };
93
+ };
94
+ const tryWidth = (value) => {
95
+ let w = clamp(value, lim.minW, min(lim.maxW, bw));
96
+ let h = w / ar;
97
+ if (h < lim.minH) ({width: w, height: h} = fromHeight(lim.minH));
98
+ if (h > min(lim.maxH, bh)) ({width: w, height: h} = fromHeight(min(lim.maxH, bh)));
99
+ if (h > bh) {
100
+ ({width: w, height: h} = fromHeight(bh));
101
+ if (h < lim.minH) ({width: w, height: h} = fromHeight(lim.minH));
102
+ }
103
+ return {
104
+ width: w,
105
+ height: h
106
+ };
107
+ };
108
+ const tryHeight = (value) => {
109
+ let h = clamp(value, lim.minH, min(lim.maxH, bh));
110
+ let w = clamp(h * ar, lim.minW, min(lim.maxW, bw));
111
+ let ah = w / ar;
112
+ if (ah < lim.minH) ({width: w, height: ah} = fromHeight(lim.minH));
113
+ if (ah > min(lim.maxH, bh)) ({width: w, height: ah} = fromHeight(min(lim.maxH, bh)));
114
+ if (w > bw) {
115
+ w = bw;
116
+ ah = w / ar;
117
+ if (ah > min(lim.maxH, bh)) ({width: w, height: ah} = fromHeight(min(lim.maxH, bh)));
118
+ if (ah < lim.minH) ({width: w, height: ah} = fromHeight(lim.minH));
119
+ }
120
+ return {
121
+ width: w,
122
+ height: ah
123
+ };
124
+ };
125
+ const byW = tryWidth(wHint);
126
+ const byH = tryHeight(hHint);
127
+ const dw = abs(byW.width - wHint) + abs(byW.height - hHint);
128
+ return abs(byH.width - wHint) + abs(byH.height - hHint) < dw ? byH : byW;
141
129
  }
142
130
  function enforceAspect(edges, handle, ar, lim, boundary) {
143
- const { left, top, right, bottom } = edges;
144
- const bw = boundary?.width ?? Infinity;
145
- const bh = boundary?.height ?? Infinity;
146
- const bx = boundary?.x ?? 0;
147
- const by = boundary?.y ?? 0;
148
- if (isCornerHandle(handle)) {
149
- let w = right - left;
150
- let h = w / ar;
151
- if (h > bottom - top || boundary && (top + h > by + bh || left + w > bx + bw)) {
152
- h = bottom - top;
153
- w = h * ar;
154
- }
155
- const s = clampAspectSize(w, h, ar, lim, bw, bh);
156
- w = s.width;
157
- h = s.height;
158
- if (isRightHandle(handle) && isBottomHandle(handle)) {
159
- return { left, top, right: left + w, bottom: top + h };
160
- }
161
- if (isRightHandle(handle) && isTopHandle(handle)) {
162
- return { left, top: bottom - h, right: left + w, bottom };
163
- }
164
- if (isLeftHandle(handle) && isBottomHandle(handle)) {
165
- return { left: right - w, top, right, bottom: top + h };
166
- }
167
- return { left: right - w, top: bottom - h, right, bottom };
168
- }
169
- if (isHorizontalHandle(handle)) {
170
- const centerY = (top + bottom) / 2;
171
- let w = right - left;
172
- let h = w / ar;
173
- const s = clampAspectSize(w, h, ar, lim, bw, bh);
174
- w = s.width;
175
- h = s.height;
176
- const halfH = h / 2;
177
- let newTop = centerY - halfH;
178
- let newBottom = centerY + halfH;
179
- if (boundary) {
180
- if (newTop < by) {
181
- newTop = by;
182
- newBottom = newTop + h;
183
- }
184
- if (newBottom > by + bh) {
185
- newBottom = by + bh;
186
- newTop = newBottom - h;
187
- }
188
- }
189
- return {
190
- left: isRightHandle(handle) ? left : right - w,
191
- top: newTop,
192
- right: isRightHandle(handle) ? left + w : right,
193
- bottom: newBottom
194
- };
195
- }
196
- if (isVerticalHandle(handle)) {
197
- const centerX = (left + right) / 2;
198
- let h = bottom - top;
199
- let w = h * ar;
200
- const s = clampAspectSize(w, h, ar, lim, bw, bh);
201
- w = s.width;
202
- h = s.height;
203
- const halfW = w / 2;
204
- let newLeft = centerX - halfW;
205
- let newRight = centerX + halfW;
206
- if (boundary) {
207
- if (newLeft < bx) {
208
- newLeft = bx;
209
- newRight = newLeft + w;
210
- }
211
- if (newRight > bx + bw) {
212
- newRight = bx + bw;
213
- newLeft = newRight - w;
214
- }
215
- }
216
- return {
217
- left: newLeft,
218
- top: isBottomHandle(handle) ? top : bottom - h,
219
- right: newRight,
220
- bottom: isBottomHandle(handle) ? top + h : bottom
221
- };
222
- }
223
- return edges;
131
+ const { left, top, right, bottom } = edges;
132
+ const bw = boundary?.width ?? 1e400;
133
+ const bh = boundary?.height ?? 1e400;
134
+ const bx = boundary?.x ?? 0;
135
+ const by = boundary?.y ?? 0;
136
+ if (isCornerHandle(handle)) {
137
+ let w = right - left;
138
+ let h = w / ar;
139
+ if (h > bottom - top || boundary && (top + h > by + bh || left + w > bx + bw)) {
140
+ h = bottom - top;
141
+ w = h * ar;
142
+ }
143
+ const s = clampAspectSize(w, h, ar, lim, bw, bh);
144
+ w = s.width;
145
+ h = s.height;
146
+ if (isRightHandle(handle) && isBottomHandle(handle)) return {
147
+ left,
148
+ top,
149
+ right: left + w,
150
+ bottom: top + h
151
+ };
152
+ if (isRightHandle(handle) && isTopHandle(handle)) return {
153
+ left,
154
+ top: bottom - h,
155
+ right: left + w,
156
+ bottom
157
+ };
158
+ if (isLeftHandle(handle) && isBottomHandle(handle)) return {
159
+ left: right - w,
160
+ top,
161
+ right,
162
+ bottom: top + h
163
+ };
164
+ return {
165
+ left: right - w,
166
+ top: bottom - h,
167
+ right,
168
+ bottom
169
+ };
170
+ }
171
+ if (isHorizontalHandle(handle)) {
172
+ const centerY = (top + bottom) / 2;
173
+ let w = right - left;
174
+ let h = w / ar;
175
+ const s = clampAspectSize(w, h, ar, lim, bw, bh);
176
+ w = s.width;
177
+ h = s.height;
178
+ const halfH = h / 2;
179
+ let newTop = centerY - halfH;
180
+ let newBottom = centerY + halfH;
181
+ if (boundary) {
182
+ if (newTop < by) {
183
+ newTop = by;
184
+ newBottom = newTop + h;
185
+ }
186
+ if (newBottom > by + bh) {
187
+ newBottom = by + bh;
188
+ newTop = newBottom - h;
189
+ }
190
+ }
191
+ return {
192
+ left: isRightHandle(handle) ? left : right - w,
193
+ top: newTop,
194
+ right: isRightHandle(handle) ? left + w : right,
195
+ bottom: newBottom
196
+ };
197
+ }
198
+ if (isVerticalHandle(handle)) {
199
+ const centerX = (left + right) / 2;
200
+ let h = bottom - top;
201
+ let w = h * ar;
202
+ const s = clampAspectSize(w, h, ar, lim, bw, bh);
203
+ w = s.width;
204
+ h = s.height;
205
+ const halfW = w / 2;
206
+ let newLeft = centerX - halfW;
207
+ let newRight = centerX + halfW;
208
+ if (boundary) {
209
+ if (newLeft < bx) {
210
+ newLeft = bx;
211
+ newRight = newLeft + w;
212
+ }
213
+ if (newRight > bx + bw) {
214
+ newRight = bx + bw;
215
+ newLeft = newRight - w;
216
+ }
217
+ }
218
+ return {
219
+ left: newLeft,
220
+ top: isBottomHandle(handle) ? top : bottom - h,
221
+ right: newRight,
222
+ bottom: isBottomHandle(handle) ? top + h : bottom
223
+ };
224
+ }
225
+ return edges;
224
226
  }
227
+ /**
228
+ * Compute the position that centers a `size` within a boundary rect.
229
+ */
225
230
  function centerInRect(size, boundary) {
226
- return {
227
- x: boundary.x + max(0, (boundary.width - size.width) / 2),
228
- y: boundary.y + max(0, (boundary.height - size.height) / 2)
229
- };
231
+ return {
232
+ x: boundary.x + max(0, (boundary.width - size.width) / 2),
233
+ y: boundary.y + max(0, (boundary.height - size.height) / 2)
234
+ };
230
235
  }
236
+ /**
237
+ * Center a rect (given by size) on a point, then clamp within a boundary.
238
+ */
231
239
  function centerOnPoint(size, center, boundary) {
232
- const pos = {
233
- x: center.x - size.width / 2,
234
- y: center.y - size.height / 2
235
- };
236
- return {
237
- x: clamp(pos.x, boundary.x, boundary.x + boundary.width - size.width),
238
- y: clamp(pos.y, boundary.y, boundary.y + boundary.height - size.height)
239
- };
240
+ const pos = {
241
+ x: center.x - size.width / 2,
242
+ y: center.y - size.height / 2
243
+ };
244
+ return {
245
+ x: clamp(pos.x, boundary.x, boundary.x + boundary.width - size.width),
246
+ y: clamp(pos.y, boundary.y, boundary.y + boundary.height - size.height)
247
+ };
240
248
  }
249
+ /**
250
+ * Clamp a point's x/y independently between a min and max point.
251
+ * Useful for constraining offsets, positions, or pan values.
252
+ */
241
253
  function clampPointInRange(point, minPoint, maxPoint) {
242
- return {
243
- x: clamp(point.x, minPoint.x, maxPoint.x),
244
- y: clamp(point.y, minPoint.y, maxPoint.y)
245
- };
254
+ return {
255
+ x: clamp(point.x, minPoint.x, maxPoint.x),
256
+ y: clamp(point.y, minPoint.y, maxPoint.y)
257
+ };
246
258
  }
259
+ /**
260
+ * Compute a new rect after moving by a pointer delta.
261
+ * Grid-snaps the delta and clamps within the boundary.
262
+ */
247
263
  function applyMove(rect, delta, options = {}) {
248
- const { boundary, gridSize = 1 } = options;
249
- const dx = round(delta.x / gridSize) * gridSize;
250
- const dy = round(delta.y / gridSize) * gridSize;
251
- let x = rect.x + dx;
252
- let y = rect.y + dy;
253
- if (boundary) {
254
- x = clamp(x, boundary.x, boundary.x + boundary.width - rect.width);
255
- y = clamp(y, boundary.y, boundary.y + boundary.height - rect.height);
256
- }
257
- return { x, y, width: rect.width, height: rect.height };
264
+ const { boundary, gridSize = 1 } = options;
265
+ const dx = round(delta.x / gridSize) * gridSize;
266
+ const dy = round(delta.y / gridSize) * gridSize;
267
+ let x = rect.x + dx;
268
+ let y = rect.y + dy;
269
+ if (boundary) {
270
+ x = clamp(x, boundary.x, boundary.x + boundary.width - rect.width);
271
+ y = clamp(y, boundary.y, boundary.y + boundary.height - rect.height);
272
+ }
273
+ return {
274
+ x,
275
+ y,
276
+ width: rect.width,
277
+ height: rect.height
278
+ };
258
279
  }
280
+ /**
281
+ * Compute a new rect after resizing via a handle drag.
282
+ * Handles size limits, boundary containment, aspect ratio, and scaling origin.
283
+ */
259
284
  function applyResize(rect, delta, handle, options = {}) {
260
- const { boundary, aspectRatio, origin = "extent" } = options;
261
- const baseLimits = resolveLimits(options);
262
- const effectiveDelta = origin === "center" ? { x: delta.x * 2, y: delta.y * 2 } : delta;
263
- let edges = applyDelta(
264
- { left: rect.x, top: rect.y, right: rect.x + rect.width, bottom: rect.y + rect.height },
265
- effectiveDelta,
266
- handle,
267
- baseLimits,
268
- boundary
269
- );
270
- if (aspectRatio != null && aspectRatio > 0) {
271
- const arLimits = resolveLimitsWithAspect(baseLimits, boundary, aspectRatio);
272
- edges = enforceAspect(edges, handle, aspectRatio, arLimits, boundary);
273
- }
274
- let { left, top, right, bottom } = edges;
275
- if (origin === "center") {
276
- const cx = rect.x + rect.width / 2;
277
- const cy = rect.y + rect.height / 2;
278
- const w = right - left;
279
- const h = bottom - top;
280
- left = cx - w / 2;
281
- top = cy - h / 2;
282
- right = cx + w / 2;
283
- bottom = cy + h / 2;
284
- if (boundary) {
285
- if (left < boundary.x) {
286
- right += boundary.x - left;
287
- left = boundary.x;
288
- }
289
- if (right > boundary.x + boundary.width) {
290
- left -= right - (boundary.x + boundary.width);
291
- right = boundary.x + boundary.width;
292
- }
293
- if (top < boundary.y) {
294
- bottom += boundary.y - top;
295
- top = boundary.y;
296
- }
297
- if (bottom > boundary.y + boundary.height) {
298
- top -= bottom - (boundary.y + boundary.height);
299
- bottom = boundary.y + boundary.height;
300
- }
301
- }
302
- }
303
- if (boundary) {
304
- const maxLeft = max(boundary.x, boundary.x + boundary.width - baseLimits.minW);
305
- const maxTop = max(boundary.y, boundary.y + boundary.height - baseLimits.minH);
306
- left = clamp(left, boundary.x, maxLeft);
307
- top = clamp(top, boundary.y, maxTop);
308
- right = clamp(right, left + baseLimits.minW, min(boundary.x + boundary.width, left + baseLimits.maxW));
309
- bottom = clamp(bottom, top + baseLimits.minH, min(boundary.y + boundary.height, top + baseLimits.maxH));
310
- }
311
- return { x: left, y: top, width: right - left, height: bottom - top };
285
+ const { boundary, aspectRatio, origin = "extent" } = options;
286
+ const baseLimits = resolveLimits(options);
287
+ const effectiveDelta = origin === "center" ? {
288
+ x: delta.x * 2,
289
+ y: delta.y * 2
290
+ } : delta;
291
+ let edges = applyDelta({
292
+ left: rect.x,
293
+ top: rect.y,
294
+ right: rect.x + rect.width,
295
+ bottom: rect.y + rect.height
296
+ }, effectiveDelta, handle, baseLimits, boundary);
297
+ if (aspectRatio != null && aspectRatio > 0) {
298
+ const arLimits = resolveLimitsWithAspect(baseLimits, boundary, aspectRatio);
299
+ edges = enforceAspect(edges, handle, aspectRatio, arLimits, boundary);
300
+ }
301
+ let { left, top, right, bottom } = edges;
302
+ if (origin === "center") {
303
+ const cx = rect.x + rect.width / 2;
304
+ const cy = rect.y + rect.height / 2;
305
+ const w = right - left;
306
+ const h = bottom - top;
307
+ left = cx - w / 2;
308
+ top = cy - h / 2;
309
+ right = cx + w / 2;
310
+ bottom = cy + h / 2;
311
+ if (boundary) {
312
+ if (left < boundary.x) {
313
+ right += boundary.x - left;
314
+ left = boundary.x;
315
+ }
316
+ if (right > boundary.x + boundary.width) {
317
+ left -= right - (boundary.x + boundary.width);
318
+ right = boundary.x + boundary.width;
319
+ }
320
+ if (top < boundary.y) {
321
+ bottom += boundary.y - top;
322
+ top = boundary.y;
323
+ }
324
+ if (bottom > boundary.y + boundary.height) {
325
+ top -= bottom - (boundary.y + boundary.height);
326
+ bottom = boundary.y + boundary.height;
327
+ }
328
+ }
329
+ }
330
+ if (boundary) {
331
+ const maxLeft = max(boundary.x, boundary.x + boundary.width - baseLimits.minW);
332
+ const maxTop = max(boundary.y, boundary.y + boundary.height - baseLimits.minH);
333
+ left = clamp(left, boundary.x, maxLeft);
334
+ top = clamp(top, boundary.y, maxTop);
335
+ right = clamp(right, left + baseLimits.minW, min(boundary.x + boundary.width, left + baseLimits.maxW));
336
+ bottom = clamp(bottom, top + baseLimits.minH, min(boundary.y + boundary.height, top + baseLimits.maxH));
337
+ }
338
+ return {
339
+ x: left,
340
+ y: top,
341
+ width: right - left,
342
+ height: bottom - top
343
+ };
312
344
  }
345
+ /**
346
+ * Convert an arrow-key direction and step into a Point delta.
347
+ * Useful for keyboard-driven move/resize.
348
+ */
313
349
  function getDirectionDelta(direction, step) {
314
- switch (direction) {
315
- case "left":
316
- return { x: -step, y: 0 };
317
- case "right":
318
- return { x: step, y: 0 };
319
- case "up":
320
- return { x: 0, y: -step };
321
- case "down":
322
- return { x: 0, y: step };
323
- }
350
+ switch (direction) {
351
+ case "left": return {
352
+ x: -step,
353
+ y: 0
354
+ };
355
+ case "right": return {
356
+ x: step,
357
+ y: 0
358
+ };
359
+ case "up": return {
360
+ x: 0,
361
+ y: -step
362
+ };
363
+ case "down": return {
364
+ x: 0,
365
+ y: step
366
+ };
367
+ }
324
368
  }
369
+ /**
370
+ * Convert an ArrowKey event key and step into a Point delta.
371
+ * Like `getDirectionDelta` but accepts `"ArrowLeft"` / `"ArrowRight"` / etc.
372
+ */
325
373
  function getArrowKeyDelta(key, step) {
326
- switch (key) {
327
- case "ArrowLeft":
328
- return { x: -step, y: 0 };
329
- case "ArrowRight":
330
- return { x: step, y: 0 };
331
- case "ArrowUp":
332
- return { x: 0, y: -step };
333
- case "ArrowDown":
334
- return { x: 0, y: step };
335
- default:
336
- return { x: 0, y: 0 };
337
- }
374
+ switch (key) {
375
+ case "ArrowLeft": return {
376
+ x: -step,
377
+ y: 0
378
+ };
379
+ case "ArrowRight": return {
380
+ x: step,
381
+ y: 0
382
+ };
383
+ case "ArrowUp": return {
384
+ x: 0,
385
+ y: -step
386
+ };
387
+ case "ArrowDown": return {
388
+ x: 0,
389
+ y: step
390
+ };
391
+ default: return {
392
+ x: 0,
393
+ y: 0
394
+ };
395
+ }
338
396
  }
397
+ /**
398
+ * Compute the delta for a keyboard-driven resize given an arrow key and handle.
399
+ *
400
+ * The convention: pressing an arrow key in the handle's "outward" direction
401
+ * expands the rect; pressing inward shrinks it. For corners, both axes move.
402
+ */
339
403
  function getKeyboardResizeDelta(key, handle, step) {
340
- let x = 0;
341
- let y = 0;
342
- if (isLeftHandle(handle)) {
343
- if (key === "ArrowLeft") x = -step;
344
- else if (key === "ArrowRight") x = step;
345
- }
346
- if (isRightHandle(handle)) {
347
- if (key === "ArrowRight") x = step;
348
- else if (key === "ArrowLeft") x = -step;
349
- }
350
- if (isTopHandle(handle)) {
351
- if (key === "ArrowUp") y = -step;
352
- else if (key === "ArrowDown") y = step;
353
- }
354
- if (isBottomHandle(handle)) {
355
- if (key === "ArrowDown") y = step;
356
- else if (key === "ArrowUp") y = -step;
357
- }
358
- return { x, y };
404
+ let x = 0;
405
+ let y = 0;
406
+ if (isLeftHandle(handle)) {
407
+ if (key === "ArrowLeft") x = -step;
408
+ else if (key === "ArrowRight") x = step;
409
+ }
410
+ if (isRightHandle(handle)) {
411
+ if (key === "ArrowRight") x = step;
412
+ else if (key === "ArrowLeft") x = -step;
413
+ }
414
+ if (isTopHandle(handle)) {
415
+ if (key === "ArrowUp") y = -step;
416
+ else if (key === "ArrowDown") y = step;
417
+ }
418
+ if (isBottomHandle(handle)) {
419
+ if (key === "ArrowDown") y = step;
420
+ else if (key === "ArrowUp") y = -step;
421
+ }
422
+ return {
423
+ x,
424
+ y
425
+ };
359
426
  }
360
- // Annotate the CommonJS export names for ESM import in node:
361
- 0 && (module.exports = {
362
- applyMove,
363
- applyResize,
364
- centerInRect,
365
- centerOnPoint,
366
- clampPointInRange,
367
- getArrowKeyDelta,
368
- getDirectionDelta,
369
- getKeyboardResizeDelta,
370
- isBottomHandle,
371
- isCornerHandle,
372
- isHorizontalHandle,
373
- isLeftHandle,
374
- isRightHandle,
375
- isTopHandle,
376
- isVerticalHandle
377
- });
427
+ exports.applyMove = applyMove;
428
+ exports.applyResize = applyResize;
429
+ exports.centerInRect = centerInRect;
430
+ exports.centerOnPoint = centerOnPoint;
431
+ exports.clampPointInRange = clampPointInRange;
432
+ exports.getArrowKeyDelta = getArrowKeyDelta;
433
+ exports.getDirectionDelta = getDirectionDelta;
434
+ exports.getKeyboardResizeDelta = getKeyboardResizeDelta;
435
+ exports.isBottomHandle = isBottomHandle;
436
+ exports.isCornerHandle = isCornerHandle;
437
+ exports.isHorizontalHandle = isHorizontalHandle;
438
+ exports.isLeftHandle = isLeftHandle;
439
+ exports.isRightHandle = isRightHandle;
440
+ exports.isTopHandle = isTopHandle;
441
+ exports.isVerticalHandle = isVerticalHandle;