@perkyzz/emvb 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -2
- package/package.json +72 -4
- package/src/admin/access/RequireEditor.tsx +39 -0
- package/src/admin/access/role.ts +36 -0
- package/src/admin/access/sidebar-shim.ts +108 -0
- package/src/admin/api.ts +48 -0
- package/src/admin/content-api.ts +147 -0
- package/src/admin/editor/Editor.tsx +457 -0
- package/src/admin/editor/EditorOverlay.tsx +54 -0
- package/src/admin/editor/ShortcutsDialog.tsx +30 -0
- package/src/admin/editor/SmallScreenNotice.tsx +20 -0
- package/src/admin/editor/TopBar.tsx +179 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +355 -0
- package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
- package/src/admin/editor/canvas/DeviceBar.tsx +50 -0
- package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
- package/src/admin/editor/canvas/SelectionOverlay.tsx +185 -0
- package/src/admin/editor/canvas/canvas-events.ts +232 -0
- package/src/admin/editor/canvas/state-preview.ts +19 -0
- package/src/admin/editor/canvas/tab-reveal.ts +35 -0
- package/src/admin/editor/canvas/vnode-react.tsx +49 -0
- package/src/admin/editor/clipboard-store.ts +44 -0
- package/src/admin/editor/dialogs.tsx +125 -0
- package/src/admin/editor/dnd/drag-stash.ts +27 -0
- package/src/admin/editor/dnd/drop-target.ts +101 -0
- package/src/admin/editor/dnd/new-element.ts +32 -0
- package/src/admin/editor/editor-css.ts +539 -0
- package/src/admin/editor/exit.ts +27 -0
- package/src/admin/editor/panels/AddPanel.tsx +142 -0
- package/src/admin/editor/panels/ClassesSection.tsx +334 -0
- package/src/admin/editor/panels/ColorControl.tsx +230 -0
- package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
- package/src/admin/editor/panels/ElementPanel.tsx +629 -0
- package/src/admin/editor/panels/LayersPanel.tsx +336 -0
- package/src/admin/editor/panels/LeftPanel.tsx +77 -0
- package/src/admin/editor/panels/PageSettings.tsx +126 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +101 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +392 -0
- package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +197 -0
- package/src/admin/editor/panels/VariableSection.tsx +492 -0
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +320 -0
- package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +349 -0
- package/src/admin/editor/panels/settings/DraftTextField.tsx +201 -0
- package/src/admin/editor/panels/settings/EffectsControls.tsx +347 -0
- package/src/admin/editor/panels/settings/FieldBindControl.tsx +83 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +150 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +97 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +109 -0
- package/src/admin/editor/panels/settings/LengthRow.tsx +181 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +124 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +218 -0
- package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
- package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +480 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +167 -0
- package/src/admin/editor/panels/settings/class-suggest.ts +51 -0
- package/src/admin/editor/panels/settings/length-units.ts +83 -0
- package/src/admin/editor/panels/settings/node-props.ts +11 -0
- package/src/admin/editor/panels/settings/style-sections.ts +291 -0
- package/src/admin/editor/panels/site-list.tsx +206 -0
- package/src/admin/editor/panels/variable-values.ts +73 -0
- package/src/admin/editor/section-templates.ts +50 -0
- package/src/admin/editor/shortcuts.ts +20 -0
- package/src/admin/editor/store.ts +212 -0
- package/src/admin/editor/useClipboardActions.ts +136 -0
- package/src/admin/editor/useEditorCommands.ts +142 -0
- package/src/admin/editor/useEditorData.ts +167 -0
- package/src/admin/editor/useEditorShortcuts.ts +148 -0
- package/src/admin/editor/useMediaQuery.ts +20 -0
- package/src/admin/editor/useNodeActions.ts +122 -0
- package/src/admin/editor/useSave.ts +185 -0
- package/src/admin/forms-api.ts +73 -0
- package/src/admin/index.tsx +20 -0
- package/src/admin/media-api.ts +68 -0
- package/src/admin/pages/CreateDialog.tsx +150 -0
- package/src/admin/pages/EditorPage.tsx +57 -0
- package/src/admin/pages/NewPageDialog.tsx +83 -0
- package/src/admin/pages/NewThemePartDialog.tsx +61 -0
- package/src/admin/pages/PageList.tsx +71 -0
- package/src/admin/pages/PagesPage.tsx +25 -0
- package/src/admin/pages/SetupShell.tsx +90 -0
- package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
- package/src/admin/pages/ThemePartList.tsx +202 -0
- package/src/admin/pages/list-kit.tsx +75 -0
- package/src/admin/setup/plan.ts +150 -0
- package/src/admin/setup/run.ts +61 -0
- package/src/admin/setup/spec.ts +100 -0
- package/src/admin/setup/useSetupStatus.ts +62 -0
- package/src/admin/theme-api.ts +186 -0
- package/src/admin/ui.ts +231 -0
- package/src/admin/widgets/LayoutField.tsx +67 -0
- package/src/astro/EmVBPage.astro +37 -0
- package/src/astro/env.d.ts +5 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
- package/src/astro/forms/index.ts +1 -0
- package/src/astro/forms-definitions.ts +104 -0
- package/src/astro/index.ts +15 -0
- package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
- package/src/astro/popups/index.ts +1 -0
- package/src/astro/popups/runtime.ts +246 -0
- package/src/astro/render.ts +74 -0
- package/src/astro/resolve-theme.ts +371 -0
- package/src/astro/resolve.ts +65 -0
- package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
- package/src/astro/tabs/index.ts +1 -0
- package/src/astro/tabs/runtime.ts +111 -0
- package/src/astro/theme-context.ts +81 -0
- package/src/astro/theme-posts.ts +84 -0
- package/src/constants.ts +12 -0
- package/src/core/arrange.ts +360 -0
- package/src/core/clipboard.ts +422 -0
- package/src/core/css/generate.ts +219 -0
- package/src/core/css/scope.ts +88 -0
- package/src/core/design/cascade.ts +28 -0
- package/src/core/design/classes.ts +138 -0
- package/src/core/design/devices.ts +56 -0
- package/src/core/design/states.ts +49 -0
- package/src/core/design/transfer.ts +33 -0
- package/src/core/design/variables.ts +319 -0
- package/src/core/elements/definition.ts +12 -0
- package/src/core/elements/dynamic-elements.ts +173 -0
- package/src/core/elements/form-elements.ts +204 -0
- package/src/core/elements/index.ts +742 -0
- package/src/core/elements/plain-text.ts +32 -0
- package/src/core/forms/binding.ts +61 -0
- package/src/core/forms/definition.ts +34 -0
- package/src/core/forms/success-layout.ts +63 -0
- package/src/core/icons/catalog.ts +273 -0
- package/src/core/index.ts +316 -0
- package/src/core/limits.ts +7 -0
- package/src/core/migrate/index.ts +76 -0
- package/src/core/render/context.ts +14 -0
- package/src/core/render/dynamic-post.ts +147 -0
- package/src/core/render/extras.ts +79 -0
- package/src/core/render/form.ts +137 -0
- package/src/core/render/index.ts +274 -0
- package/src/core/render/loop.ts +36 -0
- package/src/core/render/section.ts +33 -0
- package/src/core/render/tabs.ts +72 -0
- package/src/core/render/vnode.ts +87 -0
- package/src/core/sanitize/css.ts +591 -0
- package/src/core/sanitize/embed-url.ts +103 -0
- package/src/core/sanitize/escape.ts +10 -0
- package/src/core/sanitize/href.ts +6 -0
- package/src/core/sanitize/media-url.ts +7 -0
- package/src/core/sanitize/svg.ts +293 -0
- package/src/core/sanitize/url.ts +31 -0
- package/src/core/schema/descriptors.ts +33 -0
- package/src/core/schema/design.ts +88 -0
- package/src/core/schema/layout.ts +817 -0
- package/src/core/schema/state-names.ts +4 -0
- package/src/core/schema/style.ts +257 -0
- package/src/core/stats.ts +18 -0
- package/src/core/tabs/presence.ts +16 -0
- package/src/core/theme/conditions.ts +321 -0
- package/src/core/theme/dynamic.ts +133 -0
- package/src/core/theme/parse-doc.ts +36 -0
- package/src/core/theme/part-types.ts +79 -0
- package/src/core/theme/popup-markup.ts +39 -0
- package/src/core/theme/popup-rules.ts +61 -0
- package/src/core/theme/triggers.ts +104 -0
- package/src/core/tree-ops.ts +139 -0
- package/src/core/validate.ts +185 -0
- package/src/index.ts +18 -0
- package/src/server/design-routes.ts +125 -0
- package/src/server/hooks.ts +98 -0
- package/src/server/plugin.ts +39 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import { MAX_DEPTH, MAX_DESIGN_BYTES, MAX_LAYOUT_BYTES, MAX_NODES } from "./limits.ts";
|
|
3
|
+
import {
|
|
4
|
+
upgradeLayout,
|
|
5
|
+
type Migration,
|
|
6
|
+
type UpgradeResult,
|
|
7
|
+
DESIGN_MIGRATIONS,
|
|
8
|
+
LAYOUT_MIGRATIONS,
|
|
9
|
+
} from "./migrate/index.ts";
|
|
10
|
+
import { DESIGN_SCHEMA_VERSION, DesignSystem } from "./schema/design.ts";
|
|
11
|
+
import { Layout, LAYOUT_SCHEMA_VERSION } from "./schema/layout.ts";
|
|
12
|
+
|
|
13
|
+
export type LayoutIssue = { path: string; code: string; message: string };
|
|
14
|
+
export type LayoutValidation =
|
|
15
|
+
| { ok: true; layout: Layout; upgradedFrom: number }
|
|
16
|
+
| { ok: false; issues: LayoutIssue[] };
|
|
17
|
+
|
|
18
|
+
/** A short, user-facing summary of the first few issues, with their paths. */
|
|
19
|
+
export function summarizeIssues(issues: readonly LayoutIssue[], max = 3): string {
|
|
20
|
+
return issues
|
|
21
|
+
.slice(0, max)
|
|
22
|
+
.map((issue) => (issue.path ? `${issue.path}: ${issue.message}` : issue.message))
|
|
23
|
+
.join("; ");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function byteLength(value: unknown): number {
|
|
27
|
+
return new TextEncoder().encode(JSON.stringify(value)).length;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function formatPath(path: readonly PropertyKey[]): string {
|
|
31
|
+
return path.reduce<string>((out, key) => {
|
|
32
|
+
if (typeof key === "number") return `${out}[${key}]`;
|
|
33
|
+
return out ? `${out}.${String(key)}` : String(key);
|
|
34
|
+
}, "");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Iterative walk of the raw input, so absurd nesting can't overflow the stack in the schema parser. */
|
|
38
|
+
function checkStructure(root: unknown): LayoutIssue | undefined {
|
|
39
|
+
const stack: { node: unknown; depth: number; path: string }[] = [
|
|
40
|
+
{ node: root, depth: 1, path: "root" },
|
|
41
|
+
];
|
|
42
|
+
let count = 0;
|
|
43
|
+
while (stack.length > 0) {
|
|
44
|
+
const { node, depth, path } = stack.pop() as { node: unknown; depth: number; path: string };
|
|
45
|
+
count++;
|
|
46
|
+
if (count > MAX_NODES) {
|
|
47
|
+
return {
|
|
48
|
+
path: "root",
|
|
49
|
+
code: "too_many_nodes",
|
|
50
|
+
message: `A page can have at most ${MAX_NODES} elements.`,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (depth > MAX_DEPTH) {
|
|
54
|
+
return {
|
|
55
|
+
path,
|
|
56
|
+
code: "too_deep",
|
|
57
|
+
message: `Elements can be nested at most ${MAX_DEPTH} levels deep.`,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const children = (node as { children?: unknown } | null)?.children;
|
|
61
|
+
if (Array.isArray(children)) {
|
|
62
|
+
children.forEach((child, i) =>
|
|
63
|
+
stack.push({ node: child, depth: depth + 1, path: `${path}.children[${i}]` }),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function duplicateIds(layout: Layout): LayoutIssue[] {
|
|
71
|
+
const seen = new Set<string>();
|
|
72
|
+
const htmlIds = new Set<string>();
|
|
73
|
+
const issues: LayoutIssue[] = [];
|
|
74
|
+
const walk = (node: Layout["root"] | Layout["root"]["children"][number], path: string) => {
|
|
75
|
+
if (seen.has(node.id)) {
|
|
76
|
+
issues.push({
|
|
77
|
+
path: `${path}.id`,
|
|
78
|
+
code: "duplicate_id",
|
|
79
|
+
message: `The id "${node.id}" is used more than once.`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
seen.add(node.id);
|
|
83
|
+
if (node.htmlId) {
|
|
84
|
+
if (htmlIds.has(node.htmlId)) {
|
|
85
|
+
issues.push({
|
|
86
|
+
path: `${path}.htmlId`,
|
|
87
|
+
code: "duplicate_html_id",
|
|
88
|
+
message: `The CSS id "${node.htmlId}" is used more than once on this page.`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
htmlIds.add(node.htmlId);
|
|
92
|
+
}
|
|
93
|
+
const kids = "children" in node && Array.isArray(node.children) ? node.children : [];
|
|
94
|
+
kids.forEach((child, i) =>
|
|
95
|
+
walk(child as Layout["root"]["children"][number], `${path}.children[${i}]`),
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
walk(layout.root, "root");
|
|
99
|
+
return issues;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function checkSize(input: unknown, limit: number, what: string): LayoutIssue | undefined {
|
|
103
|
+
let bytes: number;
|
|
104
|
+
try {
|
|
105
|
+
bytes = byteLength(input);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (!(error instanceof RangeError)) throw error;
|
|
108
|
+
return {
|
|
109
|
+
path: "",
|
|
110
|
+
code: "too_deep",
|
|
111
|
+
message: `Elements can be nested at most ${MAX_DEPTH} levels deep.`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (bytes <= limit) return undefined;
|
|
115
|
+
return {
|
|
116
|
+
path: "",
|
|
117
|
+
code: "too_large",
|
|
118
|
+
message: `The ${what} is ${bytes} bytes; the limit is ${limit}.`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function upgradeIssue(upgraded: Extract<UpgradeResult, { ok: false }>): LayoutIssue {
|
|
123
|
+
const messages: Record<typeof upgraded.reason, string> = {
|
|
124
|
+
"not-an-object": "The document must be an object.",
|
|
125
|
+
"missing-version": "The document has no valid schemaVersion.",
|
|
126
|
+
"newer-version": `This was saved by a newer EmVB (schema ${upgraded.version}). Update EmVB to edit it.`,
|
|
127
|
+
"no-migration": `No migration exists from schema ${upgraded.version}.`,
|
|
128
|
+
};
|
|
129
|
+
return { path: "schemaVersion", code: upgraded.reason, message: messages[upgraded.reason] };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type DesignValidation =
|
|
133
|
+
| { ok: true; design: DesignSystem; upgradedFrom: number }
|
|
134
|
+
| { ok: false; issues: LayoutIssue[] };
|
|
135
|
+
|
|
136
|
+
/** Server-side check for the design document (D-013, N-005): 256 KiB budget, version, schema. */
|
|
137
|
+
export function validateDesign(
|
|
138
|
+
input: unknown,
|
|
139
|
+
migrations: Readonly<Record<number, Migration>> = DESIGN_MIGRATIONS,
|
|
140
|
+
): DesignValidation {
|
|
141
|
+
const sizeIssue = checkSize(input, MAX_DESIGN_BYTES, "design system");
|
|
142
|
+
if (sizeIssue) return { ok: false, issues: [sizeIssue] };
|
|
143
|
+
const upgraded = upgradeLayout(input, migrations, DESIGN_SCHEMA_VERSION);
|
|
144
|
+
if (!upgraded.ok) return { ok: false, issues: [upgradeIssue(upgraded)] };
|
|
145
|
+
const parsed = DesignSystem.safeParse(upgraded.doc);
|
|
146
|
+
if (!parsed.success) return { ok: false, issues: parsed.error.issues.map(toIssue) };
|
|
147
|
+
return { ok: true, design: parsed.data, upgradedFrom: upgraded.from };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Full server-side check for a stored layout (R-010, N-005): size budget, structure limits,
|
|
152
|
+
* version upgrade, schema, and id uniqueness. Returns path-specific issues.
|
|
153
|
+
*/
|
|
154
|
+
export function validateLayout(
|
|
155
|
+
input: unknown,
|
|
156
|
+
migrations: Readonly<Record<number, Migration>> = LAYOUT_MIGRATIONS,
|
|
157
|
+
): LayoutValidation {
|
|
158
|
+
const sizeIssue = checkSize(input, MAX_LAYOUT_BYTES, "page");
|
|
159
|
+
if (sizeIssue) return { ok: false, issues: [sizeIssue] };
|
|
160
|
+
const upgraded = upgradeLayout(input, migrations, LAYOUT_SCHEMA_VERSION);
|
|
161
|
+
if (!upgraded.ok) return { ok: false, issues: [upgradeIssue(upgraded)] };
|
|
162
|
+
const structure = checkStructure(upgraded.doc["root"]);
|
|
163
|
+
if (structure) return { ok: false, issues: [structure] };
|
|
164
|
+
const parsed = Layout.safeParse(upgraded.doc);
|
|
165
|
+
if (!parsed.success) {
|
|
166
|
+
return {
|
|
167
|
+
ok: false,
|
|
168
|
+
issues: parsed.error.issues.map((issue: z.core.$ZodIssue) => toIssue(issue)),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
const layout = parsed.data as Layout;
|
|
172
|
+
const dupes = duplicateIds(layout);
|
|
173
|
+
if (dupes.length > 0) return { ok: false, issues: dupes };
|
|
174
|
+
return { ok: true, layout, upgradedFrom: upgraded.from };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function toIssue(issue: z.core.$ZodIssue): LayoutIssue {
|
|
178
|
+
// LayoutNode's transform re-emits known-schema failures as `custom` with the original code
|
|
179
|
+
// in params.zodCode so path-specific messages stay intact (W-022).
|
|
180
|
+
const params =
|
|
181
|
+
"params" in issue ? (issue.params as { zodCode?: unknown } | undefined) : undefined;
|
|
182
|
+
const code =
|
|
183
|
+
issue.code === "custom" && typeof params?.zodCode === "string" ? params.zodCode : issue.code;
|
|
184
|
+
return { path: formatPath(issue.path), code, message: issue.message };
|
|
185
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PluginDescriptor } from "emdash";
|
|
2
|
+
import { ADMIN_ENTRY, PACKAGE_NAME, PLUGIN_ID, PLUGIN_VERSION } from "./constants.ts";
|
|
3
|
+
import { createPlugin } from "./server/plugin.ts";
|
|
4
|
+
|
|
5
|
+
/** Descriptor that host sites add to `emdash({ plugins: [...] })` in astro.config. */
|
|
6
|
+
export function emvb(): PluginDescriptor {
|
|
7
|
+
return {
|
|
8
|
+
id: PLUGIN_ID,
|
|
9
|
+
version: PLUGIN_VERSION,
|
|
10
|
+
format: "native",
|
|
11
|
+
entrypoint: PACKAGE_NAME,
|
|
12
|
+
adminEntry: ADMIN_ENTRY,
|
|
13
|
+
options: {},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { createPlugin };
|
|
18
|
+
export default createPlugin;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { PluginRouteError, type PluginRoute, type RouteContext } from "emdash";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import {
|
|
4
|
+
byteLength,
|
|
5
|
+
emptyDesign,
|
|
6
|
+
MAX_DESIGN_BYTES,
|
|
7
|
+
summarizeIssues,
|
|
8
|
+
validateDesign,
|
|
9
|
+
type DesignSystem,
|
|
10
|
+
} from "../core/index.ts";
|
|
11
|
+
import { DESIGN_DRAFT_KEY, DESIGN_KEY } from "../constants.ts";
|
|
12
|
+
|
|
13
|
+
type DesignStore = {
|
|
14
|
+
getVersioned(id: string): Promise<{ value: unknown; revision: string } | null>;
|
|
15
|
+
compareAndSet(
|
|
16
|
+
id: string,
|
|
17
|
+
expectedRevision: string | null,
|
|
18
|
+
data: unknown,
|
|
19
|
+
): Promise<{ applied: true; revision: string } | { applied: false }>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const store = (ctx: RouteContext<unknown>) => ctx.storage["design"] as unknown as DesignStore;
|
|
23
|
+
|
|
24
|
+
type DesignResponse = {
|
|
25
|
+
design: DesignSystem;
|
|
26
|
+
revision: string | null;
|
|
27
|
+
status: "ok" | "empty" | "unreadable";
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const readDesign = async (ctx: RouteContext<unknown>, key: string): Promise<DesignResponse> => {
|
|
31
|
+
const current = await store(ctx).getVersioned(key);
|
|
32
|
+
if (!current) return { design: emptyDesign(), revision: null, status: "empty" };
|
|
33
|
+
const result = validateDesign(current.value);
|
|
34
|
+
if (result.ok) return { design: result.design, revision: current.revision, status: "ok" };
|
|
35
|
+
ctx.log.error("emvb: stored design is unreadable", { code: result.issues[0]?.code });
|
|
36
|
+
return { design: emptyDesign(), revision: current.revision, status: "unreadable" };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Public: the published design only. Drafts stay on `design/draft` (W-100). */
|
|
40
|
+
export const designRoute: PluginRoute = {
|
|
41
|
+
public: true,
|
|
42
|
+
handler: (ctx) => readDesign(ctx, DESIGN_KEY),
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Editors and above. The working copy, or the published design when no draft exists yet. */
|
|
46
|
+
export const designDraftRoute: PluginRoute = {
|
|
47
|
+
permission: "content:edit_any",
|
|
48
|
+
handler: async (ctx) => {
|
|
49
|
+
const published = await readDesign(ctx, DESIGN_KEY);
|
|
50
|
+
const draft = await readDesign(ctx, DESIGN_DRAFT_KEY);
|
|
51
|
+
if (draft.status !== "ok") {
|
|
52
|
+
return {
|
|
53
|
+
design: published.design,
|
|
54
|
+
revision: null,
|
|
55
|
+
publishedRevision: published.revision,
|
|
56
|
+
unpublished: false,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
design: draft.design,
|
|
61
|
+
revision: draft.revision,
|
|
62
|
+
publishedRevision: published.revision,
|
|
63
|
+
unpublished: JSON.stringify(draft.design) !== JSON.stringify(published.design),
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const SaveInput = z.object({ design: z.unknown(), revision: z.string().nullable() });
|
|
69
|
+
|
|
70
|
+
/** Editors and above (D-020). Size is checked before CAS, which fails with a generic 500 above 1 MiB (S0-9). */
|
|
71
|
+
export const designSaveRoute: PluginRoute<z.infer<typeof SaveInput>> = {
|
|
72
|
+
permission: "content:edit_any",
|
|
73
|
+
input: SaveInput,
|
|
74
|
+
handler: async (ctx) => {
|
|
75
|
+
const bytes = byteLength(ctx.input.design);
|
|
76
|
+
if (bytes > MAX_DESIGN_BYTES) {
|
|
77
|
+
throw new PluginRouteError(
|
|
78
|
+
"DESIGN_TOO_LARGE",
|
|
79
|
+
`The design system is ${bytes} bytes; the limit is ${MAX_DESIGN_BYTES}.`,
|
|
80
|
+
413,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const result = validateDesign(ctx.input.design);
|
|
84
|
+
if (!result.ok) {
|
|
85
|
+
const summary = summarizeIssues(result.issues);
|
|
86
|
+
throw new PluginRouteError("INVALID_DESIGN", `The design system is invalid. ${summary}`, 422);
|
|
87
|
+
}
|
|
88
|
+
const write = await store(ctx).compareAndSet(
|
|
89
|
+
DESIGN_DRAFT_KEY,
|
|
90
|
+
ctx.input.revision,
|
|
91
|
+
result.design,
|
|
92
|
+
);
|
|
93
|
+
if (!write.applied) {
|
|
94
|
+
throw PluginRouteError.conflict(
|
|
95
|
+
"The design system was changed somewhere else. Reload it and try again.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return { revision: write.revision };
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const PublishInput = z.object({ publishedRevision: z.string().nullable() });
|
|
103
|
+
|
|
104
|
+
/** Copies the style draft onto the published design. Pages are not republished (W-100). */
|
|
105
|
+
export const designPublishRoute: PluginRoute<z.infer<typeof PublishInput>> = {
|
|
106
|
+
permission: "content:edit_any",
|
|
107
|
+
input: PublishInput,
|
|
108
|
+
handler: async (ctx) => {
|
|
109
|
+
const draft = await readDesign(ctx, DESIGN_DRAFT_KEY);
|
|
110
|
+
if (draft.status !== "ok") {
|
|
111
|
+
throw new PluginRouteError("NO_STYLE_DRAFT", "There are no style changes to publish.", 409);
|
|
112
|
+
}
|
|
113
|
+
const write = await store(ctx).compareAndSet(
|
|
114
|
+
DESIGN_KEY,
|
|
115
|
+
ctx.input.publishedRevision,
|
|
116
|
+
draft.design,
|
|
117
|
+
);
|
|
118
|
+
if (!write.applied) {
|
|
119
|
+
throw PluginRouteError.conflict(
|
|
120
|
+
"Site styles were published somewhere else. Reload the editor and try again.",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return { revision: write.revision };
|
|
124
|
+
},
|
|
125
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { ContentSaveRejectedError, type ContentHookEvent, type PluginContext } from "emdash";
|
|
2
|
+
import {
|
|
3
|
+
summarizeIssues,
|
|
4
|
+
validateConditions,
|
|
5
|
+
validateLayout,
|
|
6
|
+
validateTriggers,
|
|
7
|
+
} from "../core/index.ts";
|
|
8
|
+
import { EDITOR_ROLE, PAGES_COLLECTION, THEME_PARTS_COLLECTION } from "../constants.ts";
|
|
9
|
+
|
|
10
|
+
function parseJsonField(raw: unknown): unknown {
|
|
11
|
+
if (typeof raw !== "string") return raw;
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(raw) as unknown;
|
|
14
|
+
} catch {
|
|
15
|
+
return raw;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const OWNED = new Set([PAGES_COLLECTION, THEME_PARTS_COLLECTION]);
|
|
20
|
+
|
|
21
|
+
type FieldValidation =
|
|
22
|
+
| { ok: true; value: unknown }
|
|
23
|
+
| { ok: false; issues: { code: string; message: string }[] };
|
|
24
|
+
|
|
25
|
+
/** Theme-part fields validated on save, besides the layout (R-061). */
|
|
26
|
+
const THEME_PART_FIELDS: [string, (raw: unknown) => FieldValidation][] = [
|
|
27
|
+
[
|
|
28
|
+
"conditions",
|
|
29
|
+
(raw) => {
|
|
30
|
+
const result = validateConditions(raw);
|
|
31
|
+
return result.ok ? { ok: true, value: result.conditions } : result;
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
"triggers",
|
|
36
|
+
(raw) => {
|
|
37
|
+
const result = validateTriggers(raw);
|
|
38
|
+
return result.ok ? { ok: true, value: result.triggers } : result;
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `content:beforeSave` for EmVB pages and theme parts (R-006, D-020, R-060/R-061): editors and
|
|
45
|
+
* above only; validate layout (and conditions on theme parts). Logs carry ids and codes only.
|
|
46
|
+
*/
|
|
47
|
+
export async function beforeSave(
|
|
48
|
+
event: ContentHookEvent,
|
|
49
|
+
ctx: PluginContext,
|
|
50
|
+
): Promise<Record<string, unknown> | void> {
|
|
51
|
+
if (!OWNED.has(event.collection)) return;
|
|
52
|
+
const pageId = event.id ?? null;
|
|
53
|
+
const kind = event.collection === THEME_PARTS_COLLECTION ? "theme part" : "page";
|
|
54
|
+
if (event.actor && event.actor.role < EDITOR_ROLE) {
|
|
55
|
+
ctx.log.warn(`emvb: ${kind} save rejected`, { pageId, code: "role" });
|
|
56
|
+
throw new ContentSaveRejectedError(`Only editors and administrators can save EmVB ${kind}s.`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let next: Record<string, unknown> | undefined;
|
|
60
|
+
|
|
61
|
+
if (event.content["layout"] !== undefined) {
|
|
62
|
+
const result = validateLayout(parseJsonField(event.content["layout"]));
|
|
63
|
+
if (!result.ok) {
|
|
64
|
+
ctx.log.warn(`emvb: ${kind} save rejected`, {
|
|
65
|
+
pageId,
|
|
66
|
+
code: result.issues[0]?.code ?? "invalid",
|
|
67
|
+
});
|
|
68
|
+
throw new ContentSaveRejectedError(
|
|
69
|
+
`The ${kind} layout is invalid. ${summarizeIssues(result.issues)}`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (result.upgradedFrom !== result.layout.schemaVersion) {
|
|
73
|
+
next = { ...(next ?? event.content), layout: result.layout };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (event.collection === THEME_PARTS_COLLECTION) {
|
|
78
|
+
for (const [field, validate] of THEME_PART_FIELDS) {
|
|
79
|
+
if (event.content[field] === undefined) continue;
|
|
80
|
+
const result = validate(parseJsonField(event.content[field]));
|
|
81
|
+
if (!result.ok) {
|
|
82
|
+
ctx.log.warn("emvb: theme part save rejected", {
|
|
83
|
+
pageId,
|
|
84
|
+
code: result.issues[0]?.code ?? `invalid_${field}`,
|
|
85
|
+
});
|
|
86
|
+
throw new ContentSaveRejectedError(
|
|
87
|
+
`The theme part ${field} are invalid. ${result.issues
|
|
88
|
+
.slice(0, 3)
|
|
89
|
+
.map((i) => i.message)
|
|
90
|
+
.join("; ")}`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
next = { ...(next ?? event.content), [field]: result.value };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return next;
|
|
98
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { definePlugin } from "emdash";
|
|
2
|
+
import { ADMIN_ENTRY, PLUGIN_ID, PLUGIN_VERSION } from "../constants.ts";
|
|
3
|
+
import {
|
|
4
|
+
designDraftRoute,
|
|
5
|
+
designPublishRoute,
|
|
6
|
+
designRoute,
|
|
7
|
+
designSaveRoute,
|
|
8
|
+
} from "./design-routes.ts";
|
|
9
|
+
import { beforeSave } from "./hooks.ts";
|
|
10
|
+
|
|
11
|
+
export function createPlugin() {
|
|
12
|
+
return definePlugin({
|
|
13
|
+
id: PLUGIN_ID,
|
|
14
|
+
version: PLUGIN_VERSION,
|
|
15
|
+
capabilities: ["content:read", "content:write"],
|
|
16
|
+
storage: { design: { indexes: [] } },
|
|
17
|
+
hooks: {
|
|
18
|
+
"content:beforeSave": { handler: beforeSave, errorPolicy: "abort" },
|
|
19
|
+
},
|
|
20
|
+
admin: {
|
|
21
|
+
entry: ADMIN_ENTRY,
|
|
22
|
+
pages: [
|
|
23
|
+
{ path: "/pages", label: "Pages VisualBuilder", icon: "layout" },
|
|
24
|
+
{ path: "/theme", label: "Theme Builder", icon: "layout" },
|
|
25
|
+
],
|
|
26
|
+
fieldWidgets: [{ name: "layout", label: "EmVB page (read-only)", fieldTypes: ["json"] }],
|
|
27
|
+
},
|
|
28
|
+
routes: {
|
|
29
|
+
health: {
|
|
30
|
+
public: true,
|
|
31
|
+
handler: async () => ({ ok: true, plugin: PLUGIN_ID, version: PLUGIN_VERSION }),
|
|
32
|
+
},
|
|
33
|
+
design: designRoute,
|
|
34
|
+
"design/draft": designDraftRoute,
|
|
35
|
+
"design/save": designSaveRoute,
|
|
36
|
+
"design/publish": designPublishRoute,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|