@perkyzz/emvb 0.8.0 → 0.9.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.
Files changed (36) hide show
  1. package/README.md +2 -2
  2. package/package.json +4 -4
  3. package/src/admin/editor/Editor.tsx +38 -1
  4. package/src/admin/editor/FieldPreview.tsx +164 -0
  5. package/src/admin/editor/TopBar.tsx +32 -1
  6. package/src/admin/editor/editor-css.ts +3 -0
  7. package/src/admin/editor/host.ts +6 -0
  8. package/src/admin/editor/panels/ElementPanel.tsx +4 -1
  9. package/src/admin/editor/panels/settings/BackgroundControls.tsx +4 -5
  10. package/src/admin/editor/panels/settings/MediaPicker.tsx +5 -5
  11. package/src/admin/editor/panels/settings/length-units.ts +2 -1
  12. package/src/admin/index.tsx +5 -1
  13. package/src/admin/media-api.ts +27 -0
  14. package/src/admin/pages/AgentsPage.tsx +3 -3
  15. package/src/admin/pages/CollectionsPage.tsx +215 -0
  16. package/src/admin/setup/host-collections.ts +100 -0
  17. package/src/admin/widgets/CanvasField.tsx +202 -0
  18. package/src/admin/widgets/convert-body.ts +54 -0
  19. package/src/admin/widgets/field-fetcher.ts +79 -0
  20. package/src/astro/index.ts +2 -0
  21. package/src/astro/page-count.ts +77 -0
  22. package/src/astro/resolve-theme.ts +44 -2
  23. package/src/astro/resolve.ts +40 -4
  24. package/src/astro/responsive-images.ts +99 -0
  25. package/src/constants.ts +1 -1
  26. package/src/core/canvas-collections.ts +20 -0
  27. package/src/core/elements/dynamic-elements.ts +16 -1
  28. package/src/core/index.ts +2 -0
  29. package/src/core/portable-text.ts +127 -0
  30. package/src/core/render/pagination.ts +20 -1
  31. package/src/core/schema/layout.ts +5 -0
  32. package/src/core/theme/pagination.ts +9 -0
  33. package/src/index.ts +11 -1
  34. package/src/server/agent-settings.ts +16 -1
  35. package/src/server/hooks.ts +81 -0
  36. package/src/server/plugin.ts +20 -5
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { PluginDescriptor } from "emdash";
2
2
  import { ADMIN_ENTRY, PACKAGE_NAME, PLUGIN_ID, PLUGIN_VERSION } from "./constants.ts";
3
+ import { canvasCollectionSlugs } from "./core/canvas-collections.ts";
3
4
  import { canvasStyleUrls } from "./core/canvas-styles.ts";
4
5
  import { createPlugin } from "./server/plugin.ts";
5
6
 
@@ -11,18 +12,27 @@ export type EmVBOptions = {
11
12
  * (`/styles/site.css`); anything else is ignored.
12
13
  */
13
14
  canvasStyles?: string[];
15
+ /**
16
+ * Host collections (such as `pages`) whose entries can hold an `emvb:canvas` "Edit with EmVB"
17
+ * field (W-897). EmVB validates and guards that field on save and publish for these only.
18
+ */
19
+ canvasCollections?: string[];
14
20
  };
15
21
 
16
22
  /** Descriptor that host sites add to `emdash({ plugins: [...] })` in astro.config. */
17
23
  export function emvb(options: EmVBOptions = {}): PluginDescriptor {
18
24
  const canvasStyles = canvasStyleUrls(options.canvasStyles);
25
+ const canvasCollections = canvasCollectionSlugs(options.canvasCollections);
19
26
  return {
20
27
  id: PLUGIN_ID,
21
28
  version: PLUGIN_VERSION,
22
29
  format: "native",
23
30
  entrypoint: PACKAGE_NAME,
24
31
  adminEntry: ADMIN_ENTRY,
25
- options: canvasStyles.length > 0 ? { canvasStyles } : {},
32
+ options: {
33
+ ...(canvasStyles.length > 0 ? { canvasStyles } : {}),
34
+ ...(canvasCollections.length > 0 ? { canvasCollections } : {}),
35
+ },
26
36
  };
27
37
  }
28
38
 
@@ -7,6 +7,7 @@ import {
7
7
  } from "emdash";
8
8
  import { z } from "zod";
9
9
  import { PAGES_COLLECTION, THEME_PARTS_COLLECTION } from "../constants.ts";
10
+ import { canvasFieldSlugs } from "./hooks.ts";
10
11
  import { AgentSettings, DEFAULT_AGENT_SETTINGS } from "../mcp/settings.ts";
11
12
 
12
13
  /** Plugin KV key of the agent settings (plan § 4, D-055). */
@@ -145,10 +146,14 @@ const OWNED = new Set([PAGES_COLLECTION, THEME_PARTS_COLLECTION]);
145
146
  export async function beforePublish(
146
147
  event: ContentPolicyEvent,
147
148
  ctx: PluginContext,
149
+ canvasCollections: readonly string[] = [],
148
150
  ): Promise<ContentPolicyDecision> {
149
- if (!OWNED.has(event.collection)) return undefined;
151
+ const hostEntry = canvasCollections.includes(event.collection);
152
+ if (!OWNED.has(event.collection) && !hostEntry) return undefined;
150
153
  const source = event.origin.source === "mcp" || event.actor?.source === "mcp";
151
154
  if (!source) return undefined;
155
+ // W-899: a host entry falls under the EmVB rule only while it holds an EmVB layout.
156
+ if (hostEntry && !(await holdsCanvasLayout(event, ctx))) return undefined;
152
157
  const { settings } = await readAgentSettings(ctx);
153
158
  if (settings.mcpEnabled && settings.publishPages) return undefined;
154
159
  return {
@@ -157,3 +162,13 @@ export async function beforePublish(
157
162
  "Agents may not publish EmVB pages on this site. An admin can allow it in Pages VisualBuilder → AI agents.",
158
163
  };
159
164
  }
165
+
166
+ async function holdsCanvasLayout(event: ContentPolicyEvent, ctx: PluginContext): Promise<boolean> {
167
+ const record = event.content;
168
+ const data =
169
+ record["data"] && typeof record["data"] === "object"
170
+ ? (record["data"] as Record<string, unknown>)
171
+ : record;
172
+ const slugs = await canvasFieldSlugs(ctx, event.collection);
173
+ return slugs.some((slug) => data[slug] !== null && data[slug] !== undefined && data[slug] !== "");
174
+ }
@@ -58,7 +58,9 @@ const THEME_PART_FIELDS: [string, (raw: unknown) => FieldValidation, string?][]
58
58
  export async function beforeSave(
59
59
  event: ContentHookEvent,
60
60
  ctx: PluginContext,
61
+ canvasCollections: readonly string[] = [],
61
62
  ): Promise<Record<string, unknown> | void> {
63
+ if (canvasCollections.includes(event.collection)) return canvasFieldsBeforeSave(event, ctx);
62
64
  if (!OWNED.has(event.collection)) return;
63
65
  const pageId = event.id ?? null;
64
66
  const kind = event.collection === THEME_PARTS_COLLECTION ? "theme part" : "page";
@@ -118,3 +120,82 @@ export async function beforeSave(
118
120
 
119
121
  return next;
120
122
  }
123
+
124
+ /** Widget id a host field picks for "Edit with EmVB" (W-P02). */
125
+ const CANVAS_WIDGET = "emvb:canvas";
126
+
127
+ /** Slugs of the collection's fields that use the `emvb:canvas` widget. */
128
+ export async function canvasFieldSlugs(ctx: PluginContext, collection: string): Promise<string[]> {
129
+ const info = await ctx.schema?.getCollection(collection);
130
+ return (info?.fields ?? []).filter((f) => f.widget === CANVAS_WIDGET).map((f) => f.slug);
131
+ }
132
+
133
+ function isEmpty(value: unknown): boolean {
134
+ return value === null || value === undefined || value === "";
135
+ }
136
+
137
+ /** Same value whether it arrives as a JSON string or an object. */
138
+ function sameValue(a: unknown, b: unknown): boolean {
139
+ const left = parseJsonField(a);
140
+ const right = parseJsonField(b);
141
+ if (isEmpty(left) && isEmpty(right)) return true;
142
+ return JSON.stringify(left) === JSON.stringify(right);
143
+ }
144
+
145
+ /**
146
+ * W-898: `emvb:canvas` fields in host collections listed in `canvasCollections`. Editors and above
147
+ * may set, change or clear them, and a set value must be a valid layout. Lower roles may still
148
+ * save the entry, but the field must stay as stored (D-061), so they can't clear or wipe it.
149
+ */
150
+ async function canvasFieldsBeforeSave(
151
+ event: ContentHookEvent,
152
+ ctx: PluginContext,
153
+ ): Promise<Record<string, unknown> | void> {
154
+ const pageId = event.id ?? null;
155
+ const fields = (await canvasFieldSlugs(ctx, event.collection)).filter(
156
+ (slug) => event.content[slug] !== undefined,
157
+ );
158
+ if (fields.length === 0) return;
159
+
160
+ const lowerRole = event.actor !== undefined && event.actor.role < EDITOR_ROLE;
161
+ if (lowerRole) {
162
+ const stored = event.id ? await ctx.content?.get(event.collection, event.id) : null;
163
+ for (const slug of fields) {
164
+ if (sameValue(event.content[slug], stored?.data[slug])) continue;
165
+ ctx.log.warn("emvb: canvas field save rejected", { pageId, code: "role" });
166
+ throw new ContentSaveRejectedError(
167
+ "Only editors and administrators can change an EmVB layout. Your other changes were not saved.",
168
+ );
169
+ }
170
+ return;
171
+ }
172
+
173
+ let next: Record<string, unknown> | undefined;
174
+ for (const slug of fields) {
175
+ const raw = parseJsonField(event.content[slug]);
176
+ if (isEmpty(raw)) continue;
177
+ const result = validateLayout(raw);
178
+ if (!result.ok) {
179
+ ctx.log.warn("emvb: canvas field save rejected", {
180
+ pageId,
181
+ code: result.issues[0]?.code ?? "invalid",
182
+ });
183
+ throw new ContentSaveRejectedError(
184
+ `The EmVB layout is invalid. ${summarizeIssues(result.issues)}`,
185
+ );
186
+ }
187
+ const wrongPlace = nestingIssues(result.layout, 3);
188
+ if (wrongPlace.length > 0) {
189
+ ctx.log.warn("emvb: canvas field save rejected", { pageId, code: "nesting" });
190
+ throw new ContentSaveRejectedError(
191
+ `The EmVB layout has elements in the wrong place. ${wrongPlace
192
+ .map((issue) => issue.message)
193
+ .join("; ")}`,
194
+ );
195
+ }
196
+ if (result.layout.schemaVersion !== result.upgradedFrom) {
197
+ next = { ...(next ?? event.content), [slug]: result.layout };
198
+ }
199
+ }
200
+ return next;
201
+ }
@@ -22,16 +22,22 @@ import {
22
22
  pageAgentVersionsRoute,
23
23
  } from "./agent-settings.ts";
24
24
  import { canvasStyleUrls } from "../core/canvas-styles.ts";
25
+ import { canvasCollectionSlugs } from "../core/canvas-collections.ts";
25
26
  import { TOOLS } from "../mcp/tools.ts";
26
27
  import { mcpRoutes, mcpToolConfig } from "./mcp-routes.ts";
27
28
 
28
- export function createPlugin(options: { canvasStyles?: unknown } = {}) {
29
+ export function createPlugin(
30
+ options: { canvasStyles?: unknown; canvasCollections?: unknown } = {},
31
+ ) {
29
32
  const canvasStyles = canvasStyleUrls(options.canvasStyles);
33
+ const canvasCollections = canvasCollectionSlugs(options.canvasCollections);
30
34
  return definePlugin({
31
35
  id: PLUGIN_ID,
32
36
  version: PLUGIN_VERSION,
33
37
  capabilities: [
34
38
  "content:read",
39
+ // W-898: find host fields that use the emvb:canvas widget.
40
+ "schema:read",
35
41
  "content:write",
36
42
  "content:publish",
37
43
  "content:revisions:read",
@@ -48,9 +54,14 @@ export function createPlugin(options: { canvasStyles?: unknown } = {}) {
48
54
  emvb_saved_sections: { indexes: ["updatedAt"] },
49
55
  },
50
56
  hooks: {
51
- "content:beforeSave": { handler: beforeSave, errorPolicy: "abort" },
57
+ "content:beforeSave": {
58
+ handler: (event, ctx) => beforeSave(event, ctx, canvasCollections),
59
+ errorPolicy: "abort",
60
+ },
52
61
  // D-055: EmDash's own MCP tools can't publish EmVB pages unless the admin allowed it.
53
- "content:beforePublish": { handler: beforePublish },
62
+ "content:beforePublish": {
63
+ handler: (event, ctx) => beforePublish(event, ctx, canvasCollections),
64
+ },
54
65
  },
55
66
  admin: {
56
67
  entry: ADMIN_ENTRY,
@@ -58,8 +69,12 @@ export function createPlugin(options: { canvasStyles?: unknown } = {}) {
58
69
  { path: "/pages", label: "Pages VisualBuilder", icon: "layout" },
59
70
  { path: "/theme", label: "Theme Builder", icon: "layout" },
60
71
  { path: "/agents", label: "AI agents", icon: "layout" },
72
+ { path: "/collections", label: "Host collections", icon: "layout" },
73
+ ],
74
+ fieldWidgets: [
75
+ { name: "layout", label: "EmVB page (read-only)", fieldTypes: ["json"] },
76
+ { name: "canvas", label: "Edit with EmVB", fieldTypes: ["json"] },
61
77
  ],
62
- fieldWidgets: [{ name: "layout", label: "EmVB page (read-only)", fieldTypes: ["json"] }],
63
78
  },
64
79
  routes: {
65
80
  health: {
@@ -67,7 +82,7 @@ export function createPlugin(options: { canvasStyles?: unknown } = {}) {
67
82
  handler: async () => ({ ok: true, plugin: PLUGIN_ID, version: PLUGIN_VERSION }),
68
83
  },
69
84
  // W-327: what the editor needs from the host's `emvb()` options.
70
- "editor/config": { handler: async () => ({ canvasStyles }) },
85
+ "editor/config": { handler: async () => ({ canvasStyles, canvasCollections }) },
71
86
  design: designRoute,
72
87
  "design/draft": designDraftRoute,
73
88
  "design/save": designSaveRoute,