@perkyzz/emvb 0.7.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 (97) hide show
  1. package/README.md +3 -3
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +7 -7
  4. package/src/admin/editor/DialogCloseButton.tsx +16 -0
  5. package/src/admin/editor/Editor.tsx +71 -1
  6. package/src/admin/editor/FieldPreview.tsx +164 -0
  7. package/src/admin/editor/PerfMeter.tsx +3 -9
  8. package/src/admin/editor/SavedSectionDialogs.tsx +169 -0
  9. package/src/admin/editor/ShareLink.tsx +2 -7
  10. package/src/admin/editor/ShortcutsDialog.tsx +19 -11
  11. package/src/admin/editor/Timeline.tsx +2 -7
  12. package/src/admin/editor/TopBar.tsx +32 -1
  13. package/src/admin/editor/dialogs.tsx +3 -14
  14. package/src/admin/editor/editor-chrome.tsx +8 -24
  15. package/src/admin/editor/editor-commands.ts +15 -1
  16. package/src/admin/editor/editor-css.ts +6 -2
  17. package/src/admin/editor/feature-css.ts +11 -1
  18. package/src/admin/editor/focus-when-lost.ts +23 -0
  19. package/src/admin/editor/host.ts +6 -0
  20. package/src/admin/editor/icons/IconLibraryDialog.tsx +2 -7
  21. package/src/admin/editor/palette-items.ts +17 -0
  22. package/src/admin/editor/panels/AddPanel.tsx +9 -1
  23. package/src/admin/editor/panels/ElementPanel.tsx +4 -1
  24. package/src/admin/editor/panels/LayersPanel.tsx +6 -1
  25. package/src/admin/editor/panels/LeftPanel.tsx +9 -1
  26. package/src/admin/editor/panels/SavedSectionsGroup.tsx +246 -0
  27. package/src/admin/editor/panels/SiteStylesDrawer.tsx +54 -10
  28. package/src/admin/editor/panels/TokenScales.tsx +16 -16
  29. package/src/admin/editor/panels/TriggersEditor.tsx +1 -1
  30. package/src/admin/editor/panels/color-space.ts +14 -26
  31. package/src/admin/editor/panels/settings/AudienceEditor.tsx +36 -39
  32. package/src/admin/editor/panels/settings/BackgroundControls.tsx +4 -5
  33. package/src/admin/editor/panels/settings/IconControls.tsx +21 -5
  34. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
  35. package/src/admin/editor/panels/settings/MediaPicker.tsx +5 -5
  36. package/src/admin/editor/panels/settings/length-units.ts +2 -1
  37. package/src/admin/editor/shortcuts.ts +14 -0
  38. package/src/admin/editor/useSavedSections.ts +197 -0
  39. package/src/admin/image-editor/Canvas.tsx +61 -17
  40. package/src/admin/image-editor/ImageEditorDialog.tsx +130 -1
  41. package/src/admin/image-editor/image-editor-css.ts +11 -8
  42. package/src/admin/image-editor/parts.tsx +18 -17
  43. package/src/admin/image-editor/save.ts +31 -1
  44. package/src/admin/index.tsx +5 -1
  45. package/src/admin/media-api.ts +27 -0
  46. package/src/admin/pages/AgentsPage.tsx +3 -3
  47. package/src/admin/pages/CollectionsPage.tsx +215 -0
  48. package/src/admin/pages/ThemePartList.tsx +32 -4
  49. package/src/admin/saved-sections-api.ts +51 -0
  50. package/src/admin/setup/host-collections.ts +100 -0
  51. package/src/admin/theme-api.ts +3 -2
  52. package/src/admin/ui.ts +9 -0
  53. package/src/admin/widgets/CanvasField.tsx +202 -0
  54. package/src/admin/widgets/convert-body.ts +54 -0
  55. package/src/admin/widgets/field-fetcher.ts +79 -0
  56. package/src/astro/index.ts +2 -0
  57. package/src/astro/page-count.ts +77 -0
  58. package/src/astro/popups/runtime.ts +8 -6
  59. package/src/astro/resolve-theme.ts +44 -2
  60. package/src/astro/resolve.ts +40 -4
  61. package/src/astro/responsive-images.ts +99 -0
  62. package/src/constants.ts +1 -1
  63. package/src/core/a11y/audit.ts +32 -7
  64. package/src/core/arrange.ts +21 -6
  65. package/src/core/canvas-collections.ts +20 -0
  66. package/src/core/css/print.ts +5 -0
  67. package/src/core/design/classes.ts +23 -9
  68. package/src/core/design/devices.ts +7 -13
  69. package/src/core/design/states.ts +7 -13
  70. package/src/core/elements/dynamic-elements.ts +27 -12
  71. package/src/core/elements/index.ts +1 -1
  72. package/src/core/elements/menu.ts +3 -0
  73. package/src/core/icons/animation-runs.ts +11 -0
  74. package/src/core/index.ts +22 -0
  75. package/src/core/migrate/index.ts +3 -0
  76. package/src/core/portable-text.ts +127 -0
  77. package/src/core/render/loop.ts +1 -1
  78. package/src/core/render/pagination.ts +20 -1
  79. package/src/core/sanitize/css.ts +14 -6
  80. package/src/core/saved-sections.ts +318 -0
  81. package/src/core/schema/design.ts +1 -1
  82. package/src/core/schema/layout.ts +6 -1
  83. package/src/core/schema/style.ts +4 -2
  84. package/src/core/theme/float.ts +3 -0
  85. package/src/core/theme/pagination.ts +34 -24
  86. package/src/core/theme/part-types.ts +4 -4
  87. package/src/index.ts +11 -1
  88. package/src/mcp/ports.ts +22 -1
  89. package/src/mcp/read.ts +1 -1
  90. package/src/mcp/sections.ts +183 -0
  91. package/src/mcp/tools.ts +44 -0
  92. package/src/mcp/write.ts +4 -4
  93. package/src/server/agent-settings.ts +16 -1
  94. package/src/server/hooks.ts +81 -0
  95. package/src/server/mcp-routes.ts +2 -0
  96. package/src/server/plugin.ts +34 -5
  97. package/src/server/saved-sections.ts +163 -0
@@ -6,6 +6,13 @@ import {
6
6
  designRoute,
7
7
  designSaveRoute,
8
8
  } from "./design-routes.ts";
9
+ import {
10
+ savedSectionDeleteRoute,
11
+ savedSectionGetRoute,
12
+ savedSectionRenameRoute,
13
+ savedSectionSaveRoute,
14
+ savedSectionsRoute,
15
+ } from "./saved-sections.ts";
9
16
  import { iconDeleteRoute, iconRenameRoute, iconUploadRoute, iconsRoute } from "./icon-routes.ts";
10
17
  import { beforeSave } from "./hooks.ts";
11
18
  import {
@@ -15,16 +22,22 @@ import {
15
22
  pageAgentVersionsRoute,
16
23
  } from "./agent-settings.ts";
17
24
  import { canvasStyleUrls } from "../core/canvas-styles.ts";
25
+ import { canvasCollectionSlugs } from "../core/canvas-collections.ts";
18
26
  import { TOOLS } from "../mcp/tools.ts";
19
27
  import { mcpRoutes, mcpToolConfig } from "./mcp-routes.ts";
20
28
 
21
- export function createPlugin(options: { canvasStyles?: unknown } = {}) {
29
+ export function createPlugin(
30
+ options: { canvasStyles?: unknown; canvasCollections?: unknown } = {},
31
+ ) {
22
32
  const canvasStyles = canvasStyleUrls(options.canvasStyles);
33
+ const canvasCollections = canvasCollectionSlugs(options.canvasCollections);
23
34
  return definePlugin({
24
35
  id: PLUGIN_ID,
25
36
  version: PLUGIN_VERSION,
26
37
  capabilities: [
27
38
  "content:read",
39
+ // W-898: find host fields that use the emvb:canvas widget.
40
+ "schema:read",
28
41
  "content:write",
29
42
  "content:publish",
30
43
  "content:revisions:read",
@@ -37,11 +50,18 @@ export function createPlugin(options: { canvasStyles?: unknown } = {}) {
37
50
  // D-057: agent changes and the site styles history.
38
51
  agentlog: { indexes: ["at", "pageId"] },
39
52
  designhistory: { indexes: ["at"] },
53
+ // W-875 (D-060): saved sections, by name, per site.
54
+ emvb_saved_sections: { indexes: ["updatedAt"] },
40
55
  },
41
56
  hooks: {
42
- "content:beforeSave": { handler: beforeSave, errorPolicy: "abort" },
57
+ "content:beforeSave": {
58
+ handler: (event, ctx) => beforeSave(event, ctx, canvasCollections),
59
+ errorPolicy: "abort",
60
+ },
43
61
  // D-055: EmDash's own MCP tools can't publish EmVB pages unless the admin allowed it.
44
- "content:beforePublish": { handler: beforePublish },
62
+ "content:beforePublish": {
63
+ handler: (event, ctx) => beforePublish(event, ctx, canvasCollections),
64
+ },
45
65
  },
46
66
  admin: {
47
67
  entry: ADMIN_ENTRY,
@@ -49,8 +69,12 @@ export function createPlugin(options: { canvasStyles?: unknown } = {}) {
49
69
  { path: "/pages", label: "Pages VisualBuilder", icon: "layout" },
50
70
  { path: "/theme", label: "Theme Builder", icon: "layout" },
51
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"] },
52
77
  ],
53
- fieldWidgets: [{ name: "layout", label: "EmVB page (read-only)", fieldTypes: ["json"] }],
54
78
  },
55
79
  routes: {
56
80
  health: {
@@ -58,12 +82,17 @@ export function createPlugin(options: { canvasStyles?: unknown } = {}) {
58
82
  handler: async () => ({ ok: true, plugin: PLUGIN_ID, version: PLUGIN_VERSION }),
59
83
  },
60
84
  // W-327: what the editor needs from the host's `emvb()` options.
61
- "editor/config": { handler: async () => ({ canvasStyles }) },
85
+ "editor/config": { handler: async () => ({ canvasStyles, canvasCollections }) },
62
86
  design: designRoute,
63
87
  "design/draft": designDraftRoute,
64
88
  "design/save": designSaveRoute,
65
89
  "design/publish": designPublishRoute,
66
90
  icons: iconsRoute,
91
+ "saved-sections": savedSectionsRoute,
92
+ "saved-sections/get": savedSectionGetRoute,
93
+ "saved-sections/save": savedSectionSaveRoute,
94
+ "saved-sections/rename": savedSectionRenameRoute,
95
+ "saved-sections/delete": savedSectionDeleteRoute,
67
96
  "icons/upload": iconUploadRoute,
68
97
  "icons/rename": iconRenameRoute,
69
98
  "icons/delete": iconDeleteRoute,
@@ -0,0 +1,163 @@
1
+ import { PluginRouteError, type PluginRoute, type RouteContext } from "emdash";
2
+ import { z } from "zod";
3
+ import {
4
+ MAX_SAVED_SECTIONS,
5
+ readSavedSection,
6
+ savedSectionName,
7
+ savedSectionRecord,
8
+ SAVED_SECTION_REASONS,
9
+ type SavedSectionRecord,
10
+ type SavedSectionSummary,
11
+ } from "../core/index.ts";
12
+ import type { SavedSectionsPort } from "../mcp/ports.ts";
13
+ import { queued } from "../mcp/queue.ts";
14
+
15
+ /**
16
+ * Saved sections (W-875, D-060): named copies of an element kept per site in plugin storage
17
+ * `emvb_saved_sections`. Every row read goes through `readSavedSection`, so an old save comes
18
+ * back upgraded and a damaged or hand-written one is never inserted. Writes take the same
19
+ * per-isolate queue as the MCP tools.
20
+ */
21
+ const SAVED_SECTIONS_COLLECTION = "emvb_saved_sections";
22
+
23
+ type SavedSectionStore = {
24
+ get(id: string): Promise<unknown>;
25
+ put(id: string, data: SavedSectionRecord): Promise<void>;
26
+ delete(id: string): Promise<boolean>;
27
+ query(options?: {
28
+ orderBy?: Record<string, "asc" | "desc">;
29
+ limit?: number;
30
+ }): Promise<{ items: Array<{ id: string; data: unknown }>; hasMore: boolean }>;
31
+ };
32
+
33
+ const QUEUE = "saved-sections";
34
+ const ID = /^[0-9a-f]{16}$/;
35
+ const FULL_REASON = `My sections holds up to ${MAX_SAVED_SECTIONS} sections. Delete one you no longer use, then save again.`;
36
+
37
+ function summaryOf(id: string, data: unknown): SavedSectionSummary {
38
+ const read = readSavedSection(data);
39
+ if (read.ok) {
40
+ const { name, updatedAt, classes } = read.section;
41
+ return { id, name, updatedAt, classCount: classes.length };
42
+ }
43
+ const raw = (data ?? {}) as { name?: unknown; updatedAt?: unknown };
44
+ return {
45
+ id,
46
+ name: (typeof raw.name === "string" && savedSectionName(raw.name)) || "Damaged section",
47
+ updatedAt: typeof raw.updatedAt === "string" ? raw.updatedAt : "",
48
+ classCount: 0,
49
+ damaged: true,
50
+ };
51
+ }
52
+
53
+ /** The port the routes and the MCP tools share, on a plugin storage collection. */
54
+ function savedSectionsPort(store: SavedSectionStore): SavedSectionsPort {
55
+ const all = () => store.query({ orderBy: { updatedAt: "desc" }, limit: MAX_SAVED_SECTIONS });
56
+ return {
57
+ async list() {
58
+ const page = await all();
59
+ return page.items.map(({ id, data }) => summaryOf(id, data));
60
+ },
61
+ async get(id) {
62
+ if (!ID.test(id)) return null;
63
+ const data = await store.get(id);
64
+ if (data === null || data === undefined) return null;
65
+ const read = readSavedSection(data);
66
+ return read.ok ? read.section : read;
67
+ },
68
+ create: (record) =>
69
+ queued(QUEUE, async () => {
70
+ const read = readSavedSection(record);
71
+ if (!read.ok) return read;
72
+ if ((await all()).items.length >= MAX_SAVED_SECTIONS) {
73
+ return { ok: false as const, reason: FULL_REASON };
74
+ }
75
+ const id = crypto.randomUUID().replaceAll("-", "").slice(0, 16);
76
+ await store.put(id, savedSectionRecord(read.section));
77
+ return { id };
78
+ }),
79
+ rename: (id, rawName) =>
80
+ queued(QUEUE, async () => {
81
+ if (!ID.test(id)) return null;
82
+ const data = await store.get(id);
83
+ if (data === null || data === undefined) return null;
84
+ const name = savedSectionName(rawName);
85
+ if (!name) return { ok: false as const, reason: SAVED_SECTION_REASONS.name };
86
+ const read = readSavedSection(data);
87
+ if (!read.ok) return read;
88
+ const section = { ...read.section, name, updatedAt: new Date().toISOString() };
89
+ await store.put(id, savedSectionRecord(section));
90
+ return summaryOf(id, savedSectionRecord(section));
91
+ }),
92
+ remove: (id) => queued(QUEUE, async () => ID.test(id) && (await store.delete(id))),
93
+ };
94
+ }
95
+
96
+ export const portOf = (ctx: RouteContext<unknown>) =>
97
+ savedSectionsPort(ctx.storage[SAVED_SECTIONS_COLLECTION] as unknown as SavedSectionStore);
98
+
99
+ const NOT_FOUND = "That section is not in My sections.";
100
+ const refusedError = (reason: string) => new PluginRouteError("REFUSED", reason, 422);
101
+
102
+ /** Editors and above: the saved sections, newest first, by name. */
103
+ export const savedSectionsRoute: PluginRoute = {
104
+ permission: "content:edit_any",
105
+ handler: async (ctx) => ({ items: await portOf(ctx).list() }),
106
+ };
107
+
108
+ const IdInput = z.object({ id: z.string().regex(ID, "Unknown section") });
109
+
110
+ /** Editors and above: one section, read and upgraded, ready for `prepareSavedSection`. */
111
+ export const savedSectionGetRoute: PluginRoute<z.infer<typeof IdInput>> = {
112
+ permission: "content:edit_any",
113
+ input: IdInput,
114
+ handler: async (ctx) => {
115
+ const section = await portOf(ctx).get(ctx.input.id);
116
+ if (!section) throw new PluginRouteError("NOT_FOUND", NOT_FOUND, 404);
117
+ if ("ok" in section) throw refusedError(section.reason);
118
+ return { section };
119
+ },
120
+ };
121
+
122
+ /** A record from the editor; the server checks it again, whatever the client did. */
123
+ const SaveInput = z.object({ record: z.record(z.string(), z.unknown()) });
124
+
125
+ export const savedSectionSaveRoute: PluginRoute<z.infer<typeof SaveInput>> = {
126
+ permission: "content:edit_any",
127
+ input: SaveInput,
128
+ handler: async (ctx) => {
129
+ const now = new Date().toISOString();
130
+ const record = { ...ctx.input.record, createdAt: now, updatedAt: now };
131
+ const made = await portOf(ctx).create(record as SavedSectionRecord);
132
+ if ("ok" in made) {
133
+ const full = made.reason === FULL_REASON;
134
+ throw new PluginRouteError(full ? "SECTIONS_FULL" : "REFUSED", made.reason, full ? 409 : 422);
135
+ }
136
+ return { id: made.id };
137
+ },
138
+ };
139
+
140
+ const RenameInput = IdInput.extend({ name: z.string().min(1).max(255) });
141
+
142
+ export const savedSectionRenameRoute: PluginRoute<z.infer<typeof RenameInput>> = {
143
+ permission: "content:edit_any",
144
+ input: RenameInput,
145
+ handler: async (ctx) => {
146
+ const item = await portOf(ctx).rename(ctx.input.id, ctx.input.name);
147
+ if (!item) throw new PluginRouteError("NOT_FOUND", NOT_FOUND, 404);
148
+ if ("ok" in item) throw refusedError(item.reason);
149
+ return { item };
150
+ },
151
+ };
152
+
153
+ /** Pages that already hold an inserted copy keep it: a copy is not linked to its source. */
154
+ export const savedSectionDeleteRoute: PluginRoute<z.infer<typeof IdInput>> = {
155
+ permission: "content:edit_any",
156
+ input: IdInput,
157
+ handler: async (ctx) => {
158
+ if (!(await portOf(ctx).remove(ctx.input.id))) {
159
+ throw new PluginRouteError("NOT_FOUND", NOT_FOUND, 404);
160
+ }
161
+ return { id: ctx.input.id };
162
+ },
163
+ };