@perkyzz/emvb 0.3.0 → 0.5.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 (163) hide show
  1. package/README.md +22 -4
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +5 -2
  4. package/src/admin/access/role.ts +9 -3
  5. package/src/admin/agents-api.ts +39 -0
  6. package/src/admin/api.ts +17 -7
  7. package/src/admin/content-api.ts +7 -51
  8. package/src/admin/content-item.ts +22 -0
  9. package/src/admin/design-api.ts +30 -0
  10. package/src/admin/editor/A11yCopilot.tsx +1 -1
  11. package/src/admin/editor/CommandPalette.tsx +43 -2
  12. package/src/admin/editor/Editor.tsx +133 -332
  13. package/src/admin/editor/EditorOverlay.tsx +3 -1
  14. package/src/admin/editor/PerfMeter.tsx +1 -1
  15. package/src/admin/editor/ShortcutsDialog.tsx +4 -1
  16. package/src/admin/editor/Timeline.tsx +124 -9
  17. package/src/admin/editor/canvas/CanvasFrame.tsx +51 -368
  18. package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
  19. package/src/admin/editor/canvas/SelectionOverlay.tsx +86 -46
  20. package/src/admin/editor/canvas/canvas-scale.ts +20 -0
  21. package/src/admin/editor/canvas/drop-line.ts +24 -0
  22. package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
  23. package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
  24. package/src/admin/editor/collection-previews.ts +28 -5
  25. package/src/admin/editor/editor-chrome.tsx +346 -0
  26. package/src/admin/editor/editor-commands.ts +360 -0
  27. package/src/admin/editor/editor-css.ts +4 -0
  28. package/src/admin/editor/element-chrome.ts +89 -0
  29. package/src/admin/editor/feature-css.ts +37 -18
  30. package/src/admin/editor/host.ts +33 -0
  31. package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
  32. package/src/admin/editor/icons/library.ts +29 -19
  33. package/src/admin/editor/icons/use-icon-library.ts +315 -0
  34. package/src/admin/editor/palette-items.ts +36 -5
  35. package/src/admin/editor/palette.ts +9 -4
  36. package/src/admin/editor/panels/AddPanel.tsx +2 -67
  37. package/src/admin/editor/panels/ColorControl.tsx +21 -18
  38. package/src/admin/editor/panels/ElementPanel.tsx +40 -99
  39. package/src/admin/editor/panels/LayersPanel.tsx +34 -62
  40. package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
  41. package/src/admin/editor/panels/SiteStylesDrawer.tsx +16 -10
  42. package/src/admin/editor/panels/TokenScales.tsx +4 -2
  43. package/src/admin/editor/panels/VariableSection.tsx +1 -10
  44. package/src/admin/editor/panels/color-space.ts +12 -1
  45. package/src/admin/editor/panels/inline-error.tsx +11 -0
  46. package/src/admin/editor/panels/settings/AdvancedFields.tsx +143 -1
  47. package/src/admin/editor/panels/settings/AudienceEditor.tsx +115 -7
  48. package/src/admin/editor/panels/settings/DraftTextField.tsx +42 -9
  49. package/src/admin/editor/panels/settings/FieldControl.tsx +6 -0
  50. package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
  51. package/src/admin/editor/panels/settings/IconPicker.tsx +21 -1
  52. package/src/admin/editor/panels/settings/ImageSourcesField.tsx +111 -0
  53. package/src/admin/editor/panels/settings/ItemList.tsx +34 -1
  54. package/src/admin/editor/panels/settings/MediaPicker.tsx +2 -3
  55. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -0
  56. package/src/admin/editor/panels/settings/StyleRow.tsx +14 -2
  57. package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
  58. package/src/admin/editor/panels/settings/form-names.ts +7 -11
  59. package/src/admin/editor/panels/settings/length-units.ts +15 -4
  60. package/src/admin/editor/panels/settings/link-rules.ts +19 -0
  61. package/src/admin/editor/panels/settings/number-input.ts +11 -0
  62. package/src/admin/editor/panels/settings/query-style.ts +65 -0
  63. package/src/admin/editor/panels/settings/style-sections.ts +5 -1
  64. package/src/admin/editor/panels/style-target.ts +105 -0
  65. package/src/admin/editor/panels/variable-values.ts +4 -2
  66. package/src/admin/editor/remote-change.ts +67 -0
  67. package/src/admin/editor/section-templates.ts +43 -21
  68. package/src/admin/editor/shortcuts.ts +4 -0
  69. package/src/admin/editor/store.ts +10 -1
  70. package/src/admin/editor/useClipboardActions.ts +1 -1
  71. package/src/admin/editor/useEditorCommands.ts +61 -13
  72. package/src/admin/editor/useEditorData.ts +7 -1
  73. package/src/admin/editor/useEditorShortcuts.ts +23 -15
  74. package/src/admin/editor/useNodeActions.ts +6 -1
  75. package/src/admin/forms-api.ts +17 -6
  76. package/src/admin/icon-uploads-api.ts +18 -0
  77. package/src/admin/index.tsx +3 -1
  78. package/src/admin/media-api.ts +20 -0
  79. package/src/admin/pages/AgentsPage.tsx +274 -0
  80. package/src/admin/theme-api.ts +5 -18
  81. package/src/admin/ui.ts +28 -0
  82. package/src/astro/content-port.ts +24 -0
  83. package/src/astro/floats/runtime.ts +10 -0
  84. package/src/astro/forms/EmVBFormsClient.astro +11 -0
  85. package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
  86. package/src/astro/forms/client.ts +1 -0
  87. package/src/astro/forms-definitions.ts +24 -11
  88. package/src/astro/index.ts +1 -0
  89. package/src/astro/popups/runtime.ts +15 -0
  90. package/src/astro/render-session.ts +85 -0
  91. package/src/astro/request-data.ts +20 -8
  92. package/src/astro/resolve-theme.ts +166 -76
  93. package/src/astro/resolve.ts +57 -16
  94. package/src/astro/tabs/runtime.ts +60 -1
  95. package/src/astro/theme-posts.ts +16 -5
  96. package/src/astro/visitor.ts +67 -5
  97. package/src/constants.ts +1 -1
  98. package/src/core/a11y/audit.ts +202 -26
  99. package/src/core/arrange.ts +71 -38
  100. package/src/core/audience/rules.ts +22 -2
  101. package/src/core/canvas-styles.ts +24 -0
  102. package/src/core/clipboard.ts +11 -32
  103. package/src/core/css/generate.ts +18 -1
  104. package/src/core/css/print.ts +12 -0
  105. package/src/core/css/scope.ts +3 -3
  106. package/src/core/css-keywords.ts +83 -0
  107. package/src/core/data/bindings.ts +4 -6
  108. package/src/core/design/classes.ts +13 -15
  109. package/src/core/design/diff.ts +55 -0
  110. package/src/core/design/merge.ts +127 -0
  111. package/src/core/design/variables.ts +25 -21
  112. package/src/core/elements/dynamic-elements.ts +30 -0
  113. package/src/core/elements/form-elements.ts +20 -3
  114. package/src/core/elements/index.ts +211 -7
  115. package/src/core/forms/binding.ts +4 -13
  116. package/src/core/index.ts +10 -0
  117. package/src/core/local-style.ts +27 -0
  118. package/src/core/migrate/index.ts +9 -4
  119. package/src/core/nesting.ts +4 -23
  120. package/src/core/perf/scripts.ts +9 -6
  121. package/src/core/record.ts +3 -0
  122. package/src/core/render/context.ts +2 -0
  123. package/src/core/render/dynamic-post.ts +22 -7
  124. package/src/core/render/form.ts +145 -15
  125. package/src/core/render/index.ts +195 -14
  126. package/src/core/render/pagination.ts +21 -2
  127. package/src/core/render/vnode.ts +12 -2
  128. package/src/core/sanitize/css.ts +90 -77
  129. package/src/core/sanitize/embed-url.ts +7 -1
  130. package/src/core/sanitize/href.ts +13 -1
  131. package/src/core/sanitize/svg.ts +8 -3
  132. package/src/core/sanitize/url.ts +10 -1
  133. package/src/core/schema/descriptors.ts +4 -1
  134. package/src/core/schema/design.ts +5 -7
  135. package/src/core/schema/layout.ts +160 -181
  136. package/src/core/schema/style.ts +53 -63
  137. package/src/core/share/link.ts +72 -29
  138. package/src/core/tabs/presence.ts +7 -17
  139. package/src/core/theme/dynamic.ts +205 -39
  140. package/src/core/theme/float.ts +1 -1
  141. package/src/core/tree-ops.ts +57 -0
  142. package/src/index.ts +14 -2
  143. package/src/mcp/errors.ts +44 -0
  144. package/src/mcp/index.ts +4 -0
  145. package/src/mcp/ports.ts +78 -0
  146. package/src/mcp/prompts.ts +76 -0
  147. package/src/mcp/publish.ts +60 -0
  148. package/src/mcp/queue.ts +19 -0
  149. package/src/mcp/rate-limit.ts +80 -0
  150. package/src/mcp/read.ts +419 -0
  151. package/src/mcp/refs.ts +84 -0
  152. package/src/mcp/settings.ts +18 -0
  153. package/src/mcp/tools.ts +265 -0
  154. package/src/mcp/write.ts +582 -0
  155. package/src/server/agent-settings.ts +134 -0
  156. package/src/server/design-routes.ts +75 -18
  157. package/src/server/icon-routes.ts +72 -16
  158. package/src/server/mcp-routes.ts +265 -0
  159. package/src/server/plugin.ts +40 -4
  160. package/src/admin/editor/icons/sets/fontawesome.json +0 -1
  161. package/src/admin/editor/icons/sets/lucide.json +0 -1
  162. package/src/admin/editor/icons/sets/remix.json +0 -1
  163. package/src/admin/editor/icons/sets/tabler.json +0 -1
package/README.md CHANGED
@@ -12,7 +12,9 @@ A visual page builder plugin for [EmDash CMS](https://github.com/emdash-cms/emda
12
12
  npm i @perkyzz/emvb
13
13
  ```
14
14
 
15
- Peers, which an EmDash 1.x site already has: `emdash` ^1.0.0, `astro` ^7, `react` and `react-dom` ^19, `@cloudflare/kumo` 2.6.0 and `@phosphor-icons/react` ^2.1.10. `@emdash-cms/plugin-forms` is optional and enables forms.
15
+ Peers, which an EmDash 1.x site already has: `emdash` ^1.0.0, `astro` ^7, `react` and `react-dom` ^19, `@cloudflare/kumo` 2.6.0 and `@phosphor-icons/react` ^2.1.10. `@emdash-cms/plugin-forms` ^0.2.9 is optional and enables forms.
16
+
17
+ Dependencies installed with it: `zod` and [`@perkyzz/emvb-icons`](https://www.npmjs.com/package/@perkyzz/emvb-icons), the icon sets for the editor's Icon library. Only the editor loads them; a placed icon is saved into the page as SVG.
16
18
 
17
19
  ## Register the plugin
18
20
 
@@ -38,7 +40,9 @@ export default defineConfig({
38
40
  import { getEmDashEntry, decodeSlug } from "emdash";
39
41
  import { EmVBPage, resolveEmVBPage } from "@perkyzz/emvb/astro";
40
42
  // With @emdash-cms/plugin-forms installed and registered:
41
- import { EmVBFormsRuntime } from "@perkyzz/emvb/astro/forms";
43
+ import { EmVBFormsRuntime } from "@perkyzz/emvb/astro/forms/client";
44
+ import { EmVBMenuRuntime } from "@perkyzz/emvb/astro/menu";
45
+ import { EmVBTabsRuntime } from "@perkyzz/emvb/astro/tabs";
42
46
 
43
47
  const emvb = await resolveEmVBPage(Astro);
44
48
  if (!emvb && !(await getEmDashEntry("pages", decodeSlug(Astro.params.slug)))) {
@@ -46,13 +50,27 @@ if (!emvb && !(await getEmDashEntry("pages", decodeSlug(Astro.params.slug)))) {
46
50
  }
47
51
  ---
48
52
 
49
- {emvb && <EmVBPage page={emvb}>{emvb.needsFormsRuntime && <EmVBFormsRuntime />}</EmVBPage>}
53
+ {emvb && (
54
+ <EmVBPage page={emvb}>
55
+ {emvb.needsFormsRuntime && <EmVBFormsRuntime />}
56
+ {emvb.needsTabsRuntime && <EmVBTabsRuntime />}
57
+ {emvb.needsMenuRuntime && <EmVBMenuRuntime />}
58
+ </EmVBPage>
59
+ )}
50
60
  ```
51
61
 
52
- Without the forms plugin, leave out the `EmVBFormsRuntime` import and element; that subpath imports the forms plugin's client script.
62
+ Each runtime is a small script mounted only on pages that need it; the rest of the page is plain HTML and CSS. Theme parts (`resolveThemeParts`) also report popups and floats, served from `@perkyzz/emvb/astro/popups` and `@perkyzz/emvb/astro/floats`.
63
+
64
+ Without the forms plugin, import `EmVBFormsRuntime` from `@perkyzz/emvb/astro/forms` (it renders nothing and does not import the plugin) or leave the element out. The `/client` subpath is the one that calls `initForms`.
65
+
66
+ A fixed route has no slug parameter. Pass the page's slug: `await resolveEmVBPage(Astro, { slug: "home" })`. Visitor rules can use the same call's `visitor` option (`{ segments: ["member"] }`, and country, device or returning when the site knows better); `resolveThemeParts` takes it too. The [install guide](https://github.com/PerkyZZ999/EmVB/blob/main/docs/guides/install-in-a-host-site.md) shows both, and the `canvasStyles` option that loads your site's stylesheets into the editor canvas: `emvb({ canvasStyles: ["/styles/site.css"] })`.
53
67
 
54
68
  Then sign in as an admin, open **Pages VisualBuilder** and click **Set up EmVB** once.
55
69
 
70
+ ## AI agents (MCP)
71
+
72
+ On EmDash 1.2+ with `emdash({ mcp: true, … })`, AI agents can build and edit draft EmVB pages. An admin turns them on in **Pages VisualBuilder → AI agents** (off by default) and approves the EmVB tools on EmDash's Plugins screen. Agents connect to EmDash's own endpoint (`/_emdash/api/mcp`) or through the local bridge, `npx -y @perkyzz/emvb-mcp --site https://your.site` with the token in `EMVB_TOKEN`. Tokens need the `mcp:tools:emvb` scope and an editor (not an admin) user. Agents only change drafts unless an admin allows publishing; each token may make 300 reads and 60 changes a minute and 10 publishes an hour. Every agent change shows in the version timeline with an **Agent** badge. See the [MCP guide](https://github.com/PerkyZZ999/EmVB/blob/main/docs/guides/mcp.md).
73
+
56
74
  Website: <https://emvb.dev>. The full guide, the demos and the source are on GitHub: <https://github.com/PerkyZZ999/EmVB>.
57
75
 
58
76
  ## License
@@ -1,6 +1,6 @@
1
1
  # Third-party notices
2
2
 
3
- EmVB (<https://emvb.dev>) is MIT licensed. The editor's **Icon library** (`src/admin/editor/icons/sets/*.json`) contains icons from the projects below. They keep their own licenses, copied in full in [`licenses/`](licenses/). `scripts/build-icon-sets.ts` in the EmVB repository builds the set files from the pinned npm packages. It refuses to build when a package's license file changes.
3
+ EmVB (<https://emvb.dev>) is MIT licensed. The editor's **Icon library** (`@perkyzz/emvb-icons`, the JSON sets) contains icons from the projects below. They keep their own licenses, copied in full in [`licenses/`](licenses/). `scripts/build-icon-sets.ts` in the EmVB repository builds the set files from the pinned npm packages. It refuses to build when a package's license file changes.
4
4
 
5
5
  Changes made to every set: the SVG markup is reformatted into compact JSON, and only elements and attributes in EmVB's SVG allowlist are kept. Class attributes and comments are removed. Shared presentation attributes (stroke or fill, viewBox) move to the root `<svg>`. Set-specific changes are listed with each set.
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perkyzz/emvb",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Visual page builder plugin for EmDash CMS",
5
5
  "keywords": [
6
6
  "astro",
@@ -35,10 +35,12 @@
35
35
  "./admin": "./src/admin/index.tsx",
36
36
  "./astro": "./src/astro/index.ts",
37
37
  "./astro/forms": "./src/astro/forms/index.ts",
38
+ "./astro/forms/client": "./src/astro/forms/client.ts",
38
39
  "./astro/popups": "./src/astro/popups/index.ts",
39
40
  "./astro/floats": "./src/astro/floats/index.ts",
40
41
  "./astro/tabs": "./src/astro/tabs/index.ts",
41
- "./astro/menu": "./src/astro/menu/index.ts"
42
+ "./astro/menu": "./src/astro/menu/index.ts",
43
+ "./mcp": "./src/mcp/index.ts"
42
44
  },
43
45
  "publishConfig": {
44
46
  "access": "public"
@@ -47,6 +49,7 @@
47
49
  "test:astro": "bun --bun vitest run"
48
50
  },
49
51
  "dependencies": {
52
+ "@perkyzz/emvb-icons": "0.1.1",
50
53
  "zod": "~4.5.4"
51
54
  },
52
55
  "devDependencies": {
@@ -1,10 +1,16 @@
1
1
  import * as React from "react";
2
2
  import { EDITOR_ROLE } from "../../constants.ts";
3
- import { requestJson, type Fetcher } from "../api.ts";
3
+ import { ApiError, requestJson, type Fetcher } from "../api.ts";
4
4
 
5
5
  export async function readRole(fetcher: Fetcher): Promise<number> {
6
- const me = await requestJson<{ role?: unknown } | undefined>(fetcher, "/_emdash/api/auth/me");
7
- return typeof me?.role === "number" ? me.role : 0;
6
+ try {
7
+ const me = await requestJson<{ role?: unknown }>(fetcher, "/_emdash/api/auth/me");
8
+ return typeof me.role === "number" ? me.role : 0;
9
+ } catch (error) {
10
+ // A 200 with no body is "no role", the same as a role that is not a number.
11
+ if (error instanceof ApiError && error.code === "MISSING_DATA") return 0;
12
+ throw error;
13
+ }
8
14
  }
9
15
 
10
16
  /** D-020: EmVB is for editors and administrators. The server enforces the same line. */
@@ -0,0 +1,39 @@
1
+ import { PLUGIN_ID } from "../constants.ts";
2
+ import type { AgentSettings } from "../mcp/settings.ts";
3
+ import { requestJson, type Fetcher } from "./api.ts";
4
+
5
+ export type AgentActivity = {
6
+ id: string;
7
+ at: string;
8
+ tool: string;
9
+ target: "page" | "styles" | "settings";
10
+ pageId?: string;
11
+ revisionId?: string;
12
+ summary: string;
13
+ userId: string | null;
14
+ client?: string;
15
+ };
16
+
17
+ export type AgentsState = {
18
+ settings: AgentSettings;
19
+ revision: string | null;
20
+ endpoint: string;
21
+ activity: AgentActivity[];
22
+ };
23
+
24
+ const base = `/_emdash/api/plugins/${PLUGIN_ID}`;
25
+
26
+ export const loadAgents = (fetcher: Fetcher) => requestJson<AgentsState>(fetcher, `${base}/agents`);
27
+
28
+ /** Saves the agent settings; the returned revision is the one the next save must send. */
29
+ export async function saveAgentSettings(
30
+ fetcher: Fetcher,
31
+ settings: AgentSettings,
32
+ revision: string | null,
33
+ ): Promise<string> {
34
+ const body = await requestJson<{ revision: string }>(fetcher, `${base}/agents/save`, {
35
+ method: "POST",
36
+ body: { settings, revision },
37
+ });
38
+ return body.revision;
39
+ }
package/src/admin/api.ts CHANGED
@@ -44,7 +44,10 @@ export async function requestJson<T>(
44
44
  });
45
45
  const payload = await readEnvelope<T>(response);
46
46
  if (!response.ok) throw envelopeError(response, payload);
47
- return payload.data as T;
47
+ if (payload.data === undefined) {
48
+ throw new ApiError(response.status, "MISSING_DATA", "The response had no data.");
49
+ }
50
+ return payload.data;
48
51
  }
49
52
 
50
53
  const LIST_PAGE = 50;
@@ -62,12 +65,19 @@ export async function listAllItems<T>(fetcher: Fetcher, collectionPath: string):
62
65
  const query = `?limit=${LIST_PAGE}&orderBy=updatedAt&order=desc${
63
66
  cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""
64
67
  }`;
65
- // oxlint-disable-next-line no-await-in-loop -- each page needs the previous page's cursor
66
- const body = await requestJson<{ items?: T[]; nextCursor?: string }>(
67
- fetcher,
68
- `${collectionPath}${query}`,
69
- );
70
- items.push(...(body?.items ?? []));
68
+ let body: { items?: T[]; nextCursor?: string };
69
+ try {
70
+ // oxlint-disable-next-line no-await-in-loop -- each page needs the previous page's cursor
71
+ body = await requestJson<{ items?: T[]; nextCursor?: string }>(
72
+ fetcher,
73
+ `${collectionPath}${query}`,
74
+ );
75
+ } catch (error) {
76
+ // A 200 with no body ends the list. It is not a failed load.
77
+ if (error instanceof ApiError && error.code === "MISSING_DATA") break;
78
+ throw error;
79
+ }
80
+ items.push(...(body.items ?? []));
71
81
  cursor = body?.nextCursor || undefined;
72
82
  if (!cursor) break;
73
83
  }
@@ -1,32 +1,18 @@
1
1
  import type {
2
2
  ConditionsDoc,
3
- DesignSystem,
4
3
  FloatSettings,
5
4
  Layout,
6
5
  ThemePartType,
7
6
  TriggersDoc,
8
7
  } from "../core/index.ts";
9
- import { PAGES_COLLECTION, PLUGIN_ID } from "../constants.ts";
8
+ import { PAGES_COLLECTION } from "../constants.ts";
10
9
  import { listAllItems, requestJson, type Fetcher } from "./api.ts";
10
+ import { titleOf, type ContentItem } from "./content-item.ts";
11
11
  import { entryStatus } from "./entry-status.ts";
12
12
 
13
- const CONTENT = `/_emdash/api/content/${PAGES_COLLECTION}`;
13
+ export { publishDesign, saveDesign } from "./design-api.ts";
14
14
 
15
- type RawItem = {
16
- id: string;
17
- slug?: string | null;
18
- status?: string;
19
- draftRevisionId?: string | null;
20
- updatedAt?: string;
21
- data?: Record<string, unknown>;
22
- seo?: {
23
- title?: string | null;
24
- description?: string | null;
25
- canonical?: string | null;
26
- noIndex?: boolean | null;
27
- image?: string | null;
28
- } | null;
29
- };
15
+ const CONTENT = `/_emdash/api/content/${PAGES_COLLECTION}`;
30
16
 
31
17
  export type PageSummary = {
32
18
  id: string;
@@ -36,16 +22,11 @@ export type PageSummary = {
36
22
  updatedAt: string;
37
23
  };
38
24
 
39
- const titleOf = (item: RawItem) =>
40
- typeof item.data?.["title"] === "string" && item.data["title"]
41
- ? item.data["title"]
42
- : "Untitled page";
43
-
44
25
  export async function listPages(fetcher: Fetcher): Promise<PageSummary[]> {
45
- const items = await listAllItems<RawItem>(fetcher, CONTENT);
26
+ const items = await listAllItems<ContentItem>(fetcher, CONTENT);
46
27
  return items.map((item) => ({
47
28
  id: item.id,
48
- title: titleOf(item),
29
+ title: titleOf(item, "Untitled page"),
49
30
  slug: item.slug ?? "",
50
31
  status: entryStatus(item),
51
32
  updatedAt: item.updatedAt ?? "",
@@ -56,7 +37,7 @@ export async function createPage(
56
37
  fetcher: Fetcher,
57
38
  input: { title: string; slug: string; layout: Layout },
58
39
  ): Promise<string> {
59
- const body = await requestJson<{ item: RawItem }>(fetcher, CONTENT, {
40
+ const body = await requestJson<{ item: ContentItem }>(fetcher, CONTENT, {
60
41
  method: "POST",
61
42
  body: { data: { title: input.title, layout: input.layout }, slug: input.slug },
62
43
  });
@@ -183,28 +164,3 @@ export async function previewUrl(fetcher: Fetcher, id: string): Promise<string>
183
164
  );
184
165
  return body.url;
185
166
  }
186
-
187
- export async function saveDesign(
188
- fetcher: Fetcher,
189
- design: DesignSystem,
190
- revision: string | null,
191
- ): Promise<string> {
192
- const body = await requestJson<{ revision: string }>(
193
- fetcher,
194
- `/_emdash/api/plugins/${PLUGIN_ID}/design/save`,
195
- { method: "POST", body: { design, revision } },
196
- );
197
- return body.revision;
198
- }
199
-
200
- export async function publishDesign(
201
- fetcher: Fetcher,
202
- publishedRevision: string | null,
203
- ): Promise<string> {
204
- const body = await requestJson<{ revision: string }>(
205
- fetcher,
206
- `/_emdash/api/plugins/${PLUGIN_ID}/design/publish`,
207
- { method: "POST", body: { publishedRevision } },
208
- );
209
- return body.revision;
210
- }
@@ -0,0 +1,22 @@
1
+ /** One EmDash content row, as list and get return it. Pages add `seo`; theme parts omit it. */
2
+ export type ContentItem = {
3
+ id: string;
4
+ slug?: string | null;
5
+ status?: string;
6
+ draftRevisionId?: string | null;
7
+ updatedAt?: string;
8
+ data?: Record<string, unknown>;
9
+ seo?: {
10
+ title?: string | null;
11
+ description?: string | null;
12
+ canonical?: string | null;
13
+ noIndex?: boolean | null;
14
+ image?: string | null;
15
+ } | null;
16
+ };
17
+
18
+ /** The stored title, or `fallback` when it is missing or blank. */
19
+ export function titleOf(item: ContentItem, fallback: string): string {
20
+ const title = item.data?.["title"];
21
+ return typeof title === "string" && title ? title : fallback;
22
+ }
@@ -0,0 +1,30 @@
1
+ import type { DesignSystem } from "../core/index.ts";
2
+ import { PLUGIN_ID } from "../constants.ts";
3
+ import { requestJson, type Fetcher } from "./api.ts";
4
+
5
+ /** Saves the design draft. The returned revision is the one the next save must send. */
6
+ export async function saveDesign(
7
+ fetcher: Fetcher,
8
+ design: DesignSystem,
9
+ revision: string | null,
10
+ ): Promise<string> {
11
+ const body = await requestJson<{ revision: string }>(
12
+ fetcher,
13
+ `/_emdash/api/plugins/${PLUGIN_ID}/design/save`,
14
+ { method: "POST", body: { design, revision } },
15
+ );
16
+ return body.revision;
17
+ }
18
+
19
+ /** Publishes the design draft. `publishedRevision` is the revision the editor last saw published. */
20
+ export async function publishDesign(
21
+ fetcher: Fetcher,
22
+ publishedRevision: string | null,
23
+ ): Promise<string> {
24
+ const body = await requestJson<{ revision: string }>(
25
+ fetcher,
26
+ `/_emdash/api/plugins/${PLUGIN_ID}/design/publish`,
27
+ { method: "POST", body: { publishedRevision } },
28
+ );
29
+ return body.revision;
30
+ }
@@ -6,7 +6,7 @@ import {
6
6
  type A11yReport,
7
7
  type Layout,
8
8
  } from "../../core/index.ts";
9
- import { ELEMENT_NAMES } from "./panels/ElementPanel.tsx";
9
+ import { ELEMENT_NAMES } from "./element-chrome.ts";
10
10
 
11
11
  /** Score bands for the badge colour (W-317). */
12
12
  const scoreBand = (score: number) => (score >= 90 ? "good" : score >= 70 ? "fair" : "poor");
@@ -18,6 +18,44 @@ export function CommandPalette({
18
18
  const [query, setQuery] = React.useState("");
19
19
  const [active, setActive] = React.useState(0);
20
20
  const listRef = React.useRef<HTMLUListElement>(null);
21
+ // W-399: opened from a shortcut there is no trigger to return to, so focus fell to <body> on
22
+ // close. Remember what had focus and go back to it when nothing else took focus.
23
+ const wasOpen = React.useRef(false);
24
+ const returnTo = React.useRef<HTMLElement | null>(null);
25
+ if (open && !wasOpen.current && typeof document !== "undefined") {
26
+ const focused = document.activeElement;
27
+ returnTo.current = focused instanceof HTMLElement && focused !== document.body ? focused : null;
28
+ }
29
+ wasOpen.current = open;
30
+ /** After a command, the selected layer first (it may have changed); after Esc, where we were. */
31
+ const restoreFocus = (afterCommand = false) => {
32
+ // The dialog lets go of focus only once its close animation ends, so wait for that.
33
+ const started = Date.now();
34
+ const settle = () => {
35
+ const now = document.activeElement;
36
+ // Kumo's Dialog doesn't pass data attributes through; find the palette by its input.
37
+ const stillOpen = document.querySelector("[data-emvb-palette-input]") !== null;
38
+ const inPalette = now?.closest('[role="dialog"]')?.querySelector("[data-emvb-palette-input]");
39
+ if (now && now !== document.body && !inPalette) return;
40
+ if (stillOpen && Date.now() - started < 2000) {
41
+ setTimeout(settle, 30);
42
+ return;
43
+ }
44
+ const selected = document.querySelector<HTMLElement>(
45
+ '.emvb-layer-select[aria-current="true"]',
46
+ );
47
+ const back = returnTo.current?.isConnected ? returnTo.current : null;
48
+ const target =
49
+ (afterCommand ? (selected ?? back) : (back ?? selected)) ??
50
+ document.querySelector<HTMLElement>("iframe[data-emvb-canvas]");
51
+ target?.focus({ preventScroll: true });
52
+ };
53
+ setTimeout(settle, 30);
54
+ };
55
+ const close = (next: boolean) => {
56
+ onOpenChange(next);
57
+ if (!next) restoreFocus();
58
+ };
21
59
  React.useEffect(() => {
22
60
  if (open) {
23
61
  setQuery("");
@@ -35,7 +73,10 @@ export function CommandPalette({
35
73
  if (!item) return;
36
74
  onOpenChange(false);
37
75
  // After the dialog closes, so focus moves where the command wants it.
38
- setTimeout(item.run, 0);
76
+ setTimeout(() => {
77
+ item.run();
78
+ restoreFocus(true);
79
+ }, 0);
39
80
  };
40
81
  const onKeyDown = (event: React.KeyboardEvent) => {
41
82
  if (event.key === "ArrowDown" || event.key === "ArrowUp") {
@@ -51,7 +92,7 @@ export function CommandPalette({
51
92
  };
52
93
  let lastGroup = "";
53
94
  return (
54
- <Dialog.Root open={open} onOpenChange={onOpenChange}>
95
+ <Dialog.Root open={open} onOpenChange={close}>
55
96
  <Dialog size="base" className="emvb-palette" data-emvb-palette="">
56
97
  <Dialog.Title className="sr-only">Command palette</Dialog.Title>
57
98
  <input