Repository navigation
Expand file tree
/
Copy pathlayout.mjs
More file actions
142 lines (137 loc) · 5.04 KB
/
Copy pathlayout.mjs
File metadata and controls
142 lines (137 loc) · 5.04 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
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
// Pure geometry validation, also exercised with broken layouts in the unit tests.
export function validLayout(snapshot, expected) {
if (
!snapshot ||
![snapshot.width, snapshot.height, snapshot.contentWidth, snapshot.scrollTop].every(
Number.isFinite
) ||
Math.abs(snapshot.width - expected.width) > 1 ||
snapshot.height <= 0 ||
!snapshot.rows.length ||
snapshot.rows.length >= expected.total
) {
return false;
}
let visible = snapshot.rows
.filter(row => row.top + row.height > 1 && row.top < snapshot.height - 1)
.sort((a, b) => a.top - b.top);
if (
visible.length < 2 ||
visible[0].top > 1 ||
visible.at(-1).top + visible.at(-1).height < snapshot.height - 1
) {
return false;
}
if (
expected.atTop &&
(snapshot.scrollTop > 1 || visible[0].id !== 'item-0' || Math.abs(visible[0].top) > 1)
) {
return false;
}
return (
(!expected.columns ||
(snapshot.columns?.length === expected.columns.length &&
snapshot.columns.every((width, i) => Math.abs(width - expected.columns[i]) <= 1))) &&
new Set(visible.map(row => Math.round(row.height))).size >= 2 &&
visible.every(
(row, i) =>
Number.isFinite(row.index) &&
row.index >= 0 &&
row.index < expected.total &&
row.id === `item-${row.index}` &&
row.height > 0 &&
Math.abs(row.height - row.allocatedHeight) <= 1 &&
(!expected.columns || Math.abs(row.height - row.contentHeight) <= 1) &&
Math.abs(row.width - snapshot.contentWidth) <= 1 &&
(!i ||
(row.index === visible[i - 1].index + 1 &&
Math.abs(row.top - visible[i - 1].top - visible[i - 1].height) <= 1))
)
);
}
function cellContentHeight(cell) {
let style = getComputedStyle(cell);
// Use the content's natural height rather than the stretched row allocation.
return (
cell.firstElementChild.getBoundingClientRect().height +
parseFloat(style.paddingTop) +
parseFloat(style.paddingBottom) +
parseFloat(style.borderTopWidth) +
parseFloat(style.borderBottomWidth)
);
}
// Called once per animation frame, never from the MutationObserver. Reads are
// bounded by the rendered virtualized window, not the full dataset.
export function readLayout(expected) {
let viewport = document.querySelector(expected.viewport);
if (!viewport) {
return null;
}
let bounds = viewport.getBoundingClientRect();
let columns = expected.columns && [...viewport.querySelectorAll('[role="columnheader"]')];
let headingHeight = columns?.length ? columns[0].parentElement.getBoundingClientRect().height : 0;
let snapshot = {
width: bounds.width,
contentWidth: columns
? columns.reduce((total, column) => total + column.getBoundingClientRect().width, 0)
: viewport.clientWidth,
height: viewport.clientHeight - headingHeight,
scrollTop: viewport.scrollTop,
...(columns && {columns: columns.map(column => column.getBoundingClientRect().width)}),
rows: [...viewport.querySelectorAll('[data-record]')].map(node => {
let rect = (columns ? node.parentElement : node).getBoundingClientRect();
let id = node.getAttribute('data-record');
return {
id,
index: Number(id.slice(5)),
top: rect.top - bounds.top - viewport.clientTop - headingHeight,
height: rect.height,
width: rect.width,
allocatedHeight: node.parentElement.getBoundingClientRect().height,
...(columns && {
contentHeight: Math.max(
...[...node.querySelectorAll('[role="gridcell"], [role="rowheader"]')].map(
cellContentHeight
)
)
})
};
})
};
return validLayout(snapshot, expected) ? snapshot : null;
}
export function layoutSignature(snapshot) {
// Virtualizers may reorder DOM nodes or recycle offscreen rows without moving
// visible content. Compare geometry in visual order, independent of that work.
let visible = snapshot && {
...snapshot,
rows: snapshot.rows
.filter(row => row.top + row.height > 1 && row.top < snapshot.height - 1)
.sort((a, b) => a.top - b.top)
};
return JSON.stringify(visible, (_, value) =>
typeof value === 'number' ? Math.round(value * 10) / 10 : value
);
}
export async function settleLayout(expected) {
let previous;
let stable = 0;
let start = performance.now();
while (performance.now() - start < 15000) {
await new Promise(requestAnimationFrame);
let layout = readLayout(expected);
let signature = layout && layoutSignature(layout);
stable = signature && signature === previous ? stable + 1 : 1;
previous = signature;
if (signature && stable >= 3) {
return layout;
}
}
throw new Error('The variable-height viewport did not reach a complete, stable layout.');
}