@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.
Files changed (179) hide show
  1. package/README.md +3 -3
  2. package/package.json +4 -4
  3. package/src/admin/agents-api.ts +45 -0
  4. package/src/admin/editor/A11yCopilot.tsx +1 -1
  5. package/src/admin/editor/Editor.tsx +325 -239
  6. package/src/admin/editor/FieldPreview.tsx +164 -0
  7. package/src/admin/editor/Timeline.tsx +2 -1
  8. package/src/admin/editor/TopBar.tsx +36 -3
  9. package/src/admin/editor/canvas/CanvasFrame.tsx +2 -2
  10. package/src/admin/editor/canvas/DeviceBar.tsx +17 -9
  11. package/src/admin/editor/canvas/canvas-scale.ts +2 -2
  12. package/src/admin/editor/canvas/use-canvas-session.ts +2 -2
  13. package/src/admin/editor/co-design.ts +190 -0
  14. package/src/admin/editor/dnd/new-element.ts +26 -4
  15. package/src/admin/editor/editor-chrome.tsx +3 -3
  16. package/src/admin/editor/editor-commands.ts +8 -3
  17. package/src/admin/editor/editor-css.ts +3 -0
  18. package/src/admin/editor/element-chrome.ts +3 -1
  19. package/src/admin/editor/extension-manifests.ts +20 -0
  20. package/src/admin/editor/extensions/ExtensionPanels.tsx +26 -0
  21. package/src/admin/editor/extensions-context.tsx +27 -0
  22. package/src/admin/editor/feature-css.ts +17 -0
  23. package/src/admin/editor/host.ts +6 -0
  24. package/src/admin/editor/palette-items.ts +2 -2
  25. package/src/admin/editor/panels/AddPanel.tsx +31 -1
  26. package/src/admin/editor/panels/AgentRequestBox.tsx +116 -0
  27. package/src/admin/editor/panels/ClassesSection.tsx +9 -6
  28. package/src/admin/editor/panels/CoDesignPanel.tsx +170 -0
  29. package/src/admin/editor/panels/ColorControl.tsx +2 -2
  30. package/src/admin/editor/panels/ElementPanel.tsx +53 -15
  31. package/src/admin/editor/panels/LanguagesSection.tsx +152 -0
  32. package/src/admin/editor/panels/LayersPanel.tsx +10 -6
  33. package/src/admin/editor/panels/LeftPanel.tsx +33 -5
  34. package/src/admin/editor/panels/PageSettings.tsx +4 -0
  35. package/src/admin/editor/panels/SettingsPanel.tsx +21 -2
  36. package/src/admin/editor/panels/SiteStylesDrawer.tsx +7 -7
  37. package/src/admin/editor/panels/settings/BackgroundControls.tsx +76 -20
  38. package/src/admin/editor/panels/settings/BackgroundFill.tsx +8 -6
  39. package/src/admin/editor/panels/settings/BoxControl.tsx +5 -5
  40. package/src/admin/editor/panels/settings/EffectsControls.tsx +104 -9
  41. package/src/admin/editor/panels/settings/FieldControl.tsx +9 -3
  42. package/src/admin/editor/panels/settings/IconControls.tsx +14 -11
  43. package/src/admin/editor/panels/settings/LengthRow.tsx +27 -16
  44. package/src/admin/editor/panels/settings/LoopControl.tsx +269 -0
  45. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
  46. package/src/admin/editor/panels/settings/MediaPicker.tsx +5 -5
  47. package/src/admin/editor/panels/settings/MotionControl.tsx +12 -11
  48. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +7 -4
  49. package/src/admin/editor/panels/settings/StyleRow.tsx +113 -13
  50. package/src/admin/editor/panels/settings/TextMarksControl.tsx +247 -0
  51. package/src/admin/editor/panels/settings/VariableBinding.tsx +2 -2
  52. package/src/admin/editor/panels/settings/background-mode.ts +14 -11
  53. package/src/admin/editor/panels/settings/box-link-name.ts +1 -1
  54. package/src/admin/editor/panels/settings/box-sides.ts +22 -14
  55. package/src/admin/editor/panels/settings/heading-warning.ts +5 -1
  56. package/src/admin/editor/panels/settings/length-units.ts +55 -2
  57. package/src/admin/editor/panels/settings/query-style.ts +3 -3
  58. package/src/admin/editor/panels/settings/style-sections.ts +51 -10
  59. package/src/admin/editor/panels/style-target.ts +9 -7
  60. package/src/admin/editor/presence.ts +60 -0
  61. package/src/admin/editor/store.ts +6 -2
  62. package/src/admin/editor/useCoDesign.ts +177 -0
  63. package/src/admin/editor/useEditorCommands.ts +4 -4
  64. package/src/admin/editor/useExtensions.ts +45 -0
  65. package/src/admin/editor/useNodeActions.ts +15 -6
  66. package/src/admin/editor/useProposals.ts +81 -0
  67. package/src/admin/index.tsx +5 -1
  68. package/src/admin/languages-api.ts +61 -0
  69. package/src/admin/media-api.ts +27 -0
  70. package/src/admin/pages/AgentSetup.tsx +148 -0
  71. package/src/admin/pages/AgentsPage.tsx +11 -28
  72. package/src/admin/pages/CollectionsPage.tsx +215 -0
  73. package/src/admin/pages/CreateDialog.tsx +15 -0
  74. package/src/admin/pages/ImportButton.tsx +18 -0
  75. package/src/admin/pages/ImportElementorDialog.tsx +669 -0
  76. package/src/admin/pages/ImportPageLinks.tsx +86 -0
  77. package/src/admin/pages/PageList.tsx +15 -1
  78. package/src/admin/pages/ThemePartList.tsx +15 -1
  79. package/src/admin/pages/section-head.tsx +27 -0
  80. package/src/admin/proposals-api.ts +91 -0
  81. package/src/admin/setup/host-collections.ts +100 -0
  82. package/src/admin/ui.ts +39 -2
  83. package/src/admin/widgets/CanvasField.tsx +202 -0
  84. package/src/admin/widgets/convert-body.ts +54 -0
  85. package/src/admin/widgets/field-fetcher.ts +79 -0
  86. package/src/astro/content-port.ts +14 -1
  87. package/src/astro/index.ts +2 -0
  88. package/src/astro/page-count.ts +77 -0
  89. package/src/astro/resolve-theme.ts +58 -3
  90. package/src/astro/resolve.ts +128 -5
  91. package/src/astro/responsive-images.ts +99 -0
  92. package/src/constants.ts +1 -1
  93. package/src/core/a11y/audit.ts +44 -14
  94. package/src/core/arrange.ts +5 -1
  95. package/src/core/bounded-array.ts +31 -0
  96. package/src/core/canvas-collections.ts +20 -0
  97. package/src/core/clipboard.ts +9 -0
  98. package/src/core/contract.ts +285 -0
  99. package/src/core/css/generate.ts +89 -20
  100. package/src/core/css/trace.ts +18 -9
  101. package/src/core/css-keywords.ts +51 -1
  102. package/src/core/design/cascade.ts +12 -9
  103. package/src/core/design/classes.ts +4 -5
  104. package/src/core/design/devices.ts +13 -13
  105. package/src/core/design/states.ts +5 -3
  106. package/src/core/design/transfer.ts +5 -0
  107. package/src/core/design/variables.ts +17 -4
  108. package/src/core/elements/dynamic-elements.ts +19 -4
  109. package/src/core/elements/index.ts +73 -9
  110. package/src/core/extensions/define.ts +13 -0
  111. package/src/core/extensions/index.ts +41 -0
  112. package/src/core/extensions/namespace.ts +82 -0
  113. package/src/core/extensions/normalize.ts +141 -0
  114. package/src/core/extensions/placeholder.ts +41 -0
  115. package/src/core/extensions/registry.ts +94 -0
  116. package/src/core/extensions/render-ext.ts +118 -0
  117. package/src/core/extensions/safe-nodes.ts +93 -0
  118. package/src/core/extensions/schema.ts +112 -0
  119. package/src/core/extensions/types.ts +159 -0
  120. package/src/core/forms/binding.ts +1 -1
  121. package/src/core/history/diff.ts +1 -1
  122. package/src/core/import/elementor/atomic.ts +1595 -0
  123. package/src/core/import/elementor/classes.ts +127 -0
  124. package/src/core/import/elementor/context.ts +429 -0
  125. package/src/core/import/elementor/counter-steps.ts +72 -0
  126. package/src/core/import/elementor/css.ts +445 -0
  127. package/src/core/import/elementor/html.ts +500 -0
  128. package/src/core/import/elementor/index.ts +949 -0
  129. package/src/core/import/elementor/inline-marks.ts +81 -0
  130. package/src/core/import/elementor/snapshots.ts +48 -0
  131. package/src/core/import/elementor/style.ts +409 -0
  132. package/src/core/import/elementor/values.ts +241 -0
  133. package/src/core/import/elementor/widgets.ts +1897 -0
  134. package/src/core/index.ts +84 -0
  135. package/src/core/json-schema.ts +107 -0
  136. package/src/core/limits.ts +19 -0
  137. package/src/core/migrate/index.ts +6 -1
  138. package/src/core/migrate/v18.ts +222 -0
  139. package/src/core/perf/meter.ts +5 -5
  140. package/src/core/portable-text.ts +127 -0
  141. package/src/core/proposals-safety.ts +102 -0
  142. package/src/core/proposals.ts +357 -0
  143. package/src/core/recipes/index.ts +2 -2
  144. package/src/core/render/extras.ts +3 -3
  145. package/src/core/render/index.ts +140 -20
  146. package/src/core/render/loop.ts +1 -1
  147. package/src/core/render/pagination.ts +20 -1
  148. package/src/core/render/section.ts +4 -4
  149. package/src/core/render/vnode.ts +1 -1
  150. package/src/core/sanitize/css.ts +125 -10
  151. package/src/core/sanitize/motion.ts +116 -0
  152. package/src/core/saved-sections.ts +6 -0
  153. package/src/core/schema/descriptors.ts +3 -1
  154. package/src/core/schema/design.ts +2 -1
  155. package/src/core/schema/layout.ts +87 -14
  156. package/src/core/schema/style.ts +251 -114
  157. package/src/core/text-marks.ts +227 -0
  158. package/src/core/theme/dynamic.ts +9 -4
  159. package/src/core/theme/float.ts +1 -0
  160. package/src/core/theme/pagination.ts +9 -0
  161. package/src/core/theme/popup-rules.ts +17 -1
  162. package/src/core/translatable.ts +34 -0
  163. package/src/core/validate.ts +13 -1
  164. package/src/index.ts +35 -1
  165. package/src/mcp/errors.ts +3 -1
  166. package/src/mcp/ports.ts +67 -0
  167. package/src/mcp/prompts.ts +1 -11
  168. package/src/mcp/proposals.ts +281 -0
  169. package/src/mcp/read.ts +10 -9
  170. package/src/mcp/tools.ts +71 -3
  171. package/src/mcp/write.ts +66 -11
  172. package/src/server/agent-settings.ts +21 -2
  173. package/src/server/hooks.ts +81 -0
  174. package/src/server/mcp-routes.ts +15 -0
  175. package/src/server/plugin.ts +53 -5
  176. package/src/server/presence.ts +78 -0
  177. package/src/server/proposals.ts +261 -0
  178. package/src/server/requests.ts +196 -0
  179. package/src/server/tools-served.ts +45 -0
@@ -1,4 +1,4 @@
1
- import type { StyleProps } from "../../../../core/index.ts";
1
+ import type { FlatStyleProps } from "../../../../core/index.ts";
2
2
 
3
3
  export type StyleSectionId =
4
4
  | "icon"
@@ -12,7 +12,7 @@ export type StyleSectionId =
12
12
  | "effects"
13
13
  | "advanced";
14
14
 
15
- export type StyleKey = keyof StyleProps;
15
+ export type StyleKey = keyof FlatStyleProps;
16
16
 
17
17
  /** Elementor v4 order (W-088); every list below follows it. */
18
18
  const SECTION_ORDER: StyleSectionId[] = [
@@ -139,7 +139,7 @@ export const SECTION_LABELS: Record<StyleSectionId, string> = {
139
139
  advanced: "Advanced",
140
140
  };
141
141
 
142
- /** Style property keys shown in each section (subset of StyleProps). */
142
+ /** Style property keys shown in each section (subset of FlatStyleProps). */
143
143
  const SECTION_KEYS: Record<StyleSectionId, StyleKey[]> = {
144
144
  layout: [
145
145
  "flexDirection",
@@ -170,10 +170,16 @@ const SECTION_KEYS: Record<StyleSectionId, StyleKey[]> = {
170
170
  "maxWidth",
171
171
  "maxHeight",
172
172
  "overflow",
173
+ "overflowX",
174
+ "overflowY",
173
175
  "aspectRatio",
174
176
  "objectFit",
177
+ "objectPosition",
178
+ "flexGrow",
179
+ "flexShrink",
180
+ "flexBasis",
175
181
  ],
176
- position: ["position", "top", "right", "bottom", "left", "zIndex"],
182
+ position: ["position", "top", "right", "bottom", "left", "zIndex", "alignSelf", "order"],
177
183
  typography: [
178
184
  "fontFamily",
179
185
  "fontSize",
@@ -183,7 +189,12 @@ const SECTION_KEYS: Record<StyleSectionId, StyleKey[]> = {
183
189
  "textAlign",
184
190
  "textTransform",
185
191
  "textDecoration",
192
+ "fontStyle",
193
+ "whiteSpace",
194
+ "overflowWrap",
186
195
  "color",
196
+ "markerColor",
197
+ "markerSize",
187
198
  ],
188
199
  background: [
189
200
  "backgroundColor",
@@ -209,7 +220,18 @@ const SECTION_KEYS: Record<StyleSectionId, StyleKey[]> = {
209
220
  "borderBottomRightRadius",
210
221
  "borderBottomLeftRadius",
211
222
  ],
212
- effects: ["opacity", "boxShadow", "filter", "cursor", "entrance", "scrollMotion", "transition"],
223
+ effects: [
224
+ "opacity",
225
+ "boxShadow",
226
+ "filter",
227
+ "backdropFilter",
228
+ "transform",
229
+ "cursor",
230
+ "entrance",
231
+ "scrollMotion",
232
+ "loopMotion",
233
+ "transition",
234
+ ],
213
235
  icon: ["iconRotate", "iconFlip", "iconScale", "iconStrokeWidth", "iconShadow", "iconAnimation"],
214
236
  advanced: [],
215
237
  };
@@ -223,6 +245,9 @@ const ICON_MOVED: StyleKey[] = ["color", "transition"];
223
245
  /** Keys that only make sense on some element types. */
224
246
  const ONLY_FOR: Partial<Record<StyleKey, string[]>> = {
225
247
  objectFit: ["image", "video"],
248
+ objectPosition: ["image", "video"],
249
+ markerColor: ["list"],
250
+ markerSize: ["list"],
226
251
  ...Object.fromEntries(
227
252
  ["iconRotate", "iconFlip", "iconScale", "iconStrokeWidth", "iconShadow", "iconAnimation"].map(
228
253
  (key) => [key, ["icon"]],
@@ -276,7 +301,7 @@ const TYPE_SECTION_KEYS: Record<string, Partial<Record<StyleSectionId, StyleKey[
276
301
  const OFFSETS = new Set<StyleKey>(["top", "right", "bottom", "left"]);
277
302
 
278
303
  /** Offsets only do anything once the element is positioned (W-088). */
279
- export const offsetsApply = (position: StyleProps["position"]) =>
304
+ export const offsetsApply = (position: FlatStyleProps["position"]) =>
280
305
  position !== undefined && position !== "static";
281
306
 
282
307
  /**
@@ -287,8 +312,8 @@ export const offsetsApply = (position: StyleProps["position"]) =>
287
312
  export function keysFor(
288
313
  type: string,
289
314
  section: StyleSectionId,
290
- style?: StyleProps,
291
- position?: StyleProps["position"],
315
+ style?: FlatStyleProps,
316
+ position?: FlatStyleProps["position"],
292
317
  ): StyleKey[] {
293
318
  const offered = new Set(
294
319
  (
@@ -307,7 +332,7 @@ export function keysFor(
307
332
  /** The type's sections, plus any other section holding a set value, in section order. */
308
333
  export function sectionsFor(
309
334
  sections: StyleSectionId[],
310
- style: StyleProps | undefined,
335
+ style: FlatStyleProps | undefined,
311
336
  type?: string,
312
337
  ): StyleSectionId[] {
313
338
  const moved = type === "icon" ? ICON_MOVED : [];
@@ -337,6 +362,21 @@ export const STYLE_LABELS: Record<StyleKey, string> = {
337
362
  overflow: "Overflow",
338
363
  aspectRatio: "Aspect ratio",
339
364
  objectFit: "Object fit",
365
+ objectPosition: "Object position",
366
+ overflowX: "Overflow X",
367
+ overflowY: "Overflow Y",
368
+ fontStyle: "Style",
369
+ whiteSpace: "Line wrapping",
370
+ overflowWrap: "Long words",
371
+ backdropFilter: "Backdrop filters",
372
+ transform: "Transform",
373
+ flexGrow: "Grow",
374
+ flexShrink: "Shrink",
375
+ flexBasis: "Basis",
376
+ alignSelf: "Align self",
377
+ order: "Order",
378
+ markerColor: "Marker color",
379
+ markerSize: "Marker size",
340
380
  paddingTop: "Padding top",
341
381
  paddingRight: "Padding right",
342
382
  paddingBottom: "Padding bottom",
@@ -386,6 +426,7 @@ export const STYLE_LABELS: Record<StyleKey, string> = {
386
426
  cursor: "Cursor",
387
427
  entrance: "Entrance",
388
428
  scrollMotion: "Scroll motion",
429
+ loopMotion: "Loop",
389
430
  iconRotate: "Rotate",
390
431
  iconFlip: "Flip",
391
432
  iconScale: "Scale",
@@ -395,5 +436,5 @@ export const STYLE_LABELS: Record<StyleKey, string> = {
395
436
  transition: "Transition",
396
437
  };
397
438
 
398
- /** Every StyleProps key that the settings panel must be able to edit. */
439
+ /** Every FlatStyleProps key that the settings panel must be able to edit. */
399
440
  export const CONTROLLED_STYLE_KEYS: StyleKey[] = Object.values(SECTION_KEYS).flat();
@@ -7,8 +7,10 @@ import {
7
7
  type DesignSystem,
8
8
  type DeviceStyles,
9
9
  type LayoutNode,
10
- type PopupDevice,
10
+ type Device,
11
+ type FlatStyleProps,
11
12
  type StyleProps,
13
+ patchNestedStyle,
12
14
  type StyleStateName,
13
15
  type StyleStates,
14
16
  } from "../../../core/index.ts";
@@ -17,7 +19,7 @@ import type { StyleStateChoice } from "./settings/StateSwitcher.tsx";
17
19
  export type StyleSlot =
18
20
  | { owner: "element" | "class"; slot: "style" }
19
21
  | { owner: "element" | "class"; slot: "state"; state: StyleStateName }
20
- | { owner: "element" | "class"; slot: "device"; device: "tablet" | "mobile" };
22
+ | { owner: "element" | "class"; slot: "device"; device: "laptop" | "tablet" | "mobile" };
21
23
 
22
24
  /** Which style object a patch writes: the element's own, a class, a state, or a device. */
23
25
  export function styleTarget({
@@ -26,7 +28,7 @@ export function styleTarget({
26
28
  state,
27
29
  }: {
28
30
  classId: string | null;
29
- device: PopupDevice;
31
+ device: Device;
30
32
  state: StyleStateChoice;
31
33
  }): StyleSlot {
32
34
  const owner = classId ? "class" : "element";
@@ -35,8 +37,8 @@ export function styleTarget({
35
37
  return { owner, slot: "state", state };
36
38
  }
37
39
 
38
- const withStyle = (node: LayoutNode, patch: Partial<StyleProps>): LayoutNode => {
39
- const style: Record<string, unknown> = { ...node.style, ...patch };
40
+ const withStyle = (node: LayoutNode, patch: Partial<FlatStyleProps>): LayoutNode => {
41
+ const style = patchNestedStyle(node.style, patch);
40
42
  for (const key of Object.keys(style)) if (style[key] === undefined) delete style[key];
41
43
  return {
42
44
  ...node,
@@ -70,9 +72,9 @@ export function applyStylePatch({
70
72
  node: LayoutNode;
71
73
  design: DesignSystem;
72
74
  classId: string | null;
73
- device: PopupDevice;
75
+ device: Device;
74
76
  state: StyleStateChoice;
75
- patch: Partial<StyleProps>;
77
+ patch: Partial<FlatStyleProps>;
76
78
  }): StyleWrite {
77
79
  const target = styleTarget({ classId, device, state });
78
80
  if (target.slot === "device") {
@@ -0,0 +1,60 @@
1
+ import * as React from "react";
2
+ import { PLUGIN_ID } from "../../constants.ts";
3
+ import { requestJson, type Fetcher } from "../api.ts";
4
+
5
+ /** Matches the server's PRESENCE_LEASE_MS / 3 (W-929): two missed beats still leave it live. */
6
+ const PRESENCE_BEAT_MS = 30_000;
7
+ const ROUTE = `/_emdash/api/plugins/${PLUGIN_ID}/presence`;
8
+
9
+ const bodyOf = (collection: string, id: string, open: boolean) => ({
10
+ target: { collection, id },
11
+ open,
12
+ });
13
+
14
+ function sendPresence(
15
+ fetcher: Fetcher,
16
+ collection: string,
17
+ id: string,
18
+ open: boolean,
19
+ ): Promise<unknown> {
20
+ return requestJson(fetcher, ROUTE, { method: "POST", body: bodyOf(collection, id, open) });
21
+ }
22
+
23
+ /** The closing beat outlives the page (keepalive), so leaving the admin frees the page at once. */
24
+ function sendClosed(fetcher: Fetcher, collection: string, id: string): void {
25
+ fetcher(ROUTE, {
26
+ method: "POST",
27
+ keepalive: true,
28
+ headers: { "content-type": "application/json" },
29
+ body: JSON.stringify(bodyOf(collection, id, false)),
30
+ }).catch(() => undefined);
31
+ }
32
+
33
+ /**
34
+ * W-929 (D-064): tells the site this page is open in the editor while it is, so an agent's direct
35
+ * writes are refused and it proposes changes for review instead. Best effort: a failed beat only
36
+ * means an agent may write directly, which the editor's remote-change notice (W-466) still reports.
37
+ */
38
+ export function useEditorPresence(fetcher: Fetcher, collection: string, id: string): void {
39
+ React.useEffect(() => {
40
+ const beat = () => {
41
+ sendPresence(fetcher, collection, id, true).catch(() => undefined);
42
+ };
43
+ const close = () => sendClosed(fetcher, collection, id);
44
+ const show = () => {
45
+ if (document.visibilityState === "visible") beat();
46
+ };
47
+ beat();
48
+ const timer = setInterval(beat, PRESENCE_BEAT_MS);
49
+ window.addEventListener("pagehide", close);
50
+ window.addEventListener("pageshow", beat);
51
+ document.addEventListener("visibilitychange", show);
52
+ return () => {
53
+ clearInterval(timer);
54
+ window.removeEventListener("pagehide", close);
55
+ window.removeEventListener("pageshow", beat);
56
+ document.removeEventListener("visibilitychange", show);
57
+ close();
58
+ };
59
+ }, [fetcher, collection, id]);
60
+ }
@@ -13,6 +13,7 @@ import {
13
13
  type LayoutNode,
14
14
  type Removed,
15
15
  LAYOUT_SCHEMA_VERSION,
16
+ withShiftedMarks,
16
17
  } from "../../core/index.ts";
17
18
  import { withPageHeadingLevel } from "./dnd/new-element.ts";
18
19
  import type { PageDraft } from "../content-api.ts";
@@ -184,7 +185,10 @@ export function editorReducer(state: EditorState, action: EditorAction): EditorS
184
185
  if (!state.page.layout || !findNode(state.page.layout, action.id)) return state;
185
186
  return edited(state, {
186
187
  ...state.page,
187
- layout: updateNode(state.page.layout, action.id, action.update),
188
+ // W-950: formatted words follow an edit of the text, wherever it was made.
189
+ layout: updateNode(state.page.layout, action.id, (node) =>
190
+ withShiftedMarks(node, action.update(node)),
191
+ ),
188
192
  });
189
193
  case "delete-node": {
190
194
  if (!state.page.layout) return state;
@@ -211,7 +215,7 @@ export function editorReducer(state: EditorState, action: EditorAction): EditorS
211
215
  case "add-node": {
212
216
  if (!state.page.layout) return state;
213
217
  const place = { parentId: action.parentId, index: action.index };
214
- const node = withPageHeadingLevel(action.node, state.page.layout);
218
+ const node = withPageHeadingLevel(action.node, state.page.layout, state.page.partType);
215
219
  return arranged(state, addNode(state.page.layout, node, place));
216
220
  }
217
221
  case "move-node":
@@ -0,0 +1,177 @@
1
+ import * as React from "react";
2
+ import type { DesignSystem } from "../../core/schema/design.ts";
3
+ import {
4
+ cancelRequest,
5
+ createRequest,
6
+ recordAccepted,
7
+ withdrawProposal,
8
+ type PendingProposal,
9
+ } from "../proposals-api.ts";
10
+ import { ApiError, type Fetcher } from "../api.ts";
11
+ import { acceptOps, ghostCss, ghostOf, type EditorBase, type Ghost } from "./co-design.ts";
12
+ import type { EditorAction, EditorState } from "./store.ts";
13
+ import { useProposals, type ProposalsState } from "./useProposals.ts";
14
+
15
+ export type Decision = "accepted" | "rejected";
16
+
17
+ export type CoDesign = {
18
+ proposals: ProposalsState;
19
+ /** Per proposal id, per op index. Undecided ops are pending. */
20
+ decisions: Record<string, Record<number, Decision>>;
21
+ pending: (p: PendingProposal) => number[];
22
+ accept: (p: PendingProposal, indexes: readonly number[]) => void;
23
+ reject: (p: PendingProposal, indexes: readonly number[]) => void;
24
+ /** The proposal shown as a ghost on the canvas, if any. */
25
+ previewId: string | null;
26
+ setPreviewId: (id: string | null) => void;
27
+ ghost: (Ghost & { css: string }) | null;
28
+ /** Why the last accept was refused, in the editor's words. */
29
+ error: string | null;
30
+ clearError: () => void;
31
+ /** Changes accepted since the last save. */
32
+ unsaved: number;
33
+ /** W-931: sends a request to the person's agent; resolves to an error message, or null. */
34
+ ask: (prompt: string) => Promise<string | null>;
35
+ cancel: (requestId: string) => Promise<void>;
36
+ };
37
+
38
+ /** The editor state as a proposal base, with the style revision the server compares against. */
39
+ const editorBase = (state: EditorState): EditorBase => ({
40
+ rev: state.rev,
41
+ layout: state.page.layout,
42
+ design: state.design,
43
+ designRevision: state.designRevision ?? state.publishedRevision ?? null,
44
+ });
45
+
46
+ const stalePage =
47
+ "This proposal was made for an earlier version of the page. Ask the agent to propose again.";
48
+
49
+ /**
50
+ * W-930: review state for the co-design panel. Accepted page changes are one undoable edit each
51
+ * (`apply-arranged`); accepted class and variable changes go through the normal style draft save.
52
+ * A proposal is withdrawn once every change in it is decided, and the next Save draft after any
53
+ * accept is labelled "Via agent" in the timeline.
54
+ */
55
+ export function useCoDesign({
56
+ fetcher,
57
+ collection,
58
+ state,
59
+ dispatch,
60
+ changeDesign,
61
+ onError,
62
+ enabled = true,
63
+ }: {
64
+ fetcher: Fetcher;
65
+ collection: string;
66
+ state: EditorState;
67
+ dispatch: (action: EditorAction) => void;
68
+ changeDesign: (design: DesignSystem) => Promise<unknown>;
69
+ onError: (message: string) => void;
70
+ /** Off in field mode: agents propose to EmVB pages only. */
71
+ enabled?: boolean;
72
+ }): CoDesign {
73
+ const proposals = useProposals(fetcher, collection, state.id, enabled);
74
+ const [decisions, setDecisions] = React.useState<CoDesign["decisions"]>({});
75
+ const [previewId, setPreviewId] = React.useState<string | null>(null);
76
+ const [error, setError] = React.useState<string | null>(null);
77
+ const [unsaved, setUnsaved] = React.useState(0);
78
+ const latest = React.useRef(state);
79
+ latest.current = state;
80
+
81
+ const pending = React.useCallback(
82
+ (p: PendingProposal) =>
83
+ p.proposal.ops.map((_, i) => i).filter((i) => decisions[p.id]?.[i] === undefined),
84
+ [decisions],
85
+ );
86
+
87
+ const decide = (p: PendingProposal, indexes: readonly number[], decision: Decision) => {
88
+ const next = { ...decisions[p.id] };
89
+ for (const i of indexes) next[i] = decision;
90
+ setDecisions((all) => ({ ...all, [p.id]: next }));
91
+ if (p.proposal.ops.every((_, i) => next[i] !== undefined)) {
92
+ if (previewId === p.id) setPreviewId(null);
93
+ const decided = Object.values(next);
94
+ withdrawProposal(fetcher, p.id, {
95
+ accepted: decided.filter((d) => d === "accepted").length,
96
+ rejected: decided.filter((d) => d === "rejected").length,
97
+ }).then(proposals.refresh, () => undefined);
98
+ }
99
+ };
100
+
101
+ const accept = (p: PendingProposal, indexes: readonly number[]) => {
102
+ const current = latest.current;
103
+ const result = acceptOps(editorBase(current), p.proposal, indexes);
104
+ if (!result.ok) {
105
+ setError(result.code === "STALE_BASE" ? stalePage : result.reason);
106
+ return;
107
+ }
108
+ setError(null);
109
+ dispatch({
110
+ type: "apply-arranged",
111
+ layout: result.layout,
112
+ selected: current.selectedId ?? result.layout.root.id,
113
+ });
114
+ if (result.designChanged) {
115
+ changeDesign(result.design).catch((e: unknown) =>
116
+ onError(e instanceof Error ? e.message : "Couldn't save the accepted site styles."),
117
+ );
118
+ }
119
+ setUnsaved((n) => n + indexes.length);
120
+ decide(p, indexes, "accepted");
121
+ };
122
+
123
+ const reject = (p: PendingProposal, indexes: readonly number[]) => {
124
+ setError(null);
125
+ decide(p, indexes, "rejected");
126
+ };
127
+
128
+ // The next save after an accept is a normal version, labelled "Via agent" (W-930).
129
+ const savedRev = React.useRef(state.rev);
130
+ const unsavedRef = React.useRef(unsaved);
131
+ unsavedRef.current = unsaved;
132
+ React.useEffect(() => {
133
+ if (state.rev === savedRev.current) return;
134
+ savedRev.current = state.rev;
135
+ const accepted = unsavedRef.current;
136
+ if (accepted <= 0) return;
137
+ setUnsaved(0);
138
+ recordAccepted(fetcher, collection, state.id, accepted).catch(() => undefined);
139
+ }, [state.rev, fetcher, collection, state.id]);
140
+
141
+ const previewed = proposals.items.find((p) => p.id === previewId) ?? null;
142
+ const ghost = React.useMemo(() => {
143
+ if (!previewed) return null;
144
+ const g = ghostOf(editorBase(state), previewed.proposal, pending(previewed));
145
+ return g ? { ...g, css: ghostCss(g) } : null;
146
+ }, [previewed, state, pending]);
147
+
148
+ const ask = async (prompt: string) => {
149
+ try {
150
+ await createRequest(fetcher, collection, state.id, prompt);
151
+ proposals.refresh();
152
+ return null;
153
+ } catch (e) {
154
+ return e instanceof ApiError ? e.message : "Couldn't send the request. Try again.";
155
+ }
156
+ };
157
+ const cancel = async (requestId: string) => {
158
+ await cancelRequest(fetcher, requestId).catch(() => undefined);
159
+ proposals.refresh();
160
+ };
161
+
162
+ return {
163
+ ask,
164
+ cancel,
165
+ proposals,
166
+ decisions,
167
+ pending,
168
+ accept,
169
+ reject,
170
+ previewId: previewed ? previewId : null,
171
+ setPreviewId,
172
+ ghost,
173
+ error,
174
+ clearError: () => setError(null),
175
+ unsaved,
176
+ };
177
+ }
@@ -5,7 +5,7 @@ import {
5
5
  DEVICE_PREVIEW_PX,
6
6
  mergeDesign,
7
7
  type DesignSystem,
8
- type PopupDevice,
8
+ type Device,
9
9
  } from "../../core/index.ts";
10
10
  import { PAGES_COLLECTION, THEME_PARTS_COLLECTION } from "../../constants.ts";
11
11
  import { ApiError, type Fetcher } from "../api.ts";
@@ -19,13 +19,13 @@ const publicPath = (pattern: string | null | undefined, slug: string) =>
19
19
  (pattern || "/{slug}").replace("{slug}", encodeURIComponent(slug));
20
20
 
21
21
  /** Window heights for the sized Tablet and Mobile previews (W-158); widths are the canvas's. */
22
- const PREVIEW_HEIGHT = { tablet: 1024, mobile: 844 } as const;
22
+ const PREVIEW_HEIGHT = { laptop: 800, tablet: 1024, mobile: 844 } as const;
23
23
 
24
24
  /**
25
25
  * `window.open` features for a device preview (W-158): Desktop is an ordinary tab; Tablet and
26
26
  * Mobile open a window whose page area is the device width, so the page's own media queries apply.
27
27
  */
28
- export function previewWindowFeatures(device: PopupDevice): string | undefined {
28
+ export function previewWindowFeatures(device: Device): string | undefined {
29
29
  if (device === "desktop") return undefined;
30
30
  return `popup,width=${DEVICE_PREVIEW_PX[device]},height=${PREVIEW_HEIGHT[device]}`;
31
31
  }
@@ -137,7 +137,7 @@ export function useEditorCommands({
137
137
  });
138
138
  };
139
139
 
140
- const preview = (device: PopupDevice = "desktop") =>
140
+ const preview = (device: Device = "desktop") =>
141
141
  void openInNewTab(
142
142
  async () => {
143
143
  if (isDirty(latest.current) && !(await save())) return null;
@@ -0,0 +1,45 @@
1
+ import * as React from "react";
2
+ import { PLUGIN_ID } from "../../constants.ts";
3
+ import type { ExtensionManifest } from "../../core/index.ts";
4
+ import { requestJson, type Fetcher } from "../api.ts";
5
+ import { setAdminExtensionManifests } from "./extension-manifests.ts";
6
+
7
+ /**
8
+ * W-927: extension manifests from `emvb({ extensions })`, via the plugin's editor/config route.
9
+ * Empty while loading and when the route fails or no extensions are registered.
10
+ */
11
+ export function useExtensionManifests(fetcher: Fetcher): readonly ExtensionManifest[] {
12
+ const [loaded, setLoaded] = React.useState<readonly ExtensionManifest[]>([]);
13
+ React.useEffect(() => {
14
+ let cancelled = false;
15
+ void requestJson<{ extensions?: unknown }>(
16
+ fetcher,
17
+ `/_emdash/api/plugins/${PLUGIN_ID}/editor/config`,
18
+ )
19
+ .then((body) => {
20
+ if (cancelled) return;
21
+ const list = Array.isArray(body?.extensions) ? body.extensions : [];
22
+ const ok: ExtensionManifest[] = [];
23
+ for (const item of list) {
24
+ if (!item || typeof item !== "object") continue;
25
+ const m = item as ExtensionManifest;
26
+ if (m.extensionApi !== 1 || typeof m.id !== "string") continue;
27
+ ok.push(m);
28
+ }
29
+ setAdminExtensionManifests(ok);
30
+ setLoaded(ok);
31
+ })
32
+ .catch(() => undefined);
33
+ return () => {
34
+ cancelled = true;
35
+ };
36
+ }, [fetcher]);
37
+ return loaded;
38
+ }
39
+
40
+ /** Flat list of element manifests for Add panel and inspector. */
41
+ export function extensionElementManifests(
42
+ extensions: readonly ExtensionManifest[],
43
+ ): ExtensionManifest["elements"][number][] {
44
+ return extensions.flatMap((ext) => [...ext.elements]);
45
+ }
@@ -24,7 +24,8 @@ import {
24
24
  type Layout,
25
25
  type LayoutNode,
26
26
  } from "../../core/index.ts";
27
- import { newElement } from "./dnd/new-element.ts";
27
+ import { adminExtensionDefaults } from "./extension-manifests.ts";
28
+ import { newElement, withPageHeadingLevel } from "./dnd/new-element.ts";
28
29
  import { ELEMENT_NAMES } from "./element-chrome.ts";
29
30
  import type { ItemActions } from "./panels/settings/ItemList.tsx";
30
31
  import type { EditorAction, EditorState } from "./store.ts";
@@ -222,7 +223,7 @@ export function useNodeActions({
222
223
  announce(`${label} added inside ${nameOf(result.parentId) ?? "Container"}`);
223
224
  };
224
225
 
225
- const addFromPanel = (type: ElementType) => {
226
+ const addFromPanel = (type: ElementType | string) => {
226
227
  const current = latest.current.page.layout;
227
228
  if (!current && type === "container") {
228
229
  dispatch({ type: "add-root-container" });
@@ -231,9 +232,17 @@ export function useNodeActions({
231
232
  // UX-094: an empty page (one made outside EmVB has no layout) gets its container first, so
232
233
  // a click or palette insert lands instead of doing nothing.
233
234
  const layout = current ?? startingLayout();
234
- const node = newElement(type);
235
- if (!node) return;
236
- place(layout, node, ELEMENT_DESCRIPTORS.find((d) => d.type === type)?.name ?? type);
235
+ const added = newElement(type, undefined, adminExtensionDefaults(type));
236
+ if (!added) return;
237
+ // W-946: a click or palette insert picks the heading level the way a drop does.
238
+ const node = withPageHeadingLevel(added, layout, latest.current.page.partType);
239
+ const builtInName = ELEMENT_DESCRIPTORS.find((d) => d.type === type)?.name;
240
+ const label = builtInName
241
+ ? builtInName
242
+ : type.startsWith("ext:")
243
+ ? type.slice(type.lastIndexOf("/") + 1)
244
+ : type;
245
+ place(layout, node, label);
237
246
  };
238
247
 
239
248
  /** A section recipe built from the site's styles (W-320). */
@@ -241,7 +250,7 @@ export function useNodeActions({
241
250
  const layout = latest.current.page.layout ?? startingLayout();
242
251
  const node = recipeNode(id, latest.current.design, layout);
243
252
  if (!node) return;
244
- place(layout, withFreshIds(layout, node), `${node.label ?? "Section"} section`, true);
253
+ place(layout, withFreshIds(layout, node), `${node.editorLabel ?? "Section"} section`, true);
245
254
  };
246
255
 
247
256
  return {
@@ -0,0 +1,81 @@
1
+ import * as React from "react";
2
+ import {
3
+ listProposals,
4
+ listRequests,
5
+ type AgentRequest,
6
+ type PendingProposal,
7
+ } from "../proposals-api.ts";
8
+ import type { Fetcher } from "../api.ts";
9
+
10
+ /** How often the open editor asks for new proposals (D-063: EmDash's MCP has no push). */
11
+ const POLL_MS = 5_000;
12
+
13
+ export type ProposalsState = {
14
+ items: PendingProposal[];
15
+ /** True once the first request answered; false while loading or after a failure. */
16
+ ready: boolean;
17
+ failed: boolean;
18
+ refresh: () => void;
19
+ /** W-931: the page's requests for agents, newest first. */
20
+ requests: AgentRequest[];
21
+ /** When an agent last checked for requests (any page), or null if none has yet. */
22
+ agentSeenAt: string | null;
23
+ };
24
+
25
+ /**
26
+ * W-930: polls the page's proposals while the tab is visible. A failed poll keeps the last list
27
+ * and says so; the next tick retries.
28
+ */
29
+ export function useProposals(
30
+ fetcher: Fetcher,
31
+ collection: string,
32
+ id: string,
33
+ enabled = true,
34
+ ): ProposalsState {
35
+ const [items, setItems] = React.useState<PendingProposal[]>([]);
36
+ const [ready, setReady] = React.useState(false);
37
+ const [failed, setFailed] = React.useState(false);
38
+ const [tick, setTick] = React.useState(0);
39
+ const [requests, setRequests] = React.useState<AgentRequest[]>([]);
40
+ const [agentSeenAt, setAgentSeenAt] = React.useState<string | null>(null);
41
+ const refresh = React.useCallback(() => setTick((t) => t + 1), []);
42
+ React.useEffect(() => {
43
+ if (!enabled) return;
44
+ let cancelled = false;
45
+ const load = () => {
46
+ if (typeof document !== "undefined" && document.visibilityState === "hidden") return;
47
+ listRequests(fetcher, collection, id).then(
48
+ (next) => {
49
+ if (cancelled) return;
50
+ setRequests((prev) => (sameRequests(prev, next.items) ? prev : next.items));
51
+ setAgentSeenAt(next.agentSeenAt);
52
+ },
53
+ () => undefined,
54
+ );
55
+ listProposals(fetcher, collection, id).then(
56
+ (next) => {
57
+ if (cancelled) return;
58
+ setItems((prev) => (sameList(prev, next) ? prev : next));
59
+ setReady(true);
60
+ setFailed(false);
61
+ },
62
+ () => {
63
+ if (!cancelled) setFailed(true);
64
+ },
65
+ );
66
+ };
67
+ load();
68
+ const timer = setInterval(load, POLL_MS);
69
+ return () => {
70
+ cancelled = true;
71
+ clearInterval(timer);
72
+ };
73
+ }, [fetcher, collection, id, tick, enabled]);
74
+ return { items, ready, failed, refresh, requests, agentSeenAt };
75
+ }
76
+
77
+ const sameList = (a: PendingProposal[], b: PendingProposal[]) =>
78
+ a.length === b.length && a.every((p, i) => p.id === b[i]?.id);
79
+
80
+ const sameRequests = (a: AgentRequest[], b: AgentRequest[]) =>
81
+ a.length === b.length && a.every((r, i) => r.id === b[i]?.id && r.status === b[i]?.status);