Open History to see the last commit
1import {
2 PREVIEW_VIEWPORT_MAX_AREA,
3 PREVIEW_VIEWPORT_MAX_DIMENSION,
4 PREVIEW_VIEWPORT_MIN_DIMENSION,
5 type PreviewViewportSetting,
6 type PreviewViewportSize,
7} from "@t3tools/contracts";
8
9export interface BrowserViewportLayout {
10 readonly canvasWidth: number;
11 readonly canvasHeight: number;
12 readonly viewportX: number;
13 readonly viewportY: number;
14 /** Visible footprint inside the preview panel after fit-to-panel scaling. */
15 readonly viewportWidth: number;
16 readonly viewportHeight: number;
17 /** Presentation-only scale; the guest keeps its requested CSS viewport. */
18 readonly viewportScale: number;
19 readonly fillsPanel: boolean;
20}
21
22export const BROWSER_DEVICE_TOOLBAR_HEIGHT = 32;
23export const BROWSER_VIEWPORT_RESIZE_RAIL_SIZE = 10;
24
25export type BrowserViewportResizeDirection =
26 | "north"
27 | "northeast"
28 | "east"
29 | "southeast"
30 | "south"
31 | "southwest"
32 | "west"
33 | "northwest";
34
35export const browserViewportSettingKey = (setting: PreviewViewportSetting): string =>
36 setting._tag === "fill"
37 ? "fill"
38 : `${setting._tag}:${setting.width}:${setting.height}:${setting._tag === "preset" ? setting.presetId : ""}`;
39
40const normalizeZoomFactor = (zoomFactor: number): number =>
41 Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1;
42
43export function resolveFittedBrowserViewport(
44 setting: PreviewViewportSetting,
45 sourceContent: {
46 readonly width: number;
47 readonly height: number;
48 readonly scale: number;
49 } | null,
50 zoomFactor = 1,
51): Exclude<PreviewViewportSetting, { readonly _tag: "fill" }> {
52 if (setting._tag !== "fill") return setting;
53 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
54 if (sourceContent) {
55 return {
56 _tag: "freeform",
57 width: Math.max(
58 1,
59 Math.round(sourceContent.width / sourceContent.scale / normalizedZoomFactor),
60 ),
61 height: Math.max(
62 1,
63 Math.round(sourceContent.height / sourceContent.scale / normalizedZoomFactor),
64 ),
65 };
66 }
67 return { _tag: "freeform", width: 1280, height: 800 };
68}
69
70export function resolveBrowserDeviceViewportArea(container: {
71 readonly width: number;
72 readonly height: number;
73}): PreviewViewportSize {
74 return {
75 width: Math.max(1, container.width - BROWSER_VIEWPORT_RESIZE_RAIL_SIZE * 2),
76 height: Math.max(
77 1,
78 container.height - BROWSER_DEVICE_TOOLBAR_HEIGHT - BROWSER_VIEWPORT_RESIZE_RAIL_SIZE,
79 ),
80 };
81}
82
83export function resolveBrowserViewportLayout(
84 container: { readonly width: number; readonly height: number },
85 setting: PreviewViewportSetting,
86 zoomFactor = 1,
87): BrowserViewportLayout {
88 const containerWidth = Math.max(1, Math.round(container.width));
89 const containerHeight = Math.max(1, Math.round(container.height));
90 if (setting._tag === "fill") {
91 return {
92 canvasWidth: containerWidth,
93 canvasHeight: containerHeight,
94 viewportX: 0,
95 viewportY: 0,
96 viewportWidth: containerWidth,
97 viewportHeight: containerHeight,
98 viewportScale: 1,
99 fillsPanel: true,
100 };
101 }
102 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
103 const renderedWidth = setting.width * normalizedZoomFactor;
104 const renderedHeight = setting.height * normalizedZoomFactor;
105 const viewportScale = Math.min(
106 1,
107 containerWidth / renderedWidth,
108 containerHeight / renderedHeight,
109 );
110 const viewportWidth = renderedWidth * viewportScale;
111 const viewportHeight = renderedHeight * viewportScale;
112 return {
113 canvasWidth: containerWidth,
114 canvasHeight: containerHeight,
115 viewportX: Math.max(0, Math.round((containerWidth - viewportWidth) / 2)),
116 viewportY: Math.max(0, Math.round((containerHeight - viewportHeight) / 2)),
117 viewportWidth,
118 viewportHeight,
119 viewportScale,
120 fillsPanel: false,
121 };
122}
123
124export function resolveBrowserDeviceViewportLayout(
125 container: { readonly width: number; readonly height: number },
126 setting: Exclude<PreviewViewportSetting, { readonly _tag: "fill" }>,
127 zoomFactor = 1,
128): BrowserViewportLayout {
129 const layout = resolveBrowserViewportLayout(
130 resolveBrowserDeviceViewportArea(container),
131 setting,
132 zoomFactor,
133 );
134 return {
135 ...layout,
136 canvasWidth: Math.max(1, Math.round(container.width)),
137 canvasHeight: Math.max(1, Math.round(container.height)),
138 viewportX: layout.viewportX + BROWSER_VIEWPORT_RESIZE_RAIL_SIZE,
139 viewportY: layout.viewportY + BROWSER_DEVICE_TOOLBAR_HEIGHT,
140 };
141}
142
143const clampViewportDimension = (value: number): number =>
144 Math.min(PREVIEW_VIEWPORT_MAX_DIMENSION, Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, value));
145
146const validAspectRatio = (aspectRatio: number | undefined): aspectRatio is number =>
147 aspectRatio !== undefined && Number.isFinite(aspectRatio) && aspectRatio > 0;
148
149function resizeAtAspectRatio(
150 desired: number,
151 aspectRatio: number,
152 primaryAxis: "width" | "height",
153): PreviewViewportSize {
154 if (primaryAxis === "width") {
155 const minimum = Math.ceil(
156 Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, PREVIEW_VIEWPORT_MIN_DIMENSION * aspectRatio),
157 );
158 const maximum = Math.floor(
159 Math.min(
160 PREVIEW_VIEWPORT_MAX_DIMENSION,
161 PREVIEW_VIEWPORT_MAX_DIMENSION * aspectRatio,
162 Math.sqrt(PREVIEW_VIEWPORT_MAX_AREA * aspectRatio),
163 ),
164 );
165 let width = Math.min(maximum, Math.max(minimum, Math.round(desired)));
166 let height = Math.round(width / aspectRatio);
167 while (width * height > PREVIEW_VIEWPORT_MAX_AREA && width > minimum) {
168 width -= 1;
169 height = Math.round(width / aspectRatio);
170 }
171 return { width, height };
172 }
173
174 const minimum = Math.ceil(
175 Math.max(PREVIEW_VIEWPORT_MIN_DIMENSION, PREVIEW_VIEWPORT_MIN_DIMENSION / aspectRatio),
176 );
177 const maximum = Math.floor(
178 Math.min(
179 PREVIEW_VIEWPORT_MAX_DIMENSION,
180 PREVIEW_VIEWPORT_MAX_DIMENSION / aspectRatio,
181 Math.sqrt(PREVIEW_VIEWPORT_MAX_AREA / aspectRatio),
182 ),
183 );
184 let height = Math.min(maximum, Math.max(minimum, Math.round(desired)));
185 let width = Math.round(height * aspectRatio);
186 while (width * height > PREVIEW_VIEWPORT_MAX_AREA && height > minimum) {
187 height -= 1;
188 width = Math.round(height * aspectRatio);
189 }
190 return { width, height };
191}
192
193export function resizeFreeformViewport(
194 start: PreviewViewportSize,
195 delta: { readonly x: number; readonly y: number },
196 zoomFactor = 1,
197 direction: BrowserViewportResizeDirection = "southeast",
198 aspectRatio?: number,
199): PreviewViewportSize {
200 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
201 const horizontalDelta = direction.includes("east")
202 ? delta.x
203 : direction.includes("west")
204 ? -delta.x
205 : 0;
206 const verticalDelta = direction.includes("south")
207 ? delta.y
208 : direction.includes("north")
209 ? -delta.y
210 : 0;
211 const desiredWidth = start.width + horizontalDelta / normalizedZoomFactor;
212 const desiredHeight = start.height + verticalDelta / normalizedZoomFactor;
213 if (validAspectRatio(aspectRatio)) {
214 const controlsWidth = horizontalDelta !== 0 || direction === "east" || direction === "west";
215 const controlsHeight = verticalDelta !== 0 || direction === "north" || direction === "south";
216 const primaryAxis =
217 controlsWidth && !controlsHeight
218 ? "width"
219 : controlsHeight && !controlsWidth
220 ? "height"
221 : Math.abs(desiredWidth - start.width) / start.width >=
222 Math.abs(desiredHeight - start.height) / start.height
223 ? "width"
224 : "height";
225 return resizeAtAspectRatio(
226 primaryAxis === "width" ? desiredWidth : desiredHeight,
227 aspectRatio,
228 primaryAxis,
229 );
230 }
231 let width = clampViewportDimension(Math.round(desiredWidth));
232 let height = clampViewportDimension(Math.round(desiredHeight));
233 if (width * height <= PREVIEW_VIEWPORT_MAX_AREA) return { width, height };
234 if (Math.abs(horizontalDelta) >= Math.abs(verticalDelta)) {
235 width = Math.max(
236 PREVIEW_VIEWPORT_MIN_DIMENSION,
237 Math.floor(PREVIEW_VIEWPORT_MAX_AREA / height),
238 );
239 } else {
240 height = Math.max(
241 PREVIEW_VIEWPORT_MIN_DIMENSION,
242 Math.floor(PREVIEW_VIEWPORT_MAX_AREA / width),
243 );
244 }
245 return { width, height };
246}
247
248const resizeFromEndRail = (start: number, pointerDelta: number, available: number): number => {
249 const startEdge = start < available ? (available + start) / 2 : start;
250 const targetEdge = startEdge + pointerDelta;
251 return targetEdge <= available ? targetEdge * 2 - available : targetEdge;
252};
253
254const resizeFromStartRail = (start: number, pointerDelta: number, available: number): number => {
255 if (start > available) {
256 const distanceToFit = start - available;
257 return pointerDelta <= distanceToFit
258 ? start - pointerDelta
259 : available - (pointerDelta - distanceToFit) * 2;
260 }
261 const targetEdge = (available - start) / 2 + pointerDelta;
262 return targetEdge >= 0 ? available - targetEdge * 2 : available - targetEdge;
263};
264
265export function resizeBrowserViewportFromRail(
266 start: PreviewViewportSize,
267 pointerDelta: { readonly x: number; readonly y: number },
268 available: PreviewViewportSize,
269 zoomFactor = 1,
270 direction: BrowserViewportResizeDirection = "southeast",
271 aspectRatio?: number,
272): PreviewViewportSize {
273 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
274 const startWidth = start.width * normalizedZoomFactor;
275 const startHeight = start.height * normalizedZoomFactor;
276 const desiredWidth = direction.includes("east")
277 ? resizeFromEndRail(startWidth, pointerDelta.x, available.width)
278 : direction.includes("west")
279 ? resizeFromStartRail(startWidth, pointerDelta.x, available.width)
280 : startWidth;
281 const desiredHeight = direction.includes("south")
282 ? resizeFromEndRail(startHeight, pointerDelta.y, available.height)
283 : direction.includes("north")
284 ? resizeFromStartRail(startHeight, pointerDelta.y, available.height)
285 : startHeight;
286 const widthDelta = desiredWidth - startWidth;
287 const heightDelta = desiredHeight - startHeight;
288 return resizeFreeformViewport(
289 start,
290 {
291 x: direction.includes("west") ? -widthDelta : widthDelta,
292 y: direction.includes("north") ? -heightDelta : heightDelta,
293 },
294 normalizedZoomFactor,
295 direction,
296 aspectRatio,
297 );
298}
299
300export function resolveResponsiveBrowserViewportSize(
301 container: { readonly width: number; readonly height: number },
302 zoomFactor = 1,
303): PreviewViewportSize {
304 const area = resolveBrowserDeviceViewportArea(container);
305 const normalizedZoomFactor = normalizeZoomFactor(zoomFactor);
306 return resizeFreeformViewport(
307 {
308 width: area.width / normalizedZoomFactor,
309 height: area.height / normalizedZoomFactor,
310 },
311 { x: 0, y: 0 },
312 );
313}