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

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 (45) 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-B51sMTg3.js} +267 -31
  16. package/package.json +4 -4
  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/panel/editor-sidebar.tsx +50 -0
  27. package/src/app/components/panel/panel-fields.tsx +204 -16
  28. package/src/app/components/panel/panel-shell.tsx +3 -18
  29. package/src/app/components/style-panel/style-panel.tsx +4 -69
  30. package/src/app/lib/assets.ts +33 -0
  31. package/src/app/lib/inspector/geometry.test.ts +324 -0
  32. package/src/app/lib/inspector/geometry.ts +207 -0
  33. package/src/app/lib/inspector/text-edit-timeline.test.ts +121 -0
  34. package/src/app/lib/inspector/text-edit-timeline.ts +12 -0
  35. package/src/app/lib/inspector/text-selection.ts +117 -0
  36. package/src/app/lib/inspector/use-editor.ts +1 -1
  37. package/src/app/lib/inspector/use-visual-editor.ts +429 -0
  38. package/src/app/lib/inspector/visual-dom.ts +255 -0
  39. package/src/app/lib/keys.ts +9 -0
  40. package/src/app/routes/slide.tsx +34 -9
  41. package/src/locale/en.ts +70 -8
  42. package/src/locale/ja.ts +72 -7
  43. package/src/locale/types.ts +60 -0
  44. package/src/locale/zh-cn.ts +68 -8
  45. package/src/locale/zh-tw.ts +68 -8
@@ -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
+ }
@@ -0,0 +1,121 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { applyEditBatch } from '../../../editing/batch-edit';
3
+ import { appendTextEdit, type TextEditStep } from './text-edit-timeline';
4
+
5
+ const source = 'export const Slide = () => <p>Editable body copy</p>;';
6
+ const original = 'Editable body copy';
7
+ const location = { line: 1, column: source.indexOf('<p>') };
8
+
9
+ function save(steps: TextEditStep[]) {
10
+ return applyEditBatch(
11
+ source,
12
+ steps.map(({ op }) => ({ ...location, ops: [op] })),
13
+ );
14
+ }
15
+
16
+ describe('text edit timeline', () => {
17
+ it('coalesces consecutive typing while retaining the source baseline', () => {
18
+ let steps = appendTextEdit([], { kind: 'set-text', value: 'First', prevText: original }, 1);
19
+ steps = appendTextEdit(steps, { kind: 'set-text', value: 'Final', prevText: 'First' }, 2);
20
+ expect(steps).toEqual([
21
+ { seq: 2, op: { kind: 'set-text', value: 'Final', prevText: original } },
22
+ ]);
23
+ expect(save(steps).source).toContain('<p>Final</p>');
24
+ });
25
+
26
+ it('keeps the intermediate text required by a range format before later typing', () => {
27
+ const updated = `${original} updated`;
28
+ let steps = appendTextEdit([], { kind: 'set-text', value: updated, prevText: original }, 1);
29
+ steps = appendTextEdit(
30
+ steps,
31
+ {
32
+ kind: 'set-text-range-style',
33
+ start: 0,
34
+ end: 8,
35
+ key: 'fontWeight',
36
+ value: '700',
37
+ prevText: updated,
38
+ },
39
+ 2,
40
+ );
41
+ steps = appendTextEdit(
42
+ steps,
43
+ { kind: 'set-text', value: `${updated} again`, prevText: updated },
44
+ 3,
45
+ );
46
+ const result = save(steps);
47
+ expect(result.results).toEqual([{ ok: true }, { ok: true }, { ok: true }]);
48
+ expect(result.source).toContain('fontWeight');
49
+ expect(result.source).toContain('updated again');
50
+ });
51
+
52
+ it('keeps typing checkpoints across whole-text style operations', () => {
53
+ const first = appendTextEdit([], { kind: 'set-text', value: 'First', prevText: original }, 1);
54
+ const final = appendTextEdit(first, { kind: 'set-text', value: 'Final', prevText: 'First' }, 3);
55
+ expect(final).toHaveLength(2);
56
+ const result = applyEditBatch(source, [
57
+ { ...location, ops: [final[0].op] },
58
+ {
59
+ ...location,
60
+ ops: [{ kind: 'set-style', key: 'fontSize', value: '28px', prevText: 'First' }],
61
+ },
62
+ { ...location, ops: [final[1].op] },
63
+ ]);
64
+ expect(result.results.every((item) => item.ok)).toBe(true);
65
+ expect(result.source).toContain('Final');
66
+ expect(result.source).toContain('28px');
67
+ });
68
+
69
+ it('preserves earlier snapshots when typing is coalesced and then resumed after undo', () => {
70
+ const before = appendTextEdit([], { kind: 'set-text', value: 'First', prevText: original }, 1);
71
+ const after = appendTextEdit(
72
+ before,
73
+ { kind: 'set-text', value: 'Second', prevText: 'First' },
74
+ 2,
75
+ );
76
+ expect(before[0].op).toEqual({ kind: 'set-text', value: 'First', prevText: original });
77
+ expect(after[0].op).toEqual({ kind: 'set-text', value: 'Second', prevText: original });
78
+ const resumed = appendTextEdit(
79
+ before,
80
+ { kind: 'set-text', value: 'Third', prevText: 'First' },
81
+ 3,
82
+ );
83
+ expect(save(resumed).results.every((item) => item.ok)).toBe(true);
84
+ expect(save(resumed).source).toContain('<p>Third</p>');
85
+ });
86
+
87
+ it('formats current text coordinates after inserting a prefix without dropping prior styles', () => {
88
+ let steps = appendTextEdit(
89
+ [],
90
+ {
91
+ kind: 'set-text-range-style',
92
+ start: 0,
93
+ end: 8,
94
+ key: 'fontWeight',
95
+ value: '700',
96
+ prevText: original,
97
+ },
98
+ 1,
99
+ );
100
+ const prefixed = `New ${original}`;
101
+ steps = appendTextEdit(steps, { kind: 'set-text', value: prefixed, prevText: original }, 2);
102
+ steps = appendTextEdit(
103
+ steps,
104
+ {
105
+ kind: 'set-text-range-style',
106
+ start: 13,
107
+ end: 17,
108
+ key: 'fontStyle',
109
+ value: 'italic',
110
+ prevText: prefixed,
111
+ },
112
+ 3,
113
+ );
114
+ const result = save(steps);
115
+ expect(result.results).toEqual([{ ok: true }, { ok: true }, { ok: true }]);
116
+ expect(result.source).toContain('New Editable');
117
+ expect(result.source).toContain('fontWeight');
118
+ expect(result.source).toContain('fontStyle');
119
+ expect(result.source).toContain('italic');
120
+ });
121
+ });
@@ -0,0 +1,12 @@
1
+ import type { EditOp } from './use-editor';
2
+
3
+ export type TextEditOp = Extract<EditOp, { kind: 'set-text' | 'set-text-range-style' }>;
4
+ export type TextEditStep = { seq: number; op: TextEditOp };
5
+
6
+ export function appendTextEdit(steps: TextEditStep[], op: TextEditOp, seq: number): TextEditStep[] {
7
+ const last = steps.at(-1);
8
+ if (last?.seq === seq - 1 && last.op.kind === 'set-text' && op.kind === 'set-text') {
9
+ return [...steps.slice(0, -1), { seq, op: { ...op, prevText: last.op.prevText } }];
10
+ }
11
+ return [...steps, { seq, op }];
12
+ }