@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,171 @@
1
+ import { DropdownMenu } from "@cloudflare/kumo";
2
+ import { BracketsCurlyIcon, RulerIcon, TextAaIcon, TextTIcon, XIcon } from "@phosphor-icons/react";
3
+ import type { DesignSystem, StyleProps } from "../../../../core/index.ts";
4
+ import type { StyleKey } from "./style-sections.ts";
5
+
6
+ /** Variable kinds a length or font property can bind to. Colours bind through ColorControl. */
7
+ export type BindKind = "font" | "fontSize" | "spacing";
8
+
9
+ const SPACING_KEYS = new Set<StyleKey>([
10
+ "gap",
11
+ "paddingTop",
12
+ "paddingRight",
13
+ "paddingBottom",
14
+ "paddingLeft",
15
+ "marginTop",
16
+ "marginRight",
17
+ "marginBottom",
18
+ "marginLeft",
19
+ "top",
20
+ "right",
21
+ "bottom",
22
+ "left",
23
+ "borderRadius",
24
+ "borderTopLeftRadius",
25
+ "borderTopRightRadius",
26
+ "borderBottomRightRadius",
27
+ "borderBottomLeftRadius",
28
+ ]);
29
+
30
+ /** The variable kind a style property takes, or null when it has no variable button. */
31
+ export function bindKind(key: StyleKey): BindKind | null {
32
+ if (key === "fontFamily") return "font";
33
+ if (key === "fontSize") return "fontSize";
34
+ return SPACING_KEYS.has(key) ? "spacing" : null;
35
+ }
36
+
37
+ type Variable = { id: string; name: string; value: string | { value: number; unit: string } };
38
+
39
+ const LIST = { font: "fonts", fontSize: "fontSizes", spacing: "spacings" } as const;
40
+ const NOUN = { font: "font", fontSize: "font size", spacing: "spacing" } as const;
41
+ const KIND_ICON = { font: TextTIcon, fontSize: TextAaIcon, spacing: RulerIcon } as const;
42
+
43
+ const variablesOf = (design: DesignSystem, kind: BindKind): readonly Variable[] =>
44
+ design.variables[LIST[kind]] ?? [];
45
+
46
+ const shown = (value: Variable["value"]) =>
47
+ typeof value === "string" ? value : `${value.value}${value.unit}`;
48
+
49
+ /** A bound `{ var }` reference of `kind`, if `value` is one. */
50
+ export function boundRef(value: unknown, kind: BindKind): { var: string } | null {
51
+ if (!value || typeof value !== "object" || !("var" in value)) return null;
52
+ const ref = value as { var: string; from?: string };
53
+ return ref.from === kind ? ref : null;
54
+ }
55
+
56
+ /**
57
+ * Any length variable a value is bound to, whatever the row's own kind: a spacing variable on
58
+ * Font size still renders as `var(--emvb-s-…)`, so it shows as a spacing chip (W-088).
59
+ */
60
+ export function lengthRef(value: unknown): { var: string; kind: "fontSize" | "spacing" } | null {
61
+ if (!value || typeof value !== "object" || !("var" in value)) return null;
62
+ const ref = value as { var: string; from?: string };
63
+ return ref.from === "fontSize" || ref.from === "spacing"
64
+ ? { var: ref.var, kind: ref.from }
65
+ : null;
66
+ }
67
+
68
+ /** The variable button (Phosphor BracketsCurly): lists the site's variables of the kind. */
69
+ export function VariableButton({
70
+ label,
71
+ kind,
72
+ design,
73
+ current,
74
+ onBind,
75
+ }: {
76
+ label: string;
77
+ kind: BindKind;
78
+ design: DesignSystem;
79
+ current: string | null;
80
+ onBind: (ref: NonNullable<StyleProps["gap"]>) => void;
81
+ }) {
82
+ const list = variablesOf(design, kind);
83
+ return (
84
+ <DropdownMenu>
85
+ <DropdownMenu.Trigger>
86
+ <button
87
+ type="button"
88
+ className="emvb-var-btn"
89
+ aria-label={`Use a variable for ${label}`}
90
+ title="Use a variable"
91
+ data-active={current ? "true" : undefined}
92
+ >
93
+ <BracketsCurlyIcon size={16} aria-hidden="true" />
94
+ </button>
95
+ </DropdownMenu.Trigger>
96
+ <DropdownMenu.Content data-emvb-var-menu={kind}>
97
+ {list.length === 0 ? (
98
+ <DropdownMenu.Item disabled>
99
+ No {NOUN[kind]} variables yet. Add them in Site styles.
100
+ </DropdownMenu.Item>
101
+ ) : (
102
+ list.map((variable) => (
103
+ <DropdownMenu.Item
104
+ key={variable.id}
105
+ data-emvb-var-option={variable.id}
106
+ data-selected={variable.id === current ? "true" : undefined}
107
+ onClick={() => onBind({ var: variable.id, from: kind })}
108
+ >
109
+ <span className="emvb-var-option">
110
+ <span
111
+ className="emvb-var-option-sample"
112
+ aria-hidden="true"
113
+ style={kind === "font" ? { fontFamily: shown(variable.value) } : undefined}
114
+ >
115
+ {kind === "font" ? "Ag" : kind === "fontSize" ? "Aa" : ""}
116
+ </span>
117
+ <span className="emvb-var-option-name">{variable.name}</span>
118
+ <span className="emvb-var-option-value">{shown(variable.value)}</span>
119
+ </span>
120
+ </DropdownMenu.Item>
121
+ ))
122
+ )}
123
+ </DropdownMenu.Content>
124
+ </DropdownMenu>
125
+ );
126
+ }
127
+
128
+ /**
129
+ * The variable chip that stands in for a bound value: the kind icon, the variable's
130
+ * name and an × that detaches it, keeping the variable's current value as a literal.
131
+ */
132
+ export function VariableChip({
133
+ label,
134
+ kind,
135
+ design,
136
+ id,
137
+ onDetach,
138
+ }: {
139
+ label: string;
140
+ kind: BindKind;
141
+ design: DesignSystem;
142
+ id: string;
143
+ onDetach: (literal: Variable["value"] | undefined) => void;
144
+ }) {
145
+ const variable = variablesOf(design, kind).find((v) => v.id === id);
146
+ const Icon = KIND_ICON[kind];
147
+ return (
148
+ <div className="emvb-var-field" data-emvb-var-bound={id}>
149
+ <span className="emvb-var-field-label">{label}</span>
150
+ <span
151
+ className="emvb-var-chip"
152
+ data-missing={variable ? undefined : "true"}
153
+ title={
154
+ variable ? `${variable.name} · ${shown(variable.value)}` : "This variable was deleted"
155
+ }
156
+ >
157
+ <Icon size={14} aria-hidden="true" />
158
+ <span className="emvb-var-chip-name">{variable?.name ?? `Missing (${id})`}</span>
159
+ {variable && <span className="emvb-var-chip-value">{shown(variable.value)}</span>}
160
+ <button
161
+ type="button"
162
+ className="emvb-var-chip-x"
163
+ aria-label={`Detach ${variable?.name ?? id} from ${label}`}
164
+ onClick={() => onDetach(variable?.value)}
165
+ >
166
+ <XIcon size={12} aria-hidden="true" />
167
+ </button>
168
+ </span>
169
+ </div>
170
+ );
171
+ }
@@ -0,0 +1,63 @@
1
+ import type { Layout, LayoutNode } from "../../../../core/index.ts";
2
+ import { isReservedAttribute } from "../../../../core/render/extras.ts";
3
+ import { nodeChildren } from "../../../../core/tree-ops.ts";
4
+
5
+ /** Same rules as the layout schema and the renderer (W-105, R-032), checked while typing (W-189). */
6
+ const HTML_ID = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
7
+ const ATTR_NAME = /^(?:data|aria)-[a-z][a-z0-9-]{0,40}$/;
8
+ export const MAX_ATTR_VALUE = 200;
9
+
10
+ export type AttributeRow = { name: string; value: string };
11
+
12
+ /** CSS ids on `layout` other than the one on `exceptId`. */
13
+ function otherHtmlIds(layout: Layout | null, exceptId: string): Set<string> {
14
+ const ids = new Set<string>();
15
+ const walk = (node: LayoutNode) => {
16
+ if (node.id !== exceptId && node.htmlId) ids.add(node.htmlId);
17
+ for (const child of nodeChildren(node)) walk(child);
18
+ };
19
+ if (layout) walk(layout.root);
20
+ return ids;
21
+ }
22
+
23
+ /** Why a CSS id can't be saved, or null when it can (empty clears it). */
24
+ export function htmlIdError(value: string, layout: Layout | null, nodeId: string): string | null {
25
+ if (value === "") return null;
26
+ if (!HTML_ID.test(value)) {
27
+ return "Start with a letter; use letters, digits, - and _ only (up to 64).";
28
+ }
29
+ if (otherHtmlIds(layout, nodeId).has(value)) return "Another element already uses this CSS id.";
30
+ return null;
31
+ }
32
+
33
+ /** A just-added row nobody has typed in yet: not saved, and not flagged. */
34
+ const isFreshRow = (row: AttributeRow) => row.name === "data-" && row.value === "";
35
+
36
+ /** Each row's error (null when it can be saved), in row order. */
37
+ export function attributeErrors(rows: readonly AttributeRow[]): (string | null)[] {
38
+ const seen = new Set<string>();
39
+ return rows.map((row) => {
40
+ if (isFreshRow(row)) return null;
41
+ let error: string | null = null;
42
+ if (!ATTR_NAME.test(row.name)) {
43
+ error = "Use data-* or aria-*, then a lowercase letter, digits or -.";
44
+ } else if (isReservedAttribute(row.name)) {
45
+ error = "This name is reserved for the editor or forms.";
46
+ } else if (seen.has(row.name)) {
47
+ error = "This name is already used on this element.";
48
+ } else if (row.value.length > MAX_ATTR_VALUE) {
49
+ error = `Values are at most ${MAX_ATTR_VALUE} characters.`;
50
+ } else if ([...row.value].some((char) => char.charCodeAt(0) < 32)) {
51
+ error = "Values can't hold line breaks or control characters.";
52
+ }
53
+ seen.add(row.name);
54
+ return error;
55
+ });
56
+ }
57
+
58
+ /** The rows that may be stored: never one the schema would refuse, so the page stays savable. */
59
+ export function savableAttributes(rows: readonly AttributeRow[]): AttributeRow[] | undefined {
60
+ const errors = attributeErrors(rows);
61
+ const kept = rows.filter((row, i) => !isFreshRow(row) && errors[i] === null);
62
+ return kept.length > 0 ? kept : undefined;
63
+ }
@@ -0,0 +1,112 @@
1
+ import type { StyleProps } from "../../../../core/index.ts";
2
+ import { STYLE_UI } from "./style-sections.ts";
3
+ import { NEW_GRADIENT } from "./BackgroundControls.tsx";
4
+
5
+ export type FillMode = "color" | "gradient" | "image" | "video";
6
+
7
+ const BOXES = new Set([
8
+ "container",
9
+ "flexbox",
10
+ "grid",
11
+ "div-block",
12
+ "layout-section",
13
+ "form",
14
+ "section",
15
+ "tab-panel",
16
+ "accordion",
17
+ "accordion-item",
18
+ ]);
19
+
20
+ /** Color and gradient everywhere. Image and video on boxes, or when already set. */
21
+ export function fillModes(type: string, style?: StyleProps): FillMode[] {
22
+ const modes: FillMode[] = ["color", "gradient"];
23
+ const box = BOXES.has(type) || !(type in STYLE_UI);
24
+ if (box || type === "button" || type === "submit" || style?.backgroundImage !== undefined)
25
+ modes.push("image");
26
+ if (box || style?.backgroundVideo !== undefined) modes.push("video");
27
+ return modes;
28
+ }
29
+
30
+ /** The fill the page is showing. Video wins, then image, then gradient, then a solid color. */
31
+ export function activeFill(style: StyleProps | undefined): FillMode {
32
+ if (style?.backgroundVideo) return "video";
33
+ if (style?.backgroundImage) return "image";
34
+ if (style?.gradient) return "gradient";
35
+ return "color";
36
+ }
37
+
38
+ /** More than one fill is stored, so the page paints a stack the type switcher will collapse. */
39
+ export function fillIsMixed(style: StyleProps | undefined): boolean {
40
+ return (
41
+ [
42
+ style?.backgroundColor !== undefined,
43
+ style?.gradient !== undefined,
44
+ style?.backgroundImage !== undefined,
45
+ style?.backgroundVideo !== undefined,
46
+ ].filter(Boolean).length > 1
47
+ );
48
+ }
49
+
50
+ const CLEARED = {
51
+ backgroundColor: undefined,
52
+ backgroundImage: undefined,
53
+ backgroundSize: undefined,
54
+ backgroundPosition: undefined,
55
+ backgroundRepeat: undefined,
56
+ gradient: undefined,
57
+ overlay: undefined,
58
+ backgroundVideo: undefined,
59
+ } as const;
60
+
61
+ /** Choosing a type removes the other fills. Overlay stays with an image or a video. */
62
+ function switchFill(style: StyleProps | undefined, mode: FillMode): Partial<StyleProps> {
63
+ if (mode === "gradient") return { ...CLEARED, gradient: style?.gradient ?? NEW_GRADIENT };
64
+ if (mode === "image") {
65
+ return {
66
+ ...CLEARED,
67
+ backgroundImage: style?.backgroundImage,
68
+ backgroundSize: style?.backgroundSize,
69
+ backgroundPosition: style?.backgroundPosition,
70
+ backgroundRepeat: style?.backgroundRepeat,
71
+ overlay: style?.overlay,
72
+ };
73
+ }
74
+ if (mode === "video") {
75
+ return { ...CLEARED, backgroundVideo: style?.backgroundVideo, overlay: style?.overlay };
76
+ }
77
+ return { ...CLEARED, backgroundColor: style?.backgroundColor };
78
+ }
79
+
80
+ /** The fields a fill type owns as stored now, or undefined when that type has nothing set. */
81
+ function heldFill(style: StyleProps | undefined, mode: FillMode): Partial<StyleProps> | undefined {
82
+ const main = {
83
+ color: style?.backgroundColor,
84
+ gradient: style?.gradient,
85
+ image: style?.backgroundImage,
86
+ video: style?.backgroundVideo,
87
+ }[mode];
88
+ if (main === undefined) return undefined;
89
+ return Object.fromEntries(
90
+ Object.entries(switchFill(style, mode)).filter(([, value]) => value !== undefined),
91
+ ) as Partial<StyleProps>;
92
+ }
93
+
94
+ /** What each fill type last held while this element was being edited (W-184). */
95
+ export type FillMemory = Partial<Record<FillMode, Partial<StyleProps>>>;
96
+
97
+ /**
98
+ * Like `switchFill`, but the type being left is remembered in `memory`, and an empty type being
99
+ * chosen gets back what it last held, so Gradient → Color → Gradient keeps the gradient (W-184).
100
+ */
101
+ export function switchFillRemembered(
102
+ style: StyleProps | undefined,
103
+ from: FillMode,
104
+ to: FillMode,
105
+ memory: FillMemory,
106
+ ): Partial<StyleProps> {
107
+ const held = heldFill(style, from);
108
+ if (held) memory[from] = held;
109
+ const remembered = memory[to];
110
+ if (remembered && heldFill(style, to) === undefined) return { ...CLEARED, ...remembered };
111
+ return switchFill(style, to);
112
+ }
@@ -0,0 +1,38 @@
1
+ import { sanitizeHref, type LayoutNode } from "../../../../core/index.ts";
2
+ import { nodeChildren } from "../../../../core/tree-ops.ts";
3
+
4
+ /** Props whose text names a link for screen readers when the element sits inside it. */
5
+ const TEXT_PROPS = ["text", "label", "summary", "title", "alt"] as const;
6
+
7
+ /** Elements whose text comes from the post or another layout; assume they name the link. */
8
+ const DYNAMIC = new Set(["section", "loop"]);
9
+
10
+ const hasName = (node: LayoutNode): boolean => {
11
+ if (DYNAMIC.has(node.type) || node.type.startsWith("post-")) return true;
12
+ const attrs = (node as { attributes?: { name: string; value: string }[] }).attributes ?? [];
13
+ if (
14
+ attrs.some((a) => (a.name === "aria-label" || a.name === "aria-labelledby") && a.value.trim())
15
+ ) {
16
+ return true;
17
+ }
18
+ const props = (node.props ?? {}) as Record<string, unknown>;
19
+ if (TEXT_PROPS.some((key) => typeof props[key] === "string" && props[key].trim() !== "")) {
20
+ return true;
21
+ }
22
+ return nodeChildren(node).some(hasName);
23
+ };
24
+
25
+ /**
26
+ * W-261: a box (Container, Div Block, Flexbox, Grid…) with a working Link but nothing inside that
27
+ * names it: no text, no image alt, no icon title, no aria-label. It renders as an `<a>` that
28
+ * screen readers announce only as "link" (axe `link-name`).
29
+ */
30
+ export function unnamedBoxLink(node: LayoutNode): boolean {
31
+ const href = (node.props as { href?: unknown } | undefined)?.href;
32
+ if (typeof href !== "string" || !href.trim() || !sanitizeHref(href)) return false;
33
+ if (!("children" in node)) return false;
34
+ return !hasName(node);
35
+ }
36
+
37
+ export const UNNAMED_BOX_LINK_NOTE =
38
+ "Nothing inside this box has text, so screen readers announce it only as “link”. Add text inside it, or an aria-label under Custom attributes.";
@@ -0,0 +1,153 @@
1
+ import type { StyleProps } from "../../../../core/index.ts";
2
+ import type { LengthUnit } from "./length-units.ts";
3
+ import type { StyleKey } from "./style-sections.ts";
4
+
5
+ type Four<T> = readonly [T, T, T, T];
6
+
7
+ export type BoxGroupId = "padding" | "margin" | "borderWidth" | "borderRadius";
8
+
9
+ /** One linked four-box control (W-138, D-044): padding, margin, border width or radius. */
10
+ export type BoxGroup = {
11
+ id: BoxGroupId;
12
+ label: string;
13
+ /** Border width and radius keep an all-sides key; a side or corner beats it. */
14
+ all?: StyleKey;
15
+ /** Top, right, bottom, left; or the corners clockwise from top left. */
16
+ sides: Four<StyleKey>;
17
+ /** The short names under the boxes. */
18
+ names: Four<string>;
19
+ noun: "sides" | "corners";
20
+ };
21
+
22
+ export const BOX_GROUPS: readonly BoxGroup[] = [
23
+ {
24
+ id: "padding",
25
+ label: "Padding",
26
+ sides: ["paddingTop", "paddingRight", "paddingBottom", "paddingLeft"],
27
+ names: ["Top", "Right", "Bottom", "Left"],
28
+ noun: "sides",
29
+ },
30
+ {
31
+ id: "margin",
32
+ label: "Margin",
33
+ sides: ["marginTop", "marginRight", "marginBottom", "marginLeft"],
34
+ names: ["Top", "Right", "Bottom", "Left"],
35
+ noun: "sides",
36
+ },
37
+ {
38
+ id: "borderWidth",
39
+ label: "Border width",
40
+ all: "borderWidth",
41
+ sides: ["borderTopWidth", "borderRightWidth", "borderBottomWidth", "borderLeftWidth"],
42
+ names: ["Top", "Right", "Bottom", "Left"],
43
+ noun: "sides",
44
+ },
45
+ {
46
+ id: "borderRadius",
47
+ label: "Radius",
48
+ all: "borderRadius",
49
+ sides: [
50
+ "borderTopLeftRadius",
51
+ "borderTopRightRadius",
52
+ "borderBottomRightRadius",
53
+ "borderBottomLeftRadius",
54
+ ],
55
+ names: ["Top left", "Top right", "Bottom right", "Bottom left"],
56
+ noun: "corners",
57
+ },
58
+ ];
59
+
60
+ const BY_KEY = new Map<StyleKey, BoxGroup>(
61
+ BOX_GROUPS.flatMap((group) =>
62
+ [...(group.all ? [group.all] : []), ...group.sides].map((key) => [key, group] as const),
63
+ ),
64
+ );
65
+
66
+ /** The four-box control a style key belongs to, if any. */
67
+ export const boxGroupOf = (key: StyleKey): BoxGroup | undefined => BY_KEY.get(key);
68
+
69
+ /** Every key the control writes. */
70
+ const groupKeys = (group: BoxGroup): StyleKey[] => [
71
+ ...(group.all ? [group.all] : []),
72
+ ...group.sides,
73
+ ];
74
+
75
+ /** What each box shows: its own value, else the all-sides value. */
76
+ export function sideValues(group: BoxGroup, style: StyleProps | undefined): unknown[] {
77
+ const all = group.all ? style?.[group.all] : undefined;
78
+ return group.sides.map((key) => style?.[key] ?? all);
79
+ }
80
+
81
+ const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
82
+
83
+ /** True when every box shows the same value, unset included. */
84
+ export const allSame = (values: readonly unknown[]) => values.every((v) => same(v, values[0]));
85
+
86
+ /** True when the edited target sets any of the control's keys. */
87
+ export const groupSet = (group: BoxGroup, style: StyleProps | undefined) =>
88
+ groupKeys(group).some((key) => style?.[key] !== undefined);
89
+
90
+ /** One value on every box. Border width and radius use their all-sides key and drop the sides. */
91
+ export function linkedPatch(group: BoxGroup, value: unknown): Partial<StyleProps> {
92
+ const patch: Record<string, unknown> = {};
93
+ if (group.all) {
94
+ patch[group.all] = value;
95
+ for (const key of group.sides) patch[key] = undefined;
96
+ } else {
97
+ for (const key of group.sides) patch[key] = value;
98
+ }
99
+ return patch as Partial<StyleProps>;
100
+ }
101
+
102
+ /** One box's value. */
103
+ export const sidePatch = (group: BoxGroup, index: number, value: unknown): Partial<StyleProps> =>
104
+ ({ [group.sides[index] as StyleKey]: value }) as Partial<StyleProps>;
105
+
106
+ /** Linking copies the first box that has a value to all four. */
107
+ export function linkPatch(group: BoxGroup, style: StyleProps | undefined): Partial<StyleProps> {
108
+ return linkedPatch(
109
+ group,
110
+ sideValues(group, style).find((value) => value !== undefined),
111
+ );
112
+ }
113
+
114
+ /** Clears every key of the control. */
115
+ export function resetPatch(group: BoxGroup): Partial<StyleProps> {
116
+ const patch: Record<string, unknown> = {};
117
+ for (const key of groupKeys(group)) patch[key] = undefined;
118
+ return patch as Partial<StyleProps>;
119
+ }
120
+
121
+ const isLiteral = (value: unknown): value is { value: number; unit: LengthUnit } =>
122
+ typeof value === "object" &&
123
+ value !== null &&
124
+ typeof (value as { value?: unknown }).value === "number" &&
125
+ typeof (value as { unit?: unknown }).unit === "string";
126
+
127
+ /** The shared unit menu: every number the control sets keeps its value in the new unit. */
128
+ export function unitPatch(
129
+ group: BoxGroup,
130
+ style: StyleProps | undefined,
131
+ unit: LengthUnit,
132
+ ): Partial<StyleProps> {
133
+ const patch: Record<string, unknown> = {};
134
+ for (const key of groupKeys(group)) {
135
+ const value = style?.[key];
136
+ if (isLiteral(value) && value.unit !== unit) patch[key] = { value: value.value, unit };
137
+ }
138
+ return patch as Partial<StyleProps>;
139
+ }
140
+
141
+ /** The unit the shared menu shows: the first box with a number, else `fallback`. */
142
+ export function sharedUnit(values: readonly unknown[], fallback: LengthUnit): LengthUnit {
143
+ return (values.find(isLiteral) as { unit: LengthUnit } | undefined)?.unit ?? fallback;
144
+ }
145
+
146
+ /** A box's text: the number (with its unit when it differs from the menu), `auto` or "". */
147
+ export function sideDraft(value: unknown, unit: LengthUnit, varName?: string): string {
148
+ if (isLiteral(value))
149
+ return value.unit === unit ? String(value.value) : `${value.value}${value.unit}`;
150
+ if (value === "auto") return "auto";
151
+ if (typeof value === "object" && value !== null && "var" in value) return varName ?? "";
152
+ return "";
153
+ }
@@ -0,0 +1,27 @@
1
+ import { sanitizeHref, type LayoutNode } from "../../../../core/index.ts";
2
+
3
+ /**
4
+ * W-273: a Button without a usable URL renders as a plain `<button>` that does nothing on the
5
+ * live page. That's right when a popup's click trigger or a script targets it, usually by its
6
+ * HTML id or a class, so only a button with neither gets the note.
7
+ */
8
+ export function buttonUrlNote(node: LayoutNode): string | undefined {
9
+ if (node.type === "link") return linkUrlNote(node);
10
+ if (node.type !== "button") return undefined;
11
+ const props = node.props as { href?: string };
12
+ const href = props.href?.trim();
13
+ if (href && sanitizeHref(href)) return undefined;
14
+ if (node.htmlId || (node.classes?.length ?? 0) > 0) return undefined;
15
+ return href
16
+ ? "This URL isn't allowed, so the button does nothing on the live page. Use a full URL or a path such as /contact."
17
+ : "No URL: this button does nothing on the live page. Add a URL, or give it an HTML id (Advanced) for a popup's click trigger.";
18
+ }
19
+
20
+ /** W-276: a Link without a usable URL renders `href="#"`, which only jumps to the top. */
21
+ function linkUrlNote(node: LayoutNode): string | undefined {
22
+ const href = (node.props as { href?: string }).href?.trim();
23
+ if (href && sanitizeHref(href)) return undefined;
24
+ return href
25
+ ? "This URL isn't allowed, so the link only jumps to the top of the page. Use a full URL or a path such as /contact."
26
+ : "No URL: this link only jumps to the top of the page. Add a URL.";
27
+ }
@@ -0,0 +1,53 @@
1
+ import { cleanClassName, type DesignSystem } from "../../../../core/index.ts";
2
+
3
+ type StyleClass = NonNullable<DesignSystem["classes"]>[number];
4
+
5
+ export type ClassOption = { kind: "class"; cls: StyleClass } | { kind: "create"; name: string };
6
+
7
+ /** Site-wide class cap (design schema) and per-element cap (layout schema). */
8
+ export const MAX_SITE_CLASSES = 100;
9
+ export const MAX_ELEMENT_CLASSES = 20;
10
+
11
+ const rank = (cls: StyleClass, q: string) => {
12
+ const name = cls.name.toLowerCase();
13
+ if (name === q) return 0;
14
+ if (name.startsWith(q) || cls.id.startsWith(q)) return 1;
15
+ return 2;
16
+ };
17
+
18
+ /**
19
+ * Suggestions for the class chip input (W-087): classes not yet on the element whose name or id
20
+ * contains the query, exact and prefix matches first, then "Create class" when no class has
21
+ * that exact name.
22
+ */
23
+ export function classOptions(
24
+ catalog: readonly StyleClass[],
25
+ applied: readonly string[],
26
+ query: string,
27
+ ): ClassOption[] {
28
+ const q = query.trim().toLowerCase();
29
+ const available = catalog.filter((cls) => !applied.includes(cls.id));
30
+ const found = q
31
+ ? available
32
+ .filter((cls) => cls.name.toLowerCase().includes(q) || cls.id.includes(q))
33
+ .map((cls, index) => ({ cls, index, rank: rank(cls, q) }))
34
+ .toSorted((a, b) => a.rank - b.rank || a.index - b.index)
35
+ .map(({ cls }) => cls)
36
+ : available;
37
+ const options: ClassOption[] = found.map((cls) => ({ kind: "class", cls }));
38
+ // The new name as it will be saved: at most 60 characters, no control characters (W-220).
39
+ const name = cleanClassName(query);
40
+ const taken = catalog.some((cls) => cls.name.toLowerCase() === name.toLowerCase());
41
+ if (name && !taken) options.push({ kind: "create", name });
42
+ return options;
43
+ }
44
+
45
+ /** The applied class whose name is exactly `query`, ignoring case, if any. */
46
+ export const appliedByName = (
47
+ catalog: readonly StyleClass[],
48
+ applied: readonly string[],
49
+ query: string,
50
+ ) => {
51
+ const q = query.trim().toLowerCase();
52
+ return catalog.find((cls) => applied.includes(cls.id) && cls.name.toLowerCase() === q);
53
+ };
@@ -0,0 +1,38 @@
1
+ import {
2
+ findNode,
3
+ isFormNode,
4
+ parentOf,
5
+ type Layout,
6
+ type LayoutNode,
7
+ } from "../../../../core/index.ts";
8
+
9
+ const fieldOf = (node: LayoutNode): string => {
10
+ const value = (node.props as Record<string, unknown> | undefined)?.field;
11
+ return typeof value === "string" ? value.trim() : "";
12
+ };
13
+
14
+ /**
15
+ * W-194: true when another control in the same form submits under this node's field name, so
16
+ * one value would overwrite the other. Controls outside any form are not checked.
17
+ */
18
+ export function duplicateFieldName(layout: Layout, nodeId: string): boolean {
19
+ const self = findNode(layout, nodeId);
20
+ const name = self ? fieldOf(self) : "";
21
+ if (!name) return false;
22
+ let current = parentOf(layout, nodeId);
23
+ while (current) {
24
+ const form = findNode(layout, current);
25
+ if (form && isFormNode(form)) {
26
+ let count = 0;
27
+ const walk = (node: LayoutNode) => {
28
+ if (node.id !== form.id && fieldOf(node) === name) count += 1;
29
+ if ("children" in node && Array.isArray(node.children))
30
+ for (const child of node.children) walk(child);
31
+ };
32
+ walk(form);
33
+ return count > 1;
34
+ }
35
+ current = parentOf(layout, current);
36
+ }
37
+ return false;
38
+ }