@vectoriox/iox-ui 5.89.0 → 5.90.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/design-core/index.d.ts +523 -2
- package/fesm2022/vectoriox-iox-ui-design-core.mjs +581 -1
- package/fesm2022/vectoriox-iox-ui-design-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs +8403 -4898
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-harness-core.mjs +12 -1
- package/fesm2022/vectoriox-iox-ui-harness-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-scene-core.mjs +33 -6
- package/fesm2022/vectoriox-iox-ui-scene-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui.mjs +3 -2
- package/fesm2022/vectoriox-iox-ui.mjs.map +1 -1
- package/generation-core/index.d.ts +1735 -507
- package/harness-core/index.d.ts +52 -3
- package/package.json +1 -1
- package/scene-core/index.d.ts +19 -2
- package/src/design-core/flavor-spec.generated.ts +500 -0
- package/src/design-core/font-metrics.ts +105 -0
- package/src/design-core/public-api.ts +1 -0
- package/src/generation-core/ai-run.ts +11 -0
- package/src/generation-core/audit-site.ts +5 -0
- package/src/generation-core/check-layout.ts +940 -0
- package/src/generation-core/compose-layout.ts +120 -34
- package/src/generation-core/compose-site.ts +122 -3
- package/src/generation-core/layout-drift.ts +78 -0
- package/src/generation-core/layout-edits.ts +122 -0
- package/src/generation-core/layout-fingerprint.ts +203 -0
- package/src/generation-core/layout-language.ts +195 -12
- package/src/generation-core/layout-look.ts +57 -0
- package/src/generation-core/layout-photos.ts +19 -0
- package/src/generation-core/layout-render-check.ts +275 -0
- package/src/generation-core/layout-section-fallback.ts +156 -0
- package/src/generation-core/layout-type.ts +48 -0
- package/src/generation-core/layout-wire.ts +282 -0
- package/src/generation-core/nav-shape.ts +23 -1
- package/src/generation-core/photo-inventory.ts +250 -0
- package/src/generation-core/plan-edits.ts +25 -2
- package/src/generation-core/plan-wire.ts +19 -2
- package/src/generation-core/public-api.ts +11 -0
- package/src/generation-core/site-plan.model.ts +16 -0
- package/src/generation-core/site-spec.generated.ts +759 -2
- package/src/generation-core/validate-site-plan.ts +55 -1
- package/src/harness-core/generation-view.ts +52 -1
- package/src/scene-core/scroll-driver.ts +14 -2
- package/src/scene-core/scroll-range.ts +23 -3
- package/styles/iox-forms.css +4 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { parseCssColour, relativeLuminance } from '@vectoriox/iox-ui/design-core';
|
|
2
|
+
|
|
3
|
+
import { driftOverlap, layoutGlideDepth } from './layout-drift';
|
|
4
|
+
import { layerShownSpans, LayoutIssue, LayoutIssueCode, LayoutPage, shownTogether } from './layout-language';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The render check of a composed page (M8-G2-D8), split where it can run. `measureLayout` runs in a BROWSER (the owner's,
|
|
8
|
+
* on the Plan step — iox-cms has none): it reads the rendered boxes of the composed page's layers. `wordsOverPhotos` names
|
|
9
|
+
* the photo behind each word box and the part of the frame it covers; iox-cms reads those pixels (`pixelsUnder`, on the
|
|
10
|
+
* photo's own RGBA after `coverCrop`); `judgeLayoutRender` turns both into issues with a path, like `checkLayout`. The
|
|
11
|
+
* rules are the prototype's (`prototypes/breaking-limits/code/render-check.cjs`); its Playwright run stays the harness's.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export enum LayoutViewport {
|
|
15
|
+
Desktop = 'desktop',
|
|
16
|
+
Phone = 'phone',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The two widths a composed page is checked at (the prototype's). */
|
|
20
|
+
export const LAYOUT_VIEWPORTS: Readonly<Record<LayoutViewport, { width: number; height: number }>> = {
|
|
21
|
+
[LayoutViewport.Desktop]: { width: 1440, height: 900 },
|
|
22
|
+
[LayoutViewport.Phone]: { width: 390, height: 844 },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export interface LayoutRect { l: number; t: number; r: number; b: number }
|
|
26
|
+
|
|
27
|
+
export interface MeasuredLayer {
|
|
28
|
+
/** `sections[i].layers[j]`. */
|
|
29
|
+
path: string;
|
|
30
|
+
section: number;
|
|
31
|
+
kind: string;
|
|
32
|
+
role: string;
|
|
33
|
+
bleed: boolean;
|
|
34
|
+
scrim: boolean;
|
|
35
|
+
/** It rides a sideways track: where it stands at rest is not where it is read. */
|
|
36
|
+
track: boolean;
|
|
37
|
+
z: number;
|
|
38
|
+
box: LayoutRect;
|
|
39
|
+
/** Where its words are (a text's line boxes, a button's box); null when it carries none. */
|
|
40
|
+
words: LayoutRect | null;
|
|
41
|
+
/** On a laptop, in a held section: the stretches of its play its opacity tracks show it (`layerShownSpans`); absent,
|
|
42
|
+
* it shows throughout. Measured at rest every layer shows, so words that take turns would read as overlapping. */
|
|
43
|
+
shown?: Array<[number, number]>;
|
|
44
|
+
/** px. */
|
|
45
|
+
size: number;
|
|
46
|
+
weight: number;
|
|
47
|
+
/** The words' computed colour. */
|
|
48
|
+
color: string;
|
|
49
|
+
/** A photo layer's id. */
|
|
50
|
+
photo?: string;
|
|
51
|
+
/** The depth it glides at as the page scrolls (`layoutGlideDepth`); absent: it moves with the page. */
|
|
52
|
+
depth?: 'back' | 'front';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface MeasuredLayout {
|
|
56
|
+
viewport: `${LayoutViewport}`;
|
|
57
|
+
width: number;
|
|
58
|
+
height: number;
|
|
59
|
+
/** The page's own width as laid out: wider than `width` means it scrolls sideways. */
|
|
60
|
+
scrollWidth: number;
|
|
61
|
+
sections: Array<{ top: number; height: number; held: boolean }>;
|
|
62
|
+
layers: MeasuredLayer[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const WORD_KINDS = new Set(['word', 'heading', 'text', 'label', 'number', 'list', 'button']);
|
|
66
|
+
const textOf = (el: Element) => String(el.textContent ?? '').trim();
|
|
67
|
+
|
|
68
|
+
/** A layer node's class (`iox-node-<id>`) — what both renderers put on every node. */
|
|
69
|
+
export const layoutNodeClass = (prefix: string, i: number, j?: number) => `iox-node-${prefix}-s${i}${j === undefined ? '' : `-l${j}`}`;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* In the browser: the composed page's layers as rendered under `root` (the offscreen render, `prefix` its node id prefix),
|
|
73
|
+
* in the root's own coordinates. Run with the page at rest (scrolled to its top, entrances settled).
|
|
74
|
+
*/
|
|
75
|
+
export function measureLayout(root: Element, page: Pick<LayoutPage, 'sections'>, prefix: string, viewport: `${LayoutViewport}`): MeasuredLayout {
|
|
76
|
+
const origin = root.getBoundingClientRect();
|
|
77
|
+
const rel = (r: DOMRect | { left: number; top: number; right: number; bottom: number }): LayoutRect => ({ l: r.left - origin.left, t: r.top - origin.top, r: r.right - origin.left, b: r.bottom - origin.top });
|
|
78
|
+
const doc = root.ownerDocument;
|
|
79
|
+
const view = doc.defaultView;
|
|
80
|
+
const find = (cls: string) => root.querySelector(`.${cls}`);
|
|
81
|
+
const sections = page.sections.map((s, i) => {
|
|
82
|
+
const el = find(layoutNodeClass(prefix, i));
|
|
83
|
+
const r = el ? rel(el.getBoundingClientRect()) : { l: 0, t: 0, r: 0, b: 0 };
|
|
84
|
+
return { top: r.t, height: r.b - r.t, held: !!s.pin };
|
|
85
|
+
});
|
|
86
|
+
const layers: MeasuredLayer[] = [];
|
|
87
|
+
page.sections.forEach((s, i) => s.layers.forEach((l, j) => {
|
|
88
|
+
const el = find(layoutNodeClass(prefix, i, j));
|
|
89
|
+
if (!el) return;
|
|
90
|
+
const depth = layoutGlideDepth(s, l);
|
|
91
|
+
const spans = s.pin && viewport === LayoutViewport.Desktop ? layerShownSpans(l) : null;
|
|
92
|
+
const shown = spans && !(spans.length === 1 && spans[0][0] === 0 && spans[0][1] === 1) ? spans : null;
|
|
93
|
+
const style = view?.getComputedStyle(el);
|
|
94
|
+
const box = rel(el.getBoundingClientRect());
|
|
95
|
+
let words: LayoutRect | null = null;
|
|
96
|
+
if (WORD_KINDS.has(l.kind) && textOf(el)) {
|
|
97
|
+
if (l.kind === 'button') words = box;
|
|
98
|
+
else {
|
|
99
|
+
const range = doc.createRange();
|
|
100
|
+
range.selectNodeContents(el);
|
|
101
|
+
const tight = typeof range.getBoundingClientRect === 'function' ? range.getBoundingClientRect() : null;
|
|
102
|
+
words = tight && tight.width > 0 ? rel(tight) : box;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
layers.push({
|
|
106
|
+
path: `sections[${i}].layers[${j}]`, section: i, kind: l.kind, role: l.role ?? '', bleed: !!l.bleed && l.bleed !== 'none', scrim: !!l.scrim,
|
|
107
|
+
track: (s.track ?? 0) > 1, z: Number(style?.zIndex) || 0, box, words,
|
|
108
|
+
size: parseFloat(style?.fontSize ?? '') || 16, weight: Number(style?.fontWeight) || 400, color: style?.color ?? '',
|
|
109
|
+
...(l.kind === 'photo' && l.photo?.id ? { photo: l.photo.id } : {}),
|
|
110
|
+
...(depth ? { depth } : {}),
|
|
111
|
+
...(shown ? { shown } : {}),
|
|
112
|
+
});
|
|
113
|
+
}));
|
|
114
|
+
return { viewport, width: root.clientWidth || origin.width, height: LAYOUT_VIEWPORTS[viewport].height, scrollWidth: root.scrollWidth || origin.width, sections, layers };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const area = (r: LayoutRect) => Math.max(0, r.r - r.l) * Math.max(0, r.b - r.t);
|
|
118
|
+
const meet = (a: LayoutRect, b: LayoutRect): LayoutRect => ({ l: Math.max(a.l, b.l), t: Math.max(a.t, b.t), r: Math.min(a.r, b.r), b: Math.min(a.b, b.b) });
|
|
119
|
+
|
|
120
|
+
/** Where the letters are: a line box is the font's ascent + descent, taller than the ink — most of all for display faces. */
|
|
121
|
+
function ink(layer: MeasuredLayer): LayoutRect {
|
|
122
|
+
const w = layer.words!;
|
|
123
|
+
if (layer.kind === 'button') return w;
|
|
124
|
+
const [up, down] = layer.size >= 40 ? [0.2, 0.15] : [0.12, 0.08];
|
|
125
|
+
return { l: w.l, r: w.r, t: w.t + up * layer.size, b: w.b - down * layer.size };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Words on a photo: the photo under them (the topmost covering most of their box) and the part of its frame they cover. */
|
|
129
|
+
export interface PhotoWordsArea {
|
|
130
|
+
path: string;
|
|
131
|
+
viewport: `${LayoutViewport}`;
|
|
132
|
+
photo: string;
|
|
133
|
+
/** The frame's size in px (what `coverCrop` crops the photo to). */
|
|
134
|
+
frame: { w: number; h: number };
|
|
135
|
+
/** Fractions of the frame. */
|
|
136
|
+
region: { x0: number; x1: number; y0: number; y1: number };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The word boxes that sit on a photo — what iox-cms reads the pixels under. */
|
|
140
|
+
export function wordsOverPhotos(measured: ReadonlyArray<MeasuredLayout>): PhotoWordsArea[] {
|
|
141
|
+
const out: PhotoWordsArea[] = [];
|
|
142
|
+
for (const m of measured) {
|
|
143
|
+
const photos = m.layers.filter(l => l.photo);
|
|
144
|
+
for (const w of m.layers) {
|
|
145
|
+
if (!w.words || w.kind === 'button' || w.photo) continue;
|
|
146
|
+
const behind = photos
|
|
147
|
+
.filter(p => p.section === w.section && p.z <= w.z && area(meet(p.box, w.words!)) >= 0.5 * area(w.words!))
|
|
148
|
+
.sort((a, b) => b.z - a.z)[0];
|
|
149
|
+
if (!behind) continue;
|
|
150
|
+
const fw = Math.max(1, behind.box.r - behind.box.l);
|
|
151
|
+
const fh = Math.max(1, behind.box.b - behind.box.t);
|
|
152
|
+
const clamp = (v: number) => Math.min(1, Math.max(0, v));
|
|
153
|
+
out.push({
|
|
154
|
+
path: w.path, viewport: m.viewport, photo: behind.photo!, frame: { w: fw, h: fh },
|
|
155
|
+
region: { x0: clamp((w.words.l - behind.box.l) / fw), x1: clamp((w.words.r - behind.box.l) / fw), y0: clamp((w.words.t - behind.box.t) / fh), y1: clamp((w.words.b - behind.box.t) / fh) },
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The light under a word box: its mean relative luminance and how much it varies (a busy part of a photo). */
|
|
163
|
+
export interface PixelReading {
|
|
164
|
+
path: string;
|
|
165
|
+
viewport: `${LayoutViewport}`;
|
|
166
|
+
luminance: number;
|
|
167
|
+
spread: number;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const srgbToLinear = (v: number) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
|
|
171
|
+
const SAMPLES = 6000;
|
|
172
|
+
|
|
173
|
+
/** The light of a part of a photo (fractions of the PHOTO, RGBA bytes row by row) — what the server reads for each area. */
|
|
174
|
+
export function pixelsUnder(rgba: ArrayLike<number>, width: number, height: number, part: { x0: number; x1: number; y0: number; y1: number }): { luminance: number; spread: number } {
|
|
175
|
+
const x0 = Math.max(0, Math.floor(part.x0 * width)), x1 = Math.min(width, Math.ceil(part.x1 * width));
|
|
176
|
+
const y0 = Math.max(0, Math.floor(part.y0 * height)), y1 = Math.min(height, Math.ceil(part.y1 * height));
|
|
177
|
+
const count = Math.max(0, x1 - x0) * Math.max(0, y1 - y0);
|
|
178
|
+
if (!count) return { luminance: 0, spread: 0 };
|
|
179
|
+
const step = Math.max(1, Math.floor(count / SAMPLES));
|
|
180
|
+
let s = 0, s2 = 0, n = 0, k = 0;
|
|
181
|
+
for (let y = y0; y < y1; y++) for (let x = x0; x < x1; x++, k++) {
|
|
182
|
+
if (k % step) continue;
|
|
183
|
+
const i = (y * width + x) * 4;
|
|
184
|
+
const L = 0.2126 * srgbToLinear(rgba[i] / 255) + 0.7152 * srgbToLinear(rgba[i + 1] / 255) + 0.0722 * srgbToLinear(rgba[i + 2] / 255);
|
|
185
|
+
s += L; s2 += L * L; n++;
|
|
186
|
+
}
|
|
187
|
+
const mean = s / n;
|
|
188
|
+
return { luminance: mean, spread: Math.sqrt(Math.max(0, s2 / n - mean * mean)) };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The prototype's thresholds. */
|
|
192
|
+
export const RENDER_CHECK = {
|
|
193
|
+
/** Overlap counted when it covers more than this many px² AND this share of the smaller word box. */
|
|
194
|
+
overlapPx: 80,
|
|
195
|
+
overlapShare: 0.12,
|
|
196
|
+
/** px past its rows (desktop) before words count as spilling; px past its columns. */
|
|
197
|
+
spillDown: 10,
|
|
198
|
+
spillSide: 12,
|
|
199
|
+
/** The share of the first section's lead the first screen must show. */
|
|
200
|
+
firstScreen: 0.6,
|
|
201
|
+
/** WCAG: large words, other words. */
|
|
202
|
+
contrastLarge: 3,
|
|
203
|
+
contrastBody: 4.5,
|
|
204
|
+
/** The light's spread over which a part of a photo is busy. */
|
|
205
|
+
busy: 0.11,
|
|
206
|
+
} as const;
|
|
207
|
+
|
|
208
|
+
const ratio = (a: number, b: number) => (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
|
|
209
|
+
|
|
210
|
+
/** Measured boxes (each viewport) + the pixels under the words on photos → the render issues, by path, said once each. */
|
|
211
|
+
export function judgeLayoutRender(measured: ReadonlyArray<MeasuredLayout>, pixels: ReadonlyArray<PixelReading> = []): LayoutIssue[] {
|
|
212
|
+
const issues = new Map<string, LayoutIssue>();
|
|
213
|
+
const say = (key: string, path: string, message: string) => { if (!issues.has(key)) issues.set(key, { code: LayoutIssueCode.Render, path, message }); };
|
|
214
|
+
for (const m of measured) {
|
|
215
|
+
const on = m.viewport;
|
|
216
|
+
const seen = m.layers.filter(l => l.words && !l.track);
|
|
217
|
+
for (let a = 0; a < seen.length; a++) for (let b = a + 1; b < seen.length; b++) {
|
|
218
|
+
const A = seen[a], B = seen[b];
|
|
219
|
+
if (A.section !== B.section || !shownTogether(A.shown, B.shown)) continue;
|
|
220
|
+
const m2 = meet(ink(A), ink(B));
|
|
221
|
+
const ov = area(m2);
|
|
222
|
+
if (ov > RENDER_CHECK.overlapPx && ov > RENDER_CHECK.overlapShare * Math.min(area(ink(A)), area(ink(B)))) {
|
|
223
|
+
const what = A.kind === 'button' || B.kind === 'button' ? 'run into a button' : 'overlap';
|
|
224
|
+
say(`ov:${A.path}:${B.path}:${on}`, A.path, `${A.path} and ${B.path}: their words ${what} on the ${on} (${Math.round(m2.r - m2.l)}×${Math.round(m2.b - m2.t)}px) — move one, or give them more rows`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// Motion on (iox-ui#65): two word boxes at different depths move toward each other as the page scrolls.
|
|
228
|
+
const pageHeight = Math.max(m.height, ...m.sections.map(s => s.top + s.height));
|
|
229
|
+
for (let a = 0; a < seen.length; a++) for (let b = a + 1; b < seen.length; b++) {
|
|
230
|
+
const A = seen[a], B = seen[b];
|
|
231
|
+
if (A.section !== B.section || (!A.depth && !B.depth)) continue;
|
|
232
|
+
const sec = m.sections[A.section];
|
|
233
|
+
const clip = sec ? { l: -Infinity, r: Infinity, t: sec.top, b: sec.top + sec.height } : undefined;
|
|
234
|
+
const met = driftOverlap({ box: ink(A), depth: A.depth ?? null }, { box: ink(B), depth: B.depth ?? null },
|
|
235
|
+
{ width: m.width, height: m.height, pageHeight, phone: on === LayoutViewport.Phone }, clip);
|
|
236
|
+
if (met && met.area > RENDER_CHECK.overlapPx && met.area > RENDER_CHECK.overlapShare * Math.min(area(ink(A)), area(ink(B)))) {
|
|
237
|
+
const what = A.kind === 'button' || B.kind === 'button' ? 'run into a button' : 'run into each other';
|
|
238
|
+
const how = (l: MeasuredLayer) => l.depth ?? 'with the page';
|
|
239
|
+
say(`drift:${A.path}:${B.path}:${on}`, A.depth ? A.path : B.path,
|
|
240
|
+
`${A.path} (${how(A)}) and ${B.path} (${how(B)}): their words ${what} as the page scrolls on the ${on} (${Math.round(met.overlap.r - met.overlap.l)}×${Math.round(met.overlap.b - met.overlap.t)}px, ${Math.round(met.scroll)}px down) — give them the same depth, or more rows apart`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
for (const l of seen) {
|
|
244
|
+
const w = ink(l);
|
|
245
|
+
if (on === LayoutViewport.Desktop && l.kind !== 'button') {
|
|
246
|
+
const down = Math.max(w.b - l.box.b, l.box.t - w.t);
|
|
247
|
+
const side = Math.max(l.words!.r - l.box.r, l.box.l - l.words!.l);
|
|
248
|
+
if (down > RENDER_CHECK.spillDown) say(`spill:${l.path}`, l.path, `${l.path}: its words are ${Math.round(down)}px taller than its rows — more rows, fewer words or a smaller size`);
|
|
249
|
+
else if (side > RENDER_CHECK.spillSide && !l.bleed) say(`spillx:${l.path}`, l.path, `${l.path}: its words are ${Math.round(side)}px wider than its columns — more columns or a smaller size`);
|
|
250
|
+
}
|
|
251
|
+
if (!l.bleed && (l.words!.l < -2 || l.words!.r > m.width + 2)) say(`off:${l.path}:${on}`, l.path, `${l.path}: its words run off the ${on} screen`);
|
|
252
|
+
}
|
|
253
|
+
if (on === LayoutViewport.Desktop) {
|
|
254
|
+
for (const lead of m.layers.filter(l => l.section === 0 && l.role === 'lead')) {
|
|
255
|
+
const h = Math.max(1, lead.box.b - lead.box.t);
|
|
256
|
+
const shown = Math.max(0, Math.min(lead.box.b, m.height) - Math.max(lead.box.t, 0)) / h;
|
|
257
|
+
if (shown < RENDER_CHECK.firstScreen) say('first', lead.path, `${lead.path}: the first screen shows ${Math.round(shown * 100)}% of the lead before anyone scrolls — move it up into the first screen`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (on === LayoutViewport.Phone && m.scrollWidth > m.width + 1) say('sideways', 'sections', `the page scrolls sideways on a phone (${m.scrollWidth - m.width}px too wide)`);
|
|
261
|
+
}
|
|
262
|
+
const layerAt = new Map(measured.flatMap(m => m.layers.map(l => [`${m.viewport}:${l.path}`, l] as const)));
|
|
263
|
+
for (const px of pixels) {
|
|
264
|
+
const l = layerAt.get(`${px.viewport}:${px.path}`);
|
|
265
|
+
const colour = l ? parseCssColour(l.color) : null;
|
|
266
|
+
// Under a scrim the build chooses the scrim's strength from these same pixels (M7 `chooseScrim`).
|
|
267
|
+
if (!l || !colour || l.scrim) continue;
|
|
268
|
+
const big = l.size >= 24 || (l.size >= 18.6 && l.weight >= 700);
|
|
269
|
+
const need = big ? RENDER_CHECK.contrastLarge : RENDER_CHECK.contrastBody;
|
|
270
|
+
const r = ratio(relativeLuminance(colour), px.luminance);
|
|
271
|
+
if (r < need) say(`contrast:${px.path}`, px.path, `${px.path}: ${r.toFixed(1)}:1 against the photo behind it on the ${px.viewport} (needs ${need}) — another colour, a calm part, or off the photo`);
|
|
272
|
+
else if (px.spread > RENDER_CHECK.busy) say(`busy:${px.path}`, px.path, `${px.path}: sits on a busy part of a photo on the ${px.viewport} (light varies ${px.spread.toFixed(2)}) — move it to a calm part or onto the ground`);
|
|
273
|
+
}
|
|
274
|
+
return [...issues.values()];
|
|
275
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { LayoutIssue, LayoutLayer, LayoutPage, LayoutSection } from './layout-language';
|
|
2
|
+
import { PageSectionPlan } from './site-plan.model';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SILENT SECTION FALLBACK (Doron, 2026-11-09: "for now we need to do silent section fallback and not fallback whole page").
|
|
6
|
+
* A composed page that still fails after the mends and its one repair — or after the browser's check — no longer falls
|
|
7
|
+
* back whole: each section an issue names is given to the plan's READY-MADE section for the same place (the pattern the
|
|
8
|
+
* whole-page fallback would have stamped there, worn in the same look), or dropped where none fits and the page keeps its
|
|
9
|
+
* opening and its close. Then the page is judged again, once, exactly like any composed page. Only a page that still
|
|
10
|
+
* fails — an issue that names no section, a place with nothing to give it, a swap that breaks a section kept or a count
|
|
11
|
+
* of the page — falls back whole: the last resort, counted. A swap never leads to another swap. The owner sees a composed page; the swaps are recorded for us, the model's sections kept.
|
|
12
|
+
* Pure: the service and `ai:replay` run this one function.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** What became of one place of the model's page. */
|
|
16
|
+
export enum SectionSwapAction {
|
|
17
|
+
/** The plan's ready-made section stands there. */
|
|
18
|
+
Replaced = 'replaced',
|
|
19
|
+
/** Nothing fitted the place: the page goes on without it. */
|
|
20
|
+
Dropped = 'dropped',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Why a place was given away (and which ready-made section took it). */
|
|
24
|
+
export enum SectionSwapRule {
|
|
25
|
+
/** The page's first section: always the plan's opening, never dropped. */
|
|
26
|
+
Opening = 'opening',
|
|
27
|
+
/** The page's last section: always the plan's close, never dropped. */
|
|
28
|
+
Close = 'close',
|
|
29
|
+
/** A middle section: the ready-made section that shows the same list, holds the form, or says most of its words. */
|
|
30
|
+
Fit = 'fit',
|
|
31
|
+
/** A middle section no ready-made section fits: dropped. */
|
|
32
|
+
NoFit = 'no-fit',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** One place swapped, kept for us on the composed record — never shown to the owner. */
|
|
36
|
+
export interface SectionSwap {
|
|
37
|
+
/** The section's id (links to it keep working when it is replaced). */
|
|
38
|
+
id: string;
|
|
39
|
+
/** Its place in the model's page. */
|
|
40
|
+
at: number;
|
|
41
|
+
action: `${SectionSwapAction}`;
|
|
42
|
+
rule: `${SectionSwapRule}`;
|
|
43
|
+
/** The plan section that took it: its index in `page.sections` and its pattern. */
|
|
44
|
+
readyMade?: { at: number; pattern: string };
|
|
45
|
+
/** What the check said about it. */
|
|
46
|
+
why: string[];
|
|
47
|
+
/** The model's section as it stood. */
|
|
48
|
+
original: LayoutSection;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Why a page fell back whole after all (the last resort). */
|
|
52
|
+
export enum SectionFallbackLastResort {
|
|
53
|
+
/** Nothing left names a section (a count, a share, the order of sections), or after the swaps only such an issue is. */
|
|
54
|
+
PageWide = 'page-wide',
|
|
55
|
+
/** The place named is the opening or the close, and the plan has no ready-made section left for it. */
|
|
56
|
+
NoReadyMade = 'no-ready-made',
|
|
57
|
+
/** It names a section already given to a ready-made one. */
|
|
58
|
+
ReadyMadeNamed = 'ready-made-named',
|
|
59
|
+
/** After the swaps, the check names a section the model composed and kept (a neighbour the swap broke). */
|
|
60
|
+
Neighbour = 'neighbour',
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface SectionFallback {
|
|
64
|
+
/** The page as it now stands (ready-made places marked `readyMade`, dropped ones gone). */
|
|
65
|
+
page: LayoutPage;
|
|
66
|
+
swaps: SectionSwap[];
|
|
67
|
+
/** What is left: empty — the page composes. */
|
|
68
|
+
left: LayoutIssue[];
|
|
69
|
+
/** Set when the page must fall back whole after all. */
|
|
70
|
+
lastResort?: `${SectionFallbackLastResort}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A judge: the page settled (the mends) and what the gate still finds — static and sameness, as the service checks. */
|
|
74
|
+
export type SectionJudge = (page: LayoutPage) => { page: LayoutPage; issues: LayoutIssue[] };
|
|
75
|
+
|
|
76
|
+
const NAMED = /sections\[(\d+)\]/g;
|
|
77
|
+
|
|
78
|
+
/** The sections an issue names: its path's, and any its words name. */
|
|
79
|
+
export function sectionsNamed(issue: Pick<LayoutIssue, 'path' | 'message'>): number[] {
|
|
80
|
+
const at = new Set<number>();
|
|
81
|
+
for (const text of [issue.path, issue.message]) for (const m of String(text ?? '').matchAll(NAMED)) at.add(Number(m[1]));
|
|
82
|
+
return [...at].sort((a, b) => a - b);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const WORD = /[\p{L}\p{N}]{3,}/gu;
|
|
86
|
+
const wordsOf = (text: string): string[] => (String(text ?? '').toLowerCase().match(WORD) ?? []);
|
|
87
|
+
const layerWords = (l: LayoutLayer): string[] => [l.text ?? '', ...(l.items ?? []), ...(l.gallery?.slides ?? []).map(s => s.caption ?? ''), l.form?.title ?? '', l.form?.intro ?? ''].flatMap(wordsOf);
|
|
88
|
+
|
|
89
|
+
/** How well a ready-made section stands for a model's section: the same list, the form, the words they share. */
|
|
90
|
+
function fitScore(section: LayoutSection, plan: PageSectionPlan): number {
|
|
91
|
+
const layers = section.layers ?? [];
|
|
92
|
+
let score = 0;
|
|
93
|
+
if (plan.binding && layers.some(l => l.gallery?.source === plan.binding)) score += 10;
|
|
94
|
+
if ((plan.form || /^(contact|cta)\./.test(plan.pattern)) && layers.some(l => l.kind === 'form')) score += 5;
|
|
95
|
+
const said = new Set(layers.flatMap(layerWords));
|
|
96
|
+
score += new Set(Object.values(plan.slots ?? {}).flatMap(wordsOf).filter(w => said.has(w))).size;
|
|
97
|
+
return score;
|
|
98
|
+
}
|
|
99
|
+
/** The least a middle section's fit scores to be given a ready-made section (two shared words, a form or a list). */
|
|
100
|
+
const FIT_MIN = 2;
|
|
101
|
+
|
|
102
|
+
/** A ready-made stub at a place: no layers of its own — the composer stamps the plan section's pattern there. */
|
|
103
|
+
function readyMadeSection(original: LayoutSection, at: number, plan: PageSectionPlan): LayoutSection {
|
|
104
|
+
return { id: original.id, kind: 'content', screens: 1, rows: 1, layers: [], phone: [], readyMade: { at, pattern: plan.pattern } };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Give every section the issues name to the plan's ready-made section for its place, or drop it, then judge the page once
|
|
109
|
+
* more (a page-wide issue beside them may be cured by the swap — the judge says). `plan` is the page's ready-made
|
|
110
|
+
* sections (`PagePlan.sections`), in order; `issues` what is left after the mends and the one repair.
|
|
111
|
+
*/
|
|
112
|
+
export function fallBackBySection(page: LayoutPage, plan: ReadonlyArray<PageSectionPlan>, issues: ReadonlyArray<LayoutIssue>, judge: SectionJudge): SectionFallback {
|
|
113
|
+
const current: LayoutPage = structuredClone(page) as LayoutPage;
|
|
114
|
+
const swaps: SectionSwap[] = [];
|
|
115
|
+
const last = (lastResort: `${SectionFallbackLastResort}`, left: LayoutIssue[], at: LayoutPage = current): SectionFallback => ({ page: at, swaps, left, lastResort });
|
|
116
|
+
if (!issues.length) return { page: current, swaps, left: [] };
|
|
117
|
+
const named = new Map<number, string[]>();
|
|
118
|
+
for (const issue of issues) for (const i of sectionsNamed(issue)) if (i < current.sections.length) named.set(i, [...(named.get(i) ?? []), issue.message]);
|
|
119
|
+
if (!named.size) return last(SectionFallbackLastResort.PageWide, [...issues]);
|
|
120
|
+
if ([...named.keys()].some(i => current.sections[i].readyMade)) return last(SectionFallbackLastResort.ReadyMadeNamed, [...issues]);
|
|
121
|
+
const used = new Set(current.sections.map(s => s.readyMade?.at).filter((n): n is number => n !== undefined));
|
|
122
|
+
const lastAt = current.sections.length - 1;
|
|
123
|
+
const next = [...current.sections];
|
|
124
|
+
const drop = new Set<number>();
|
|
125
|
+
for (const [i, why] of [...named.entries()].sort((a, b) => a[0] - b[0])) {
|
|
126
|
+
const s = current.sections[i];
|
|
127
|
+
const record = (action: SectionSwapAction, rule: SectionSwapRule, readyMade?: { at: number; pattern: string }) =>
|
|
128
|
+
swaps.push({ id: s.id, at: i, action, rule, ...(readyMade ? { readyMade } : {}), why, original: structuredClone(s) });
|
|
129
|
+
if (i === 0 || i === lastAt) {
|
|
130
|
+
const at = i === 0 ? 0 : plan.length - 1;
|
|
131
|
+
if (!plan[at] || used.has(at)) return last(SectionFallbackLastResort.NoReadyMade, [...issues]);
|
|
132
|
+
used.add(at);
|
|
133
|
+
next[i] = readyMadeSection(s, at, plan[at]);
|
|
134
|
+
record(SectionSwapAction.Replaced, i === 0 ? SectionSwapRule.Opening : SectionSwapRule.Close, { at, pattern: plan[at].pattern });
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const best = plan
|
|
138
|
+
.map((p, at) => ({ at, score: at > 0 && at < plan.length - 1 && !used.has(at) ? fitScore(s, p) : -1 }))
|
|
139
|
+
.filter(c => c.score >= FIT_MIN)
|
|
140
|
+
.sort((a, b) => b.score - a.score || a.at - b.at)[0];
|
|
141
|
+
if (best) {
|
|
142
|
+
used.add(best.at);
|
|
143
|
+
next[i] = readyMadeSection(s, best.at, plan[best.at]);
|
|
144
|
+
record(SectionSwapAction.Replaced, SectionSwapRule.Fit, { at: best.at, pattern: plan[best.at].pattern });
|
|
145
|
+
} else {
|
|
146
|
+
drop.add(i);
|
|
147
|
+
record(SectionSwapAction.Dropped, SectionSwapRule.NoFit);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const judged = judge({ ...current, sections: next.filter((_, i) => !drop.has(i)) });
|
|
151
|
+
if (!judged.issues.length) return { page: judged.page, swaps, left: [] };
|
|
152
|
+
const kept = judged.issues.flatMap(sectionsNamed).filter(i => !judged.page.sections[i]?.readyMade);
|
|
153
|
+
const reason = kept.length ? SectionFallbackLastResort.Neighbour
|
|
154
|
+
: judged.issues.some(i => sectionsNamed(i).length) ? SectionFallbackLastResort.ReadyMadeNamed : SectionFallbackLastResort.PageWide;
|
|
155
|
+
return last(reason, judged.issues, judged.page);
|
|
156
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { NO_ELEMENT_STYLE } from '@vectoriox/iox-ui/composite-core';
|
|
2
|
+
import { DesignChoices, DesignMode, elementStyleFace, presetChoices, resolveDesignChoices, textWidthEm } from '@vectoriox/iox-ui/design-core';
|
|
3
|
+
|
|
4
|
+
import { LayoutBleed, LayoutLayer, LayoutLayerKind, LayoutRole, LayoutSize, screenWordOverflowPx } from './layout-language';
|
|
5
|
+
|
|
6
|
+
/** The design a page wears when only its look's preset is known: the preset in `mode` (else its own), else the default. */
|
|
7
|
+
export function layoutDesignOf(presetId: string | null | undefined, mode?: string | null): DesignChoices {
|
|
8
|
+
const id = String(presetId ?? '');
|
|
9
|
+
return presetChoices(id, (mode ?? undefined) as DesignMode | undefined) ?? presetChoices(id) ?? resolveDesignChoices(null);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The element style a layer wears, by kind, role and size (the round-four renderer's `elementStyleOf`). */
|
|
13
|
+
export function layoutElementStyle(l: Pick<LayoutLayer, 'kind' | 'role' | 'size'>): string {
|
|
14
|
+
switch (l.kind) {
|
|
15
|
+
case LayoutLayerKind.Word: return 'display';
|
|
16
|
+
case LayoutLayerKind.Heading: return l.role === LayoutRole.Lead ? 'h1' : 'h2';
|
|
17
|
+
case LayoutLayerKind.Text: return l.size === 'm' || l.size === 'l' ? 'lead' : 'body';
|
|
18
|
+
case LayoutLayerKind.List: return 'body';
|
|
19
|
+
case LayoutLayerKind.Label: return !l.size || l.size === 'xs' || l.size === 's' ? 'eyebrow' : 'small';
|
|
20
|
+
default: return NO_ELEMENT_STYLE;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** How wide a layer's words stand at 1em on one line, in the face its element style wears in `design` (iox-ui#46). */
|
|
25
|
+
export function layoutTextWidthEm(l: Pick<LayoutLayer, 'kind' | 'role' | 'size' | 'text'>, design: DesignChoices): number {
|
|
26
|
+
return textWidthEm(l.text ?? '', elementStyleFace(design, layoutElementStyle(l)));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The columns a `screen` word fills: its span, and half a column more for each side it bleeds off. */
|
|
30
|
+
export function screenWordColumns(l: Pick<LayoutLayer, 'col' | 'bleed'>): number {
|
|
31
|
+
const bleed = l.bleed && l.bleed !== LayoutBleed.None ? (l.bleed === LayoutBleed.Both ? 1 : 0.5) : 0;
|
|
32
|
+
return l.col[1] - l.col[0] + bleed;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The screens a composed page is judged at (the render check's): a laptop and a phone. */
|
|
36
|
+
const SCREENS: ReadonlyArray<{ width: number; phone: boolean }> = [{ width: 1440, phone: false }, { width: 390, phone: true }];
|
|
37
|
+
|
|
38
|
+
/** Where a `screen` word cannot fit even at its smallest size: the screen and by how many px — null when it fits both. */
|
|
39
|
+
export function screenWordMisfit(l: LayoutLayer, design: DesignChoices): { width: number; phone: boolean; px: number } | null {
|
|
40
|
+
if (l.size !== LayoutSize.Screen || !l.text) return null;
|
|
41
|
+
const em = layoutTextWidthEm(l, design);
|
|
42
|
+
for (const s of SCREENS) {
|
|
43
|
+
if (s.phone ? false : !!l.bleed && l.bleed !== LayoutBleed.None) continue;
|
|
44
|
+
const px = screenWordOverflowPx(em, s.phone ? 12 : screenWordColumns(l), s.width, s.phone);
|
|
45
|
+
if (px > 0) return { ...s, px };
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|