@perkyzz/emvb 0.2.1 → 0.3.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/package.json +1 -1
- package/src/admin/editor/A11yCopilot.tsx +126 -0
- package/src/admin/editor/CommandPalette.tsx +118 -0
- package/src/admin/editor/Editor.tsx +274 -12
- package/src/admin/editor/EditorOverlay.tsx +6 -0
- package/src/admin/editor/PerfMeter.tsx +121 -0
- package/src/admin/editor/ShareLink.tsx +164 -0
- package/src/admin/editor/Timeline.tsx +274 -0
- package/src/admin/editor/TopBar.tsx +4 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +12 -3
- package/src/admin/editor/canvas/SelectionOverlay.tsx +1 -1
- package/src/admin/editor/collection-previews.ts +91 -0
- package/src/admin/editor/editor-css.ts +5 -3
- package/src/admin/editor/feature-css.ts +105 -0
- package/src/admin/editor/host.ts +2 -0
- package/src/admin/editor/live-data.tsx +71 -0
- package/src/admin/editor/palette-items.ts +105 -0
- package/src/admin/editor/palette.ts +67 -0
- package/src/admin/editor/panels/AddPanel.tsx +47 -2
- package/src/admin/editor/panels/ClassesSection.tsx +5 -1
- package/src/admin/editor/panels/ElementPanel.tsx +34 -3
- package/src/admin/editor/panels/LayersPanel.tsx +38 -1
- package/src/admin/editor/panels/LeftPanel.tsx +4 -1
- package/src/admin/editor/panels/SettingsPanel.tsx +12 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +2 -0
- package/src/admin/editor/panels/TokenScales.tsx +168 -0
- package/src/admin/editor/panels/VariableSection.tsx +9 -4
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +215 -0
- package/src/admin/editor/panels/settings/BindingsEditor.tsx +221 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +1 -1
- package/src/admin/editor/panels/settings/EffectsControls.tsx +2 -2
- package/src/admin/editor/panels/settings/MotionControl.tsx +252 -0
- package/src/admin/editor/panels/settings/StateSwitcher.tsx +1 -1
- package/src/admin/editor/panels/settings/StyleRow.tsx +12 -0
- package/src/admin/editor/panels/settings/StyleSources.tsx +94 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +1 -1
- package/src/admin/editor/panels/settings/VariantEditor.tsx +83 -0
- package/src/admin/editor/panels/settings/length-units.ts +1 -1
- package/src/admin/editor/panels/settings/style-sections.ts +2 -1
- package/src/admin/editor/shortcuts.ts +2 -1
- package/src/admin/editor/useEditorShortcuts.ts +8 -0
- package/src/admin/editor/useNodeActions.ts +38 -15
- package/src/admin/ui.ts +4 -4
- package/src/astro/request-data.ts +97 -0
- package/src/astro/resolve-theme.ts +35 -5
- package/src/astro/resolve.ts +8 -2
- package/src/astro/theme-posts.ts +32 -0
- package/src/astro/visitor.ts +142 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +282 -0
- package/src/core/arrange.ts +91 -0
- package/src/core/audience/rules.ts +104 -0
- package/src/core/audience/variants.ts +51 -0
- package/src/core/css/generate.ts +20 -2
- package/src/core/css/trace.ts +169 -0
- package/src/core/data/bindings.ts +257 -0
- package/src/core/design/tokens.ts +127 -0
- package/src/core/elements/dynamic-elements.ts +87 -2
- package/src/core/elements/index.ts +2 -0
- package/src/core/history/diff.ts +152 -0
- package/src/core/index.ts +111 -0
- package/src/core/migrate/index.ts +4 -1
- package/src/core/nesting.ts +1 -0
- package/src/core/perf/meter.ts +160 -0
- package/src/core/perf/scripts.ts +58 -0
- package/src/core/recipes/index.ts +433 -0
- package/src/core/render/index.ts +51 -4
- package/src/core/render/loop.ts +54 -7
- package/src/core/render/pagination.ts +9 -1
- package/src/core/sanitize/css.ts +39 -3
- package/src/core/sanitize/motion.ts +129 -0
- package/src/core/schema/design.ts +25 -2
- package/src/core/schema/layout.ts +168 -18
- package/src/core/schema/motion.ts +14 -0
- package/src/core/schema/style.ts +48 -3
- package/src/core/share/link.ts +147 -0
- package/src/core/theme/dynamic.ts +49 -0
- package/src/core/theme/pagination.ts +56 -0
|
@@ -35,6 +35,7 @@ import { menu, menuItem } from "./menu.ts";
|
|
|
35
35
|
import { checkbox, form, radio, selectEl, submit, textInput, textareaEl } from "./form-elements.ts";
|
|
36
36
|
import {
|
|
37
37
|
loop,
|
|
38
|
+
loopEmpty,
|
|
38
39
|
pagination,
|
|
39
40
|
section,
|
|
40
41
|
postContent,
|
|
@@ -931,6 +932,7 @@ export const ELEMENTS = {
|
|
|
931
932
|
"post-date": postDate,
|
|
932
933
|
"post-author": postAuthor,
|
|
933
934
|
loop,
|
|
935
|
+
"loop-empty": loopEmpty,
|
|
934
936
|
pagination,
|
|
935
937
|
section,
|
|
936
938
|
"div-block": divBlock,
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { withFreshIds } from "../arrange.ts";
|
|
2
|
+
import { MAX_NODES } from "../limits.ts";
|
|
3
|
+
import type { Layout, LayoutNode } from "../schema/layout.ts";
|
|
4
|
+
import { insertNode, nodeChildren, updateNode } from "../tree-ops.ts";
|
|
5
|
+
|
|
6
|
+
export type SectionChange = {
|
|
7
|
+
id: string;
|
|
8
|
+
type: string;
|
|
9
|
+
label?: string;
|
|
10
|
+
kind: "added" | "removed" | "changed" | "moved" | "same";
|
|
11
|
+
/** Elements inside that differ (added, removed or edited), for changed sections. */
|
|
12
|
+
edits: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** A stable text of a node without its children, to tell edits from unchanged nodes. */
|
|
16
|
+
function own(node: LayoutNode): string {
|
|
17
|
+
const { children: _children, ...rest } = node as LayoutNode & { children?: unknown };
|
|
18
|
+
return JSON.stringify(rest);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function index(node: LayoutNode, into = new Map<string, LayoutNode>()): Map<string, LayoutNode> {
|
|
22
|
+
into.set(node.id, node);
|
|
23
|
+
for (const child of nodeChildren(node)) index(child, into);
|
|
24
|
+
return into;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** How many elements differ between two versions of a subtree. */
|
|
28
|
+
function editsBetween(before: LayoutNode, after: LayoutNode): number {
|
|
29
|
+
const a = index(before);
|
|
30
|
+
const b = index(after);
|
|
31
|
+
let edits = 0;
|
|
32
|
+
for (const [id, node] of a) {
|
|
33
|
+
const other = b.get(id);
|
|
34
|
+
if (!other) edits += 1;
|
|
35
|
+
else if (own(node) !== own(other)) edits += 1;
|
|
36
|
+
else if (
|
|
37
|
+
nodeChildren(node)
|
|
38
|
+
.map((c) => c.id)
|
|
39
|
+
.join() !==
|
|
40
|
+
nodeChildren(other)
|
|
41
|
+
.map((c) => c.id)
|
|
42
|
+
.join()
|
|
43
|
+
)
|
|
44
|
+
edits += 1;
|
|
45
|
+
}
|
|
46
|
+
for (const id of b.keys()) if (!a.has(id)) edits += 1;
|
|
47
|
+
return edits;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const describe = (node: LayoutNode) => ({
|
|
51
|
+
id: node.id,
|
|
52
|
+
type: node.type,
|
|
53
|
+
...(node.label ? { label: node.label } : {}),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* What changed between an older version and the current page, section by section (W-315). A
|
|
58
|
+
* section is a child of the page root. Sections only in the old version are "removed"; in
|
|
59
|
+
* page order, with removed ones at their old position.
|
|
60
|
+
*/
|
|
61
|
+
export function diffSections(older: Layout, current: Layout): SectionChange[] {
|
|
62
|
+
const before = older.root.children;
|
|
63
|
+
const after = current.root.children;
|
|
64
|
+
const beforeIds = new Map(before.map((node, i) => [node.id, i]));
|
|
65
|
+
const afterIds = new Set(after.map((node) => node.id));
|
|
66
|
+
const changes: SectionChange[] = [];
|
|
67
|
+
const removedAt = new Map<number, LayoutNode[]>();
|
|
68
|
+
for (const [i, node] of before.entries()) {
|
|
69
|
+
if (!afterIds.has(node.id)) removedAt.set(i, [...(removedAt.get(i) ?? []), node]);
|
|
70
|
+
}
|
|
71
|
+
const keptOrder = before.filter((n) => afterIds.has(n.id)).map((n) => n.id);
|
|
72
|
+
const nowOrder = after.filter((n) => beforeIds.has(n.id)).map((n) => n.id);
|
|
73
|
+
const flush = (upTo: number) => {
|
|
74
|
+
for (const [at, nodes] of [...removedAt].filter(([pos]) => pos <= upTo)) {
|
|
75
|
+
for (const node of nodes) changes.push({ ...describe(node), kind: "removed", edits: 0 });
|
|
76
|
+
removedAt.delete(at);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
for (const node of after) {
|
|
80
|
+
const old = beforeIds.get(node.id);
|
|
81
|
+
if (old === undefined) {
|
|
82
|
+
changes.push({ ...describe(node), kind: "added", edits: 0 });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
flush(old);
|
|
86
|
+
const oldNode = before[old] as LayoutNode;
|
|
87
|
+
const edits = editsBetween(oldNode, node);
|
|
88
|
+
const moved = keptOrder.indexOf(node.id) !== nowOrder.indexOf(node.id);
|
|
89
|
+
changes.push({
|
|
90
|
+
...describe(node),
|
|
91
|
+
kind: edits > 0 ? "changed" : moved ? "moved" : "same",
|
|
92
|
+
edits,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
flush(Number.POSITIVE_INFINITY);
|
|
96
|
+
return changes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const count = (node: LayoutNode): number =>
|
|
100
|
+
1 + nodeChildren(node).reduce((sum, child) => sum + count(child), 0);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Puts one section of an older version back on the current page (W-315): it replaces the
|
|
104
|
+
* current section with the same id, or, if it was removed, goes back near its old place. Ids
|
|
105
|
+
* already used elsewhere on the page are renewed, so the page stays valid.
|
|
106
|
+
*/
|
|
107
|
+
export function restoreSection(
|
|
108
|
+
current: Layout,
|
|
109
|
+
older: Layout,
|
|
110
|
+
sectionId: string,
|
|
111
|
+
): { ok: true; layout: Layout } | { ok: false; reason: string } {
|
|
112
|
+
const oldIndex = older.root.children.findIndex((n) => n.id === sectionId);
|
|
113
|
+
const section = older.root.children[oldIndex];
|
|
114
|
+
if (!section) return { ok: false, reason: "That section isn't in this version." };
|
|
115
|
+
const replacing = current.root.children.find((n) => n.id === sectionId);
|
|
116
|
+
const elsewhere = new Set(index(current.root).keys());
|
|
117
|
+
if (replacing) for (const id of index(replacing).keys()) elsewhere.delete(id);
|
|
118
|
+
const clashes = [...index(section).keys()].some((id) => elsewhere.has(id));
|
|
119
|
+
const incoming = clashes ? withFreshIds(current, section) : section;
|
|
120
|
+
const size = count(current.root) - (replacing ? count(replacing) : 0) + count(incoming);
|
|
121
|
+
if (size > MAX_NODES) return { ok: false, reason: "The page would have too many elements." };
|
|
122
|
+
if (replacing && !clashes) {
|
|
123
|
+
return { ok: true, layout: updateNode(current, sectionId, () => structuredClone(section)) };
|
|
124
|
+
}
|
|
125
|
+
// Removed (or renewed): after the nearest earlier section that is still on the page.
|
|
126
|
+
const before = new Set(older.root.children.slice(0, oldIndex).map((n) => n.id));
|
|
127
|
+
let at = 0;
|
|
128
|
+
for (const [i, node] of current.root.children.entries()) if (before.has(node.id)) at = i + 1;
|
|
129
|
+
if (replacing) {
|
|
130
|
+
at = current.root.children.indexOf(replacing);
|
|
131
|
+
const without = {
|
|
132
|
+
...current,
|
|
133
|
+
root: { ...current.root, children: current.root.children.filter((n) => n !== replacing) },
|
|
134
|
+
};
|
|
135
|
+
return {
|
|
136
|
+
ok: true,
|
|
137
|
+
layout: insertNode(without, current.root.id, at, structuredClone(incoming)),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return { ok: true, layout: insertNode(current, current.root.id, at, structuredClone(incoming)) };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The ids each kind of change covers, for outlining a diff on a rendered page (W-315). */
|
|
144
|
+
export function changedIds(
|
|
145
|
+
changes: SectionChange[],
|
|
146
|
+
): Record<"added" | "removed" | "changed", string[]> {
|
|
147
|
+
const out = { added: [] as string[], removed: [] as string[], changed: [] as string[] };
|
|
148
|
+
for (const c of changes) {
|
|
149
|
+
if (c.kind === "added" || c.kind === "removed" || c.kind === "changed") out[c.kind].push(c.id);
|
|
150
|
+
}
|
|
151
|
+
return out;
|
|
152
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -42,6 +42,13 @@ export {
|
|
|
42
42
|
isUnknownNode,
|
|
43
43
|
FormNode,
|
|
44
44
|
LoopNode,
|
|
45
|
+
LoopEmptyNode,
|
|
46
|
+
Variant,
|
|
47
|
+
AB_TEST_NAME,
|
|
48
|
+
Audience,
|
|
49
|
+
AUDIENCE_DEVICES,
|
|
50
|
+
LOOP_DISPLAYS,
|
|
51
|
+
LOOP_ORDERS,
|
|
45
52
|
SectionNode,
|
|
46
53
|
TextInputNode,
|
|
47
54
|
TextareaNode,
|
|
@@ -55,6 +62,10 @@ export {
|
|
|
55
62
|
PostImageNode,
|
|
56
63
|
PostLinkNode,
|
|
57
64
|
PaginationNode,
|
|
65
|
+
BIND_SOURCES,
|
|
66
|
+
Binding,
|
|
67
|
+
type BindSource,
|
|
68
|
+
type NodeExtras,
|
|
58
69
|
} from "./schema/layout.ts";
|
|
59
70
|
export {
|
|
60
71
|
type FieldKind,
|
|
@@ -210,6 +221,25 @@ export {
|
|
|
210
221
|
} from "./sanitize/svg-upload.ts";
|
|
211
222
|
export { resolveEmbedUrl, type EmbedTarget } from "./sanitize/embed-url.ts";
|
|
212
223
|
export { summarizeLayout, type LayoutSummary } from "./stats.ts";
|
|
224
|
+
export {
|
|
225
|
+
BINDABLE_FIELDS,
|
|
226
|
+
BIND_SOURCE_LABELS,
|
|
227
|
+
MAX_BOUND_LENGTH,
|
|
228
|
+
POST_BIND_KEYS,
|
|
229
|
+
SITE_BIND_KEYS,
|
|
230
|
+
applyBindings,
|
|
231
|
+
boundValue,
|
|
232
|
+
bindableFields,
|
|
233
|
+
hasBindings,
|
|
234
|
+
layoutUsesSource,
|
|
235
|
+
paramsFromSearch,
|
|
236
|
+
resolveBinding,
|
|
237
|
+
siteBindingValues,
|
|
238
|
+
withBinding,
|
|
239
|
+
type BindKind,
|
|
240
|
+
type BindableField,
|
|
241
|
+
type BindingData,
|
|
242
|
+
} from "./data/bindings.ts";
|
|
213
243
|
export {
|
|
214
244
|
findNode,
|
|
215
245
|
updateNode,
|
|
@@ -225,8 +255,12 @@ export {
|
|
|
225
255
|
REASONS,
|
|
226
256
|
addNode,
|
|
227
257
|
addNodeNear,
|
|
258
|
+
addSectionNear,
|
|
228
259
|
canDrop,
|
|
229
260
|
duplicateNode,
|
|
261
|
+
createVariantB,
|
|
262
|
+
endAbTest,
|
|
263
|
+
defaultTestName,
|
|
230
264
|
firstChild,
|
|
231
265
|
insertionPoint,
|
|
232
266
|
moveDown,
|
|
@@ -336,6 +370,8 @@ export {
|
|
|
336
370
|
portableTextToVNodes,
|
|
337
371
|
collectLoopItemPartIds,
|
|
338
372
|
collectSectionPartIds,
|
|
373
|
+
collectCollectionLoops,
|
|
374
|
+
type CollectionLoop,
|
|
339
375
|
resolvePostForRender,
|
|
340
376
|
resolvePostsForLoop,
|
|
341
377
|
type ThemePostFields,
|
|
@@ -378,3 +414,78 @@ export {
|
|
|
378
414
|
type PasteMode,
|
|
379
415
|
type ReadClip,
|
|
380
416
|
} from "./clipboard.ts";
|
|
417
|
+
export {
|
|
418
|
+
layoutScripts,
|
|
419
|
+
nodeScript,
|
|
420
|
+
SCRIPT_BYTES,
|
|
421
|
+
SCRIPT_LABELS,
|
|
422
|
+
typeMayAddScript,
|
|
423
|
+
type PageScript,
|
|
424
|
+
} from "./perf/scripts.ts";
|
|
425
|
+
export {
|
|
426
|
+
measurePage,
|
|
427
|
+
PERF_BUDGETS,
|
|
428
|
+
type PerfKind,
|
|
429
|
+
type PerfReport,
|
|
430
|
+
type SectionWeight,
|
|
431
|
+
} from "./perf/meter.ts";
|
|
432
|
+
export {
|
|
433
|
+
abCookieName,
|
|
434
|
+
collectAbTests,
|
|
435
|
+
pickArm,
|
|
436
|
+
variantShows,
|
|
437
|
+
type AbArm,
|
|
438
|
+
type AbTest,
|
|
439
|
+
} from "./audience/variants.ts";
|
|
440
|
+
export {
|
|
441
|
+
audienceShows,
|
|
442
|
+
audienceSummary,
|
|
443
|
+
clockIn,
|
|
444
|
+
deviceFromUserAgent,
|
|
445
|
+
layoutUsesAudience,
|
|
446
|
+
type VisitorInfo,
|
|
447
|
+
} from "./audience/rules.ts";
|
|
448
|
+
export { changedIds, diffSections, restoreSection, type SectionChange } from "./history/diff.ts";
|
|
449
|
+
export {
|
|
450
|
+
applyTokens,
|
|
451
|
+
DEFAULT_FLUID_RANGE,
|
|
452
|
+
fluidClamp,
|
|
453
|
+
SCALE_RATIOS,
|
|
454
|
+
spaceScale,
|
|
455
|
+
typeScale,
|
|
456
|
+
type ScaleInput,
|
|
457
|
+
} from "./design/tokens.ts";
|
|
458
|
+
export {
|
|
459
|
+
applyA11yFix,
|
|
460
|
+
applyAllA11yFixes,
|
|
461
|
+
auditPage,
|
|
462
|
+
contrastRatio,
|
|
463
|
+
type A11yIssue,
|
|
464
|
+
type A11yReport,
|
|
465
|
+
} from "./a11y/audit.ts";
|
|
466
|
+
export {
|
|
467
|
+
defaultTagFor,
|
|
468
|
+
inheritedStyle,
|
|
469
|
+
traceStyle,
|
|
470
|
+
type StyleSource,
|
|
471
|
+
type StyleTrace,
|
|
472
|
+
type TraceDevice,
|
|
473
|
+
} from "./css/trace.ts";
|
|
474
|
+
export { MOTION_EFFECTS, MOTION_LIMITS, type MotionEffect } from "./schema/motion.ts";
|
|
475
|
+
export {
|
|
476
|
+
recipeNode,
|
|
477
|
+
SECTION_RECIPES,
|
|
478
|
+
siteTokens,
|
|
479
|
+
type RecipeId,
|
|
480
|
+
type RecipeTokens,
|
|
481
|
+
} from "./recipes/index.ts";
|
|
482
|
+
export {
|
|
483
|
+
decodeSharedPage,
|
|
484
|
+
encodeSharedPage,
|
|
485
|
+
LONG_LINK_CHARS,
|
|
486
|
+
PLAYGROUND_URL,
|
|
487
|
+
sharedFromHash,
|
|
488
|
+
shareUrl,
|
|
489
|
+
type SharedPage,
|
|
490
|
+
type ShareRead,
|
|
491
|
+
} from "./share/link.ts";
|
|
@@ -11,7 +11,8 @@ export type Migration = (doc: Record<string, unknown>) => Record<string, unknown
|
|
|
11
11
|
* a link on a box, and tag defaults, and v9 → v10 (D-044, D-039) per-side border widths,
|
|
12
12
|
* per-corner radii and grid columns per device, and v10 → v11 (D-045) the layout Section and node
|
|
13
13
|
* labels, and v11 → v12 (W-160) rewrites a gradient `{ angle, from, to }` into
|
|
14
|
-
* `{ type: "linear", angle, stops }
|
|
14
|
+
* `{ type: "linear", angle, stops }`, and v12 → v13 (W-307 onward) adds optional fields only
|
|
15
|
+
* (bindings, and the rest of the W-307 feature batch). Every other value is unchanged.
|
|
15
16
|
*/
|
|
16
17
|
const unchanged: Migration = (doc) => doc;
|
|
17
18
|
|
|
@@ -69,6 +70,7 @@ export const LAYOUT_MIGRATIONS: Readonly<Record<number, Migration>> = {
|
|
|
69
70
|
9: unchanged,
|
|
70
71
|
10: unchanged,
|
|
71
72
|
11: gradients,
|
|
73
|
+
12: unchanged,
|
|
72
74
|
};
|
|
73
75
|
|
|
74
76
|
/** `DESIGN_MIGRATIONS[n]` upgrades a version-n design document to version n + 1. */
|
|
@@ -84,6 +86,7 @@ export const DESIGN_MIGRATIONS: Readonly<Record<number, Migration>> = {
|
|
|
84
86
|
9: unchanged,
|
|
85
87
|
10: unchanged,
|
|
86
88
|
11: gradients,
|
|
89
|
+
12: unchanged,
|
|
87
90
|
};
|
|
88
91
|
|
|
89
92
|
export type UpgradeResult =
|
package/src/core/nesting.ts
CHANGED
|
@@ -33,6 +33,7 @@ export function nestingIssues(layout: Layout, max = 5): LayoutIssue[] {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
function misplaced(parent: LayoutNode, child: LayoutNode, inForm: boolean): string | undefined {
|
|
36
|
+
if (child.type === "loop-empty" && parent.type !== "loop") return REASONS.emptyOutsideLoop;
|
|
36
37
|
if (child.type === "tab-panel" && parent.type !== "tabs") return REASONS.tabOutsideTabs;
|
|
37
38
|
if (parent.type === "tabs" && child.type !== "tab-panel") return REASONS.onlyTabPanels;
|
|
38
39
|
if (child.type === "accordion-item" && parent.type !== "accordion") {
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { DesignSystem } from "../schema/design.ts";
|
|
2
|
+
import { isParentNode, type Layout, type LayoutNode } from "../schema/layout.ts";
|
|
3
|
+
import { renderPage } from "../render/index.ts";
|
|
4
|
+
import type { ThemeDynamicData } from "../theme/dynamic.ts";
|
|
5
|
+
import { layoutScripts, SCRIPT_BYTES, type PageScript } from "./scripts.ts";
|
|
6
|
+
|
|
7
|
+
/** Budgets per page (W-310), in bytes over the wire. Generous for a marketing page. */
|
|
8
|
+
export const PERF_BUDGETS = {
|
|
9
|
+
html: 100_000,
|
|
10
|
+
css: 60_000,
|
|
11
|
+
js: 30_000,
|
|
12
|
+
images: 1_500_000,
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
export type PerfKind = keyof typeof PERF_BUDGETS;
|
|
16
|
+
|
|
17
|
+
export type SectionWeight = {
|
|
18
|
+
id: string;
|
|
19
|
+
type: string;
|
|
20
|
+
label?: string;
|
|
21
|
+
bytes: number;
|
|
22
|
+
images: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type PerfReport = {
|
|
26
|
+
bytes: Record<PerfKind, number>;
|
|
27
|
+
/** Which kinds go over their budget. */
|
|
28
|
+
over: PerfKind[];
|
|
29
|
+
scripts: { script: PageScript; bytes: number; nodeIds: string[] }[];
|
|
30
|
+
imageCount: number;
|
|
31
|
+
embeds: number;
|
|
32
|
+
/** Top-level sections, heaviest first. */
|
|
33
|
+
sections: SectionWeight[];
|
|
34
|
+
warnings: string[];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Bytes a string takes as UTF-8; HTML and CSS gzip to about a quarter, which we apply. */
|
|
38
|
+
const utf8 = (text: string) => new TextEncoder().encode(text).length;
|
|
39
|
+
const GZIP = 0.25;
|
|
40
|
+
/** An image with no size is guessed at a typical optimized photo (W-310). */
|
|
41
|
+
const IMAGE_GUESS = 120_000;
|
|
42
|
+
/** A YouTube iframe pulls in roughly this much before the visitor presses play. */
|
|
43
|
+
const EMBED_BYTES = 500_000;
|
|
44
|
+
|
|
45
|
+
function imageBytes(node: LayoutNode): number {
|
|
46
|
+
const props = node.props as { width?: unknown; height?: unknown };
|
|
47
|
+
const w = typeof props.width === "number" ? props.width : 0;
|
|
48
|
+
const h = typeof props.height === "number" ? props.height : 0;
|
|
49
|
+
// About 0.25 bytes per pixel for a WebP/AVIF photo at good quality.
|
|
50
|
+
return w > 0 && h > 0 ? Math.max(5_000, Math.round(w * h * 0.25)) : IMAGE_GUESS;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const hasBackgroundImage = (node: LayoutNode): boolean => {
|
|
54
|
+
const sheets = [node.style, ...Object.values(node.devices ?? {})];
|
|
55
|
+
return sheets.some((style) => {
|
|
56
|
+
const value = (style as { backgroundImage?: unknown } | undefined)?.backgroundImage;
|
|
57
|
+
return typeof value === "string" ? value.length > 0 : Boolean(value);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Images and embeds under a node, with their estimated weight. */
|
|
62
|
+
function media(node: LayoutNode): { images: number; bytes: number; embeds: number } {
|
|
63
|
+
let images = 0;
|
|
64
|
+
let bytes = 0;
|
|
65
|
+
let embeds = 0;
|
|
66
|
+
const walk = (n: LayoutNode): void => {
|
|
67
|
+
if (n.type === "image" || n.type === "post-image") {
|
|
68
|
+
const src = (n.props as { src?: unknown }).src;
|
|
69
|
+
if (n.type === "post-image" || (typeof src === "string" && src.trim())) {
|
|
70
|
+
images += 1;
|
|
71
|
+
bytes += imageBytes(n);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (hasBackgroundImage(n)) {
|
|
75
|
+
images += 1;
|
|
76
|
+
bytes += IMAGE_GUESS;
|
|
77
|
+
}
|
|
78
|
+
if (n.type === "video") {
|
|
79
|
+
embeds += 1;
|
|
80
|
+
bytes += EMBED_BYTES;
|
|
81
|
+
}
|
|
82
|
+
if (isParentNode(n)) for (const child of n.children) walk(child);
|
|
83
|
+
};
|
|
84
|
+
walk(node);
|
|
85
|
+
return { images, bytes, embeds };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const kb = (bytes: number) => `${Math.round(bytes / 1000)} KB`;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The page's weight against its budget (W-310): HTML and CSS as rendered (gzipped estimate),
|
|
92
|
+
* the scripts it loads, and its images, with the heaviest top-level section first.
|
|
93
|
+
*/
|
|
94
|
+
export function measurePage(
|
|
95
|
+
layout: Layout,
|
|
96
|
+
design: DesignSystem,
|
|
97
|
+
dynamic?: ThemeDynamicData,
|
|
98
|
+
): PerfReport {
|
|
99
|
+
const rendered = renderPage(layout, design, dynamic ? { dynamic } : {});
|
|
100
|
+
const scripts = [...layoutScripts(layout)].map(([script, nodeIds]) => ({
|
|
101
|
+
script,
|
|
102
|
+
bytes: SCRIPT_BYTES[script],
|
|
103
|
+
nodeIds,
|
|
104
|
+
}));
|
|
105
|
+
const all = media(layout.root);
|
|
106
|
+
const bytes: Record<PerfKind, number> = {
|
|
107
|
+
html: Math.round(utf8(rendered.html) * GZIP),
|
|
108
|
+
css: Math.round(utf8(rendered.css) * GZIP),
|
|
109
|
+
js: scripts.reduce((sum, s) => sum + s.bytes, 0),
|
|
110
|
+
images: all.bytes,
|
|
111
|
+
};
|
|
112
|
+
const over = (Object.keys(PERF_BUDGETS) as PerfKind[]).filter((k) => bytes[k] > PERF_BUDGETS[k]);
|
|
113
|
+
const opts = dynamic ? { dynamic } : {};
|
|
114
|
+
const sections: SectionWeight[] = [];
|
|
115
|
+
for (const child of layout.root.children.slice(0, 100)) {
|
|
116
|
+
const alone: Layout = {
|
|
117
|
+
schemaVersion: layout.schemaVersion,
|
|
118
|
+
root: Object.assign({}, layout.root, { children: [child] }),
|
|
119
|
+
};
|
|
120
|
+
const one = renderPage(alone, design, opts);
|
|
121
|
+
const m = media(child);
|
|
122
|
+
sections.push({
|
|
123
|
+
id: child.id,
|
|
124
|
+
type: child.type,
|
|
125
|
+
...(child.label ? { label: child.label } : {}),
|
|
126
|
+
bytes: Math.round(utf8(one.html) * GZIP) + m.bytes,
|
|
127
|
+
images: m.images,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
sections.sort((a, b) => b.bytes - a.bytes);
|
|
131
|
+
const warnings: string[] = [];
|
|
132
|
+
for (const kind of over) {
|
|
133
|
+
warnings.push(
|
|
134
|
+
`${kind.toUpperCase()} is ${kb(bytes[kind])}, over the ${kb(PERF_BUDGETS[kind])} budget.`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
if (all.embeds > 0) {
|
|
138
|
+
warnings.push(
|
|
139
|
+
`${all.embeds} video embed${all.embeds === 1 ? "" : "s"} load a third-party player (about ${kb(EMBED_BYTES)} each).`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
const first = layout.root.children[0];
|
|
143
|
+
const firstImage = first ? findFirstImage(first) : undefined;
|
|
144
|
+
if (firstImage && !(firstImage.props as { priority?: unknown }).priority) {
|
|
145
|
+
warnings.push(
|
|
146
|
+
"The first section's image isn't marked Priority, so it loads late; mark the hero image Priority.",
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
return { bytes, over, scripts, imageCount: all.images, embeds: all.embeds, sections, warnings };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function findFirstImage(node: LayoutNode): LayoutNode | undefined {
|
|
153
|
+
if (node.type === "image") return node;
|
|
154
|
+
if (!isParentNode(node)) return undefined;
|
|
155
|
+
for (const child of node.children) {
|
|
156
|
+
const found = findFirstImage(child);
|
|
157
|
+
if (found) return found;
|
|
158
|
+
}
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { isParentNode, type Layout, type LayoutNode } from "../schema/layout.ts";
|
|
2
|
+
|
|
3
|
+
/** The small scripts an EmVB page can load; everything else is plain HTML and CSS (W-311). */
|
|
4
|
+
export type PageScript = "forms" | "tabs" | "menu";
|
|
5
|
+
|
|
6
|
+
/** What each script is for, for badges and the performance meter (W-311). */
|
|
7
|
+
export const SCRIPT_LABELS: Record<PageScript, string> = {
|
|
8
|
+
forms: "Form submission script",
|
|
9
|
+
tabs: "Tabs keyboard script",
|
|
10
|
+
menu: "Dropdown menu script",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* About how many bytes (minified, gzipped) each script adds (W-310). Measured from the built
|
|
15
|
+
* runtimes; the forms client is the forms plugin's own script.
|
|
16
|
+
*/
|
|
17
|
+
export const SCRIPT_BYTES: Record<PageScript, number> = {
|
|
18
|
+
forms: 6_000,
|
|
19
|
+
tabs: 1_400,
|
|
20
|
+
menu: 600,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Elements that make the page load a script by being there (W-311). */
|
|
24
|
+
const SCRIPT_TYPES: Partial<Record<LayoutNode["type"], PageScript>> = {
|
|
25
|
+
form: "forms",
|
|
26
|
+
tabs: "tabs",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The script a node adds to the page, or undefined for the zero-JS default (W-311). A Menu item
|
|
31
|
+
* adds the menu script only when it has a dropdown; a Form only once it is connected to a form.
|
|
32
|
+
*/
|
|
33
|
+
export function nodeScript(node: LayoutNode): PageScript | undefined {
|
|
34
|
+
if (node.type === "menu-item") {
|
|
35
|
+
return isParentNode(node) && node.children.length > 0 ? "menu" : undefined;
|
|
36
|
+
}
|
|
37
|
+
if (node.type === "form") {
|
|
38
|
+
const formId = (node.props as { formId?: unknown }).formId;
|
|
39
|
+
return typeof formId === "string" && formId.trim() ? "forms" : undefined;
|
|
40
|
+
}
|
|
41
|
+
return SCRIPT_TYPES[node.type];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Does this element type ever add a script? For the Add panel's JS badge (W-311). */
|
|
45
|
+
export const typeMayAddScript = (type: string): boolean =>
|
|
46
|
+
type === "form" || type === "tabs" || type === "menu" || type === "menu-item";
|
|
47
|
+
|
|
48
|
+
/** Every script the layout loads, with the elements that cause it (W-311). */
|
|
49
|
+
export function layoutScripts(layout: Layout): Map<PageScript, string[]> {
|
|
50
|
+
const found = new Map<PageScript, string[]>();
|
|
51
|
+
const walk = (node: LayoutNode): void => {
|
|
52
|
+
const script = nodeScript(node);
|
|
53
|
+
if (script) found.set(script, [...(found.get(script) ?? []), node.id]);
|
|
54
|
+
if (isParentNode(node)) for (const child of node.children) walk(child);
|
|
55
|
+
};
|
|
56
|
+
walk(layout.root);
|
|
57
|
+
return found;
|
|
58
|
+
}
|