@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
@@ -120,46 +120,37 @@ function underForm(layout: Layout, parentId: string): boolean {
120
120
 
121
121
  type Drop = { layout: Layout; node: LayoutNode; into: Located; isNew: boolean };
122
122
 
123
- /** Placement rules in the order their reasons take precedence (R-003, W-034, W-074). */
123
+ /**
124
+ * Parent/child rules shared by drag-and-drop and the save-time nesting check (W-354).
125
+ * `inForm` is true when `parent` is a form or sits inside one. Depth, count and tab caps stay
126
+ * in `canDrop`.
127
+ */
128
+ export function parentChildRefusal(
129
+ parent: LayoutNode,
130
+ child: LayoutNode,
131
+ inForm: boolean,
132
+ ): string | undefined {
133
+ if (child.type === "loop-empty" && parent.type !== "loop") return REASONS.emptyOutsideLoop;
134
+ if (child.type === "tab-panel" && parent.type !== "tabs") return REASONS.tabOutsideTabs;
135
+ if (parent.type === "tabs" && child.type !== "tab-panel") return REASONS.onlyTabPanels;
136
+ if (child.type === "accordion-item" && parent.type !== "accordion") {
137
+ return REASONS.itemOutsideAccordion;
138
+ }
139
+ if (parent.type === "accordion" && child.type !== "accordion-item") {
140
+ return REASONS.onlyAccordionItems;
141
+ }
142
+ if (child.type === "menu-item" && parent.type !== "menu" && parent.type !== "menu-item") {
143
+ return REASONS.itemOutsideMenu;
144
+ }
145
+ if (parent.type === "menu" && child.type !== "menu-item") return REASONS.onlyMenuItems;
146
+ if (isFormNode(child) && inForm) return REASONS.nestedForm;
147
+ if (isFormFieldType(child.type) && !inForm) return REASONS.fieldOutsideForm;
148
+ return undefined;
149
+ }
150
+
151
+ /** Caps and the form-into-a-leaf rule. Parent/child pairs are `parentChildRefusal`. */
124
152
  const DROP_RULES: [breaks: (drop: Drop) => boolean, reason: string][] = [
125
- [
126
- ({ node, into }) => node.type === "loop-empty" && into.node.type !== "loop",
127
- REASONS.emptyOutsideLoop,
128
- ],
129
- [
130
- ({ node, into }) => node.type === "tab-panel" && into.node.type !== "tabs",
131
- REASONS.tabOutsideTabs,
132
- ],
133
- [
134
- ({ node, into }) => into.node.type === "tabs" && node.type !== "tab-panel",
135
- REASONS.onlyTabPanels,
136
- ],
137
- [
138
- ({ node, into }) => node.type === "accordion-item" && into.node.type !== "accordion",
139
- REASONS.itemOutsideAccordion,
140
- ],
141
- [
142
- ({ node, into }) => into.node.type === "accordion" && node.type !== "accordion-item",
143
- REASONS.onlyAccordionItems,
144
- ],
145
- [
146
- ({ node, into }) =>
147
- node.type === "menu-item" && into.node.type !== "menu" && into.node.type !== "menu-item",
148
- REASONS.itemOutsideMenu,
149
- ],
150
- [
151
- ({ node, into }) => into.node.type === "menu" && node.type !== "menu-item",
152
- REASONS.onlyMenuItems,
153
- ],
154
- [
155
- ({ node, into, layout }) => isFormNode(node) && underForm(layout, into.node.id),
156
- REASONS.nestedForm,
157
- ],
158
153
  [({ node, into }) => isFormNode(node) && !isLayoutParentNode(into.node), REASONS.formIntoField],
159
- [
160
- ({ node, into, layout }) => isFormFieldType(node.type) && !underForm(layout, into.node.id),
161
- REASONS.fieldOutsideForm,
162
- ],
163
154
  // A 13th panel would be stored but never shown (W-188); moving within the same Tabs is fine.
164
155
  [
165
156
  ({ node, into }) =>
@@ -196,6 +187,8 @@ export function canDrop(layout: Layout, source: DragSource, parentId: string): A
196
187
  const moving = dragged(layout, source, parentId);
197
188
  if (!moving.ok) return moving;
198
189
  if (!isParentNode(into.node)) return refuse(REASONS.notContainer);
190
+ const placed = parentChildRefusal(into.node, moving.node, underForm(layout, into.node.id));
191
+ if (placed) return refuse(placed);
199
192
  const drop: Drop = { layout, node: moving.node, into, isNew: source.kind === "new" };
200
193
  const broken = DROP_RULES.find(([breaks]) => breaks(drop));
201
194
  return broken ? refuse(broken[1]) : { ok: true };
@@ -310,6 +303,46 @@ export function duplicateNode(
310
303
  };
311
304
  }
312
305
 
306
+ export const WRAP_TYPES = ["container", "div-block", "flexbox", "grid"] as const;
307
+ export type WrapType = (typeof WRAP_TYPES)[number];
308
+
309
+ /**
310
+ * Puts `id` inside a new box at the same place, in one step. The root cannot be wrapped.
311
+ * The new box is selected so it is obvious on the canvas.
312
+ */
313
+ export function wrapNode(
314
+ layout: Layout,
315
+ id: string,
316
+ type: WrapType,
317
+ random: () => number = Math.random,
318
+ ): Arranged {
319
+ const found = locate(layout, id);
320
+ if (!found) return refuse(REASONS.missing);
321
+ if (!found.parent) return refuse(REASONS.rootFixed);
322
+ if (size(layout.root) + 1 > MAX_NODES) return refuse(REASONS.tooMany);
323
+ const wrapper: LayoutNode = {
324
+ id: newNodeId(random),
325
+ type,
326
+ props: type === "grid" ? { columns: 2 } : {},
327
+ children: [found.node],
328
+ } as LayoutNode;
329
+ // W-389: the same parent/child rules as a drop, so an accordion item, tab panel or menu item
330
+ // isn't wrapped into a page that can't save, and the extra level stays within the depth cap.
331
+ const inForm = underForm(layout, found.parent.id);
332
+ const nesting =
333
+ parentChildRefusal(found.parent, wrapper, inForm) ??
334
+ parentChildRefusal(wrapper, found.node, inForm);
335
+ if (nesting) return refuse(nesting);
336
+ if (found.depth + height(found.node) > MAX_DEPTH) return refuse(REASONS.tooDeep);
337
+ const { layout: without, removed } = removeNode(layout, id);
338
+ if (!removed) return refuse(REASONS.missing);
339
+ return {
340
+ ok: true,
341
+ layout: insertNode(without, found.parent.id, found.index, wrapper),
342
+ selected: wrapper.id,
343
+ };
344
+ }
345
+
313
346
  /** Where click-to-add puts a new element: into a selected container, after a selected element, or at the end. */
314
347
  export function insertionPoint(layout: Layout, selectedId: string | null): Place {
315
348
  const found = selectedId ? locate(layout, selectedId) : undefined;
@@ -1,4 +1,5 @@
1
- import { isParentNode, type Audience, type Layout, type LayoutNode } from "../schema/layout.ts";
1
+ import { type Audience, type Layout, type LayoutNode } from "../schema/layout.ts";
2
+ import { knownChildren, someNode } from "../tree-ops.ts";
2
3
 
3
4
  /** What the host knows about this visitor (W-313). Anything unknown is left out. */
4
5
  export type VisitorInfo = {
@@ -8,6 +9,8 @@ export type VisitorInfo = {
8
9
  returning?: boolean;
9
10
  /** The request time, epoch ms. */
10
11
  now?: number;
12
+ /** Segments the host says this visitor is in (W-329), e.g. member, pro. */
13
+ segments?: readonly string[];
11
14
  };
12
15
 
13
16
  const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
@@ -49,6 +52,10 @@ function matches(audience: Audience, visitor: VisitorInfo): boolean {
49
52
  if (visitor.returning === undefined) return false;
50
53
  if ((audience.visitor === "returning") !== visitor.returning) return false;
51
54
  }
55
+ if (audience.segments) {
56
+ const has = new Set(visitor.segments ?? []);
57
+ if (!audience.segments.some((segment) => has.has(segment))) return false;
58
+ }
52
59
  if (audience.hours || audience.days) {
53
60
  if (visitor.now === undefined) return false;
54
61
  const { hour, day } = clockIn(visitor.now, audience.timeZone);
@@ -71,8 +78,19 @@ export function layoutUsesAudience(layout: Layout): boolean {
71
78
  return hasAudience(layout.root);
72
79
  }
73
80
 
81
+ /**
82
+ * A new/returning rule is the only reason to read or set the `emvb_seen` cookie.
83
+ * Country, device and segment rules do not.
84
+ */
85
+ export function layoutUsesVisitorRule(layout: Layout): boolean {
86
+ return hasVisitorRule(layout.root);
87
+ }
88
+
74
89
  const hasAudience = (node: LayoutNode): boolean =>
75
- Boolean(node.audience) || (isParentNode(node) && node.children.some(hasAudience));
90
+ someNode(node, (current) => Boolean(current.audience), knownChildren);
91
+
92
+ const hasVisitorRule = (node: LayoutNode): boolean =>
93
+ someNode(node, (current) => current.audience?.visitor !== undefined, knownChildren);
76
94
 
77
95
  /** A device from a User-Agent and the Sec-CH-UA-Mobile hint (W-313). */
78
96
  export function deviceFromUserAgent(
@@ -97,6 +115,8 @@ export function audienceSummary(audience: Audience): string {
97
115
  );
98
116
  if (audience.devices) parts.push(audience.devices.map((d) => DEVICE_LABELS[d]).join("/"));
99
117
  if (audience.visitor) parts.push(audience.visitor === "new" ? "new visitors" : "returning");
118
+ if (audience.segments)
119
+ parts.push(audience.segments.slice(0, 3).join("/") + (audience.segments.length > 3 ? "…" : ""));
100
120
  if (audience.hours) parts.push(`${audience.hours.from}:00–${audience.hours.to}:00`);
101
121
  if (audience.days) parts.push(audience.days.map((d) => WEEKDAYS[d]).join(" "));
102
122
  const who = parts.join(" · ") || "everyone";
@@ -0,0 +1,24 @@
1
+ /**
2
+ * W-327: stylesheets (and so fonts) a host site wants the editor canvas to load before EmVB's
3
+ * own CSS, so pages look in the editor the way the site's layout makes them look.
4
+ *
5
+ * Only `https:`/`http:` URLs and site paths (`/fonts.css`) are kept; anything else (`javascript:`,
6
+ * `data:`, `//host`, relative paths) is dropped. At most 10, each at most 2048 characters.
7
+ */
8
+ export const MAX_CANVAS_STYLES = 10;
9
+ const MAX_URL = 2048;
10
+
11
+ export function canvasStyleUrls(input: unknown): string[] {
12
+ if (!Array.isArray(input)) return [];
13
+ const out: string[] = [];
14
+ for (const item of input) {
15
+ if (typeof item !== "string") continue;
16
+ const url = item.trim();
17
+ if (!url || url.length > MAX_URL || /[\s"'<>\\]/.test(url)) continue;
18
+ const sitePath = url.startsWith("/") && !url.startsWith("//");
19
+ if (!sitePath && !/^https?:\/\/[^/]/i.test(url)) continue;
20
+ if (!out.includes(url)) out.push(url);
21
+ if (out.length === MAX_CANVAS_STYLES) break;
22
+ }
23
+ return out;
24
+ }
@@ -9,9 +9,10 @@ import {
9
9
  type Refusal,
10
10
  } from "./arrange.ts";
11
11
  import type { VariableKind } from "./design/variables.ts";
12
- import { clearVariableRefs } from "./design/variables.ts";
12
+ import { clearVariableRefs, forEachStyleRef } from "./design/variables.ts";
13
13
  import { ELEMENT_DESCRIPTORS, ELEMENTS, type ElementType } from "./elements/index.ts";
14
14
  import { MAX_LAYOUT_BYTES } from "./limits.ts";
15
+ import { isRecord } from "./record.ts";
15
16
  import { serialize } from "./render/vnode.ts";
16
17
  import { sanitizeHref } from "./sanitize/href.ts";
17
18
  import { sanitizeMediaUrl } from "./sanitize/media-url.ts";
@@ -26,6 +27,7 @@ import {
26
27
  } from "./schema/layout.ts";
27
28
  import { STYLE_STATES } from "./schema/state-names.ts";
28
29
  import type { DeviceStyles, HiddenOn, StyleProps, StyleStates } from "./schema/style.ts";
30
+ import { localStyleOf, withLocalStyle } from "./local-style.ts";
29
31
  import { findNode, nodeChildren } from "./tree-ops.ts";
30
32
  import { summarizeIssues, validateLayout } from "./validate.ts";
31
33
 
@@ -73,14 +75,7 @@ const header = () =>
73
75
  }) as const;
74
76
 
75
77
  /** The local style of a node; classes stay behind (D-033). */
76
- export function styleOf(node: LayoutNode): CopiedStyle {
77
- const copied: CopiedStyle = {};
78
- if (node.style) copied.style = structuredClone(node.style);
79
- if (node.states) copied.states = structuredClone(node.states);
80
- if (node.devices) copied.devices = structuredClone(node.devices);
81
- if (node.hiddenOn) copied.hiddenOn = structuredClone(node.hiddenOn);
82
- return copied;
83
- }
78
+ export const styleOf = localStyleOf;
84
79
 
85
80
  export const elementClip = (node: LayoutNode): ClipEnvelope => ({
86
81
  ...header(),
@@ -100,9 +95,6 @@ export type ReadClip = { ok: true; clip: Clip } | Refusal;
100
95
 
101
96
  const refuse = (reason: string): Refusal => ({ ok: false, reason });
102
97
 
103
- const isRecord = (value: unknown): value is Record<string, unknown> =>
104
- !!value && typeof value === "object" && !Array.isArray(value);
105
-
106
98
  const isCount = (value: unknown): value is number =>
107
99
  typeof value === "number" && Number.isInteger(value) && value >= 1;
108
100
 
@@ -200,15 +192,12 @@ const asRef = (value: unknown): Ref | null =>
200
192
 
201
193
  /** Every variable ref in a style: each value, plus nested colours (shadows, gradient, overlay). */
202
194
  function refsInStyle(style: StyleProps | undefined): Ref[] {
203
- if (!style) return [];
204
- const values: unknown[] = [
205
- ...Object.values(style),
206
- style.boxShadow?.color,
207
- style.iconShadow?.color,
208
- ...(style.gradient?.stops ?? []).map((stop) => stop.color),
209
- style.overlay?.color,
210
- ];
211
- return values.map(asRef).filter((ref): ref is Ref => ref !== null);
195
+ const refs: Ref[] = [];
196
+ forEachStyleRef(style, (value) => {
197
+ const ref = asRef(value);
198
+ if (ref) refs.push(ref);
199
+ });
200
+ return refs;
212
201
  }
213
202
 
214
203
  const refsOf = (owner: CopiedStyle): Ref[] => [
@@ -413,17 +402,7 @@ function styleFor(type: string, style: CopiedStyle): CopiedStyle {
413
402
  * Icon glyph keys are left out on anything but an Icon (W-292).
414
403
  */
415
404
  export function applyStyle<T extends LayoutNode>(node: T, copied: CopiedStyle): T {
416
- const style = styleFor(node.type, copied);
417
- const next = { ...node };
418
- delete next.style;
419
- delete next.states;
420
- delete next.devices;
421
- delete next.hiddenOn;
422
- if (style.style) next.style = structuredClone(style.style);
423
- if (style.states) next.states = structuredClone(style.states);
424
- if (style.devices) next.devices = structuredClone(style.devices);
425
- if (style.hiddenOn) next.hiddenOn = structuredClone(style.hiddenOn);
426
- return next;
405
+ return withLocalStyle(node, styleFor(node.type, copied));
427
406
  }
428
407
 
429
408
  export type PasteMode = "auto" | "inside";
@@ -32,6 +32,11 @@ export type CssInput = {
32
32
  tablet?: Declaration[];
33
33
  mobile?: Declaration[];
34
34
  hiddenOn?: readonly PopupDevice[];
35
+ /** W-443: an image with a caption, rendered inside a figure that hides with it. */
36
+ figure?: boolean;
37
+ /** Container-query overrides (W-346). */
38
+ query?: Declaration[];
39
+ queryMin?: number;
35
40
  }>;
36
41
  /** Editor canvas only: repeat each state rule for `[data-emvb-state="<state>"]` (W-089). */
37
42
  previewStates?: boolean;
@@ -193,6 +198,7 @@ export function generateCss({
193
198
  tablet: rule.tablet ?? [],
194
199
  mobile: rule.mobile ?? [],
195
200
  hiddenOn: rule.hiddenOn,
201
+ figure: rule.figure === true,
196
202
  })),
197
203
  ];
198
204
  /** A device block: in its media query, or, for a previewed device, on or off outright. */
@@ -208,9 +214,14 @@ export function generateCss({
208
214
  responsive.map((rule) => block(rule.selector, rule[which])).join(""),
209
215
  );
210
216
  const hide = (device: PopupDevice, query: string) => {
217
+ // W-443: a captioned image hides its figure too, or the caption stayed on its own.
211
218
  const selectors = responsive
212
219
  .filter((rule) => rule.hiddenOn?.includes(device))
213
- .map((rule) => rule.selector);
220
+ .flatMap((rule) =>
221
+ "figure" in rule && rule.figure
222
+ ? [rule.selector, `.emvb-figure:has(>${rule.selector},>a>${rule.selector})`]
223
+ : [rule.selector],
224
+ );
214
225
  return forDevices(
215
226
  [device],
216
227
  query,
@@ -239,6 +250,12 @@ export function generateCss({
239
250
  ...[...usedTypes].toSorted().map((type) => baseCss.get(type) ?? ""),
240
251
  ...classes,
241
252
  ...locals,
253
+ ...localRules
254
+ .filter((rule) => rule.query && rule.query.length > 0 && rule.queryMin)
255
+ .map(
256
+ (rule) =>
257
+ `@container (min-width:${rule.queryMin}px){${block(`.emvb-e-${rule.id}`, rule.query ?? [])}}`,
258
+ ),
242
259
  at(DEVICE_MEDIA.tablet, "tablet"),
243
260
  at(DEVICE_MEDIA.mobile, "mobile"),
244
261
  hide("desktop", DEVICE_MEDIA.hideDesktop),
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Print stylesheet (W-335). Chrome that only makes sense on screen is hidden.
3
+ * Appended after scoped page CSS so the selectors stay global: a header, float or popup
4
+ * is not inside the page's scope wrapper.
5
+ */
6
+ export function printCss(): string {
7
+ return (
8
+ "@media print{" +
9
+ ".emvb-theme-header,.emvb-theme-footer,.emvb-float,.emvb-popup{display:none!important}" +
10
+ "}"
11
+ );
12
+ }
@@ -22,9 +22,9 @@ export function scopeWrapperCss(): string {
22
22
  return `:where(.${WRAPPER_CLASS}){display:contents}`;
23
23
  }
24
24
 
25
- /** The wrapper's class attribute for a scope token. */
26
- export function scopeAttribute(token: string): string {
27
- return `class="${WRAPPER_CLASS} ${scopeClass(token)}"`;
25
+ /** Classes on the scope wrapper. The editor's vnode and the public HTML both use them. */
26
+ export function scopeClassName(token: string): string {
27
+ return `${WRAPPER_CLASS} ${scopeClass(token)}`;
28
28
  }
29
29
 
30
30
  function splitSelectors(prelude: string): string[] {
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Keyword allowlists the schema and the CSS sanitizer both read (W-357).
3
+ * Emission stays in the sanitizer.
4
+ */
5
+
6
+ export const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
7
+
8
+ export const LENGTH_UNITS = ["px", "rem", "em", "%", "vw", "vh", "ch"] as const;
9
+
10
+ export const FLEX_DIRECTIONS = ["row", "column", "row-reverse", "column-reverse"] as const;
11
+
12
+ export const FLEX_WRAPS = ["nowrap", "wrap", "wrap-reverse"] as const;
13
+
14
+ export const JUSTIFY_CONTENT = [
15
+ "flex-start",
16
+ "flex-end",
17
+ "center",
18
+ "space-between",
19
+ "space-around",
20
+ "space-evenly",
21
+ ] as const;
22
+
23
+ export const ALIGN_ITEMS = ["stretch", "flex-start", "flex-end", "center", "baseline"] as const;
24
+
25
+ export const TEXT_ALIGNS = ["left", "center", "right", "justify"] as const;
26
+
27
+ export const TEXT_TRANSFORMS = ["none", "uppercase", "lowercase", "capitalize"] as const;
28
+
29
+ export const TEXT_DECORATIONS = ["none", "underline", "overline", "line-through"] as const;
30
+
31
+ export const BORDER_STYLES = ["none", "solid", "dashed", "dotted"] as const;
32
+
33
+ export const OVERFLOWS = ["visible", "hidden", "clip", "scroll", "auto"] as const;
34
+
35
+ export const OBJECT_FITS = ["fill", "contain", "cover", "none", "scale-down"] as const;
36
+
37
+ export const POSITIONS = ["static", "relative", "absolute", "fixed", "sticky"] as const;
38
+
39
+ export const CURSORS = [
40
+ "default",
41
+ "pointer",
42
+ "text",
43
+ "move",
44
+ "grab",
45
+ "not-allowed",
46
+ "help",
47
+ "crosshair",
48
+ "zoom-in",
49
+ ] as const;
50
+
51
+ export const FONT_WEIGHTS = [
52
+ "normal",
53
+ "bold",
54
+ "100",
55
+ "200",
56
+ "300",
57
+ "400",
58
+ "500",
59
+ "600",
60
+ "700",
61
+ "800",
62
+ "900",
63
+ ] as const;
64
+
65
+ export const TRANSITION_EASINGS = ["ease", "ease-in", "ease-out", "ease-in-out", "linear"] as const;
66
+
67
+ export const TRANSITION_PROPERTIES = ["all", "colors", "opacity", "shadow", "filter"] as const;
68
+
69
+ export const BACKGROUND_SIZES = ["auto", "cover", "contain"] as const;
70
+
71
+ export const BACKGROUND_POSITIONS = [
72
+ "center",
73
+ "top",
74
+ "bottom",
75
+ "left",
76
+ "right",
77
+ "top left",
78
+ "top right",
79
+ "bottom left",
80
+ "bottom right",
81
+ ] as const;
82
+
83
+ export const BACKGROUND_REPEATS = ["no-repeat", "repeat", "repeat-x", "repeat-y"] as const;
@@ -1,4 +1,5 @@
1
1
  import type { Binding, BindSource, LayoutNode } from "../schema/layout.ts";
2
+ import { someNode } from "../tree-ops.ts";
2
3
  import { sanitizeHref } from "../sanitize/href.ts";
3
4
  import { sanitizeMediaUrl } from "../sanitize/media-url.ts";
4
5
  import type { ThemePostFields } from "../theme/dynamic.ts";
@@ -248,10 +249,7 @@ export function siteBindingValues(settings: unknown): Record<string, string> {
248
249
 
249
250
  /** Whether any node in the tree reads a source (hosts fetch site settings only when needed). */
250
251
  export function layoutUsesSource(root: LayoutNode, source: BindSource): boolean {
251
- const walk = (node: LayoutNode): boolean => {
252
- if (node.bind && Object.values(node.bind).some((b) => b.source === source)) return true;
253
- const children: unknown = (node as { children?: unknown }).children;
254
- return Array.isArray(children) && (children as LayoutNode[]).some(walk);
255
- };
256
- return walk(root);
252
+ return someNode(root, (node) =>
253
+ Boolean(node.bind && Object.values(node.bind).some((b) => b.source === source)),
254
+ );
257
255
  }
@@ -1,7 +1,8 @@
1
1
  import type { DesignSystem } from "../schema/design.ts";
2
2
  import type { Layout, LayoutNode } from "../schema/layout.ts";
3
3
  import type { StyleProps } from "../schema/style.ts";
4
- import { nodeChildren, slugify, updateNode } from "../tree-ops.ts";
4
+ import { localStyleOf, withLocalStyle } from "../local-style.ts";
5
+ import { forEachNode, slugify, updateNode } from "../tree-ops.ts";
5
6
 
6
7
  /** Reorder an applied class-id list. Out-of-range moves are no-ops. Pure (W-031). */
7
8
  export function moveClassId(ids: readonly string[], index: number, delta: -1 | 1): string[] {
@@ -27,15 +28,10 @@ export function removeClassId(ids: readonly string[], id: string): string[] {
27
28
  return ids.filter((entry) => entry !== id);
28
29
  }
29
30
 
30
- function walk(node: LayoutNode, visit: (node: LayoutNode) => void): void {
31
- visit(node);
32
- for (const child of nodeChildren(node)) walk(child, visit);
33
- }
34
-
35
31
  /** Nodes that list `classId` in `node.classes` (W-032 delete dialog). */
36
32
  export function findClassUsages(layout: Layout, classId: string): { nodeId: string }[] {
37
33
  const usages: { nodeId: string }[] = [];
38
- walk(layout.root, (node) => {
34
+ forEachNode(layout.root, (node) => {
39
35
  if (node.classes?.includes(classId)) usages.push({ nodeId: node.id });
40
36
  });
41
37
  return usages;
@@ -85,10 +81,11 @@ const filled = (style: object | undefined) =>
85
81
 
86
82
  /** Whether an element has local styles: base, hover/focus/active or tablet/mobile (W-134). */
87
83
  export function hasLocalStyles(node: LayoutNode): boolean {
84
+ const local = localStyleOf(node);
88
85
  return (
89
- filled(node.style) ||
90
- Object.values(node.states ?? {}).some(filled) ||
91
- Object.values(node.devices ?? {}).some(filled)
86
+ filled(local.style) ||
87
+ Object.values(local.states ?? {}).some(filled) ||
88
+ Object.values(local.devices ?? {}).some(filled)
92
89
  );
93
90
  }
94
91
 
@@ -130,17 +127,18 @@ export function localStylesToClass(
130
127
  const taken = new Set(classes.map((c) => c.id));
131
128
  let id = base;
132
129
  for (let n = 2; taken.has(id); n++) id = `${base}-${n}`;
130
+ const local = localStyleOf(node);
133
131
  const cls = {
134
132
  id,
135
133
  name: trimmed,
136
- style: structuredClone(node.style ?? {}) as StyleProps,
137
- ...(node.states ? { states: structuredClone(node.states) } : {}),
138
- ...(node.devices ? { devices: structuredClone(node.devices) } : {}),
134
+ style: (local.style ?? {}) as StyleProps,
135
+ ...(local.states ? { states: local.states } : {}),
136
+ ...(local.devices ? { devices: local.devices } : {}),
139
137
  };
140
- const { style: _style, states: _states, devices: _devices, ...rest } = node;
138
+ const cleared = withLocalStyle(node, local.hiddenOn ? { hiddenOn: local.hiddenOn } : {});
141
139
  return {
142
140
  design: { ...design, classes: [...classes, cls] },
143
- node: { ...rest, classes: addClassId(node.classes ?? [], id) } as LayoutNode,
141
+ node: { ...cleared, classes: addClassId(node.classes ?? [], id) },
144
142
  classId: id,
145
143
  };
146
144
  }
@@ -0,0 +1,55 @@
1
+ import type { DesignSystem } from "../schema/design.ts";
2
+
3
+ export type DesignChange = {
4
+ kind: "added" | "removed" | "changed";
5
+ what: "variable" | "class" | "setting";
6
+ name: string;
7
+ };
8
+
9
+ const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
10
+
11
+ /** What Publish styles will write: draft compared with the published design (W-340). */
12
+ export function designChanges(published: DesignSystem, draft: DesignSystem): DesignChange[] {
13
+ const changes: DesignChange[] = [];
14
+ listChanges(published.variables.colors, draft.variables.colors, "variable", changes);
15
+ listChanges(published.variables.fonts, draft.variables.fonts, "variable", changes);
16
+ listChanges(published.variables.fontSizes, draft.variables.fontSizes, "variable", changes);
17
+ listChanges(published.variables.spacings, draft.variables.spacings, "variable", changes);
18
+ listChanges(published.classes, draft.classes, "class", changes);
19
+ if (!same(published.direction, draft.direction)) {
20
+ changes.push({ kind: "changed", what: "setting", name: "Direction" });
21
+ }
22
+ if (!same(published.fluidRange, draft.fluidRange)) {
23
+ changes.push({ kind: "changed", what: "setting", name: "Fluid range" });
24
+ }
25
+ if (!same(published.defaults, draft.defaults)) {
26
+ changes.push({ kind: "changed", what: "setting", name: "Tag defaults" });
27
+ }
28
+ return changes;
29
+ }
30
+
31
+ export function designChangeLabel(change: DesignChange): string {
32
+ const noun =
33
+ change.what === "class" ? "Class" : change.what === "setting" ? "Setting" : "Variable";
34
+ if (change.kind === "added") return `Add ${noun.toLowerCase()} ${change.name}`;
35
+ if (change.kind === "removed") return `Remove ${noun.toLowerCase()} ${change.name}`;
36
+ return `Change ${noun.toLowerCase()} ${change.name}`;
37
+ }
38
+
39
+ function listChanges<T extends { id: string; name: string }>(
40
+ published: T[] | undefined,
41
+ draft: T[] | undefined,
42
+ what: "variable" | "class",
43
+ into: DesignChange[],
44
+ ) {
45
+ const from = new Map((published ?? []).map((item) => [item.id, item]));
46
+ const to = new Map((draft ?? []).map((item) => [item.id, item]));
47
+ for (const item of draft ?? []) {
48
+ const before = from.get(item.id);
49
+ if (!before) into.push({ kind: "added", what, name: item.name });
50
+ else if (!same(before, item)) into.push({ kind: "changed", what, name: item.name });
51
+ }
52
+ for (const item of published ?? []) {
53
+ if (!to.has(item.id)) into.push({ kind: "removed", what, name: item.name });
54
+ }
55
+ }