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