@perkyzz/emvb 0.3.0 → 0.5.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 (163) hide show
  1. package/README.md +22 -4
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +5 -2
  4. package/src/admin/access/role.ts +9 -3
  5. package/src/admin/agents-api.ts +39 -0
  6. package/src/admin/api.ts +17 -7
  7. package/src/admin/content-api.ts +7 -51
  8. package/src/admin/content-item.ts +22 -0
  9. package/src/admin/design-api.ts +30 -0
  10. package/src/admin/editor/A11yCopilot.tsx +1 -1
  11. package/src/admin/editor/CommandPalette.tsx +43 -2
  12. package/src/admin/editor/Editor.tsx +133 -332
  13. package/src/admin/editor/EditorOverlay.tsx +3 -1
  14. package/src/admin/editor/PerfMeter.tsx +1 -1
  15. package/src/admin/editor/ShortcutsDialog.tsx +4 -1
  16. package/src/admin/editor/Timeline.tsx +124 -9
  17. package/src/admin/editor/canvas/CanvasFrame.tsx +51 -368
  18. package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
  19. package/src/admin/editor/canvas/SelectionOverlay.tsx +86 -46
  20. package/src/admin/editor/canvas/canvas-scale.ts +20 -0
  21. package/src/admin/editor/canvas/drop-line.ts +24 -0
  22. package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
  23. package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
  24. package/src/admin/editor/collection-previews.ts +28 -5
  25. package/src/admin/editor/editor-chrome.tsx +346 -0
  26. package/src/admin/editor/editor-commands.ts +360 -0
  27. package/src/admin/editor/editor-css.ts +4 -0
  28. package/src/admin/editor/element-chrome.ts +89 -0
  29. package/src/admin/editor/feature-css.ts +37 -18
  30. package/src/admin/editor/host.ts +33 -0
  31. package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
  32. package/src/admin/editor/icons/library.ts +29 -19
  33. package/src/admin/editor/icons/use-icon-library.ts +315 -0
  34. package/src/admin/editor/palette-items.ts +36 -5
  35. package/src/admin/editor/palette.ts +9 -4
  36. package/src/admin/editor/panels/AddPanel.tsx +2 -67
  37. package/src/admin/editor/panels/ColorControl.tsx +21 -18
  38. package/src/admin/editor/panels/ElementPanel.tsx +40 -99
  39. package/src/admin/editor/panels/LayersPanel.tsx +34 -62
  40. package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
  41. package/src/admin/editor/panels/SiteStylesDrawer.tsx +16 -10
  42. package/src/admin/editor/panels/TokenScales.tsx +4 -2
  43. package/src/admin/editor/panels/VariableSection.tsx +1 -10
  44. package/src/admin/editor/panels/color-space.ts +12 -1
  45. package/src/admin/editor/panels/inline-error.tsx +11 -0
  46. package/src/admin/editor/panels/settings/AdvancedFields.tsx +143 -1
  47. package/src/admin/editor/panels/settings/AudienceEditor.tsx +115 -7
  48. package/src/admin/editor/panels/settings/DraftTextField.tsx +42 -9
  49. package/src/admin/editor/panels/settings/FieldControl.tsx +6 -0
  50. package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
  51. package/src/admin/editor/panels/settings/IconPicker.tsx +21 -1
  52. package/src/admin/editor/panels/settings/ImageSourcesField.tsx +111 -0
  53. package/src/admin/editor/panels/settings/ItemList.tsx +34 -1
  54. package/src/admin/editor/panels/settings/MediaPicker.tsx +2 -3
  55. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -0
  56. package/src/admin/editor/panels/settings/StyleRow.tsx +14 -2
  57. package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
  58. package/src/admin/editor/panels/settings/form-names.ts +7 -11
  59. package/src/admin/editor/panels/settings/length-units.ts +15 -4
  60. package/src/admin/editor/panels/settings/link-rules.ts +19 -0
  61. package/src/admin/editor/panels/settings/number-input.ts +11 -0
  62. package/src/admin/editor/panels/settings/query-style.ts +65 -0
  63. package/src/admin/editor/panels/settings/style-sections.ts +5 -1
  64. package/src/admin/editor/panels/style-target.ts +105 -0
  65. package/src/admin/editor/panels/variable-values.ts +4 -2
  66. package/src/admin/editor/remote-change.ts +67 -0
  67. package/src/admin/editor/section-templates.ts +43 -21
  68. package/src/admin/editor/shortcuts.ts +4 -0
  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 +23 -15
  74. package/src/admin/editor/useNodeActions.ts +6 -1
  75. package/src/admin/forms-api.ts +17 -6
  76. package/src/admin/icon-uploads-api.ts +18 -0
  77. package/src/admin/index.tsx +3 -1
  78. package/src/admin/media-api.ts +20 -0
  79. package/src/admin/pages/AgentsPage.tsx +274 -0
  80. package/src/admin/theme-api.ts +5 -18
  81. package/src/admin/ui.ts +28 -0
  82. package/src/astro/content-port.ts +24 -0
  83. package/src/astro/floats/runtime.ts +10 -0
  84. package/src/astro/forms/EmVBFormsClient.astro +11 -0
  85. package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
  86. package/src/astro/forms/client.ts +1 -0
  87. package/src/astro/forms-definitions.ts +24 -11
  88. package/src/astro/index.ts +1 -0
  89. package/src/astro/popups/runtime.ts +15 -0
  90. package/src/astro/render-session.ts +85 -0
  91. package/src/astro/request-data.ts +20 -8
  92. package/src/astro/resolve-theme.ts +166 -76
  93. package/src/astro/resolve.ts +57 -16
  94. package/src/astro/tabs/runtime.ts +60 -1
  95. package/src/astro/theme-posts.ts +16 -5
  96. package/src/astro/visitor.ts +67 -5
  97. package/src/constants.ts +1 -1
  98. package/src/core/a11y/audit.ts +202 -26
  99. package/src/core/arrange.ts +71 -38
  100. package/src/core/audience/rules.ts +22 -2
  101. package/src/core/canvas-styles.ts +24 -0
  102. package/src/core/clipboard.ts +11 -32
  103. package/src/core/css/generate.ts +18 -1
  104. package/src/core/css/print.ts +12 -0
  105. package/src/core/css/scope.ts +3 -3
  106. package/src/core/css-keywords.ts +83 -0
  107. package/src/core/data/bindings.ts +4 -6
  108. package/src/core/design/classes.ts +13 -15
  109. package/src/core/design/diff.ts +55 -0
  110. package/src/core/design/merge.ts +127 -0
  111. package/src/core/design/variables.ts +25 -21
  112. package/src/core/elements/dynamic-elements.ts +30 -0
  113. package/src/core/elements/form-elements.ts +20 -3
  114. package/src/core/elements/index.ts +211 -7
  115. package/src/core/forms/binding.ts +4 -13
  116. package/src/core/index.ts +10 -0
  117. package/src/core/local-style.ts +27 -0
  118. package/src/core/migrate/index.ts +9 -4
  119. package/src/core/nesting.ts +4 -23
  120. package/src/core/perf/scripts.ts +9 -6
  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 +22 -7
  124. package/src/core/render/form.ts +145 -15
  125. package/src/core/render/index.ts +195 -14
  126. package/src/core/render/pagination.ts +21 -2
  127. package/src/core/render/vnode.ts +12 -2
  128. package/src/core/sanitize/css.ts +90 -77
  129. package/src/core/sanitize/embed-url.ts +7 -1
  130. package/src/core/sanitize/href.ts +13 -1
  131. package/src/core/sanitize/svg.ts +8 -3
  132. package/src/core/sanitize/url.ts +10 -1
  133. package/src/core/schema/descriptors.ts +4 -1
  134. package/src/core/schema/design.ts +5 -7
  135. package/src/core/schema/layout.ts +160 -181
  136. package/src/core/schema/style.ts +53 -63
  137. package/src/core/share/link.ts +72 -29
  138. package/src/core/tabs/presence.ts +7 -17
  139. package/src/core/theme/dynamic.ts +205 -39
  140. package/src/core/theme/float.ts +1 -1
  141. package/src/core/tree-ops.ts +57 -0
  142. package/src/index.ts +14 -2
  143. package/src/mcp/errors.ts +44 -0
  144. package/src/mcp/index.ts +4 -0
  145. package/src/mcp/ports.ts +78 -0
  146. package/src/mcp/prompts.ts +76 -0
  147. package/src/mcp/publish.ts +60 -0
  148. package/src/mcp/queue.ts +19 -0
  149. package/src/mcp/rate-limit.ts +80 -0
  150. package/src/mcp/read.ts +419 -0
  151. package/src/mcp/refs.ts +84 -0
  152. package/src/mcp/settings.ts +18 -0
  153. package/src/mcp/tools.ts +265 -0
  154. package/src/mcp/write.ts +582 -0
  155. package/src/server/agent-settings.ts +134 -0
  156. package/src/server/design-routes.ts +75 -18
  157. package/src/server/icon-routes.ts +72 -16
  158. package/src/server/mcp-routes.ts +265 -0
  159. package/src/server/plugin.ts +40 -4
  160. package/src/admin/editor/icons/sets/fontawesome.json +0 -1
  161. package/src/admin/editor/icons/sets/lucide.json +0 -1
  162. package/src/admin/editor/icons/sets/remix.json +0 -1
  163. package/src/admin/editor/icons/sets/tabler.json +0 -1
@@ -0,0 +1,127 @@
1
+ import { emptyDesign, type DesignSystem } from "../schema/design.ts";
2
+
3
+ export type DesignConflict = { kind: "variable" | "class" | "setting"; name: string };
4
+
5
+ const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
6
+
7
+ type Named = { id: string; name: string };
8
+
9
+ /**
10
+ * Replays `ours` (what this tab tried to save) onto `theirs` (what the server has now),
11
+ * starting from `base` (what this tab last loaded). A list item this tab changed is kept
12
+ * when the server still has the base copy. When both sides changed the same item, the
13
+ * server copy stays and the name is reported. Nothing is overwritten silently.
14
+ */
15
+ export function mergeDesign(
16
+ base: DesignSystem | undefined,
17
+ ours: DesignSystem,
18
+ theirs: DesignSystem,
19
+ ): { design: DesignSystem; conflicts: DesignConflict[] } {
20
+ const from = base ?? emptyDesign();
21
+ const conflicts: DesignConflict[] = [];
22
+ const design: DesignSystem = {
23
+ ...theirs,
24
+ variables: {
25
+ colors: mergeList(
26
+ from.variables.colors,
27
+ ours.variables.colors,
28
+ theirs.variables.colors,
29
+ conflicts,
30
+ "variable",
31
+ ),
32
+ fonts: mergeList(
33
+ from.variables.fonts,
34
+ ours.variables.fonts,
35
+ theirs.variables.fonts,
36
+ conflicts,
37
+ "variable",
38
+ ),
39
+ fontSizes: mergeList(
40
+ from.variables.fontSizes,
41
+ ours.variables.fontSizes,
42
+ theirs.variables.fontSizes,
43
+ conflicts,
44
+ "variable",
45
+ ),
46
+ spacings: mergeList(
47
+ from.variables.spacings,
48
+ ours.variables.spacings,
49
+ theirs.variables.spacings,
50
+ conflicts,
51
+ "variable",
52
+ ),
53
+ },
54
+ classes: mergeList(from.classes, ours.classes, theirs.classes, conflicts, "class"),
55
+ fluidRange: pickSetting(
56
+ from.fluidRange,
57
+ ours.fluidRange,
58
+ theirs.fluidRange,
59
+ conflicts,
60
+ "Fluid range",
61
+ ),
62
+ direction: pickSetting(
63
+ from.direction,
64
+ ours.direction,
65
+ theirs.direction,
66
+ conflicts,
67
+ "Direction",
68
+ ),
69
+ defaults: pickSetting(from.defaults, ours.defaults, theirs.defaults, conflicts, "Tag defaults"),
70
+ };
71
+ return { design, conflicts };
72
+ }
73
+
74
+ function mergeList<T extends Named>(
75
+ base: T[] | undefined,
76
+ ours: T[] | undefined,
77
+ theirs: T[] | undefined,
78
+ conflicts: DesignConflict[],
79
+ kind: "variable" | "class",
80
+ ): T[] {
81
+ const baseItems = base ?? [];
82
+ const ourItems = ours ?? [];
83
+ const theirItems = theirs ?? [];
84
+ const by = (list: T[]) => new Map(list.map((item) => [item.id, item]));
85
+ const baseBy = by(baseItems);
86
+ const oursBy = by(ourItems);
87
+ const theirsBy = by(theirItems);
88
+ const result: T[] = [];
89
+ const seen = new Set<string>();
90
+ const take = (id: string) => {
91
+ if (seen.has(id)) return;
92
+ seen.add(id);
93
+ const baseItem = baseBy.get(id);
94
+ const ourItem = oursBy.get(id);
95
+ const theirItem = theirsBy.get(id);
96
+ const weChanged = !same(baseItem, ourItem);
97
+ const theyChanged = !same(baseItem, theirItem);
98
+ if (!weChanged) {
99
+ if (theirItem) result.push(theirItem);
100
+ return;
101
+ }
102
+ if (!theyChanged || same(ourItem, theirItem)) {
103
+ if (ourItem) result.push(ourItem);
104
+ return;
105
+ }
106
+ conflicts.push({ kind, name: ourItem?.name ?? theirItem?.name ?? id });
107
+ if (theirItem) result.push(theirItem);
108
+ };
109
+ for (const item of theirItems) take(item.id);
110
+ for (const item of ourItems) take(item.id);
111
+ return result;
112
+ }
113
+
114
+ function pickSetting<T>(
115
+ base: T | undefined,
116
+ ours: T | undefined,
117
+ theirs: T | undefined,
118
+ conflicts: DesignConflict[],
119
+ name: string,
120
+ ): T | undefined {
121
+ const weChanged = !same(base, ours);
122
+ const theyChanged = !same(base, theirs);
123
+ if (!weChanged) return theirs;
124
+ if (!theyChanged || same(ours, theirs)) return ours;
125
+ conflicts.push({ kind: "setting", name });
126
+ return theirs;
127
+ }
@@ -1,9 +1,9 @@
1
1
  import type { DesignSystem } from "../schema/design.ts";
2
- import type { Layout, LayoutNode } from "../schema/layout.ts";
2
+ import type { Layout } from "../schema/layout.ts";
3
3
  import { cleanClassName } from "./classes.ts";
4
4
  import { STYLE_STATES } from "../schema/state-names.ts";
5
5
  import type { DeviceStyles, StyleProps, StyleStates } from "../schema/style.ts";
6
- import { nodeChildren, slugify, updateNode } from "../tree-ops.ts";
6
+ import { forEachNode, slugify, updateNode } from "../tree-ops.ts";
7
7
 
8
8
  export type VariableKind = "color" | "font" | "fontSize" | "spacing";
9
9
 
@@ -32,26 +32,35 @@ export function refMatchesKind(ref: VariableRefLike, kind: VariableKind): boolea
32
32
  const matches = (value: unknown, id: string, kind: VariableKind | undefined) =>
33
33
  isVariableRef(value) && value.var === id && (kind === undefined || refMatchesKind(value, kind));
34
34
 
35
- /** Nested colour refs: the box shadow (W-088), and gradient stops and the overlay (W-094). */
36
- const shadowColorMatches = (style: StyleProps, id: string, kind: VariableKind | undefined) =>
37
- matches(style.boxShadow?.color, id, kind);
35
+ const NESTED_COLOR_KEYS = new Set(["boxShadow", "iconShadow", "gradient", "overlay"]);
38
36
 
39
- function collectStyleUsages(
37
+ /** Every value that can be a variable, including nested colours (W-356). */
38
+ export function forEachStyleRef(
40
39
  style: StyleProps | undefined,
41
- id: string,
42
- kind: VariableKind | undefined,
43
- hit: (prop: string) => void,
40
+ visit: (value: unknown, path: string) => void,
44
41
  ): void {
45
42
  if (!style) return;
46
43
  for (const [prop, value] of Object.entries(style)) {
47
- if (matches(value, id, kind)) hit(prop);
44
+ if (NESTED_COLOR_KEYS.has(prop)) continue;
45
+ visit(value, prop);
48
46
  }
49
- if (shadowColorMatches(style, id, kind)) hit("boxShadow.color");
50
- if (matches(style.iconShadow?.color, id, kind)) hit("iconShadow.color");
47
+ if (style.boxShadow) visit(style.boxShadow.color, "boxShadow.color");
48
+ if (style.iconShadow) visit(style.iconShadow.color, "iconShadow.color");
51
49
  style.gradient?.stops.forEach((stop, index) => {
52
- if (matches(stop.color, id, kind)) hit(`gradient.stops.${index}`);
50
+ visit(stop.color, `gradient.stops.${index}`);
51
+ });
52
+ if (style.overlay) visit(style.overlay.color, "overlay.color");
53
+ }
54
+
55
+ function collectStyleUsages(
56
+ style: StyleProps | undefined,
57
+ id: string,
58
+ kind: VariableKind | undefined,
59
+ hit: (prop: string) => void,
60
+ ): void {
61
+ forEachStyleRef(style, (value, path) => {
62
+ if (matches(value, id, kind)) hit(path);
53
63
  });
54
- if (matches(style.overlay?.color, id, kind)) hit("overlay.color");
55
64
  }
56
65
 
57
66
  const RESPONSIVE = ["tablet", "mobile"] as const;
@@ -72,11 +81,6 @@ function collectAllUsages(
72
81
  }
73
82
  }
74
83
 
75
- function walkNodes(node: LayoutNode, visit: (node: LayoutNode) => void): void {
76
- visit(node);
77
- for (const child of nodeChildren(node)) walkNodes(child, visit);
78
- }
79
-
80
84
  /** Find every style property on `layout` that references variable `id` (optionally of `kind`). */
81
85
  export function findVariableUsages(
82
86
  layout: Layout,
@@ -84,7 +88,7 @@ export function findVariableUsages(
84
88
  kind?: VariableKind,
85
89
  ): VariableUsage[] {
86
90
  const usages: VariableUsage[] = [];
87
- walkNodes(layout.root, (node) => {
91
+ forEachNode(layout.root, (node) => {
88
92
  collectAllUsages(node, id, kind, (prop) => {
89
93
  usages.push({ nodeId: node.id, prop });
90
94
  });
@@ -123,7 +127,7 @@ function stripRefsFromStyle(
123
127
  delete next[prop];
124
128
  changed = true;
125
129
  }
126
- if (style.boxShadow && shadowColorMatches(style, id, kind)) {
130
+ if (style.boxShadow && matches(style.boxShadow.color, id, kind)) {
127
131
  const { color: _drop, ...shadow } = style.boxShadow;
128
132
  next["boxShadow"] = shadow;
129
133
  changed = true;
@@ -216,6 +216,36 @@ export const loop: ElementDefinition<LoopNode> = {
216
216
  { value: "title", label: "By title" },
217
217
  ],
218
218
  },
219
+ {
220
+ key: "permalink",
221
+ kind: "text",
222
+ label: "Link pattern",
223
+ optional: true,
224
+ message:
225
+ "Where each entry links, e.g. /team/{slug}; {slug} and {id} are filled in. Leave it empty for /collection/{slug}.",
226
+ },
227
+ {
228
+ key: "imageField",
229
+ kind: "text",
230
+ label: "Image field",
231
+ optional: true,
232
+ message: "The field Post Image shows, e.g. photo. Leave it empty for the featured image.",
233
+ },
234
+ {
235
+ key: "excerptField",
236
+ kind: "text",
237
+ label: "Excerpt field",
238
+ optional: true,
239
+ message: "The field Post Excerpt shows, e.g. role. Leave it empty for the excerpt.",
240
+ },
241
+ {
242
+ key: "filter",
243
+ kind: "text",
244
+ label: "Only entries where",
245
+ optional: true,
246
+ message:
247
+ "field=value pairs separated by ;, e.g. category=news; team=design. A taxonomy works too. The same field twice matches either value.",
248
+ },
219
249
  {
220
250
  key: "itemPartId",
221
251
  kind: "text",
@@ -50,6 +50,7 @@ const FORM_FIELD_CSS =
50
50
  export const form: ElementDefinition<FormNode> = {
51
51
  baseCss:
52
52
  ".emvb-form{display:flex;flex-direction:column;gap:12px;min-width:0}.emvb-form-unbound{min-height:48px;padding:12px;color:var(--text-color-kumo-subtle,#666);background:var(--color-kumo-tint,#eee)}.ec-form-hp{position:absolute;left:-9999px}" +
53
+ ":where(.emvb-form-paused){padding:12px 16px;border:1px solid rgba(128,128,128,.5);border:1px solid color-mix(in srgb,currentColor 25%,transparent);border-radius:6px}:where(.emvb-form-paused p){margin:0}" +
53
54
  FORM_FIELD_CSS,
54
55
  defaults: () => ({ type: "form", props: { formId: "" }, children: [] }),
55
56
  descriptor: fieldDescriptor("form", "Form", [
@@ -183,9 +184,15 @@ export const radio: ElementDefinition<RadioNode> = {
183
184
  build: (node, attrs) => {
184
185
  const name = node.props.field;
185
186
  // Options filled from definition in W-035; placeholder single option for schema/render smoke.
187
+ const id = controlId(node.id);
188
+ // W-442: a fieldset can't be the target of <label for>, so the question named nothing and
189
+ // the group was read without it. The label names the group through aria-labelledby.
190
+ const labelled: Record<string, string> = node.props.label
191
+ ? { "aria-labelledby": `${id}-label` }
192
+ : {};
186
193
  const control: VNode = {
187
194
  tag: "fieldset",
188
- attrs: { class: "emvb-radio-group" },
195
+ attrs: { class: "emvb-radio-group", ...labelled },
189
196
  children: [
190
197
  {
191
198
  tag: "label",
@@ -197,7 +204,16 @@ export const radio: ElementDefinition<RadioNode> = {
197
204
  },
198
205
  ],
199
206
  };
200
- return fieldWrap(controlId(node.id), name, node.props.label, control, attrs);
207
+ const label: VNode[] = node.props.label
208
+ ? [
209
+ {
210
+ tag: "label",
211
+ attrs: { class: "emvb-form-label", id: `${id}-label` },
212
+ children: [node.props.label],
213
+ },
214
+ ]
215
+ : [];
216
+ return fieldBox(attrs, [...label, control, errorSlot(name)]);
201
217
  },
202
218
  };
203
219
 
@@ -214,6 +230,7 @@ export const submit: ElementDefinition<SubmitNode> = {
214
230
  type: "submit",
215
231
  class: `${attrs.class ?? ""} ec-form-submit`.trim(),
216
232
  },
217
- children: [node.props.label ?? "Submit"],
233
+ // W-447: a blank label left an empty button with no name; it falls back like an unset one.
234
+ children: [node.props.label?.trim() || "Submit"],
218
235
  }),
219
236
  };
@@ -12,6 +12,7 @@ import type {
12
12
  AccordionItemNode,
13
13
  DividerNode,
14
14
  HeadingNode,
15
+ TocNode,
15
16
  IconNode,
16
17
  ImageNode,
17
18
  LabelNode,
@@ -47,7 +48,7 @@ import {
47
48
  postAuthor,
48
49
  } from "./dynamic-elements.ts";
49
50
 
50
- const HEADING_TAGS = ["h1", "h2", "h3", "h4", "h5", "h6"] as const;
51
+ export const HEADING_TAGS = ["h1", "h2", "h3", "h4", "h5", "h6"] as const;
51
52
 
52
53
  const newTabAttrs = (newTab: boolean | undefined): Record<string, string> =>
53
54
  newTab ? { target: "_blank", rel: "noopener noreferrer" } : {};
@@ -257,7 +258,7 @@ const link: ElementDefinition<LinkNode> = {
257
258
  const button: ElementDefinition<ButtonNode> = {
258
259
  baseCss:
259
260
  // The look sits in :where() so site tag defaults, classes and local styles all win (W-148).
260
- ".emvb-button{display:inline-flex;align-items:center;justify-content:center;margin:0;font:inherit;text-decoration:none;cursor:pointer}:where(.emvb-button){color:inherit;background:transparent;border:1px solid currentColor;border-radius:6px;padding:0.625em 1.25em;line-height:1.2}",
261
+ ".emvb-button{display:inline-flex;align-items:center;justify-content:center;gap:0.5em;margin:0;font:inherit;text-decoration:none;cursor:pointer}:where(.emvb-button){color:inherit;background:transparent;border:1px solid currentColor;border-radius:6px;padding:0.625em 1.25em;line-height:1.2}.emvb-button-icon{display:inline-flex;flex:none}.emvb-button-icon svg{width:100%;height:100%;display:block}.emvb-button-icon--sm{width:1em;height:1em}.emvb-button-icon--md{width:1.25em;height:1.25em}.emvb-button-icon--lg{width:1.5em;height:1.5em}.emvb-figure{margin:0}.emvb-figcaption{margin-top:0.35em}.emvb-image-link{display:contents}",
261
262
  defaults: () => ({ type: "button", props: { text: "Button" } }),
262
263
  descriptor: {
263
264
  type: "button",
@@ -274,25 +275,86 @@ const button: ElementDefinition<ButtonNode> = {
274
275
  message: "Use a full URL such as https://example.com or a path such as /pricing.",
275
276
  },
276
277
  { key: "newTab", kind: "boolean", label: "Open in a new tab", optional: true },
278
+ { key: "iconId", kind: "icon", label: "Icon", optional: true },
279
+ {
280
+ key: "iconPosition",
281
+ kind: "select",
282
+ label: "Icon position",
283
+ optional: true,
284
+ options: [
285
+ { value: "before", label: "Before the label" },
286
+ { value: "after", label: "After the label" },
287
+ ],
288
+ },
289
+ {
290
+ key: "iconSize",
291
+ kind: "select",
292
+ label: "Icon size",
293
+ optional: true,
294
+ options: [
295
+ { value: "sm", label: "Small" },
296
+ { value: "md", label: "Medium" },
297
+ { value: "lg", label: "Large" },
298
+ ],
299
+ },
300
+ { key: "iconGap", kind: "int", label: "Gap (px)", optional: true, min: 0, max: 64 },
277
301
  ],
278
302
  },
279
303
  build: (node, attrs) => {
280
304
  const href = node.props.href !== undefined ? sanitizeHref(node.props.href) : undefined;
305
+ const children = buttonChildren(node);
281
306
  if (href) {
282
307
  return {
283
308
  tag: "a",
284
309
  attrs: { ...attrs, href, ...newTabAttrs(node.props.newTab) },
285
- children: [node.props.text],
310
+ children,
286
311
  };
287
312
  }
288
313
  return {
289
314
  tag: "button",
290
315
  attrs: { ...attrs, type: "button" },
291
- children: [node.props.text],
316
+ children,
292
317
  };
293
318
  },
294
319
  };
295
320
 
321
+ function buttonChildren(node: ButtonNode): (VNode | string)[] {
322
+ const buttonText = node.props.text;
323
+ const picked = node.props.iconSvg ? sanitizeSvgMarkup(node.props.iconSvg) : undefined;
324
+ const bundled = picked || !node.props.iconId ? undefined : getBundledIcon(node.props.iconId);
325
+ if (!picked && !bundled) return [buttonText];
326
+ const svg: VNode = {
327
+ tag: "svg",
328
+ attrs: picked
329
+ ? { ...picked.attrs, focusable: "false", "aria-hidden": "true" }
330
+ : {
331
+ xmlns: "http://www.w3.org/2000/svg",
332
+ viewBox: "0 0 24 24",
333
+ fill: "none",
334
+ stroke: "currentColor",
335
+ "stroke-width": "2",
336
+ "stroke-linecap": "round",
337
+ "stroke-linejoin": "round",
338
+ focusable: "false",
339
+ "aria-hidden": "true",
340
+ },
341
+ children: picked
342
+ ? picked.children
343
+ : (bundled?.children ?? []).map((child) => ({
344
+ tag: child.tag,
345
+ attrs: { ...child.attrs },
346
+ children: [],
347
+ })),
348
+ };
349
+ const size = node.props.iconSize ?? "md";
350
+ const icon: VNode = {
351
+ tag: "span",
352
+ attrs: { class: `emvb-button-icon emvb-button-icon--${size}`, "aria-hidden": "true" },
353
+ children: [svg],
354
+ };
355
+ return node.props.iconPosition === "after" ? [buttonText, icon] : [icon, buttonText];
356
+ }
357
+
296
358
  const list: ElementDefinition<ListNode> = {
297
359
  baseCss: ".emvb-list{margin:0;padding-inline-start:1.25em}.emvb-list li{margin:0}",
298
360
  defaults: () => ({ type: "list", props: { items: ["Item"] } }),
@@ -347,6 +409,17 @@ const image: ElementDefinition<ImageNode> = {
347
409
  },
348
410
  // The first big image people see (hero, LCP) shouldn't wait for lazy loading (W-226).
349
411
  { key: "priority", kind: "boolean", label: "Load right away", optional: true },
412
+ {
413
+ key: "href",
414
+ kind: "href",
415
+ label: "Link URL",
416
+ optional: true,
417
+ message: "Use a full URL or a path such as /pricing. Size styles stay on the image.",
418
+ },
419
+ { key: "newTab", kind: "boolean", label: "Open link in a new tab", optional: true },
420
+ { key: "caption", kind: "text", label: "Caption", optional: true },
421
+ { key: "focalX", kind: "int", label: "Focal point X (%)", optional: true, min: 0, max: 100 },
422
+ { key: "focalY", kind: "int", label: "Focal point Y (%)", optional: true, min: 0, max: 100 },
350
423
  {
351
424
  key: "width",
352
425
  kind: "int",
@@ -361,6 +434,7 @@ const image: ElementDefinition<ImageNode> = {
361
434
  optional: true,
362
435
  message: "Height must be a positive whole number.",
363
436
  },
437
+ { key: "sources", kind: "image-sources", label: "Extra sizes (srcset)", optional: true },
364
438
  ],
365
439
  },
366
440
  build: (node, attrs) => {
@@ -384,10 +458,74 @@ const image: ElementDefinition<ImageNode> = {
384
458
  if (decorative) imgAttrs.role = "presentation";
385
459
  if (node.props.width !== undefined) imgAttrs.width = String(node.props.width);
386
460
  if (node.props.height !== undefined) imgAttrs.height = String(node.props.height);
387
- return { tag: "img", attrs: imgAttrs, children: [] };
461
+ const sources = imageSources(src, node.props.sources, node.props.width);
462
+ if (sources) {
463
+ imgAttrs.srcset = sources;
464
+ imgAttrs.sizes = "100vw";
465
+ }
466
+ const img: VNode = { tag: "img", attrs: imgAttrs, children: [] };
467
+ const href = node.props.href ? sanitizeHref(node.props.href) : undefined;
468
+ const linked: VNode = href
469
+ ? {
470
+ tag: "a",
471
+ attrs: { href, class: "emvb-image-link", ...newTabAttrs(node.props.newTab) },
472
+ children: [img],
473
+ }
474
+ : img;
475
+ const caption = node.props.caption?.trim();
476
+ if (!caption) return linked;
477
+ return {
478
+ tag: "figure",
479
+ attrs: { class: "emvb-figure" },
480
+ children: [
481
+ linked,
482
+ { tag: "figcaption", attrs: { class: "emvb-figcaption" }, children: [caption] },
483
+ ],
484
+ };
388
485
  },
389
486
  };
390
487
 
488
+ const validSourceWidth = (width: unknown): width is number =>
489
+ Number.isInteger(width) && (width as number) >= 1 && (width as number) <= 8000;
490
+
491
+ /**
492
+ * W-428: a same-site path may hold a space ("/uploads/my photo.jpg"); in srcset that space ends
493
+ * the URL, so the size was dropped. It's encoded; full addresses can't hold spaces anyway.
494
+ */
495
+ const srcsetUrl = (url: string | undefined): string | undefined => url?.replaceAll(" ", "%20");
496
+
497
+ /**
498
+ * W-385: with `w` descriptors the browser picks only from srcset and ignores `src`, so the main
499
+ * image is listed too when its width is known (its own source entry, else the Width field).
500
+ */
501
+ function imageSources(
502
+ src: string,
503
+ sources: { src: string; width: number }[] | undefined,
504
+ width: number | undefined,
505
+ ): string | undefined {
506
+ if (!sources || sources.length === 0) return undefined;
507
+ const parts: string[] = [];
508
+ const seen = new Set<string>();
509
+ let srcWidth: number | undefined;
510
+ for (const source of sources) {
511
+ const url = srcsetUrl(sanitizeMediaUrl(source.src));
512
+ if (!validSourceWidth(source.width)) continue;
513
+ if (url === srcsetUrl(src)) {
514
+ srcWidth ??= source.width;
515
+ continue;
516
+ }
517
+ // A comma or space is another srcset candidate, so one field would request a second host.
518
+ if (!url || /[\s,]/.test(url) || seen.has(url)) continue;
519
+ seen.add(url);
520
+ parts.push(`${url} ${source.width}w`);
521
+ }
522
+ if (parts.length === 0) return undefined;
523
+ srcWidth ??= validSourceWidth(width) ? width : undefined;
524
+ const main = srcsetUrl(src);
525
+ if (srcWidth !== undefined && main && !/[\s,]/.test(main)) parts.push(`${main} ${srcWidth}w`);
526
+ return parts.join(", ");
527
+ }
528
+
391
529
  /** Root stroke widths the Stroke width control can override (W-237); Lucide and Tabler use 2. */
392
530
  const ICON_STROKE_WIDTHS = ["1", "1.5", "2", "2.5", "3"] as const;
393
531
 
@@ -626,6 +764,9 @@ const video: ElementDefinition<VideoNode> = {
626
764
  loading: "lazy",
627
765
  referrerpolicy: "strict-origin-when-cross-origin",
628
766
  allow: "encrypted-media; picture-in-picture; fullscreen",
767
+ // The player can run on its own origin. It cannot navigate this page.
768
+ sandbox:
769
+ "allow-scripts allow-same-origin allow-presentation allow-popups allow-popups-to-escape-sandbox",
629
770
  },
630
771
  children: [],
631
772
  };
@@ -644,6 +785,26 @@ const video: ElementDefinition<VideoNode> = {
644
785
  },
645
786
  };
646
787
 
788
+ const SVG_WSP = String.raw`[ \t\r\n]`;
789
+ const SVG_NUM = String.raw`[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?`;
790
+ const SVG_SEP = `(?:${SVG_WSP}*,${SVG_WSP}*|${SVG_WSP}+)`;
791
+ const VIEW_BOX = new RegExp(
792
+ `^${SVG_WSP}*${SVG_NUM}${SVG_SEP}${SVG_NUM}${SVG_SEP}(${SVG_NUM})${SVG_SEP}(${SVG_NUM})${SVG_WSP}*$`,
793
+ );
794
+
795
+ /** Longer side at `size`; the other side keeps the viewBox ratio. Square or unreadable stays square. */
796
+ function svgBox(size: number, viewBox: string | undefined): { width: string; height: string } {
797
+ const match = VIEW_BOX.exec(viewBox ?? "");
798
+ const vbWidth = Number(match?.[1]);
799
+ const vbHeight = Number(match?.[2]);
800
+ const across = vbWidth / vbHeight;
801
+ const down = vbHeight / vbWidth;
802
+ const valid = vbWidth > 0 && [across, down].every((ratio) => Number.isFinite(ratio) && ratio > 0);
803
+ if (!valid) return { width: String(size), height: String(size) };
804
+ const side = (ratio: number) => String(Math.max(1, Math.round(size * Math.min(1, ratio))));
805
+ return { width: side(across), height: side(down) };
806
+ }
807
+
647
808
  const DEFAULT_SVG =
648
809
  '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M8 12h8"/></svg>';
649
810
 
@@ -700,8 +861,7 @@ const svgEl: ElementDefinition<SvgNode> = {
700
861
  const svgAttrs: Record<string, string> = {
701
862
  ...tree.attrs,
702
863
  xmlns: tree.attrs.xmlns ?? "http://www.w3.org/2000/svg",
703
- width: String(size),
704
- height: String(size),
864
+ ...svgBox(size, tree.attrs.viewBox),
705
865
  focusable: "false",
706
866
  };
707
867
  if (!svgAttrs.fill && !svgAttrs.stroke) {
@@ -821,6 +981,10 @@ const layoutSection: ElementDefinition<LayoutSectionNode> = {
821
981
  },
822
982
  };
823
983
 
984
+ /** W-394: shown when a custom track line can't be saved. */
985
+ const TRACKS_HINT =
986
+ "Up to 12 tracks, one per line: 1fr, 2fr, 3fr, auto, min-content, max-content, or a length.";
987
+
824
988
  const grid: ElementDefinition<GridNode> = {
825
989
  baseCss: ".emvb-grid{display:grid;min-width:0}",
826
990
  defaults: () => ({ type: "grid", props: { columns: 3 }, children: [] }),
@@ -840,6 +1004,27 @@ const grid: ElementDefinition<GridNode> = {
840
1004
  label: String(n),
841
1005
  })),
842
1006
  },
1007
+ {
1008
+ key: "columnTracks",
1009
+ kind: "list-items",
1010
+ label: "Custom tracks",
1011
+ optional: true,
1012
+ message: TRACKS_HINT,
1013
+ },
1014
+ {
1015
+ key: "columnTracksTablet",
1016
+ kind: "list-items",
1017
+ label: "Custom tracks on tablet",
1018
+ optional: true,
1019
+ message: `${TRACKS_HINT} Leave empty to follow the desktop tracks.`,
1020
+ },
1021
+ {
1022
+ key: "columnTracksMobile",
1023
+ kind: "list-items",
1024
+ label: "Custom tracks on mobile",
1025
+ optional: true,
1026
+ message: `${TRACKS_HINT} Leave empty to follow the next wider device.`,
1027
+ },
843
1028
  ...BOX_LINK_FIELDS,
844
1029
  ],
845
1030
  },
@@ -903,8 +1088,27 @@ const accordionItem: ElementDefinition<AccordionItemNode> = {
903
1088
  },
904
1089
  };
905
1090
 
1091
+ const toc: ElementDefinition<TocNode> = {
1092
+ baseCss:
1093
+ ".emvb-toc{margin:0}.emvb-toc-title{margin:0 0 0.5em;font:inherit;font-weight:600}.emvb-toc ol{margin:0;padding-inline-start:1.25em}.emvb-toc a{color:inherit}",
1094
+ defaults: () => ({ type: "toc", props: {} }),
1095
+ descriptor: {
1096
+ type: "toc",
1097
+ name: "Table of contents",
1098
+ group: "content",
1099
+ defaultTab: "content",
1100
+ fields: [{ key: "title", kind: "text", label: "Title", optional: true }],
1101
+ },
1102
+ build: (node, attrs) => ({
1103
+ tag: "nav",
1104
+ attrs: { ...attrs, "aria-label": node.props.title?.trim() || "Table of contents" },
1105
+ children: [],
1106
+ }),
1107
+ };
1108
+
906
1109
  export const ELEMENTS = {
907
1110
  heading,
1111
+ toc,
908
1112
  container,
909
1113
  "layout-section": layoutSection,
910
1114
  spacer,
@@ -1,5 +1,5 @@
1
1
  import { isFormNode, type Layout, type LayoutNode } from "../schema/layout.ts";
2
- import { nodeChildren } from "../tree-ops.ts";
2
+ import { forEachNode, someNode } from "../tree-ops.ts";
3
3
  import { fieldsOf, type FormDefinitions } from "./definition.ts";
4
4
 
5
5
  export type MissingRequiredField = {
@@ -8,14 +8,9 @@ export type MissingRequiredField = {
8
8
  field: string;
9
9
  };
10
10
 
11
- function walk(node: LayoutNode, visit: (node: LayoutNode) => void): void {
12
- visit(node);
13
- for (const child of nodeChildren(node)) walk(child, visit);
14
- }
15
-
16
11
  function boundFields(form: LayoutNode): Set<string> {
17
12
  const names = new Set<string>();
18
- walk(form, (node) => {
13
+ forEachNode(form, (node) => {
19
14
  if (
20
15
  node.type === "text-input" ||
21
16
  node.type === "textarea" ||
@@ -38,7 +33,7 @@ export function findMissingRequiredFields(
38
33
  definitions: FormDefinitions,
39
34
  ): MissingRequiredField[] {
40
35
  const missing: MissingRequiredField[] = [];
41
- walk(layout.root, (node) => {
36
+ forEachNode(layout.root, (node) => {
42
37
  if (!isFormNode(node) || !node.props.formId) return;
43
38
  const definition = definitions.get(node.props.formId);
44
39
  if (!definition) return;
@@ -53,9 +48,5 @@ export function findMissingRequiredFields(
53
48
  }
54
49
 
55
50
  export function layoutHasForm(layout: Layout): boolean {
56
- let found = false;
57
- walk(layout.root, (node) => {
58
- if (isFormNode(node) && node.props.formId.trim()) found = true;
59
- });
60
- return found;
51
+ return someNode(layout.root, (node) => isFormNode(node) && node.props.formId.trim() !== "");
61
52
  }