@perkyzz/emvb 0.8.0 → 1.0.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 +3 -3
- package/package.json +4 -4
- package/src/admin/agents-api.ts +45 -0
- package/src/admin/editor/A11yCopilot.tsx +1 -1
- package/src/admin/editor/Editor.tsx +325 -239
- package/src/admin/editor/FieldPreview.tsx +164 -0
- package/src/admin/editor/Timeline.tsx +2 -1
- package/src/admin/editor/TopBar.tsx +36 -3
- package/src/admin/editor/canvas/CanvasFrame.tsx +2 -2
- package/src/admin/editor/canvas/DeviceBar.tsx +17 -9
- package/src/admin/editor/canvas/canvas-scale.ts +2 -2
- package/src/admin/editor/canvas/use-canvas-session.ts +2 -2
- package/src/admin/editor/co-design.ts +190 -0
- package/src/admin/editor/dnd/new-element.ts +26 -4
- package/src/admin/editor/editor-chrome.tsx +3 -3
- package/src/admin/editor/editor-commands.ts +8 -3
- package/src/admin/editor/editor-css.ts +3 -0
- package/src/admin/editor/element-chrome.ts +3 -1
- package/src/admin/editor/extension-manifests.ts +20 -0
- package/src/admin/editor/extensions/ExtensionPanels.tsx +26 -0
- package/src/admin/editor/extensions-context.tsx +27 -0
- package/src/admin/editor/feature-css.ts +17 -0
- package/src/admin/editor/host.ts +6 -0
- package/src/admin/editor/palette-items.ts +2 -2
- package/src/admin/editor/panels/AddPanel.tsx +31 -1
- package/src/admin/editor/panels/AgentRequestBox.tsx +116 -0
- package/src/admin/editor/panels/ClassesSection.tsx +9 -6
- package/src/admin/editor/panels/CoDesignPanel.tsx +170 -0
- package/src/admin/editor/panels/ColorControl.tsx +2 -2
- package/src/admin/editor/panels/ElementPanel.tsx +53 -15
- package/src/admin/editor/panels/LanguagesSection.tsx +152 -0
- package/src/admin/editor/panels/LayersPanel.tsx +10 -6
- package/src/admin/editor/panels/LeftPanel.tsx +33 -5
- package/src/admin/editor/panels/PageSettings.tsx +4 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +21 -2
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +7 -7
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +76 -20
- package/src/admin/editor/panels/settings/BackgroundFill.tsx +8 -6
- package/src/admin/editor/panels/settings/BoxControl.tsx +5 -5
- package/src/admin/editor/panels/settings/EffectsControls.tsx +104 -9
- package/src/admin/editor/panels/settings/FieldControl.tsx +9 -3
- package/src/admin/editor/panels/settings/IconControls.tsx +14 -11
- package/src/admin/editor/panels/settings/LengthRow.tsx +27 -16
- package/src/admin/editor/panels/settings/LoopControl.tsx +269 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
- package/src/admin/editor/panels/settings/MediaPicker.tsx +5 -5
- package/src/admin/editor/panels/settings/MotionControl.tsx +12 -11
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +7 -4
- package/src/admin/editor/panels/settings/StyleRow.tsx +113 -13
- package/src/admin/editor/panels/settings/TextMarksControl.tsx +247 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +2 -2
- package/src/admin/editor/panels/settings/background-mode.ts +14 -11
- package/src/admin/editor/panels/settings/box-link-name.ts +1 -1
- package/src/admin/editor/panels/settings/box-sides.ts +22 -14
- package/src/admin/editor/panels/settings/heading-warning.ts +5 -1
- package/src/admin/editor/panels/settings/length-units.ts +55 -2
- package/src/admin/editor/panels/settings/query-style.ts +3 -3
- package/src/admin/editor/panels/settings/style-sections.ts +51 -10
- package/src/admin/editor/panels/style-target.ts +9 -7
- package/src/admin/editor/presence.ts +60 -0
- package/src/admin/editor/store.ts +6 -2
- package/src/admin/editor/useCoDesign.ts +177 -0
- package/src/admin/editor/useEditorCommands.ts +4 -4
- package/src/admin/editor/useExtensions.ts +45 -0
- package/src/admin/editor/useNodeActions.ts +15 -6
- package/src/admin/editor/useProposals.ts +81 -0
- package/src/admin/index.tsx +5 -1
- package/src/admin/languages-api.ts +61 -0
- package/src/admin/media-api.ts +27 -0
- package/src/admin/pages/AgentSetup.tsx +148 -0
- package/src/admin/pages/AgentsPage.tsx +11 -28
- package/src/admin/pages/CollectionsPage.tsx +215 -0
- package/src/admin/pages/CreateDialog.tsx +15 -0
- package/src/admin/pages/ImportButton.tsx +18 -0
- package/src/admin/pages/ImportElementorDialog.tsx +669 -0
- package/src/admin/pages/ImportPageLinks.tsx +86 -0
- package/src/admin/pages/PageList.tsx +15 -1
- package/src/admin/pages/ThemePartList.tsx +15 -1
- package/src/admin/pages/section-head.tsx +27 -0
- package/src/admin/proposals-api.ts +91 -0
- package/src/admin/setup/host-collections.ts +100 -0
- package/src/admin/ui.ts +39 -2
- package/src/admin/widgets/CanvasField.tsx +202 -0
- package/src/admin/widgets/convert-body.ts +54 -0
- package/src/admin/widgets/field-fetcher.ts +79 -0
- package/src/astro/content-port.ts +14 -1
- package/src/astro/index.ts +2 -0
- package/src/astro/page-count.ts +77 -0
- package/src/astro/resolve-theme.ts +58 -3
- package/src/astro/resolve.ts +128 -5
- package/src/astro/responsive-images.ts +99 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +44 -14
- package/src/core/arrange.ts +5 -1
- package/src/core/bounded-array.ts +31 -0
- package/src/core/canvas-collections.ts +20 -0
- package/src/core/clipboard.ts +9 -0
- package/src/core/contract.ts +285 -0
- package/src/core/css/generate.ts +89 -20
- package/src/core/css/trace.ts +18 -9
- package/src/core/css-keywords.ts +51 -1
- package/src/core/design/cascade.ts +12 -9
- package/src/core/design/classes.ts +4 -5
- package/src/core/design/devices.ts +13 -13
- package/src/core/design/states.ts +5 -3
- package/src/core/design/transfer.ts +5 -0
- package/src/core/design/variables.ts +17 -4
- package/src/core/elements/dynamic-elements.ts +19 -4
- package/src/core/elements/index.ts +73 -9
- package/src/core/extensions/define.ts +13 -0
- package/src/core/extensions/index.ts +41 -0
- package/src/core/extensions/namespace.ts +82 -0
- package/src/core/extensions/normalize.ts +141 -0
- package/src/core/extensions/placeholder.ts +41 -0
- package/src/core/extensions/registry.ts +94 -0
- package/src/core/extensions/render-ext.ts +118 -0
- package/src/core/extensions/safe-nodes.ts +93 -0
- package/src/core/extensions/schema.ts +112 -0
- package/src/core/extensions/types.ts +159 -0
- package/src/core/forms/binding.ts +1 -1
- package/src/core/history/diff.ts +1 -1
- package/src/core/import/elementor/atomic.ts +1595 -0
- package/src/core/import/elementor/classes.ts +127 -0
- package/src/core/import/elementor/context.ts +429 -0
- package/src/core/import/elementor/counter-steps.ts +72 -0
- package/src/core/import/elementor/css.ts +445 -0
- package/src/core/import/elementor/html.ts +500 -0
- package/src/core/import/elementor/index.ts +949 -0
- package/src/core/import/elementor/inline-marks.ts +81 -0
- package/src/core/import/elementor/snapshots.ts +48 -0
- package/src/core/import/elementor/style.ts +409 -0
- package/src/core/import/elementor/values.ts +241 -0
- package/src/core/import/elementor/widgets.ts +1897 -0
- package/src/core/index.ts +84 -0
- package/src/core/json-schema.ts +107 -0
- package/src/core/limits.ts +19 -0
- package/src/core/migrate/index.ts +6 -1
- package/src/core/migrate/v18.ts +222 -0
- package/src/core/perf/meter.ts +5 -5
- package/src/core/portable-text.ts +127 -0
- package/src/core/proposals-safety.ts +102 -0
- package/src/core/proposals.ts +357 -0
- package/src/core/recipes/index.ts +2 -2
- package/src/core/render/extras.ts +3 -3
- package/src/core/render/index.ts +140 -20
- package/src/core/render/loop.ts +1 -1
- package/src/core/render/pagination.ts +20 -1
- package/src/core/render/section.ts +4 -4
- package/src/core/render/vnode.ts +1 -1
- package/src/core/sanitize/css.ts +125 -10
- package/src/core/sanitize/motion.ts +116 -0
- package/src/core/saved-sections.ts +6 -0
- package/src/core/schema/descriptors.ts +3 -1
- package/src/core/schema/design.ts +2 -1
- package/src/core/schema/layout.ts +87 -14
- package/src/core/schema/style.ts +251 -114
- package/src/core/text-marks.ts +227 -0
- package/src/core/theme/dynamic.ts +9 -4
- package/src/core/theme/float.ts +1 -0
- package/src/core/theme/pagination.ts +9 -0
- package/src/core/theme/popup-rules.ts +17 -1
- package/src/core/translatable.ts +34 -0
- package/src/core/validate.ts +13 -1
- package/src/index.ts +35 -1
- package/src/mcp/errors.ts +3 -1
- package/src/mcp/ports.ts +67 -0
- package/src/mcp/prompts.ts +1 -11
- package/src/mcp/proposals.ts +281 -0
- package/src/mcp/read.ts +10 -9
- package/src/mcp/tools.ts +71 -3
- package/src/mcp/write.ts +66 -11
- package/src/server/agent-settings.ts +21 -2
- package/src/server/hooks.ts +81 -0
- package/src/server/mcp-routes.ts +15 -0
- package/src/server/plugin.ts +53 -5
- package/src/server/presence.ts +78 -0
- package/src/server/proposals.ts +261 -0
- package/src/server/requests.ts +196 -0
- package/src/server/tools-served.ts +45 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ELEMENT_DESCRIPTORS } from "./elements/index.ts";
|
|
2
|
+
import { isFlatMotionKey } from "./migrate/v18.ts";
|
|
3
|
+
import { isRecord } from "./record.ts";
|
|
4
|
+
import { isSafeCssValue, isSafeFontStack } from "./sanitize/css.ts";
|
|
5
|
+
import { sanitizeHref } from "./sanitize/href.ts";
|
|
6
|
+
import { sanitizeMediaUrl } from "./sanitize/media-url.ts";
|
|
7
|
+
import { isSafeSvgMarkup } from "./sanitize/svg.ts";
|
|
8
|
+
import type { DesignSystem } from "./schema/design.ts";
|
|
9
|
+
import type { Layout } from "./schema/layout.ts";
|
|
10
|
+
import { stableJson } from "./stable-json.ts";
|
|
11
|
+
import { findNode, forEachNode } from "./tree-ops.ts";
|
|
12
|
+
|
|
13
|
+
const same = (a: unknown, b: unknown) => stableJson(a) === stableJson(b);
|
|
14
|
+
|
|
15
|
+
function styleRefusal(raw: unknown, key = ""): string | undefined {
|
|
16
|
+
if (typeof raw === "string" && raw !== "") {
|
|
17
|
+
const safe = key === "fontFamily" ? isSafeFontStack(raw) : isSafeCssValue(raw);
|
|
18
|
+
if (!safe) return `Unsafe style value at ${key}.`;
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(raw)) {
|
|
21
|
+
for (const value of raw) {
|
|
22
|
+
const reason = styleRefusal(value, key);
|
|
23
|
+
if (reason) return reason;
|
|
24
|
+
}
|
|
25
|
+
} else if (isRecord(raw)) {
|
|
26
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
27
|
+
if (key !== "motion" && isFlatMotionKey(name))
|
|
28
|
+
return "Use frozen style.motion keys, not legacy motion fields.";
|
|
29
|
+
if ((name === "src" || name === "url") && typeof value === "string") {
|
|
30
|
+
if (value.trim() && !sanitizeMediaUrl(value)) return "Unsafe background media URL.";
|
|
31
|
+
} else {
|
|
32
|
+
const reason = styleRefusal(value, name);
|
|
33
|
+
if (reason) return reason;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Reuse the renderer's allowlists. Old, unchanged unsafe fields remain readable like MCP writes. */
|
|
41
|
+
export function proposalSafetyRefusal(
|
|
42
|
+
before: Layout,
|
|
43
|
+
after: Layout,
|
|
44
|
+
oldDesign: DesignSystem,
|
|
45
|
+
design: DesignSystem,
|
|
46
|
+
): string | undefined {
|
|
47
|
+
let reason: string | undefined;
|
|
48
|
+
forEachNode(after.root, (node) => {
|
|
49
|
+
if (reason) return;
|
|
50
|
+
const old = findNode(before, node.id);
|
|
51
|
+
const props = node.props as Record<string, unknown>;
|
|
52
|
+
const oldProps = old?.props as Record<string, unknown> | undefined;
|
|
53
|
+
for (const field of ELEMENT_DESCRIPTORS.find((d) => d.type === node.type)?.fields ?? []) {
|
|
54
|
+
const value = props[field.key];
|
|
55
|
+
if (
|
|
56
|
+
field.kind === "href" &&
|
|
57
|
+
typeof value === "string" &&
|
|
58
|
+
value.trim() &&
|
|
59
|
+
value !== oldProps?.[field.key] &&
|
|
60
|
+
sanitizeHref(value) === undefined
|
|
61
|
+
)
|
|
62
|
+
reason = `Unsafe link on ${node.id}.${field.key}.`;
|
|
63
|
+
}
|
|
64
|
+
if (!same(node.props, old?.props)) {
|
|
65
|
+
const sources = node.type === "image" && Array.isArray(props.sources) ? props.sources : [];
|
|
66
|
+
const urls = [
|
|
67
|
+
node.type === "image" || node.type === "video" ? props.src : undefined,
|
|
68
|
+
...sources.map((s: { src?: unknown }) => s.src),
|
|
69
|
+
];
|
|
70
|
+
if (urls.some((url) => typeof url === "string" && url.trim() && !sanitizeMediaUrl(url)))
|
|
71
|
+
reason = `Unsafe media URL on ${node.id}.`;
|
|
72
|
+
for (const markup of [node.type === "svg" ? props.markup : undefined, props.iconSvg]) {
|
|
73
|
+
if (typeof markup === "string" && markup.trim() && !isSafeSvgMarkup(markup))
|
|
74
|
+
reason = `Unsafe SVG on ${node.id}.`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const key of ["style", "states", "devices", "query"] as const) {
|
|
78
|
+
if (!same(node[key], old?.[key])) reason ??= styleRefusal(node[key]);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
for (const cls of design.classes ?? []) {
|
|
82
|
+
if (
|
|
83
|
+
!same(
|
|
84
|
+
cls,
|
|
85
|
+
oldDesign.classes?.find((c) => c.id === cls.id),
|
|
86
|
+
)
|
|
87
|
+
) {
|
|
88
|
+
for (const raw of [cls.style, cls.states, cls.devices]) reason ??= styleRefusal(raw);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (const font of design.variables.fonts ?? []) {
|
|
92
|
+
if (
|
|
93
|
+
!same(
|
|
94
|
+
font,
|
|
95
|
+
oldDesign.variables.fonts?.find((v) => v.id === font.id),
|
|
96
|
+
) &&
|
|
97
|
+
!isSafeFontStack(font.value)
|
|
98
|
+
)
|
|
99
|
+
reason ??= "Unsafe font stack.";
|
|
100
|
+
}
|
|
101
|
+
return reason;
|
|
102
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { boundedArray } from "./bounded-array.ts";
|
|
3
|
+
import { addNode, moveNode, type Refusal } from "./arrange.ts";
|
|
4
|
+
import { cleanClassName, nameKey } from "./design/classes.ts";
|
|
5
|
+
import { isExtensionType, liveElement, validateExtensionProps } from "./extensions/index.ts";
|
|
6
|
+
import { MAX_PROPOSAL_BYTES, MAX_PROPOSAL_DEPTH, MAX_PROPOSAL_OPS } from "./limits.ts";
|
|
7
|
+
import { nestingIssues } from "./nesting.ts";
|
|
8
|
+
import { isRecord } from "./record.ts";
|
|
9
|
+
import { type DesignSystem } from "./schema/design.ts";
|
|
10
|
+
import {
|
|
11
|
+
isParentNode,
|
|
12
|
+
KNOWN_ELEMENT_TYPES,
|
|
13
|
+
NodeId,
|
|
14
|
+
type Layout,
|
|
15
|
+
type LayoutNode,
|
|
16
|
+
} from "./schema/layout.ts";
|
|
17
|
+
import { findNode, forEachNode, removeNode, updateNode } from "./tree-ops.ts";
|
|
18
|
+
import { byteLength, summarizeIssues, validateDesign, validateLayout } from "./validate.ts";
|
|
19
|
+
import { proposalSafetyRefusal } from "./proposals-safety.ts";
|
|
20
|
+
|
|
21
|
+
const Revision = z.string().min(1).max(256);
|
|
22
|
+
export const ProposalTarget = z.strictObject({
|
|
23
|
+
collection: z.string().regex(/^[a-z][a-z0-9_-]{0,127}$/),
|
|
24
|
+
id: z.string().min(1).max(128),
|
|
25
|
+
});
|
|
26
|
+
const Value = z.record(z.string(), z.unknown());
|
|
27
|
+
const Position = { parentId: NodeId, index: z.number().int().min(0).max(2000) };
|
|
28
|
+
const DesignId = z.string().regex(/^[a-z0-9-]{1,40}$/);
|
|
29
|
+
export const ProposalOperation = z.discriminatedUnion("kind", [
|
|
30
|
+
z.strictObject({ kind: z.literal("insert"), ...Position, node: Value }),
|
|
31
|
+
z.strictObject({ kind: z.literal("move"), id: NodeId, ...Position }),
|
|
32
|
+
z.strictObject({ kind: z.literal("update"), id: NodeId, set: Value }),
|
|
33
|
+
z.strictObject({ kind: z.literal("delete"), id: NodeId }),
|
|
34
|
+
z.strictObject({ kind: z.literal("class"), id: DesignId, value: Value.nullable() }),
|
|
35
|
+
z.strictObject({
|
|
36
|
+
kind: z.literal("variable"),
|
|
37
|
+
group: z.enum(["colors", "fonts", "fontSizes", "spacings"]),
|
|
38
|
+
id: DesignId,
|
|
39
|
+
value: Value.nullable(),
|
|
40
|
+
}),
|
|
41
|
+
]);
|
|
42
|
+
const ProposalSchema = z.strictObject({
|
|
43
|
+
version: z.literal(1),
|
|
44
|
+
target: ProposalTarget,
|
|
45
|
+
baseRevision: Revision,
|
|
46
|
+
/** Effective style stamp: draft when present, otherwise published; null for an empty site. */
|
|
47
|
+
baseDesignRevision: Revision.nullable(),
|
|
48
|
+
ops: boundedArray(ProposalOperation, MAX_PROPOSAL_OPS, 1),
|
|
49
|
+
});
|
|
50
|
+
export type Proposal = z.infer<typeof ProposalSchema>;
|
|
51
|
+
export type ProposalBase = {
|
|
52
|
+
revision: string;
|
|
53
|
+
layout: unknown;
|
|
54
|
+
designRevision: string | null;
|
|
55
|
+
design: unknown;
|
|
56
|
+
};
|
|
57
|
+
export type ProposalRecord = { proposal: Proposal; createdAt: string };
|
|
58
|
+
export type ProposalRefusal = Refusal & {
|
|
59
|
+
code: "INVALID_PROPOSAL" | "STALE_BASE" | "NOT_FOUND" | "PROPOSALS_FULL";
|
|
60
|
+
opIndex?: number;
|
|
61
|
+
expectedRevision?: string | null;
|
|
62
|
+
currentRevision?: string | null;
|
|
63
|
+
};
|
|
64
|
+
const refuse = (reason: string, opIndex?: number): ProposalRefusal => ({
|
|
65
|
+
ok: false,
|
|
66
|
+
code: "INVALID_PROPOSAL",
|
|
67
|
+
reason,
|
|
68
|
+
...(opIndex === undefined ? {} : { opIndex }),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/** Bound JSON before recursive schema parsing or serialization, including hand-written port calls. */
|
|
72
|
+
function jsonRefusal(raw: unknown): string | undefined {
|
|
73
|
+
const stack = [{ value: raw, depth: 0 }];
|
|
74
|
+
const seen = new Set<object>();
|
|
75
|
+
let count = 0;
|
|
76
|
+
while (stack.length) {
|
|
77
|
+
const { value, depth } = stack.pop() as { value: unknown; depth: number };
|
|
78
|
+
if (++count > MAX_PROPOSAL_BYTES || depth > MAX_PROPOSAL_DEPTH)
|
|
79
|
+
return "Proposal nesting or size limit exceeded.";
|
|
80
|
+
if (typeof value === "string" && value.length > MAX_PROPOSAL_BYTES)
|
|
81
|
+
return "Proposal string exceeds size limit.";
|
|
82
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") continue;
|
|
83
|
+
if (typeof value === "number" && Number.isFinite(value)) continue;
|
|
84
|
+
if (typeof value !== "object" || seen.has(value)) return "Proposals must be finite JSON trees.";
|
|
85
|
+
seen.add(value);
|
|
86
|
+
if (
|
|
87
|
+
!Array.isArray(value) &&
|
|
88
|
+
Object.getPrototypeOf(value) !== Object.prototype &&
|
|
89
|
+
Object.getPrototypeOf(value) !== null
|
|
90
|
+
)
|
|
91
|
+
return "Proposals must contain plain JSON objects.";
|
|
92
|
+
for (const [key, item] of Object.entries(value)) {
|
|
93
|
+
if (["__proto__", "prototype", "constructor"].includes(key)) return "Unsafe proposal key.";
|
|
94
|
+
stack.push({ value: item, depth: depth + 1 });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return byteLength(raw) > MAX_PROPOSAL_BYTES
|
|
98
|
+
? `Proposal exceeds ${MAX_PROPOSAL_BYTES} bytes.`
|
|
99
|
+
: undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function readProposal(raw: unknown): { ok: true; proposal: Proposal } | ProposalRefusal {
|
|
103
|
+
const reason = jsonRefusal(raw);
|
|
104
|
+
if (reason) return refuse(reason);
|
|
105
|
+
const parsed = ProposalSchema.safeParse(raw);
|
|
106
|
+
if (!parsed.success)
|
|
107
|
+
return refuse(parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; "));
|
|
108
|
+
return { ok: true, proposal: parsed.data };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const UPDATE_KEYS = new Set([
|
|
112
|
+
"props",
|
|
113
|
+
"style",
|
|
114
|
+
"states",
|
|
115
|
+
"devices",
|
|
116
|
+
"hiddenOn",
|
|
117
|
+
"classes",
|
|
118
|
+
"htmlId",
|
|
119
|
+
"attributes",
|
|
120
|
+
"editorLabel",
|
|
121
|
+
"bind",
|
|
122
|
+
"variant",
|
|
123
|
+
"audience",
|
|
124
|
+
"dir",
|
|
125
|
+
"container",
|
|
126
|
+
"query",
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
function wrapperId(raw: Record<string, unknown>): string {
|
|
130
|
+
const ids = new Set<unknown>();
|
|
131
|
+
const stack: unknown[] = [raw];
|
|
132
|
+
while (stack.length) {
|
|
133
|
+
const node = stack.pop();
|
|
134
|
+
if (!isRecord(node)) continue;
|
|
135
|
+
ids.add(node.id);
|
|
136
|
+
if (Array.isArray(node.children)) for (const child of node.children) stack.push(child);
|
|
137
|
+
}
|
|
138
|
+
let id = "proposal-wrapper";
|
|
139
|
+
for (let n = 1; ids.has(id); n++) id = `proposal-wrapper-${n}`;
|
|
140
|
+
return id;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function nodeRefusal(
|
|
144
|
+
node: LayoutNode,
|
|
145
|
+
installed: (type: string) => boolean = (type) => liveElement(type) !== undefined,
|
|
146
|
+
): string | undefined {
|
|
147
|
+
let reason: string | undefined;
|
|
148
|
+
forEachNode(node, (item) => {
|
|
149
|
+
if (reason) return;
|
|
150
|
+
if (
|
|
151
|
+
!(KNOWN_ELEMENT_TYPES as readonly string[]).includes(item.type) &&
|
|
152
|
+
!isExtensionType(item.type)
|
|
153
|
+
) {
|
|
154
|
+
reason = `Unknown frozen element type: ${item.type}.`;
|
|
155
|
+
}
|
|
156
|
+
if (
|
|
157
|
+
Object.hasOwn(item, "label") ||
|
|
158
|
+
(item.type === "loop" && Object.hasOwn(item.props, "itemPartId"))
|
|
159
|
+
) {
|
|
160
|
+
reason = "Use frozen schema names: editorLabel and props.partId.";
|
|
161
|
+
}
|
|
162
|
+
const extension = liveElement(item.type);
|
|
163
|
+
// W-933: a page may keep a placeholder for a missing extension, but an agent can't add or
|
|
164
|
+
// change one: its props can't be checked without the extension's schema.
|
|
165
|
+
if (isExtensionType(item.type) && !installed(item.type)) {
|
|
166
|
+
reason = `The extension for ${item.type} is not installed on this site.`;
|
|
167
|
+
}
|
|
168
|
+
if (extension && !validateExtensionProps(extension.element.schema, item.props).ok) {
|
|
169
|
+
reason = `Invalid extension props for ${item.type}.`;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
return reason;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function editNode(
|
|
176
|
+
layout: Layout,
|
|
177
|
+
op: Extract<Proposal["ops"][number], { kind: "update" }>,
|
|
178
|
+
installed?: (type: string) => boolean,
|
|
179
|
+
): Layout | ProposalRefusal {
|
|
180
|
+
const found = findNode(layout, op.id);
|
|
181
|
+
if (!found) return refuse(`Element ${op.id} is no longer on the page.`);
|
|
182
|
+
if (
|
|
183
|
+
Object.keys(op.set).length === 0 ||
|
|
184
|
+
Object.keys(op.set).some((key) => !UPDATE_KEYS.has(key))
|
|
185
|
+
) {
|
|
186
|
+
return refuse("An update needs editable frozen fields; id, type and children cannot change.");
|
|
187
|
+
}
|
|
188
|
+
if (Object.hasOwn(op.set, "props") && !isRecord(op.set.props))
|
|
189
|
+
return refuse("props must be an object.");
|
|
190
|
+
const next = { ...found } as Record<string, unknown>;
|
|
191
|
+
for (const [key, value] of Object.entries(op.set)) {
|
|
192
|
+
if (key === "props") next[key] = { ...found.props, ...(value as Record<string, unknown>) };
|
|
193
|
+
else if (value === null) delete next[key];
|
|
194
|
+
else next[key] = value;
|
|
195
|
+
}
|
|
196
|
+
const reason = nodeRefusal(next as LayoutNode, installed);
|
|
197
|
+
return reason ? refuse(reason) : updateNode(layout, op.id, () => next as LayoutNode);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
type Named = { id: string; name: string };
|
|
201
|
+
function editNamed(
|
|
202
|
+
list: Named[],
|
|
203
|
+
id: string,
|
|
204
|
+
raw: Record<string, unknown> | null,
|
|
205
|
+
): Named[] | ProposalRefusal {
|
|
206
|
+
if (raw === null) {
|
|
207
|
+
if (!list.some((v) => v.id === id)) return refuse(`No class or variable with id ${id}.`);
|
|
208
|
+
return list.filter((v) => v.id !== id);
|
|
209
|
+
}
|
|
210
|
+
if (
|
|
211
|
+
raw.id !== id ||
|
|
212
|
+
typeof raw.name !== "string" ||
|
|
213
|
+
!raw.name.trim() ||
|
|
214
|
+
cleanClassName(raw.name) !== raw.name
|
|
215
|
+
)
|
|
216
|
+
return refuse(
|
|
217
|
+
"Class/variable ids must match and names must be clean, nonblank and at most 60 characters.",
|
|
218
|
+
);
|
|
219
|
+
if (list.some((v) => v.id !== id && nameKey(v.name) === nameKey(raw.name as string)))
|
|
220
|
+
return refuse(`The name ${raw.name} is already in use.`);
|
|
221
|
+
const next = raw as Named;
|
|
222
|
+
return list.some((v) => v.id === id)
|
|
223
|
+
? list.map((v) => (v.id === id ? next : v))
|
|
224
|
+
: [...list, next];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function stale(
|
|
228
|
+
expectedRevision: string | null,
|
|
229
|
+
currentRevision: string | null,
|
|
230
|
+
what: string,
|
|
231
|
+
): ProposalRefusal {
|
|
232
|
+
return {
|
|
233
|
+
ok: false,
|
|
234
|
+
code: "STALE_BASE",
|
|
235
|
+
expectedRevision,
|
|
236
|
+
currentRevision,
|
|
237
|
+
reason: `${what} base revision ${JSON.stringify(expectedRevision)} does not match current revision ${JSON.stringify(currentRevision)}. Rebase: read the current ${what.toLowerCase()} and submit a new proposal against that revision.`,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Pure and atomic. Selection uses original op indexes and always runs in proposal order. */
|
|
242
|
+
/**
|
|
243
|
+
* `extensionInstalled` says whether an `ext:` type is registered; the server's default reads the
|
|
244
|
+
* live registry, and the browser editor passes the manifests it loaded (W-933).
|
|
245
|
+
*/
|
|
246
|
+
export type ApplyOptions = { extensionInstalled?: (type: string) => boolean };
|
|
247
|
+
|
|
248
|
+
export function applyProposal(
|
|
249
|
+
base: ProposalBase,
|
|
250
|
+
raw: unknown,
|
|
251
|
+
selected?: readonly number[],
|
|
252
|
+
options: ApplyOptions = {},
|
|
253
|
+
): { ok: true; layout: Layout; design: DesignSystem } | ProposalRefusal {
|
|
254
|
+
const read = readProposal(raw);
|
|
255
|
+
if (!read.ok) return read;
|
|
256
|
+
const proposal = read.proposal;
|
|
257
|
+
if (proposal.baseRevision !== base.revision)
|
|
258
|
+
return stale(proposal.baseRevision, base.revision, "Page");
|
|
259
|
+
if (
|
|
260
|
+
proposal.ops.some((op) => op.kind === "class" || op.kind === "variable") &&
|
|
261
|
+
proposal.baseDesignRevision !== base.designRevision
|
|
262
|
+
)
|
|
263
|
+
return stale(proposal.baseDesignRevision, base.designRevision, "Site styles");
|
|
264
|
+
if (
|
|
265
|
+
selected &&
|
|
266
|
+
(new Set(selected).size !== selected.length ||
|
|
267
|
+
selected.some((i) => !Number.isInteger(i) || i < 0 || i >= proposal.ops.length))
|
|
268
|
+
)
|
|
269
|
+
return refuse("Selected op indexes must be unique and in range.");
|
|
270
|
+
const checkedLayout = validateLayout(base.layout);
|
|
271
|
+
const checkedDesign = validateDesign(base.design);
|
|
272
|
+
if (!checkedLayout.ok)
|
|
273
|
+
return refuse(`Invalid base layout: ${summarizeIssues(checkedLayout.issues)}`);
|
|
274
|
+
if (!checkedDesign.ok)
|
|
275
|
+
return refuse(`Invalid base design: ${summarizeIssues(checkedDesign.issues)}`);
|
|
276
|
+
let layout = checkedLayout.layout;
|
|
277
|
+
let design = checkedDesign.design;
|
|
278
|
+
for (const [opIndex, op] of proposal.ops.entries()) {
|
|
279
|
+
if (selected && !selected.includes(opIndex)) continue;
|
|
280
|
+
let reason: string | undefined;
|
|
281
|
+
if (op.kind === "insert" || op.kind === "move") {
|
|
282
|
+
const parent = findNode(layout, op.parentId);
|
|
283
|
+
if (!parent || !isParentNode(parent) || op.index > parent.children.length)
|
|
284
|
+
return refuse("Missing parent or insertion index outside its children.", opIndex);
|
|
285
|
+
}
|
|
286
|
+
switch (op.kind) {
|
|
287
|
+
case "insert": {
|
|
288
|
+
// Validate the raw subtree before arrange walks it; arrange is for already-checked nodes.
|
|
289
|
+
const isolated = validateLayout({
|
|
290
|
+
schemaVersion: layout.schemaVersion,
|
|
291
|
+
root: { id: wrapperId(op.node), type: "container", props: {}, children: [op.node] },
|
|
292
|
+
});
|
|
293
|
+
if (!isolated.ok) {
|
|
294
|
+
reason = summarizeIssues(isolated.issues);
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
const node = isolated.layout.root.children[0] as LayoutNode;
|
|
298
|
+
reason = nodeRefusal(node, options.extensionInstalled);
|
|
299
|
+
if (reason) break;
|
|
300
|
+
const added = addNode(layout, node, op);
|
|
301
|
+
if (!added.ok) reason = added.reason;
|
|
302
|
+
else layout = added.layout;
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
case "move": {
|
|
306
|
+
const moved = moveNode(layout, op.id, op.parentId, op.index);
|
|
307
|
+
if (!moved.ok) reason = moved.reason;
|
|
308
|
+
else layout = moved.layout;
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
case "update": {
|
|
312
|
+
const edited = editNode(layout, op, options.extensionInstalled);
|
|
313
|
+
if ("ok" in edited) reason = edited.reason;
|
|
314
|
+
else layout = edited;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case "delete": {
|
|
318
|
+
const removed = removeNode(layout, op.id);
|
|
319
|
+
if (!removed.removed) reason = `Cannot delete missing element ${op.id} or the page root.`;
|
|
320
|
+
else layout = removed.layout;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case "class": {
|
|
324
|
+
const next = editNamed(design.classes ?? [], op.id, op.value);
|
|
325
|
+
if ("ok" in next) reason = next.reason;
|
|
326
|
+
else design = Object.assign({}, design, { classes: next as DesignSystem["classes"] });
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
case "variable": {
|
|
330
|
+
const next = editNamed(design.variables[op.group] ?? [], op.id, op.value);
|
|
331
|
+
if ("ok" in next) reason = next.reason;
|
|
332
|
+
else
|
|
333
|
+
design = Object.assign({}, design, {
|
|
334
|
+
variables: Object.assign({}, design.variables, { [op.group]: next }),
|
|
335
|
+
}) as DesignSystem;
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (reason) return refuse(reason, opIndex);
|
|
340
|
+
const validLayout = validateLayout(layout);
|
|
341
|
+
const validDesign = validateDesign(design);
|
|
342
|
+
if (!validLayout.ok) return refuse(summarizeIssues(validLayout.issues), opIndex);
|
|
343
|
+
if (!validDesign.ok) return refuse(summarizeIssues(validDesign.issues), opIndex);
|
|
344
|
+
const misplaced = nestingIssues(validLayout.layout);
|
|
345
|
+
if (misplaced.length) return refuse(summarizeIssues(misplaced), opIndex);
|
|
346
|
+
const unsafe = proposalSafetyRefusal(
|
|
347
|
+
checkedLayout.layout,
|
|
348
|
+
validLayout.layout,
|
|
349
|
+
checkedDesign.design,
|
|
350
|
+
validDesign.design,
|
|
351
|
+
);
|
|
352
|
+
if (unsafe) return refuse(unsafe, opIndex);
|
|
353
|
+
layout = validLayout.layout;
|
|
354
|
+
design = validDesign.design;
|
|
355
|
+
}
|
|
356
|
+
return { ok: true, layout, design };
|
|
357
|
+
}
|
|
@@ -5,7 +5,7 @@ import type { StyleProps } from "../schema/style.ts";
|
|
|
5
5
|
import { nodeChildren, newNodeId } from "../tree-ops.ts";
|
|
6
6
|
|
|
7
7
|
type Color = NonNullable<StyleProps["color"]>;
|
|
8
|
-
type Size = NonNullable<StyleProps["
|
|
8
|
+
type Size = NonNullable<StyleProps["borderRadius"]>;
|
|
9
9
|
|
|
10
10
|
/** What a recipe takes from Site styles (W-320); unset parts fall back to plain values. */
|
|
11
11
|
export type RecipeTokens = {
|
|
@@ -430,5 +430,5 @@ export function recipeNode(
|
|
|
430
430
|
if (!recipe) return undefined;
|
|
431
431
|
const topLevel = layout && hasH1(layout.root) ? 2 : 1;
|
|
432
432
|
const node = withIds(recipe.build(siteTokens(design), { topLevel }), random);
|
|
433
|
-
return { ...node,
|
|
433
|
+
return { ...node, editorLabel: recipe.name } as LayoutNode;
|
|
434
434
|
}
|
|
@@ -76,11 +76,11 @@ const ALWAYS_INTERACTIVE = new Set([
|
|
|
76
76
|
|
|
77
77
|
function containsInteractive(node: LayoutNode): boolean {
|
|
78
78
|
if (ALWAYS_INTERACTIVE.has(node.type)) return true;
|
|
79
|
-
const props = node.props as { partId?: unknown
|
|
79
|
+
const props = node.props as { partId?: unknown };
|
|
80
80
|
// A synced section or Loop Item part is another layout this check can't see into.
|
|
81
|
-
if (node.type === "
|
|
81
|
+
if (node.type === "part-ref" && typeof props.partId === "string" && props.partId.trim())
|
|
82
82
|
return true;
|
|
83
|
-
if (node.type === "loop" && typeof props.
|
|
83
|
+
if (node.type === "loop" && typeof props.partId === "string" && props.partId.trim()) {
|
|
84
84
|
return true;
|
|
85
85
|
}
|
|
86
86
|
if (
|