@superwall/studio 1.3.0 → 1.5.0
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 +2 -2
- package/dist/assets/index-Bi2YHYVJ.js +11 -0
- package/dist/assets/index-DrsTCwot.css +1 -0
- package/dist/index.html +2 -2
- package/dist/lib/components/DownloadScreenshot.d.ts +6 -0
- package/dist/lib/components/PreviewOnDevice.d.ts +6 -0
- package/dist/lib/components/Tooltip.d.ts +5 -0
- package/dist/lib/components/chrome/Toolbar.d.ts +1 -0
- package/dist/lib/components/chrome/TopBar.d.ts +1 -0
- package/dist/lib/components/icons.d.ts +1 -0
- package/dist/lib/components/menus/CompareMenu.d.ts +2 -1
- package/dist/lib/components/preview/Canvas.d.ts +18 -0
- package/dist/lib/components/preview/DeviceFrame.d.ts +4 -4
- package/dist/lib/components/preview/ResizeHandles.d.ts +13 -0
- package/dist/lib/index.d.ts +5 -1
- package/dist/lib/index.js +2924 -1229
- package/dist/lib/lib/actions.d.ts +1 -0
- package/dist/lib/lib/canvas.d.ts +27 -0
- package/dist/lib/lib/contract.d.ts +7 -1
- package/dist/lib/lib/devices.d.ts +9 -0
- package/dist/lib/lib/host.d.ts +2 -1
- package/dist/lib/lib/notify.d.ts +4 -0
- package/dist/lib/lib/presentation.d.ts +23 -0
- package/dist/lib/lib/preview.d.ts +1 -0
- package/dist/lib/lib/project.d.ts +1 -0
- package/dist/lib/lib/scheme.d.ts +11 -0
- package/dist/lib/lib/surfaces.d.ts +26 -2
- package/dist/lib/studio.css +1 -1
- package/package.json +3 -2
- package/src/components/DownloadScreenshot.tsx +133 -0
- package/src/components/OpenInApp.tsx +5 -25
- package/src/components/PaywallToasts.tsx +5 -3
- package/src/components/PreviewOnDevice.tsx +133 -0
- package/src/components/SurfaceCard.tsx +1 -1
- package/src/components/Tooltip.tsx +27 -0
- package/src/components/chrome/Toolbar.tsx +47 -6
- package/src/components/chrome/TopBar.tsx +37 -3
- package/src/components/icons.tsx +6 -0
- package/src/components/menus/ActionMenu.tsx +8 -8
- package/src/components/menus/CompareMenu.tsx +49 -30
- package/src/components/panels/MissingProducts.tsx +1 -1
- package/src/components/preview/Canvas.tsx +426 -0
- package/src/components/preview/DeviceFrame.tsx +83 -74
- package/src/components/preview/ResizeHandles.tsx +205 -0
- package/src/index.ts +6 -0
- package/src/lib/actions.ts +7 -0
- package/src/lib/canvas.ts +60 -0
- package/src/lib/contract.ts +8 -1
- package/src/lib/devices.ts +52 -4
- package/src/lib/host.ts +7 -2
- package/src/lib/notify.tsx +31 -0
- package/src/lib/presentation.ts +102 -0
- package/src/lib/preview.ts +3 -0
- package/src/lib/project.ts +45 -1
- package/src/lib/scheme.ts +31 -0
- package/src/lib/surfaces.ts +42 -1
- package/src/main.tsx +5 -1
- package/src/screens/SurfaceEditor.tsx +219 -52
- package/src/screens/SurfaceList.tsx +2 -2
- package/src/styles.css +89 -8
- package/dist/assets/index-CEz-HnIH.js +0 -11
- package/dist/assets/index-_HCp6gCi.css +0 -1
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import {
|
|
3
|
+
centered,
|
|
4
|
+
fitTransform,
|
|
5
|
+
panBy,
|
|
6
|
+
stepScale,
|
|
7
|
+
zoomAt,
|
|
8
|
+
type CanvasTransform,
|
|
9
|
+
type Size,
|
|
10
|
+
} from "../../lib/canvas";
|
|
11
|
+
|
|
12
|
+
const WHEEL_ZOOM_SENSITIVITY = 0.01;
|
|
13
|
+
const WHEEL_ZOOM_STEP_LIMIT = { min: 0.6, max: 1.6 };
|
|
14
|
+
const DRAG_THRESHOLD = 4;
|
|
15
|
+
const GRAB_STYLE_ATTRIBUTE = "data-studio-grab";
|
|
16
|
+
|
|
17
|
+
type Point = { x: number; y: number };
|
|
18
|
+
|
|
19
|
+
type Mode = { kind: "fit" } | { kind: "manual"; transform: CanvasTransform };
|
|
20
|
+
|
|
21
|
+
type Pinch = { span: number; midpoint: Point };
|
|
22
|
+
|
|
23
|
+
const CanvasScaleContext = React.createContext(1);
|
|
24
|
+
|
|
25
|
+
export const useCanvasScale = (): number => React.useContext(CanvasScaleContext);
|
|
26
|
+
|
|
27
|
+
const distance = (a: Point, b: Point): number => Math.hypot(a.x - b.x, a.y - b.y);
|
|
28
|
+
|
|
29
|
+
const midpointOf = (a: Point, b: Point): Point => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 });
|
|
30
|
+
|
|
31
|
+
const wheelZoomFactor = (deltaY: number): number =>
|
|
32
|
+
Math.min(
|
|
33
|
+
WHEEL_ZOOM_STEP_LIMIT.max,
|
|
34
|
+
Math.max(WHEEL_ZOOM_STEP_LIMIT.min, Math.exp(-deltaY * WHEEL_ZOOM_SENSITIVITY)),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const isZoomGesture = (event: WheelEvent): boolean => event.ctrlKey || event.metaKey;
|
|
38
|
+
|
|
39
|
+
const swallow = (event: Event) => {
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
event.stopPropagation();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const isTyping = (target: EventTarget | null): boolean =>
|
|
45
|
+
target instanceof HTMLElement &&
|
|
46
|
+
(target.isContentEditable || /^(input|textarea|select)$/i.test(target.tagName));
|
|
47
|
+
|
|
48
|
+
const sameOriginWindow = (frame: HTMLIFrameElement): Window | null => {
|
|
49
|
+
try {
|
|
50
|
+
return frame.contentDocument ? frame.contentWindow : null;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const isElement = (target: EventTarget | null): target is Element =>
|
|
57
|
+
typeof target === "object" && target !== null && (target as Node).nodeType === 1;
|
|
58
|
+
|
|
59
|
+
type StyleKey = "overflowX" | "overflowY" | "overscrollBehaviorX" | "overscrollBehaviorY";
|
|
60
|
+
|
|
61
|
+
const styleOf = (element: Element, key: StyleKey): string => {
|
|
62
|
+
const computed = element.ownerDocument.defaultView?.getComputedStyle(element)[key];
|
|
63
|
+
return computed || ("style" in element ? (element as HTMLElement).style[key] : "");
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const claimsWheel = (element: Element, axis: "x" | "y"): boolean => {
|
|
67
|
+
const overflow = styleOf(element, axis === "y" ? "overflowY" : "overflowX");
|
|
68
|
+
const overscroll = styleOf(element, axis === "y" ? "overscrollBehaviorY" : "overscrollBehaviorX");
|
|
69
|
+
if (/(contain|none)/.test(overscroll)) return true;
|
|
70
|
+
const overflowing =
|
|
71
|
+
(axis === "y"
|
|
72
|
+
? element.scrollHeight - element.clientHeight
|
|
73
|
+
: element.scrollWidth - element.clientWidth) > 0;
|
|
74
|
+
if (!overflowing) return false;
|
|
75
|
+
return (
|
|
76
|
+
/(auto|scroll|hidden|clip)/.test(overflow) || element === element.ownerDocument.scrollingElement
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const pageClaimsWheel = (event: WheelEvent): boolean => {
|
|
81
|
+
if (event.defaultPrevented) return true;
|
|
82
|
+
const axis = Math.abs(event.deltaX) > Math.abs(event.deltaY) ? "x" : "y";
|
|
83
|
+
for (let node = isElement(event.target) ? event.target : null; node; node = node.parentElement) {
|
|
84
|
+
if (claimsWheel(node, axis)) return true;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const useSize = (element: HTMLElement | null): Size => {
|
|
90
|
+
const [size, setSize] = React.useState<Size>({ width: 0, height: 0 });
|
|
91
|
+
|
|
92
|
+
React.useLayoutEffect(() => {
|
|
93
|
+
if (!element) return undefined;
|
|
94
|
+
const measure = () => setSize({ width: element.offsetWidth, height: element.offsetHeight });
|
|
95
|
+
measure();
|
|
96
|
+
if (typeof ResizeObserver === "undefined") return undefined;
|
|
97
|
+
const observer = new ResizeObserver(measure);
|
|
98
|
+
observer.observe(element);
|
|
99
|
+
return () => observer.disconnect();
|
|
100
|
+
}, [element]);
|
|
101
|
+
|
|
102
|
+
return size;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const useLatest = <T,>(value: T): React.RefObject<T> => {
|
|
106
|
+
const ref = React.useRef(value);
|
|
107
|
+
ref.current = value;
|
|
108
|
+
return ref;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const useSpaceHeld = (): boolean => {
|
|
112
|
+
const [held, setHeld] = React.useState(false);
|
|
113
|
+
|
|
114
|
+
React.useEffect(() => {
|
|
115
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
116
|
+
if (event.code !== "Space" || event.repeat || isTyping(event.target)) return;
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
setHeld(true);
|
|
119
|
+
};
|
|
120
|
+
const onKeyUp = (event: KeyboardEvent) => {
|
|
121
|
+
if (event.code === "Space") setHeld(false);
|
|
122
|
+
};
|
|
123
|
+
const release = () => setHeld(false);
|
|
124
|
+
window.addEventListener("keydown", onKeyDown);
|
|
125
|
+
window.addEventListener("keyup", onKeyUp);
|
|
126
|
+
window.addEventListener("blur", release);
|
|
127
|
+
return () => {
|
|
128
|
+
window.removeEventListener("keydown", onKeyDown);
|
|
129
|
+
window.removeEventListener("keyup", onKeyUp);
|
|
130
|
+
window.removeEventListener("blur", release);
|
|
131
|
+
};
|
|
132
|
+
}, []);
|
|
133
|
+
|
|
134
|
+
return held;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export type CanvasZoom = { scale: number; fitted: boolean };
|
|
138
|
+
|
|
139
|
+
export type CanvasHandle = {
|
|
140
|
+
zoomStep: (direction: 1 | -1) => void;
|
|
141
|
+
actualSize: () => void;
|
|
142
|
+
fit: () => void;
|
|
143
|
+
hold: () => void;
|
|
144
|
+
pan: (dx: number, dy: number) => void;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export const Canvas = ({
|
|
148
|
+
fitKey,
|
|
149
|
+
ref,
|
|
150
|
+
onZoom,
|
|
151
|
+
children,
|
|
152
|
+
}: React.PropsWithChildren<{
|
|
153
|
+
fitKey: string;
|
|
154
|
+
ref?: React.Ref<CanvasHandle>;
|
|
155
|
+
onZoom?: (zoom: CanvasZoom) => void;
|
|
156
|
+
}>) => {
|
|
157
|
+
const [viewport, setViewport] = React.useState<HTMLDivElement | null>(null);
|
|
158
|
+
const [content, setContent] = React.useState<HTMLDivElement | null>(null);
|
|
159
|
+
const [mode, setMode] = React.useState<Mode>({ kind: "fit" });
|
|
160
|
+
const [dragging, setDragging] = React.useState(false);
|
|
161
|
+
const viewportSize = useSize(viewport);
|
|
162
|
+
const contentSize = useSize(content);
|
|
163
|
+
const handTool = useSpaceHeld();
|
|
164
|
+
|
|
165
|
+
const transform = mode.kind === "fit" ? fitTransform(contentSize, viewportSize) : mode.transform;
|
|
166
|
+
const latest = useLatest(transform);
|
|
167
|
+
const pointers = React.useRef(new Map<number, Point>());
|
|
168
|
+
const pinch = React.useRef<Pinch | null>(null);
|
|
169
|
+
const lastFitKey = React.useRef(fitKey);
|
|
170
|
+
|
|
171
|
+
const apply = React.useCallback((next: CanvasTransform) => {
|
|
172
|
+
setMode({ kind: "manual", transform: next });
|
|
173
|
+
}, []);
|
|
174
|
+
|
|
175
|
+
const fit = React.useCallback(() => setMode({ kind: "fit" }), []);
|
|
176
|
+
|
|
177
|
+
const toViewport = React.useCallback(
|
|
178
|
+
(clientX: number, clientY: number): Point => {
|
|
179
|
+
const rect = viewport?.getBoundingClientRect();
|
|
180
|
+
return { x: clientX - (rect?.left ?? 0), y: clientY - (rect?.top ?? 0) };
|
|
181
|
+
},
|
|
182
|
+
[viewport],
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const zoomWheel = React.useCallback(
|
|
186
|
+
(event: WheelEvent, at: Point) => {
|
|
187
|
+
event.preventDefault();
|
|
188
|
+
apply(zoomAt(latest.current, wheelZoomFactor(event.deltaY), at));
|
|
189
|
+
},
|
|
190
|
+
[apply, latest],
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
const zoomStep = React.useCallback(
|
|
194
|
+
(direction: 1 | -1) => {
|
|
195
|
+
const centre = { x: viewportSize.width / 2, y: viewportSize.height / 2 };
|
|
196
|
+
const next = stepScale(latest.current.scale, direction);
|
|
197
|
+
apply(zoomAt(latest.current, next / latest.current.scale, centre));
|
|
198
|
+
},
|
|
199
|
+
[apply, latest, viewportSize],
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const actualSize = React.useCallback(() => {
|
|
203
|
+
apply(centered(1, contentSize, viewportSize));
|
|
204
|
+
}, [apply, contentSize, viewportSize]);
|
|
205
|
+
|
|
206
|
+
const hold = React.useCallback(() => apply(latest.current), [apply, latest]);
|
|
207
|
+
|
|
208
|
+
const pan = React.useCallback(
|
|
209
|
+
(dx: number, dy: number) => apply(panBy(latest.current, dx, dy)),
|
|
210
|
+
[apply, latest],
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
React.useImperativeHandle(ref, () => ({ zoomStep, actualSize, fit, hold, pan }), [
|
|
214
|
+
zoomStep,
|
|
215
|
+
actualSize,
|
|
216
|
+
fit,
|
|
217
|
+
hold,
|
|
218
|
+
pan,
|
|
219
|
+
]);
|
|
220
|
+
|
|
221
|
+
React.useEffect(() => {
|
|
222
|
+
onZoom?.({ scale: transform.scale, fitted: mode.kind === "fit" });
|
|
223
|
+
}, [onZoom, transform.scale, mode.kind]);
|
|
224
|
+
|
|
225
|
+
React.useEffect(() => {
|
|
226
|
+
if (lastFitKey.current === fitKey) return;
|
|
227
|
+
lastFitKey.current = fitKey;
|
|
228
|
+
fit();
|
|
229
|
+
}, [fitKey, fit]);
|
|
230
|
+
|
|
231
|
+
React.useEffect(() => {
|
|
232
|
+
if (!viewport) return undefined;
|
|
233
|
+
const onWheel = (event: WheelEvent) => {
|
|
234
|
+
if (isZoomGesture(event)) {
|
|
235
|
+
zoomWheel(event, toViewport(event.clientX, event.clientY));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
event.preventDefault();
|
|
239
|
+
apply(panBy(latest.current, -event.deltaX, -event.deltaY));
|
|
240
|
+
};
|
|
241
|
+
viewport.addEventListener("wheel", onWheel, { passive: false });
|
|
242
|
+
return () => viewport.removeEventListener("wheel", onWheel);
|
|
243
|
+
}, [viewport, apply, latest, toViewport, zoomWheel]);
|
|
244
|
+
|
|
245
|
+
React.useEffect(() => {
|
|
246
|
+
if (!content) return undefined;
|
|
247
|
+
const attached = new Map<HTMLIFrameElement, () => void>();
|
|
248
|
+
const attach = (frame: HTMLIFrameElement) => {
|
|
249
|
+
attached.get(frame)?.();
|
|
250
|
+
attached.delete(frame);
|
|
251
|
+
const target = sameOriginWindow(frame);
|
|
252
|
+
if (!target) return;
|
|
253
|
+
const onWheel = (event: WheelEvent) => {
|
|
254
|
+
if (isZoomGesture(event)) {
|
|
255
|
+
const rect = frame.getBoundingClientRect();
|
|
256
|
+
const scale = latest.current.scale;
|
|
257
|
+
zoomWheel(
|
|
258
|
+
event,
|
|
259
|
+
toViewport(rect.left + event.clientX * scale, rect.top + event.clientY * scale),
|
|
260
|
+
);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (pageClaimsWheel(event)) return;
|
|
264
|
+
event.preventDefault();
|
|
265
|
+
apply(panBy(latest.current, -event.deltaX, -event.deltaY));
|
|
266
|
+
};
|
|
267
|
+
let press: { at: Point; panning: boolean } | null = null;
|
|
268
|
+
let grabStyle: HTMLStyleElement | null = null;
|
|
269
|
+
const grab = (on: boolean) => {
|
|
270
|
+
const doc = frame.contentDocument;
|
|
271
|
+
if (!doc) return;
|
|
272
|
+
grabStyle?.remove();
|
|
273
|
+
grabStyle = null;
|
|
274
|
+
if (!on) return;
|
|
275
|
+
grabStyle = doc.createElement("style");
|
|
276
|
+
grabStyle.setAttribute(GRAB_STYLE_ATTRIBUTE, "");
|
|
277
|
+
grabStyle.textContent = "*, *::before, *::after { cursor: grabbing !important; }";
|
|
278
|
+
(doc.head ?? doc.documentElement).appendChild(grabStyle);
|
|
279
|
+
};
|
|
280
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
281
|
+
if (event.pointerType !== "mouse" || event.button !== 1) return;
|
|
282
|
+
event.preventDefault();
|
|
283
|
+
press = { at: { x: event.screenX, y: event.screenY }, panning: false };
|
|
284
|
+
};
|
|
285
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
286
|
+
if (!press) return;
|
|
287
|
+
const point = { x: event.screenX, y: event.screenY };
|
|
288
|
+
if (!press.panning) {
|
|
289
|
+
if (distance(press.at, point) < DRAG_THRESHOLD) return;
|
|
290
|
+
press.panning = true;
|
|
291
|
+
setDragging(true);
|
|
292
|
+
grab(true);
|
|
293
|
+
}
|
|
294
|
+
event.preventDefault();
|
|
295
|
+
apply(panBy(latest.current, point.x - press.at.x, point.y - press.at.y));
|
|
296
|
+
press.at = point;
|
|
297
|
+
};
|
|
298
|
+
const onPointerUp = () => {
|
|
299
|
+
if (press?.panning) {
|
|
300
|
+
setDragging(false);
|
|
301
|
+
grab(false);
|
|
302
|
+
target.addEventListener("click", swallow, { capture: true, once: true });
|
|
303
|
+
}
|
|
304
|
+
press = null;
|
|
305
|
+
};
|
|
306
|
+
target.addEventListener("wheel", onWheel, { passive: false });
|
|
307
|
+
target.addEventListener("pointerdown", onPointerDown);
|
|
308
|
+
target.addEventListener("pointermove", onPointerMove);
|
|
309
|
+
target.addEventListener("pointerup", onPointerUp);
|
|
310
|
+
target.addEventListener("pointercancel", onPointerUp);
|
|
311
|
+
attached.set(frame, () => {
|
|
312
|
+
target.removeEventListener("wheel", onWheel);
|
|
313
|
+
target.removeEventListener("pointerdown", onPointerDown);
|
|
314
|
+
target.removeEventListener("pointermove", onPointerMove);
|
|
315
|
+
target.removeEventListener("pointerup", onPointerUp);
|
|
316
|
+
target.removeEventListener("pointercancel", onPointerUp);
|
|
317
|
+
});
|
|
318
|
+
};
|
|
319
|
+
const onLoad = (event: Event) => {
|
|
320
|
+
if (event.target instanceof HTMLIFrameElement) attach(event.target);
|
|
321
|
+
};
|
|
322
|
+
content.querySelectorAll("iframe").forEach(attach);
|
|
323
|
+
content.addEventListener("load", onLoad, true);
|
|
324
|
+
return () => {
|
|
325
|
+
content.removeEventListener("load", onLoad, true);
|
|
326
|
+
attached.forEach((detach) => detach());
|
|
327
|
+
};
|
|
328
|
+
}, [content, apply, latest, toViewport, zoomWheel]);
|
|
329
|
+
|
|
330
|
+
React.useEffect(() => {
|
|
331
|
+
const onKey = (event: KeyboardEvent) => {
|
|
332
|
+
if (!(event.metaKey || event.ctrlKey) || isTyping(event.target)) return;
|
|
333
|
+
if (event.key === "=" || event.key === "+") zoomStep(1);
|
|
334
|
+
else if (event.key === "-") zoomStep(-1);
|
|
335
|
+
else if (event.key === "0") fit();
|
|
336
|
+
else if (event.key === "1") actualSize();
|
|
337
|
+
else return;
|
|
338
|
+
event.preventDefault();
|
|
339
|
+
};
|
|
340
|
+
window.addEventListener("keydown", onKey);
|
|
341
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
342
|
+
}, [zoomStep, fit, actualSize]);
|
|
343
|
+
|
|
344
|
+
const onPointerDown = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
345
|
+
if (event.pointerType === "mouse" && event.button !== 0 && event.button !== 1) return;
|
|
346
|
+
if (event.button === 1) event.preventDefault();
|
|
347
|
+
try {
|
|
348
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
349
|
+
} catch {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
pointers.current.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
353
|
+
setDragging(true);
|
|
354
|
+
if (pointers.current.size === 2) {
|
|
355
|
+
const [a, b] = [...pointers.current.values()];
|
|
356
|
+
pinch.current = { span: distance(a, b), midpoint: midpointOf(a, b) };
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const onPointerMove = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
361
|
+
const previous = pointers.current.get(event.pointerId);
|
|
362
|
+
if (!previous) return;
|
|
363
|
+
const point = { x: event.clientX, y: event.clientY };
|
|
364
|
+
pointers.current.set(event.pointerId, point);
|
|
365
|
+
|
|
366
|
+
if (pointers.current.size >= 2 && pinch.current) {
|
|
367
|
+
const [a, b] = [...pointers.current.values()];
|
|
368
|
+
const next: Pinch = { span: distance(a, b), midpoint: midpointOf(a, b) };
|
|
369
|
+
const zoomed = zoomAt(
|
|
370
|
+
latest.current,
|
|
371
|
+
next.span / pinch.current.span,
|
|
372
|
+
toViewport(pinch.current.midpoint.x, pinch.current.midpoint.y),
|
|
373
|
+
);
|
|
374
|
+
apply(
|
|
375
|
+
panBy(
|
|
376
|
+
zoomed,
|
|
377
|
+
next.midpoint.x - pinch.current.midpoint.x,
|
|
378
|
+
next.midpoint.y - pinch.current.midpoint.y,
|
|
379
|
+
),
|
|
380
|
+
);
|
|
381
|
+
pinch.current = next;
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
apply(panBy(latest.current, point.x - previous.x, point.y - previous.y));
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const onPointerUp = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
389
|
+
pointers.current.delete(event.pointerId);
|
|
390
|
+
if (pointers.current.size < 2) pinch.current = null;
|
|
391
|
+
if (pointers.current.size === 0) setDragging(false);
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
const cursor = dragging ? "cursor-grabbing" : handTool ? "cursor-grab" : "";
|
|
395
|
+
const framesInert = handTool || dragging ? "[&_iframe]:pointer-events-none" : "";
|
|
396
|
+
|
|
397
|
+
return (
|
|
398
|
+
<div
|
|
399
|
+
className={`relative min-h-0 min-w-0 flex-1 overflow-hidden touch-none select-none ${cursor} ${framesInert}`}
|
|
400
|
+
data-testid="canvas"
|
|
401
|
+
data-panning={handTool || undefined}
|
|
402
|
+
data-dragging={dragging || undefined}
|
|
403
|
+
ref={setViewport}
|
|
404
|
+
onPointerDown={onPointerDown}
|
|
405
|
+
onPointerMove={onPointerMove}
|
|
406
|
+
onPointerUp={onPointerUp}
|
|
407
|
+
onPointerCancel={onPointerUp}
|
|
408
|
+
onContextMenu={(event) => {
|
|
409
|
+
if (dragging) event.preventDefault();
|
|
410
|
+
}}
|
|
411
|
+
>
|
|
412
|
+
<div
|
|
413
|
+
className="absolute top-0 left-0 flex origin-top-left items-start gap-16"
|
|
414
|
+
data-testid="canvas-content"
|
|
415
|
+
ref={setContent}
|
|
416
|
+
style={{
|
|
417
|
+
transform: `translate(${transform.x}px, ${transform.y}px) scale(${transform.scale})`,
|
|
418
|
+
}}
|
|
419
|
+
>
|
|
420
|
+
<CanvasScaleContext.Provider value={transform.scale}>
|
|
421
|
+
{children}
|
|
422
|
+
</CanvasScaleContext.Provider>
|
|
423
|
+
</div>
|
|
424
|
+
</div>
|
|
425
|
+
);
|
|
426
|
+
};
|
|
@@ -3,64 +3,26 @@ import type { HostDelegate, HostState } from "@superwall/host";
|
|
|
3
3
|
import { DeviceChrome } from "./DeviceChrome";
|
|
4
4
|
import { HostedIframe } from "./HostedIframe";
|
|
5
5
|
import type { DeviceChromeSpec } from "../../lib/devices";
|
|
6
|
-
|
|
7
|
-
const FRAME_MARGIN = 96;
|
|
6
|
+
import { presentationRect, type StudioPresentation } from "../../lib/presentation";
|
|
8
7
|
|
|
9
8
|
const SWAP_DELAY_MS = 250;
|
|
10
9
|
|
|
11
|
-
const useFitScale = (
|
|
12
|
-
container: React.RefObject<HTMLElement | null>,
|
|
13
|
-
width: number,
|
|
14
|
-
height: number,
|
|
15
|
-
): number => {
|
|
16
|
-
const [scale, setScale] = React.useState(1);
|
|
17
|
-
|
|
18
|
-
React.useLayoutEffect(() => {
|
|
19
|
-
const element = container.current;
|
|
20
|
-
if (!element) return undefined;
|
|
21
|
-
|
|
22
|
-
const fit = () => {
|
|
23
|
-
const available = element.getBoundingClientRect();
|
|
24
|
-
const next = Math.min(
|
|
25
|
-
1,
|
|
26
|
-
(available.width - FRAME_MARGIN) / width,
|
|
27
|
-
(available.height - FRAME_MARGIN) / height,
|
|
28
|
-
);
|
|
29
|
-
setScale(Math.max(0.1, next));
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
fit();
|
|
33
|
-
if (typeof ResizeObserver === "undefined") return undefined;
|
|
34
|
-
const observer = new ResizeObserver(fit);
|
|
35
|
-
observer.observe(element);
|
|
36
|
-
return () => observer.disconnect();
|
|
37
|
-
}, [container, width, height]);
|
|
38
|
-
|
|
39
|
-
return scale;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
10
|
export const DevicePlaceholder = ({
|
|
43
11
|
width,
|
|
44
12
|
height,
|
|
45
|
-
stageRef,
|
|
46
13
|
children,
|
|
47
14
|
}: React.PropsWithChildren<{
|
|
48
15
|
width: number;
|
|
49
16
|
height: number;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
>
|
|
60
|
-
{children}
|
|
61
|
-
</div>
|
|
62
|
-
);
|
|
63
|
-
};
|
|
17
|
+
}>) => (
|
|
18
|
+
<div
|
|
19
|
+
className="flex flex-none items-center justify-center overflow-hidden border border-dashed border-cream/20 p-6 text-center text-sm text-cream/55"
|
|
20
|
+
data-testid="device-placeholder"
|
|
21
|
+
style={{ width, height }}
|
|
22
|
+
>
|
|
23
|
+
{children}
|
|
24
|
+
</div>
|
|
25
|
+
);
|
|
64
26
|
|
|
65
27
|
type Frame = {
|
|
66
28
|
src: string;
|
|
@@ -75,9 +37,9 @@ export const DeviceFrame = ({
|
|
|
75
37
|
src,
|
|
76
38
|
hostState,
|
|
77
39
|
delegate,
|
|
78
|
-
stageRef,
|
|
79
40
|
reloadKey = 0,
|
|
80
41
|
chrome,
|
|
42
|
+
presentation,
|
|
81
43
|
landscape = false,
|
|
82
44
|
theme = "light",
|
|
83
45
|
}: {
|
|
@@ -87,14 +49,12 @@ export const DeviceFrame = ({
|
|
|
87
49
|
src: string;
|
|
88
50
|
hostState: HostState;
|
|
89
51
|
delegate: HostDelegate;
|
|
90
|
-
stageRef: React.RefObject<HTMLElement | null>;
|
|
91
52
|
reloadKey?: number;
|
|
92
53
|
chrome?: DeviceChromeSpec;
|
|
54
|
+
presentation?: StudioPresentation;
|
|
93
55
|
landscape?: boolean;
|
|
94
56
|
theme?: "light" | "dark";
|
|
95
57
|
}) => {
|
|
96
|
-
const scale = useFitScale(stageRef, width, height);
|
|
97
|
-
|
|
98
58
|
const generation = React.useRef(0);
|
|
99
59
|
const [frames, setFrames] = React.useState<Frame[]>([{ src, generation: 0, ready: true }]);
|
|
100
60
|
|
|
@@ -122,7 +82,16 @@ export const DeviceFrame = ({
|
|
|
122
82
|
|
|
123
83
|
const visible = frames.findLast((frame) => frame.ready);
|
|
124
84
|
|
|
125
|
-
const
|
|
85
|
+
const sheet = presentationRect(presentation, { width, height });
|
|
86
|
+
const navigationBar = landscape ? 0 : (chrome?.navigationBarHeight ?? 0);
|
|
87
|
+
const page = sheet ?? {
|
|
88
|
+
x: 0,
|
|
89
|
+
y: 0,
|
|
90
|
+
width,
|
|
91
|
+
height: height - navigationBar,
|
|
92
|
+
radius: { top: 0, bottom: 0 },
|
|
93
|
+
};
|
|
94
|
+
const dark = theme === "dark";
|
|
126
95
|
|
|
127
96
|
return (
|
|
128
97
|
<div
|
|
@@ -130,31 +99,71 @@ export const DeviceFrame = ({
|
|
|
130
99
|
chrome ? "shadow-[0_0_0_1px] shadow-cream/25" : "border border-cream/20"
|
|
131
100
|
}`}
|
|
132
101
|
data-testid="device-frame"
|
|
133
|
-
style={{ width
|
|
102
|
+
style={{ width, height, borderRadius: chrome?.radius ?? 0 }}
|
|
134
103
|
>
|
|
135
|
-
{
|
|
136
|
-
<
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
title={title}
|
|
144
|
-
src={frame.src}
|
|
145
|
-
width={width}
|
|
146
|
-
height={height}
|
|
147
|
-
style={{ transform: `scale(${scale})` }}
|
|
148
|
-
onLoad={() => {
|
|
149
|
-
if (!frame.ready) commit(frame.generation);
|
|
104
|
+
{sheet && (
|
|
105
|
+
<div
|
|
106
|
+
className="absolute inset-0"
|
|
107
|
+
data-testid="presentation-backdrop"
|
|
108
|
+
style={{
|
|
109
|
+
background: dark
|
|
110
|
+
? "repeating-linear-gradient(-45deg, #1c1f26 0 7px, #171a20 7px 20px)"
|
|
111
|
+
: "repeating-linear-gradient(-45deg, #e3e3e3 0 7px, #ededed 7px 20px)",
|
|
150
112
|
}}
|
|
113
|
+
>
|
|
114
|
+
{sheet.underlay && (
|
|
115
|
+
<div
|
|
116
|
+
className="absolute left-1/2 -translate-x-1/2"
|
|
117
|
+
style={{
|
|
118
|
+
top: sheet.y - 12,
|
|
119
|
+
width: Math.round(width * 0.92),
|
|
120
|
+
height: 30,
|
|
121
|
+
borderRadius: "20px 20px 0 0",
|
|
122
|
+
background: dark ? "rgba(255,255,255,0.2)" : "rgba(0,0,0,0.3)",
|
|
123
|
+
}}
|
|
124
|
+
/>
|
|
125
|
+
)}
|
|
126
|
+
</div>
|
|
127
|
+
)}
|
|
128
|
+
<div
|
|
129
|
+
className="absolute overflow-hidden"
|
|
130
|
+
data-testid="presentation-page"
|
|
131
|
+
style={{
|
|
132
|
+
left: page.x,
|
|
133
|
+
top: page.y,
|
|
134
|
+
width: page.width,
|
|
135
|
+
height: page.height,
|
|
136
|
+
borderRadius: `${page.radius.top}px ${page.radius.top}px ${page.radius.bottom}px ${page.radius.bottom}px`,
|
|
137
|
+
boxShadow: sheet ? "0 0 40px rgba(0,0,0,0.25)" : undefined,
|
|
138
|
+
}}
|
|
139
|
+
>
|
|
140
|
+
{frames.map((frame) => (
|
|
141
|
+
<HostedIframe
|
|
142
|
+
key={frame.generation}
|
|
143
|
+
hostState={hostState}
|
|
144
|
+
delegate={delegate}
|
|
145
|
+
className={`absolute inset-0 border-0 bg-white transition-opacity duration-150 ${
|
|
146
|
+
frame === visible ? "opacity-100" : "opacity-0"
|
|
147
|
+
}`}
|
|
148
|
+
title={title}
|
|
149
|
+
src={frame.src}
|
|
150
|
+
width={page.width}
|
|
151
|
+
height={page.height}
|
|
152
|
+
onLoad={() => {
|
|
153
|
+
if (!frame.ready) commit(frame.generation);
|
|
154
|
+
}}
|
|
155
|
+
/>
|
|
156
|
+
))}
|
|
157
|
+
</div>
|
|
158
|
+
{navigationBar > 0 && !sheet && (
|
|
159
|
+
<div
|
|
160
|
+
className="absolute inset-x-0 bottom-0"
|
|
161
|
+
data-testid="navigation-bar"
|
|
162
|
+
style={{ height: navigationBar, background: dark ? "#000000" : "#ffffff" }}
|
|
151
163
|
/>
|
|
152
|
-
)
|
|
164
|
+
)}
|
|
153
165
|
{chrome && (
|
|
154
|
-
<div
|
|
155
|
-
className="pointer-events-none absolute inset-0 origin-top-left"
|
|
156
|
-
style={{ width, height, transform: `scale(${scale})` }}
|
|
157
|
-
>
|
|
166
|
+
<div className="pointer-events-none absolute inset-0" style={{ width, height }}>
|
|
158
167
|
<DeviceChrome
|
|
159
168
|
spec={chrome}
|
|
160
169
|
width={width}
|