@superwall/studio 1.4.0 → 1.6.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-CljhSsYl.js +11 -0
- package/dist/assets/index-DrsTCwot.css +1 -0
- package/dist/index.html +2 -2
- 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 +1 -1
- package/dist/lib/index.js +2297 -1616
- 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/presentation.d.ts +23 -0
- package/dist/lib/lib/preview.d.ts +1 -0
- package/dist/lib/lib/surfaces.d.ts +23 -2
- package/dist/lib/studio.css +1 -1
- package/package.json +2 -2
- package/src/components/DownloadScreenshot.tsx +45 -18
- package/src/components/PaywallToasts.tsx +5 -3
- 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 +1 -1
- package/src/components/menus/CompareMenu.tsx +49 -30
- 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 +2 -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/presentation.ts +102 -0
- package/src/lib/preview.ts +3 -0
- package/src/lib/project.ts +43 -1
- package/src/lib/surfaces.ts +39 -1
- package/src/main.tsx +5 -1
- package/src/screens/SurfaceEditor.tsx +215 -52
- package/src/styles.css +46 -0
- package/dist/assets/index-GB-RwJI6.js +0 -11
- package/dist/assets/index-Sz7S5UsH.css +0 -1
|
@@ -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}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
export type FrameSize = { width: number; height: number };
|
|
4
|
+
|
|
5
|
+
type Horizontal = "left" | "right";
|
|
6
|
+
type Vertical = "top" | "bottom";
|
|
7
|
+
type Handle = {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
x?: Horizontal;
|
|
11
|
+
y?: Vertical;
|
|
12
|
+
className: string;
|
|
13
|
+
cursor: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const HANDLES: readonly Handle[] = [
|
|
17
|
+
{
|
|
18
|
+
id: "top",
|
|
19
|
+
label: "Resize height from the top",
|
|
20
|
+
y: "top",
|
|
21
|
+
className: "-top-2 left-0 h-4 w-full",
|
|
22
|
+
cursor: "ns-resize",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "bottom",
|
|
26
|
+
label: "Resize height",
|
|
27
|
+
y: "bottom",
|
|
28
|
+
className: "-bottom-2 left-0 h-4 w-full",
|
|
29
|
+
cursor: "ns-resize",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "left",
|
|
33
|
+
label: "Resize width from the left",
|
|
34
|
+
x: "left",
|
|
35
|
+
className: "top-0 -left-2 h-full w-4",
|
|
36
|
+
cursor: "ew-resize",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: "right",
|
|
40
|
+
label: "Resize width",
|
|
41
|
+
x: "right",
|
|
42
|
+
className: "top-0 -right-2 h-full w-4",
|
|
43
|
+
cursor: "ew-resize",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: "top-left",
|
|
47
|
+
label: "Resize from the top left",
|
|
48
|
+
x: "left",
|
|
49
|
+
y: "top",
|
|
50
|
+
className: "-top-2 -left-2 h-5 w-5",
|
|
51
|
+
cursor: "nwse-resize",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: "top-right",
|
|
55
|
+
label: "Resize from the top right",
|
|
56
|
+
x: "right",
|
|
57
|
+
y: "top",
|
|
58
|
+
className: "-top-2 -right-2 h-5 w-5",
|
|
59
|
+
cursor: "nesw-resize",
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: "bottom-left",
|
|
63
|
+
label: "Resize from the bottom left",
|
|
64
|
+
x: "left",
|
|
65
|
+
y: "bottom",
|
|
66
|
+
className: "-bottom-2 -left-2 h-5 w-5",
|
|
67
|
+
cursor: "nesw-resize",
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
id: "bottom-right",
|
|
71
|
+
label: "Resize",
|
|
72
|
+
x: "right",
|
|
73
|
+
y: "bottom",
|
|
74
|
+
className: "-right-2 -bottom-2 h-5 w-5",
|
|
75
|
+
cursor: "nwse-resize",
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
type Drag = {
|
|
80
|
+
pointerId: number;
|
|
81
|
+
handle: Handle;
|
|
82
|
+
from: { x: number; y: number };
|
|
83
|
+
start: FrameSize;
|
|
84
|
+
last: FrameSize;
|
|
85
|
+
release: () => void;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const ResizeHandles = ({
|
|
89
|
+
size,
|
|
90
|
+
scale,
|
|
91
|
+
clamp,
|
|
92
|
+
onResize,
|
|
93
|
+
onDragStart,
|
|
94
|
+
onShift,
|
|
95
|
+
}: {
|
|
96
|
+
size: FrameSize;
|
|
97
|
+
scale: number;
|
|
98
|
+
clamp: (value: number, fallback: number) => number;
|
|
99
|
+
onResize: (next: FrameSize) => void;
|
|
100
|
+
onDragStart?: () => void;
|
|
101
|
+
onShift?: (dx: number, dy: number) => void;
|
|
102
|
+
}) => {
|
|
103
|
+
const drag = React.useRef<Drag | null>(null);
|
|
104
|
+
|
|
105
|
+
const end = React.useCallback(() => {
|
|
106
|
+
drag.current?.release();
|
|
107
|
+
drag.current = null;
|
|
108
|
+
}, []);
|
|
109
|
+
|
|
110
|
+
React.useEffect(() => end, [end]);
|
|
111
|
+
|
|
112
|
+
const onPointerDown = (handle: Handle) => (event: React.PointerEvent<HTMLDivElement>) => {
|
|
113
|
+
if (event.pointerType === "mouse" && event.button !== 0) return;
|
|
114
|
+
event.stopPropagation();
|
|
115
|
+
event.preventDefault();
|
|
116
|
+
const element = event.currentTarget;
|
|
117
|
+
try {
|
|
118
|
+
element.setPointerCapture(event.pointerId);
|
|
119
|
+
} catch {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
end();
|
|
123
|
+
const pointerId = event.pointerId;
|
|
124
|
+
const released = (release: PointerEvent) => {
|
|
125
|
+
if (release.pointerId === pointerId) end();
|
|
126
|
+
};
|
|
127
|
+
const finish = () => end();
|
|
128
|
+
window.addEventListener("pointerup", released);
|
|
129
|
+
window.addEventListener("pointercancel", released);
|
|
130
|
+
window.addEventListener("blur", finish);
|
|
131
|
+
element.addEventListener("lostpointercapture", finish);
|
|
132
|
+
drag.current = {
|
|
133
|
+
pointerId,
|
|
134
|
+
handle,
|
|
135
|
+
from: { x: event.clientX, y: event.clientY },
|
|
136
|
+
start: size,
|
|
137
|
+
last: size,
|
|
138
|
+
release: () => {
|
|
139
|
+
window.removeEventListener("pointerup", released);
|
|
140
|
+
window.removeEventListener("pointercancel", released);
|
|
141
|
+
window.removeEventListener("blur", finish);
|
|
142
|
+
element.removeEventListener("lostpointercapture", finish);
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
onDragStart?.();
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const onPointerMove = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
149
|
+
const current = drag.current;
|
|
150
|
+
if (!current || current.pointerId !== event.pointerId) return;
|
|
151
|
+
event.stopPropagation();
|
|
152
|
+
if (event.pointerType === "mouse" && event.buttons === 0) {
|
|
153
|
+
end();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const { handle, start } = current;
|
|
157
|
+
const dx = (event.clientX - current.from.x) / scale;
|
|
158
|
+
const dy = (event.clientY - current.from.y) / scale;
|
|
159
|
+
const next: FrameSize = {
|
|
160
|
+
width:
|
|
161
|
+
handle.x === "right"
|
|
162
|
+
? clamp(Math.round(start.width + dx), start.width)
|
|
163
|
+
: handle.x === "left"
|
|
164
|
+
? clamp(Math.round(start.width - dx), start.width)
|
|
165
|
+
: start.width,
|
|
166
|
+
height:
|
|
167
|
+
handle.y === "bottom"
|
|
168
|
+
? clamp(Math.round(start.height + dy), start.height)
|
|
169
|
+
: handle.y === "top"
|
|
170
|
+
? clamp(Math.round(start.height - dy), start.height)
|
|
171
|
+
: start.height,
|
|
172
|
+
};
|
|
173
|
+
const shiftX = handle.x === "left" ? -(next.width - current.last.width) * scale : 0;
|
|
174
|
+
const shiftY = handle.y === "top" ? -(next.height - current.last.height) * scale : 0;
|
|
175
|
+
current.last = next;
|
|
176
|
+
onResize(next);
|
|
177
|
+
if (shiftX !== 0 || shiftY !== 0) onShift?.(shiftX, shiftY);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const onPointerUp = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
181
|
+
if (drag.current?.pointerId !== event.pointerId) return;
|
|
182
|
+
event.stopPropagation();
|
|
183
|
+
end();
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
<>
|
|
188
|
+
{HANDLES.map((handle) => (
|
|
189
|
+
<div
|
|
190
|
+
key={handle.id}
|
|
191
|
+
role="separator"
|
|
192
|
+
aria-label={handle.label}
|
|
193
|
+
aria-orientation={handle.y && !handle.x ? "horizontal" : "vertical"}
|
|
194
|
+
data-testid={`resize-${handle.id}`}
|
|
195
|
+
className={`absolute z-10 touch-none select-none ${handle.className}`}
|
|
196
|
+
style={{ cursor: handle.cursor }}
|
|
197
|
+
onPointerDown={onPointerDown(handle)}
|
|
198
|
+
onPointerMove={onPointerMove}
|
|
199
|
+
onPointerUp={onPointerUp}
|
|
200
|
+
onPointerCancel={onPointerUp}
|
|
201
|
+
/>
|
|
202
|
+
))}
|
|
203
|
+
</>
|
|
204
|
+
);
|
|
205
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -44,9 +44,11 @@ export type {
|
|
|
44
44
|
StudioActionName,
|
|
45
45
|
StudioActionProgress,
|
|
46
46
|
StudioActionResult,
|
|
47
|
+
StudioPresentation,
|
|
47
48
|
StudioSource,
|
|
48
49
|
StudioStatus,
|
|
49
50
|
StudioSurface,
|
|
50
51
|
StudioSurfaceKind,
|
|
52
|
+
StudioVersion,
|
|
51
53
|
} from "./lib/surfaces";
|
|
52
54
|
export type { SampleDefaults, VariableOverrides } from "./lib/variables";
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export type CanvasTransform = { scale: number; x: number; y: number };
|
|
2
|
+
|
|
3
|
+
export type Size = { width: number; height: number };
|
|
4
|
+
|
|
5
|
+
export const MIN_SCALE = 0.1;
|
|
6
|
+
export const MAX_SCALE = 4;
|
|
7
|
+
export const FIT_INSET = { top: 48, right: 48, bottom: 48, left: 48 };
|
|
8
|
+
|
|
9
|
+
const STEP_FACTOR = 1.25;
|
|
10
|
+
|
|
11
|
+
export const clampScale = (scale: number): number =>
|
|
12
|
+
Math.min(MAX_SCALE, Math.max(MIN_SCALE, scale));
|
|
13
|
+
|
|
14
|
+
export const fitTransform = (content: Size, viewport: Size): CanvasTransform => {
|
|
15
|
+
if (content.width <= 0 || content.height <= 0 || viewport.width <= 0 || viewport.height <= 0) {
|
|
16
|
+
return { scale: 1, x: 0, y: 0 };
|
|
17
|
+
}
|
|
18
|
+
const room = insetRoom(viewport);
|
|
19
|
+
const scale = clampScale(Math.min(1, room.width / content.width, room.height / content.height));
|
|
20
|
+
return centered(scale, content, viewport);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const insetRoom = (viewport: Size): Size => ({
|
|
24
|
+
width: viewport.width - FIT_INSET.left - FIT_INSET.right,
|
|
25
|
+
height: viewport.height - FIT_INSET.top - FIT_INSET.bottom,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const centered = (scale: number, content: Size, viewport: Size): CanvasTransform => {
|
|
29
|
+
const room = insetRoom(viewport);
|
|
30
|
+
return {
|
|
31
|
+
scale,
|
|
32
|
+
x: FIT_INSET.left + (room.width - content.width * scale) / 2,
|
|
33
|
+
y: FIT_INSET.top + (room.height - content.height * scale) / 2,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const zoomAt = (
|
|
38
|
+
transform: CanvasTransform,
|
|
39
|
+
factor: number,
|
|
40
|
+
point: { x: number; y: number },
|
|
41
|
+
): CanvasTransform => {
|
|
42
|
+
const scale = clampScale(transform.scale * factor);
|
|
43
|
+
const ratio = scale / transform.scale;
|
|
44
|
+
return {
|
|
45
|
+
scale,
|
|
46
|
+
x: point.x - (point.x - transform.x) * ratio,
|
|
47
|
+
y: point.y - (point.y - transform.y) * ratio,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const panBy = (transform: CanvasTransform, dx: number, dy: number): CanvasTransform => ({
|
|
52
|
+
...transform,
|
|
53
|
+
x: transform.x + dx,
|
|
54
|
+
y: transform.y + dy,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export const stepScale = (scale: number, direction: 1 | -1): number =>
|
|
58
|
+
clampScale(Math.round((direction === 1 ? scale * STEP_FACTOR : scale / STEP_FACTOR) * 100) / 100);
|
|
59
|
+
|
|
60
|
+
export const percent = (scale: number): string => `${Math.round(scale * 100)}%`;
|
package/src/lib/contract.ts
CHANGED
|
@@ -6,10 +6,17 @@ export type StudioActionResult =
|
|
|
6
6
|
|
|
7
7
|
export type StudioActionProgress = (step: string, state: "start" | "done") => void;
|
|
8
8
|
|
|
9
|
+
export type StudioVersion = {
|
|
10
|
+
version: number;
|
|
11
|
+
url: string;
|
|
12
|
+
live: boolean;
|
|
13
|
+
pushedAt?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
9
16
|
export type StudioStatus = {
|
|
10
17
|
paywallId?: string;
|
|
11
18
|
identifier?: string;
|
|
12
|
-
|
|
19
|
+
versions?: StudioVersion[];
|
|
13
20
|
active?: boolean;
|
|
14
21
|
updatedAt?: string;
|
|
15
22
|
unreachable?: string;
|
package/src/lib/devices.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type DeviceChromeSpec = {
|
|
|
3
3
|
cutout?: "island" | "punch";
|
|
4
4
|
statusBarHeight: number;
|
|
5
5
|
homeIndicator: boolean;
|
|
6
|
+
navigationBarHeight?: number;
|
|
6
7
|
};
|
|
7
8
|
|
|
8
9
|
export type StudioDevice = {
|
|
@@ -14,6 +15,7 @@ export type StudioDevice = {
|
|
|
14
15
|
platform: "iOS" | "Android";
|
|
15
16
|
deviceModel: string;
|
|
16
17
|
osVersion: string;
|
|
18
|
+
interfaceType?: string;
|
|
17
19
|
};
|
|
18
20
|
chrome?: DeviceChromeSpec;
|
|
19
21
|
};
|
|
@@ -64,7 +66,13 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
|
|
|
64
66
|
width: 412,
|
|
65
67
|
height: 915,
|
|
66
68
|
device: { platform: "Android", deviceModel: "Pixel 10", osVersion: "16" },
|
|
67
|
-
chrome: {
|
|
69
|
+
chrome: {
|
|
70
|
+
radius: 28,
|
|
71
|
+
cutout: "punch",
|
|
72
|
+
statusBarHeight: 24,
|
|
73
|
+
homeIndicator: true,
|
|
74
|
+
navigationBarHeight: 24,
|
|
75
|
+
},
|
|
68
76
|
},
|
|
69
77
|
{
|
|
70
78
|
id: "galaxy-s25",
|
|
@@ -72,14 +80,20 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
|
|
|
72
80
|
width: 360,
|
|
73
81
|
height: 780,
|
|
74
82
|
device: { platform: "Android", deviceModel: "SM-S931B", osVersion: "15" },
|
|
75
|
-
chrome: {
|
|
83
|
+
chrome: {
|
|
84
|
+
radius: 26,
|
|
85
|
+
cutout: "punch",
|
|
86
|
+
statusBarHeight: 24,
|
|
87
|
+
homeIndicator: true,
|
|
88
|
+
navigationBarHeight: 24,
|
|
89
|
+
},
|
|
76
90
|
},
|
|
77
91
|
{
|
|
78
92
|
id: "ipad-mini",
|
|
79
93
|
name: "iPad mini",
|
|
80
94
|
width: 744,
|
|
81
95
|
height: 1133,
|
|
82
|
-
device: { platform: "iOS", deviceModel: "iPad14,1", osVersion: "26.0" },
|
|
96
|
+
device: { platform: "iOS", deviceModel: "iPad14,1", osVersion: "26.0", interfaceType: "ipad" },
|
|
83
97
|
chrome: { radius: 21, statusBarHeight: 24, homeIndicator: true },
|
|
84
98
|
},
|
|
85
99
|
{
|
|
@@ -87,7 +101,7 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
|
|
|
87
101
|
name: "iPad Pro 11″",
|
|
88
102
|
width: 834,
|
|
89
103
|
height: 1194,
|
|
90
|
-
device: { platform: "iOS", deviceModel: "iPad16,3", osVersion: "26.0" },
|
|
104
|
+
device: { platform: "iOS", deviceModel: "iPad16,3", osVersion: "26.0", interfaceType: "ipad" },
|
|
91
105
|
chrome: { radius: 18, statusBarHeight: 24, homeIndicator: true },
|
|
92
106
|
},
|
|
93
107
|
{
|
|
@@ -100,6 +114,40 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
|
|
|
100
114
|
|
|
101
115
|
export const DEFAULT_DEVICE_ID = "iphone-17";
|
|
102
116
|
|
|
117
|
+
export const isWebOnly = (platforms: readonly string[] | undefined): boolean =>
|
|
118
|
+
platforms !== undefined &&
|
|
119
|
+
platforms.length > 0 &&
|
|
120
|
+
platforms.every((platform) => platform.toLowerCase() === "web");
|
|
121
|
+
|
|
122
|
+
export const chromeFor = (device: StudioDevice, web: boolean): DeviceChromeSpec | undefined =>
|
|
123
|
+
web ? undefined : device.chrome;
|
|
124
|
+
|
|
125
|
+
const BROWSER_CHROME = { iOS: 84, Android: 104, tablet: 66, desktop: 52 };
|
|
126
|
+
|
|
127
|
+
export const webViewportFor = (device: StudioDevice): { width: number; height: number } => {
|
|
128
|
+
const statusBar = device.chrome?.statusBarHeight ?? 0;
|
|
129
|
+
const bars =
|
|
130
|
+
device.device === undefined
|
|
131
|
+
? BROWSER_CHROME.desktop
|
|
132
|
+
: device.width >= 600
|
|
133
|
+
? BROWSER_CHROME.tablet
|
|
134
|
+
: BROWSER_CHROME[device.device.platform];
|
|
135
|
+
return { width: device.width, height: device.height - statusBar - bars };
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const DESKTOP_HOST = {
|
|
139
|
+
platform: "iOS",
|
|
140
|
+
deviceModel: "Mac",
|
|
141
|
+
osVersion: "26.0",
|
|
142
|
+
interfaceType: "mac",
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const deviceVariablesFor = (
|
|
146
|
+
device: StudioDevice,
|
|
147
|
+
web: boolean,
|
|
148
|
+
): Record<string, unknown> | undefined =>
|
|
149
|
+
web ? { ...device.device, platform: "web" } : (device.device ?? DESKTOP_HOST);
|
|
150
|
+
|
|
103
151
|
export const RESPONSIVE_ID = "responsive";
|
|
104
152
|
export const RESPONSIVE_DEFAULT = { width: 393, height: 852 };
|
|
105
153
|
|
package/src/lib/host.ts
CHANGED
|
@@ -27,6 +27,11 @@ export const studioDefaults = (): SampleDefaults => ({
|
|
|
27
27
|
params: DEFAULT_PARAMS,
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
+
export const previewDevice = (device: HostVariables): HostVariables => {
|
|
31
|
+
const { publicApiKey: _key, ...rest } = device;
|
|
32
|
+
return rest;
|
|
33
|
+
};
|
|
34
|
+
|
|
30
35
|
export const hostStateFor = ({
|
|
31
36
|
surface,
|
|
32
37
|
defaults,
|
|
@@ -37,7 +42,7 @@ export const hostStateFor = ({
|
|
|
37
42
|
locale,
|
|
38
43
|
edited,
|
|
39
44
|
}: HostInputs): HostState => {
|
|
40
|
-
const device: HostVariables = {
|
|
45
|
+
const device: HostVariables = previewDevice({
|
|
41
46
|
...defaults.device,
|
|
42
47
|
...sourceSamples?.device,
|
|
43
48
|
...samples.device,
|
|
@@ -45,7 +50,7 @@ export const hostStateFor = ({
|
|
|
45
50
|
interfaceStyle: theme === "dark" ? "Dark" : "Light",
|
|
46
51
|
...(locale ? { deviceLocale: locale, deviceLanguageCode: locale.split(/[-_]/)[0] } : {}),
|
|
47
52
|
...edited.device,
|
|
48
|
-
};
|
|
53
|
+
});
|
|
49
54
|
const user: HostVariables = {
|
|
50
55
|
...defaults.user,
|
|
51
56
|
...sourceSamples?.user,
|