@perkyzz/emvb 0.2.1 → 0.4.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/README.md +4 -2
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/package.json +3 -1
- package/src/admin/access/role.ts +9 -3
- package/src/admin/api.ts +17 -7
- package/src/admin/content-api.ts +7 -51
- package/src/admin/content-item.ts +22 -0
- package/src/admin/design-api.ts +30 -0
- package/src/admin/editor/A11yCopilot.tsx +126 -0
- package/src/admin/editor/CommandPalette.tsx +118 -0
- package/src/admin/editor/Editor.tsx +188 -152
- package/src/admin/editor/EditorOverlay.tsx +9 -1
- package/src/admin/editor/PerfMeter.tsx +121 -0
- package/src/admin/editor/ShareLink.tsx +164 -0
- package/src/admin/editor/ShortcutsDialog.tsx +4 -1
- package/src/admin/editor/Timeline.tsx +287 -0
- package/src/admin/editor/TopBar.tsx +4 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +62 -370
- package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
- package/src/admin/editor/canvas/SelectionOverlay.tsx +87 -47
- package/src/admin/editor/canvas/canvas-scale.ts +20 -0
- package/src/admin/editor/canvas/drop-line.ts +24 -0
- package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
- package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
- package/src/admin/editor/collection-previews.ts +114 -0
- package/src/admin/editor/editor-chrome.tsx +346 -0
- package/src/admin/editor/editor-commands.ts +360 -0
- package/src/admin/editor/editor-css.ts +5 -3
- package/src/admin/editor/element-chrome.ts +89 -0
- package/src/admin/editor/feature-css.ts +105 -0
- package/src/admin/editor/host.ts +35 -0
- package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
- package/src/admin/editor/icons/library.ts +29 -19
- package/src/admin/editor/icons/use-icon-library.ts +315 -0
- package/src/admin/editor/live-data.tsx +71 -0
- package/src/admin/editor/palette-items.ts +136 -0
- package/src/admin/editor/palette.ts +68 -0
- package/src/admin/editor/panels/AddPanel.tsx +48 -68
- package/src/admin/editor/panels/ClassesSection.tsx +5 -1
- package/src/admin/editor/panels/ColorControl.tsx +2 -10
- package/src/admin/editor/panels/ElementPanel.tsx +74 -102
- package/src/admin/editor/panels/LayersPanel.tsx +72 -63
- package/src/admin/editor/panels/LeftPanel.tsx +4 -1
- package/src/admin/editor/panels/SettingsPanel.tsx +13 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +18 -10
- package/src/admin/editor/panels/TokenScales.tsx +168 -0
- package/src/admin/editor/panels/VariableSection.tsx +10 -14
- package/src/admin/editor/panels/inline-error.tsx +11 -0
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +87 -1
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +285 -0
- package/src/admin/editor/panels/settings/BindingsEditor.tsx +221 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +1 -1
- package/src/admin/editor/panels/settings/DraftTextField.tsx +4 -0
- package/src/admin/editor/panels/settings/EffectsControls.tsx +2 -2
- package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
- package/src/admin/editor/panels/settings/ItemList.tsx +34 -1
- package/src/admin/editor/panels/settings/MotionControl.tsx +252 -0
- package/src/admin/editor/panels/settings/StateSwitcher.tsx +1 -1
- package/src/admin/editor/panels/settings/StyleRow.tsx +26 -2
- package/src/admin/editor/panels/settings/StyleSources.tsx +94 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +1 -1
- package/src/admin/editor/panels/settings/VariantEditor.tsx +83 -0
- package/src/admin/editor/panels/settings/form-names.ts +7 -11
- package/src/admin/editor/panels/settings/length-units.ts +16 -5
- package/src/admin/editor/panels/settings/style-sections.ts +7 -2
- package/src/admin/editor/panels/style-target.ts +105 -0
- package/src/admin/editor/section-templates.ts +43 -21
- package/src/admin/editor/shortcuts.ts +6 -1
- package/src/admin/editor/store.ts +10 -1
- package/src/admin/editor/useClipboardActions.ts +1 -1
- package/src/admin/editor/useEditorCommands.ts +61 -13
- package/src/admin/editor/useEditorData.ts +7 -1
- package/src/admin/editor/useEditorShortcuts.ts +30 -14
- package/src/admin/editor/useNodeActions.ts +44 -16
- package/src/admin/forms-api.ts +17 -6
- package/src/admin/icon-uploads-api.ts +18 -0
- package/src/admin/theme-api.ts +5 -18
- package/src/admin/ui.ts +4 -4
- package/src/astro/content-port.ts +24 -0
- package/src/astro/forms/EmVBFormsClient.astro +11 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
- package/src/astro/forms/client.ts +1 -0
- package/src/astro/forms-definitions.ts +24 -11
- package/src/astro/index.ts +1 -0
- package/src/astro/render-session.ts +85 -0
- package/src/astro/request-data.ts +109 -0
- package/src/astro/resolve-theme.ts +184 -64
- package/src/astro/resolve.ts +63 -16
- package/src/astro/theme-posts.ts +48 -5
- package/src/astro/visitor.ts +204 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +282 -0
- package/src/core/arrange.ts +150 -34
- package/src/core/audience/rules.ts +124 -0
- package/src/core/audience/variants.ts +51 -0
- package/src/core/canvas-styles.ts +24 -0
- package/src/core/clipboard.ts +11 -32
- package/src/core/css/generate.ts +29 -2
- package/src/core/css/print.ts +12 -0
- package/src/core/css/scope.ts +3 -3
- package/src/core/css/trace.ts +169 -0
- package/src/core/css-keywords.ts +83 -0
- package/src/core/data/bindings.ts +255 -0
- package/src/core/design/classes.ts +13 -15
- package/src/core/design/diff.ts +55 -0
- package/src/core/design/merge.ts +127 -0
- package/src/core/design/tokens.ts +127 -0
- package/src/core/design/variables.ts +25 -21
- package/src/core/elements/dynamic-elements.ts +117 -2
- package/src/core/elements/form-elements.ts +1 -0
- package/src/core/elements/index.ts +185 -7
- package/src/core/forms/binding.ts +4 -13
- package/src/core/history/diff.ts +152 -0
- package/src/core/index.ts +121 -0
- package/src/core/local-style.ts +27 -0
- package/src/core/migrate/index.ts +12 -4
- package/src/core/nesting.ts +4 -22
- package/src/core/perf/meter.ts +160 -0
- package/src/core/perf/scripts.ts +61 -0
- package/src/core/recipes/index.ts +433 -0
- package/src/core/record.ts +3 -0
- package/src/core/render/context.ts +2 -0
- package/src/core/render/dynamic-post.ts +1 -2
- package/src/core/render/form.ts +133 -10
- package/src/core/render/index.ts +206 -17
- package/src/core/render/loop.ts +54 -7
- package/src/core/render/pagination.ts +9 -1
- package/src/core/render/vnode.ts +12 -2
- package/src/core/sanitize/css.ts +129 -80
- package/src/core/sanitize/motion.ts +129 -0
- package/src/core/sanitize/svg.ts +8 -3
- package/src/core/sanitize/url.ts +10 -1
- package/src/core/schema/design.ts +29 -8
- package/src/core/schema/layout.ts +291 -169
- package/src/core/schema/motion.ts +14 -0
- package/src/core/schema/style.ts +101 -66
- package/src/core/share/link.ts +187 -0
- package/src/core/tabs/presence.ts +7 -17
- package/src/core/theme/dynamic.ts +242 -32
- package/src/core/theme/float.ts +1 -1
- package/src/core/theme/pagination.ts +56 -0
- package/src/core/tree-ops.ts +57 -0
- package/src/index.ts +14 -2
- package/src/server/design-routes.ts +4 -1
- package/src/server/icon-routes.ts +68 -16
- package/src/server/plugin.ts +8 -2
- package/src/admin/editor/icons/sets/fontawesome.json +0 -1
- package/src/admin/editor/icons/sets/lucide.json +0 -1
- package/src/admin/editor/icons/sets/remix.json +0 -1
- package/src/admin/editor/icons/sets/tabler.json +0 -1
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { DefaultStyleTag, DesignSystem } from "../schema/design.ts";
|
|
2
|
+
import type { LayoutNode } from "../schema/layout.ts";
|
|
3
|
+
import type { StyleStateName } from "../schema/state-names.ts";
|
|
4
|
+
import type { StyleProps } from "../schema/style.ts";
|
|
5
|
+
|
|
6
|
+
export type TraceDevice = "desktop" | "tablet" | "mobile";
|
|
7
|
+
|
|
8
|
+
/** Where one style value comes from (W-318). */
|
|
9
|
+
export type StyleSource = {
|
|
10
|
+
kind: "default" | "class" | "local";
|
|
11
|
+
/** "Site default (H2)", ".card", "This element". */
|
|
12
|
+
label: string;
|
|
13
|
+
/** Desktop, Tablet, Mobile, or a state such as Hover. */
|
|
14
|
+
layer: string;
|
|
15
|
+
classId?: string;
|
|
16
|
+
value: unknown;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** For one property: the value that applies and the ones it overrides, strongest first. */
|
|
20
|
+
export type StyleTrace = { winner: StyleSource; overridden: StyleSource[] };
|
|
21
|
+
|
|
22
|
+
/** The tag a node renders whose Site styles default applies (W-318). */
|
|
23
|
+
export function defaultTagFor(node: LayoutNode): DefaultStyleTag | undefined {
|
|
24
|
+
const props = node.props as Record<string, unknown>;
|
|
25
|
+
switch (node.type) {
|
|
26
|
+
case "heading": {
|
|
27
|
+
const level = typeof props["level"] === "number" ? props["level"] : 2;
|
|
28
|
+
return `h${Math.min(6, Math.max(1, level))}` as DefaultStyleTag;
|
|
29
|
+
}
|
|
30
|
+
case "text":
|
|
31
|
+
return props["tag"] === "div" ? undefined : "p";
|
|
32
|
+
case "link":
|
|
33
|
+
return "a";
|
|
34
|
+
case "button":
|
|
35
|
+
return typeof props["href"] === "string" && props["href"].trim() ? "a" : "button";
|
|
36
|
+
default:
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const LAYER = { desktop: "Desktop", tablet: "Tablet", mobile: "Mobile" } as const;
|
|
42
|
+
const STATE_LABEL: Record<StyleStateName, string> = {
|
|
43
|
+
hover: "Hover",
|
|
44
|
+
focus: "Focus",
|
|
45
|
+
active: "Active",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type Owner = {
|
|
49
|
+
kind: "class" | "local";
|
|
50
|
+
label: string;
|
|
51
|
+
classId?: string;
|
|
52
|
+
style?: StyleProps;
|
|
53
|
+
devices?: { tablet?: StyleProps; mobile?: StyleProps };
|
|
54
|
+
states?: Partial<Record<StyleStateName, StyleProps>>;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Every style layer that applies to a node on a device and in a state, weakest first, in the
|
|
59
|
+
* cascade EmVB emits (W-318): the tag's Site styles default, each class in site order (later
|
|
60
|
+
* classes win), then the element's own styles; within each, Desktop then Tablet then Mobile;
|
|
61
|
+
* state styles last, classes before the element.
|
|
62
|
+
*/
|
|
63
|
+
export function styleLayers(
|
|
64
|
+
node: LayoutNode,
|
|
65
|
+
design: DesignSystem,
|
|
66
|
+
device: TraceDevice = "desktop",
|
|
67
|
+
state: StyleStateName | "normal" = "normal",
|
|
68
|
+
): { source: Omit<StyleSource, "value">; style: StyleProps }[] {
|
|
69
|
+
const layers: { source: Omit<StyleSource, "value">; style: StyleProps }[] = [];
|
|
70
|
+
const tag = defaultTagFor(node);
|
|
71
|
+
const tagStyle = tag ? design.defaults?.[tag] : undefined;
|
|
72
|
+
if (tag && tagStyle) {
|
|
73
|
+
layers.push({
|
|
74
|
+
source: { kind: "default", label: `Site default (${tag.toUpperCase()})`, layer: "Desktop" },
|
|
75
|
+
style: tagStyle,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const applied = new Set(node.classes ?? []);
|
|
79
|
+
const owners: Owner[] = [];
|
|
80
|
+
for (const cls of design.classes ?? []) {
|
|
81
|
+
if (!applied.has(cls.id)) continue;
|
|
82
|
+
const owner: Owner = {
|
|
83
|
+
kind: "class",
|
|
84
|
+
label: `.${cls.name}`,
|
|
85
|
+
classId: cls.id,
|
|
86
|
+
style: cls.style,
|
|
87
|
+
};
|
|
88
|
+
if (cls.devices) owner.devices = cls.devices;
|
|
89
|
+
if (cls.states) owner.states = cls.states;
|
|
90
|
+
owners.push(owner);
|
|
91
|
+
}
|
|
92
|
+
owners.push({
|
|
93
|
+
kind: "local",
|
|
94
|
+
label: "This element",
|
|
95
|
+
...(node.style ? { style: node.style } : {}),
|
|
96
|
+
...(node.devices ? { devices: node.devices } : {}),
|
|
97
|
+
...(node.states ? { states: node.states as Owner["states"] } : {}),
|
|
98
|
+
});
|
|
99
|
+
for (const owner of owners) {
|
|
100
|
+
const base = {
|
|
101
|
+
kind: owner.kind,
|
|
102
|
+
label: owner.label,
|
|
103
|
+
...(owner.classId ? { classId: owner.classId } : {}),
|
|
104
|
+
};
|
|
105
|
+
if (owner.style) layers.push({ source: { ...base, layer: LAYER.desktop }, style: owner.style });
|
|
106
|
+
if (device !== "desktop" && owner.devices?.tablet) {
|
|
107
|
+
layers.push({ source: { ...base, layer: LAYER.tablet }, style: owner.devices.tablet });
|
|
108
|
+
}
|
|
109
|
+
if (device === "mobile" && owner.devices?.mobile) {
|
|
110
|
+
layers.push({ source: { ...base, layer: LAYER.mobile }, style: owner.devices.mobile });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (state !== "normal") {
|
|
114
|
+
for (const owner of owners) {
|
|
115
|
+
const style = owner.states?.[state];
|
|
116
|
+
if (!style) continue;
|
|
117
|
+
layers.push({
|
|
118
|
+
source: {
|
|
119
|
+
kind: owner.kind,
|
|
120
|
+
label: owner.label,
|
|
121
|
+
...(owner.classId ? { classId: owner.classId } : {}),
|
|
122
|
+
layer: STATE_LABEL[state],
|
|
123
|
+
},
|
|
124
|
+
style,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return layers;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Where each set property of a node comes from (W-318), by property name. */
|
|
132
|
+
export function traceStyle(
|
|
133
|
+
node: LayoutNode,
|
|
134
|
+
design: DesignSystem,
|
|
135
|
+
device: TraceDevice = "desktop",
|
|
136
|
+
state: StyleStateName | "normal" = "normal",
|
|
137
|
+
): Record<string, StyleTrace> {
|
|
138
|
+
const all: Record<string, StyleSource[]> = {};
|
|
139
|
+
for (const { source, style } of styleLayers(node, design, device, state)) {
|
|
140
|
+
for (const [key, value] of Object.entries(style)) {
|
|
141
|
+
if (value === undefined) continue;
|
|
142
|
+
(all[key] ??= []).push({ ...source, value });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const out: Record<string, StyleTrace> = {};
|
|
146
|
+
for (const [key, sources] of Object.entries(all)) {
|
|
147
|
+
const strongest = sources.toReversed();
|
|
148
|
+
out[key] = { winner: strongest[0] as StyleSource, overridden: strongest.slice(1) };
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* What a node's own style edit sits on top of (W-318): every value from its tag default and its
|
|
155
|
+
* classes on this device (and, on Tablet/Mobile or in a state, from its own wider layers). The
|
|
156
|
+
* editor shows these as the controls' inherited values.
|
|
157
|
+
*/
|
|
158
|
+
export function inheritedStyle(
|
|
159
|
+
node: LayoutNode,
|
|
160
|
+
design: DesignSystem,
|
|
161
|
+
device: TraceDevice,
|
|
162
|
+
state: StyleStateName | "normal",
|
|
163
|
+
): StyleProps {
|
|
164
|
+
const layers = styleLayers(node, design, device, state);
|
|
165
|
+
// Drop the layer being edited: the element's own on this device and state.
|
|
166
|
+
const editing = state !== "normal" ? STATE_LABEL[state] : LAYER[device];
|
|
167
|
+
const below = layers.filter((l) => !(l.source.kind === "local" && l.source.layer === editing));
|
|
168
|
+
return Object.assign({}, ...below.map((l) => l.style)) as StyleProps;
|
|
169
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyword allowlists the schema and the CSS sanitizer both read (W-357).
|
|
3
|
+
* Emission stays in the sanitizer.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
7
|
+
|
|
8
|
+
export const LENGTH_UNITS = ["px", "rem", "em", "%", "vw", "vh", "ch"] as const;
|
|
9
|
+
|
|
10
|
+
export const FLEX_DIRECTIONS = ["row", "column", "row-reverse", "column-reverse"] as const;
|
|
11
|
+
|
|
12
|
+
export const FLEX_WRAPS = ["nowrap", "wrap", "wrap-reverse"] as const;
|
|
13
|
+
|
|
14
|
+
export const JUSTIFY_CONTENT = [
|
|
15
|
+
"flex-start",
|
|
16
|
+
"flex-end",
|
|
17
|
+
"center",
|
|
18
|
+
"space-between",
|
|
19
|
+
"space-around",
|
|
20
|
+
"space-evenly",
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
export const ALIGN_ITEMS = ["stretch", "flex-start", "flex-end", "center", "baseline"] as const;
|
|
24
|
+
|
|
25
|
+
export const TEXT_ALIGNS = ["left", "center", "right", "justify"] as const;
|
|
26
|
+
|
|
27
|
+
export const TEXT_TRANSFORMS = ["none", "uppercase", "lowercase", "capitalize"] as const;
|
|
28
|
+
|
|
29
|
+
export const TEXT_DECORATIONS = ["none", "underline", "overline", "line-through"] as const;
|
|
30
|
+
|
|
31
|
+
export const BORDER_STYLES = ["none", "solid", "dashed", "dotted"] as const;
|
|
32
|
+
|
|
33
|
+
export const OVERFLOWS = ["visible", "hidden", "clip", "scroll", "auto"] as const;
|
|
34
|
+
|
|
35
|
+
export const OBJECT_FITS = ["fill", "contain", "cover", "none", "scale-down"] as const;
|
|
36
|
+
|
|
37
|
+
export const POSITIONS = ["static", "relative", "absolute", "fixed", "sticky"] as const;
|
|
38
|
+
|
|
39
|
+
export const CURSORS = [
|
|
40
|
+
"default",
|
|
41
|
+
"pointer",
|
|
42
|
+
"text",
|
|
43
|
+
"move",
|
|
44
|
+
"grab",
|
|
45
|
+
"not-allowed",
|
|
46
|
+
"help",
|
|
47
|
+
"crosshair",
|
|
48
|
+
"zoom-in",
|
|
49
|
+
] as const;
|
|
50
|
+
|
|
51
|
+
export const FONT_WEIGHTS = [
|
|
52
|
+
"normal",
|
|
53
|
+
"bold",
|
|
54
|
+
"100",
|
|
55
|
+
"200",
|
|
56
|
+
"300",
|
|
57
|
+
"400",
|
|
58
|
+
"500",
|
|
59
|
+
"600",
|
|
60
|
+
"700",
|
|
61
|
+
"800",
|
|
62
|
+
"900",
|
|
63
|
+
] as const;
|
|
64
|
+
|
|
65
|
+
export const TRANSITION_EASINGS = ["ease", "ease-in", "ease-out", "ease-in-out", "linear"] as const;
|
|
66
|
+
|
|
67
|
+
export const TRANSITION_PROPERTIES = ["all", "colors", "opacity", "shadow", "filter"] as const;
|
|
68
|
+
|
|
69
|
+
export const BACKGROUND_SIZES = ["auto", "cover", "contain"] as const;
|
|
70
|
+
|
|
71
|
+
export const BACKGROUND_POSITIONS = [
|
|
72
|
+
"center",
|
|
73
|
+
"top",
|
|
74
|
+
"bottom",
|
|
75
|
+
"left",
|
|
76
|
+
"right",
|
|
77
|
+
"top left",
|
|
78
|
+
"top right",
|
|
79
|
+
"bottom left",
|
|
80
|
+
"bottom right",
|
|
81
|
+
] as const;
|
|
82
|
+
|
|
83
|
+
export const BACKGROUND_REPEATS = ["no-repeat", "repeat", "repeat-x", "repeat-y"] as const;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import type { Binding, BindSource, LayoutNode } from "../schema/layout.ts";
|
|
2
|
+
import { someNode } from "../tree-ops.ts";
|
|
3
|
+
import { sanitizeHref } from "../sanitize/href.ts";
|
|
4
|
+
import { sanitizeMediaUrl } from "../sanitize/media-url.ts";
|
|
5
|
+
import type { ThemePostFields } from "../theme/dynamic.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Live data bindings (W-307). A text, image or link field can read a post field, a site setting
|
|
9
|
+
* or a URL parameter instead of its typed value. The typed value stays as the fallback, so a page
|
|
10
|
+
* never shows an empty heading because a parameter is missing. Values are plain text: the element
|
|
11
|
+
* still escapes text and sanitizes links and image URLs, exactly as for a typed value.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** How a bound value is used, which decides how it's cleaned before the element sees it. */
|
|
15
|
+
export type BindKind = "text" | "url" | "image";
|
|
16
|
+
|
|
17
|
+
export type BindableField = { key: string; label: string; kind: BindKind };
|
|
18
|
+
|
|
19
|
+
const TEXT = (label = "Text"): BindableField => ({ key: "text", label, kind: "text" });
|
|
20
|
+
const HREF = (label = "Link"): BindableField => ({ key: "href", label, kind: "url" });
|
|
21
|
+
|
|
22
|
+
/** Which props of which elements can be bound. Anything else in `bind` is ignored on render. */
|
|
23
|
+
export const BINDABLE_FIELDS: Readonly<Record<string, readonly BindableField[]>> = {
|
|
24
|
+
heading: [TEXT(), HREF()],
|
|
25
|
+
text: [TEXT()],
|
|
26
|
+
label: [TEXT()],
|
|
27
|
+
link: [TEXT(), HREF("URL")],
|
|
28
|
+
button: [TEXT(), HREF()],
|
|
29
|
+
image: [
|
|
30
|
+
{ key: "src", label: "Image", kind: "image" },
|
|
31
|
+
{ key: "alt", label: "Alt text", kind: "text" },
|
|
32
|
+
],
|
|
33
|
+
icon: [HREF(), { key: "title", label: "Title", kind: "text" }],
|
|
34
|
+
"menu-item": [TEXT("Label"), HREF()],
|
|
35
|
+
container: [HREF()],
|
|
36
|
+
"div-block": [HREF()],
|
|
37
|
+
flexbox: [HREF()],
|
|
38
|
+
grid: [HREF()],
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const bindableFields = (type: string): readonly BindableField[] =>
|
|
42
|
+
Object.hasOwn(BINDABLE_FIELDS, type) ? (BINDABLE_FIELDS[type] ?? []) : [];
|
|
43
|
+
|
|
44
|
+
/** The post fields the editor offers by name; any other key reads the post's own data. */
|
|
45
|
+
export const POST_BIND_KEYS = [
|
|
46
|
+
{ key: "title", label: "Title" },
|
|
47
|
+
{ key: "excerpt", label: "Excerpt" },
|
|
48
|
+
{ key: "permalink", label: "Permalink" },
|
|
49
|
+
{ key: "slug", label: "Slug" },
|
|
50
|
+
{ key: "publishedAt", label: "Published date" },
|
|
51
|
+
{ key: "authorName", label: "Author" },
|
|
52
|
+
{ key: "featuredImageUrl", label: "Featured image" },
|
|
53
|
+
{ key: "featuredImageAlt", label: "Featured image alt" },
|
|
54
|
+
] as const;
|
|
55
|
+
|
|
56
|
+
/** Site settings hosts pass in by default (EmDash's `getSiteSettings`). */
|
|
57
|
+
export const SITE_BIND_KEYS = [
|
|
58
|
+
{ key: "title", label: "Site title" },
|
|
59
|
+
{ key: "tagline", label: "Tagline" },
|
|
60
|
+
{ key: "url", label: "Site URL" },
|
|
61
|
+
] as const;
|
|
62
|
+
|
|
63
|
+
export const BIND_SOURCE_LABELS: Readonly<Record<BindSource, string>> = {
|
|
64
|
+
post: "Post field",
|
|
65
|
+
site: "Site setting",
|
|
66
|
+
param: "URL parameter",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** Where bound values come from on this render. */
|
|
70
|
+
export type BindingData = {
|
|
71
|
+
post?: ThemePostFields;
|
|
72
|
+
site?: Readonly<Record<string, string>>;
|
|
73
|
+
params?: Readonly<Record<string, string>>;
|
|
74
|
+
/** The page's origin; a URL-parameter link or image must stay on it unless `outside` (W-307). */
|
|
75
|
+
origin?: string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Longest bound value an element receives; longer values are cut (URL parameters are untrusted). */
|
|
79
|
+
export const MAX_BOUND_LENGTH = 500;
|
|
80
|
+
|
|
81
|
+
const POST_KEYS = new Set<string>(POST_BIND_KEYS.map((k) => k.key));
|
|
82
|
+
|
|
83
|
+
function scalar(value: unknown): string | undefined {
|
|
84
|
+
if (typeof value === "string") return value;
|
|
85
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
86
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function postValue(post: ThemePostFields | undefined, key: string): string | undefined {
|
|
91
|
+
if (!post) return undefined;
|
|
92
|
+
if (POST_KEYS.has(key)) return scalar((post as Record<string, unknown>)[key]);
|
|
93
|
+
const fields = post.fields;
|
|
94
|
+
if (!fields || !Object.hasOwn(fields, key)) return undefined;
|
|
95
|
+
return scalar(fields[key]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Text with control characters removed and its length capped. */
|
|
99
|
+
function clean(value: string): string {
|
|
100
|
+
let out = "";
|
|
101
|
+
for (const char of value) {
|
|
102
|
+
const code = char.codePointAt(0) ?? 0;
|
|
103
|
+
if (code === 10 || code === 9) out += char === "\t" ? " " : char;
|
|
104
|
+
else if (code >= 32 && code !== 127) out += char;
|
|
105
|
+
if (out.length >= MAX_BOUND_LENGTH) break;
|
|
106
|
+
}
|
|
107
|
+
return out.slice(0, MAX_BOUND_LENGTH).trim();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The live value of one binding, or undefined when its source has nothing for this key. */
|
|
111
|
+
export function resolveBinding(binding: Binding, data: BindingData): string | undefined {
|
|
112
|
+
let raw: string | undefined;
|
|
113
|
+
if (binding.source === "post") raw = postValue(data.post, binding.key);
|
|
114
|
+
else if (binding.source === "site") {
|
|
115
|
+
raw = data.site && Object.hasOwn(data.site, binding.key) ? data.site[binding.key] : undefined;
|
|
116
|
+
} else {
|
|
117
|
+
raw =
|
|
118
|
+
data.params && Object.hasOwn(data.params, binding.key) ? data.params[binding.key] : undefined;
|
|
119
|
+
}
|
|
120
|
+
if (raw === undefined) return undefined;
|
|
121
|
+
const value = clean(raw);
|
|
122
|
+
return value.length > 0 ? value : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** True when the node has at least one binding render would use. */
|
|
126
|
+
export function hasBindings(node: LayoutNode): boolean {
|
|
127
|
+
const bind = node.bind;
|
|
128
|
+
if (!bind) return false;
|
|
129
|
+
return bindableFields(node.type).some((field) => Object.hasOwn(bind, field.key));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Whether a URL stays on this site: relative (`/path`, `?q`, `#top`, `page`), or absolute on
|
|
134
|
+
* `origin`. `//host` and other schemes are outside; without an origin, only relative URLs pass.
|
|
135
|
+
*/
|
|
136
|
+
function isSameSiteUrl(value: string, origin: string | undefined): boolean {
|
|
137
|
+
const url = value.trim();
|
|
138
|
+
if (/^[/\\]{2}/.test(url)) return false;
|
|
139
|
+
if (!/^[a-z][a-z0-9+.-]*:/i.test(url)) return true;
|
|
140
|
+
if (!origin) return false;
|
|
141
|
+
try {
|
|
142
|
+
return new URL(url).origin === new URL(origin).origin;
|
|
143
|
+
} catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The value a bound field shows (W-307), after every check render applies, or undefined when
|
|
150
|
+
* the typed value shows instead: nothing to read, a link or image the sanitizer refuses, or a
|
|
151
|
+
* URL-parameter link or image to another site without "Allow outside sites".
|
|
152
|
+
*/
|
|
153
|
+
export function boundValue(
|
|
154
|
+
field: BindableField,
|
|
155
|
+
binding: Binding,
|
|
156
|
+
data: BindingData,
|
|
157
|
+
multiline = false,
|
|
158
|
+
): string | undefined {
|
|
159
|
+
let value = resolveBinding(binding, data);
|
|
160
|
+
if (value === undefined) return undefined;
|
|
161
|
+
if (!multiline) value = value.replace(/\s*\n\s*/g, " ");
|
|
162
|
+
if (field.kind === "url" && !sanitizeHref(value)) return undefined;
|
|
163
|
+
if (field.kind === "image" && !sanitizeMediaUrl(value)) return undefined;
|
|
164
|
+
if (
|
|
165
|
+
field.kind !== "text" &&
|
|
166
|
+
binding.source === "param" &&
|
|
167
|
+
!binding.outside &&
|
|
168
|
+
!isSameSiteUrl(value, data.origin)
|
|
169
|
+
) {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
return value;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The node with its bound props replaced by live values (W-307). Unbindable keys, empty values and
|
|
177
|
+
* links or images the sanitizer would refuse leave the typed prop as it is, so a bad URL parameter
|
|
178
|
+
* can't blank a button or an image. A URL parameter can't send a link or image off the site unless
|
|
179
|
+
* the binding allows outside sites (see `boundValue`). A bound text field that is single-line stays single-line.
|
|
180
|
+
*/
|
|
181
|
+
export function applyBindings(node: LayoutNode, data: BindingData): LayoutNode {
|
|
182
|
+
const bind = node.bind;
|
|
183
|
+
if (!bind) return node;
|
|
184
|
+
let props: Record<string, unknown> | undefined;
|
|
185
|
+
for (const field of bindableFields(node.type)) {
|
|
186
|
+
const binding = Object.hasOwn(bind, field.key) ? bind[field.key] : undefined;
|
|
187
|
+
if (!binding) continue;
|
|
188
|
+
const value = boundValue(field, binding, data, field.kind === "text" && node.type === "text");
|
|
189
|
+
if (value === undefined) continue;
|
|
190
|
+
props ??= { ...(node.props as Record<string, unknown>) };
|
|
191
|
+
props[field.key] = value;
|
|
192
|
+
// A bound image is no longer the media library item it was picked from.
|
|
193
|
+
if (field.kind === "image") delete props["mediaId"];
|
|
194
|
+
}
|
|
195
|
+
return props ? ({ ...node, props } as LayoutNode) : node;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Sets or clears one binding, dropping `bind` once it is empty. */
|
|
199
|
+
export function withBinding(
|
|
200
|
+
node: LayoutNode,
|
|
201
|
+
key: string,
|
|
202
|
+
binding: Binding | undefined,
|
|
203
|
+
): LayoutNode {
|
|
204
|
+
const next: Record<string, Binding> = { ...node.bind };
|
|
205
|
+
if (binding) next[key] = binding;
|
|
206
|
+
else delete next[key];
|
|
207
|
+
const { bind: _old, ...rest } = node;
|
|
208
|
+
return (Object.keys(next).length > 0 ? { ...rest, bind: next } : rest) as LayoutNode;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Search parameters as a plain record, first value per name, with names and values capped. */
|
|
212
|
+
export function paramsFromSearch(search: URLSearchParams): Record<string, string> {
|
|
213
|
+
const params: Record<string, string> = {};
|
|
214
|
+
let count = 0;
|
|
215
|
+
for (const [name, value] of search) {
|
|
216
|
+
if (count >= 50) break;
|
|
217
|
+
if (!/^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(name) || Object.hasOwn(params, name)) continue;
|
|
218
|
+
params[name] = value.slice(0, MAX_BOUND_LENGTH * 4);
|
|
219
|
+
count += 1;
|
|
220
|
+
}
|
|
221
|
+
return params;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Site settings as flat text for bindings (W-307): `title`, `tagline`, `url`, `social.github`… */
|
|
225
|
+
export function siteBindingValues(settings: unknown): Record<string, string> {
|
|
226
|
+
const out: Record<string, string> = {};
|
|
227
|
+
if (!settings || typeof settings !== "object") return out;
|
|
228
|
+
const add = (key: string, value: unknown) => {
|
|
229
|
+
if (typeof value === "string" && value.trim()) out[key] = value.trim();
|
|
230
|
+
else if (typeof value === "number" && Number.isFinite(value)) out[key] = String(value);
|
|
231
|
+
};
|
|
232
|
+
for (const [key, value] of Object.entries(settings as Record<string, unknown>)) {
|
|
233
|
+
if (key === "seo") continue;
|
|
234
|
+
if (value && typeof value === "object") {
|
|
235
|
+
if (key === "logo" || key === "favicon") {
|
|
236
|
+
const media = value as { src?: unknown; url?: unknown };
|
|
237
|
+
add(key, media.src ?? media.url);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
for (const [inner, innerValue] of Object.entries(value as Record<string, unknown>)) {
|
|
241
|
+
add(`${key}.${inner}`, innerValue);
|
|
242
|
+
}
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
add(key, value);
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Whether any node in the tree reads a source (hosts fetch site settings only when needed). */
|
|
251
|
+
export function layoutUsesSource(root: LayoutNode, source: BindSource): boolean {
|
|
252
|
+
return someNode(root, (node) =>
|
|
253
|
+
Boolean(node.bind && Object.values(node.bind).some((b) => b.source === source)),
|
|
254
|
+
);
|
|
255
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { DesignSystem } from "../schema/design.ts";
|
|
2
2
|
import type { Layout, LayoutNode } from "../schema/layout.ts";
|
|
3
3
|
import type { StyleProps } from "../schema/style.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { localStyleOf, withLocalStyle } from "../local-style.ts";
|
|
5
|
+
import { forEachNode, slugify, updateNode } from "../tree-ops.ts";
|
|
5
6
|
|
|
6
7
|
/** Reorder an applied class-id list. Out-of-range moves are no-ops. Pure (W-031). */
|
|
7
8
|
export function moveClassId(ids: readonly string[], index: number, delta: -1 | 1): string[] {
|
|
@@ -27,15 +28,10 @@ export function removeClassId(ids: readonly string[], id: string): string[] {
|
|
|
27
28
|
return ids.filter((entry) => entry !== id);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
function walk(node: LayoutNode, visit: (node: LayoutNode) => void): void {
|
|
31
|
-
visit(node);
|
|
32
|
-
for (const child of nodeChildren(node)) walk(child, visit);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
31
|
/** Nodes that list `classId` in `node.classes` (W-032 delete dialog). */
|
|
36
32
|
export function findClassUsages(layout: Layout, classId: string): { nodeId: string }[] {
|
|
37
33
|
const usages: { nodeId: string }[] = [];
|
|
38
|
-
|
|
34
|
+
forEachNode(layout.root, (node) => {
|
|
39
35
|
if (node.classes?.includes(classId)) usages.push({ nodeId: node.id });
|
|
40
36
|
});
|
|
41
37
|
return usages;
|
|
@@ -85,10 +81,11 @@ const filled = (style: object | undefined) =>
|
|
|
85
81
|
|
|
86
82
|
/** Whether an element has local styles: base, hover/focus/active or tablet/mobile (W-134). */
|
|
87
83
|
export function hasLocalStyles(node: LayoutNode): boolean {
|
|
84
|
+
const local = localStyleOf(node);
|
|
88
85
|
return (
|
|
89
|
-
filled(
|
|
90
|
-
Object.values(
|
|
91
|
-
Object.values(
|
|
86
|
+
filled(local.style) ||
|
|
87
|
+
Object.values(local.states ?? {}).some(filled) ||
|
|
88
|
+
Object.values(local.devices ?? {}).some(filled)
|
|
92
89
|
);
|
|
93
90
|
}
|
|
94
91
|
|
|
@@ -130,17 +127,18 @@ export function localStylesToClass(
|
|
|
130
127
|
const taken = new Set(classes.map((c) => c.id));
|
|
131
128
|
let id = base;
|
|
132
129
|
for (let n = 2; taken.has(id); n++) id = `${base}-${n}`;
|
|
130
|
+
const local = localStyleOf(node);
|
|
133
131
|
const cls = {
|
|
134
132
|
id,
|
|
135
133
|
name: trimmed,
|
|
136
|
-
style:
|
|
137
|
-
...(
|
|
138
|
-
...(
|
|
134
|
+
style: (local.style ?? {}) as StyleProps,
|
|
135
|
+
...(local.states ? { states: local.states } : {}),
|
|
136
|
+
...(local.devices ? { devices: local.devices } : {}),
|
|
139
137
|
};
|
|
140
|
-
const
|
|
138
|
+
const cleared = withLocalStyle(node, local.hiddenOn ? { hiddenOn: local.hiddenOn } : {});
|
|
141
139
|
return {
|
|
142
140
|
design: { ...design, classes: [...classes, cls] },
|
|
143
|
-
node: { ...
|
|
141
|
+
node: { ...cleared, classes: addClassId(node.classes ?? [], id) },
|
|
144
142
|
classId: id,
|
|
145
143
|
};
|
|
146
144
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { DesignSystem } from "../schema/design.ts";
|
|
2
|
+
|
|
3
|
+
export type DesignChange = {
|
|
4
|
+
kind: "added" | "removed" | "changed";
|
|
5
|
+
what: "variable" | "class" | "setting";
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
|
|
10
|
+
|
|
11
|
+
/** What Publish styles will write: draft compared with the published design (W-340). */
|
|
12
|
+
export function designChanges(published: DesignSystem, draft: DesignSystem): DesignChange[] {
|
|
13
|
+
const changes: DesignChange[] = [];
|
|
14
|
+
listChanges(published.variables.colors, draft.variables.colors, "variable", changes);
|
|
15
|
+
listChanges(published.variables.fonts, draft.variables.fonts, "variable", changes);
|
|
16
|
+
listChanges(published.variables.fontSizes, draft.variables.fontSizes, "variable", changes);
|
|
17
|
+
listChanges(published.variables.spacings, draft.variables.spacings, "variable", changes);
|
|
18
|
+
listChanges(published.classes, draft.classes, "class", changes);
|
|
19
|
+
if (!same(published.direction, draft.direction)) {
|
|
20
|
+
changes.push({ kind: "changed", what: "setting", name: "Direction" });
|
|
21
|
+
}
|
|
22
|
+
if (!same(published.fluidRange, draft.fluidRange)) {
|
|
23
|
+
changes.push({ kind: "changed", what: "setting", name: "Fluid range" });
|
|
24
|
+
}
|
|
25
|
+
if (!same(published.defaults, draft.defaults)) {
|
|
26
|
+
changes.push({ kind: "changed", what: "setting", name: "Tag defaults" });
|
|
27
|
+
}
|
|
28
|
+
return changes;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function designChangeLabel(change: DesignChange): string {
|
|
32
|
+
const noun =
|
|
33
|
+
change.what === "class" ? "Class" : change.what === "setting" ? "Setting" : "Variable";
|
|
34
|
+
if (change.kind === "added") return `Add ${noun.toLowerCase()} ${change.name}`;
|
|
35
|
+
if (change.kind === "removed") return `Remove ${noun.toLowerCase()} ${change.name}`;
|
|
36
|
+
return `Change ${noun.toLowerCase()} ${change.name}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function listChanges<T extends { id: string; name: string }>(
|
|
40
|
+
published: T[] | undefined,
|
|
41
|
+
draft: T[] | undefined,
|
|
42
|
+
what: "variable" | "class",
|
|
43
|
+
into: DesignChange[],
|
|
44
|
+
) {
|
|
45
|
+
const from = new Map((published ?? []).map((item) => [item.id, item]));
|
|
46
|
+
const to = new Map((draft ?? []).map((item) => [item.id, item]));
|
|
47
|
+
for (const item of draft ?? []) {
|
|
48
|
+
const before = from.get(item.id);
|
|
49
|
+
if (!before) into.push({ kind: "added", what, name: item.name });
|
|
50
|
+
else if (!same(before, item)) into.push({ kind: "changed", what, name: item.name });
|
|
51
|
+
}
|
|
52
|
+
for (const item of published ?? []) {
|
|
53
|
+
if (!to.has(item.id)) into.push({ kind: "removed", what, name: item.name });
|
|
54
|
+
}
|
|
55
|
+
}
|