@perkyzz/emvb 1.4.0 → 1.4.1

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 (52) hide show
  1. package/package.json +2 -2
  2. package/src/admin/content-item.ts +3 -0
  3. package/src/admin/copy-rich-text.ts +99 -9
  4. package/src/admin/editor/Editor.tsx +7 -2
  5. package/src/admin/editor/collection-previews.ts +7 -8
  6. package/src/admin/editor/extensions/ExtensionPanels.tsx +33 -1
  7. package/src/admin/editor/extensions-context.tsx +16 -1
  8. package/src/admin/editor/feature-css.ts +5 -0
  9. package/src/admin/editor/panels/ConditionsEditor.tsx +18 -0
  10. package/src/admin/editor/panels/ElementPanel.tsx +2 -2
  11. package/src/admin/editor/panels/settings/FieldControl.tsx +15 -0
  12. package/src/admin/editor/panels/settings/FormBindControl.tsx +27 -31
  13. package/src/admin/editor/panels/settings/ImageFieldControl.tsx +106 -0
  14. package/src/admin/editor/panels/settings/loop-context.ts +34 -3
  15. package/src/admin/export-api.ts +2 -0
  16. package/src/admin/extension-outputs.ts +57 -0
  17. package/src/admin/import-forms.ts +133 -0
  18. package/src/admin/pages/ImportElementorDialog.tsx +32 -0
  19. package/src/admin/pages/ImportForms.tsx +71 -0
  20. package/src/admin/setup/plan.ts +2 -0
  21. package/src/admin/ui.ts +7 -0
  22. package/src/admin/widgets/ConvertToRichText.tsx +3 -2
  23. package/src/astro/render.ts +6 -0
  24. package/src/astro/resolve.ts +2 -0
  25. package/src/astro/theme-posts.ts +17 -23
  26. package/src/astro/visitor.ts +14 -5
  27. package/src/constants.ts +1 -1
  28. package/src/core/css/generate.ts +6 -1
  29. package/src/core/css/trace.ts +8 -0
  30. package/src/core/data/bindings.ts +7 -4
  31. package/src/core/elements/dynamic-elements.ts +1 -1
  32. package/src/core/elements/element-nouns.ts +48 -0
  33. package/src/core/export/site.ts +3 -0
  34. package/src/core/extensions/index.ts +2 -0
  35. package/src/core/extensions/placeholder.ts +7 -2
  36. package/src/core/extensions/render-ext.ts +49 -15
  37. package/src/core/import/elementor/context.ts +29 -0
  38. package/src/core/import/elementor/index.ts +23 -2
  39. package/src/core/import/elementor/widgets.ts +137 -17
  40. package/src/core/index.ts +9 -0
  41. package/src/core/render/index.ts +15 -2
  42. package/src/core/schema/layout.ts +4 -1
  43. package/src/core/theme/media-value.ts +88 -0
  44. package/src/core/visitor-cookie.ts +91 -0
  45. package/src/index.ts +28 -1
  46. package/src/mcp/agent-defaults.ts +40 -0
  47. package/src/mcp/extensions.ts +138 -0
  48. package/src/mcp/read.ts +55 -19
  49. package/src/mcp/tools.ts +3 -3
  50. package/src/mcp/write.ts +83 -8
  51. package/src/server/extension-routes.ts +37 -0
  52. package/src/server/plugin.ts +7 -1
package/src/mcp/write.ts CHANGED
@@ -2,7 +2,6 @@ import { z } from "zod";
2
2
  import { boundedArray } from "../core/bounded-array.ts";
3
3
  import {
4
4
  byteLength,
5
- defaultElement,
6
5
  findNode,
7
6
  MAX_LAYOUT_BYTES,
8
7
  nameKey,
@@ -24,6 +23,7 @@ import {
24
23
  import { stableJson } from "../core/stable-json.ts";
25
24
  import { addNode, moveNode, withFreshIds } from "../core/arrange.ts";
26
25
  import { ELEMENT_DESCRIPTORS } from "../core/elements/index.ts";
26
+ import { isExtensionType, liveElement } from "../core/extensions/index.ts";
27
27
  import { nodeChildren, pageSlug, removeNode, starterLayout, updateNode } from "../core/tree-ops.ts";
28
28
  import { isSafeFontStack } from "../core/sanitize/css.ts";
29
29
  import { sanitizeMediaUrl } from "../core/sanitize/media-url.ts";
@@ -36,8 +36,10 @@ import {
36
36
  patchNestedStyle,
37
37
  upgradeLegacyNode,
38
38
  } from "../core/migrate/v18.ts";
39
+ import { agentDefaultElement } from "./agent-defaults.ts";
40
+ import { checkedExtensionProps } from "./extensions.ts";
39
41
  import { McpToolError } from "./errors.ts";
40
- import type { EmvbPorts } from "./ports.ts";
42
+ import type { EmvbPorts, PageRecord } from "./ports.ts";
41
43
  import { queued } from "./queue.ts";
42
44
  import { nestsDeeperThan, resolveDesignRefs } from "./refs.ts";
43
45
  import {
@@ -103,7 +105,10 @@ export async function mutatePage(
103
105
  tool: string,
104
106
  pageId: string,
105
107
  expectedRev: string | undefined,
106
- apply: (layout: Layout) => { layout: Layout; changed: string[]; summary: string },
108
+ apply: (
109
+ layout: Layout,
110
+ page: PageRecord,
111
+ ) => { layout: Layout; changed: string[]; summary: string },
107
112
  ): Promise<WriteResult> {
108
113
  return queued(`page:${pageId}`, async () => {
109
114
  // The retry must read, write and log in order.
@@ -132,7 +137,7 @@ export async function mutatePage(
132
137
  "The stored layout can't be read. Repair it in the editor.",
133
138
  );
134
139
  }
135
- const next = apply(current.layout);
140
+ const next = apply(current.layout, page);
136
141
  refuseUnsafeImageSources(current.layout, next.layout);
137
142
  refuseUnsafeLinks(current.layout, next.layout);
138
143
  refuseUnsafeSvg(current.layout, next.layout);
@@ -306,13 +311,27 @@ const BoundedElementSpec = z.preprocess((value, ctx) => {
306
311
  return upgradeLegacyNode(value);
307
312
  }, ElementSpec);
308
313
 
314
+ /**
315
+ * W-1089 (#38): a registered extension element, its props checked by the extension's schema.
316
+ * Extension elements hold no children (their render output is built-in elements).
317
+ */
318
+ function buildExtensionNode(spec: ElementSpecT): LayoutNode {
319
+ const { type, props, children, ...rest } = spec;
320
+ delete (rest as { id?: unknown }).id;
321
+ const checked = checkedExtensionProps(type, props ?? {});
322
+ if (children?.length) refused(`${type} is an extension element and holds no children.`);
323
+ return { ...rest, id: "new", type, props: checked } as LayoutNode;
324
+ }
325
+
309
326
  function buildNode(spec: ElementSpecT): LayoutNode {
327
+ if (isExtensionType(spec.type)) return buildExtensionNode(spec);
310
328
  if (!KNOWN_TYPES.has(spec.type)) {
311
329
  refused(`Unknown element type ${spec.type}. Call list_element_types.`);
312
330
  }
313
331
  const { type, props, children, ...rest } = spec;
314
332
  delete (rest as { id?: unknown }).id;
315
- const base = defaultElement(type as ElementType, "new") as LayoutNode & {
333
+ // W-1089 (#29): the type's defaults without the Add panel's sample content.
334
+ const base = agentDefaultElement(type as ElementType, "new") as LayoutNode & {
316
335
  props?: Record<string, unknown>;
317
336
  };
318
337
  const node = { ...base, ...rest, props: { ...base.props, ...props } } as LayoutNode;
@@ -333,6 +352,11 @@ export const CreatePageInput = z.strictObject({
333
352
  .optional(),
334
353
  /** W-1068: start from a starter page template (see list_templates) instead of recipes. */
335
354
  template: z.enum(PAGE_TEMPLATES.map((t) => t.id) as [string, ...string[]]).optional(),
355
+ /**
356
+ * W-1089 (#28): a page without a template or a hero recipe starts with an H1 of its title.
357
+ * `false` leaves it out, for an agent that adds its own H1. Unset keeps it.
358
+ */
359
+ titleHeading: z.boolean().optional(),
336
360
  });
337
361
  export const CreatePageOutput = z.object({ pageId: z.string(), slug: z.string(), rev: z.string() });
338
362
 
@@ -393,6 +417,9 @@ export async function createPage(ports: EmvbPorts, input: z.infer<typeof CreateP
393
417
  };
394
418
  if (recipes[0] === "hero") layout = { ...layout, root: { ...layout.root, children: [] } };
395
419
  }
420
+ if (input.titleHeading === false && !input.template) {
421
+ layout = { ...layout, root: { ...layout.root, children: [] } };
422
+ }
396
423
  for (const id of recipes) {
397
424
  const node = recipeNode(id, design, layout);
398
425
  if (node)
@@ -429,22 +456,57 @@ export const AddElementInput = z.strictObject({
429
456
  expectedRev: Rev.optional(),
430
457
  });
431
458
 
459
+ const isH1 = (node: LayoutNode): boolean =>
460
+ (node.type === "heading" || node.type === "headline") && propsOf(node)["level"] === 1;
461
+ const h1Ids = (node: LayoutNode): string[] => [
462
+ ...(isH1(node) ? [node.id] : []),
463
+ ...nodeChildren(node).flatMap(h1Ids),
464
+ ];
465
+
466
+ /**
467
+ * W-1089 (#28): the H1 create_page starts a page with, as long as nobody changed it: a heading
468
+ * straight in the root with the page's title as its only text and nothing else set.
469
+ */
470
+ function untouchedTitleHeading(layout: Layout, title: string): LayoutNode | undefined {
471
+ return nodeChildren(layout.root).find((node) => {
472
+ if (
473
+ node.type !== "heading" ||
474
+ Object.keys(node).some((k) => !["id", "type", "props"].includes(k))
475
+ )
476
+ return false;
477
+ const props = propsOf(node);
478
+ return (
479
+ Object.keys(props).length === 2 && props["level"] === 1 && props["text"] === title.trim()
480
+ );
481
+ });
482
+ }
483
+
432
484
  export async function addElement(ports: EmvbPorts, input: z.infer<typeof AddElementInput>) {
433
485
  const element = resolveDesignRefs(input.element, (await ports.getDesign()).design);
434
- return mutatePage(ports, "add_element", input.pageId, input.expectedRev, (layout) => {
486
+ return mutatePage(ports, "add_element", input.pageId, input.expectedRev, (layout, page) => {
435
487
  if (!findNode(layout, input.parentId)) {
436
488
  refused(
437
489
  `No element with id ${input.parentId} on this page to add into. Call get_layout for the ids.`,
438
490
  );
439
491
  }
440
492
  const node = withFreshIds(layout, buildNode(element));
441
- const next = arranged(
493
+ let next = arranged(
442
494
  addNode(layout, node, {
443
495
  parentId: input.parentId,
444
496
  index: input.index ?? Number.MAX_SAFE_INTEGER,
445
497
  }),
446
498
  );
447
- return { layout: next, changed: [node.id], summary: `Added ${node.type} ${node.id}` };
499
+ const changed = [node.id];
500
+ let summary = `Added ${node.type} ${node.id}`;
501
+ // W-1089 (#28): an agent that builds the page's own H1 replaces the title heading
502
+ // create_page started it with, instead of leaving two H1s with the title out of place.
503
+ const title = h1Ids(node).length > 0 ? untouchedTitleHeading(layout, page.title) : undefined;
504
+ if (title && h1Ids(layout.root).length === 1 && input.parentId !== title.id) {
505
+ next = removeNode(next, title.id).layout;
506
+ changed.push(title.id);
507
+ summary += `; removed the page's title heading ${title.id} (the new element has the page's H1)`;
508
+ }
509
+ return { layout: next, changed, summary };
448
510
  });
449
511
  }
450
512
 
@@ -584,6 +646,19 @@ export async function updateElement(ports: EmvbPorts, input: z.infer<typeof Upda
584
646
  if (isRecord(style)) copy[top] = patchNestedStyle(style, { [sub]: undefined });
585
647
  } else if (isRecord(copy[top])) delete copy[top][sub];
586
648
  }
649
+ // W-1089 (#38): an extension element changes only while its extension is registered, and
650
+ // its props must still pass the extension's schema.
651
+ if (isExtensionType(node.type)) {
652
+ if (!liveElement(node.type)) {
653
+ refused(
654
+ `${node.type} can't be changed: no extension on this site provides it. Remove it or install the extension.`,
655
+ );
656
+ }
657
+ const touchesProps =
658
+ "props" in set ||
659
+ (input.unset ?? []).some((path) => path === "props" || path.startsWith("props."));
660
+ if (touchesProps) copy["props"] = checkedExtensionProps(node.type, copy["props"]);
661
+ }
587
662
  return copy as LayoutNode;
588
663
  });
589
664
  return { layout: next, changed: [input.nodeId], summary: `Updated ${input.nodeId}` };
@@ -4,7 +4,9 @@ import {
4
4
  extensionOutputKey,
5
5
  isExtensionType,
6
6
  liveExtensionOutput,
7
+ resolveExtensionSource,
7
8
  withExtensionClasses,
9
+ type ExtensionManifest,
8
10
  } from "../core/extensions/index.ts";
9
11
  import { MAX_DEPTH, MAX_LAYOUT_BYTES } from "../core/limits.ts";
10
12
 
@@ -71,3 +73,38 @@ export const extensionRenderRoute: PluginRoute<z.infer<typeof RenderInput>> = {
71
73
  }),
72
74
  }),
73
75
  };
76
+
77
+ /** One source's values for the editor's panels; `values: null` when it can't be read here. */
78
+ type ExtensionSourceValues = { id: string; values: Record<string, string> | null };
79
+
80
+ /**
81
+ * W-1088 (#36): the values of every source an extension panel names, for the editor's left panel,
82
+ * which lists them read-only. Sources are read-only resolvers; their values are cleaned like bound
83
+ * values. `values: null` when the source's code isn't in this server (an extension without
84
+ * `module` on Workers or a production build), it throws, or it doesn't answer in time.
85
+ */
86
+ export function extensionSourcesRoute(manifests: readonly ExtensionManifest[]): PluginRoute {
87
+ return {
88
+ permission: "content:edit_any",
89
+ handler: async () => {
90
+ const wanted = new Map<string, { pkg: string; name: string }>();
91
+ for (const manifest of manifests) {
92
+ for (const panel of manifest.panels) {
93
+ if (!panel.source) continue;
94
+ if (!manifest.sources.some((source) => source.name === panel.source)) continue;
95
+ wanted.set(`ext:${manifest.id}/${panel.source}`, {
96
+ pkg: manifest.id,
97
+ name: panel.source,
98
+ });
99
+ }
100
+ }
101
+ const sources: ExtensionSourceValues[] = await Promise.all(
102
+ [...wanted].map(async ([id, { pkg, name }]) => ({
103
+ id,
104
+ values: (await resolveExtensionSource(pkg, name)) ?? null,
105
+ })),
106
+ );
107
+ return { sources };
108
+ },
109
+ };
110
+ }
@@ -1,5 +1,6 @@
1
1
  import { setLoopCollections } from "../core/loop-collections.ts";
2
2
  import { setGeoTrust } from "../core/geo-trust.ts";
3
+ import { setVisitorCookie } from "../core/visitor-cookie.ts";
3
4
  import {
4
5
  proposalCreateRoute,
5
6
  proposalGetRoute,
@@ -36,7 +37,7 @@ import {
36
37
  import { canvasStyleUrls } from "../core/canvas-styles.ts";
37
38
  import { canvasCollectionSlugs, setCanvasCollections } from "../core/canvas-collections.ts";
38
39
  import { manifestsFromOptions } from "../core/extensions/index.ts";
39
- import { extensionRenderRoute } from "./extension-routes.ts";
40
+ import { extensionRenderRoute, extensionSourcesRoute } from "./extension-routes.ts";
40
41
  import { TOOLS } from "../mcp/tools.ts";
41
42
  import { mcpRoutes, mcpToolConfig } from "./mcp-routes.ts";
42
43
 
@@ -53,9 +54,12 @@ export function createPlugin(
53
54
  extensions?: unknown;
54
55
  trustGeoHeaders?: unknown;
55
56
  loopCollections?: unknown;
57
+ visitorCookie?: unknown;
58
+ visitorCookieConsent?: unknown;
56
59
  } = {},
57
60
  ) {
58
61
  setGeoTrust(options.trustGeoHeaders);
62
+ setVisitorCookie(options.visitorCookie, options.visitorCookieConsent);
59
63
  setLoopCollections(options.loopCollections);
60
64
  setCanvasCollections(options.canvasCollections);
61
65
  const canvasStyles = canvasStyleUrls(options.canvasStyles);
@@ -138,6 +142,8 @@ export function createPlugin(
138
142
  }),
139
143
  },
140
144
  "editor/extensions/render": extensionRenderRoute,
145
+ // W-1088 (#36): the values of the sources the extension panels name.
146
+ "editor/extensions/sources": extensionSourcesRoute(extensions),
141
147
  design: designRoute,
142
148
  "design/draft": designDraftRoute,
143
149
  "design/save": designSaveRoute,