@perkyzz/emvb 0.0.0-stage → 0.1.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 (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -2
  3. package/package.json +72 -4
  4. package/src/admin/access/RequireEditor.tsx +39 -0
  5. package/src/admin/access/role.ts +36 -0
  6. package/src/admin/access/sidebar-shim.ts +108 -0
  7. package/src/admin/api.ts +48 -0
  8. package/src/admin/content-api.ts +147 -0
  9. package/src/admin/editor/Editor.tsx +457 -0
  10. package/src/admin/editor/EditorOverlay.tsx +54 -0
  11. package/src/admin/editor/ShortcutsDialog.tsx +30 -0
  12. package/src/admin/editor/SmallScreenNotice.tsx +20 -0
  13. package/src/admin/editor/TopBar.tsx +179 -0
  14. package/src/admin/editor/canvas/CanvasFrame.tsx +355 -0
  15. package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
  16. package/src/admin/editor/canvas/DeviceBar.tsx +50 -0
  17. package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
  18. package/src/admin/editor/canvas/SelectionOverlay.tsx +185 -0
  19. package/src/admin/editor/canvas/canvas-events.ts +232 -0
  20. package/src/admin/editor/canvas/state-preview.ts +19 -0
  21. package/src/admin/editor/canvas/tab-reveal.ts +35 -0
  22. package/src/admin/editor/canvas/vnode-react.tsx +49 -0
  23. package/src/admin/editor/clipboard-store.ts +44 -0
  24. package/src/admin/editor/dialogs.tsx +125 -0
  25. package/src/admin/editor/dnd/drag-stash.ts +27 -0
  26. package/src/admin/editor/dnd/drop-target.ts +101 -0
  27. package/src/admin/editor/dnd/new-element.ts +32 -0
  28. package/src/admin/editor/editor-css.ts +539 -0
  29. package/src/admin/editor/exit.ts +27 -0
  30. package/src/admin/editor/panels/AddPanel.tsx +142 -0
  31. package/src/admin/editor/panels/ClassesSection.tsx +334 -0
  32. package/src/admin/editor/panels/ColorControl.tsx +230 -0
  33. package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
  34. package/src/admin/editor/panels/ElementPanel.tsx +629 -0
  35. package/src/admin/editor/panels/LayersPanel.tsx +336 -0
  36. package/src/admin/editor/panels/LeftPanel.tsx +77 -0
  37. package/src/admin/editor/panels/PageSettings.tsx +126 -0
  38. package/src/admin/editor/panels/SettingsPanel.tsx +101 -0
  39. package/src/admin/editor/panels/SiteStylesDrawer.tsx +392 -0
  40. package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
  41. package/src/admin/editor/panels/TriggersEditor.tsx +197 -0
  42. package/src/admin/editor/panels/VariableSection.tsx +492 -0
  43. package/src/admin/editor/panels/settings/BackgroundControls.tsx +320 -0
  44. package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
  45. package/src/admin/editor/panels/settings/ClassChipInput.tsx +349 -0
  46. package/src/admin/editor/panels/settings/DraftTextField.tsx +201 -0
  47. package/src/admin/editor/panels/settings/EffectsControls.tsx +347 -0
  48. package/src/admin/editor/panels/settings/FieldBindControl.tsx +83 -0
  49. package/src/admin/editor/panels/settings/FieldControl.tsx +150 -0
  50. package/src/admin/editor/panels/settings/FormBindControl.tsx +97 -0
  51. package/src/admin/editor/panels/settings/IconPicker.tsx +109 -0
  52. package/src/admin/editor/panels/settings/LengthRow.tsx +181 -0
  53. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +124 -0
  54. package/src/admin/editor/panels/settings/MediaPicker.tsx +218 -0
  55. package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
  56. package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
  57. package/src/admin/editor/panels/settings/StyleRow.tsx +480 -0
  58. package/src/admin/editor/panels/settings/VariableBinding.tsx +167 -0
  59. package/src/admin/editor/panels/settings/class-suggest.ts +51 -0
  60. package/src/admin/editor/panels/settings/length-units.ts +83 -0
  61. package/src/admin/editor/panels/settings/node-props.ts +11 -0
  62. package/src/admin/editor/panels/settings/style-sections.ts +291 -0
  63. package/src/admin/editor/panels/site-list.tsx +206 -0
  64. package/src/admin/editor/panels/variable-values.ts +73 -0
  65. package/src/admin/editor/section-templates.ts +50 -0
  66. package/src/admin/editor/shortcuts.ts +20 -0
  67. package/src/admin/editor/store.ts +212 -0
  68. package/src/admin/editor/useClipboardActions.ts +136 -0
  69. package/src/admin/editor/useEditorCommands.ts +142 -0
  70. package/src/admin/editor/useEditorData.ts +167 -0
  71. package/src/admin/editor/useEditorShortcuts.ts +148 -0
  72. package/src/admin/editor/useMediaQuery.ts +20 -0
  73. package/src/admin/editor/useNodeActions.ts +122 -0
  74. package/src/admin/editor/useSave.ts +185 -0
  75. package/src/admin/forms-api.ts +73 -0
  76. package/src/admin/index.tsx +20 -0
  77. package/src/admin/media-api.ts +68 -0
  78. package/src/admin/pages/CreateDialog.tsx +150 -0
  79. package/src/admin/pages/EditorPage.tsx +57 -0
  80. package/src/admin/pages/NewPageDialog.tsx +83 -0
  81. package/src/admin/pages/NewThemePartDialog.tsx +61 -0
  82. package/src/admin/pages/PageList.tsx +71 -0
  83. package/src/admin/pages/PagesPage.tsx +25 -0
  84. package/src/admin/pages/SetupShell.tsx +90 -0
  85. package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
  86. package/src/admin/pages/ThemePartList.tsx +202 -0
  87. package/src/admin/pages/list-kit.tsx +75 -0
  88. package/src/admin/setup/plan.ts +150 -0
  89. package/src/admin/setup/run.ts +61 -0
  90. package/src/admin/setup/spec.ts +100 -0
  91. package/src/admin/setup/useSetupStatus.ts +62 -0
  92. package/src/admin/theme-api.ts +186 -0
  93. package/src/admin/ui.ts +231 -0
  94. package/src/admin/widgets/LayoutField.tsx +67 -0
  95. package/src/astro/EmVBPage.astro +37 -0
  96. package/src/astro/env.d.ts +5 -0
  97. package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
  98. package/src/astro/forms/index.ts +1 -0
  99. package/src/astro/forms-definitions.ts +104 -0
  100. package/src/astro/index.ts +15 -0
  101. package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
  102. package/src/astro/popups/index.ts +1 -0
  103. package/src/astro/popups/runtime.ts +246 -0
  104. package/src/astro/render.ts +74 -0
  105. package/src/astro/resolve-theme.ts +371 -0
  106. package/src/astro/resolve.ts +65 -0
  107. package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
  108. package/src/astro/tabs/index.ts +1 -0
  109. package/src/astro/tabs/runtime.ts +111 -0
  110. package/src/astro/theme-context.ts +81 -0
  111. package/src/astro/theme-posts.ts +84 -0
  112. package/src/constants.ts +12 -0
  113. package/src/core/arrange.ts +360 -0
  114. package/src/core/clipboard.ts +422 -0
  115. package/src/core/css/generate.ts +219 -0
  116. package/src/core/css/scope.ts +88 -0
  117. package/src/core/design/cascade.ts +28 -0
  118. package/src/core/design/classes.ts +138 -0
  119. package/src/core/design/devices.ts +56 -0
  120. package/src/core/design/states.ts +49 -0
  121. package/src/core/design/transfer.ts +33 -0
  122. package/src/core/design/variables.ts +319 -0
  123. package/src/core/elements/definition.ts +12 -0
  124. package/src/core/elements/dynamic-elements.ts +173 -0
  125. package/src/core/elements/form-elements.ts +204 -0
  126. package/src/core/elements/index.ts +742 -0
  127. package/src/core/elements/plain-text.ts +32 -0
  128. package/src/core/forms/binding.ts +61 -0
  129. package/src/core/forms/definition.ts +34 -0
  130. package/src/core/forms/success-layout.ts +63 -0
  131. package/src/core/icons/catalog.ts +273 -0
  132. package/src/core/index.ts +316 -0
  133. package/src/core/limits.ts +7 -0
  134. package/src/core/migrate/index.ts +76 -0
  135. package/src/core/render/context.ts +14 -0
  136. package/src/core/render/dynamic-post.ts +147 -0
  137. package/src/core/render/extras.ts +79 -0
  138. package/src/core/render/form.ts +137 -0
  139. package/src/core/render/index.ts +274 -0
  140. package/src/core/render/loop.ts +36 -0
  141. package/src/core/render/section.ts +33 -0
  142. package/src/core/render/tabs.ts +72 -0
  143. package/src/core/render/vnode.ts +87 -0
  144. package/src/core/sanitize/css.ts +591 -0
  145. package/src/core/sanitize/embed-url.ts +103 -0
  146. package/src/core/sanitize/escape.ts +10 -0
  147. package/src/core/sanitize/href.ts +6 -0
  148. package/src/core/sanitize/media-url.ts +7 -0
  149. package/src/core/sanitize/svg.ts +293 -0
  150. package/src/core/sanitize/url.ts +31 -0
  151. package/src/core/schema/descriptors.ts +33 -0
  152. package/src/core/schema/design.ts +88 -0
  153. package/src/core/schema/layout.ts +817 -0
  154. package/src/core/schema/state-names.ts +4 -0
  155. package/src/core/schema/style.ts +257 -0
  156. package/src/core/stats.ts +18 -0
  157. package/src/core/tabs/presence.ts +16 -0
  158. package/src/core/theme/conditions.ts +321 -0
  159. package/src/core/theme/dynamic.ts +133 -0
  160. package/src/core/theme/parse-doc.ts +36 -0
  161. package/src/core/theme/part-types.ts +79 -0
  162. package/src/core/theme/popup-markup.ts +39 -0
  163. package/src/core/theme/popup-rules.ts +61 -0
  164. package/src/core/theme/triggers.ts +104 -0
  165. package/src/core/tree-ops.ts +139 -0
  166. package/src/core/validate.ts +185 -0
  167. package/src/index.ts +18 -0
  168. package/src/server/design-routes.ts +125 -0
  169. package/src/server/hooks.ts +98 -0
  170. package/src/server/plugin.ts +39 -0
@@ -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,138 @@
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
+ /** Duplicate a design class with a new id/name. Pure (W-071). */
84
+ export function duplicateClass(
85
+ design: DesignSystem,
86
+ classId: string,
87
+ randomSuffix?: string,
88
+ ): DesignSystem {
89
+ const classes = design.classes ?? [];
90
+ const source = classes.find((c) => c.id === classId);
91
+ if (!source) return design;
92
+ const baseName = `${source.name} copy`;
93
+ const base = slugify(baseName).slice(0, 34) || "class";
94
+ const taken = new Set(classes.map((c) => c.id));
95
+ let id = randomSuffix ? `${base}-${randomSuffix}`.slice(0, 40) : base;
96
+ for (let n = 2; taken.has(id); n++) id = `${base}-${n}`.slice(0, 40);
97
+ const style = { ...source.style } as StyleProps;
98
+ const copy = source.states
99
+ ? { id, name: baseName, style, states: structuredClone(source.states) }
100
+ : { id, name: baseName, style };
101
+ return { ...design, classes: [...classes, copy] };
102
+ }
103
+
104
+ /** Rename a design class; the id (and so its `.emvb-k-<id>` selector) stays. Pure (W-087). */
105
+ export function renameClass(design: DesignSystem, classId: string, name: string): DesignSystem {
106
+ const trimmed = name.trim();
107
+ const classes = design.classes ?? [];
108
+ if (!trimmed || !classes.some((c) => c.id === classId)) return design;
109
+ return {
110
+ ...design,
111
+ classes: classes.map((c) => (c.id === classId ? Object.assign({}, c, { name: trimmed }) : c)),
112
+ };
113
+ }
114
+
115
+ /** Merge `patch` into a class's style; `undefined` values clear their property. Pure (W-087). */
116
+ export function patchClassStyle(
117
+ design: DesignSystem,
118
+ classId: string,
119
+ patch: Partial<StyleProps>,
120
+ ): DesignSystem {
121
+ const classes = design.classes ?? [];
122
+ if (!classes.some((c) => c.id === classId)) return design;
123
+ return {
124
+ ...design,
125
+ classes: classes.map((c) => {
126
+ if (c.id !== classId) return c;
127
+ const style: Record<string, unknown> = { ...c.style, ...patch };
128
+ for (const [key, value] of Object.entries(patch)) if (value === undefined) delete style[key];
129
+ return Object.assign({}, c, { style: style as StyleProps });
130
+ }),
131
+ };
132
+ }
133
+
134
+ /** Swap one applied class id for another in place, keeping the cascade order. Pure (W-087). */
135
+ export function replaceClassId(ids: readonly string[], from: string, to: string): string[] {
136
+ if (ids.includes(to)) return removeClassId(ids, from);
137
+ return ids.map((id) => (id === from ? to : id));
138
+ }
@@ -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
+ }
@@ -0,0 +1,33 @@
1
+ import { DesignSystem as DesignSystemSchema, DESIGN_SCHEMA_VERSION } from "../schema/design.ts";
2
+ import type { DesignSystem } from "../schema/design.ts";
3
+ import { DESIGN_MIGRATIONS, upgradeLayout } from "../migrate/index.ts";
4
+
5
+ /** The design document as JSON. This file is the backup: there is no revision history. */
6
+ export function designToJson(design: DesignSystem): string {
7
+ return JSON.stringify(design, null, 2);
8
+ }
9
+
10
+ export type DesignImport = { ok: true; design: DesignSystem } | { ok: false; message: string };
11
+
12
+ /** Parse an exported design. Older schema versions are upgraded. A newer file is refused. */
13
+ export function designFromJson(text: string): DesignImport {
14
+ let parsed: unknown;
15
+ try {
16
+ parsed = JSON.parse(text);
17
+ } catch {
18
+ return { ok: false, message: "That file is not JSON." };
19
+ }
20
+ const upgraded = upgradeLayout(parsed, DESIGN_MIGRATIONS, DESIGN_SCHEMA_VERSION);
21
+ if (!upgraded.ok) {
22
+ return {
23
+ ok: false,
24
+ message:
25
+ upgraded.reason === "newer-version"
26
+ ? "That design was saved by a newer EmVB."
27
+ : "That file is not an EmVB design document.",
28
+ };
29
+ }
30
+ const result = DesignSystemSchema.safeParse(upgraded.doc);
31
+ if (!result.success) return { ok: false, message: "That file is not an EmVB design document." };
32
+ return { ok: true, design: result.data };
33
+ }