@perkyzz/emvb 0.0.0-stage → 0.2.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 (217) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -2
  3. package/THIRD_PARTY_NOTICES.md +32 -0
  4. package/licenses/fontawesome-free.LICENSE +165 -0
  5. package/licenses/lucide-static.LICENSE +43 -0
  6. package/licenses/remixicon.LICENSE +201 -0
  7. package/licenses/tabler-icons.LICENSE +21 -0
  8. package/package.json +76 -4
  9. package/src/admin/access/RequireEditor.tsx +39 -0
  10. package/src/admin/access/role.ts +36 -0
  11. package/src/admin/access/sidebar-shim.ts +108 -0
  12. package/src/admin/api.ts +75 -0
  13. package/src/admin/content-api.ts +210 -0
  14. package/src/admin/editor/Editor.tsx +532 -0
  15. package/src/admin/editor/EditorOverlay.tsx +66 -0
  16. package/src/admin/editor/ShortcutsDialog.tsx +30 -0
  17. package/src/admin/editor/SmallScreenNotice.tsx +21 -0
  18. package/src/admin/editor/TopBar.tsx +243 -0
  19. package/src/admin/editor/canvas/CanvasFrame.tsx +536 -0
  20. package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
  21. package/src/admin/editor/canvas/DeviceBar.tsx +91 -0
  22. package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
  23. package/src/admin/editor/canvas/SelectionOverlay.tsx +239 -0
  24. package/src/admin/editor/canvas/canvas-events.ts +245 -0
  25. package/src/admin/editor/canvas/state-preview.ts +19 -0
  26. package/src/admin/editor/canvas/tab-reveal.ts +101 -0
  27. package/src/admin/editor/canvas/vnode-react.tsx +69 -0
  28. package/src/admin/editor/clipboard-store.ts +44 -0
  29. package/src/admin/editor/dialogs.tsx +164 -0
  30. package/src/admin/editor/dnd/drag-stash.ts +27 -0
  31. package/src/admin/editor/dnd/drop-target.ts +148 -0
  32. package/src/admin/editor/dnd/new-element.ts +107 -0
  33. package/src/admin/editor/editor-css.ts +625 -0
  34. package/src/admin/editor/exit.ts +27 -0
  35. package/src/admin/editor/host.ts +30 -0
  36. package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
  37. package/src/admin/editor/icons/library.ts +243 -0
  38. package/src/admin/editor/icons/set-format.ts +29 -0
  39. package/src/admin/editor/icons/sets/fontawesome.json +1 -0
  40. package/src/admin/editor/icons/sets/lucide.json +1 -0
  41. package/src/admin/editor/icons/sets/remix.json +1 -0
  42. package/src/admin/editor/icons/sets/tabler.json +1 -0
  43. package/src/admin/editor/panels/AddPanel.tsx +146 -0
  44. package/src/admin/editor/panels/ClassesSection.tsx +372 -0
  45. package/src/admin/editor/panels/ColorControl.tsx +477 -0
  46. package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
  47. package/src/admin/editor/panels/ElementPanel.tsx +773 -0
  48. package/src/admin/editor/panels/LayersPanel.tsx +573 -0
  49. package/src/admin/editor/panels/LeftPanel.tsx +81 -0
  50. package/src/admin/editor/panels/PageSettings.tsx +255 -0
  51. package/src/admin/editor/panels/SettingsPanel.tsx +113 -0
  52. package/src/admin/editor/panels/SiteStylesDrawer.tsx +548 -0
  53. package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
  54. package/src/admin/editor/panels/TriggersEditor.tsx +214 -0
  55. package/src/admin/editor/panels/VariableSection.tsx +530 -0
  56. package/src/admin/editor/panels/color-space.ts +195 -0
  57. package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
  58. package/src/admin/editor/panels/settings/BackgroundControls.tsx +418 -0
  59. package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
  60. package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
  61. package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
  62. package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
  63. package/src/admin/editor/panels/settings/ClassChipInput.tsx +451 -0
  64. package/src/admin/editor/panels/settings/DraftTextField.tsx +242 -0
  65. package/src/admin/editor/panels/settings/EffectsControls.tsx +354 -0
  66. package/src/admin/editor/panels/settings/FieldBindControl.tsx +119 -0
  67. package/src/admin/editor/panels/settings/FieldControl.tsx +270 -0
  68. package/src/admin/editor/panels/settings/FormBindControl.tsx +106 -0
  69. package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
  70. package/src/admin/editor/panels/settings/IconPicker.tsx +170 -0
  71. package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
  72. package/src/admin/editor/panels/settings/LengthRow.tsx +217 -0
  73. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +130 -0
  74. package/src/admin/editor/panels/settings/MediaPicker.tsx +220 -0
  75. package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
  76. package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
  77. package/src/admin/editor/panels/settings/StyleRow.tsx +563 -0
  78. package/src/admin/editor/panels/settings/VariableBinding.tsx +171 -0
  79. package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
  80. package/src/admin/editor/panels/settings/background-mode.ts +112 -0
  81. package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
  82. package/src/admin/editor/panels/settings/box-sides.ts +153 -0
  83. package/src/admin/editor/panels/settings/button-url.ts +27 -0
  84. package/src/admin/editor/panels/settings/class-suggest.ts +53 -0
  85. package/src/admin/editor/panels/settings/form-names.ts +38 -0
  86. package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
  87. package/src/admin/editor/panels/settings/image-alt.ts +25 -0
  88. package/src/admin/editor/panels/settings/length-units.ts +116 -0
  89. package/src/admin/editor/panels/settings/loop-context.ts +26 -0
  90. package/src/admin/editor/panels/settings/node-props.ts +11 -0
  91. package/src/admin/editor/panels/settings/style-sections.ts +394 -0
  92. package/src/admin/editor/panels/settings/svg-images.ts +14 -0
  93. package/src/admin/editor/panels/site-list.tsx +209 -0
  94. package/src/admin/editor/panels/variable-values.ts +73 -0
  95. package/src/admin/editor/restore-deleted.ts +80 -0
  96. package/src/admin/editor/section-templates.ts +50 -0
  97. package/src/admin/editor/shortcuts.ts +20 -0
  98. package/src/admin/editor/store.ts +240 -0
  99. package/src/admin/editor/useClipboardActions.ts +136 -0
  100. package/src/admin/editor/useEditorCommands.ts +203 -0
  101. package/src/admin/editor/useEditorData.ts +193 -0
  102. package/src/admin/editor/useEditorShortcuts.ts +180 -0
  103. package/src/admin/editor/useMediaQuery.ts +20 -0
  104. package/src/admin/editor/useNodeActions.ts +184 -0
  105. package/src/admin/editor/useSave.ts +242 -0
  106. package/src/admin/entry-status.ts +19 -0
  107. package/src/admin/forms-api.ts +73 -0
  108. package/src/admin/icon-uploads-api.ts +44 -0
  109. package/src/admin/index.tsx +20 -0
  110. package/src/admin/media-api.ts +68 -0
  111. package/src/admin/pages/CreateDialog.tsx +150 -0
  112. package/src/admin/pages/EditorPage.tsx +57 -0
  113. package/src/admin/pages/NewPageDialog.tsx +83 -0
  114. package/src/admin/pages/NewThemePartDialog.tsx +61 -0
  115. package/src/admin/pages/PageList.tsx +91 -0
  116. package/src/admin/pages/PagesPage.tsx +25 -0
  117. package/src/admin/pages/SetupShell.tsx +90 -0
  118. package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
  119. package/src/admin/pages/ThemePartList.tsx +221 -0
  120. package/src/admin/pages/list-kit.tsx +125 -0
  121. package/src/admin/setup/plan.ts +150 -0
  122. package/src/admin/setup/run.ts +61 -0
  123. package/src/admin/setup/spec.ts +107 -0
  124. package/src/admin/setup/useSetupStatus.ts +62 -0
  125. package/src/admin/theme-api.ts +233 -0
  126. package/src/admin/ui.ts +244 -0
  127. package/src/admin/widgets/LayoutField.tsx +67 -0
  128. package/src/astro/EmVBPage.astro +63 -0
  129. package/src/astro/env.d.ts +5 -0
  130. package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
  131. package/src/astro/floats/index.ts +1 -0
  132. package/src/astro/floats/runtime.ts +78 -0
  133. package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
  134. package/src/astro/forms/index.ts +1 -0
  135. package/src/astro/forms-definitions.ts +126 -0
  136. package/src/astro/index.ts +16 -0
  137. package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
  138. package/src/astro/menu/index.ts +1 -0
  139. package/src/astro/menu/runtime.ts +37 -0
  140. package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
  141. package/src/astro/popups/index.ts +1 -0
  142. package/src/astro/popups/runtime.ts +296 -0
  143. package/src/astro/render.ts +108 -0
  144. package/src/astro/resolve-theme.ts +515 -0
  145. package/src/astro/resolve.ts +65 -0
  146. package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
  147. package/src/astro/tabs/index.ts +1 -0
  148. package/src/astro/tabs/runtime.ts +116 -0
  149. package/src/astro/theme-context.ts +89 -0
  150. package/src/astro/theme-posts.ts +84 -0
  151. package/src/constants.ts +12 -0
  152. package/src/core/arrange.ts +448 -0
  153. package/src/core/clipboard.ts +504 -0
  154. package/src/core/css/generate.ts +236 -0
  155. package/src/core/css/scope.ts +88 -0
  156. package/src/core/design/cascade.ts +28 -0
  157. package/src/core/design/classes.ts +224 -0
  158. package/src/core/design/devices.ts +56 -0
  159. package/src/core/design/states.ts +49 -0
  160. package/src/core/design/transfer.ts +132 -0
  161. package/src/core/design/variables.ts +359 -0
  162. package/src/core/elements/definition.ts +12 -0
  163. package/src/core/elements/dynamic-elements.ts +225 -0
  164. package/src/core/elements/field-id.ts +6 -0
  165. package/src/core/elements/form-elements.ts +219 -0
  166. package/src/core/elements/index.ts +960 -0
  167. package/src/core/elements/menu.ts +190 -0
  168. package/src/core/elements/plain-text.ts +32 -0
  169. package/src/core/forms/binding.ts +61 -0
  170. package/src/core/forms/definition.ts +45 -0
  171. package/src/core/forms/success-layout.ts +63 -0
  172. package/src/core/icons/catalog.ts +273 -0
  173. package/src/core/index.ts +380 -0
  174. package/src/core/limits.ts +9 -0
  175. package/src/core/migrate/index.ts +126 -0
  176. package/src/core/nesting.ts +51 -0
  177. package/src/core/render/context.ts +21 -0
  178. package/src/core/render/dynamic-post.ts +175 -0
  179. package/src/core/render/extras.ts +98 -0
  180. package/src/core/render/form.ts +235 -0
  181. package/src/core/render/index.ts +372 -0
  182. package/src/core/render/loop.ts +43 -0
  183. package/src/core/render/pagination.ts +76 -0
  184. package/src/core/render/section.ts +37 -0
  185. package/src/core/render/tabs.ts +76 -0
  186. package/src/core/render/vnode.ts +87 -0
  187. package/src/core/sanitize/css.ts +771 -0
  188. package/src/core/sanitize/embed-url.ts +160 -0
  189. package/src/core/sanitize/escape.ts +10 -0
  190. package/src/core/sanitize/href.ts +6 -0
  191. package/src/core/sanitize/media-url.ts +7 -0
  192. package/src/core/sanitize/svg-upload.ts +203 -0
  193. package/src/core/sanitize/svg.ts +374 -0
  194. package/src/core/sanitize/url.ts +31 -0
  195. package/src/core/schema/descriptors.ts +37 -0
  196. package/src/core/schema/design.ts +93 -0
  197. package/src/core/schema/layout.ts +1008 -0
  198. package/src/core/schema/state-names.ts +4 -0
  199. package/src/core/schema/style.ts +336 -0
  200. package/src/core/stats.ts +18 -0
  201. package/src/core/tabs/presence.ts +25 -0
  202. package/src/core/theme/conditions.ts +324 -0
  203. package/src/core/theme/dynamic.ts +143 -0
  204. package/src/core/theme/float.ts +113 -0
  205. package/src/core/theme/pagination.ts +97 -0
  206. package/src/core/theme/parse-doc.ts +36 -0
  207. package/src/core/theme/part-types.ts +81 -0
  208. package/src/core/theme/popup-markup.ts +41 -0
  209. package/src/core/theme/popup-rules.ts +62 -0
  210. package/src/core/theme/triggers.ts +104 -0
  211. package/src/core/tree-ops.ts +139 -0
  212. package/src/core/validate.ts +185 -0
  213. package/src/index.ts +18 -0
  214. package/src/server/design-routes.ts +125 -0
  215. package/src/server/hooks.ts +118 -0
  216. package/src/server/icon-routes.ts +79 -0
  217. package/src/server/plugin.ts +43 -0
@@ -0,0 +1,236 @@
1
+ import type { DesignSystem } from "../schema/design.ts";
2
+ import { DEFAULT_STYLE_TAGS } from "../schema/design.ts";
3
+ import {
4
+ colorVariableName,
5
+ fontSizeVariableName,
6
+ fontVariableName,
7
+ isSafeCssValue,
8
+ isSafeFontStack,
9
+ spacingVariableName,
10
+ stateDeclarations,
11
+ styleClassName,
12
+ styleDeclarations,
13
+ withInheritedLayers,
14
+ type Declaration,
15
+ type StateDeclarations,
16
+ } from "../sanitize/css.ts";
17
+ import { STYLE_STATES, type StyleStateName } from "../schema/state-names.ts";
18
+ import { DEVICE_MEDIA, type PopupDevice } from "../theme/popup-rules.ts";
19
+
20
+ export type CssInput = {
21
+ design: DesignSystem;
22
+ usedTypes: ReadonlySet<string>;
23
+ baseCss: ReadonlyMap<string, string>;
24
+ localRules: ReadonlyArray<{
25
+ id: string;
26
+ declarations: Declaration[];
27
+ states?: StateDeclarations;
28
+ /** Tablet and mobile overrides (W-096). Desktop stays in `declarations`. */
29
+ tablet?: Declaration[];
30
+ mobile?: Declaration[];
31
+ hiddenOn?: readonly PopupDevice[];
32
+ }>;
33
+ /** Editor canvas only: repeat each state rule for `[data-emvb-state="<state>"]` (W-089). */
34
+ previewStates?: boolean;
35
+ /** When a node rendered a background video (W-110). */
36
+ backgroundVideo?: boolean;
37
+ /**
38
+ * Editor canvas only (W-116): the device the canvas previews. Device rules then follow this
39
+ * device instead of the frame's width, so the Desktop canvas never picks up tablet styles in a
40
+ * narrow window. Without it, the rules sit in their media queries as usual.
41
+ */
42
+ previewDevice?: PopupDevice;
43
+ };
44
+
45
+ /** Focus is keyboard focus (W-089): `:focus-visible`, never `:focus`. */
46
+ const STATE_PSEUDO: Readonly<Record<StyleStateName, string>> = {
47
+ hover: ":hover",
48
+ focus: ":focus-visible",
49
+ active: ":active",
50
+ };
51
+
52
+ /**
53
+ * Heading and Icon put their link inside the element (`innerLink`), so keyboard focus lands on
54
+ * that link, never on the element itself. Their Focus state also matches then (W-242).
55
+ */
56
+ const INNER_LINK_FOCUS = ":has(> :is(.emvb-heading-link,.emvb-icon-link):focus-visible)";
57
+
58
+ /** One state's selector list: Focus adds the inner-link form (W-242). */
59
+ const stateSelector = (selector: string, state: StyleStateName) =>
60
+ state === "focus"
61
+ ? `${selector}${STATE_PSEUDO.focus},${selector}${INNER_LINK_FOCUS}`
62
+ : `${selector}${STATE_PSEUDO[state]}`;
63
+
64
+ const block = (selector: string, declarations: Declaration[]) =>
65
+ declarations.length === 0
66
+ ? ""
67
+ : `${selector}{${declarations.map((d) => `${d.property}:${d.value}`).join(";")}}`;
68
+
69
+ /** Tag defaults sit under `:where` so a class (0,1,0) still beats the tag (0,0,1). */
70
+ function tagDefaultCss(design: DesignSystem): string {
71
+ const defaults = design.defaults;
72
+ if (!defaults) return "";
73
+ let css = "";
74
+ for (const tag of DEFAULT_STYLE_TAGS) {
75
+ const style = defaults[tag];
76
+ if (!style) continue;
77
+ const { declarations } = styleDeclarations(style);
78
+ css += block(`:where(.emvb-root) ${tag}`, declarations);
79
+ }
80
+ return css;
81
+ }
82
+
83
+ const safe = (value: string) => (isSafeCssValue(value) ? value : undefined);
84
+
85
+ const fontStack = (value: string) => (isSafeFontStack(value) ? value : undefined);
86
+
87
+ const lengthCss = (length: { value: number; unit: string }) =>
88
+ safe(`${length.value}${length.unit}`);
89
+
90
+ /** One custom property per variable whose id and value are both safe to emit. */
91
+ function declare<T extends { id: string }>(
92
+ variables: readonly T[] | undefined,
93
+ name: (id: string) => string | undefined,
94
+ value: (variable: T) => string | undefined,
95
+ ): Declaration[] {
96
+ return (variables ?? []).flatMap((variable) => {
97
+ const property = name(variable.id);
98
+ const css = value(variable);
99
+ return property && css ? [{ property, value: css }] : [];
100
+ });
101
+ }
102
+
103
+ /**
104
+ * Cascade order (R-021 / W-030 / W-089): variables on `.emvb-root`, base CSS, shared
105
+ * `.emvb-k-<id>` class rules, then local `.emvb-e-<id>` (local wins). Each selector's `:hover`,
106
+ * `:focus-visible` and `:active` rules follow its base rule in that order, so a state beats Normal,
107
+ * local states beat class states, and Active beats Hover when both apply. Transitions sit on the
108
+ * base rule, and one closing `prefers-reduced-motion: reduce` block turns them all off.
109
+ */
110
+ export function generateCss({
111
+ design,
112
+ usedTypes,
113
+ baseCss,
114
+ localRules,
115
+ previewStates = false,
116
+ backgroundVideo = false,
117
+ previewDevice,
118
+ }: CssInput): string {
119
+ const { colors, fonts, fontSizes, spacings } = design.variables;
120
+ const variables = [
121
+ ...declare(colors, colorVariableName, (color) => safe(color.value)),
122
+ ...declare(fonts, fontVariableName, (font) => fontStack(font.value)),
123
+ ...declare(fontSizes, fontSizeVariableName, (size) => lengthCss(size.value)),
124
+ ...declare(spacings, spacingVariableName, (space) => lengthCss(space.value)),
125
+ ];
126
+ const animated: string[] = [];
127
+ const entrances: string[] = [];
128
+ const rules = (selector: string, declarations: Declaration[], states: StateDeclarations) => [
129
+ block(selector, declarations),
130
+ ...STYLE_STATES.map((state) =>
131
+ block(
132
+ previewStates
133
+ ? `${stateSelector(selector, state)},${selector}[data-emvb-state="${state}"]`
134
+ : stateSelector(selector, state),
135
+ states[state] ?? [],
136
+ ),
137
+ ),
138
+ ];
139
+ const track = (selector: string, declarations: Declaration[]) => {
140
+ if (declarations.some((d) => d.property === "transition")) animated.push(selector);
141
+ if (declarations.some((d) => d.property === "animation")) entrances.push(selector);
142
+ return declarations;
143
+ };
144
+ const classes = (design.classes ?? []).flatMap((cls) => {
145
+ const className = styleClassName(cls.id);
146
+ return className
147
+ ? rules(
148
+ `.${className}`,
149
+ track(`.${className}`, styleDeclarations(cls.style).declarations),
150
+ stateDeclarations(cls.states).states,
151
+ )
152
+ : [];
153
+ });
154
+ const locals = localRules.flatMap((rule) =>
155
+ rules(`.emvb-e-${rule.id}`, track(`.emvb-e-${rule.id}`, rule.declarations), rule.states ?? {}),
156
+ );
157
+ const responsive = [
158
+ ...(design.classes ?? []).flatMap((cls) => {
159
+ const className = styleClassName(cls.id);
160
+ return className
161
+ ? [
162
+ {
163
+ selector: `.${className}`,
164
+ // W-210: a device that sets one background layer keeps the layers it inherits.
165
+ tablet: styleDeclarations(withInheritedLayers(cls.devices?.tablet, cls.style ?? {}))
166
+ .declarations,
167
+ mobile: styleDeclarations(
168
+ withInheritedLayers(cls.devices?.mobile, { ...cls.style, ...cls.devices?.tablet }),
169
+ ).declarations,
170
+ hiddenOn: cls.hiddenOn,
171
+ },
172
+ ]
173
+ : [];
174
+ }),
175
+ ...localRules.map((rule) => ({
176
+ selector: `.emvb-e-${rule.id}`,
177
+ tablet: rule.tablet ?? [],
178
+ mobile: rule.mobile ?? [],
179
+ hiddenOn: rule.hiddenOn,
180
+ })),
181
+ ];
182
+ /** A device block: in its media query, or, for a previewed device, on or off outright. */
183
+ const forDevices = (devices: readonly PopupDevice[], query: string, body: string) => {
184
+ if (!body) return "";
185
+ if (previewDevice === undefined) return `@media ${query}{${body}}`;
186
+ return devices.includes(previewDevice) ? body : "";
187
+ };
188
+ const at = (query: string, which: "tablet" | "mobile") =>
189
+ forDevices(
190
+ which === "tablet" ? ["tablet", "mobile"] : ["mobile"],
191
+ query,
192
+ responsive.map((rule) => block(rule.selector, rule[which])).join(""),
193
+ );
194
+ const hide = (device: PopupDevice, query: string) => {
195
+ const selectors = responsive
196
+ .filter((rule) => rule.hiddenOn?.includes(device))
197
+ .map((rule) => rule.selector);
198
+ return forDevices(
199
+ [device],
200
+ query,
201
+ selectors.length > 0 ? `${selectors.join(",")}{display:none}` : "",
202
+ );
203
+ };
204
+ for (const rule of responsive) {
205
+ if (
206
+ !entrances.includes(rule.selector) &&
207
+ [...rule.tablet, ...rule.mobile].some((d) => d.property === "animation")
208
+ ) {
209
+ entrances.push(rule.selector);
210
+ }
211
+ }
212
+ return [
213
+ entrances.length > 0
214
+ ? "@keyframes emvb-fade{from{opacity:0}to{opacity:1}}@keyframes emvb-fade-up{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}@keyframes emvb-fade-down{from{opacity:0;transform:translateY(-8px)}to{opacity:1;transform:none}}@keyframes emvb-slide-up{from{opacity:0;transform:translateY(16px)}to{opacity:1;transform:none}}@keyframes emvb-slide-down{from{opacity:0;transform:translateY(-16px)}to{opacity:1;transform:none}}@keyframes emvb-scale{from{opacity:0;transform:scale(0.96)}to{opacity:1;transform:none}}"
215
+ : "",
216
+ block(".emvb-root", variables),
217
+ tagDefaultCss(design),
218
+ backgroundVideo
219
+ ? ".emvb-has-bg-video{position:relative;overflow:hidden}.emvb-bg-video{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;z-index:0;pointer-events:none}.emvb-has-bg-video>:not(.emvb-bg-video){position:relative;z-index:1}"
220
+ : "",
221
+ ...[...usedTypes].toSorted().map((type) => baseCss.get(type) ?? ""),
222
+ ...classes,
223
+ ...locals,
224
+ at(DEVICE_MEDIA.tablet, "tablet"),
225
+ at(DEVICE_MEDIA.mobile, "mobile"),
226
+ hide("desktop", DEVICE_MEDIA.hideDesktop),
227
+ hide("tablet", DEVICE_MEDIA.hideTablet),
228
+ hide("mobile", DEVICE_MEDIA.hideMobile),
229
+ animated.length > 0
230
+ ? `@media (prefers-reduced-motion: reduce){${animated.join(",")}{transition:none}}`
231
+ : "",
232
+ entrances.length > 0
233
+ ? `@media (prefers-reduced-motion: reduce){${entrances.join(",")}{animation:none}}`
234
+ : "",
235
+ ].join("");
236
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Per-render style scoping (W-112). A public page and each theme part are rendered on their own,
3
+ * and each stylesheet repeats the base rules of the element types it uses. Without a scope, the
4
+ * page's base rules come after a header's local rules on the same specificity and win. Every
5
+ * selector here is prefixed with `:where(.emvb-s-<id>)`, so a sheet styles only the markup it was
6
+ * rendered with. The scope is a class, not a `data-emvb-*` attribute, because published HTML
7
+ * carries no `data-emvb` markers. `:where` adds no specificity, so the cascade inside a sheet and
8
+ * against the host's own CSS stays as it was.
9
+ */
10
+
11
+ const WRAPPER_CLASS = "emvb-scope";
12
+ const scopeClass = (token: string) => `emvb-s-${token}`;
13
+
14
+ /** A scope token that is safe in an attribute value and an attribute selector. */
15
+ export function cssScopeToken(id: string): string | undefined {
16
+ const token = id.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 80);
17
+ return token.length > 0 ? token : undefined;
18
+ }
19
+
20
+ /** The wrapper the scoped markup sits in. It takes no part in layout. */
21
+ export function scopeWrapperCss(): string {
22
+ return `:where(.${WRAPPER_CLASS}){display:contents}`;
23
+ }
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)}"`;
28
+ }
29
+
30
+ function splitSelectors(prelude: string): string[] {
31
+ const out: string[] = [];
32
+ let depth = 0;
33
+ let start = 0;
34
+ for (let i = 0; i < prelude.length; i++) {
35
+ const ch = prelude[i];
36
+ if (ch === "(" || ch === "[") depth++;
37
+ else if (ch === ")" || ch === "]") depth--;
38
+ else if (ch === "," && depth === 0) {
39
+ out.push(prelude.slice(start, i));
40
+ start = i + 1;
41
+ }
42
+ }
43
+ out.push(prelude.slice(start));
44
+ return out.map((s) => s.trim()).filter(Boolean);
45
+ }
46
+
47
+ /** The index just past the `}` that closes the block opened at `open`. */
48
+ function blockEnd(css: string, open: number): number {
49
+ let depth = 0;
50
+ for (let i = open; i < css.length; i++) {
51
+ if (css[i] === "{") depth++;
52
+ else if (css[i] === "}") {
53
+ depth--;
54
+ if (depth === 0) return i + 1;
55
+ }
56
+ }
57
+ return css.length;
58
+ }
59
+
60
+ const GROUPING_AT_RULE = /^@(media|supports|container|layer)\b/;
61
+
62
+ /** Prefixes every style rule in `css`, inside `@media` and `@supports` too. Keyframes stay as they are. */
63
+ export function scopeCss(css: string, token: string): string {
64
+ const prefix = `:where(.${scopeClass(token)}) `;
65
+ let out = "";
66
+ let i = 0;
67
+ while (i < css.length) {
68
+ const open = css.indexOf("{", i);
69
+ if (open === -1) {
70
+ out += css.slice(i);
71
+ break;
72
+ }
73
+ const prelude = css.slice(i, open).trim();
74
+ const end = blockEnd(css, open);
75
+ const body = css.slice(open + 1, end - 1);
76
+ if (GROUPING_AT_RULE.test(prelude)) {
77
+ out += `${prelude}{${scopeCss(body, token)}}`;
78
+ } else if (prelude.startsWith("@")) {
79
+ out += css.slice(i, end).trim();
80
+ } else {
81
+ out += `${splitSelectors(prelude)
82
+ .map((selector) => prefix + selector)
83
+ .join(",")}{${body}}`;
84
+ }
85
+ i = end;
86
+ }
87
+ return out;
88
+ }
@@ -0,0 +1,28 @@
1
+ import type { StyleProps } from "../schema/style.ts";
2
+
3
+ /** Classes the element uses, in Site styles list order. Later entries win, matching the CSS. */
4
+ export function classStylesInListOrder(
5
+ classes: ReadonlyArray<{ id: string; style?: StyleProps }> | undefined,
6
+ appliedIds: readonly string[],
7
+ ): Array<StyleProps | undefined> {
8
+ const applied = new Set(appliedIds);
9
+ return (classes ?? []).filter((cls) => applied.has(cls.id)).map((cls) => cls.style);
10
+ }
11
+
12
+ /**
13
+ * Merge class styles, then local. Later entries win (R-021 / W-030).
14
+ * Pass classes from `classStylesInListOrder`, not from the order written on the element.
15
+ * Pure — does not consult CSS; used for editor computed-style tables and unit tests.
16
+ */
17
+ export function resolveCascade(
18
+ classStylesInAppliedOrder: ReadonlyArray<StyleProps | undefined>,
19
+ local?: StyleProps,
20
+ ): StyleProps {
21
+ const out: Record<string, unknown> = {};
22
+ for (const style of classStylesInAppliedOrder) {
23
+ if (!style) continue;
24
+ Object.assign(out, style);
25
+ }
26
+ if (local) Object.assign(out, local);
27
+ return out as StyleProps;
28
+ }
@@ -0,0 +1,224 @@
1
+ import type { DesignSystem } from "../schema/design.ts";
2
+ import type { Layout, LayoutNode } from "../schema/layout.ts";
3
+ import type { StyleProps } from "../schema/style.ts";
4
+ import { nodeChildren, slugify, updateNode } from "../tree-ops.ts";
5
+
6
+ /** Reorder an applied class-id list. Out-of-range moves are no-ops. Pure (W-031). */
7
+ export function moveClassId(ids: readonly string[], index: number, delta: -1 | 1): string[] {
8
+ const next = [...ids];
9
+ const j = index + delta;
10
+ if (index < 0 || index >= next.length || j < 0 || j >= next.length) return [...ids];
11
+ const at = next[index];
12
+ const swap = next[j];
13
+ if (at === undefined || swap === undefined) return [...ids];
14
+ next[index] = swap;
15
+ next[j] = at;
16
+ return next;
17
+ }
18
+
19
+ /** Add a class id if missing (max 20 — layout schema). Preserves order. */
20
+ export function addClassId(ids: readonly string[], id: string): string[] {
21
+ if (ids.includes(id) || ids.length >= 20) return [...ids];
22
+ return [...ids, id];
23
+ }
24
+
25
+ /** Remove a class id; missing id is a silent no-op. */
26
+ export function removeClassId(ids: readonly string[], id: string): string[] {
27
+ return ids.filter((entry) => entry !== id);
28
+ }
29
+
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
+ /** Nodes that list `classId` in `node.classes` (W-032 delete dialog). */
36
+ export function findClassUsages(layout: Layout, classId: string): { nodeId: string }[] {
37
+ const usages: { nodeId: string }[] = [];
38
+ walk(layout.root, (node) => {
39
+ if (node.classes?.includes(classId)) usages.push({ nodeId: node.id });
40
+ });
41
+ return usages;
42
+ }
43
+
44
+ /** Drop a class id from every node that lists it. Pure. */
45
+ export function clearClassRefs(layout: Layout, classId: string): Layout {
46
+ let next = layout;
47
+ for (const { nodeId } of findClassUsages(layout, classId)) {
48
+ next = updateNode(next, nodeId, (node) => {
49
+ const classes = (node.classes ?? []).filter((id) => id !== classId);
50
+ if (classes.length === (node.classes ?? []).length) return node;
51
+ if (classes.length === 0) {
52
+ const { classes: _drop, ...rest } = node;
53
+ return rest as LayoutNode;
54
+ }
55
+ return { ...node, classes };
56
+ });
57
+ }
58
+ return next;
59
+ }
60
+
61
+ /**
62
+ * Move a class one step in the site list. Later classes win when they set the same property,
63
+ * because their CSS rule comes later. Out of range is a no-op. Pure.
64
+ */
65
+ export function moveDesignClass(
66
+ design: DesignSystem,
67
+ classId: string,
68
+ delta: -1 | 1,
69
+ ): DesignSystem {
70
+ const classes = [...(design.classes ?? [])];
71
+ const index = classes.findIndex((cls) => cls.id === classId);
72
+ const j = index + delta;
73
+ if (index < 0 || j < 0 || j >= classes.length) return design;
74
+ const next = classes.slice();
75
+ const at = next[index];
76
+ const swap = next[j];
77
+ if (at === undefined || swap === undefined) return design;
78
+ next[index] = swap;
79
+ next[j] = at;
80
+ return { ...design, classes: next };
81
+ }
82
+
83
+ const filled = (style: object | undefined) =>
84
+ !!style && Object.values(style).some((value) => value !== undefined);
85
+
86
+ /** Whether an element has local styles: base, hover/focus/active or tablet/mobile (W-134). */
87
+ export function hasLocalStyles(node: LayoutNode): boolean {
88
+ return (
89
+ filled(node.style) ||
90
+ Object.values(node.states ?? {}).some(filled) ||
91
+ Object.values(node.devices ?? {}).some(filled)
92
+ );
93
+ }
94
+
95
+ /** Why "Save local styles as class" can't run, or null when it can (W-134). */
96
+ /** Most classes a site may have; the design schema refuses more (W-218). */
97
+ export const MAX_CLASSES = 100;
98
+
99
+ /** A class name as saved: control characters gone, trimmed, at most 60 characters (W-218). */
100
+ export function cleanClassName(name: string): string {
101
+ const spaced = Array.from(name, (ch) => {
102
+ const code = ch.charCodeAt(0);
103
+ return code < 32 || code === 127 ? " " : ch;
104
+ }).join("");
105
+ return spaced.replace(/ {2,}/g, " ").trim().slice(0, 60).trim();
106
+ }
107
+
108
+ export function localToClassRefusal(design: DesignSystem, node: LayoutNode): string | null {
109
+ if (!hasLocalStyles(node)) return "This element has no local styles to save.";
110
+ if ((design.classes ?? []).length >= MAX_CLASSES)
111
+ return "This site already has 100 classes, the most allowed.";
112
+ if ((node.classes ?? []).length >= 20) return "20 classes is the most one element can have.";
113
+ return null;
114
+ }
115
+
116
+ /**
117
+ * Moves an element's local, state and tablet/mobile styles into a new class, applied last on the
118
+ * element and added last in Site styles, so it wins over the element's other classes as the local
119
+ * styles did (W-134). Visibility (`hiddenOn`) stays on the element. Null when it can't run. Pure.
120
+ */
121
+ export function localStylesToClass(
122
+ design: DesignSystem,
123
+ node: LayoutNode,
124
+ name: string,
125
+ ): { design: DesignSystem; node: LayoutNode; classId: string } | null {
126
+ const trimmed = name.trim().slice(0, 60);
127
+ if (!trimmed || localToClassRefusal(design, node)) return null;
128
+ const classes = design.classes ?? [];
129
+ const base = slugify(trimmed).slice(0, 34) || "class";
130
+ const taken = new Set(classes.map((c) => c.id));
131
+ let id = base;
132
+ for (let n = 2; taken.has(id); n++) id = `${base}-${n}`;
133
+ const cls = {
134
+ id,
135
+ name: trimmed,
136
+ style: structuredClone(node.style ?? {}) as StyleProps,
137
+ ...(node.states ? { states: structuredClone(node.states) } : {}),
138
+ ...(node.devices ? { devices: structuredClone(node.devices) } : {}),
139
+ };
140
+ const { style: _style, states: _states, devices: _devices, ...rest } = node;
141
+ return {
142
+ design: { ...design, classes: [...classes, cls] },
143
+ node: { ...rest, classes: addClassId(node.classes ?? [], id) } as LayoutNode,
144
+ classId: id,
145
+ };
146
+ }
147
+
148
+ /** Duplicate a design class with a new id/name, deep-copying all its styles. Pure (W-071, W-135). */
149
+ export function duplicateClass(
150
+ design: DesignSystem,
151
+ classId: string,
152
+ randomSuffix?: string,
153
+ ): DesignSystem {
154
+ const classes = design.classes ?? [];
155
+ const source = classes.find((c) => c.id === classId);
156
+ // At the cap the copy would make the design invalid and the save fail (W-218).
157
+ if (!source || classes.length >= MAX_CLASSES) return design;
158
+ // W-277: "Card copy", then "Card copy 2"…, so the copy's name is free too.
159
+ const stem = `${source.name.slice(0, 52).trim()} copy`;
160
+ let baseName = stem;
161
+ for (let n = 2; n <= MAX_CLASSES && classNameTaken(design, baseName); n++) {
162
+ baseName = `${stem} ${n}`;
163
+ }
164
+ const base = slugify(baseName).slice(0, 34) || "class";
165
+ const taken = new Set(classes.map((c) => c.id));
166
+ let id = randomSuffix ? `${base}-${randomSuffix}`.slice(0, 40) : base;
167
+ for (let n = 2; taken.has(id); n++) id = `${base}-${n}`.slice(0, 40);
168
+ // Every part of the class, tablet and mobile styles and Hide on included (W-135).
169
+ const copy = { ...structuredClone(source), id, name: baseName };
170
+ return { ...design, classes: [...classes, copy] };
171
+ }
172
+
173
+ /**
174
+ * W-277: whether another class already has this name (case-insensitive). Two classes with one
175
+ * name can't be told apart in the class picker or the Site styles list.
176
+ */
177
+ export function classNameTaken(design: DesignSystem, name: string, exceptId?: string): boolean {
178
+ const wanted = cleanClassName(name).toLowerCase();
179
+ return (design.classes ?? []).some((c) => c.id !== exceptId && c.name.toLowerCase() === wanted);
180
+ }
181
+
182
+ /** The message shown when a new or renamed class would take another class's name (W-277). */
183
+ export const classNameTakenMessage = (name: string) =>
184
+ `A class named "${cleanClassName(name)}" already exists. Pick another name.`;
185
+
186
+ /**
187
+ * Rename a design class; the id (and so its `.emvb-k-<id>` selector) stays. A name another class
188
+ * has is refused (W-277). Pure (W-087).
189
+ */
190
+ export function renameClass(design: DesignSystem, classId: string, name: string): DesignSystem {
191
+ const trimmed = cleanClassName(name);
192
+ const classes = design.classes ?? [];
193
+ if (!trimmed || !classes.some((c) => c.id === classId)) return design;
194
+ if (classNameTaken(design, trimmed, classId)) return design;
195
+ return {
196
+ ...design,
197
+ classes: classes.map((c) => (c.id === classId ? Object.assign({}, c, { name: trimmed }) : c)),
198
+ };
199
+ }
200
+
201
+ /** Merge `patch` into a class's style; `undefined` values clear their property. Pure (W-087). */
202
+ export function patchClassStyle(
203
+ design: DesignSystem,
204
+ classId: string,
205
+ patch: Partial<StyleProps>,
206
+ ): DesignSystem {
207
+ const classes = design.classes ?? [];
208
+ if (!classes.some((c) => c.id === classId)) return design;
209
+ return {
210
+ ...design,
211
+ classes: classes.map((c) => {
212
+ if (c.id !== classId) return c;
213
+ const style: Record<string, unknown> = { ...c.style, ...patch };
214
+ for (const [key, value] of Object.entries(patch)) if (value === undefined) delete style[key];
215
+ return Object.assign({}, c, { style: style as StyleProps });
216
+ }),
217
+ };
218
+ }
219
+
220
+ /** Swap one applied class id for another in place, keeping the cascade order. Pure (W-087). */
221
+ export function replaceClassId(ids: readonly string[], from: string, to: string): string[] {
222
+ if (ids.includes(to)) return removeClassId(ids, from);
223
+ return ids.map((id) => (id === from ? to : id));
224
+ }
@@ -0,0 +1,56 @@
1
+ import type { DesignSystem } from "../schema/design.ts";
2
+ import type { DeviceStyles, StyleProps } from "../schema/style.ts";
3
+ import { POPUP_DEVICES, type PopupDevice } from "../theme/popup-rules.ts";
4
+
5
+ export type ResponsiveDevice = "tablet" | "mobile";
6
+
7
+ /**
8
+ * Merge `patch` into one device's overrides (W-096). `undefined` clears a key. An emptied device
9
+ * is dropped, and so is `devices` itself. Pure.
10
+ */
11
+ export function patchDeviceStyle(
12
+ devices: DeviceStyles | undefined,
13
+ device: ResponsiveDevice,
14
+ patch: Partial<StyleProps>,
15
+ ): DeviceStyles | undefined {
16
+ const merged: Record<string, unknown> = { ...devices?.[device], ...patch };
17
+ for (const key of Object.keys(merged)) if (merged[key] === undefined) delete merged[key];
18
+ const next: DeviceStyles = { ...devices };
19
+ if (Object.keys(merged).length > 0) next[device] = merged as StyleProps;
20
+ else delete next[device];
21
+ return next.tablet || next.mobile ? next : undefined;
22
+ }
23
+
24
+ /** `patchDeviceStyle` on a design class. Pure. */
25
+ export function patchClassDevices(
26
+ design: DesignSystem,
27
+ classId: string,
28
+ device: ResponsiveDevice,
29
+ patch: Partial<StyleProps>,
30
+ ): DesignSystem {
31
+ const classes = design.classes ?? [];
32
+ if (!classes.some((c) => c.id === classId)) return design;
33
+ return {
34
+ ...design,
35
+ classes: classes.map((c) => {
36
+ if (c.id !== classId) return c;
37
+ const next = Object.assign({}, c);
38
+ const devices = patchDeviceStyle(c.devices, device, patch);
39
+ if (devices) next.devices = devices;
40
+ else delete next.devices;
41
+ return next;
42
+ }),
43
+ };
44
+ }
45
+
46
+ /** Add or remove one device. The result stays in desktop, tablet, mobile order, or undefined. */
47
+ export function toggleHidden(
48
+ hidden: readonly PopupDevice[] | undefined,
49
+ device: PopupDevice,
50
+ ): PopupDevice[] | undefined {
51
+ const set = new Set(hidden ?? []);
52
+ if (set.has(device)) set.delete(device);
53
+ else set.add(device);
54
+ const next = POPUP_DEVICES.filter((item) => set.has(item));
55
+ return next.length > 0 ? next : undefined;
56
+ }
@@ -0,0 +1,49 @@
1
+ import type { DesignSystem } from "../schema/design.ts";
2
+ import { STYLE_STATES, type StyleStateName } from "../schema/state-names.ts";
3
+ import type { StyleProps, StyleStates } from "../schema/style.ts";
4
+
5
+ type StateStyle = NonNullable<StyleStates[StyleStateName]>;
6
+
7
+ /**
8
+ * Merge `patch` into one state's styles (W-089); `undefined` clears a key. An emptied state is
9
+ * dropped, and so is `states` itself. Pure.
10
+ */
11
+ export function patchStates(
12
+ states: StyleStates | undefined,
13
+ state: StyleStateName,
14
+ patch: Partial<StyleProps>,
15
+ ): StyleStates | undefined {
16
+ const merged: Record<string, unknown> = { ...states?.[state], ...patch };
17
+ for (const key of Object.keys(merged)) if (merged[key] === undefined) delete merged[key];
18
+ delete merged["transition"];
19
+ const next: StyleStates = { ...states };
20
+ if (Object.keys(merged).length > 0) next[state] = merged as StateStyle;
21
+ else delete next[state];
22
+ return STYLE_STATES.some((s) => next[s] !== undefined) ? next : undefined;
23
+ }
24
+
25
+ /** Whether a node or class has any state value (the "Has state styles" dot). */
26
+ export const hasStateStyles = (owner: { states?: StyleStates } | undefined): boolean =>
27
+ STYLE_STATES.some((state) => Object.keys(owner?.states?.[state] ?? {}).length > 0);
28
+
29
+ /** `patchStates` on a design class. Pure. */
30
+ export function patchClassState(
31
+ design: DesignSystem,
32
+ classId: string,
33
+ state: StyleStateName,
34
+ patch: Partial<StyleProps>,
35
+ ): DesignSystem {
36
+ const classes = design.classes ?? [];
37
+ if (!classes.some((c) => c.id === classId)) return design;
38
+ return {
39
+ ...design,
40
+ classes: classes.map((c) => {
41
+ if (c.id !== classId) return c;
42
+ const next = Object.assign({}, c);
43
+ const states = patchStates(c.states, state, patch);
44
+ if (states) next.states = states;
45
+ else delete next.states;
46
+ return next;
47
+ }),
48
+ };
49
+ }