@perkyzz/emvb 0.7.0 → 0.8.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 +2 -2
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/package.json +7 -7
- package/src/admin/editor/DialogCloseButton.tsx +16 -0
- package/src/admin/editor/Editor.tsx +33 -0
- package/src/admin/editor/PerfMeter.tsx +3 -9
- package/src/admin/editor/SavedSectionDialogs.tsx +169 -0
- package/src/admin/editor/ShareLink.tsx +2 -7
- package/src/admin/editor/ShortcutsDialog.tsx +19 -11
- package/src/admin/editor/Timeline.tsx +2 -7
- package/src/admin/editor/dialogs.tsx +3 -14
- package/src/admin/editor/editor-chrome.tsx +8 -24
- package/src/admin/editor/editor-commands.ts +15 -1
- package/src/admin/editor/editor-css.ts +3 -2
- package/src/admin/editor/feature-css.ts +11 -1
- package/src/admin/editor/focus-when-lost.ts +23 -0
- package/src/admin/editor/icons/IconLibraryDialog.tsx +2 -7
- package/src/admin/editor/palette-items.ts +17 -0
- package/src/admin/editor/panels/AddPanel.tsx +9 -1
- package/src/admin/editor/panels/LayersPanel.tsx +6 -1
- package/src/admin/editor/panels/LeftPanel.tsx +9 -1
- package/src/admin/editor/panels/SavedSectionsGroup.tsx +246 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +54 -10
- package/src/admin/editor/panels/TokenScales.tsx +16 -16
- package/src/admin/editor/panels/TriggersEditor.tsx +1 -1
- package/src/admin/editor/panels/color-space.ts +14 -26
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +36 -39
- package/src/admin/editor/panels/settings/IconControls.tsx +21 -5
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
- package/src/admin/editor/shortcuts.ts +14 -0
- package/src/admin/editor/useSavedSections.ts +197 -0
- package/src/admin/image-editor/Canvas.tsx +61 -17
- package/src/admin/image-editor/ImageEditorDialog.tsx +130 -1
- package/src/admin/image-editor/image-editor-css.ts +11 -8
- package/src/admin/image-editor/parts.tsx +18 -17
- package/src/admin/image-editor/save.ts +31 -1
- package/src/admin/pages/ThemePartList.tsx +32 -4
- package/src/admin/saved-sections-api.ts +51 -0
- package/src/admin/theme-api.ts +3 -2
- package/src/admin/ui.ts +9 -0
- package/src/astro/popups/runtime.ts +8 -6
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +32 -7
- package/src/core/arrange.ts +21 -6
- package/src/core/css/print.ts +5 -0
- package/src/core/design/classes.ts +23 -9
- package/src/core/design/devices.ts +7 -13
- package/src/core/design/states.ts +7 -13
- package/src/core/elements/dynamic-elements.ts +11 -11
- package/src/core/elements/index.ts +1 -1
- package/src/core/elements/menu.ts +3 -0
- package/src/core/icons/animation-runs.ts +11 -0
- package/src/core/index.ts +20 -0
- package/src/core/migrate/index.ts +3 -0
- package/src/core/render/loop.ts +1 -1
- package/src/core/sanitize/css.ts +14 -6
- package/src/core/saved-sections.ts +318 -0
- package/src/core/schema/design.ts +1 -1
- package/src/core/schema/layout.ts +1 -1
- package/src/core/schema/style.ts +4 -2
- package/src/core/theme/float.ts +3 -0
- package/src/core/theme/pagination.ts +25 -24
- package/src/core/theme/part-types.ts +4 -4
- package/src/mcp/ports.ts +22 -1
- package/src/mcp/read.ts +1 -1
- package/src/mcp/sections.ts +183 -0
- package/src/mcp/tools.ts +44 -0
- package/src/mcp/write.ts +4 -4
- package/src/server/mcp-routes.ts +2 -0
- package/src/server/plugin.ts +14 -0
- package/src/server/saved-sections.ts +163 -0
|
@@ -207,25 +207,39 @@ export function renameClass(design: DesignSystem, classId: string, name: string)
|
|
|
207
207
|
};
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
|
|
211
|
-
|
|
210
|
+
type DesignClass = NonNullable<DesignSystem["classes"]>[number];
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Replace one class with `edit(copy)`; the design comes back unchanged when no class has that id.
|
|
214
|
+
* The copy is shallow, so `edit` may set or delete keys on it. Pure.
|
|
215
|
+
*/
|
|
216
|
+
export function editClass(
|
|
212
217
|
design: DesignSystem,
|
|
213
218
|
classId: string,
|
|
214
|
-
|
|
219
|
+
edit: (copy: DesignClass) => DesignClass,
|
|
215
220
|
): DesignSystem {
|
|
216
221
|
const classes = design.classes ?? [];
|
|
217
222
|
if (!classes.some((c) => c.id === classId)) return design;
|
|
218
223
|
return {
|
|
219
224
|
...design,
|
|
220
|
-
classes: classes.map((c) => {
|
|
221
|
-
if (c.id !== classId) return c;
|
|
222
|
-
const style: Record<string, unknown> = { ...c.style, ...patch };
|
|
223
|
-
for (const [key, value] of Object.entries(patch)) if (value === undefined) delete style[key];
|
|
224
|
-
return Object.assign({}, c, { style: style as StyleProps });
|
|
225
|
-
}),
|
|
225
|
+
classes: classes.map((c) => (c.id === classId ? edit(Object.assign({}, c)) : c)),
|
|
226
226
|
};
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
+
/** Merge `patch` into a class's style; `undefined` values clear their property. Pure (W-087). */
|
|
230
|
+
export function patchClassStyle(
|
|
231
|
+
design: DesignSystem,
|
|
232
|
+
classId: string,
|
|
233
|
+
patch: Partial<StyleProps>,
|
|
234
|
+
): DesignSystem {
|
|
235
|
+
return editClass(design, classId, (next) => {
|
|
236
|
+
const style: Record<string, unknown> = { ...next.style, ...patch };
|
|
237
|
+
for (const [key, value] of Object.entries(patch)) if (value === undefined) delete style[key];
|
|
238
|
+
next.style = style as StyleProps;
|
|
239
|
+
return next;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
229
243
|
/** Swap one applied class id for another in place, keeping the cascade order. Pure (W-087). */
|
|
230
244
|
export function replaceClassId(ids: readonly string[], from: string, to: string): string[] {
|
|
231
245
|
if (ids.includes(to)) return removeClassId(ids, from);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { DesignSystem } from "../schema/design.ts";
|
|
2
|
+
import { editClass } from "./classes.ts";
|
|
2
3
|
import type { DeviceStyles, StyleProps } from "../schema/style.ts";
|
|
3
4
|
import { POPUP_DEVICES, type PopupDevice } from "../theme/popup-rules.ts";
|
|
4
5
|
|
|
@@ -28,19 +29,12 @@ export function patchClassDevices(
|
|
|
28
29
|
device: ResponsiveDevice,
|
|
29
30
|
patch: Partial<StyleProps>,
|
|
30
31
|
): DesignSystem {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
};
|
|
32
|
+
return editClass(design, classId, (next) => {
|
|
33
|
+
const devices = patchDeviceStyle(next.devices, device, patch);
|
|
34
|
+
if (devices) next.devices = devices;
|
|
35
|
+
else delete next.devices;
|
|
36
|
+
return next;
|
|
37
|
+
});
|
|
44
38
|
}
|
|
45
39
|
|
|
46
40
|
/** Add or remove one device. The result stays in desktop, tablet, mobile order, or undefined. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { DesignSystem } from "../schema/design.ts";
|
|
2
|
+
import { editClass } from "./classes.ts";
|
|
2
3
|
import { STYLE_STATES, type StyleStateName } from "../schema/state-names.ts";
|
|
3
4
|
import type { StyleProps, StyleStates } from "../schema/style.ts";
|
|
4
5
|
|
|
@@ -33,17 +34,10 @@ export function patchClassState(
|
|
|
33
34
|
state: StyleStateName,
|
|
34
35
|
patch: Partial<StyleProps>,
|
|
35
36
|
): DesignSystem {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
};
|
|
37
|
+
return editClass(design, classId, (next) => {
|
|
38
|
+
const states = patchStates(next.states, state, patch);
|
|
39
|
+
if (states) next.states = states;
|
|
40
|
+
else delete next.states;
|
|
41
|
+
return next;
|
|
42
|
+
});
|
|
49
43
|
}
|
|
@@ -18,7 +18,7 @@ export const postTitle: ElementDefinition<PostTitleNode> = {
|
|
|
18
18
|
defaults: () => ({ type: "post-title", props: { level: 1 } }),
|
|
19
19
|
descriptor: {
|
|
20
20
|
type: "post-title",
|
|
21
|
-
name: "Post
|
|
21
|
+
name: "Post title",
|
|
22
22
|
group: "dynamic",
|
|
23
23
|
defaultTab: "content",
|
|
24
24
|
fields: [
|
|
@@ -40,7 +40,7 @@ export const postExcerpt: ElementDefinition<PostExcerptNode> = {
|
|
|
40
40
|
defaults: () => ({ type: "post-excerpt", props: {} }),
|
|
41
41
|
descriptor: {
|
|
42
42
|
type: "post-excerpt",
|
|
43
|
-
name: "Post
|
|
43
|
+
name: "Post excerpt",
|
|
44
44
|
group: "dynamic",
|
|
45
45
|
defaultTab: "style",
|
|
46
46
|
fields: [
|
|
@@ -61,7 +61,7 @@ export const postContent: ElementDefinition<PostContentNode> = {
|
|
|
61
61
|
defaults: () => ({ type: "post-content", props: {} }),
|
|
62
62
|
descriptor: {
|
|
63
63
|
type: "post-content",
|
|
64
|
-
name: "Post
|
|
64
|
+
name: "Post content",
|
|
65
65
|
group: "dynamic",
|
|
66
66
|
defaultTab: "style",
|
|
67
67
|
fields: [],
|
|
@@ -78,7 +78,7 @@ export const postImage: ElementDefinition<PostImageNode> = {
|
|
|
78
78
|
defaults: () => ({ type: "post-image", props: { decorative: false } }),
|
|
79
79
|
descriptor: {
|
|
80
80
|
type: "post-image",
|
|
81
|
-
name: "Post
|
|
81
|
+
name: "Post image",
|
|
82
82
|
group: "dynamic",
|
|
83
83
|
defaultTab: "content",
|
|
84
84
|
fields: [{ key: "decorative", kind: "boolean", label: "Decorative", optional: true }],
|
|
@@ -95,7 +95,7 @@ export const postDate: ElementDefinition<PostDateNode> = {
|
|
|
95
95
|
defaults: () => ({ type: "post-date", props: {} }),
|
|
96
96
|
descriptor: {
|
|
97
97
|
type: "post-date",
|
|
98
|
-
name: "Post
|
|
98
|
+
name: "Post date",
|
|
99
99
|
group: "dynamic",
|
|
100
100
|
defaultTab: "style",
|
|
101
101
|
fields: [
|
|
@@ -122,7 +122,7 @@ export const postAuthor: ElementDefinition<PostAuthorNode> = {
|
|
|
122
122
|
defaults: () => ({ type: "post-author", props: {} }),
|
|
123
123
|
descriptor: {
|
|
124
124
|
type: "post-author",
|
|
125
|
-
name: "Post
|
|
125
|
+
name: "Post author",
|
|
126
126
|
group: "dynamic",
|
|
127
127
|
defaultTab: "style",
|
|
128
128
|
fields: [],
|
|
@@ -135,7 +135,7 @@ export const postLink: ElementDefinition<PostLinkNode> = {
|
|
|
135
135
|
defaults: () => ({ type: "post-link", props: {} }),
|
|
136
136
|
descriptor: {
|
|
137
137
|
type: "post-link",
|
|
138
|
-
name: "Post
|
|
138
|
+
name: "Post link",
|
|
139
139
|
group: "dynamic",
|
|
140
140
|
defaultTab: "content",
|
|
141
141
|
fields: [
|
|
@@ -230,14 +230,14 @@ export const loop: ElementDefinition<LoopNode> = {
|
|
|
230
230
|
label: "Image field",
|
|
231
231
|
optional: true,
|
|
232
232
|
message:
|
|
233
|
-
"The field Post
|
|
233
|
+
"The field Post image shows, such as photo. Leave it empty for the featured image.",
|
|
234
234
|
},
|
|
235
235
|
{
|
|
236
236
|
key: "excerptField",
|
|
237
237
|
kind: "text",
|
|
238
238
|
label: "Excerpt field",
|
|
239
239
|
optional: true,
|
|
240
|
-
message: "The field Post
|
|
240
|
+
message: "The field Post excerpt shows, such as role. Leave it empty for the excerpt.",
|
|
241
241
|
},
|
|
242
242
|
{
|
|
243
243
|
key: "filter",
|
|
@@ -250,10 +250,10 @@ export const loop: ElementDefinition<LoopNode> = {
|
|
|
250
250
|
{
|
|
251
251
|
key: "itemPartId",
|
|
252
252
|
kind: "text",
|
|
253
|
-
label: "Loop
|
|
253
|
+
label: "Loop item",
|
|
254
254
|
optional: true,
|
|
255
255
|
message:
|
|
256
|
-
"Optional published Loop
|
|
256
|
+
"Optional published Loop item theme part. Leave blank to use nested elements as the item template.",
|
|
257
257
|
},
|
|
258
258
|
{
|
|
259
259
|
key: "perPage",
|
|
@@ -962,7 +962,7 @@ const divBlock: ElementDefinition<DivBlockNode> = {
|
|
|
962
962
|
defaults: () => ({ type: "div-block", props: {}, children: [] }),
|
|
963
963
|
descriptor: {
|
|
964
964
|
type: "div-block",
|
|
965
|
-
name: "Div
|
|
965
|
+
name: "Div block",
|
|
966
966
|
group: "layout",
|
|
967
967
|
defaultTab: "style",
|
|
968
968
|
fields: BOX_LINK_FIELDS,
|
|
@@ -86,6 +86,9 @@ const MENU_CSS = [
|
|
|
86
86
|
".emvb-menu--column .emvb-menu-item__bar{position:relative;width:fit-content;max-width:100%}",
|
|
87
87
|
".emvb-menu--column .emvb-menu-panel{top:0;inset-inline-start:100%}",
|
|
88
88
|
".emvb-menu--column .emvb-menu-panel--wide{inset-inline:auto;inset-inline-start:100%;min-width:18rem}",
|
|
89
|
+
// W-847 (D-056): on a phone there is no room beside the item (the flyout overflowed by about
|
|
90
|
+
// 40-50 px), so a column flyout opens below its item instead, no wider than the screen.
|
|
91
|
+
"@media (max-width: 767.98px){.emvb-menu--column .emvb-menu-panel,.emvb-menu--column .emvb-menu-panel--wide{top:100%;inset-inline:auto;inset-inline-start:0;min-width:min(12rem,calc(100vw - 2rem));max-width:calc(100vw - 2rem)}}",
|
|
89
92
|
].join("");
|
|
90
93
|
|
|
91
94
|
export const menu: ElementDefinition<MenuNode> = {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-848 (D-057): a spin or pulse stops on its own after about 5 seconds (WCAG 2.2.2), so it repeats
|
|
3
|
+
* as many whole times as fit. A pulse alternates, so it runs an even number of times and ends at
|
|
4
|
+
* rest. One spin longer than 5 s, or one pulse there and back longer than 5 s, still runs once.
|
|
5
|
+
*/
|
|
6
|
+
export const ICON_ANIMATION_LIMIT_MS = 5000;
|
|
7
|
+
|
|
8
|
+
export function iconAnimationRuns(type: "spin" | "pulse", durationMs: number): number {
|
|
9
|
+
if (type === "spin") return Math.max(1, Math.floor(ICON_ANIMATION_LIMIT_MS / durationMs));
|
|
10
|
+
return 2 * Math.max(1, Math.floor(ICON_ANIMATION_LIMIT_MS / (2 * durationMs)));
|
|
11
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -272,6 +272,7 @@ export {
|
|
|
272
272
|
canDrop,
|
|
273
273
|
duplicateNode,
|
|
274
274
|
wrapNode,
|
|
275
|
+
wrapInThemeSection,
|
|
275
276
|
WRAP_TYPES,
|
|
276
277
|
type WrapType,
|
|
277
278
|
createVariantB,
|
|
@@ -527,3 +528,22 @@ export {
|
|
|
527
528
|
} from "./image-edit/schema.ts";
|
|
528
529
|
export { summarizeImageEdit } from "./image-edit/summary.ts";
|
|
529
530
|
export { boxLinkHeldBack } from "./render/extras.ts";
|
|
531
|
+
export {
|
|
532
|
+
classMergeNotice,
|
|
533
|
+
insertSavedSection,
|
|
534
|
+
MAX_SAVED_SECTIONS,
|
|
535
|
+
mergeSectionClasses,
|
|
536
|
+
prepareSavedSection,
|
|
537
|
+
readSavedSection,
|
|
538
|
+
SAVED_SECTION_NAME_MAX,
|
|
539
|
+
SAVED_SECTION_REASONS,
|
|
540
|
+
savedSectionName,
|
|
541
|
+
savedSectionRecord,
|
|
542
|
+
snapshotSection,
|
|
543
|
+
type ClassMerge,
|
|
544
|
+
type PreparedSection,
|
|
545
|
+
type SavedSection,
|
|
546
|
+
type SavedSectionRecord,
|
|
547
|
+
type SavedSectionSummary,
|
|
548
|
+
syncedPartLayout,
|
|
549
|
+
} from "./saved-sections.ts";
|
|
@@ -59,6 +59,7 @@ function walkGradients(value: unknown): unknown {
|
|
|
59
59
|
|
|
60
60
|
const gradients: Migration = (doc) => walkGradients(doc) as Record<string, unknown>;
|
|
61
61
|
|
|
62
|
+
/** 0.8.0 (W-885, D-031, D-059): layout 16 -> 17 and design 15 -> 16 add only the optional `loop`. */
|
|
62
63
|
/** `LAYOUT_MIGRATIONS[n]` upgrades a version-n layout to version n + 1. Migrations must be pure. */
|
|
63
64
|
export const LAYOUT_MIGRATIONS: Readonly<Record<number, Migration>> = {
|
|
64
65
|
1: unchanged,
|
|
@@ -76,6 +77,7 @@ export const LAYOUT_MIGRATIONS: Readonly<Record<number, Migration>> = {
|
|
|
76
77
|
13: unchanged,
|
|
77
78
|
14: unchanged,
|
|
78
79
|
15: unchanged,
|
|
80
|
+
16: unchanged,
|
|
79
81
|
};
|
|
80
82
|
|
|
81
83
|
/** `DESIGN_MIGRATIONS[n]` upgrades a version-n design document to version n + 1. */
|
|
@@ -94,6 +96,7 @@ export const DESIGN_MIGRATIONS: Readonly<Record<number, Migration>> = {
|
|
|
94
96
|
12: unchanged,
|
|
95
97
|
13: unchanged,
|
|
96
98
|
14: unchanged,
|
|
99
|
+
15: unchanged,
|
|
97
100
|
};
|
|
98
101
|
|
|
99
102
|
export type UpgradeResult =
|
package/src/core/render/loop.ts
CHANGED
|
@@ -94,7 +94,7 @@ export function renderLoop(
|
|
|
94
94
|
attrs: { ...attrs, "data-emvb-loop-empty": "" },
|
|
95
95
|
children: [
|
|
96
96
|
...ctx.children(emptyNodes, rest),
|
|
97
|
-
"Choose a Loop
|
|
97
|
+
"Choose a Loop item, or add elements here to design the item.",
|
|
98
98
|
],
|
|
99
99
|
};
|
|
100
100
|
}
|
package/src/core/sanitize/css.ts
CHANGED
|
@@ -24,6 +24,7 @@ import type { StyleProps } from "../schema/style.ts";
|
|
|
24
24
|
import { sanitizeMediaUrl } from "./media-url.ts";
|
|
25
25
|
import { motionDeclarations, withMotion } from "./motion.ts";
|
|
26
26
|
import { isRecord } from "../record.ts";
|
|
27
|
+
import { iconAnimationRuns } from "../icons/animation-runs.ts";
|
|
27
28
|
|
|
28
29
|
const setOf = (values: readonly string[]) => new Set<string>(values);
|
|
29
30
|
const VAR_ID = /^[a-z0-9-]{1,40}$/;
|
|
@@ -379,20 +380,27 @@ function cssIconShadow(value: unknown): string | undefined {
|
|
|
379
380
|
}
|
|
380
381
|
|
|
381
382
|
const ICON_ANIMATIONS: Readonly<Record<string, string>> = {
|
|
382
|
-
spin: "emvb-icon-spin {ms} linear
|
|
383
|
-
pulse: "emvb-icon-pulse {ms} ease-in-out
|
|
383
|
+
spin: "emvb-icon-spin {ms} linear {runs}",
|
|
384
|
+
pulse: "emvb-icon-pulse {ms} ease-in-out {runs} alternate",
|
|
384
385
|
};
|
|
385
386
|
|
|
386
|
-
/**
|
|
387
|
+
/**
|
|
388
|
+
* W-237: a spin or pulse; the Icon's base CSS turns it off for reduced motion. W-848: it repeats
|
|
389
|
+
* only for about 5 seconds (`iconAnimationRuns`), unless it loops forever (W-853).
|
|
390
|
+
*/
|
|
387
391
|
function cssIconAnimation(value: unknown): string | undefined {
|
|
388
392
|
if (!isRecord(value)) return undefined;
|
|
389
|
-
if (Object.keys(value).some((key) => key !== "type" && key !== "duration"))
|
|
390
|
-
|
|
393
|
+
if (Object.keys(value).some((key) => key !== "type" && key !== "duration" && key !== "loop"))
|
|
394
|
+
return undefined;
|
|
395
|
+
const { type, duration, loop } = value;
|
|
396
|
+
if (loop !== undefined && (loop !== true || type === "none")) return undefined;
|
|
391
397
|
// W-248: stops an animation inherited from a wider device.
|
|
392
398
|
if (type === "none") return duration === undefined ? "none" : undefined;
|
|
393
399
|
if (typeof type !== "string" || !Object.hasOwn(ICON_ANIMATIONS, type)) return undefined;
|
|
394
400
|
if (!Number.isInteger(duration) || !inRangeNumber(duration, 200, 10_000)) return undefined;
|
|
395
|
-
|
|
401
|
+
// W-853 (D-059): Loop forever keeps a spinner going, as before W-848.
|
|
402
|
+
const runs = loop ? "infinite" : iconAnimationRuns(type === "spin" ? "spin" : "pulse", duration);
|
|
403
|
+
return ICON_ANIMATIONS[type]?.replace("{ms}", `${duration}ms`).replace("{runs}", String(runs));
|
|
396
404
|
}
|
|
397
405
|
|
|
398
406
|
export type Declaration = { property: string; value: string };
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { addSectionNear, REASONS, withFreshIds, type Added, type Refusal } from "./arrange.ts";
|
|
2
|
+
import { prepareElement, prepareStyle, type Dropped } from "./clipboard.ts";
|
|
3
|
+
import { cleanClassName, MAX_CLASSES, nameKey } from "./design/classes.ts";
|
|
4
|
+
import { clipText, MAX_LAYOUT_BYTES } from "./limits.ts";
|
|
5
|
+
import { isRecord } from "./record.ts";
|
|
6
|
+
import { DESIGN_SCHEMA_VERSION, type DesignSystem } from "./schema/design.ts";
|
|
7
|
+
import { LAYOUT_SCHEMA_VERSION, type Layout, type LayoutNode } from "./schema/layout.ts";
|
|
8
|
+
import { findNode, newNodeId, nodeChildren, slugify } from "./tree-ops.ts";
|
|
9
|
+
import { byteLength, summarizeIssues, validateDesign, validateLayout } from "./validate.ts";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Saved sections (W-874, D-060): a named copy of an element and its children, kept per site so it
|
|
13
|
+
* can be inserted on any page. Each save carries a snapshot of the classes it uses, so it keeps
|
|
14
|
+
* its look on insert. Old saves are upgraded when read, through the same layout and design
|
|
15
|
+
* migrations as pages and site styles.
|
|
16
|
+
*/
|
|
17
|
+
const SAVED_SECTION_VERSION = 1;
|
|
18
|
+
export const SAVED_SECTION_NAME_MAX = 60;
|
|
19
|
+
export const MAX_SAVED_SECTIONS = 200;
|
|
20
|
+
|
|
21
|
+
type StyleClass = NonNullable<DesignSystem["classes"]>[number];
|
|
22
|
+
|
|
23
|
+
/** What storage holds for one saved section. */
|
|
24
|
+
export type SavedSectionRecord = {
|
|
25
|
+
version: typeof SAVED_SECTION_VERSION;
|
|
26
|
+
name: string;
|
|
27
|
+
layout: Layout;
|
|
28
|
+
designSchemaVersion: number;
|
|
29
|
+
classes: StyleClass[];
|
|
30
|
+
createdAt: string;
|
|
31
|
+
updatedAt: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** One saved section in a list (W-875). `damaged` rows can only be deleted. */
|
|
35
|
+
export type SavedSectionSummary = {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string;
|
|
38
|
+
updatedAt: string;
|
|
39
|
+
classCount: number;
|
|
40
|
+
damaged?: true;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** A saved section after it has been read, checked and upgraded. */
|
|
44
|
+
export type SavedSection = {
|
|
45
|
+
name: string;
|
|
46
|
+
node: LayoutNode;
|
|
47
|
+
classes: StyleClass[];
|
|
48
|
+
createdAt: string;
|
|
49
|
+
updatedAt: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const SAVED_SECTION_REASONS = {
|
|
53
|
+
name: "Give the section a name.",
|
|
54
|
+
root: "Choose an element inside the page; the page itself can't be saved as a section.",
|
|
55
|
+
missing: REASONS.missing,
|
|
56
|
+
tooBig: "This section is too big to save.",
|
|
57
|
+
damaged: "This saved section can't be read.",
|
|
58
|
+
tooManyClasses: `Inserting this section would go over ${MAX_CLASSES} classes on this site.`,
|
|
59
|
+
} as const;
|
|
60
|
+
|
|
61
|
+
const refuse = (reason: string): Refusal => ({ ok: false, reason });
|
|
62
|
+
const WRAPPER_ID = "savedsection";
|
|
63
|
+
|
|
64
|
+
/** The name as stored: cleaned like a class name, at most 60 characters; "" when blank. */
|
|
65
|
+
export const savedSectionName = (raw: string): string =>
|
|
66
|
+
clipText(cleanClassName(raw), SAVED_SECTION_NAME_MAX).trim();
|
|
67
|
+
|
|
68
|
+
/** A copy without A/B test arms: a saved copy is never part of the original's test (as W-629). */
|
|
69
|
+
function withoutVariants(node: LayoutNode): LayoutNode {
|
|
70
|
+
const { variant: _variant, ...rest } = node as LayoutNode & { variant?: unknown };
|
|
71
|
+
const kids = nodeChildren(node);
|
|
72
|
+
if (kids.length === 0) return rest as LayoutNode;
|
|
73
|
+
return { ...rest, children: kids.map(withoutVariants) } as LayoutNode;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function classIdsIn(node: LayoutNode, into = new Set<string>()): Set<string> {
|
|
77
|
+
for (const id of node.classes ?? []) into.add(id);
|
|
78
|
+
for (const child of nodeChildren(node)) classIdsIn(child, into);
|
|
79
|
+
return into;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const wrap = (node: LayoutNode): Layout => ({
|
|
83
|
+
schemaVersion: LAYOUT_SCHEMA_VERSION,
|
|
84
|
+
root: { id: WRAPPER_ID, type: "container", props: {}, children: [node] },
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
/** The record storage holds for a section that has been read (an old save comes back upgraded). */
|
|
88
|
+
export const savedSectionRecord = (section: SavedSection): SavedSectionRecord => ({
|
|
89
|
+
version: SAVED_SECTION_VERSION,
|
|
90
|
+
name: section.name,
|
|
91
|
+
layout: wrap(section.node),
|
|
92
|
+
designSchemaVersion: DESIGN_SCHEMA_VERSION,
|
|
93
|
+
classes: section.classes,
|
|
94
|
+
createdAt: section.createdAt,
|
|
95
|
+
updatedAt: section.updatedAt,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/** A record for `nodeId` with the classes it uses, or why it can't be saved. */
|
|
99
|
+
export function snapshotSection(
|
|
100
|
+
layout: Layout,
|
|
101
|
+
nodeId: string,
|
|
102
|
+
design: DesignSystem,
|
|
103
|
+
rawName: string,
|
|
104
|
+
now: string,
|
|
105
|
+
): { ok: true; record: SavedSectionRecord } | Refusal {
|
|
106
|
+
const name = savedSectionName(rawName);
|
|
107
|
+
if (!name) return refuse(SAVED_SECTION_REASONS.name);
|
|
108
|
+
if (nodeId === layout.root.id) return refuse(SAVED_SECTION_REASONS.root);
|
|
109
|
+
const found = findNode(layout, nodeId);
|
|
110
|
+
if (!found) return refuse(SAVED_SECTION_REASONS.missing);
|
|
111
|
+
const node = withoutVariants(structuredClone(found));
|
|
112
|
+
const used = classIdsIn(node);
|
|
113
|
+
const classes = (design.classes ?? [])
|
|
114
|
+
.filter((c) => used.has(c.id))
|
|
115
|
+
.map((c) => structuredClone(c));
|
|
116
|
+
const record: SavedSectionRecord = {
|
|
117
|
+
version: SAVED_SECTION_VERSION,
|
|
118
|
+
name,
|
|
119
|
+
layout: wrap(node),
|
|
120
|
+
designSchemaVersion: DESIGN_SCHEMA_VERSION,
|
|
121
|
+
classes,
|
|
122
|
+
createdAt: now,
|
|
123
|
+
updatedAt: now,
|
|
124
|
+
};
|
|
125
|
+
if (byteLength(record) > MAX_LAYOUT_BYTES) return refuse(SAVED_SECTION_REASONS.tooBig);
|
|
126
|
+
const checked = validateLayout(record.layout);
|
|
127
|
+
if (!checked.ok) return refuse(summarizeIssues(checked.issues));
|
|
128
|
+
return { ok: true, record };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A stored record, checked and upgraded: the layout through the layout migrations, the classes
|
|
133
|
+
* through the design migrations. Anything that doesn't pass is refused, never half-used.
|
|
134
|
+
*/
|
|
135
|
+
export function readSavedSection(raw: unknown): { ok: true; section: SavedSection } | Refusal {
|
|
136
|
+
if (!isRecord(raw) || raw["version"] !== SAVED_SECTION_VERSION) {
|
|
137
|
+
return refuse(SAVED_SECTION_REASONS.damaged);
|
|
138
|
+
}
|
|
139
|
+
const name = typeof raw["name"] === "string" ? savedSectionName(raw["name"]) : "";
|
|
140
|
+
const createdAt = typeof raw["createdAt"] === "string" ? raw["createdAt"] : "";
|
|
141
|
+
const updatedAt = typeof raw["updatedAt"] === "string" ? raw["updatedAt"] : createdAt;
|
|
142
|
+
if (!name || !createdAt) return refuse(SAVED_SECTION_REASONS.damaged);
|
|
143
|
+
const layout = validateLayout(raw["layout"]);
|
|
144
|
+
if (!layout.ok) return refuse(SAVED_SECTION_REASONS.damaged);
|
|
145
|
+
const node = layout.layout.root.children[0];
|
|
146
|
+
if (!node || layout.layout.root.children.length !== 1) {
|
|
147
|
+
return refuse(SAVED_SECTION_REASONS.damaged);
|
|
148
|
+
}
|
|
149
|
+
const version = raw["designSchemaVersion"];
|
|
150
|
+
const design = validateDesign({
|
|
151
|
+
schemaVersion: typeof version === "number" ? version : DESIGN_SCHEMA_VERSION,
|
|
152
|
+
variables: { colors: [] },
|
|
153
|
+
classes: Array.isArray(raw["classes"]) ? raw["classes"] : [],
|
|
154
|
+
});
|
|
155
|
+
if (!design.ok) return refuse(SAVED_SECTION_REASONS.damaged);
|
|
156
|
+
return {
|
|
157
|
+
ok: true,
|
|
158
|
+
section: { name, node, classes: design.design.classes ?? [], createdAt, updatedAt },
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A class's look, in one comparable form (key order doesn't matter). */
|
|
163
|
+
function lookOf(cls: StyleClass): string {
|
|
164
|
+
const sorted = (value: unknown): unknown =>
|
|
165
|
+
Array.isArray(value)
|
|
166
|
+
? value.map(sorted)
|
|
167
|
+
: isRecord(value)
|
|
168
|
+
? Object.fromEntries(
|
|
169
|
+
Object.keys(value)
|
|
170
|
+
.toSorted()
|
|
171
|
+
.map((key) => [key, sorted(value[key])]),
|
|
172
|
+
)
|
|
173
|
+
: value;
|
|
174
|
+
const { id: _id, name: _name, ...look } = cls;
|
|
175
|
+
return JSON.stringify(sorted(look));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export type ClassMerge = {
|
|
179
|
+
design: DesignSystem;
|
|
180
|
+
/** Saved class id → the id it has on this site. */
|
|
181
|
+
ids: Map<string, string>;
|
|
182
|
+
added: string[];
|
|
183
|
+
reused: string[];
|
|
184
|
+
renamed: { from: string; to: string }[];
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Brings a saved section's classes onto this site (D-060). Never changes a class that is already
|
|
189
|
+
* here: a class with the same id or name and the same look is reused; one whose look differs is
|
|
190
|
+
* added as a copy with a numbered name ("Card 2") and a new id; a missing one is added.
|
|
191
|
+
*/
|
|
192
|
+
export function mergeSectionClasses(
|
|
193
|
+
design: DesignSystem,
|
|
194
|
+
incoming: readonly StyleClass[],
|
|
195
|
+
): ClassMerge | Refusal {
|
|
196
|
+
const classes = [...(design.classes ?? [])];
|
|
197
|
+
const merge: ClassMerge = { design, ids: new Map(), added: [], reused: [], renamed: [] };
|
|
198
|
+
for (const raw of incoming) {
|
|
199
|
+
// A ref to a colour or size this site doesn't have is dropped, like on paste.
|
|
200
|
+
const { style } = prepareStyle(raw, design);
|
|
201
|
+
const cls: StyleClass = { id: raw.id, name: raw.name, style: style.style ?? {} };
|
|
202
|
+
if (style.states) cls.states = style.states;
|
|
203
|
+
if (style.devices) cls.devices = style.devices;
|
|
204
|
+
if (style.hiddenOn) cls.hiddenOn = style.hiddenOn;
|
|
205
|
+
const look = lookOf(cls);
|
|
206
|
+
const same = classes.find(
|
|
207
|
+
(c) => (c.id === cls.id || nameKey(c.name) === nameKey(cls.name)) && lookOf(c) === look,
|
|
208
|
+
);
|
|
209
|
+
if (same) {
|
|
210
|
+
merge.ids.set(raw.id, same.id);
|
|
211
|
+
merge.reused.push(same.name);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (classes.length >= MAX_CLASSES) return refuse(SAVED_SECTION_REASONS.tooManyClasses);
|
|
215
|
+
const names = new Set(classes.map((c) => nameKey(c.name)));
|
|
216
|
+
let name = cls.name;
|
|
217
|
+
for (let n = 2; names.has(nameKey(name)); n++) {
|
|
218
|
+
name = `${clipText(cls.name, 54).trim()} ${n}`;
|
|
219
|
+
}
|
|
220
|
+
const ids = new Set(classes.map((c) => c.id));
|
|
221
|
+
const base = ids.has(cls.id)
|
|
222
|
+
? slugify(name).slice(0, 34).replace(/-+$/, "") || "class"
|
|
223
|
+
: cls.id;
|
|
224
|
+
let id = base;
|
|
225
|
+
for (let n = 2; ids.has(id); n++) id = `${base}-${n}`.slice(0, 40);
|
|
226
|
+
classes.push({ ...cls, id, name });
|
|
227
|
+
merge.ids.set(raw.id, id);
|
|
228
|
+
if (name === cls.name) merge.added.push(name);
|
|
229
|
+
else merge.renamed.push({ from: cls.name, to: name });
|
|
230
|
+
}
|
|
231
|
+
merge.design = { ...design, classes };
|
|
232
|
+
return merge;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function withClassIds(node: LayoutNode, ids: ReadonlyMap<string, string>): LayoutNode {
|
|
236
|
+
const kids = nodeChildren(node);
|
|
237
|
+
const next = { ...node } as LayoutNode;
|
|
238
|
+
if (node.classes) next.classes = node.classes.map((id) => ids.get(id) ?? id);
|
|
239
|
+
if (kids.length > 0) {
|
|
240
|
+
(next as { children: LayoutNode[] }).children = kids.map((kid) => withClassIds(kid, ids));
|
|
241
|
+
}
|
|
242
|
+
return next;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export type PreparedSection = {
|
|
246
|
+
node: LayoutNode;
|
|
247
|
+
design: DesignSystem;
|
|
248
|
+
merge: ClassMerge;
|
|
249
|
+
dropped: Dropped;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The node to insert for a saved section on `layout`, and the site styles with its classes:
|
|
254
|
+
* classes merged as above, then the same checks as a paste (new ids, CSS ids already on the page
|
|
255
|
+
* dropped, links, images and SVGs sanitized).
|
|
256
|
+
*/
|
|
257
|
+
export function prepareSavedSection(
|
|
258
|
+
section: SavedSection,
|
|
259
|
+
layout: Layout,
|
|
260
|
+
design: DesignSystem,
|
|
261
|
+
random: () => number = Math.random,
|
|
262
|
+
/** A merge already applied to `design` (its classes are saved): reused, not merged again. */
|
|
263
|
+
merged?: ClassMerge,
|
|
264
|
+
): ({ ok: true } & PreparedSection) | Refusal {
|
|
265
|
+
const merge = merged ?? mergeSectionClasses(design, section.classes);
|
|
266
|
+
if ("ok" in merge) return merge;
|
|
267
|
+
const remapped = withClassIds(section.node, merge.ids);
|
|
268
|
+
const { node, dropped } = prepareElement(remapped, layout, merge.design, random);
|
|
269
|
+
return { ok: true, node, design: merge.design, merge, dropped };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Prepares and adds a saved section near the selection, like a section recipe (W-320). */
|
|
273
|
+
export function insertSavedSection(
|
|
274
|
+
section: SavedSection,
|
|
275
|
+
layout: Layout,
|
|
276
|
+
design: DesignSystem,
|
|
277
|
+
selectedId: string | null,
|
|
278
|
+
random: () => number = Math.random,
|
|
279
|
+
): ({ design: DesignSystem; merge: ClassMerge; dropped: Dropped } & Added) | Refusal {
|
|
280
|
+
const prepared = prepareSavedSection(section, layout, design, random);
|
|
281
|
+
if (!prepared.ok) return prepared;
|
|
282
|
+
const added = addSectionNear(layout, prepared.node, selectedId);
|
|
283
|
+
if (!added.ok) return added;
|
|
284
|
+
return { ...added, design: prepared.design, merge: prepared.merge, dropped: prepared.dropped };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** "Classes: 1 added, 1 renamed (Card → Card 2)" for the notice, or null when nothing changed. */
|
|
288
|
+
export function classMergeNotice(merge: ClassMerge): string | null {
|
|
289
|
+
const parts: string[] = [];
|
|
290
|
+
if (merge.added.length > 0) parts.push(`${merge.added.length} added`);
|
|
291
|
+
if (merge.renamed.length > 0) {
|
|
292
|
+
const list = merge.renamed.map(({ from, to }) => `${from} → ${to}`).join(", ");
|
|
293
|
+
parts.push(`${merge.renamed.length} added as a copy (${list})`);
|
|
294
|
+
}
|
|
295
|
+
return parts.length > 0 ? `Classes: ${parts.join("; ")}.` : null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* The layout of a new Section theme part for a synced section (W-876, W-098): a copy of the
|
|
300
|
+
* element with fresh ids and no A/B arms. Null for the page itself or an unknown id.
|
|
301
|
+
*/
|
|
302
|
+
export function syncedPartLayout(
|
|
303
|
+
layout: Layout,
|
|
304
|
+
nodeId: string,
|
|
305
|
+
random: () => number = Math.random,
|
|
306
|
+
): Layout | null {
|
|
307
|
+
const found = nodeId === layout.root.id ? undefined : findNode(layout, nodeId);
|
|
308
|
+
if (!found) return null;
|
|
309
|
+
return {
|
|
310
|
+
schemaVersion: LAYOUT_SCHEMA_VERSION,
|
|
311
|
+
root: {
|
|
312
|
+
id: newNodeId(random),
|
|
313
|
+
type: "container",
|
|
314
|
+
props: {},
|
|
315
|
+
children: [withFreshIds(layout, withoutVariants(found), random)],
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
@@ -8,7 +8,7 @@ import { DeviceStyles, HiddenOn, StyleProps, StyleStates } from "./style.ts";
|
|
|
8
8
|
* 13 since W-307, in step with layouts (the W-307 batch only adds optional fields).
|
|
9
9
|
* 12 since W-160: class gradients use the same stops as layouts. v11 `{ angle, from, to }` upgrades on read.
|
|
10
10
|
*/
|
|
11
|
-
export const DESIGN_SCHEMA_VERSION =
|
|
11
|
+
export const DESIGN_SCHEMA_VERSION = 16;
|
|
12
12
|
|
|
13
13
|
/** Tags a site can give a starting style. Classes and local styles still win. */
|
|
14
14
|
export const DEFAULT_STYLE_TAGS = ["h1", "h2", "h3", "h4", "h5", "h6", "p", "a", "button"] as const;
|