forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayoutAudit.ts
More file actions
307 lines (271 loc) · 8.83 KB
/
Copy pathlayoutAudit.ts
File metadata and controls
307 lines (271 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
export interface LayoutRect {
left: number;
top: number;
right: number;
bottom: number;
width: number;
height: number;
}
export type LayoutOverflow = Partial<Record<"left" | "right" | "top" | "bottom", number>>;
export type LayoutIssueCode =
| "text_box_overflow"
| "clipped_text"
| "canvas_overflow"
| "container_overflow"
| "content_overlap"
| "text_occluded";
export type LayoutIssueSeverity = "error" | "warning" | "info";
export interface LayoutIssue {
code: LayoutIssueCode;
severity: LayoutIssueSeverity;
time: number;
firstSeen?: number;
lastSeen?: number;
occurrences?: number;
selector: string;
containerSelector?: string;
text?: string;
message: string;
rect: LayoutRect;
containerRect?: LayoutRect;
overflow?: LayoutOverflow;
fixHint?: string;
}
export interface LayoutSummary {
ok: boolean;
errorCount: number;
warningCount: number;
infoCount: number;
issueCount: number;
}
export interface LayoutSampleOptions {
duration: number;
samples: number;
at?: number[];
}
export function buildLayoutSampleTimes({ duration, samples, at }: LayoutSampleOptions): number[] {
if (at?.length) {
return uniqueSortedTimes(
at.filter(
(time) => Number.isFinite(time) && time >= 0 && (duration <= 0 || time <= duration),
),
);
}
if (!Number.isFinite(duration) || duration <= 0 || samples <= 0) return [];
const count = Math.max(1, Math.floor(samples));
return Array.from({ length: count }, (_, index) => roundTime(((index + 0.5) / count) * duration));
}
export function computeOverflow(
subject: LayoutRect,
container: LayoutRect,
tolerance: number,
): LayoutOverflow | null {
const overflow: LayoutOverflow = {};
if (subject.left < container.left - tolerance) {
overflow.left = roundPx(container.left - subject.left);
}
if (subject.right > container.right + tolerance) {
overflow.right = roundPx(subject.right - container.right);
}
if (subject.top < container.top - tolerance) {
overflow.top = roundPx(container.top - subject.top);
}
if (subject.bottom > container.bottom + tolerance) {
overflow.bottom = roundPx(subject.bottom - container.bottom);
}
return Object.keys(overflow).length > 0 ? overflow : null;
}
export function summarizeLayoutIssues(issues: LayoutIssue[]): LayoutSummary {
const errorCount = issues.filter((issue) => issue.severity === "error").length;
const warningCount = issues.filter((issue) => issue.severity === "warning").length;
const infoCount = issues.filter((issue) => issue.severity === "info").length;
return {
ok: errorCount === 0,
errorCount,
warningCount,
infoCount,
issueCount: issues.length,
};
}
export function formatLayoutIssue(issue: LayoutIssue): string {
const timeLabel =
issue.occurrences && issue.occurrences > 1
? `t=${formatNumber(issue.firstSeen ?? issue.time)}-${formatNumber(issue.lastSeen ?? issue.time)}s (${issue.occurrences} samples)`
: `t=${formatNumber(issue.time)}s`;
const parts = [
timeLabel,
issue.code,
issue.selector,
issue.containerSelector ? `inside ${issue.containerSelector}` : "",
issue.overflow ? `overflowed ${formatOverflow(issue.overflow)}` : "",
issue.text ? quoteText(issue.text) : "",
].filter(Boolean);
const line = `${parts.join(" ")} — ${issue.message}`;
return issue.fixHint ? `${line}\n Fix: ${issue.fixHint}` : line;
}
export function dedupeLayoutIssues(issues: LayoutIssue[]): LayoutIssue[] {
const seen = new Set<string>();
const result: LayoutIssue[] = [];
for (const issue of issues) {
const key = [
issue.code,
issue.severity,
issue.time.toFixed(3),
issue.selector,
issue.containerSelector ?? "",
issue.text ?? "",
issue.overflow ? formatOverflow(issue.overflow) : "",
].join("|");
if (seen.has(key)) continue;
seen.add(key);
result.push(issue);
}
return result;
}
export function collapseStaticLayoutIssues(issues: LayoutIssue[]): LayoutIssue[] {
const groups = new Map<
string,
{
issue: LayoutIssue;
firstSeen: number;
lastSeen: number;
occurrences: number;
}
>();
for (const issue of issues) {
const key = staticIssueKey(issue);
const existing = groups.get(key);
if (!existing) {
groups.set(key, {
issue,
firstSeen: issue.time,
lastSeen: issue.time,
occurrences: 1,
});
continue;
}
existing.firstSeen = Math.min(existing.firstSeen, issue.time);
existing.lastSeen = Math.max(existing.lastSeen, issue.time);
existing.occurrences += 1;
}
return [...groups.values()].map(({ issue, firstSeen, lastSeen, occurrences }) => ({
...issue,
time: firstSeen,
firstSeen,
lastSeen,
occurrences,
}));
}
export function limitLayoutIssues(
issues: LayoutIssue[],
maxIssues: number,
): { issues: LayoutIssue[]; totalIssueCount: number; truncated: boolean } {
const limit = Math.max(1, Math.floor(maxIssues));
const sortedIssues = [...issues].sort((a, b) => {
const severityDelta = severityRank(a.severity) - severityRank(b.severity);
if (severityDelta !== 0) return severityDelta;
return a.time - b.time;
});
return {
issues: sortedIssues.slice(0, limit),
totalIssueCount: issues.length,
truncated: issues.length > limit,
};
}
function severityRank(severity: LayoutIssueSeverity): number {
if (severity === "error") return 0;
if (severity === "warning") return 1;
return 2;
}
function staticIssueKey(issue: LayoutIssue): string {
return [
issue.code,
issue.severity,
issue.selector,
issue.containerSelector ?? "",
issue.text ?? "",
issue.overflow ? formatOverflow(issue.overflow) : "",
].join("|");
}
function uniqueSortedTimes(times: number[]): number[] {
const rounded = times.map(roundTime);
return [...new Set(rounded)].sort((a, b) => a - b);
}
export interface TransitionSampleOptions {
duration: number;
boundaries: number[];
/** Optional hard limit on the returned sample count. No limit when absent. */
cap?: number;
}
export interface TransitionSamples {
times: number[];
/** Sample times omitted because of `cap`. Always 0 when no cap is given. */
dropped: number;
}
/**
* Build sample times from tween start/end boundaries: the boundaries
* themselves plus the midpoint of every segment between consecutive
* boundaries. Boundary frames are where transient overlaps live (#1380), but
* sampling exactly at a boundary can land on an element at opacity 0 — the
* segment midpoints catch the window where both sides of a transition are
* partially visible. Every collected boundary is sampled unless the caller
* passes an explicit `cap`, in which case the result is an evenly-strided
* subset and `dropped` reports how many sample times were omitted.
*/
export function buildTransitionSampleTimes({
duration,
boundaries,
cap,
}: TransitionSampleOptions): TransitionSamples {
if (!Number.isFinite(duration) || duration <= 0) return { times: [], dropped: 0 };
const inRange = uniqueSortedTimes(
boundaries.filter((time) => Number.isFinite(time) && time >= 0 && time <= duration),
);
const withMidpoints = [...inRange];
for (let i = 0; i < inRange.length - 1; i++) {
const current = inRange[i];
const next = inRange[i + 1];
if (current === undefined || next === undefined) continue;
withMidpoints.push(roundTime((current + next) / 2));
}
const merged = uniqueSortedTimes(withMidpoints);
if (cap === undefined || merged.length <= Math.max(2, cap)) {
return { times: merged, dropped: 0 };
}
const limit = Math.max(2, cap);
const strided: number[] = [];
for (let i = 0; i < limit; i++) {
const pick = merged[Math.floor((i * (merged.length - 1)) / (limit - 1))];
if (pick !== undefined) strided.push(pick);
}
const times = uniqueSortedTimes(strided);
return { times, dropped: merged.length - times.length };
}
/** Merge sample-time lists into one deduplicated ascending list. */
export function mergeSampleTimes(...lists: number[][]): number[] {
return uniqueSortedTimes(lists.flat());
}
function formatOverflow(overflow: LayoutOverflow): string {
return (["left", "right", "top", "bottom"] as const)
.flatMap((side) => {
const value = overflow[side];
return value == null ? [] : `${side} ${formatNumber(value)}px`;
})
.join(", ");
}
function quoteText(text: string): string {
const normalized = text.replace(/\s+/g, " ").trim();
const truncated = normalized.length > 80 ? `${normalized.slice(0, 77)}...` : normalized;
return `"${truncated}"`;
}
function formatNumber(value: number): string {
return Number.isInteger(value)
? String(value)
: value.toFixed(2).replace(/0+$/, "").replace(/\.$/, "");
}
function roundTime(value: number): number {
return Math.round(value * 1000) / 1000;
}
function roundPx(value: number): number {
return Math.round(value * 100) / 100;
}