@superwall/studio 2.4.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/assets/index-CrvkmzpL.js +11 -0
- package/dist/assets/index-DKNVIOVd.css +1 -0
- package/dist/index.html +2 -2
- package/dist/lib/components/preview/Canvas.d.ts +5 -1
- package/dist/lib/index.d.ts +4 -2
- package/dist/lib/index.js +2114 -2059
- package/dist/lib/lib/canvas.d.ts +4 -3
- package/dist/lib/studio.css +1 -1
- package/package.json +2 -2
- package/src/components/preview/Canvas.tsx +171 -42
- package/src/index.ts +17 -2
- package/src/lib/canvas.ts +22 -11
- package/src/screens/SurfaceEditor.tsx +0 -33
- package/dist/assets/index-DYECTotv.js +0 -11
- package/dist/assets/index-xuSjpy2B.css +0 -1
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import {
|
|
3
3
|
centered,
|
|
4
|
+
clampScale,
|
|
5
|
+
FIT_INSET,
|
|
4
6
|
fitTransform,
|
|
5
7
|
panBy,
|
|
6
8
|
stepScale,
|
|
7
9
|
zoomAt,
|
|
8
10
|
type CanvasTransform,
|
|
11
|
+
type Inset,
|
|
9
12
|
type Size,
|
|
10
13
|
} from "../../lib/canvas";
|
|
11
14
|
|
|
@@ -28,7 +31,7 @@ type WheelGesture = { at: number; sum: Point; pans: Axes | null };
|
|
|
28
31
|
const PANS_NONE: Axes = { x: false, y: false };
|
|
29
32
|
const PANS_BOTH: Axes = { x: true, y: true };
|
|
30
33
|
|
|
31
|
-
type Mode = { kind: "fit" } | { kind: "manual"; transform: CanvasTransform };
|
|
34
|
+
type Mode = { kind: "fit" } | { kind: "manual"; transform: CanvasTransform; followsFit: boolean };
|
|
32
35
|
|
|
33
36
|
type Pinch = { span: number; midpoint: Point };
|
|
34
37
|
|
|
@@ -138,12 +141,13 @@ const useLatest = <T,>(value: T): React.RefObject<T> => {
|
|
|
138
141
|
return ref;
|
|
139
142
|
};
|
|
140
143
|
|
|
141
|
-
const useSpaceHeld = (): boolean => {
|
|
144
|
+
const useSpaceHeld = (engaged: React.RefObject<boolean>): boolean => {
|
|
142
145
|
const [held, setHeld] = React.useState(false);
|
|
143
146
|
|
|
144
147
|
React.useEffect(() => {
|
|
145
148
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
146
149
|
if (event.code !== "Space" || event.repeat || isTyping(event.target)) return;
|
|
150
|
+
if (!engaged.current) return;
|
|
147
151
|
event.preventDefault();
|
|
148
152
|
setHeld(true);
|
|
149
153
|
};
|
|
@@ -159,7 +163,7 @@ const useSpaceHeld = (): boolean => {
|
|
|
159
163
|
window.removeEventListener("keyup", onKeyUp);
|
|
160
164
|
window.removeEventListener("blur", release);
|
|
161
165
|
};
|
|
162
|
-
}, []);
|
|
166
|
+
}, [engaged]);
|
|
163
167
|
|
|
164
168
|
return held;
|
|
165
169
|
};
|
|
@@ -168,21 +172,83 @@ export type CanvasZoom = { scale: number; fitted: boolean };
|
|
|
168
172
|
|
|
169
173
|
export type CanvasHandle = {
|
|
170
174
|
zoomStep: (direction: 1 | -1) => void;
|
|
175
|
+
zoomTo: (scale: number) => void;
|
|
176
|
+
zoom: () => CanvasZoom;
|
|
171
177
|
actualSize: () => void;
|
|
172
178
|
fit: () => void;
|
|
173
179
|
hold: () => void;
|
|
174
180
|
pan: (dx: number, dy: number) => void;
|
|
175
181
|
};
|
|
176
182
|
|
|
183
|
+
const insetOf = (inset: number | undefined): Inset =>
|
|
184
|
+
inset === undefined ? FIT_INSET : { top: inset, right: inset, bottom: inset, left: inset };
|
|
185
|
+
|
|
186
|
+
const sameSize = (a: Size, b: Size): boolean => a.width === b.width && a.height === b.height;
|
|
187
|
+
|
|
188
|
+
const useEngaged = (viewport: HTMLElement | null, contained: boolean): React.RefObject<boolean> => {
|
|
189
|
+
const engaged = React.useRef(!contained);
|
|
190
|
+
|
|
191
|
+
React.useEffect(() => {
|
|
192
|
+
engaged.current = !contained;
|
|
193
|
+
if (!contained || !viewport) return undefined;
|
|
194
|
+
let hovered = false;
|
|
195
|
+
let focused = false;
|
|
196
|
+
const update = () => {
|
|
197
|
+
engaged.current = hovered || focused;
|
|
198
|
+
};
|
|
199
|
+
const controller = new AbortController();
|
|
200
|
+
const { signal } = controller;
|
|
201
|
+
viewport.addEventListener(
|
|
202
|
+
"pointerenter",
|
|
203
|
+
() => {
|
|
204
|
+
hovered = true;
|
|
205
|
+
update();
|
|
206
|
+
},
|
|
207
|
+
{ signal },
|
|
208
|
+
);
|
|
209
|
+
viewport.addEventListener(
|
|
210
|
+
"pointerleave",
|
|
211
|
+
() => {
|
|
212
|
+
hovered = false;
|
|
213
|
+
update();
|
|
214
|
+
},
|
|
215
|
+
{ signal },
|
|
216
|
+
);
|
|
217
|
+
viewport.addEventListener(
|
|
218
|
+
"focusin",
|
|
219
|
+
() => {
|
|
220
|
+
focused = true;
|
|
221
|
+
update();
|
|
222
|
+
},
|
|
223
|
+
{ signal },
|
|
224
|
+
);
|
|
225
|
+
viewport.addEventListener(
|
|
226
|
+
"focusout",
|
|
227
|
+
(event: FocusEvent) => {
|
|
228
|
+
focused = event.relatedTarget instanceof Node && viewport.contains(event.relatedTarget);
|
|
229
|
+
update();
|
|
230
|
+
},
|
|
231
|
+
{ signal },
|
|
232
|
+
);
|
|
233
|
+
return () => controller.abort();
|
|
234
|
+
}, [contained, viewport]);
|
|
235
|
+
|
|
236
|
+
return engaged;
|
|
237
|
+
};
|
|
238
|
+
|
|
177
239
|
export const Canvas = ({
|
|
178
240
|
fitKey,
|
|
179
241
|
ref,
|
|
180
242
|
onZoom,
|
|
243
|
+
inset,
|
|
244
|
+
contained = false,
|
|
181
245
|
children,
|
|
182
246
|
}: React.PropsWithChildren<{
|
|
183
247
|
fitKey: string;
|
|
184
248
|
ref?: React.Ref<CanvasHandle>;
|
|
185
249
|
onZoom?: (zoom: CanvasZoom) => void;
|
|
250
|
+
inset?: number;
|
|
251
|
+
contained?: boolean;
|
|
186
252
|
}>) => {
|
|
187
253
|
const [viewport, setViewport] = React.useState<HTMLDivElement | null>(null);
|
|
188
254
|
const [content, setContent] = React.useState<HTMLDivElement | null>(null);
|
|
@@ -190,14 +256,25 @@ export const Canvas = ({
|
|
|
190
256
|
const [dragging, setDragging] = React.useState(false);
|
|
191
257
|
const viewportSize = useSize(viewport);
|
|
192
258
|
const contentSize = useSize(content);
|
|
193
|
-
const
|
|
259
|
+
const engaged = useEngaged(viewport, contained);
|
|
260
|
+
const handTool = useSpaceHeld(engaged);
|
|
261
|
+
const room = React.useMemo(() => insetOf(inset), [inset]);
|
|
194
262
|
|
|
195
|
-
const transform =
|
|
196
|
-
|
|
263
|
+
const transform =
|
|
264
|
+
mode.kind === "fit" ? fitTransform(contentSize, viewportSize, room) : mode.transform;
|
|
197
265
|
const pointers = React.useRef(new Map<number, Point>());
|
|
198
266
|
const pinch = React.useRef<Pinch | null>(null);
|
|
199
267
|
const lastFitKey = React.useRef(fitKey);
|
|
200
268
|
const gesture = React.useRef<WheelGesture | null>(null);
|
|
269
|
+
const requested = React.useRef<Mode>(mode);
|
|
270
|
+
const measured = useLatest({ contentSize, viewportSize, room });
|
|
271
|
+
|
|
272
|
+
const current = React.useCallback((): CanvasTransform => {
|
|
273
|
+
const asked = requested.current;
|
|
274
|
+
if (asked.kind === "manual") return asked.transform;
|
|
275
|
+
const sizes = measured.current;
|
|
276
|
+
return fitTransform(sizes.contentSize, sizes.viewportSize, sizes.room);
|
|
277
|
+
}, [measured]);
|
|
201
278
|
|
|
202
279
|
const wheelGesture = React.useCallback((): WheelGesture => {
|
|
203
280
|
const now = performance.now();
|
|
@@ -210,11 +287,25 @@ export const Canvas = ({
|
|
|
210
287
|
return gesture.current;
|
|
211
288
|
}, []);
|
|
212
289
|
|
|
213
|
-
const
|
|
214
|
-
|
|
290
|
+
const settle = React.useCallback((transform: CanvasTransform, followsFit: boolean) => {
|
|
291
|
+
requested.current = { kind: "manual", transform, followsFit };
|
|
292
|
+
setMode(requested.current);
|
|
215
293
|
}, []);
|
|
216
294
|
|
|
217
|
-
const
|
|
295
|
+
const apply = React.useCallback((next: CanvasTransform) => settle(next, false), [settle]);
|
|
296
|
+
|
|
297
|
+
const move = React.useCallback(
|
|
298
|
+
(next: CanvasTransform) => {
|
|
299
|
+
const asked = requested.current;
|
|
300
|
+
settle(next, asked.kind === "fit" || asked.followsFit);
|
|
301
|
+
},
|
|
302
|
+
[settle],
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
const fit = React.useCallback(() => {
|
|
306
|
+
requested.current = { kind: "fit" };
|
|
307
|
+
setMode(requested.current);
|
|
308
|
+
}, []);
|
|
218
309
|
|
|
219
310
|
const toViewport = React.useCallback(
|
|
220
311
|
(clientX: number, clientY: number): Point => {
|
|
@@ -227,33 +318,48 @@ export const Canvas = ({
|
|
|
227
318
|
const zoomWheel = React.useCallback(
|
|
228
319
|
(event: WheelEvent, at: Point) => {
|
|
229
320
|
event.preventDefault();
|
|
230
|
-
apply(zoomAt(
|
|
321
|
+
apply(zoomAt(current(), wheelZoomFactor(event.deltaY), at));
|
|
231
322
|
},
|
|
232
|
-
[apply,
|
|
323
|
+
[apply, current],
|
|
233
324
|
);
|
|
234
325
|
|
|
235
326
|
const zoomStep = React.useCallback(
|
|
236
327
|
(direction: 1 | -1) => {
|
|
237
328
|
const centre = { x: viewportSize.width / 2, y: viewportSize.height / 2 };
|
|
238
|
-
const
|
|
239
|
-
apply(zoomAt(
|
|
329
|
+
const from = current();
|
|
330
|
+
apply(zoomAt(from, stepScale(from.scale, direction) / from.scale, centre));
|
|
331
|
+
},
|
|
332
|
+
[apply, current, viewportSize],
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
const zoomTo = React.useCallback(
|
|
336
|
+
(scale: number) => {
|
|
337
|
+
const sizes = measured.current;
|
|
338
|
+
apply(centered(clampScale(scale), sizes.contentSize, sizes.viewportSize, sizes.room));
|
|
240
339
|
},
|
|
241
|
-
[apply,
|
|
340
|
+
[apply, measured],
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
const zoom = React.useCallback(
|
|
344
|
+
(): CanvasZoom => ({ scale: current().scale, fitted: requested.current.kind === "fit" }),
|
|
345
|
+
[current],
|
|
242
346
|
);
|
|
243
347
|
|
|
244
348
|
const actualSize = React.useCallback(() => {
|
|
245
|
-
apply(centered(1, contentSize, viewportSize));
|
|
246
|
-
}, [apply, contentSize, viewportSize]);
|
|
349
|
+
apply(centered(1, contentSize, viewportSize, room));
|
|
350
|
+
}, [apply, contentSize, viewportSize, room]);
|
|
247
351
|
|
|
248
|
-
const hold = React.useCallback(() =>
|
|
352
|
+
const hold = React.useCallback(() => move(current()), [move, current]);
|
|
249
353
|
|
|
250
354
|
const pan = React.useCallback(
|
|
251
|
-
(dx: number, dy: number) =>
|
|
252
|
-
[
|
|
355
|
+
(dx: number, dy: number) => move(panBy(current(), dx, dy)),
|
|
356
|
+
[move, current],
|
|
253
357
|
);
|
|
254
358
|
|
|
255
|
-
React.useImperativeHandle(ref, () => ({ zoomStep, actualSize, fit, hold, pan }), [
|
|
359
|
+
React.useImperativeHandle(ref, () => ({ zoomStep, zoomTo, zoom, actualSize, fit, hold, pan }), [
|
|
256
360
|
zoomStep,
|
|
361
|
+
zoomTo,
|
|
362
|
+
zoom,
|
|
257
363
|
actualSize,
|
|
258
364
|
fit,
|
|
259
365
|
hold,
|
|
@@ -264,6 +370,18 @@ export const Canvas = ({
|
|
|
264
370
|
onZoom?.({ scale: transform.scale, fitted: mode.kind === "fit" });
|
|
265
371
|
}, [onZoom, transform.scale, mode.kind]);
|
|
266
372
|
|
|
373
|
+
const lastViewport = React.useRef(viewportSize);
|
|
374
|
+
React.useLayoutEffect(() => {
|
|
375
|
+
const before = lastViewport.current;
|
|
376
|
+
lastViewport.current = viewportSize;
|
|
377
|
+
if (sameSize(before, viewportSize)) return;
|
|
378
|
+
const asked = requested.current;
|
|
379
|
+
if (asked.kind !== "manual" || before.width <= 0 || before.height <= 0) return;
|
|
380
|
+
const { contentSize: size, room: margin } = measured.current;
|
|
381
|
+
if (asked.followsFit) fit();
|
|
382
|
+
else apply(centered(asked.transform.scale, size, viewportSize, margin));
|
|
383
|
+
}, [viewportSize, measured, fit, apply]);
|
|
384
|
+
|
|
267
385
|
React.useEffect(() => {
|
|
268
386
|
if (lastFitKey.current === fitKey) return;
|
|
269
387
|
lastFitKey.current = fitKey;
|
|
@@ -278,16 +396,17 @@ export const Canvas = ({
|
|
|
278
396
|
zoomWheel(event, toViewport(event.clientX, event.clientY));
|
|
279
397
|
return;
|
|
280
398
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
399
|
+
if (contained) return;
|
|
400
|
+
const ongoing = wheelGesture();
|
|
401
|
+
ongoing.pans ??= PANS_BOTH;
|
|
402
|
+
if (!ongoing.pans.x && !ongoing.pans.y) return;
|
|
284
403
|
event.preventDefault();
|
|
285
|
-
const delta = panDelta(event,
|
|
286
|
-
|
|
404
|
+
const delta = panDelta(event, ongoing.pans);
|
|
405
|
+
move(panBy(current(), delta.x, delta.y));
|
|
287
406
|
};
|
|
288
407
|
viewport.addEventListener("wheel", onWheel, { passive: false });
|
|
289
408
|
return () => viewport.removeEventListener("wheel", onWheel);
|
|
290
|
-
}, [viewport,
|
|
409
|
+
}, [viewport, contained, move, current, toViewport, zoomWheel, wheelGesture]);
|
|
291
410
|
|
|
292
411
|
React.useEffect(() => {
|
|
293
412
|
if (!content) return undefined;
|
|
@@ -303,25 +422,26 @@ export const Canvas = ({
|
|
|
303
422
|
if (isZoomGesture(event)) {
|
|
304
423
|
gesture.current = null;
|
|
305
424
|
const rect = frame.getBoundingClientRect();
|
|
306
|
-
const scale =
|
|
425
|
+
const scale = current().scale;
|
|
307
426
|
zoomWheel(
|
|
308
427
|
event,
|
|
309
428
|
toViewport(rect.left + event.clientX * scale, rect.top + event.clientY * scale),
|
|
310
429
|
);
|
|
311
430
|
return;
|
|
312
431
|
}
|
|
313
|
-
|
|
432
|
+
if (contained) return;
|
|
433
|
+
const ongoing = wheelGesture();
|
|
314
434
|
if (event.defaultPrevented) return;
|
|
315
|
-
if (!
|
|
435
|
+
if (!ongoing.pans) {
|
|
316
436
|
const pixels = wheelPixels(event);
|
|
317
|
-
|
|
318
|
-
if (Math.hypot(
|
|
319
|
-
|
|
437
|
+
ongoing.sum = { x: ongoing.sum.x + pixels.x, y: ongoing.sum.y + pixels.y };
|
|
438
|
+
if (Math.hypot(ongoing.sum.x, ongoing.sum.y) < WHEEL_GESTURE_DECIDE_PX) return;
|
|
439
|
+
ongoing.pans = ownerOverPage(ongoing.sum, pageClaimedAxes(event));
|
|
320
440
|
}
|
|
321
|
-
if (!
|
|
441
|
+
if (!ongoing.pans.x && !ongoing.pans.y) return;
|
|
322
442
|
event.preventDefault();
|
|
323
|
-
const delta = panDelta(event,
|
|
324
|
-
|
|
443
|
+
const delta = panDelta(event, ongoing.pans);
|
|
444
|
+
move(panBy(current(), delta.x, delta.y));
|
|
325
445
|
};
|
|
326
446
|
let press: { at: Point; panning: boolean } | null = null;
|
|
327
447
|
let grabStyle: HTMLStyleElement | null = null;
|
|
@@ -351,7 +471,7 @@ export const Canvas = ({
|
|
|
351
471
|
grab(true);
|
|
352
472
|
}
|
|
353
473
|
event.preventDefault();
|
|
354
|
-
|
|
474
|
+
move(panBy(current(), point.x - press.at.x, point.y - press.at.y));
|
|
355
475
|
press.at = point;
|
|
356
476
|
};
|
|
357
477
|
const onPointerUp = () => {
|
|
@@ -378,11 +498,11 @@ export const Canvas = ({
|
|
|
378
498
|
content.removeEventListener("load", onLoad, true);
|
|
379
499
|
attached.forEach((detach) => detach());
|
|
380
500
|
};
|
|
381
|
-
}, [content,
|
|
501
|
+
}, [content, contained, move, current, toViewport, zoomWheel, wheelGesture]);
|
|
382
502
|
|
|
383
503
|
React.useEffect(() => {
|
|
384
504
|
const onKey = (event: KeyboardEvent) => {
|
|
385
|
-
if (!(event.metaKey || event.ctrlKey) || isTyping(event.target)) return;
|
|
505
|
+
if (!(event.metaKey || event.ctrlKey) || isTyping(event.target) || !engaged.current) return;
|
|
386
506
|
if (event.key === "=" || event.key === "+") zoomStep(1);
|
|
387
507
|
else if (event.key === "-") zoomStep(-1);
|
|
388
508
|
else if (event.key === "0") fit();
|
|
@@ -392,7 +512,7 @@ export const Canvas = ({
|
|
|
392
512
|
};
|
|
393
513
|
window.addEventListener("keydown", onKey);
|
|
394
514
|
return () => window.removeEventListener("keydown", onKey);
|
|
395
|
-
}, [zoomStep, fit, actualSize]);
|
|
515
|
+
}, [zoomStep, fit, actualSize, engaged]);
|
|
396
516
|
|
|
397
517
|
const onPointerDown = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
398
518
|
if (event.pointerType === "mouse" && event.button !== 0 && event.button !== 1) return;
|
|
@@ -420,7 +540,7 @@ export const Canvas = ({
|
|
|
420
540
|
const [a, b] = [...pointers.current.values()];
|
|
421
541
|
const next: Pinch = { span: distance(a, b), midpoint: midpointOf(a, b) };
|
|
422
542
|
const zoomed = zoomAt(
|
|
423
|
-
|
|
543
|
+
current(),
|
|
424
544
|
next.span / pinch.current.span,
|
|
425
545
|
toViewport(pinch.current.midpoint.x, pinch.current.midpoint.y),
|
|
426
546
|
);
|
|
@@ -435,7 +555,10 @@ export const Canvas = ({
|
|
|
435
555
|
return;
|
|
436
556
|
}
|
|
437
557
|
|
|
438
|
-
|
|
558
|
+
const asked = requested.current;
|
|
559
|
+
const fitted = asked.kind === "fit" || asked.followsFit;
|
|
560
|
+
if (contained && event.pointerType !== "mouse" && fitted) return;
|
|
561
|
+
move(panBy(current(), point.x - previous.x, point.y - previous.y));
|
|
439
562
|
};
|
|
440
563
|
|
|
441
564
|
const onPointerUp = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
@@ -446,11 +569,17 @@ export const Canvas = ({
|
|
|
446
569
|
|
|
447
570
|
const cursor = dragging ? "cursor-grabbing" : handTool ? "cursor-grab" : "";
|
|
448
571
|
const framesInert = handTool || dragging ? "[&_iframe]:pointer-events-none" : "";
|
|
572
|
+
const touch = !contained
|
|
573
|
+
? "touch-none"
|
|
574
|
+
: mode.kind === "fit" || mode.followsFit
|
|
575
|
+
? "touch-pan-y"
|
|
576
|
+
: "touch-none pointer-coarse:[&_iframe]:pointer-events-none";
|
|
449
577
|
|
|
450
578
|
return (
|
|
451
579
|
<div
|
|
452
|
-
className={`relative min-h-0 min-w-0 flex-1 overflow-hidden touch
|
|
580
|
+
className={`relative min-h-0 min-w-0 flex-1 overflow-hidden ${touch} select-none ${cursor} ${framesInert}`}
|
|
453
581
|
data-testid="canvas"
|
|
582
|
+
data-contained={contained || undefined}
|
|
454
583
|
data-panning={handTool || undefined}
|
|
455
584
|
data-dragging={dragging || undefined}
|
|
456
585
|
ref={setViewport}
|
package/src/index.ts
CHANGED
|
@@ -13,8 +13,23 @@ export { Tooltip } from "./components/Tooltip";
|
|
|
13
13
|
export { TopBar } from "./components/chrome/TopBar";
|
|
14
14
|
export { VariablesPanel } from "./components/panels/VariablesPanel";
|
|
15
15
|
export { Segmented, Select, Switch } from "./components/controls";
|
|
16
|
-
export {
|
|
17
|
-
|
|
16
|
+
export {
|
|
17
|
+
Canvas,
|
|
18
|
+
useCanvasScale,
|
|
19
|
+
type CanvasHandle,
|
|
20
|
+
type CanvasZoom,
|
|
21
|
+
} from "./components/preview/Canvas";
|
|
22
|
+
export { percent } from "./lib/canvas";
|
|
23
|
+
export {
|
|
24
|
+
chromeFor,
|
|
25
|
+
DEFAULT_DEVICE_ID,
|
|
26
|
+
deviceById,
|
|
27
|
+
deviceVariablesFor,
|
|
28
|
+
isWebOnly,
|
|
29
|
+
STUDIO_DEVICES,
|
|
30
|
+
webViewportFor,
|
|
31
|
+
} from "./lib/devices";
|
|
32
|
+
export type { DeviceChromeSpec, SafeAreaInsets, StudioDevice } from "./lib/devices";
|
|
18
33
|
export { hostStateFor, studioDefaults } from "./lib/host";
|
|
19
34
|
export { hasActions } from "./lib/surfaces";
|
|
20
35
|
export { devServerActions } from "./lib/actions";
|
package/src/lib/canvas.ts
CHANGED
|
@@ -4,33 +4,44 @@ export type Size = { width: number; height: number };
|
|
|
4
4
|
|
|
5
5
|
export const MIN_SCALE = 0.1;
|
|
6
6
|
export const MAX_SCALE = 4;
|
|
7
|
-
export
|
|
7
|
+
export type Inset = { top: number; right: number; bottom: number; left: number };
|
|
8
|
+
|
|
9
|
+
export const FIT_INSET: Inset = { top: 48, right: 48, bottom: 48, left: 48 };
|
|
8
10
|
|
|
9
11
|
const STEP_FACTOR = 1.25;
|
|
10
12
|
|
|
11
13
|
export const clampScale = (scale: number): number =>
|
|
12
14
|
Math.min(MAX_SCALE, Math.max(MIN_SCALE, scale));
|
|
13
15
|
|
|
14
|
-
export const fitTransform = (
|
|
16
|
+
export const fitTransform = (
|
|
17
|
+
content: Size,
|
|
18
|
+
viewport: Size,
|
|
19
|
+
inset: Inset = FIT_INSET,
|
|
20
|
+
): CanvasTransform => {
|
|
15
21
|
if (content.width <= 0 || content.height <= 0 || viewport.width <= 0 || viewport.height <= 0) {
|
|
16
22
|
return { scale: 1, x: 0, y: 0 };
|
|
17
23
|
}
|
|
18
|
-
const room = insetRoom(viewport);
|
|
24
|
+
const room = insetRoom(viewport, inset);
|
|
19
25
|
const scale = clampScale(Math.min(1, room.width / content.width, room.height / content.height));
|
|
20
|
-
return centered(scale, content, viewport);
|
|
26
|
+
return centered(scale, content, viewport, inset);
|
|
21
27
|
};
|
|
22
28
|
|
|
23
|
-
const insetRoom = (viewport: Size): Size => ({
|
|
24
|
-
width: viewport.width -
|
|
25
|
-
height: viewport.height -
|
|
29
|
+
const insetRoom = (viewport: Size, inset: Inset): Size => ({
|
|
30
|
+
width: viewport.width - inset.left - inset.right,
|
|
31
|
+
height: viewport.height - inset.top - inset.bottom,
|
|
26
32
|
});
|
|
27
33
|
|
|
28
|
-
export const centered = (
|
|
29
|
-
|
|
34
|
+
export const centered = (
|
|
35
|
+
scale: number,
|
|
36
|
+
content: Size,
|
|
37
|
+
viewport: Size,
|
|
38
|
+
inset: Inset = FIT_INSET,
|
|
39
|
+
): CanvasTransform => {
|
|
40
|
+
const room = insetRoom(viewport, inset);
|
|
30
41
|
return {
|
|
31
42
|
scale,
|
|
32
|
-
x:
|
|
33
|
-
y:
|
|
43
|
+
x: inset.left + (room.width - content.width * scale) / 2,
|
|
44
|
+
y: inset.top + (room.height - content.height * scale) / 2,
|
|
34
45
|
};
|
|
35
46
|
};
|
|
36
47
|
|
|
@@ -24,9 +24,7 @@ import {
|
|
|
24
24
|
RotateDeviceIcon,
|
|
25
25
|
RotateIcon,
|
|
26
26
|
SlidersIcon,
|
|
27
|
-
SmartphoneIcon,
|
|
28
27
|
SunIcon,
|
|
29
|
-
CompassIcon,
|
|
30
28
|
} from "../components/icons";
|
|
31
29
|
import { Tooltip } from "../components/Tooltip";
|
|
32
30
|
import { SidePanel } from "../components/panels/SidePanel";
|
|
@@ -133,7 +131,6 @@ const StagePane = ({
|
|
|
133
131
|
onResize,
|
|
134
132
|
canvas,
|
|
135
133
|
browser,
|
|
136
|
-
place,
|
|
137
134
|
}: {
|
|
138
135
|
label?: string;
|
|
139
136
|
width: number;
|
|
@@ -152,7 +149,6 @@ const StagePane = ({
|
|
|
152
149
|
onResize?: (next: FrameSize) => void;
|
|
153
150
|
canvas?: React.RefObject<CanvasHandle | null>;
|
|
154
151
|
browser?: BrowserLayout;
|
|
155
|
-
place?: PreviewPlace;
|
|
156
152
|
}) => {
|
|
157
153
|
const scale = useCanvasScale();
|
|
158
154
|
const [broken, setBroken] = React.useState(false);
|
|
@@ -168,14 +164,6 @@ const StagePane = ({
|
|
|
168
164
|
</span>
|
|
169
165
|
)}
|
|
170
166
|
<div className="relative">
|
|
171
|
-
{place?.teleported && (
|
|
172
|
-
<div
|
|
173
|
-
className="absolute right-0 bottom-full mb-2 origin-bottom-right"
|
|
174
|
-
style={{ transform: `scale(${1 / scale})` }}
|
|
175
|
-
>
|
|
176
|
-
<PlaceBadge place={place} />
|
|
177
|
-
</div>
|
|
178
|
-
)}
|
|
179
167
|
{image && !broken ? (
|
|
180
168
|
<div
|
|
181
169
|
className="flex flex-none items-center justify-center overflow-hidden bg-black"
|
|
@@ -225,25 +213,6 @@ const StagePane = ({
|
|
|
225
213
|
);
|
|
226
214
|
};
|
|
227
215
|
|
|
228
|
-
const PlaceBadge = ({ place }: { place: PreviewPlace }) => {
|
|
229
|
-
const browser = place.where === "browser";
|
|
230
|
-
const label = browser ? "In browser" : "In app";
|
|
231
|
-
return (
|
|
232
|
-
<span
|
|
233
|
-
role="status"
|
|
234
|
-
aria-label={label}
|
|
235
|
-
className={`inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-colors duration-hover ${
|
|
236
|
-
browser
|
|
237
|
-
? "border-primary/30 bg-primary/10 text-primary"
|
|
238
|
-
: "border-border bg-background text-fg-secondary"
|
|
239
|
-
}`}
|
|
240
|
-
>
|
|
241
|
-
{browser ? <CompassIcon /> : <SmartphoneIcon />}
|
|
242
|
-
{label}
|
|
243
|
-
</span>
|
|
244
|
-
);
|
|
245
|
-
};
|
|
246
|
-
|
|
247
216
|
const CanvasFooter = ({ children }: React.PropsWithChildren) => {
|
|
248
217
|
const scale = useCanvasScale();
|
|
249
218
|
return (
|
|
@@ -657,7 +626,6 @@ export const SurfaceEditor = ({
|
|
|
657
626
|
landscape={landscape}
|
|
658
627
|
theme={theme}
|
|
659
628
|
browser={browserFor(comparing.place, compareWeb)}
|
|
660
|
-
place={comparing.place}
|
|
661
629
|
/>
|
|
662
630
|
)}
|
|
663
631
|
<StagePane
|
|
@@ -674,7 +642,6 @@ export const SurfaceEditor = ({
|
|
|
674
642
|
landscape={landscape}
|
|
675
643
|
theme={theme}
|
|
676
644
|
browser={browserFor(local.place, web)}
|
|
677
|
-
place={local.place}
|
|
678
645
|
onResize={
|
|
679
646
|
responsive
|
|
680
647
|
? (next) =>
|