@perkyzz/emvb 0.2.1 → 0.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.
Files changed (150) hide show
  1. package/README.md +4 -2
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +3 -1
  4. package/src/admin/access/role.ts +9 -3
  5. package/src/admin/api.ts +17 -7
  6. package/src/admin/content-api.ts +7 -51
  7. package/src/admin/content-item.ts +22 -0
  8. package/src/admin/design-api.ts +30 -0
  9. package/src/admin/editor/A11yCopilot.tsx +126 -0
  10. package/src/admin/editor/CommandPalette.tsx +118 -0
  11. package/src/admin/editor/Editor.tsx +188 -152
  12. package/src/admin/editor/EditorOverlay.tsx +9 -1
  13. package/src/admin/editor/PerfMeter.tsx +121 -0
  14. package/src/admin/editor/ShareLink.tsx +164 -0
  15. package/src/admin/editor/ShortcutsDialog.tsx +4 -1
  16. package/src/admin/editor/Timeline.tsx +287 -0
  17. package/src/admin/editor/TopBar.tsx +4 -0
  18. package/src/admin/editor/canvas/CanvasFrame.tsx +62 -370
  19. package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
  20. package/src/admin/editor/canvas/SelectionOverlay.tsx +87 -47
  21. package/src/admin/editor/canvas/canvas-scale.ts +20 -0
  22. package/src/admin/editor/canvas/drop-line.ts +24 -0
  23. package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
  24. package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
  25. package/src/admin/editor/collection-previews.ts +114 -0
  26. package/src/admin/editor/editor-chrome.tsx +346 -0
  27. package/src/admin/editor/editor-commands.ts +360 -0
  28. package/src/admin/editor/editor-css.ts +5 -3
  29. package/src/admin/editor/element-chrome.ts +89 -0
  30. package/src/admin/editor/feature-css.ts +105 -0
  31. package/src/admin/editor/host.ts +35 -0
  32. package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
  33. package/src/admin/editor/icons/library.ts +29 -19
  34. package/src/admin/editor/icons/use-icon-library.ts +315 -0
  35. package/src/admin/editor/live-data.tsx +71 -0
  36. package/src/admin/editor/palette-items.ts +136 -0
  37. package/src/admin/editor/palette.ts +68 -0
  38. package/src/admin/editor/panels/AddPanel.tsx +48 -68
  39. package/src/admin/editor/panels/ClassesSection.tsx +5 -1
  40. package/src/admin/editor/panels/ColorControl.tsx +2 -10
  41. package/src/admin/editor/panels/ElementPanel.tsx +74 -102
  42. package/src/admin/editor/panels/LayersPanel.tsx +72 -63
  43. package/src/admin/editor/panels/LeftPanel.tsx +4 -1
  44. package/src/admin/editor/panels/SettingsPanel.tsx +13 -0
  45. package/src/admin/editor/panels/SiteStylesDrawer.tsx +18 -10
  46. package/src/admin/editor/panels/TokenScales.tsx +168 -0
  47. package/src/admin/editor/panels/VariableSection.tsx +10 -14
  48. package/src/admin/editor/panels/inline-error.tsx +11 -0
  49. package/src/admin/editor/panels/settings/AdvancedFields.tsx +87 -1
  50. package/src/admin/editor/panels/settings/AudienceEditor.tsx +285 -0
  51. package/src/admin/editor/panels/settings/BindingsEditor.tsx +221 -0
  52. package/src/admin/editor/panels/settings/ClassChipInput.tsx +1 -1
  53. package/src/admin/editor/panels/settings/DraftTextField.tsx +4 -0
  54. package/src/admin/editor/panels/settings/EffectsControls.tsx +2 -2
  55. package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
  56. package/src/admin/editor/panels/settings/ItemList.tsx +34 -1
  57. package/src/admin/editor/panels/settings/MotionControl.tsx +252 -0
  58. package/src/admin/editor/panels/settings/StateSwitcher.tsx +1 -1
  59. package/src/admin/editor/panels/settings/StyleRow.tsx +26 -2
  60. package/src/admin/editor/panels/settings/StyleSources.tsx +94 -0
  61. package/src/admin/editor/panels/settings/VariableBinding.tsx +1 -1
  62. package/src/admin/editor/panels/settings/VariantEditor.tsx +83 -0
  63. package/src/admin/editor/panels/settings/form-names.ts +7 -11
  64. package/src/admin/editor/panels/settings/length-units.ts +16 -5
  65. package/src/admin/editor/panels/settings/style-sections.ts +7 -2
  66. package/src/admin/editor/panels/style-target.ts +105 -0
  67. package/src/admin/editor/section-templates.ts +43 -21
  68. package/src/admin/editor/shortcuts.ts +6 -1
  69. package/src/admin/editor/store.ts +10 -1
  70. package/src/admin/editor/useClipboardActions.ts +1 -1
  71. package/src/admin/editor/useEditorCommands.ts +61 -13
  72. package/src/admin/editor/useEditorData.ts +7 -1
  73. package/src/admin/editor/useEditorShortcuts.ts +30 -14
  74. package/src/admin/editor/useNodeActions.ts +44 -16
  75. package/src/admin/forms-api.ts +17 -6
  76. package/src/admin/icon-uploads-api.ts +18 -0
  77. package/src/admin/theme-api.ts +5 -18
  78. package/src/admin/ui.ts +4 -4
  79. package/src/astro/content-port.ts +24 -0
  80. package/src/astro/forms/EmVBFormsClient.astro +11 -0
  81. package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
  82. package/src/astro/forms/client.ts +1 -0
  83. package/src/astro/forms-definitions.ts +24 -11
  84. package/src/astro/index.ts +1 -0
  85. package/src/astro/render-session.ts +85 -0
  86. package/src/astro/request-data.ts +109 -0
  87. package/src/astro/resolve-theme.ts +184 -64
  88. package/src/astro/resolve.ts +63 -16
  89. package/src/astro/theme-posts.ts +48 -5
  90. package/src/astro/visitor.ts +204 -0
  91. package/src/constants.ts +1 -1
  92. package/src/core/a11y/audit.ts +282 -0
  93. package/src/core/arrange.ts +150 -34
  94. package/src/core/audience/rules.ts +124 -0
  95. package/src/core/audience/variants.ts +51 -0
  96. package/src/core/canvas-styles.ts +24 -0
  97. package/src/core/clipboard.ts +11 -32
  98. package/src/core/css/generate.ts +29 -2
  99. package/src/core/css/print.ts +12 -0
  100. package/src/core/css/scope.ts +3 -3
  101. package/src/core/css/trace.ts +169 -0
  102. package/src/core/css-keywords.ts +83 -0
  103. package/src/core/data/bindings.ts +255 -0
  104. package/src/core/design/classes.ts +13 -15
  105. package/src/core/design/diff.ts +55 -0
  106. package/src/core/design/merge.ts +127 -0
  107. package/src/core/design/tokens.ts +127 -0
  108. package/src/core/design/variables.ts +25 -21
  109. package/src/core/elements/dynamic-elements.ts +117 -2
  110. package/src/core/elements/form-elements.ts +1 -0
  111. package/src/core/elements/index.ts +185 -7
  112. package/src/core/forms/binding.ts +4 -13
  113. package/src/core/history/diff.ts +152 -0
  114. package/src/core/index.ts +121 -0
  115. package/src/core/local-style.ts +27 -0
  116. package/src/core/migrate/index.ts +12 -4
  117. package/src/core/nesting.ts +4 -22
  118. package/src/core/perf/meter.ts +160 -0
  119. package/src/core/perf/scripts.ts +61 -0
  120. package/src/core/recipes/index.ts +433 -0
  121. package/src/core/record.ts +3 -0
  122. package/src/core/render/context.ts +2 -0
  123. package/src/core/render/dynamic-post.ts +1 -2
  124. package/src/core/render/form.ts +133 -10
  125. package/src/core/render/index.ts +206 -17
  126. package/src/core/render/loop.ts +54 -7
  127. package/src/core/render/pagination.ts +9 -1
  128. package/src/core/render/vnode.ts +12 -2
  129. package/src/core/sanitize/css.ts +129 -80
  130. package/src/core/sanitize/motion.ts +129 -0
  131. package/src/core/sanitize/svg.ts +8 -3
  132. package/src/core/sanitize/url.ts +10 -1
  133. package/src/core/schema/design.ts +29 -8
  134. package/src/core/schema/layout.ts +291 -169
  135. package/src/core/schema/motion.ts +14 -0
  136. package/src/core/schema/style.ts +101 -66
  137. package/src/core/share/link.ts +187 -0
  138. package/src/core/tabs/presence.ts +7 -17
  139. package/src/core/theme/dynamic.ts +242 -32
  140. package/src/core/theme/float.ts +1 -1
  141. package/src/core/theme/pagination.ts +56 -0
  142. package/src/core/tree-ops.ts +57 -0
  143. package/src/index.ts +14 -2
  144. package/src/server/design-routes.ts +4 -1
  145. package/src/server/icon-routes.ts +68 -16
  146. package/src/server/plugin.ts +8 -2
  147. package/src/admin/editor/icons/sets/fontawesome.json +0 -1
  148. package/src/admin/editor/icons/sets/lucide.json +0 -1
  149. package/src/admin/editor/icons/sets/remix.json +0 -1
  150. package/src/admin/editor/icons/sets/tabler.json +0 -1
@@ -1,11 +1,16 @@
1
+ import { variantShows } from "../audience/variants.ts";
2
+ import { audienceShows, audienceSummary } from "../audience/rules.ts";
1
3
  import { generateCss } from "../css/generate.ts";
2
- import { cssScopeToken, scopeAttribute, scopeCss, scopeWrapperCss } from "../css/scope.ts";
4
+ import { printCss } from "../css/print.ts";
5
+ import { cssScopeToken, scopeClassName, scopeCss, scopeWrapperCss } from "../css/scope.ts";
3
6
  import { ELEMENTS } from "../elements/index.ts";
4
7
  import { controlId } from "../elements/field-id.ts";
5
8
  import { layoutHasForm } from "../forms/binding.ts";
9
+ import { forEachNode, someNode } from "../tree-ops.ts";
6
10
  import { layoutHasMenuDropdown, layoutHasTabs } from "../tabs/presence.ts";
7
11
  import type { DesignSystem } from "../schema/design.ts";
8
12
  import {
13
+ FORM_FIELD_TYPES,
9
14
  isFormNode,
10
15
  isLoopNode,
11
16
  isParentNode,
@@ -20,6 +25,7 @@ import {
20
25
  stateDeclarations,
21
26
  styleClassName,
22
27
  styleDeclarations,
28
+ withInheritedAnimation,
23
29
  withInheritedLayers,
24
30
  type Declaration,
25
31
  type StateDeclarations,
@@ -37,7 +43,8 @@ import { renderPagination } from "./pagination.ts";
37
43
  import { renderSection } from "./section.ts";
38
44
  import { renderTabs } from "./tabs.ts";
39
45
  import { serialize, type VNode } from "./vnode.ts";
40
- import { classStylesInListOrder } from "../design/cascade.ts";
46
+ import { classStylesInListOrder, resolveCascade } from "../design/cascade.ts";
47
+ import { applyBindings, hasBindings, type BindingData } from "../data/bindings.ts";
41
48
  import {
42
49
  applyBackgroundVideo,
43
50
  applyBoxLink,
@@ -69,6 +76,32 @@ export type RenderResult = {
69
76
  const ID = /^[A-Za-z0-9_-]{4,24}$/;
70
77
  const HTML_ID = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
71
78
 
79
+ /** A focal-point percentage, or the fallback when that axis is unset. Anything else is dropped. */
80
+ function focalPercent(value: unknown, fallback: number): number | undefined {
81
+ if (value === undefined) return fallback;
82
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0 || value > 100) {
83
+ return undefined;
84
+ }
85
+ return value;
86
+ }
87
+
88
+ /** `object-position` only when both axes are whole percentages. A string here would break the sheet. */
89
+ function objectPosition(x: unknown, y: unknown): string | undefined {
90
+ if (x === undefined && y === undefined) return undefined;
91
+ const px = focalPercent(x, 50);
92
+ const py = focalPercent(y, 50);
93
+ if (px === undefined || py === undefined) return undefined;
94
+ return `${px}% ${py}%`;
95
+ }
96
+
97
+ /** `@container (min-width)` only for a whole pixel width in the schema's range. */
98
+ function containerQueryMin(value: unknown): number | undefined {
99
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 3840) {
100
+ return undefined;
101
+ }
102
+ return value;
103
+ }
104
+
72
105
  // Canvas iframe has no allow-scripts (D-011): live YouTube/Vimeo players fail there.
73
106
  function videoPreview(node: LayoutNode, attrs: Record<string, string>): VNode | undefined {
74
107
  const target = resolveEmbedUrl((node.props as { url: string }).url);
@@ -97,6 +130,23 @@ const gridTracks = (columns: unknown): Declaration[] =>
97
130
  ? [{ property: "grid-template-columns", value: `repeat(${columns}, minmax(0, 1fr))` }]
98
131
  : [];
99
132
 
133
+ const GRID_TRACK =
134
+ /^(?:1fr|2fr|3fr|auto|min-content|max-content|0|(?:\d+(?:\.\d+)?)(?:px|rem|em|%|vw|vh|ch))$/;
135
+
136
+ /** Custom tracks replace equal columns when every track is in the allowlist. */
137
+ const customTracks = (value: unknown): string | undefined => {
138
+ if (!Array.isArray(value) || value.length < 1 || value.length > 12) return undefined;
139
+ if (!value.every((track) => typeof track === "string" && GRID_TRACK.test(track)))
140
+ return undefined;
141
+ return value.join(" ");
142
+ };
143
+
144
+ const gridTemplate = (tracks: unknown, columns: unknown): Declaration[] => {
145
+ const custom = customTracks(tracks);
146
+ if (custom) return [{ property: "grid-template-columns", value: custom }];
147
+ return gridTracks(columns);
148
+ };
149
+
100
150
  /** A node with at least one child; tolerant of a malformed node without a children array. */
101
151
  const hasChildren = (node: LayoutNode): boolean => {
102
152
  const children: unknown = (node as { children?: unknown }).children;
@@ -114,7 +164,7 @@ function originHash(origin: string): string {
114
164
  }
115
165
 
116
166
  /** Form fields whose control carries an `emvb-field-<id>` HTML id (W-187). */
117
- const FIELD_TYPES = new Set(["text-input", "textarea", "select", "checkbox", "radio"]);
167
+ const FIELD_TYPES = new Set<string>(FORM_FIELD_TYPES.filter((type) => type !== "submit"));
118
168
 
119
169
  const ID_ATTRS = ["id", "for", "aria-controls", "aria-labelledby", "aria-describedby"] as const;
120
170
 
@@ -164,7 +214,10 @@ export function renderPage(
164
214
  tablet?: Declaration[];
165
215
  mobile?: Declaration[];
166
216
  hiddenOn?: LayoutNode["hiddenOn"];
217
+ query?: Declaration[];
218
+ queryMin?: number;
167
219
  }[] = [];
220
+ const layoutHasToc = treeHasType(layout.root, "toc");
168
221
  const knownVariables = new Set(design.variables.colors.map((c) => c.id));
169
222
  let backgroundVideo = false;
170
223
 
@@ -207,9 +260,18 @@ export function renderPage(
207
260
  const name = styleClassName(classId);
208
261
  if (name) classes.push(name);
209
262
  }
210
- const { declarations, rejected } = styleDeclarations(node.style);
263
+ // W-319: an element's own entrance or scroll motion keeps the other one its classes set.
264
+ const fromClasses = resolveCascade(classStylesInListOrder(design.classes, node.classes ?? []));
265
+ const classAnimation = {
266
+ ...(fromClasses.entrance ? { entrance: fromClasses.entrance } : {}),
267
+ ...(fromClasses.scrollMotion ? { scrollMotion: fromClasses.scrollMotion } : {}),
268
+ };
269
+ const own = node.style
270
+ ? (withInheritedAnimation(node.style, classAnimation) as typeof node.style)
271
+ : undefined;
272
+ const { declarations, rejected } = styleDeclarations(own);
211
273
  // W-210: a device that sets one background layer keeps the layers it inherits.
212
- const base = node.style ?? {};
274
+ const base = { ...classAnimation, ...own };
213
275
  const tabletStyle = node.devices?.tablet;
214
276
  const tablet = styleDeclarations(withInheritedLayers(tabletStyle, base));
215
277
  const mobile = styleDeclarations(
@@ -228,12 +290,49 @@ export function renderPage(
228
290
  const width = props.fullWidth === true ? "none" : cssLength(props.contentWidth);
229
291
  if (width) declarations.push({ property: "--emvb-content-width", value: width });
230
292
  }
293
+ if (node.type === "loop") {
294
+ // W-308: grid and cards columns, as a custom property the Loop's base CSS reads.
295
+ const columns = (node as { props: { columns?: unknown } }).props.columns;
296
+ if (
297
+ typeof columns === "number" &&
298
+ Number.isInteger(columns) &&
299
+ columns >= 1 &&
300
+ columns <= 6
301
+ ) {
302
+ declarations.push({ property: "--emvb-loop-columns", value: String(columns) });
303
+ }
304
+ }
305
+ if (node.container) declarations.push({ property: "container-type", value: "inline-size" });
306
+ if (node.type === "image") {
307
+ const focal = node.props as { focalX?: unknown; focalY?: unknown };
308
+ const position = objectPosition(focal.focalX, focal.focalY);
309
+ if (position) {
310
+ declarations.push({ property: "object-position", value: position });
311
+ declarations.push({ property: "object-fit", value: "cover" });
312
+ }
313
+ }
314
+ if (node.type === "button") {
315
+ const gap = (node.props as { iconGap?: unknown }).iconGap;
316
+ if (typeof gap === "number" && Number.isInteger(gap) && gap >= 0 && gap <= 64) {
317
+ declarations.push({ property: "gap", value: `${gap}px` });
318
+ }
319
+ }
320
+ const queryStyle = node.query ? styleDeclarations(node.query.style) : undefined;
321
+ for (const key of queryStyle?.rejected ?? []) {
322
+ warnings.push({ nodeId, code: "rejected-style", detail: key });
323
+ }
231
324
  if (node.type === "grid") {
232
325
  const props = (node as { props: Record<string, unknown> }).props;
233
- declarations.push(...gridTracks(props["columns"]));
234
- // Tablet and mobile counts go in their media queries (W-139).
235
- tablet.declarations.unshift(...gridTracks(props["columnsTablet"]));
236
- mobile.declarations.unshift(...gridTracks(props["columnsMobile"]));
326
+ declarations.push(...gridTemplate(props["columnTracks"], props["columns"]));
327
+ // Tablet and mobile counts go in their media queries (W-139). Custom tracks replace them.
328
+ const tabletColumns = gridTemplate(props["columnTracksTablet"], props["columnsTablet"]);
329
+ const mobileColumns = gridTemplate(props["columnTracksMobile"], props["columnsMobile"]);
330
+ if (props["columnTracksTablet"] !== undefined || props["columnsTablet"] !== undefined) {
331
+ tablet.declarations.unshift(...tabletColumns);
332
+ }
333
+ if (props["columnTracksMobile"] !== undefined || props["columnsMobile"] !== undefined) {
334
+ mobile.declarations.unshift(...mobileColumns);
335
+ }
237
336
  }
238
337
  const states = stateDeclarations(node.states);
239
338
  for (const key of states.rejected) {
@@ -249,9 +348,14 @@ export function renderPage(
249
348
  tablet.declarations.length > 0 ||
250
349
  mobile.declarations.length > 0 ||
251
350
  (hiddenOn?.length ?? 0) > 0;
351
+ const queryDeclarations = queryStyle?.declarations ?? [];
352
+ const queryMin = containerQueryMin(node.query?.min);
252
353
  if (
253
354
  ruleId &&
254
- (declarations.length > 0 || Object.keys(states.states).length > 0 || responsive)
355
+ (declarations.length > 0 ||
356
+ Object.keys(states.states).length > 0 ||
357
+ responsive ||
358
+ (queryDeclarations.length > 0 && queryMin !== undefined))
255
359
  ) {
256
360
  classes.push(`emvb-e-${ruleId}`);
257
361
  localRules.push({
@@ -261,21 +365,32 @@ export function renderPage(
261
365
  tablet: tablet.declarations,
262
366
  mobile: mobile.declarations,
263
367
  hiddenOn,
368
+ ...(queryDeclarations.length > 0 && queryMin !== undefined
369
+ ? { query: queryDeclarations, queryMin }
370
+ : {}),
264
371
  });
265
372
  }
266
373
  const attrs: Record<string, string> = { class: classes.join(" "), ...customAttributes(node) };
267
374
  if (mode === "editor" && id) attrs["data-emvb-id"] = id;
268
375
  if (node.htmlId && HTML_ID.test(node.htmlId)) attrs.id = node.htmlId;
376
+ else if (node.type === "heading" && id && layoutHasToc) attrs.id = `emvb-h-${id}`;
269
377
  // W-230: the site's text direction on the root, also a fallback when the host's <html> has none.
270
- if (isRoot && design.direction) attrs.dir = design.direction;
378
+ if (node.dir === "ltr" || node.dir === "rtl" || node.dir === "auto") attrs.dir = node.dir;
379
+ else if (isRoot && design.direction) attrs.dir = design.direction;
271
380
  return attrs;
272
381
  };
273
382
 
274
383
  function visit(
275
- node: LayoutNode,
384
+ stored: LayoutNode,
276
385
  isRoot: boolean,
277
386
  dynamic: ThemeDynamicData | undefined = baseDynamic,
278
387
  ): VNode | undefined {
388
+ // W-307: bound fields read their live value; the typed value is the fallback.
389
+ const node = stored.bind ? applyBindings(stored, bindingData(dynamic)) : stored;
390
+ // W-312: only the visitor's arm of an A/B test renders; the editor shows every arm.
391
+ if (mode === "public" && !variantShows(stored, dynamic?.abArms)) return undefined;
392
+ // W-313: visitor-aware elements render only for their audience; the editor shows them all.
393
+ if (mode === "public" && !audienceShows(stored, dynamic?.visitor)) return undefined;
279
394
  const id = ID.test(node.id) ? node.id : undefined;
280
395
  const nodeId = id ?? "(invalid id)";
281
396
  const type: string = node.type;
@@ -294,6 +409,13 @@ export function renderPage(
294
409
  if (type === "menu-item" && hasChildren(node)) rendered.menuDropdown = true;
295
410
  const ruleId = id === undefined ? undefined : ruleIdFor(id, dynamic?.idOrigin ?? "");
296
411
  const attrs = nodeAttrs(node, isRoot, id, nodeId, ruleId);
412
+ if (mode === "editor" && hasBindings(stored)) attrs["data-emvb-bound"] = "";
413
+ if (mode === "editor" && stored.audience) {
414
+ attrs["data-emvb-audience"] = audienceSummary(stored.audience);
415
+ }
416
+ if (mode === "editor" && stored.variant) {
417
+ attrs["data-emvb-variant"] = `${stored.variant.test} · ${stored.variant.arm.toUpperCase()}`;
418
+ }
297
419
  const def = ELEMENTS[type as keyof typeof ELEMENTS];
298
420
  if (isFormNode(node)) return finish(node, renderForm(node, attrs, ctx, dynamic));
299
421
  if (isLoopNode(node)) return finish(node, renderLoop(node, attrs, ctx, dynamic));
@@ -308,6 +430,10 @@ export function renderPage(
308
430
  );
309
431
  }
310
432
  if (isTabsNode(node)) return finish(node, renderTabs(node, id, attrs, ctx, dynamic));
433
+ if (node.type === "toc") {
434
+ const built = def.build(node as never, attrs, []);
435
+ return finish(node, { ...built, children: tocChildren(layout, node, mode) });
436
+ }
311
437
  if (isParentNode(node)) {
312
438
  return finish(node, def.build(node as never, attrs, ctx.children(node.children, dynamic)));
313
439
  }
@@ -315,7 +441,12 @@ export function renderPage(
315
441
  const preview = videoPreview(node, attrs);
316
442
  if (preview) return finish(node, preview);
317
443
  }
318
- const built = withFieldOptions(def.build(node as never, attrs, []), node, ctx.definitions);
444
+ const built = withFieldOptions(
445
+ def.build(node as never, attrs, []),
446
+ node,
447
+ ctx.definitions,
448
+ ctx.formId,
449
+ );
319
450
  return finish(node, FIELD_TYPES.has(type) && id ? uniqueField(built, controlId(id)) : built);
320
451
  }
321
452
 
@@ -342,13 +473,21 @@ export function renderPage(
342
473
  return applyBackgroundVideo(applyBoxLink(node, vnode), src);
343
474
  }
344
475
 
345
- const vnode = visit(layout.root, true) ?? {
476
+ function bindingData(dynamic: ThemeDynamicData | undefined): BindingData {
477
+ return {
478
+ post: resolvePostForRender(dynamic, mode),
479
+ ...(dynamic?.site ? { site: dynamic.site } : {}),
480
+ ...(dynamic?.params ? { params: dynamic.params } : {}),
481
+ ...(dynamic?.origin ? { origin: dynamic.origin } : {}),
482
+ };
483
+ }
484
+
485
+ const page = visit(layout.root, true) ?? {
346
486
  tag: "div",
347
487
  attrs: { class: "emvb-root" },
348
488
  children: [],
349
489
  };
350
490
  const baseCss = new Map(Object.entries(ELEMENTS).map(([type, def]) => [type, def.baseCss]));
351
- const html = serialize(vnode);
352
491
  const css = generateCss({
353
492
  design,
354
493
  usedTypes,
@@ -359,10 +498,14 @@ export function renderPage(
359
498
  ...(mode === "editor" && opts.previewDevice ? { previewDevice: opts.previewDevice } : {}),
360
499
  });
361
500
  const scope = opts.scope === undefined ? undefined : cssScopeToken(opts.scope);
501
+ const vnode = scope
502
+ ? { tag: "div", attrs: { class: scopeClassName(scope) }, children: [page] }
503
+ : page;
504
+ const html = serialize(vnode);
362
505
  return {
363
506
  vnode,
364
- html: scope ? `<div ${scopeAttribute(scope)}>${html}</div>` : html,
365
- css: scope ? scopeWrapperCss() + scopeCss(css, scope) : css,
507
+ html,
508
+ css: (scope ? scopeWrapperCss() + scopeCss(css, scope) : css) + printCss(),
366
509
  needsFormsRuntime: rendered.form || layoutHasForm(layout),
367
510
  needsTabsRuntime: rendered.tabs || layoutHasTabs(layout),
368
511
  needsMenuRuntime: rendered.menuDropdown || layoutHasMenuDropdown(layout),
@@ -370,3 +513,49 @@ export function renderPage(
370
513
  warnings,
371
514
  };
372
515
  }
516
+
517
+ function treeHasType(node: LayoutNode, type: string): boolean {
518
+ return someNode(node, (current) => current.type === type);
519
+ }
520
+
521
+ function headingAnchors(root: LayoutNode): { id: string; text: string }[] {
522
+ const out: { id: string; text: string }[] = [];
523
+ forEachNode(root, (node) => {
524
+ if (node.type !== "heading") return;
525
+ const text = String((node.props as { text?: unknown }).text ?? "").trim();
526
+ if (!text) return;
527
+ const id = node.htmlId && HTML_ID.test(node.htmlId) ? node.htmlId : `emvb-h-${node.id}`;
528
+ out.push({ id, text });
529
+ });
530
+ return out;
531
+ }
532
+
533
+ function tocChildren(layout: Layout, node: LayoutNode, mode: RenderMode): (VNode | string)[] {
534
+ const title =
535
+ node.type === "toc"
536
+ ? String((node.props as { title?: unknown }).title ?? "").trim() || undefined
537
+ : undefined;
538
+ const items = headingAnchors(layout.root);
539
+ const children: (VNode | string)[] = [];
540
+ if (title) children.push({ tag: "p", attrs: { class: "emvb-toc-title" }, children: [title] });
541
+ if (items.length === 0) {
542
+ if (mode === "editor") {
543
+ children.push({
544
+ tag: "p",
545
+ attrs: {},
546
+ children: ["Headings on this page appear here."],
547
+ });
548
+ }
549
+ return children;
550
+ }
551
+ children.push({
552
+ tag: "ol",
553
+ attrs: {},
554
+ children: items.map((item) => ({
555
+ tag: "li",
556
+ attrs: {},
557
+ children: [{ tag: "a", attrs: { href: `#${item.id}` }, children: [item.text] }],
558
+ })),
559
+ });
560
+ return children;
561
+ }
@@ -1,33 +1,79 @@
1
1
  import { ELEMENTS } from "../elements/index.ts";
2
2
  import type { LayoutNode, LoopNode } from "../schema/layout.ts";
3
- import { resolvePostsForLoop, type ThemeDynamicData } from "../theme/dynamic.ts";
3
+ import {
4
+ resolvePostsForLoop,
5
+ type ThemeDynamicData,
6
+ type ThemePostFields,
7
+ } from "../theme/dynamic.ts";
4
8
  import type { RenderContext } from "./context.ts";
5
9
  import type { VNode } from "./vnode.ts";
6
10
 
7
- /** One item per post, from the chosen loop-item part or the loop's own children. */
11
+ /** Entries a collection Loop shows when the Loop doesn't say (W-308). */
12
+ const DEFAULT_LOOP_LIMIT = 6;
13
+
14
+ /** Editor stand-ins for a collection Loop before its entries load (W-308). */
15
+ const sampleEntries = (collection: string, count: number): ThemePostFields[] =>
16
+ Array.from({ length: Math.min(count, 3) }, (_, i) => ({
17
+ id: `sample-${i + 1}`,
18
+ slug: `sample-${i + 1}`,
19
+ title: `${collection} entry ${i + 1}`,
20
+ excerpt: "Entry excerpt…",
21
+ content: "",
22
+ permalink: "#",
23
+ }));
24
+
25
+ /** The entries a Loop lists: a collection's (W-308) or the archive's posts. */
26
+ function entriesFor(
27
+ node: LoopNode,
28
+ dynamic: ThemeDynamicData | undefined,
29
+ mode: "public" | "editor",
30
+ ): ThemePostFields[] {
31
+ const collection = node.props.collection;
32
+ if (!collection) return resolvePostsForLoop(dynamic, mode);
33
+ const limit = node.props.limit ?? DEFAULT_LOOP_LIMIT;
34
+ const found = dynamic?.collections?.[node.id];
35
+ if (found) return found.slice(0, limit);
36
+ return mode === "editor" ? sampleEntries(collection, limit) : [];
37
+ }
38
+
39
+ /**
40
+ * One item per entry, from the chosen loop-item part or the loop's own children. A Loop's Empty
41
+ * state child (W-308) is left out of the item design and shows when there are no entries; the
42
+ * editor shows it under the items too, so it can be designed.
43
+ */
8
44
  export function renderLoop(
9
45
  node: LoopNode,
10
46
  attrs: Record<string, string>,
11
47
  ctx: RenderContext,
12
48
  dynamic: ThemeDynamicData | undefined,
13
49
  ): VNode | undefined {
14
- const posts = resolvePostsForLoop(dynamic, ctx.mode);
50
+ const posts = entriesFor(node, dynamic, ctx.mode);
15
51
  const itemPartId = node.props.itemPartId?.trim() ?? "";
16
52
  const templateLayout =
17
53
  itemPartId && dynamic?.loopTemplates ? dynamic.loopTemplates[itemPartId] : undefined;
18
- const templateNodes: LayoutNode[] = templateLayout ? templateLayout.root.children : node.children;
54
+ const own = node.children.filter((child) => child.type !== "loop-empty");
55
+ const emptyNodes = node.children.filter((child) => child.type === "loop-empty");
56
+ const templateNodes: LayoutNode[] = templateLayout ? templateLayout.root.children : own;
57
+ // Items don't get the archive's page data: a Pagination belongs after the Loop (W-228).
58
+ const { pagination: _pagination, ...rest } = dynamic ?? {};
59
+ const empty = () => ctx.children(emptyNodes, rest);
60
+ if (posts.length === 0) {
61
+ const designed = empty();
62
+ if (designed.length > 0) return ELEMENTS.loop.build(node as never, attrs, designed);
63
+ }
19
64
  // Editor mode always has a sample post, so an empty item template would paint a zero-height box.
20
65
  if (posts.length === 0 || (ctx.mode === "editor" && templateNodes.length === 0)) {
21
66
  if (ctx.mode !== "editor") return undefined;
22
67
  return {
23
68
  tag: "div",
24
69
  attrs: { ...attrs, "data-emvb-loop-empty": "" },
25
- children: ["Choose a Loop Item, or add elements here to design the item."],
70
+ children: [
71
+ ...ctx.children(emptyNodes, rest),
72
+ "Choose a Loop Item, or add elements here to design the item.",
73
+ ],
26
74
  };
27
75
  }
28
76
  const children: VNode[] = [];
29
- // Items don't get the archive's page data: a Pagination belongs after the Loop (W-228).
30
- const { pagination: _pagination, ...rest } = dynamic ?? {};
31
77
  // A loop item part is another layout, so its ids may match the page's (W-250).
32
78
  const itemDynamic = templateLayout
33
79
  ? { ...rest, idOrigin: `${rest.idOrigin ?? ""}/l:${itemPartId}` }
@@ -39,5 +85,6 @@ export function renderLoop(
39
85
  children: ctx.children(templateNodes, { ...itemDynamic, post, inLoopItem: true }),
40
86
  });
41
87
  }
88
+ if (ctx.mode === "editor") children.push(...empty());
42
89
  return ELEMENTS.loop.build(node as never, attrs, children);
43
90
  }
@@ -1,6 +1,6 @@
1
1
  import type { PaginationNode } from "../schema/layout.ts";
2
2
  import type { ThemeDynamicData } from "../theme/dynamic.ts";
3
- import { paginationItems, type ArchivePagination } from "../theme/pagination.ts";
3
+ import { pageCountText, paginationItems, type ArchivePagination } from "../theme/pagination.ts";
4
4
  import type { VNode } from "./vnode.ts";
5
5
 
6
6
  /** What the editor canvas shows: page 2 of several, links going nowhere (W-222). */
@@ -72,5 +72,13 @@ export function renderPagination(
72
72
  };
73
73
  }
74
74
  });
75
+ if (node.props.showCount) {
76
+ // W-309: "Page N of M", or "Page N" while the host has no total.
77
+ children.unshift({
78
+ tag: "span",
79
+ attrs: { class: "emvb-pagination-count" },
80
+ children: [pageCountText(data, node.props.countText)],
81
+ });
82
+ }
75
83
  return { tag: "nav", attrs: { ...attrs, "aria-label": "Pagination" }, children };
76
84
  }
@@ -26,6 +26,8 @@ const TAGS = new Set([
26
26
  "li",
27
27
  "hr",
28
28
  "img",
29
+ "figure",
30
+ "figcaption",
29
31
  "iframe",
30
32
  "video",
31
33
  "form",
@@ -39,6 +41,14 @@ const TAGS = new Set([
39
41
  "details",
40
42
  "summary",
41
43
  "time",
44
+ // Post Content formatting (W-328)
45
+ "strong",
46
+ "em",
47
+ "code",
48
+ "u",
49
+ "s",
50
+ "br",
51
+ "blockquote",
42
52
  // Lucide icon primitives (W-025 / A-04)
43
53
  "svg",
44
54
  "path",
@@ -65,10 +75,10 @@ const TAGS = new Set([
65
75
  "text",
66
76
  "tspan",
67
77
  ]);
68
- const VOID = new Set(["hr", "img", "input"]);
78
+ const VOID = new Set(["hr", "img", "input", "br"]);
69
79
  // iframe/video are not void — they need closing tags.
70
80
  const ATTR_NAME =
71
- /^(?:class|id|href|src|alt|width|height|loading|decoding|fetchpriority|dir|role|type|checked|target|rel|aria-[a-z][a-z0-9-]*|title|allow|referrerpolicy|preload|controls|allowfullscreen|xmlns|viewBox|fill|stroke|stroke-width|stroke-linecap|stroke-linejoin|stroke-dasharray|stroke-opacity|fill-opacity|opacity|transform|focusable|d|cx|cy|r|x|y|x1|y1|x2|y2|points|rx|ry|xlink:href|preserveAspectRatio|gradientUnits|gradientTransform|offset|stop-color|stop-opacity|clipPathUnits|maskUnits|maskContentUnits|patternUnits|patternContentUnits|patternTransform|markerUnits|markerWidth|markerHeight|refX|refY|orient|font-size|font-family|font-weight|text-anchor|dominant-baseline|dx|dy|clip-path|method|action|name|value|placeholder|for|tabindex|autocomplete|open|datetime|muted|loop|playsinline|autoplay|required|novalidate|minlength|maxlength|min|max|pattern|data-[a-z][a-z0-9-]*)$/;
81
+ /^(?:class|id|href|src|srcset|sizes|alt|width|height|loading|decoding|fetchpriority|dir|role|type|checked|target|rel|aria-[a-z][a-z0-9-]*|title|allow|referrerpolicy|preload|controls|allowfullscreen|sandbox|xmlns|viewBox|fill|stroke|stroke-width|stroke-linecap|stroke-linejoin|stroke-dasharray|stroke-opacity|fill-opacity|opacity|transform|focusable|d|cx|cy|r|x|y|x1|y1|x2|y2|points|rx|ry|xlink:href|preserveAspectRatio|gradientUnits|gradientTransform|offset|stop-color|stop-opacity|clipPathUnits|maskUnits|maskContentUnits|patternUnits|patternContentUnits|patternTransform|markerUnits|markerWidth|markerHeight|refX|refY|orient|font-size|font-family|font-weight|text-anchor|dominant-baseline|dx|dy|clip-path|method|action|name|value|placeholder|for|tabindex|autocomplete|open|datetime|muted|loop|playsinline|autoplay|required|novalidate|minlength|maxlength|min|max|pattern|data-[a-z][a-z0-9-]*)$/;
72
82
 
73
83
  export const isAllowedTag = (tag: string) => TAGS.has(tag);
74
84
  export const isAllowedAttr = (name: string) => ATTR_NAME.test(name);