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