nodality 1.3.26 → 1.4.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/dist/animator.cjs.js +1 -1
- package/dist/animator.cjs.js.LICENSE.txt +1 -1
- package/dist/animator.esm.js +1 -1
- package/dist/animator.esm.js.LICENSE.txt +1 -1
- package/dist/audionew.cjs.js +1 -1
- package/dist/audionew.cjs.js.LICENSE.txt +1 -1
- package/dist/audionew.esm.js +1 -1
- package/dist/audionew.esm.js.LICENSE.txt +1 -1
- package/dist/base.cjs.js +1 -1
- package/dist/base.cjs.js.LICENSE.txt +1 -1
- package/dist/base.esm.js +1 -1
- package/dist/base.esm.js.LICENSE.txt +1 -1
- package/dist/beta-desktop-bar.cjs.js +1 -1
- package/dist/beta-desktop-bar.cjs.js.LICENSE.txt +1 -1
- package/dist/beta-desktop-bar.esm.js +1 -1
- package/dist/beta-desktop-bar.esm.js.LICENSE.txt +1 -1
- package/dist/beta-mobile-bar.cjs.js +1 -1
- package/dist/beta-mobile-bar.cjs.js.LICENSE.txt +1 -1
- package/dist/beta-mobile-bar.esm.js +1 -1
- package/dist/beta-mobile-bar.esm.js.LICENSE.txt +1 -1
- package/dist/bundle.umd.js +1 -1
- package/dist/bundle.umd.js.LICENSE.txt +20 -1
- package/dist/button.cjs.js +1 -1
- package/dist/button.cjs.js.LICENSE.txt +1 -1
- package/dist/button.esm.js +1 -1
- package/dist/button.esm.js.LICENSE.txt +1 -1
- package/dist/card-getter.cjs.js.LICENSE.txt +1 -1
- package/dist/card-getter.esm.js.LICENSE.txt +1 -1
- package/dist/center.cjs.js +1 -1
- package/dist/center.cjs.js.LICENSE.txt +1 -1
- package/dist/center.esm.js +1 -1
- package/dist/center.esm.js.LICENSE.txt +1 -1
- package/dist/checkbox.cjs.js +1 -1
- package/dist/checkbox.cjs.js.LICENSE.txt +1 -1
- package/dist/checkbox.esm.js +1 -1
- package/dist/checkbox.esm.js.LICENSE.txt +1 -1
- package/dist/code.cjs.js +1 -1
- package/dist/code.cjs.js.LICENSE.txt +1 -1
- package/dist/code.esm.js +1 -1
- package/dist/code.esm.js.LICENSE.txt +1 -1
- package/dist/container.cjs.js +1 -1
- package/dist/container.cjs.js.LICENSE.txt +1 -1
- package/dist/container.esm.js +1 -1
- package/dist/container.esm.js.LICENSE.txt +1 -1
- package/dist/data-list.cjs.js +1 -1
- package/dist/data-list.cjs.js.LICENSE.txt +1 -1
- package/dist/data-list.esm.js.LICENSE.txt +1 -1
- package/dist/designer.cjs.js +1 -1
- package/dist/designer.cjs.js.LICENSE.txt +20 -1
- package/dist/designer.esm.js +1 -1
- package/dist/designer.esm.js.LICENSE.txt +20 -1
- package/dist/element-mapper.cjs.js +1 -1
- package/dist/element-mapper.cjs.js.LICENSE.txt +1 -1
- package/dist/element-mapper.esm.js +1 -1
- package/dist/element-mapper.esm.js.LICENSE.txt +1 -1
- package/dist/finalresult.esm.js +1 -1
- package/dist/finalresult.esm.js.LICENSE.txt +20 -1
- package/dist/flex-card.cjs.js +1 -1
- package/dist/flex-card.cjs.js.LICENSE.txt +1 -1
- package/dist/flex-card.esm.js +1 -1
- package/dist/flex-card.esm.js.LICENSE.txt +1 -1
- package/dist/flex-grid.cjs.js +1 -1
- package/dist/flex-grid.cjs.js.LICENSE.txt +1 -1
- package/dist/flex-grid.esm.js +1 -1
- package/dist/flex-grid.esm.js.LICENSE.txt +1 -1
- package/dist/flex-row.cjs.js +1 -1
- package/dist/flex-row.cjs.js.LICENSE.txt +1 -1
- package/dist/flex-row.esm.js +1 -1
- package/dist/flex-row.esm.js.LICENSE.txt +1 -1
- package/dist/floating-input.cjs.js +1 -1
- package/dist/floating-input.cjs.js.LICENSE.txt +1 -1
- package/dist/floating-input.esm.js +1 -1
- package/dist/floating-input.esm.js.LICENSE.txt +1 -1
- package/dist/free.cjs.js +1 -1
- package/dist/free.cjs.js.LICENSE.txt +1 -1
- package/dist/free.esm.js +1 -1
- package/dist/free.esm.js.LICENSE.txt +1 -1
- package/dist/horizontal-scroller.cjs.js.LICENSE.txt +1 -1
- package/dist/horizontal-scroller.esm.js.LICENSE.txt +1 -1
- package/dist/image-picker.cjs.js +1 -1
- package/dist/image-picker.cjs.js.LICENSE.txt +1 -1
- package/dist/image-picker.esm.js +1 -1
- package/dist/image-picker.esm.js.LICENSE.txt +1 -1
- package/dist/image.cjs.js +1 -1
- package/dist/image.cjs.js.LICENSE.txt +1 -1
- package/dist/image.esm.js +1 -1
- package/dist/image.esm.js.LICENSE.txt +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.LICENSE.txt +20 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.LICENSE.txt +20 -1
- package/dist/keyframe-animation.cjs.js.LICENSE.txt +1 -1
- package/dist/keyframe-animation.esm.js.LICENSE.txt +1 -1
- package/dist/link-getter.cjs.js +1 -1
- package/dist/link-getter.cjs.js.LICENSE.txt +1 -1
- package/dist/link-getter.esm.js +1 -1
- package/dist/link-getter.esm.js.LICENSE.txt +1 -1
- package/dist/link.cjs.js +1 -1
- package/dist/link.cjs.js.LICENSE.txt +1 -1
- package/dist/link.esm.js +1 -1
- package/dist/link.esm.js.LICENSE.txt +1 -1
- package/dist/meta-adder.cjs.js +1 -1
- package/dist/meta-adder.cjs.js.LICENSE.txt +1 -1
- package/dist/meta-adder.esm.js +1 -1
- package/dist/meta-adder.esm.js.LICENSE.txt +1 -1
- package/dist/modal-2025.cjs.js +1 -1
- package/dist/modal-2025.cjs.js.LICENSE.txt +1 -1
- package/dist/modal-2025.esm.js +1 -1
- package/dist/modal-2025.esm.js.LICENSE.txt +1 -1
- package/dist/multiswitcher.cjs.js +1 -1
- package/dist/multiswitcher.cjs.js.LICENSE.txt +1 -1
- package/dist/multiswitcher.esm.js +1 -1
- package/dist/multiswitcher.esm.js.LICENSE.txt +1 -1
- package/dist/new-nav-bar.cjs.js +1 -1
- package/dist/new-nav-bar.cjs.js.LICENSE.txt +1 -1
- package/dist/new-nav-bar.esm.js +1 -1
- package/dist/new-nav-bar.esm.js.LICENSE.txt +1 -1
- package/dist/picker.cjs.js +1 -1
- package/dist/picker.cjs.js.LICENSE.txt +1 -1
- package/dist/picker.esm.js +1 -1
- package/dist/picker.esm.js.LICENSE.txt +1 -1
- package/dist/progress.cjs.js +1 -1
- package/dist/progress.cjs.js.LICENSE.txt +1 -1
- package/dist/progress.esm.js +1 -1
- package/dist/progress.esm.js.LICENSE.txt +1 -1
- package/dist/radio.cjs.js +1 -1
- package/dist/radio.cjs.js.LICENSE.txt +1 -1
- package/dist/radio.esm.js.LICENSE.txt +1 -1
- package/dist/range.cjs.js +1 -1
- package/dist/range.cjs.js.LICENSE.txt +1 -1
- package/dist/range.esm.js.LICENSE.txt +1 -1
- package/dist/scroll-video.cjs.js.LICENSE.txt +1 -1
- package/dist/scroll-video.esm.js.LICENSE.txt +1 -1
- package/dist/side-bar.cjs.js +1 -1
- package/dist/side-bar.cjs.js.LICENSE.txt +1 -1
- package/dist/side-bar.esm.js +1 -1
- package/dist/side-bar.esm.js.LICENSE.txt +1 -1
- package/dist/side-nav-bar.cjs.js +1 -1
- package/dist/side-nav-bar.cjs.js.LICENSE.txt +1 -1
- package/dist/side-nav-bar.esm.js +1 -1
- package/dist/side-nav-bar.esm.js.LICENSE.txt +1 -1
- package/dist/simple-bar.cjs.js +1 -1
- package/dist/simple-bar.cjs.js.LICENSE.txt +1 -1
- package/dist/simple-bar.esm.js +1 -1
- package/dist/simple-bar.esm.js.LICENSE.txt +1 -1
- package/dist/slider-2025.cjs.js +1 -1
- package/dist/slider-2025.cjs.js.LICENSE.txt +1 -1
- package/dist/slider-2025.esm.js +1 -1
- package/dist/slider-2025.esm.js.LICENSE.txt +1 -1
- package/dist/spacer.cjs.js +1 -1
- package/dist/spacer.cjs.js.LICENSE.txt +1 -1
- package/dist/spacer.esm.js +1 -1
- package/dist/spacer.esm.js.LICENSE.txt +1 -1
- package/dist/stack.cjs.js +1 -1
- package/dist/stack.cjs.js.LICENSE.txt +1 -1
- package/dist/stack.esm.js +1 -1
- package/dist/stack.esm.js.LICENSE.txt +1 -1
- package/dist/stacker.cjs.js.LICENSE.txt +1 -1
- package/dist/stacker.esm.js.LICENSE.txt +1 -1
- package/dist/table.cjs.js +1 -1
- package/dist/table.cjs.js.LICENSE.txt +1 -1
- package/dist/table.esm.js +1 -1
- package/dist/table.esm.js.LICENSE.txt +1 -1
- package/dist/text-field.cjs.js +1 -1
- package/dist/text-field.cjs.js.LICENSE.txt +1 -1
- package/dist/text-field.esm.js +1 -1
- package/dist/text-field.esm.js.LICENSE.txt +1 -1
- package/dist/text.cjs.js +1 -1
- package/dist/text.cjs.js.LICENSE.txt +1 -1
- package/dist/text.esm.js +1 -1
- package/dist/text.esm.js.LICENSE.txt +1 -1
- package/dist/theme.cjs.js.LICENSE.txt +1 -1
- package/dist/theme.esm.js.LICENSE.txt +1 -1
- package/dist/transform-anim.cjs.js.LICENSE.txt +1 -1
- package/dist/transform-anim.esm.js.LICENSE.txt +1 -1
- package/dist/ulist.cjs.js +1 -1
- package/dist/ulist.cjs.js.LICENSE.txt +1 -1
- package/dist/ulist.esm.js +1 -1
- package/dist/ulist.esm.js.LICENSE.txt +1 -1
- package/dist/video.cjs.js +1 -1
- package/dist/video.cjs.js.LICENSE.txt +1 -1
- package/dist/video.esm.js +1 -1
- package/dist/video.esm.js.LICENSE.txt +1 -1
- package/dist/wrap.cjs.js +1 -1
- package/dist/wrap.cjs.js.LICENSE.txt +1 -1
- package/dist/wrap.esm.js +1 -1
- package/dist/wrap.esm.js.LICENSE.txt +1 -1
- package/dist/zoom-card.cjs.js +1 -1
- package/dist/zoom-card.cjs.js.LICENSE.txt +1 -1
- package/dist/zoom-card.esm.js +1 -1
- package/dist/zoom-card.esm.js.LICENSE.txt +1 -1
- package/layout/animator.js +13 -4
- package/layout/audio.js +1 -1
- package/layout/audionew.js +1 -1
- package/layout/base.js +1 -1
- package/layout/beta-desktop-bar.js +1 -1
- package/layout/beta-mobile-bar.js +1 -1
- package/layout/button.js +1 -1
- package/layout/center.js +1 -1
- package/layout/checkbox.js +1 -1
- package/layout/circle.js +1 -1
- package/layout/code.js +1 -1
- package/layout/container.js +1 -1
- package/layout/dropdown-2025.js +1 -1
- package/layout/flex-card.js +1 -1
- package/layout/flex-grid.js +1 -1
- package/layout/flex-row.js +1 -1
- package/layout/form-components/custom.js +1 -1
- package/layout/form-components/data-list.js +1 -1
- package/layout/form-components/floating-input.js +1 -1
- package/layout/form-components/form-all.js +1 -1
- package/layout/form-components/form.js +1 -1
- package/layout/form-components/image-picker.js +1 -1
- package/layout/form-components/picker.js +1 -1
- package/layout/form-components/radio.js +1 -1
- package/layout/form-components/radiogroup.js +1 -1
- package/layout/form-components/range.js +1 -1
- package/layout/free.js +1 -1
- package/layout/grid-overlay.js +1 -1
- package/layout/grid-switcher.js +1 -1
- package/layout/grid.js +1 -1
- package/layout/horizontal-scroller.js +1 -1
- package/layout/image.js +1 -1
- package/layout/index.js +1 -1
- package/layout/link.js +1 -1
- package/layout/list.js +1 -1
- package/layout/meta-adder.js +1 -1
- package/layout/modal-2025.js +1 -1
- package/layout/morph.js +1 -1
- package/layout/multiswitcher.js +1 -1
- package/layout/nav-bar.js +1 -1
- package/layout/nav-factor/custom-div.js +1 -1
- package/layout/new-nav-bar.js +1 -1
- package/layout/polygon.js +1 -1
- package/layout/prerender-site.js +1 -1
- package/layout/prerender.js +1 -1
- package/layout/progress.js +1 -1
- package/layout/row.js +1 -1
- package/layout/scroll-video.js +1 -1
- package/layout/shop.js +1 -1
- package/layout/side-bar.js +1 -1
- package/layout/side-nav-bar.js +1 -1
- package/layout/simple-bar.js +1 -1
- package/layout/slider-2025.js +1 -1
- package/layout/spacer.js +1 -1
- package/layout/stack.js +1 -1
- package/layout/svg.js +1 -1
- package/layout/switcher.js +1 -1
- package/layout/table.js +1 -1
- package/layout/text-field.js +1 -1
- package/layout/text.js +1 -1
- package/layout/ulist.js +1 -1
- package/layout/video.js +1 -1
- package/layout/wrap.js +1 -1
- package/layout/zoom-card.js +1 -1
- package/lib/agent-surface.js +1 -1
- package/lib/card-getter.js +1 -1
- package/lib/check-page.js +1 -1
- package/lib/codegen.js +1 -1
- package/lib/data.js +1 -1
- package/lib/designer.js +14 -1
- package/lib/element-mapper.js +1 -1
- package/lib/element-params.generated.js +1 -1
- package/lib/grid-geometry.js +1 -1
- package/lib/keyframe-animation.js +1 -1
- package/lib/known-inert.js +1 -1
- package/lib/link-getter.js +1 -1
- package/lib/morph-node.js +39 -7
- package/lib/normalize-spec.js +1 -1
- package/lib/parse-html.js +1 -1
- package/lib/raster-inspect.js +1 -1
- package/lib/raster-ops.js +255 -34
- package/lib/raster-presets.js +32 -1
- package/lib/react.js +1 -1
- package/lib/resolve-refs.js +1 -1
- package/lib/responsive.js +647 -0
- package/lib/scroll-video.js +1 -1
- package/lib/seo.js +1 -1
- package/lib/stacker.js +1 -1
- package/lib/suggest.js +1 -1
- package/lib/theme.js +1 -1
- package/lib/transform-anim.js +1 -1
- package/lib/transition.js +1 -1
- package/lib/validate-nodes.js +27 -2
- package/lib/view-transition.js +287 -0
- package/lib/webmcp-adapter.js +1 -1
- package/package.json +3 -1
- package/skills/nodality/SKILL.md +9 -0
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* nodality v1.4.0
|
|
3
|
+
* (c) 2026 Filip Vabrousek
|
|
4
|
+
* License: MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/*! responsive — breakpoints solved from content, emitted as `arrange` nodes.
|
|
8
|
+
*
|
|
9
|
+
* A group (a `row`, a `cards` grid, a `nav`) has a wide arrangement and a
|
|
10
|
+
* narrow one. The wide one fits at viewport width v exactly when
|
|
11
|
+
*
|
|
12
|
+
* g(v) = need(v) - have(v) <= 0,
|
|
13
|
+
*
|
|
14
|
+
* where need(v) is the group's min-content width on one line and have(v) the
|
|
15
|
+
* width its parent gives it, both measured by the browser. The breakpoints of
|
|
16
|
+
* the group are the sign changes of g, and nothing else: one is emitted where
|
|
17
|
+
* the arrangement has to change and none where it does not. Each interval on
|
|
18
|
+
* which the wide arrangement does not fit becomes one `arrange` node scoped by
|
|
19
|
+
* the group's id and that interval.
|
|
20
|
+
*
|
|
21
|
+
* Pure apart from respond(), which drives a Playwright page the caller owns —
|
|
22
|
+
* this module never imports a browser. No randomness, no clock; everything
|
|
23
|
+
* runs in declaration order, so the same page always yields the same nodes.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export const ARRANGE = "arrange";
|
|
27
|
+
export const MAX_WIDTH = 99999;
|
|
28
|
+
export const DEFAULT_SAMPLE_WIDTHS = [320, 480, 768, 1024, 1280, 1600, 1920];
|
|
29
|
+
// Widths at which the library's own components change style by themselves
|
|
30
|
+
// (the (max-device-width: 415px) rules of rows, buttons and inputs). A group's
|
|
31
|
+
// fit can jump there, so both sides are always sampled.
|
|
32
|
+
export const LIBRARY_WIDTHS = [415, 416];
|
|
33
|
+
|
|
34
|
+
const bare = (id) => (typeof id === "string" && id.charAt(0) === "#" ? id.slice(1) : id == null ? id : String(id));
|
|
35
|
+
|
|
36
|
+
const RESPONSIVE_KEYS = {
|
|
37
|
+
row: ["colat", "column", "resprop", "toColumn"],
|
|
38
|
+
cards: ["colat", "column", "resprop"],
|
|
39
|
+
nav: ["breakpoint"],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function groupKind(el) {
|
|
43
|
+
if (el.type === "row") return "row";
|
|
44
|
+
if (el.type === "cards") return "cards";
|
|
45
|
+
if (el.type === "nav") return "nav";
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function memberCount(el, kind) {
|
|
50
|
+
if (kind === "row") return Array.isArray(el.children) ? el.children.length : 0;
|
|
51
|
+
return Array.isArray(el.items) ? el.items.length : 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Every group on the page, in document order. `authored` is true when the
|
|
56
|
+
* author already wrote responsive behaviour for it; such a group is left alone.
|
|
57
|
+
*/
|
|
58
|
+
export function findGroups(elements) {
|
|
59
|
+
const out = [];
|
|
60
|
+
const walk = (list, path, depth, parent) => {
|
|
61
|
+
if (!Array.isArray(list)) return;
|
|
62
|
+
list.forEach((el, i) => {
|
|
63
|
+
if (!el || typeof el !== "object") return;
|
|
64
|
+
const at = `${path}[${i}]`;
|
|
65
|
+
const kind = groupKind(el);
|
|
66
|
+
let inner = parent;
|
|
67
|
+
let innerDepth = depth;
|
|
68
|
+
if (kind) {
|
|
69
|
+
const id = el.id == null || el.id === "" ? null : bare(String(el.id));
|
|
70
|
+
out.push({
|
|
71
|
+
id,
|
|
72
|
+
kind,
|
|
73
|
+
path: at,
|
|
74
|
+
depth,
|
|
75
|
+
parent,
|
|
76
|
+
members: memberCount(el, kind),
|
|
77
|
+
authored: RESPONSIVE_KEYS[kind].some((k) => el[k] !== undefined),
|
|
78
|
+
});
|
|
79
|
+
inner = id;
|
|
80
|
+
innerDepth = depth + 1;
|
|
81
|
+
}
|
|
82
|
+
walk(el.children, `${at}.children`, innerDepth, inner);
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
walk(elements, "elements", 0, null);
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The groups a synthesis pass can act on: unauthored, with two or more members. */
|
|
90
|
+
export function eligibleGroups(elements) {
|
|
91
|
+
return findGroups(elements).filter((g) => !g.authored && g.members >= 2);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function collectIds(elements) {
|
|
95
|
+
const ids = new Set();
|
|
96
|
+
const walk = (list) => {
|
|
97
|
+
if (!Array.isArray(list)) return;
|
|
98
|
+
for (const el of list) {
|
|
99
|
+
if (!el || typeof el !== "object") continue;
|
|
100
|
+
if (el.id != null && el.id !== "") ids.add(bare(String(el.id)));
|
|
101
|
+
walk(el.children);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
walk(elements);
|
|
105
|
+
return ids;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A copy of `elements` in which every group without an id gets one, so that a
|
|
110
|
+
* node can target it. Ids are `prefix` plus the group's document-order index;
|
|
111
|
+
* a clash with an existing id appends a letter. Untouched elements are shared.
|
|
112
|
+
*/
|
|
113
|
+
export function ensureIds(elements, prefix = "nod-g") {
|
|
114
|
+
const taken = collectIds(elements);
|
|
115
|
+
let index = 0;
|
|
116
|
+
const fresh = (n) => {
|
|
117
|
+
let base = `${prefix}${n}`;
|
|
118
|
+
let id = base;
|
|
119
|
+
let k = 0;
|
|
120
|
+
while (taken.has(id)) id = `${base}${String.fromCharCode(97 + (k++ % 26))}${k > 26 ? k : ""}`;
|
|
121
|
+
taken.add(id);
|
|
122
|
+
return id;
|
|
123
|
+
};
|
|
124
|
+
const walk = (list) => {
|
|
125
|
+
if (!Array.isArray(list)) return list;
|
|
126
|
+
let changed = false;
|
|
127
|
+
const next = list.map((el) => {
|
|
128
|
+
if (!el || typeof el !== "object") return el;
|
|
129
|
+
let out = el;
|
|
130
|
+
if (groupKind(el)) {
|
|
131
|
+
const n = index++;
|
|
132
|
+
if (el.id == null || el.id === "") out = { ...out, id: fresh(n) };
|
|
133
|
+
}
|
|
134
|
+
const kids = walk(el.children);
|
|
135
|
+
if (kids !== el.children) out = { ...(out === el ? el : out), children: kids };
|
|
136
|
+
if (out !== el) changed = true;
|
|
137
|
+
return out;
|
|
138
|
+
});
|
|
139
|
+
return changed ? next : list;
|
|
140
|
+
};
|
|
141
|
+
return walk(elements);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* ---------- solving g(v) = 0 ---------- */
|
|
145
|
+
|
|
146
|
+
/** Where the line through two samples crosses zero, or null if it is flat. */
|
|
147
|
+
export function affineRoot(a, b) {
|
|
148
|
+
const dv = b.v - a.v;
|
|
149
|
+
const dg = b.g - a.g;
|
|
150
|
+
if (dv === 0 || dg === 0) return null;
|
|
151
|
+
return a.v - (a.g * dv) / dg;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Least-squares line through samples {v, y}: slope, intercept, worst residual. */
|
|
155
|
+
export function fitAffine(samples, key = "y") {
|
|
156
|
+
const n = samples.length;
|
|
157
|
+
if (n < 2) return null;
|
|
158
|
+
let sv = 0, sy = 0, svv = 0, svy = 0;
|
|
159
|
+
for (const s of samples) { sv += s.v; sy += s[key]; svv += s.v * s.v; svy += s.v * s[key]; }
|
|
160
|
+
const den = n * svv - sv * sv;
|
|
161
|
+
if (den === 0) return null;
|
|
162
|
+
const slope = (n * svy - sv * sy) / den;
|
|
163
|
+
const intercept = (sy - slope * sv) / n;
|
|
164
|
+
let residual = 0;
|
|
165
|
+
for (const s of samples) residual = Math.max(residual, Math.abs(s[key] - (intercept + slope * s.v)));
|
|
166
|
+
return { slope, intercept, residual };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const fitsBy = (key) => (s) => !(typeof s[key] === "number") || s[key] <= 0;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Adjacent sample pairs between which an arrangement starts or stops fitting.
|
|
173
|
+
* `key` is "g" for the wide arrangement and "gn" for the narrow one.
|
|
174
|
+
*/
|
|
175
|
+
export function brackets(samples, key = "g") {
|
|
176
|
+
const fits = fitsBy(key);
|
|
177
|
+
const sorted = [...samples].filter((s) => typeof s[key] === "number").sort((a, b) => a.v - b.v);
|
|
178
|
+
const out = [];
|
|
179
|
+
for (let i = 0; i + 1 < sorted.length; i++) {
|
|
180
|
+
if (fits(sorted[i]) !== fits(sorted[i + 1])) {
|
|
181
|
+
const lo = sorted[i], hi = sorted[i + 1];
|
|
182
|
+
out.push([{ ...lo, g: lo[key] }, { ...hi, g: hi[key] }]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The next integer width to measure inside a bracket. For the first probes it
|
|
190
|
+
* is the affine root, rounded and nudged one pixel inside when it lands on an
|
|
191
|
+
* end: where sizes are affine in the width that pins the threshold to the
|
|
192
|
+
* pixel in two probes. After `affineTries` probes it bisects, which bounds the
|
|
193
|
+
* cost where they are not (a capped container, a scrollbar appearing).
|
|
194
|
+
* Returns null once the bracket is one pixel wide, the resolution of a viewport.
|
|
195
|
+
*/
|
|
196
|
+
export function nextProbe(lo, hi, tries = 0, affineTries = 3) {
|
|
197
|
+
if (hi.v - lo.v <= 1) return null;
|
|
198
|
+
const mid = Math.floor((lo.v + hi.v) / 2);
|
|
199
|
+
if (tries >= affineTries) return mid;
|
|
200
|
+
const r = affineRoot(lo, hi);
|
|
201
|
+
if (r == null || !Number.isFinite(r)) return mid;
|
|
202
|
+
let v = Math.round(r);
|
|
203
|
+
if (v <= lo.v) v = lo.v + 1;
|
|
204
|
+
if (v >= hi.v) v = hi.v - 1;
|
|
205
|
+
return v;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The integer viewport intervals on which the wide arrangement does not fit,
|
|
210
|
+
* from samples refined to one-pixel brackets. Below the narrowest sample and
|
|
211
|
+
* above the widest, the nearest sample's state is assumed to hold.
|
|
212
|
+
*/
|
|
213
|
+
export function narrowIntervals(samples, key = "g") {
|
|
214
|
+
const fits = fitsBy(key);
|
|
215
|
+
const s = [...samples].filter((x) => typeof x[key] === "number").sort((a, b) => a.v - b.v);
|
|
216
|
+
if (!s.length) return [];
|
|
217
|
+
const out = [];
|
|
218
|
+
let start = fits(s[0]) ? null : 0;
|
|
219
|
+
for (let i = 0; i + 1 < s.length; i++) {
|
|
220
|
+
const a = s[i];
|
|
221
|
+
const b = s[i + 1];
|
|
222
|
+
if (fits(a) === fits(b)) continue;
|
|
223
|
+
// A bracket still wider than one pixel (the probe cap stopped it) is
|
|
224
|
+
// unresolved; its widths go to the narrow side, which overflows less.
|
|
225
|
+
if (!fits(a)) { out.push([start, b.v - 1]); start = null; }
|
|
226
|
+
else start = a.v + 1;
|
|
227
|
+
}
|
|
228
|
+
if (start !== null) out.push([start, MAX_WIDTH]);
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Translate a measured interval into the width the runtime compares against.
|
|
234
|
+
* Measurements are taken at integer `innerWidth`; `resprop` compares
|
|
235
|
+
* `visualViewport.width`, which is narrower by the scrollbar. Each end is
|
|
236
|
+
* mapped through the sample at that width, so the narrow arrangement covers
|
|
237
|
+
* every non-fitting width and none of the fitting ones.
|
|
238
|
+
*/
|
|
239
|
+
export function toRuntimeRange(interval, samples, unit) {
|
|
240
|
+
if (unit === "innerWidth") return interval;
|
|
241
|
+
const at = new Map(samples.map((s) => [s.v, s]));
|
|
242
|
+
const map = (v, edge) => {
|
|
243
|
+
if (v === 0 || v === MAX_WIDTH) return v;
|
|
244
|
+
const s = at.get(v);
|
|
245
|
+
if (!s || typeof s.vv !== "number") return v;
|
|
246
|
+
return edge === "lo" ? Math.floor(s.vv) : Math.ceil(s.vv);
|
|
247
|
+
};
|
|
248
|
+
return [map(interval[0], "lo"), map(interval[1], "hi")];
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Split [0, MAX_WIDTH] into the narrow intervals given and the wide ones
|
|
253
|
+
* between them, in ascending order: every width belongs to exactly one.
|
|
254
|
+
*/
|
|
255
|
+
export function partition(narrow) {
|
|
256
|
+
const out = [];
|
|
257
|
+
let next = 0;
|
|
258
|
+
for (const [lo, hi] of [...narrow].sort((a, b) => a[0] - b[0])) {
|
|
259
|
+
if (lo > next) out.push({ narrow: false, range: [next, lo - 1] });
|
|
260
|
+
out.push({ narrow: true, range: [lo, hi] });
|
|
261
|
+
next = hi + 1;
|
|
262
|
+
}
|
|
263
|
+
if (next <= MAX_WIDTH) out.push({ narrow: false, range: [next, MAX_WIDTH] });
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The nodes for solved groups, in document order and, within a group, from
|
|
269
|
+
* narrow viewports to wide. `solved` maps a group id to its narrow intervals.
|
|
270
|
+
* A group gets one node per interval of its partition, wide ones included, so
|
|
271
|
+
* its arrangement is stated at every width and no library default (the 700px
|
|
272
|
+
* cards switch, the 1200px nav breakpoint) is left to decide any of them.
|
|
273
|
+
*/
|
|
274
|
+
export function navHull(ranges) {
|
|
275
|
+
if (!ranges.length) return ranges;
|
|
276
|
+
if (ranges.length === 1 && ranges[0][0] === 0) return ranges;
|
|
277
|
+
return [[0, Math.max(...ranges.map((r) => r[1]))]];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function synthesize(groups, solved) {
|
|
281
|
+
const nodes = [];
|
|
282
|
+
for (const g of groups) {
|
|
283
|
+
let ranges = solved.get ? solved.get(g.id) : solved[g.id];
|
|
284
|
+
if (!g.id || !Array.isArray(ranges)) continue;
|
|
285
|
+
// A nav can only collapse below one width. If its bar does not fit on
|
|
286
|
+
// some interval that starts above zero, it collapses from zero up to
|
|
287
|
+
// the end of the last such interval: never overflowing, at the cost of
|
|
288
|
+
// collapsing early in between (respond() reports it as widened).
|
|
289
|
+
if (g.kind === "nav") ranges = navHull(ranges);
|
|
290
|
+
for (const part of partition(ranges)) {
|
|
291
|
+
const mode = g.kind === "nav" ? (part.narrow ? "collapse" : "expand") : (part.narrow ? "column" : "row");
|
|
292
|
+
nodes.push({ op: { name: ARRANGE, mode }, target: [g.id], range: part.range });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return nodes;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/* ---------- lowering `arrange` onto the runtime ---------- */
|
|
299
|
+
|
|
300
|
+
const toInt = (x) => (typeof x === "number" ? Math.round(x) : parseInt(x, 10));
|
|
301
|
+
|
|
302
|
+
function parseRange(range) {
|
|
303
|
+
if (!Array.isArray(range) || range.length !== 2) return null;
|
|
304
|
+
const lo = toInt(range[0]);
|
|
305
|
+
const hi = toInt(range[1]);
|
|
306
|
+
if (!Number.isFinite(lo) || !Number.isFinite(hi) || lo < 0 || hi < lo) return null;
|
|
307
|
+
return [lo, hi];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const overlaps = (a, b) => a[0] <= b[1] && b[0] <= a[1];
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Lower every `arrange` node onto the element it targets, returning a new
|
|
314
|
+
* element array; the caller's arrays are never written. A `row` or `cards`
|
|
315
|
+
* group gets one `resprop` entry per range (and `cards` loses its built-in
|
|
316
|
+
* 700px column switch, which would otherwise write the same property); a
|
|
317
|
+
* `nav` gets its `breakpoint`. Of two nodes whose ranges overlap on one
|
|
318
|
+
* element, the first declared is kept and the later one reported.
|
|
319
|
+
*/
|
|
320
|
+
export function lowerArrange(elements, nodes) {
|
|
321
|
+
const applied = [];
|
|
322
|
+
const skipped = [];
|
|
323
|
+
const plan = new Map();
|
|
324
|
+
(Array.isArray(nodes) ? nodes : []).forEach((node, i) => {
|
|
325
|
+
if (!node || !node.op || typeof node.op !== "object" || node.op.name !== ARRANGE) return;
|
|
326
|
+
const range = parseRange(node.range);
|
|
327
|
+
const targets = Array.isArray(node.target) ? node.target : [];
|
|
328
|
+
if (!range || !targets.length) { skipped.push({ node: i, reason: "needs target and range [lo, hi]" }); return; }
|
|
329
|
+
for (const t of targets) {
|
|
330
|
+
const id = bare(String(t));
|
|
331
|
+
const list = plan.get(id) || [];
|
|
332
|
+
const clash = list.find((p) => overlaps(p.range, range));
|
|
333
|
+
if (clash) { skipped.push({ node: i, target: id, reason: `overlaps node ${clash.node}` }); continue; }
|
|
334
|
+
list.push({ node: i, mode: node.op.mode || "column", range });
|
|
335
|
+
plan.set(id, list);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
if (!plan.size) return { elements, applied, skipped };
|
|
339
|
+
|
|
340
|
+
const lowerOne = (el) => {
|
|
341
|
+
const id = el.id == null ? null : bare(String(el.id));
|
|
342
|
+
const list = id && plan.get(id);
|
|
343
|
+
if (!list) return el;
|
|
344
|
+
const kind = groupKind(el);
|
|
345
|
+
const sorted = list.slice().sort((a, b) => a.range[0] - b.range[0]);
|
|
346
|
+
if ((kind === "row" || kind === "cards") && list.every((p) => p.mode === "column" || p.mode === "row")) {
|
|
347
|
+
const entries = sorted.map((p) => {
|
|
348
|
+
const e = { breakpoint: [p.range[0], p.range[1]], flexDirection: p.mode };
|
|
349
|
+
if (kind === "cards" && p.mode === "column") e.alignItems = "center";
|
|
350
|
+
return e;
|
|
351
|
+
});
|
|
352
|
+
list.forEach((p) => applied.push({ node: p.node, target: id, via: "resprop" }));
|
|
353
|
+
const out = { ...el, resprop: [...(Array.isArray(el.resprop) ? el.resprop.map((e) => ({ ...e })) : []), ...entries] };
|
|
354
|
+
if (kind === "cards") out.colat = false;
|
|
355
|
+
return out;
|
|
356
|
+
}
|
|
357
|
+
if (kind === "nav" && list.every((p) => p.mode === "collapse" || p.mode === "expand")) {
|
|
358
|
+
// A nav has one switch: collapsed on [0, b-1], expanded from b up.
|
|
359
|
+
// The nodes must say exactly that, in at most two pieces.
|
|
360
|
+
const collapsed = sorted.filter((p) => p.mode === "collapse");
|
|
361
|
+
const expanded = sorted.filter((p) => p.mode === "expand");
|
|
362
|
+
let at = null;
|
|
363
|
+
if (!collapsed.length && expanded.length === 1 && expanded[0].range[0] === 0) at = 0;
|
|
364
|
+
else if (collapsed.length === 1 && collapsed[0].range[0] === 0) {
|
|
365
|
+
const hi = collapsed[0].range[1];
|
|
366
|
+
if (!expanded.length) at = hi >= MAX_WIDTH ? MAX_WIDTH : hi + 1;
|
|
367
|
+
else if (expanded.length === 1 && expanded[0].range[0] === hi + 1) at = hi + 1;
|
|
368
|
+
}
|
|
369
|
+
if (at !== null) {
|
|
370
|
+
list.forEach((p) => applied.push({ node: p.node, target: id, via: "breakpoint" }));
|
|
371
|
+
return { ...el, breakpoint: `${at}px` };
|
|
372
|
+
}
|
|
373
|
+
list.forEach((p) => skipped.push({ node: p.node, target: id, reason: "a nav collapses below one width and expands above it; these ranges say otherwise" }));
|
|
374
|
+
return el;
|
|
375
|
+
}
|
|
376
|
+
list.forEach((p) => skipped.push({ node: p.node, target: id, reason: `mode "${p.mode}" does not apply to ${el.type}` }));
|
|
377
|
+
return el;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const walk = (list) => {
|
|
381
|
+
if (!Array.isArray(list)) return list;
|
|
382
|
+
let changed = false;
|
|
383
|
+
const next = list.map((el) => {
|
|
384
|
+
if (!el || typeof el !== "object") return el;
|
|
385
|
+
let out = lowerOne(el);
|
|
386
|
+
const kids = walk(el.children);
|
|
387
|
+
if (kids !== el.children) out = { ...(out === el ? el : out), children: kids };
|
|
388
|
+
if (out !== el) changed = true;
|
|
389
|
+
return out;
|
|
390
|
+
});
|
|
391
|
+
return changed ? next : list;
|
|
392
|
+
};
|
|
393
|
+
return { elements: walk(elements), applied, skipped };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/* ---------- measuring in a real browser ---------- */
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Evaluated inside the page. For each group: the width it has, the width its
|
|
400
|
+
* members need on one line (min-content of a nowrap flex container is the sum
|
|
401
|
+
* of its items' min-content contributions), and both viewport widths. A `cards`
|
|
402
|
+
* grid is measured with its first two cards, the narrowest wide arrangement.
|
|
403
|
+
*/
|
|
404
|
+
export const PROBE = (arg) => {
|
|
405
|
+
const groups = Array.isArray(arg) ? arg : arg.groups;
|
|
406
|
+
const fit = (!Array.isArray(arg) && arg.fit) || "min-content";
|
|
407
|
+
const iw = window.innerWidth;
|
|
408
|
+
const vv = window.visualViewport ? window.visualViewport.width : iw;
|
|
409
|
+
return groups.map((g) => {
|
|
410
|
+
let el = document.getElementById(g.id);
|
|
411
|
+
if (!el) return { id: g.id, missing: true, iw, vv };
|
|
412
|
+
if (g.kind === "nav") {
|
|
413
|
+
const view = el.firstElementChild;
|
|
414
|
+
if (!view) return { id: g.id, missing: true, iw, vv };
|
|
415
|
+
el = view;
|
|
416
|
+
}
|
|
417
|
+
// The width the group is given. A flex row parent allocates it, so
|
|
418
|
+
// the group's own box is right; any other parent (a block, a column,
|
|
419
|
+
// a centred column) offers its content width, and a group whose box
|
|
420
|
+
// has grown past that to fit its own content does not fit.
|
|
421
|
+
const box = el.getBoundingClientRect().width;
|
|
422
|
+
let have = box;
|
|
423
|
+
const parent = el.parentElement;
|
|
424
|
+
if (parent) {
|
|
425
|
+
const ps = getComputedStyle(parent);
|
|
426
|
+
const rowParent = ps.display.includes("flex") && ps.flexDirection.startsWith("row");
|
|
427
|
+
if (!rowParent) {
|
|
428
|
+
const offered = parent.clientWidth - (parseFloat(ps.paddingLeft) || 0) - (parseFloat(ps.paddingRight) || 0);
|
|
429
|
+
if (offered > 0) have = Math.min(box, offered);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
have = Math.min(have, document.documentElement.clientWidth);
|
|
433
|
+
const saved = el.getAttribute("style");
|
|
434
|
+
const hidden = [];
|
|
435
|
+
if (g.kind === "cards") {
|
|
436
|
+
[...el.children].slice(2).forEach((c) => { hidden.push([c, c.style.display]); c.style.display = "none"; });
|
|
437
|
+
}
|
|
438
|
+
el.style.flexDirection = "row";
|
|
439
|
+
el.style.flexWrap = "nowrap";
|
|
440
|
+
// min-content: the row fits while nothing overflows (members may wrap
|
|
441
|
+
// their text). max-content: it fits only while no member has to wrap.
|
|
442
|
+
el.style.width = fit === "max-content" ? "max-content" : "min-content";
|
|
443
|
+
el.style.maxWidth = "none";
|
|
444
|
+
el.style.minWidth = "0";
|
|
445
|
+
const need = el.getBoundingClientRect().width;
|
|
446
|
+
// The narrow arrangement's own need: its widest member. Where even
|
|
447
|
+
// that exceeds what the group has, no arrangement in the family fits.
|
|
448
|
+
let needNarrow = null;
|
|
449
|
+
if (g.kind !== "nav") {
|
|
450
|
+
el.style.flexDirection = "column";
|
|
451
|
+
el.style.width = "min-content";
|
|
452
|
+
needNarrow = el.getBoundingClientRect().width;
|
|
453
|
+
}
|
|
454
|
+
if (saved == null) el.removeAttribute("style"); else el.setAttribute("style", saved);
|
|
455
|
+
hidden.forEach(([c, d]) => { c.style.display = d; });
|
|
456
|
+
return { id: g.id, have, need, needNarrow, iw, vv };
|
|
457
|
+
});
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
/** A page that renders whatever spec it is handed, for respond() to drive. */
|
|
461
|
+
export function harnessHTML(designerURL) {
|
|
462
|
+
return `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">` +
|
|
463
|
+
`<meta name="viewport" content="width=device-width, initial-scale=1"></head>` +
|
|
464
|
+
`<body><div id="mount"></div><script type="module">` +
|
|
465
|
+
`import { Des } from ${JSON.stringify(designerURL)};` +
|
|
466
|
+
`window.__nodalityRender = (spec) => { const m = document.querySelector("#mount"); m.innerHTML = "";` +
|
|
467
|
+
`const d = new Des(); if (spec.defs) d.defs(spec.defs);` +
|
|
468
|
+
`d.nodes(spec.nodes || []).add(spec.elements || []).set({ mount: "#mount", code: false, elements: false }); };` +
|
|
469
|
+
`window.__nodalityReady = true;</script></body></html>`;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Measure, solve and emit. `renderAt(width, spec)` must leave the page showing
|
|
474
|
+
* `spec` freshly rendered at viewport `width`; respond() calls it for every
|
|
475
|
+
* measurement, so no listener or state carries over between widths.
|
|
476
|
+
*
|
|
477
|
+
* Groups are solved by depth, top-down first: a nested group is measured with
|
|
478
|
+
* the nodes already chosen for its ancestors in force, because its available
|
|
479
|
+
* width is whatever their arrangement leaves it. A parent's need in turn can
|
|
480
|
+
* depend on its children's arrangement, so pages with nested groups are solved
|
|
481
|
+
* again, every level with all other groups' nodes in force, until no interval
|
|
482
|
+
* changes (`passes`, `converged` in the result).
|
|
483
|
+
*/
|
|
484
|
+
export async function respond(page, spec, renderAt, opts = {}) {
|
|
485
|
+
const widths = (opts.widths || DEFAULT_SAMPLE_WIDTHS).slice().sort((a, b) => a - b);
|
|
486
|
+
const maxProbes = opts.maxProbes || 24;
|
|
487
|
+
const elements = ensureIds(spec.elements || []);
|
|
488
|
+
const userNodes = Array.isArray(spec.nodes) ? spec.nodes : [];
|
|
489
|
+
const groups = eligibleGroups(elements);
|
|
490
|
+
const maxPasses = opts.maxPasses || 4;
|
|
491
|
+
let measurements = 0;
|
|
492
|
+
|
|
493
|
+
// Solve one depth level, with `others` (the current nodes of every group
|
|
494
|
+
// not in the level) in force. Returns each group's narrow intervals and
|
|
495
|
+
// report entry.
|
|
496
|
+
// The sample widths for a level: the grid, the library's own style
|
|
497
|
+
// widths, and both sides of every switch of the other groups. A group's
|
|
498
|
+
// fit can jump where a neighbour switches (a nested row that stacks makes
|
|
499
|
+
// its parent need less); sampling there puts every such jump on a sample
|
|
500
|
+
// width, where bracketing resolves it instead of missing two changes in
|
|
501
|
+
// one cell.
|
|
502
|
+
const lo = widths[0], hi = widths[widths.length - 1];
|
|
503
|
+
const levelWidths = (others) => {
|
|
504
|
+
const ws = new Set([...widths, ...(opts.libraryWidths || LIBRARY_WIDTHS).filter((w) => w > lo && w < hi)]);
|
|
505
|
+
for (const n of others) {
|
|
506
|
+
for (const b of [n.range[0], n.range[1] + 1]) {
|
|
507
|
+
for (const w of [b - 1, b]) if (w > lo && w < hi) ws.add(w);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return [...ws].sort((a, b) => a - b);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
const solveLevel = async (level, others) => {
|
|
514
|
+
const grid = levelWidths(others);
|
|
515
|
+
// A nav is measured showing its desktop bar, whatever the width.
|
|
516
|
+
const measureSpec = {
|
|
517
|
+
...spec,
|
|
518
|
+
elements: lowerArrange(forceDesktop(elements, level), others).elements,
|
|
519
|
+
nodes: userNodes,
|
|
520
|
+
};
|
|
521
|
+
const samples = new Map(level.map((g) => [g.id, []]));
|
|
522
|
+
const measureAt = async (width, only) => {
|
|
523
|
+
await renderAt(width, measureSpec);
|
|
524
|
+
measurements++;
|
|
525
|
+
const found = await page.evaluate(PROBE, {
|
|
526
|
+
groups: only.map((g) => ({ id: g.id, kind: g.kind })),
|
|
527
|
+
fit: opts.fit || "min-content",
|
|
528
|
+
});
|
|
529
|
+
for (const f of found) {
|
|
530
|
+
if (f.missing) continue;
|
|
531
|
+
samples.get(f.id).push({
|
|
532
|
+
v: width, g: f.need - f.have, need: f.need, have: f.have, vv: f.vv, iw: f.iw,
|
|
533
|
+
...(typeof f.needNarrow === "number" ? { gn: f.needNarrow - f.have, needNarrow: f.needNarrow } : {}),
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
for (const w of grid) await measureAt(w, level);
|
|
538
|
+
const solved = new Map();
|
|
539
|
+
const reports = new Map();
|
|
540
|
+
for (const g of level) {
|
|
541
|
+
const s = samples.get(g.id);
|
|
542
|
+
// Probes are counted per root: per arrangement (g or gn) and per
|
|
543
|
+
// cell of the sample grid the root lies in, so one root's cost
|
|
544
|
+
// never depends on how many others the group has.
|
|
545
|
+
const cell = (v) => grid.filter((w) => w <= v).length;
|
|
546
|
+
// The wide arrangement's fit (g) and the narrow one's (gn) each
|
|
547
|
+
// have their own budget, so infeasible widths never use up the
|
|
548
|
+
// probes that decide the nodes.
|
|
549
|
+
const tries = new Map();
|
|
550
|
+
const spent = { g: 0, gn: 0 };
|
|
551
|
+
let probes = 0;
|
|
552
|
+
for (;;) {
|
|
553
|
+
const open = [];
|
|
554
|
+
for (const key of ["g", "gn"]) {
|
|
555
|
+
if (spent[key] >= maxProbes) continue;
|
|
556
|
+
for (const [lo, hi] of brackets(s, key)) {
|
|
557
|
+
const k = `${key}:${cell(lo.v)}`;
|
|
558
|
+
const v = nextProbe(lo, hi, tries.get(k) || 0);
|
|
559
|
+
if (v === null) continue;
|
|
560
|
+
tries.set(k, (tries.get(k) || 0) + 1);
|
|
561
|
+
open.push([key, v]);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (!open.length) break;
|
|
565
|
+
const widthsNow = [...new Set(open.map(([, v]) => v))];
|
|
566
|
+
for (const [key] of open) spent[key]++;
|
|
567
|
+
for (const v of widthsNow) { await measureAt(v, [g]); probes++; }
|
|
568
|
+
}
|
|
569
|
+
const unit = g.kind === "nav" ? "innerWidth" : "visualViewport";
|
|
570
|
+
const intervals = narrowIntervals(s).map((iv) => toRuntimeRange(iv, s, unit));
|
|
571
|
+
solved.set(g.id, intervals);
|
|
572
|
+
const sorted = [...s].sort((a, b) => a.v - b.v);
|
|
573
|
+
reports.set(g.id, {
|
|
574
|
+
id: g.id, kind: g.kind, depth: g.depth, parent: g.parent, grid,
|
|
575
|
+
widened: g.kind === "nav" && JSON.stringify(navHull(intervals)) !== JSON.stringify(intervals),
|
|
576
|
+
samples: sorted, probes, intervals,
|
|
577
|
+
// Widths (innerWidth) where even the narrow arrangement does not
|
|
578
|
+
// fit: a member wider than the group. No arrangement fixes it.
|
|
579
|
+
infeasible: narrowIntervals(s, "gn"),
|
|
580
|
+
roots: brackets(sorted).map(([lo, hi]) => [lo.v, hi.v]),
|
|
581
|
+
need: fitAffine(sorted.filter((x) => x.v >= 320), "need"),
|
|
582
|
+
have: fitAffine(sorted.filter((x) => x.v >= 320), "have"),
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
return { solved, reports };
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
// The first pass is top-down: each level is measured with its ancestors'
|
|
589
|
+
// nodes in force and its descendants in their defaults. A parent's need
|
|
590
|
+
// can depend on how its children are arranged, so on a page with nested
|
|
591
|
+
// groups every level is then solved again with all other groups' current
|
|
592
|
+
// nodes in force, until a pass changes no interval (at most maxPasses).
|
|
593
|
+
const depths = [...new Set(groups.map((g) => g.depth))].sort((a, b) => a - b);
|
|
594
|
+
const intervalsById = new Map();
|
|
595
|
+
const reportById = new Map();
|
|
596
|
+
const others = (level) => synthesize(groups.filter((g) => !level.includes(g) && intervalsById.has(g.id)), intervalsById);
|
|
597
|
+
let passes = 0;
|
|
598
|
+
let converged = depths.length < 2;
|
|
599
|
+
for (let pass = 0; pass < maxPasses; pass++) {
|
|
600
|
+
passes++;
|
|
601
|
+
let changed = false;
|
|
602
|
+
for (const depth of depths) {
|
|
603
|
+
const level = groups.filter((g) => g.depth === depth);
|
|
604
|
+
const { solved, reports } = await solveLevel(level, others(level));
|
|
605
|
+
for (const g of level) {
|
|
606
|
+
const next = solved.get(g.id);
|
|
607
|
+
if (pass > 0 && JSON.stringify(intervalsById.get(g.id)) !== JSON.stringify(next)) changed = true;
|
|
608
|
+
intervalsById.set(g.id, next);
|
|
609
|
+
reportById.set(g.id, { ...reports.get(g.id), pass });
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (depths.length < 2) break;
|
|
613
|
+
if (pass > 0 && !changed) { converged = true; break; }
|
|
614
|
+
}
|
|
615
|
+
const synthesized = synthesize(groups, intervalsById);
|
|
616
|
+
const report = groups.map((g) => reportById.get(g.id)).filter(Boolean);
|
|
617
|
+
|
|
618
|
+
return {
|
|
619
|
+
elements,
|
|
620
|
+
nodes: [...userNodes, ...synthesized],
|
|
621
|
+
synthesized,
|
|
622
|
+
report,
|
|
623
|
+
measurements,
|
|
624
|
+
passes,
|
|
625
|
+
converged,
|
|
626
|
+
skipped: findGroups(elements).filter((g) => g.authored || g.members < 2).map((g) => ({ id: g.id, kind: g.kind, reason: g.authored ? "authored" : "fewer than two members" })),
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function forceDesktop(elements, level) {
|
|
631
|
+
const navIds = new Set(level.filter((g) => g.kind === "nav").map((g) => g.id));
|
|
632
|
+
if (!navIds.size) return elements;
|
|
633
|
+
const walk = (list) => {
|
|
634
|
+
if (!Array.isArray(list)) return list;
|
|
635
|
+
let changed = false;
|
|
636
|
+
const next = list.map((el) => {
|
|
637
|
+
if (!el || typeof el !== "object") return el;
|
|
638
|
+
let out = el.type === "nav" && navIds.has(el.id == null ? null : bare(String(el.id))) ? { ...el, breakpoint: "0px" } : el;
|
|
639
|
+
const kids = walk(el.children);
|
|
640
|
+
if (kids !== el.children) out = { ...out, children: kids };
|
|
641
|
+
if (out !== el) changed = true;
|
|
642
|
+
return out;
|
|
643
|
+
});
|
|
644
|
+
return changed ? next : list;
|
|
645
|
+
};
|
|
646
|
+
return walk(elements);
|
|
647
|
+
}
|
package/lib/scroll-video.js
CHANGED
package/lib/seo.js
CHANGED
package/lib/stacker.js
CHANGED
package/lib/suggest.js
CHANGED
package/lib/theme.js
CHANGED