@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.
- package/LICENSE +21 -0
- package/README.md +59 -2
- package/THIRD_PARTY_NOTICES.md +32 -0
- package/licenses/fontawesome-free.LICENSE +165 -0
- package/licenses/lucide-static.LICENSE +43 -0
- package/licenses/remixicon.LICENSE +201 -0
- package/licenses/tabler-icons.LICENSE +21 -0
- package/package.json +76 -4
- package/src/admin/access/RequireEditor.tsx +39 -0
- package/src/admin/access/role.ts +36 -0
- package/src/admin/access/sidebar-shim.ts +108 -0
- package/src/admin/api.ts +75 -0
- package/src/admin/content-api.ts +210 -0
- package/src/admin/editor/Editor.tsx +532 -0
- package/src/admin/editor/EditorOverlay.tsx +66 -0
- package/src/admin/editor/ShortcutsDialog.tsx +30 -0
- package/src/admin/editor/SmallScreenNotice.tsx +21 -0
- package/src/admin/editor/TopBar.tsx +243 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +536 -0
- package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
- package/src/admin/editor/canvas/DeviceBar.tsx +91 -0
- package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
- package/src/admin/editor/canvas/SelectionOverlay.tsx +239 -0
- package/src/admin/editor/canvas/canvas-events.ts +245 -0
- package/src/admin/editor/canvas/state-preview.ts +19 -0
- package/src/admin/editor/canvas/tab-reveal.ts +101 -0
- package/src/admin/editor/canvas/vnode-react.tsx +69 -0
- package/src/admin/editor/clipboard-store.ts +44 -0
- package/src/admin/editor/dialogs.tsx +164 -0
- package/src/admin/editor/dnd/drag-stash.ts +27 -0
- package/src/admin/editor/dnd/drop-target.ts +148 -0
- package/src/admin/editor/dnd/new-element.ts +107 -0
- package/src/admin/editor/editor-css.ts +625 -0
- package/src/admin/editor/exit.ts +27 -0
- package/src/admin/editor/host.ts +30 -0
- package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
- package/src/admin/editor/icons/library.ts +243 -0
- package/src/admin/editor/icons/set-format.ts +29 -0
- package/src/admin/editor/icons/sets/fontawesome.json +1 -0
- package/src/admin/editor/icons/sets/lucide.json +1 -0
- package/src/admin/editor/icons/sets/remix.json +1 -0
- package/src/admin/editor/icons/sets/tabler.json +1 -0
- package/src/admin/editor/panels/AddPanel.tsx +146 -0
- package/src/admin/editor/panels/ClassesSection.tsx +372 -0
- package/src/admin/editor/panels/ColorControl.tsx +477 -0
- package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
- package/src/admin/editor/panels/ElementPanel.tsx +773 -0
- package/src/admin/editor/panels/LayersPanel.tsx +573 -0
- package/src/admin/editor/panels/LeftPanel.tsx +81 -0
- package/src/admin/editor/panels/PageSettings.tsx +255 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +113 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +548 -0
- package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +214 -0
- package/src/admin/editor/panels/VariableSection.tsx +530 -0
- package/src/admin/editor/panels/color-space.ts +195 -0
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +418 -0
- package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
- package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
- package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
- package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +451 -0
- package/src/admin/editor/panels/settings/DraftTextField.tsx +242 -0
- package/src/admin/editor/panels/settings/EffectsControls.tsx +354 -0
- package/src/admin/editor/panels/settings/FieldBindControl.tsx +119 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +270 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +106 -0
- package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +170 -0
- package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
- package/src/admin/editor/panels/settings/LengthRow.tsx +217 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +130 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +220 -0
- package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
- package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +563 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +171 -0
- package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
- package/src/admin/editor/panels/settings/background-mode.ts +112 -0
- package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
- package/src/admin/editor/panels/settings/box-sides.ts +153 -0
- package/src/admin/editor/panels/settings/button-url.ts +27 -0
- package/src/admin/editor/panels/settings/class-suggest.ts +53 -0
- package/src/admin/editor/panels/settings/form-names.ts +38 -0
- package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
- package/src/admin/editor/panels/settings/image-alt.ts +25 -0
- package/src/admin/editor/panels/settings/length-units.ts +116 -0
- package/src/admin/editor/panels/settings/loop-context.ts +26 -0
- package/src/admin/editor/panels/settings/node-props.ts +11 -0
- package/src/admin/editor/panels/settings/style-sections.ts +394 -0
- package/src/admin/editor/panels/settings/svg-images.ts +14 -0
- package/src/admin/editor/panels/site-list.tsx +209 -0
- package/src/admin/editor/panels/variable-values.ts +73 -0
- package/src/admin/editor/restore-deleted.ts +80 -0
- package/src/admin/editor/section-templates.ts +50 -0
- package/src/admin/editor/shortcuts.ts +20 -0
- package/src/admin/editor/store.ts +240 -0
- package/src/admin/editor/useClipboardActions.ts +136 -0
- package/src/admin/editor/useEditorCommands.ts +203 -0
- package/src/admin/editor/useEditorData.ts +193 -0
- package/src/admin/editor/useEditorShortcuts.ts +180 -0
- package/src/admin/editor/useMediaQuery.ts +20 -0
- package/src/admin/editor/useNodeActions.ts +184 -0
- package/src/admin/editor/useSave.ts +242 -0
- package/src/admin/entry-status.ts +19 -0
- package/src/admin/forms-api.ts +73 -0
- package/src/admin/icon-uploads-api.ts +44 -0
- package/src/admin/index.tsx +20 -0
- package/src/admin/media-api.ts +68 -0
- package/src/admin/pages/CreateDialog.tsx +150 -0
- package/src/admin/pages/EditorPage.tsx +57 -0
- package/src/admin/pages/NewPageDialog.tsx +83 -0
- package/src/admin/pages/NewThemePartDialog.tsx +61 -0
- package/src/admin/pages/PageList.tsx +91 -0
- package/src/admin/pages/PagesPage.tsx +25 -0
- package/src/admin/pages/SetupShell.tsx +90 -0
- package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
- package/src/admin/pages/ThemePartList.tsx +221 -0
- package/src/admin/pages/list-kit.tsx +125 -0
- package/src/admin/setup/plan.ts +150 -0
- package/src/admin/setup/run.ts +61 -0
- package/src/admin/setup/spec.ts +107 -0
- package/src/admin/setup/useSetupStatus.ts +62 -0
- package/src/admin/theme-api.ts +233 -0
- package/src/admin/ui.ts +244 -0
- package/src/admin/widgets/LayoutField.tsx +67 -0
- package/src/astro/EmVBPage.astro +63 -0
- package/src/astro/env.d.ts +5 -0
- package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
- package/src/astro/floats/index.ts +1 -0
- package/src/astro/floats/runtime.ts +78 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
- package/src/astro/forms/index.ts +1 -0
- package/src/astro/forms-definitions.ts +126 -0
- package/src/astro/index.ts +16 -0
- package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
- package/src/astro/menu/index.ts +1 -0
- package/src/astro/menu/runtime.ts +37 -0
- package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
- package/src/astro/popups/index.ts +1 -0
- package/src/astro/popups/runtime.ts +296 -0
- package/src/astro/render.ts +108 -0
- package/src/astro/resolve-theme.ts +515 -0
- package/src/astro/resolve.ts +65 -0
- package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
- package/src/astro/tabs/index.ts +1 -0
- package/src/astro/tabs/runtime.ts +116 -0
- package/src/astro/theme-context.ts +89 -0
- package/src/astro/theme-posts.ts +84 -0
- package/src/constants.ts +12 -0
- package/src/core/arrange.ts +448 -0
- package/src/core/clipboard.ts +504 -0
- package/src/core/css/generate.ts +236 -0
- package/src/core/css/scope.ts +88 -0
- package/src/core/design/cascade.ts +28 -0
- package/src/core/design/classes.ts +224 -0
- package/src/core/design/devices.ts +56 -0
- package/src/core/design/states.ts +49 -0
- package/src/core/design/transfer.ts +132 -0
- package/src/core/design/variables.ts +359 -0
- package/src/core/elements/definition.ts +12 -0
- package/src/core/elements/dynamic-elements.ts +225 -0
- package/src/core/elements/field-id.ts +6 -0
- package/src/core/elements/form-elements.ts +219 -0
- package/src/core/elements/index.ts +960 -0
- package/src/core/elements/menu.ts +190 -0
- package/src/core/elements/plain-text.ts +32 -0
- package/src/core/forms/binding.ts +61 -0
- package/src/core/forms/definition.ts +45 -0
- package/src/core/forms/success-layout.ts +63 -0
- package/src/core/icons/catalog.ts +273 -0
- package/src/core/index.ts +380 -0
- package/src/core/limits.ts +9 -0
- package/src/core/migrate/index.ts +126 -0
- package/src/core/nesting.ts +51 -0
- package/src/core/render/context.ts +21 -0
- package/src/core/render/dynamic-post.ts +175 -0
- package/src/core/render/extras.ts +98 -0
- package/src/core/render/form.ts +235 -0
- package/src/core/render/index.ts +372 -0
- package/src/core/render/loop.ts +43 -0
- package/src/core/render/pagination.ts +76 -0
- package/src/core/render/section.ts +37 -0
- package/src/core/render/tabs.ts +76 -0
- package/src/core/render/vnode.ts +87 -0
- package/src/core/sanitize/css.ts +771 -0
- package/src/core/sanitize/embed-url.ts +160 -0
- package/src/core/sanitize/escape.ts +10 -0
- package/src/core/sanitize/href.ts +6 -0
- package/src/core/sanitize/media-url.ts +7 -0
- package/src/core/sanitize/svg-upload.ts +203 -0
- package/src/core/sanitize/svg.ts +374 -0
- package/src/core/sanitize/url.ts +31 -0
- package/src/core/schema/descriptors.ts +37 -0
- package/src/core/schema/design.ts +93 -0
- package/src/core/schema/layout.ts +1008 -0
- package/src/core/schema/state-names.ts +4 -0
- package/src/core/schema/style.ts +336 -0
- package/src/core/stats.ts +18 -0
- package/src/core/tabs/presence.ts +25 -0
- package/src/core/theme/conditions.ts +324 -0
- package/src/core/theme/dynamic.ts +143 -0
- package/src/core/theme/float.ts +113 -0
- package/src/core/theme/pagination.ts +97 -0
- package/src/core/theme/parse-doc.ts +36 -0
- package/src/core/theme/part-types.ts +81 -0
- package/src/core/theme/popup-markup.ts +41 -0
- package/src/core/theme/popup-rules.ts +62 -0
- package/src/core/theme/triggers.ts +104 -0
- package/src/core/tree-ops.ts +139 -0
- package/src/core/validate.ts +185 -0
- package/src/index.ts +18 -0
- package/src/server/design-routes.ts +125 -0
- package/src/server/hooks.ts +118 -0
- package/src/server/icon-routes.ts +79 -0
- package/src/server/plugin.ts +43 -0
|
@@ -0,0 +1,132 @@
|
|
|
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
|
+
import type { Layout } from "../schema/layout.ts";
|
|
5
|
+
import { findClassUsages } from "./classes.ts";
|
|
6
|
+
import { findVariableUsages } from "./variables.ts";
|
|
7
|
+
|
|
8
|
+
/** The design document as JSON. This file is the backup: there is no revision history. */
|
|
9
|
+
export function designToJson(design: DesignSystem): string {
|
|
10
|
+
return JSON.stringify(design, null, 2);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A name Import changed so names stay unique (W-283). */
|
|
14
|
+
export type ImportRename = { from: string; to: string };
|
|
15
|
+
|
|
16
|
+
export type DesignImport =
|
|
17
|
+
| { ok: true; design: DesignSystem; renamed: ImportRename[] }
|
|
18
|
+
| { ok: false; message: string };
|
|
19
|
+
|
|
20
|
+
/** Names are capped at 60 characters; "Card 2" keeps the number inside the cap. */
|
|
21
|
+
function freeName(name: string, taken: Set<string>): string {
|
|
22
|
+
for (let n = 2; ; n++) {
|
|
23
|
+
const suffix = ` ${n}`;
|
|
24
|
+
const next = `${name.slice(0, 60 - suffix.length).trim()}${suffix}`;
|
|
25
|
+
if (!taken.has(next.toLowerCase())) return next;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function dedupe<T extends { name: string }>(items: T[], renamed: ImportRename[]): T[] {
|
|
30
|
+
const taken = new Set(items.map((item) => item.name.toLowerCase()));
|
|
31
|
+
const seen = new Set<string>();
|
|
32
|
+
return items.map((item) => {
|
|
33
|
+
const key = item.name.toLowerCase();
|
|
34
|
+
if (!seen.has(key)) {
|
|
35
|
+
seen.add(key);
|
|
36
|
+
return item;
|
|
37
|
+
}
|
|
38
|
+
const to = freeName(item.name, taken);
|
|
39
|
+
taken.add(to.toLowerCase());
|
|
40
|
+
seen.add(to.toLowerCase());
|
|
41
|
+
renamed.push({ from: item.name, to });
|
|
42
|
+
return { ...item, name: to };
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* W-283: the editor keeps class names, and variable names within a kind, unique (W-277, W-278).
|
|
48
|
+
* A hand-edited file can still repeat one, so Import renames the later ones ("Card 2") and the
|
|
49
|
+
* confirm lists them. Ids, and so every reference, stay as they are.
|
|
50
|
+
*/
|
|
51
|
+
export function uniqueImportNames(design: DesignSystem): {
|
|
52
|
+
design: DesignSystem;
|
|
53
|
+
renamed: ImportRename[];
|
|
54
|
+
} {
|
|
55
|
+
const renamed: ImportRename[] = [];
|
|
56
|
+
const classes = design.classes ? dedupe(design.classes, renamed) : design.classes;
|
|
57
|
+
const variables = { ...design.variables };
|
|
58
|
+
for (const [group] of VARIABLE_GROUPS) {
|
|
59
|
+
const list = design.variables[group];
|
|
60
|
+
if (list) Object.assign(variables, { [group]: dedupe<{ name: string }>(list, renamed) });
|
|
61
|
+
}
|
|
62
|
+
if (renamed.length === 0) return { design, renamed };
|
|
63
|
+
return { design: { ...design, variables, ...(classes ? { classes } : {}) }, renamed };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Parse an exported design. Older schema versions are upgraded. A newer file is refused. */
|
|
67
|
+
export function designFromJson(text: string): DesignImport {
|
|
68
|
+
let parsed: unknown;
|
|
69
|
+
try {
|
|
70
|
+
parsed = JSON.parse(text);
|
|
71
|
+
} catch {
|
|
72
|
+
return { ok: false, message: "That file is not JSON." };
|
|
73
|
+
}
|
|
74
|
+
const upgraded = upgradeLayout(parsed, DESIGN_MIGRATIONS, DESIGN_SCHEMA_VERSION);
|
|
75
|
+
if (!upgraded.ok) {
|
|
76
|
+
return {
|
|
77
|
+
ok: false,
|
|
78
|
+
message:
|
|
79
|
+
upgraded.reason === "newer-version"
|
|
80
|
+
? "That design was saved by a newer EmVB."
|
|
81
|
+
: "That file is not an EmVB design document.",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const result = DesignSystemSchema.safeParse(upgraded.doc);
|
|
85
|
+
if (!result.success) return { ok: false, message: "That file is not an EmVB design document." };
|
|
86
|
+
return { ok: true, ...uniqueImportNames(result.data) };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The largest design file Import reads (W-214); the saved design is capped far lower. */
|
|
90
|
+
export const MAX_DESIGN_FILE_BYTES = 1024 * 1024;
|
|
91
|
+
|
|
92
|
+
export type ImportLosses = {
|
|
93
|
+
/** Classes and variables in the current design that the file doesn't have. */
|
|
94
|
+
classes: number;
|
|
95
|
+
variables: number;
|
|
96
|
+
/** Of those, how many this page uses (its elements would lose that styling). */
|
|
97
|
+
usedOnPage: number;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const VARIABLE_GROUPS = [
|
|
101
|
+
["colors", "color"],
|
|
102
|
+
["fonts", "font"],
|
|
103
|
+
["fontSizes", "fontSize"],
|
|
104
|
+
["spacings", "spacing"],
|
|
105
|
+
] as const;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* W-214: what Import would drop. It replaces the whole design and there is no revision history,
|
|
109
|
+
* so the editor confirms first and names the cost.
|
|
110
|
+
*/
|
|
111
|
+
export function importLosses(
|
|
112
|
+
current: DesignSystem,
|
|
113
|
+
incoming: DesignSystem,
|
|
114
|
+
layout: Layout | null,
|
|
115
|
+
): ImportLosses {
|
|
116
|
+
const keptClasses = new Set((incoming.classes ?? []).map((c) => c.id));
|
|
117
|
+
const lostClasses = (current.classes ?? []).filter((c) => !keptClasses.has(c.id));
|
|
118
|
+
let variables = 0;
|
|
119
|
+
let usedOnPage = 0;
|
|
120
|
+
for (const [group, kind] of VARIABLE_GROUPS) {
|
|
121
|
+
const kept = new Set((incoming.variables[group] ?? []).map((v) => v.id));
|
|
122
|
+
for (const variable of current.variables[group] ?? []) {
|
|
123
|
+
if (kept.has(variable.id)) continue;
|
|
124
|
+
variables += 1;
|
|
125
|
+
if (layout && findVariableUsages(layout, variable.id, kind).length > 0) usedOnPage += 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (layout) {
|
|
129
|
+
usedOnPage += lostClasses.filter((c) => findClassUsages(layout, c.id).length > 0).length;
|
|
130
|
+
}
|
|
131
|
+
return { classes: lostClasses.length, variables, usedOnPage };
|
|
132
|
+
}
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import type { DesignSystem } from "../schema/design.ts";
|
|
2
|
+
import type { Layout, LayoutNode } from "../schema/layout.ts";
|
|
3
|
+
import { cleanClassName } from "./classes.ts";
|
|
4
|
+
import { STYLE_STATES } from "../schema/state-names.ts";
|
|
5
|
+
import type { DeviceStyles, StyleProps, StyleStates } from "../schema/style.ts";
|
|
6
|
+
import { nodeChildren, slugify, updateNode } from "../tree-ops.ts";
|
|
7
|
+
|
|
8
|
+
export type VariableKind = "color" | "font" | "fontSize" | "spacing";
|
|
9
|
+
|
|
10
|
+
/** Where a design variable is referenced from a layout (or, later, a class style). */
|
|
11
|
+
export type VariableUsage = {
|
|
12
|
+
nodeId: string;
|
|
13
|
+
prop: string;
|
|
14
|
+
/** Present when the ref lives on a design class style (W-030). */
|
|
15
|
+
classId?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type VariableRefLike = { var: string; from?: VariableKind };
|
|
19
|
+
|
|
20
|
+
const isVariableRef = (value: unknown): value is VariableRefLike =>
|
|
21
|
+
typeof value === "object" &&
|
|
22
|
+
value !== null &&
|
|
23
|
+
"var" in value &&
|
|
24
|
+
typeof (value as { var: unknown }).var === "string";
|
|
25
|
+
|
|
26
|
+
/** Colour refs omit `from` (legacy); length/font refs set it (W-028). */
|
|
27
|
+
export function refMatchesKind(ref: VariableRefLike, kind: VariableKind): boolean {
|
|
28
|
+
if (kind === "color") return ref.from === undefined || ref.from === "color";
|
|
29
|
+
return ref.from === kind;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const matches = (value: unknown, id: string, kind: VariableKind | undefined) =>
|
|
33
|
+
isVariableRef(value) && value.var === id && (kind === undefined || refMatchesKind(value, kind));
|
|
34
|
+
|
|
35
|
+
/** Nested colour refs: the box shadow (W-088), and gradient stops and the overlay (W-094). */
|
|
36
|
+
const shadowColorMatches = (style: StyleProps, id: string, kind: VariableKind | undefined) =>
|
|
37
|
+
matches(style.boxShadow?.color, id, kind);
|
|
38
|
+
|
|
39
|
+
function collectStyleUsages(
|
|
40
|
+
style: StyleProps | undefined,
|
|
41
|
+
id: string,
|
|
42
|
+
kind: VariableKind | undefined,
|
|
43
|
+
hit: (prop: string) => void,
|
|
44
|
+
): void {
|
|
45
|
+
if (!style) return;
|
|
46
|
+
for (const [prop, value] of Object.entries(style)) {
|
|
47
|
+
if (matches(value, id, kind)) hit(prop);
|
|
48
|
+
}
|
|
49
|
+
if (shadowColorMatches(style, id, kind)) hit("boxShadow.color");
|
|
50
|
+
if (matches(style.iconShadow?.color, id, kind)) hit("iconShadow.color");
|
|
51
|
+
style.gradient?.stops.forEach((stop, index) => {
|
|
52
|
+
if (matches(stop.color, id, kind)) hit(`gradient.stops.${index}`);
|
|
53
|
+
});
|
|
54
|
+
if (matches(style.overlay?.color, id, kind)) hit("overlay.color");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const RESPONSIVE = ["tablet", "mobile"] as const;
|
|
58
|
+
|
|
59
|
+
/** Style, state styles and device overrides. A nested usage is `<state|device>.<prop>`. */
|
|
60
|
+
function collectAllUsages(
|
|
61
|
+
owner: { style?: StyleProps; states?: StyleStates; devices?: DeviceStyles },
|
|
62
|
+
id: string,
|
|
63
|
+
kind: VariableKind | undefined,
|
|
64
|
+
hit: (prop: string) => void,
|
|
65
|
+
): void {
|
|
66
|
+
collectStyleUsages(owner.style, id, kind, hit);
|
|
67
|
+
for (const state of STYLE_STATES) {
|
|
68
|
+
collectStyleUsages(owner.states?.[state], id, kind, (prop) => hit(`${state}.${prop}`));
|
|
69
|
+
}
|
|
70
|
+
for (const device of RESPONSIVE) {
|
|
71
|
+
collectStyleUsages(owner.devices?.[device], id, kind, (prop) => hit(`${device}.${prop}`));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function walkNodes(node: LayoutNode, visit: (node: LayoutNode) => void): void {
|
|
76
|
+
visit(node);
|
|
77
|
+
for (const child of nodeChildren(node)) walkNodes(child, visit);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Find every style property on `layout` that references variable `id` (optionally of `kind`). */
|
|
81
|
+
export function findVariableUsages(
|
|
82
|
+
layout: Layout,
|
|
83
|
+
id: string,
|
|
84
|
+
kind?: VariableKind,
|
|
85
|
+
): VariableUsage[] {
|
|
86
|
+
const usages: VariableUsage[] = [];
|
|
87
|
+
walkNodes(layout.root, (node) => {
|
|
88
|
+
collectAllUsages(node, id, kind, (prop) => {
|
|
89
|
+
usages.push({ nodeId: node.id, prop });
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return usages;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Usages on design class styles (W-030). Safe no-op until `classes` exist on the design.
|
|
97
|
+
* Keeps the W-029 API ready for the Site styles delete dialog.
|
|
98
|
+
*/
|
|
99
|
+
export function findVariableUsagesInDesign(
|
|
100
|
+
design: DesignSystem,
|
|
101
|
+
id: string,
|
|
102
|
+
kind?: VariableKind,
|
|
103
|
+
): VariableUsage[] {
|
|
104
|
+
const usages: VariableUsage[] = [];
|
|
105
|
+
for (const cls of design.classes ?? []) {
|
|
106
|
+
collectAllUsages(cls, id, kind, (prop) => {
|
|
107
|
+
usages.push({ nodeId: "", prop, classId: cls.id });
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return usages;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function stripRefsFromStyle(
|
|
114
|
+
style: StyleProps | undefined,
|
|
115
|
+
id: string,
|
|
116
|
+
kind: VariableKind | undefined,
|
|
117
|
+
): StyleProps | undefined {
|
|
118
|
+
if (!style) return undefined;
|
|
119
|
+
let changed = false;
|
|
120
|
+
const next: Record<string, unknown> = { ...style };
|
|
121
|
+
for (const [prop, value] of Object.entries(style)) {
|
|
122
|
+
if (!matches(value, id, kind)) continue;
|
|
123
|
+
delete next[prop];
|
|
124
|
+
changed = true;
|
|
125
|
+
}
|
|
126
|
+
if (style.boxShadow && shadowColorMatches(style, id, kind)) {
|
|
127
|
+
const { color: _drop, ...shadow } = style.boxShadow;
|
|
128
|
+
next["boxShadow"] = shadow;
|
|
129
|
+
changed = true;
|
|
130
|
+
}
|
|
131
|
+
// W-245: the icon drop shadow (W-237) keeps its offsets and blur, like the box shadow.
|
|
132
|
+
if (style.iconShadow && matches(style.iconShadow.color, id, kind)) {
|
|
133
|
+
const { color: _drop, ...shadow } = style.iconShadow;
|
|
134
|
+
next["iconShadow"] = shadow;
|
|
135
|
+
changed = true;
|
|
136
|
+
}
|
|
137
|
+
if (style.gradient?.stops.some((stop) => matches(stop.color, id, kind))) {
|
|
138
|
+
delete next["gradient"];
|
|
139
|
+
changed = true;
|
|
140
|
+
}
|
|
141
|
+
if (style.overlay && matches(style.overlay.color, id, kind)) {
|
|
142
|
+
delete next["overlay"];
|
|
143
|
+
changed = true;
|
|
144
|
+
}
|
|
145
|
+
if (!changed) return style;
|
|
146
|
+
return Object.keys(next).length === 0 ? undefined : (next as StyleProps);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The same for every state; a state left empty is dropped, and so is `states` itself. */
|
|
150
|
+
function stripRefsFromStates(
|
|
151
|
+
states: StyleStates | undefined,
|
|
152
|
+
id: string,
|
|
153
|
+
kind: VariableKind | undefined,
|
|
154
|
+
): StyleStates | undefined {
|
|
155
|
+
if (!states) return undefined;
|
|
156
|
+
let changed = false;
|
|
157
|
+
const next: StyleStates = {};
|
|
158
|
+
for (const state of STYLE_STATES) {
|
|
159
|
+
const style = states[state];
|
|
160
|
+
if (!style) continue;
|
|
161
|
+
const stripped = stripRefsFromStyle(style, id, kind);
|
|
162
|
+
if (stripped !== style) changed = true;
|
|
163
|
+
if (stripped) next[state] = stripped;
|
|
164
|
+
}
|
|
165
|
+
if (!changed) return states;
|
|
166
|
+
return Object.keys(next).length === 0 ? undefined : next;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function stripRefsFromDevices(
|
|
170
|
+
devices: DeviceStyles | undefined,
|
|
171
|
+
id: string,
|
|
172
|
+
kind: VariableKind | undefined,
|
|
173
|
+
): DeviceStyles | undefined {
|
|
174
|
+
if (!devices) return undefined;
|
|
175
|
+
let changed = false;
|
|
176
|
+
const next: DeviceStyles = { ...devices };
|
|
177
|
+
for (const device of RESPONSIVE) {
|
|
178
|
+
const style = stripRefsFromStyle(devices[device], id, kind);
|
|
179
|
+
if (style === devices[device]) continue;
|
|
180
|
+
changed = true;
|
|
181
|
+
if (style) next[device] = style;
|
|
182
|
+
else delete next[device];
|
|
183
|
+
}
|
|
184
|
+
if (!changed) return devices;
|
|
185
|
+
return next.tablet || next.mobile ? next : undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Style, states and device overrides with the refs removed. Same object when nothing changed. */
|
|
189
|
+
function stripOwner<T extends { style?: StyleProps; states?: StyleStates; devices?: DeviceStyles }>(
|
|
190
|
+
owner: T,
|
|
191
|
+
id: string,
|
|
192
|
+
kind: VariableKind | undefined,
|
|
193
|
+
): T {
|
|
194
|
+
const style = stripRefsFromStyle(owner.style, id, kind);
|
|
195
|
+
const states = stripRefsFromStates(owner.states, id, kind);
|
|
196
|
+
const devices = stripRefsFromDevices(owner.devices, id, kind);
|
|
197
|
+
if (style === owner.style && states === owner.states && devices === owner.devices) return owner;
|
|
198
|
+
const next: T = { ...owner, style, states, devices };
|
|
199
|
+
if (style === undefined) delete next.style;
|
|
200
|
+
if (states === undefined) delete next.states;
|
|
201
|
+
if (devices === undefined) delete next.devices;
|
|
202
|
+
return next;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Drop every `{ var: id }` style binding on the layout (fallback to defaults). Pure. */
|
|
206
|
+
export function clearVariableRefs(layout: Layout, id: string, kind?: VariableKind): Layout {
|
|
207
|
+
let next = layout;
|
|
208
|
+
const usages = findVariableUsages(layout, id, kind);
|
|
209
|
+
// Deduplicate node ids — a node may reference the same var on several props.
|
|
210
|
+
const nodeIds = [...new Set(usages.map((u) => u.nodeId))];
|
|
211
|
+
for (const nodeId of nodeIds) {
|
|
212
|
+
next = updateNode(next, nodeId, (node) => stripOwner(node, id, kind));
|
|
213
|
+
}
|
|
214
|
+
return next;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const KIND_TO_LIST = {
|
|
218
|
+
color: "colors",
|
|
219
|
+
font: "fonts",
|
|
220
|
+
fontSize: "fontSizes",
|
|
221
|
+
spacing: "spacings",
|
|
222
|
+
} as const;
|
|
223
|
+
|
|
224
|
+
/** Most variables of each kind a site may have; the design schema refuses more (W-219). */
|
|
225
|
+
export const MAX_VARIABLES = { color: 200, font: 50, fontSize: 50, spacing: 100 } as const;
|
|
226
|
+
|
|
227
|
+
/** True when a site already has the most variables of `kind` allowed (W-219). */
|
|
228
|
+
export function variableListFull(design: DesignSystem, kind: VariableKind): boolean {
|
|
229
|
+
return (design.variables[KIND_TO_LIST[kind]] ?? []).length >= MAX_VARIABLES[kind];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Drop every binding to the variable on class styles; a class left with none keeps `{}`. */
|
|
233
|
+
function clearClassStyleRefs(
|
|
234
|
+
classes: DesignSystem["classes"],
|
|
235
|
+
id: string,
|
|
236
|
+
kind: VariableKind,
|
|
237
|
+
): DesignSystem["classes"] {
|
|
238
|
+
if (!classes) return classes;
|
|
239
|
+
let changed = false;
|
|
240
|
+
const next = classes.map((cls) => {
|
|
241
|
+
const stripped = stripOwner(cls, id, kind);
|
|
242
|
+
if (stripped === cls) return cls;
|
|
243
|
+
changed = true;
|
|
244
|
+
return { ...stripped, style: stripped.style ?? {} };
|
|
245
|
+
});
|
|
246
|
+
return changed ? next : classes;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Remove a variable from the design document and its bindings on class styles. Pure; does not touch layouts. */
|
|
250
|
+
export function removeVariable(design: DesignSystem, id: string, kind: VariableKind): DesignSystem {
|
|
251
|
+
const key = KIND_TO_LIST[kind];
|
|
252
|
+
const list = design.variables[key] ?? [];
|
|
253
|
+
const filtered = list.filter((v) => v.id !== id);
|
|
254
|
+
const classes = clearClassStyleRefs(design.classes, id, kind);
|
|
255
|
+
if (filtered.length === list.length && classes === design.classes) return design;
|
|
256
|
+
return {
|
|
257
|
+
...design,
|
|
258
|
+
variables: {
|
|
259
|
+
...design.variables,
|
|
260
|
+
[key]: filtered,
|
|
261
|
+
},
|
|
262
|
+
...(classes ? { classes } : {}),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** A copy of a variable, with a new id and the name "… copy". The source is unchanged. Pure. */
|
|
267
|
+
export function duplicateVariable(
|
|
268
|
+
design: DesignSystem,
|
|
269
|
+
id: string,
|
|
270
|
+
kind: VariableKind,
|
|
271
|
+
): DesignSystem {
|
|
272
|
+
const key = KIND_TO_LIST[kind];
|
|
273
|
+
const list = design.variables[key] ?? [];
|
|
274
|
+
const source = list.find((entry) => entry.id === id);
|
|
275
|
+
// A copy past the cap, or a name past 60 characters, would fail to save (W-219).
|
|
276
|
+
if (!source || variableListFull(design, kind)) return design;
|
|
277
|
+
// W-278: "Brand copy", then "Brand copy 2"…, so the copy's name is free too.
|
|
278
|
+
const stem = `${source.name.slice(0, 52).trim()} copy`;
|
|
279
|
+
let name = stem;
|
|
280
|
+
for (let n = 2; n <= MAX_VARIABLES[kind] && variableNameTaken(design, kind, name); n++) {
|
|
281
|
+
name = `${stem} ${n}`;
|
|
282
|
+
}
|
|
283
|
+
const base = slugify(name).slice(0, 34) || kind;
|
|
284
|
+
const taken = new Set(list.map((entry) => entry.id));
|
|
285
|
+
let nextId = base;
|
|
286
|
+
for (let n = 2; taken.has(nextId); n++) nextId = `${base}-${n}`.slice(0, 40);
|
|
287
|
+
const copy = Object.assign({}, source, {
|
|
288
|
+
id: nextId,
|
|
289
|
+
name,
|
|
290
|
+
value: typeof source.value === "object" ? structuredClone(source.value) : source.value,
|
|
291
|
+
});
|
|
292
|
+
return {
|
|
293
|
+
...design,
|
|
294
|
+
variables: { ...design.variables, [key]: [...list, copy] },
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* W-278: whether another variable of this kind already has the name (case-insensitive). Pickers
|
|
300
|
+
* list variables by name, so two with one name can't be told apart.
|
|
301
|
+
*/
|
|
302
|
+
export function variableNameTaken(
|
|
303
|
+
design: DesignSystem,
|
|
304
|
+
kind: VariableKind,
|
|
305
|
+
name: string,
|
|
306
|
+
exceptId?: string,
|
|
307
|
+
): boolean {
|
|
308
|
+
const wanted = cleanClassName(name).toLowerCase();
|
|
309
|
+
return (design.variables[KIND_TO_LIST[kind]] ?? []).some(
|
|
310
|
+
(v) => v.id !== exceptId && v.name.toLowerCase() === wanted,
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The message shown when a new or renamed variable would take another's name (W-278). */
|
|
315
|
+
export const variableNameTakenMessage = (name: string) =>
|
|
316
|
+
`A variable named "${cleanClassName(name)}" already exists. Pick another name.`;
|
|
317
|
+
|
|
318
|
+
/** Rename is name-only; variable ids stay stable (slug at create time). A taken name is refused (W-278). */
|
|
319
|
+
export function renameVariable(
|
|
320
|
+
design: DesignSystem,
|
|
321
|
+
id: string,
|
|
322
|
+
kind: VariableKind,
|
|
323
|
+
name: string,
|
|
324
|
+
): DesignSystem {
|
|
325
|
+
const trimmed = cleanClassName(name);
|
|
326
|
+
if (!trimmed || variableNameTaken(design, kind, trimmed, id)) return design;
|
|
327
|
+
const key = KIND_TO_LIST[kind];
|
|
328
|
+
const list = design.variables[key] ?? [];
|
|
329
|
+
let changed = false;
|
|
330
|
+
const next = list.map((v) => {
|
|
331
|
+
if (v.id !== id || v.name === trimmed) return v;
|
|
332
|
+
changed = true;
|
|
333
|
+
return Object.assign({}, v, { name: trimmed });
|
|
334
|
+
});
|
|
335
|
+
if (!changed) return design;
|
|
336
|
+
return {
|
|
337
|
+
...design,
|
|
338
|
+
variables: {
|
|
339
|
+
...design.variables,
|
|
340
|
+
[key]: next,
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Delete a variable: remove it from the design, clear refs on class styles and on `layout`.
|
|
347
|
+
* Callers that need a confirmation dialog should run `findVariableUsages` first.
|
|
348
|
+
*/
|
|
349
|
+
export function deleteVariable(
|
|
350
|
+
design: DesignSystem,
|
|
351
|
+
layout: Layout,
|
|
352
|
+
id: string,
|
|
353
|
+
kind: VariableKind,
|
|
354
|
+
): { design: DesignSystem; layout: Layout } {
|
|
355
|
+
return {
|
|
356
|
+
design: removeVariable(design, id, kind),
|
|
357
|
+
layout: clearVariableRefs(layout, id, kind),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ElementDescriptor } from "../schema/descriptors.ts";
|
|
2
|
+
import type { VNode } from "../render/vnode.ts";
|
|
3
|
+
|
|
4
|
+
/** Shared shape of an element definition: defaults, descriptor, CSS, and its VNode build. */
|
|
5
|
+
export type Build<N> = (node: N, attrs: Record<string, string>, children: VNode[]) => VNode;
|
|
6
|
+
|
|
7
|
+
export type ElementDefinition<N> = {
|
|
8
|
+
baseCss: string;
|
|
9
|
+
build: Build<N>;
|
|
10
|
+
descriptor: ElementDescriptor;
|
|
11
|
+
defaults: () => Omit<N, "id">;
|
|
12
|
+
};
|