@open-slide/core 2.0.0-beta.3 → 2.0.0-beta.6

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 (51) hide show
  1. package/README.md +1 -1
  2. package/dist/{build-Bzy-qlGc.js → build-Cu5_qRyE.js} +1 -1
  3. package/dist/cli/bin.js +3 -3
  4. package/dist/{config-nbSvY7z_.js → config-BQ6rD2iE.js} +226 -27
  5. package/dist/{config-DjWifhaA.d.ts → config-GMR1QUlM.d.ts} +1 -1
  6. package/dist/{dev-MXa5Tod4.js → dev-BK641O5e.js} +1 -1
  7. package/dist/index.d.ts +2 -2
  8. package/dist/index.js +1 -1
  9. package/dist/locale/index.d.ts +1 -1
  10. package/dist/locale/index.js +1 -1
  11. package/dist/{preview-DEpQcKz3.js → preview-BuCI-RMW.js} +1 -1
  12. package/dist/{types-BWg-Dtbg.d.ts → types-BcbaKTNY.d.ts} +60 -0
  13. package/dist/vite/index.d.ts +1 -1
  14. package/dist/vite/index.js +1 -1
  15. package/dist/{zh-tw-DlKyeNX7.js → zh-tw-DlZoAc2-.js} +271 -35
  16. package/package.json +6 -6
  17. package/src/app/components/asset-view.tsx +448 -0
  18. package/src/app/components/history-provider.tsx +68 -44
  19. package/src/app/components/icon-switcher-indicator.tsx +10 -0
  20. package/src/app/components/inspector/arrange-panel.tsx +449 -0
  21. package/src/app/components/inspector/inline-text-editor.tsx +170 -284
  22. package/src/app/components/inspector/inspect-overlay.tsx +642 -295
  23. package/src/app/components/inspector/inspector-panel.tsx +434 -427
  24. package/src/app/components/inspector/inspector-provider.tsx +564 -293
  25. package/src/app/components/inspector/save-bar.tsx +36 -0
  26. package/src/app/components/note-markdown.test.tsx +42 -0
  27. package/src/app/components/note-markdown.tsx +85 -0
  28. package/src/app/components/panel/editor-sidebar.tsx +50 -0
  29. package/src/app/components/panel/panel-fields.tsx +204 -16
  30. package/src/app/components/panel/panel-shell.tsx +3 -18
  31. package/src/app/components/style-panel/style-panel.tsx +4 -69
  32. package/src/app/lib/assets.ts +33 -0
  33. package/src/app/lib/export-html.ts +48 -5
  34. package/src/app/lib/inspector/geometry.test.ts +324 -0
  35. package/src/app/lib/inspector/geometry.ts +207 -0
  36. package/src/app/lib/inspector/text-edit-timeline.test.ts +121 -0
  37. package/src/app/lib/inspector/text-edit-timeline.ts +12 -0
  38. package/src/app/lib/inspector/text-selection.ts +117 -0
  39. package/src/app/lib/inspector/use-editor.ts +1 -1
  40. package/src/app/lib/inspector/use-visual-editor.ts +429 -0
  41. package/src/app/lib/inspector/visual-dom.ts +255 -0
  42. package/src/app/lib/keys.ts +9 -0
  43. package/src/app/lib/note-markdown.test.ts +147 -0
  44. package/src/app/lib/note-markdown.ts +116 -0
  45. package/src/app/routes/presenter.tsx +3 -2
  46. package/src/app/routes/slide.tsx +34 -9
  47. package/src/locale/en.ts +71 -9
  48. package/src/locale/ja.ts +73 -8
  49. package/src/locale/types.ts +60 -0
  50. package/src/locale/zh-cn.ts +69 -9
  51. package/src/locale/zh-tw.ts +69 -9
@@ -159,6 +159,39 @@ export async function fetchSvgAsFile(routeUrl: string, filename: string): Promis
159
159
  return new File([blob], filename, { type: 'image/svg+xml' });
160
160
  }
161
161
 
162
+ export type GoogleFontItem = {
163
+ family: string;
164
+ category: string;
165
+ // Weight keys from the Google Fonts catalog, e.g. ['400', '400i', '700'].
166
+ variants: string[];
167
+ };
168
+
169
+ export async function searchGoogleFonts(
170
+ query: string,
171
+ signal?: AbortSignal,
172
+ ): Promise<GoogleFontItem[]> {
173
+ const q = query.trim();
174
+ const params = new URLSearchParams();
175
+ if (q) params.set('q', q);
176
+ else params.set('limit', '30');
177
+ const res = await fetch(`/__gfonts/search?${params.toString()}`, { signal });
178
+ if (!res.ok) throw new Error(`gfonts ${res.status}`);
179
+ return (await res.json()) as GoogleFontItem[];
180
+ }
181
+
182
+ export async function fetchFontAsFile(
183
+ family: string,
184
+ variant: string,
185
+ filenameBase: string,
186
+ ): Promise<File> {
187
+ const params = new URLSearchParams({ family, variant });
188
+ const res = await fetch(`/__gfonts/download?${params.toString()}`);
189
+ if (!res.ok) throw new Error(`gfonts download ${res.status}`);
190
+ const ext = res.headers.get('x-font-ext') || 'ttf';
191
+ const blob = await res.blob();
192
+ return new File([blob], `${filenameBase}.${ext}`, { type: blob.type || 'font/ttf' });
193
+ }
194
+
162
195
  export type UseAssetsResult = {
163
196
  assets: AssetEntry[];
164
197
  loading: boolean;
@@ -274,14 +274,40 @@ html, body { margin: 0; height: 100%; background: #000; overflow: hidden; font-f
274
274
  (function () {
275
275
  var pages = document.querySelectorAll('.os-page');
276
276
  var idx = 0;
277
+ var shown = 0;
277
278
  var frame = document.getElementById('os-frame');
278
279
  var cur = document.getElementById('os-cur');
279
280
  function fit() {
280
281
  var s = Math.min(window.innerWidth / ${CANVAS_WIDTH}, window.innerHeight / ${CANVAS_HEIGHT});
281
282
  frame.style.transform = 'scale(' + s + ')';
282
283
  }
283
- function go(i) {
284
+ // Pages authored with <Steps>/<Step> render every step into the markup, so the file still
285
+ // reads with scripting off. This walks them one keypress at a time, the way the player does:
286
+ // arrive forward and the page starts unrevealed, arrive backward and it is already complete.
287
+ function stepsOf(page) {
288
+ return page ? page.querySelectorAll('[data-osd-step]') : [];
289
+ }
290
+ function setStep(el, revealed, animate) {
291
+ var transition = el.style.transition;
292
+ if (!animate) el.style.transition = 'none';
293
+ el.setAttribute('data-osd-step', revealed ? 'revealed' : 'pending');
294
+ el.style.opacity = revealed ? '1' : '0';
295
+ el.style.visibility = revealed ? 'visible' : 'hidden';
296
+ if (!animate) {
297
+ void el.offsetWidth;
298
+ el.style.transition = transition;
299
+ }
300
+ }
301
+ function paintSteps(animate) {
302
+ var els = stepsOf(pages[idx]);
303
+ for (var i = 0; i < els.length; i++) setStep(els[i], i < shown, animate);
304
+ }
305
+ function go(i, revealAll) {
306
+ var from = idx;
284
307
  idx = Math.max(0, Math.min(pages.length - 1, i));
308
+ if (revealAll) shown = stepsOf(pages[idx]).length;
309
+ else if (idx > from) shown = 0;
310
+ else if (idx < from) shown = stepsOf(pages[idx]).length;
285
311
  pages.forEach(function (p, n) {
286
312
  p.hidden = n !== idx;
287
313
  p.querySelectorAll('video').forEach(function (video) {
@@ -293,14 +319,31 @@ html, body { margin: 0; height: 100%; background: #000; overflow: hidden; font-f
293
319
  }
294
320
  });
295
321
  });
322
+ paintSteps(false);
296
323
  cur.textContent = String(idx + 1);
297
324
  }
325
+ function next() {
326
+ if (shown < stepsOf(pages[idx]).length) {
327
+ shown += 1;
328
+ paintSteps(true);
329
+ return;
330
+ }
331
+ go(idx + 1);
332
+ }
333
+ function prev() {
334
+ if (shown > 0) {
335
+ shown -= 1;
336
+ paintSteps(true);
337
+ return;
338
+ }
339
+ go(idx - 1);
340
+ }
298
341
  window.addEventListener('resize', fit);
299
342
  window.addEventListener('keydown', function (e) {
300
- if (['ArrowRight','ArrowDown','PageDown',' '].indexOf(e.key) >= 0) { e.preventDefault(); go(idx + 1); }
301
- else if (['ArrowLeft','ArrowUp','PageUp'].indexOf(e.key) >= 0) { e.preventDefault(); go(idx - 1); }
302
- else if (e.key === 'Home') { e.preventDefault(); go(0); }
303
- else if (e.key === 'End') { e.preventDefault(); go(pages.length - 1); }
343
+ if (['ArrowRight','ArrowDown','PageDown',' '].indexOf(e.key) >= 0) { e.preventDefault(); next(); }
344
+ else if (['ArrowLeft','ArrowUp','PageUp'].indexOf(e.key) >= 0) { e.preventDefault(); prev(); }
345
+ else if (e.key === 'Home') { e.preventDefault(); go(0, true); }
346
+ else if (e.key === 'End') { e.preventDefault(); go(pages.length - 1, true); }
304
347
  });
305
348
  fit();
306
349
  go(0);
@@ -0,0 +1,324 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ alignRects,
4
+ distributeRects,
5
+ type Rect,
6
+ rectContains,
7
+ rectsIntersect,
8
+ resizeRect,
9
+ snapMove,
10
+ solveResizeDimensions,
11
+ unionRects,
12
+ } from './geometry.ts';
13
+
14
+ describe('unionRects', () => {
15
+ it('finds group bounds across negative coordinates and nested objects', () => {
16
+ expect(
17
+ unionRects([
18
+ { x: -20, y: 30, width: 100, height: 50 },
19
+ { x: 0, y: -10, width: 20, height: 20 },
20
+ { x: 5, y: 40, width: 10, height: 10 },
21
+ ]),
22
+ ).toEqual({ x: -20, y: -10, width: 100, height: 90 });
23
+ expect(unionRects([])).toBeNull();
24
+ });
25
+ });
26
+
27
+ describe('snapMove', () => {
28
+ it('chooses the nearest target on each axis and spans the final snapped objects', () => {
29
+ expect(
30
+ snapMove(
31
+ { x: 0, y: 0, width: 20, height: 20 },
32
+ { x: 78, y: 79 },
33
+ [
34
+ { x: 104, y: 200, width: 40, height: 40 },
35
+ { x: 100, y: 250, width: 40, height: 40 },
36
+ { x: 300, y: 100, width: 20, height: 20 },
37
+ ],
38
+ 6,
39
+ ),
40
+ ).toEqual({
41
+ delta: { x: 80, y: 80 },
42
+ guides: [
43
+ { axis: 'x', position: 100, start: 80, end: 290 },
44
+ { axis: 'y', position: 100, start: 80, end: 320 },
45
+ ],
46
+ });
47
+ });
48
+
49
+ it('snaps centers and includes candidates exactly at the threshold', () => {
50
+ const result = snapMove(
51
+ { x: 0, y: 0, width: 20, height: 20 },
52
+ { x: 76, y: 50 },
53
+ [{ x: 60, y: 200, width: 60, height: 20 }],
54
+ 4,
55
+ );
56
+ expect(result.delta).toEqual({ x: 80, y: 50 });
57
+ expect(result.guides).toEqual([{ axis: 'x', position: 90, start: 50, end: 220 }]);
58
+ });
59
+
60
+ it('breaks equal-distance ties consistently regardless of target order', () => {
61
+ const rect = { x: 0, y: 0, width: 20, height: 20 };
62
+ const targets = [
63
+ { x: 103, y: 200, width: 20, height: 20 },
64
+ { x: 97, y: 200, width: 20, height: 20 },
65
+ ];
66
+ const result = snapMove(rect, { x: 80, y: 50 }, targets, 3);
67
+ expect(result.delta).toEqual({ x: 77, y: 50 });
68
+ expect(snapMove(rect, { x: 80, y: 50 }, [...targets].reverse(), 3)).toEqual(result);
69
+ });
70
+
71
+ it('preserves unsnapped movement when every target is outside the threshold', () => {
72
+ expect(
73
+ snapMove(
74
+ { x: 0, y: 0, width: 20, height: 20 },
75
+ { x: 10, y: 10 },
76
+ [{ x: 100, y: 100, width: 20, height: 20 }],
77
+ 5,
78
+ ),
79
+ ).toEqual({ delta: { x: 10, y: 10 }, guides: [] });
80
+ });
81
+ });
82
+
83
+ describe('alignRects', () => {
84
+ const rects = [
85
+ { x: 10, y: 20, width: 20, height: 40 },
86
+ { x: 80, y: 100, width: 40, height: 20 },
87
+ ];
88
+
89
+ it.each([
90
+ [
91
+ 'left',
92
+ [
93
+ { x: 0, y: 0 },
94
+ { x: -70, y: 0 },
95
+ ],
96
+ ],
97
+ [
98
+ 'center',
99
+ [
100
+ { x: 45, y: 0 },
101
+ { x: -35, y: 0 },
102
+ ],
103
+ ],
104
+ [
105
+ 'right',
106
+ [
107
+ { x: 90, y: 0 },
108
+ { x: 0, y: 0 },
109
+ ],
110
+ ],
111
+ [
112
+ 'top',
113
+ [
114
+ { x: 0, y: 0 },
115
+ { x: 0, y: -80 },
116
+ ],
117
+ ],
118
+ [
119
+ 'middle',
120
+ [
121
+ { x: 0, y: 30 },
122
+ { x: 0, y: -40 },
123
+ ],
124
+ ],
125
+ [
126
+ 'bottom',
127
+ [
128
+ { x: 0, y: 60 },
129
+ { x: 0, y: 0 },
130
+ ],
131
+ ],
132
+ ] as const)('aligns %s against group bounds', (alignment, expected) => {
133
+ expect(alignRects(rects, alignment)).toEqual(expected);
134
+ });
135
+
136
+ it('aligns a single object against explicit slide bounds', () => {
137
+ expect(alignRects([rects[0]], 'center', { x: 0, y: 0, width: 1280, height: 720 })).toEqual([
138
+ { x: 620, y: 0 },
139
+ ]);
140
+ expect(alignRects([], 'left')).toEqual([]);
141
+ });
142
+ });
143
+
144
+ describe('distributeRects', () => {
145
+ it('creates equal horizontal gaps for unequal sizes and preserves the input order', () => {
146
+ const rects = [
147
+ { x: 200, y: 20, width: 40, height: 20 },
148
+ { x: 60, y: 30, width: 20, height: 30 },
149
+ { x: 0, y: 10, width: 60, height: 40 },
150
+ ];
151
+ expect(distributeRects(rects, 'x')).toEqual([
152
+ { x: 0, y: 0 },
153
+ { x: 60, y: 0 },
154
+ { x: 0, y: 0 },
155
+ ]);
156
+ });
157
+
158
+ it('distributes vertical spacing while keeping the first and last objects fixed', () => {
159
+ expect(
160
+ distributeRects(
161
+ [
162
+ { x: 10, y: 0, width: 10, height: 30 },
163
+ { x: 30, y: 40, width: 10, height: 10 },
164
+ { x: 50, y: 150, width: 10, height: 20 },
165
+ { x: 70, y: 210, width: 10, height: 30 },
166
+ ],
167
+ 'y',
168
+ ),
169
+ ).toEqual([
170
+ { x: 0, y: 0 },
171
+ { x: 0, y: 40 },
172
+ { x: 0, y: -10 },
173
+ { x: 0, y: 0 },
174
+ ]);
175
+ });
176
+
177
+ it('leaves fewer than three objects in place', () => {
178
+ expect(distributeRects([], 'x')).toEqual([]);
179
+ expect(distributeRects([{ x: 10, y: 20, width: 30, height: 40 }], 'y')).toEqual([
180
+ { x: 0, y: 0 },
181
+ ]);
182
+ });
183
+ });
184
+
185
+ describe('resizeRect', () => {
186
+ const rect: Rect = { x: 100, y: 200, width: 80, height: 40 };
187
+
188
+ it('clamps crossed edges to a minimum size while keeping the opposite corner anchored', () => {
189
+ expect(resizeRect(rect, 'nw', { x: 200, y: 200 })).toEqual({
190
+ x: 172,
191
+ y: 232,
192
+ width: 8,
193
+ height: 8,
194
+ });
195
+ expect(resizeRect(rect, 'se', { x: -200, y: -200 })).toEqual({
196
+ x: 100,
197
+ y: 200,
198
+ width: 8,
199
+ height: 8,
200
+ });
201
+ });
202
+
203
+ it('locks corner aspect ratio using the dominant relative movement', () => {
204
+ expect(resizeRect(rect, 'nw', { x: -20, y: -40 }, true)).toEqual({
205
+ x: 20,
206
+ y: 160,
207
+ width: 160,
208
+ height: 80,
209
+ });
210
+ expect(resizeRect(rect, 'sw', { x: -80, y: 10 }, true)).toEqual({
211
+ x: 20,
212
+ y: 200,
213
+ width: 160,
214
+ height: 80,
215
+ });
216
+ });
217
+
218
+ it('keeps both minimum size and aspect ratio when shrinking', () => {
219
+ expect(resizeRect(rect, 'ne', { x: -100, y: 100 }, true)).toEqual({
220
+ x: 100,
221
+ y: 232,
222
+ width: 16,
223
+ height: 8,
224
+ });
225
+ });
226
+
227
+ it('keeps the opposite edge midpoint fixed for aspect-locked side handles', () => {
228
+ expect(resizeRect(rect, 'e', { x: 40, y: 100 }, true)).toEqual({
229
+ x: 100,
230
+ y: 190,
231
+ width: 120,
232
+ height: 60,
233
+ });
234
+ expect(resizeRect(rect, 'n', { x: 100, y: -20 }, true)).toEqual({
235
+ x: 80,
236
+ y: 180,
237
+ width: 120,
238
+ height: 60,
239
+ });
240
+ });
241
+
242
+ it('ignores perpendicular movement for an unlocked side handle', () => {
243
+ expect(resizeRect(rect, 'w', { x: 20, y: 100 })).toEqual({
244
+ x: 120,
245
+ y: 200,
246
+ width: 60,
247
+ height: 40,
248
+ });
249
+ });
250
+ });
251
+
252
+ describe('solveResizeDimensions', () => {
253
+ const size = { width: 200, height: 100 };
254
+ const rotatedBasis = (degrees: number, scale = 1) => {
255
+ const angle = (degrees * Math.PI) / 180;
256
+ return {
257
+ a: Math.cos(angle) * scale,
258
+ b: Math.sin(angle) * scale,
259
+ c: Math.sin(angle) * scale,
260
+ d: Math.cos(angle) * scale,
261
+ };
262
+ };
263
+
264
+ it('resizes independent axes exactly for an unrotated element', () => {
265
+ expect(solveResizeDimensions(size, { a: 1, b: 0, c: 0, d: 1 }, { x: 50, y: -20 })).toEqual({
266
+ width: 250,
267
+ height: 80,
268
+ });
269
+ });
270
+
271
+ it('solves feasible rotated sizes without forcing an aspect lock', () => {
272
+ const basis = rotatedBasis(30);
273
+ const result = solveResizeDimensions(size, basis, {
274
+ x: 40 * basis.a - 20 * basis.c,
275
+ y: 40 * basis.b - 20 * basis.d,
276
+ });
277
+ expect(result.width).toBeCloseTo(240);
278
+ expect(result.height).toBeCloseTo(80);
279
+ });
280
+
281
+ it.each([44, 45, 46])('preserves proportions instead of distorting at %s degrees', (angle) => {
282
+ const result = solveResizeDimensions(size, rotatedBasis(angle), { x: 50, y: 0 });
283
+ expect(result.width).toBeGreaterThan(200);
284
+ expect(result.width).toBeLessThan(250);
285
+ expect(result.height).toBeGreaterThan(100);
286
+ expect(result.width / result.height).toBeCloseTo(2);
287
+ });
288
+
289
+ it('uses the same safe result under a scaled parent', () => {
290
+ const normal = solveResizeDimensions(size, rotatedBasis(44), { x: 50, y: 0 });
291
+ const scaled = solveResizeDimensions(size, rotatedBasis(44, 0.2), { x: 10, y: 0 });
292
+ expect(scaled.width).toBeCloseTo(normal.width);
293
+ expect(scaled.height).toBeCloseTo(normal.height);
294
+ });
295
+
296
+ it('preserves proportions when the requested bounds require a negative local size', () => {
297
+ const result = solveResizeDimensions(size, rotatedBasis(30), { x: 200, y: 0 });
298
+ expect(result.width).toBeLessThan(400);
299
+ expect(result.width / result.height).toBeCloseTo(2);
300
+ });
301
+
302
+ it('keeps both local dimensions above the minimum when fitting a smaller box', () => {
303
+ const result = solveResizeDimensions(size, rotatedBasis(45), { x: -1000, y: -1000 });
304
+ expect(result).toEqual({ width: 16, height: 8 });
305
+ });
306
+ });
307
+
308
+ describe('rectsIntersect', () => {
309
+ it('detects overlap but not edges that only touch', () => {
310
+ const marquee: Rect = { x: 90, y: 130, width: 510, height: 270 };
311
+ expect(rectsIntersect(marquee, { x: 520, y: 360, width: 240, height: 160 })).toBe(true);
312
+ expect(rectsIntersect(marquee, { x: 600, y: 360, width: 240, height: 160 })).toBe(false);
313
+ expect(rectsIntersect(marquee, { x: 1120, y: 660, width: 240, height: 160 })).toBe(false);
314
+ });
315
+ });
316
+
317
+ describe('rectContains', () => {
318
+ it('requires the inner rect to sit fully inside the outer rect', () => {
319
+ const outer: Rect = { x: 0, y: 0, width: 100, height: 100 };
320
+ expect(rectContains(outer, { x: 10, y: 10, width: 80, height: 80 })).toBe(true);
321
+ expect(rectContains(outer, { x: 0, y: 0, width: 100, height: 100 })).toBe(true);
322
+ expect(rectContains(outer, { x: 50, y: 50, width: 60, height: 20 })).toBe(false);
323
+ });
324
+ });
@@ -0,0 +1,207 @@
1
+ export type Point = { x: number; y: number };
2
+
3
+ export type Rect = Point & { width: number; height: number };
4
+
5
+ export type Alignment = 'left' | 'center' | 'right' | 'top' | 'middle' | 'bottom';
6
+
7
+ export type Guide = {
8
+ axis: 'x' | 'y';
9
+ position: number;
10
+ start: number;
11
+ end: number;
12
+ };
13
+
14
+ export type ResizeHandle = 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw';
15
+
16
+ export function unionRects(rects: Rect[]): Rect | null {
17
+ if (!rects.length) return null;
18
+ const x = Math.min(...rects.map((rect) => rect.x));
19
+ const y = Math.min(...rects.map((rect) => rect.y));
20
+ return {
21
+ x,
22
+ y,
23
+ width: Math.max(...rects.map((rect) => rect.x + rect.width)) - x,
24
+ height: Math.max(...rects.map((rect) => rect.y + rect.height)) - y,
25
+ };
26
+ }
27
+
28
+ function anchors(rect: Rect, axis: 'x' | 'y'): number[] {
29
+ const size = axis === 'x' ? rect.width : rect.height;
30
+ return [rect[axis], rect[axis] + size / 2, rect[axis] + size];
31
+ }
32
+
33
+ function nearestSnap(rect: Rect, targets: Rect[], axis: 'x' | 'y', threshold: number) {
34
+ let best: { correction: number; position: number } | null = null;
35
+ for (const target of targets) {
36
+ for (const position of anchors(target, axis)) {
37
+ for (const anchor of anchors(rect, axis)) {
38
+ const correction = position - anchor;
39
+ const distance = Math.abs(correction);
40
+ if (distance > threshold) continue;
41
+ if (
42
+ !best ||
43
+ distance < Math.abs(best.correction) ||
44
+ (distance === Math.abs(best.correction) && correction < best.correction) ||
45
+ (correction === best.correction && position < best.position)
46
+ ) {
47
+ best = { correction, position };
48
+ }
49
+ }
50
+ }
51
+ }
52
+ return best;
53
+ }
54
+
55
+ export function snapMove(
56
+ rect: Rect,
57
+ delta: Point,
58
+ targets: Rect[],
59
+ threshold: number,
60
+ ): { delta: Point; guides: Guide[] } {
61
+ const moved = { ...rect, x: rect.x + delta.x, y: rect.y + delta.y };
62
+ const xSnap = nearestSnap(moved, targets, 'x', threshold);
63
+ const ySnap = nearestSnap(moved, targets, 'y', threshold);
64
+ const snappedDelta = {
65
+ x: delta.x + (xSnap?.correction ?? 0),
66
+ y: delta.y + (ySnap?.correction ?? 0),
67
+ };
68
+ const snapped = { ...rect, x: rect.x + snappedDelta.x, y: rect.y + snappedDelta.y };
69
+ const guides: Guide[] = [];
70
+ for (const axis of ['x', 'y'] as const) {
71
+ const snap = axis === 'x' ? xSnap : ySnap;
72
+ if (!snap) continue;
73
+ const aligned = targets.filter((target) =>
74
+ anchors(target, axis).some((anchor) => Math.abs(anchor - snap.position) < 0.000001),
75
+ );
76
+ const bounds = unionRects([snapped, ...aligned]);
77
+ if (!bounds) continue;
78
+ guides.push({
79
+ axis,
80
+ position: snap.position,
81
+ start: axis === 'x' ? bounds.y : bounds.x,
82
+ end: axis === 'x' ? bounds.y + bounds.height : bounds.x + bounds.width,
83
+ });
84
+ }
85
+ return { delta: snappedDelta, guides };
86
+ }
87
+
88
+ export function alignRects(rects: Rect[], alignment: Alignment, bounds?: Rect): Point[] {
89
+ const target = bounds ?? unionRects(rects);
90
+ if (!target) return [];
91
+ return rects.map((rect) => {
92
+ switch (alignment) {
93
+ case 'left':
94
+ return { x: target.x - rect.x, y: 0 };
95
+ case 'center':
96
+ return { x: target.x + target.width / 2 - rect.x - rect.width / 2, y: 0 };
97
+ case 'right':
98
+ return { x: target.x + target.width - rect.x - rect.width, y: 0 };
99
+ case 'top':
100
+ return { x: 0, y: target.y - rect.y };
101
+ case 'middle':
102
+ return { x: 0, y: target.y + target.height / 2 - rect.y - rect.height / 2 };
103
+ default:
104
+ return { x: 0, y: target.y + target.height - rect.y - rect.height };
105
+ }
106
+ });
107
+ }
108
+
109
+ export function distributeRects(rects: Rect[], axis: 'x' | 'y'): Point[] {
110
+ const deltas = rects.map(() => ({ x: 0, y: 0 }));
111
+ if (rects.length < 3) return deltas;
112
+ const size = axis === 'x' ? 'width' : 'height';
113
+ const sorted = rects
114
+ .map((rect, index) => ({ rect, index }))
115
+ .sort((a, b) => a.rect[axis] - b.rect[axis] || a.index - b.index);
116
+ const first = sorted[0];
117
+ const last = sorted[sorted.length - 1];
118
+ const totalSize = rects.reduce((sum, rect) => sum + rect[size], 0);
119
+ const gap =
120
+ (last.rect[axis] + last.rect[size] - first.rect[axis] - totalSize) / (rects.length - 1);
121
+ let position = first.rect[axis];
122
+ for (const { rect, index } of sorted) {
123
+ deltas[index][axis] = position - rect[axis];
124
+ position += rect[size] + gap;
125
+ }
126
+ return deltas;
127
+ }
128
+
129
+ export function solveResizeDimensions(
130
+ size: Pick<Rect, 'width' | 'height'>,
131
+ basis: { a: number; b: number; c: number; d: number },
132
+ delta: Point,
133
+ ): Pick<Rect, 'width' | 'height'> {
134
+ const { a, b, c, d } = basis;
135
+ const determinant = a * d - b * c;
136
+ const magnitude = Math.hypot(a, b) * Math.hypot(c, d);
137
+ // Nearly parallel dimension vectors amplify small pointer deltas into large distortions.
138
+ if (Math.abs(determinant) > magnitude * 0.1) {
139
+ const width = size.width + (d * delta.x - c * delta.y) / determinant;
140
+ const height = size.height + (a * delta.y - b * delta.x) / determinant;
141
+ if (Number.isFinite(width) && Number.isFinite(height) && width >= 8 && height >= 8)
142
+ return { width, height };
143
+ }
144
+ const direction = {
145
+ x: a * size.width + c * size.height,
146
+ y: b * size.width + d * size.height,
147
+ };
148
+ const lengthSquared = direction.x ** 2 + direction.y ** 2;
149
+ const requestedScale =
150
+ lengthSquared > 0 ? 1 + (direction.x * delta.x + direction.y * delta.y) / lengthSquared : 1;
151
+ const scale = Math.max(8 / size.width, 8 / size.height, requestedScale);
152
+ return { width: size.width * scale, height: size.height * scale };
153
+ }
154
+
155
+ export function resizeRect(
156
+ rect: Rect,
157
+ handle: ResizeHandle,
158
+ delta: Point,
159
+ lockAspect = false,
160
+ ): Rect {
161
+ const west = handle.includes('w');
162
+ const east = handle.includes('e');
163
+ const north = handle.includes('n');
164
+ const south = handle.includes('s');
165
+ const horizontal = west || east;
166
+ const vertical = north || south;
167
+ const requestedWidth = rect.width + (west ? -delta.x : east ? delta.x : 0);
168
+ const requestedHeight = rect.height + (north ? -delta.y : south ? delta.y : 0);
169
+ let width = Math.max(8, requestedWidth);
170
+ let height = Math.max(8, requestedHeight);
171
+ if (lockAspect && rect.width > 0 && rect.height > 0) {
172
+ const widthScale = requestedWidth / rect.width;
173
+ const heightScale = requestedHeight / rect.height;
174
+ const scale = Math.max(
175
+ 8 / rect.width,
176
+ 8 / rect.height,
177
+ horizontal && (!vertical || Math.abs(widthScale - 1) >= Math.abs(heightScale - 1))
178
+ ? widthScale
179
+ : heightScale,
180
+ );
181
+ width = rect.width * scale;
182
+ height = rect.height * scale;
183
+ }
184
+ return {
185
+ x: west ? rect.x + rect.width - width : horizontal ? rect.x : rect.x + (rect.width - width) / 2,
186
+ y: north
187
+ ? rect.y + rect.height - height
188
+ : vertical
189
+ ? rect.y
190
+ : rect.y + (rect.height - height) / 2,
191
+ width,
192
+ height,
193
+ };
194
+ }
195
+
196
+ export function rectsIntersect(a: Rect, b: Rect): boolean {
197
+ 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;
198
+ }
199
+
200
+ export function rectContains(outer: Rect, inner: Rect): boolean {
201
+ return (
202
+ inner.x >= outer.x &&
203
+ inner.y >= outer.y &&
204
+ inner.x + inner.width <= outer.x + outer.width &&
205
+ inner.y + inner.height <= outer.y + outer.height
206
+ );
207
+ }