payload-canvas 0.5.2 → 0.6.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 (193) hide show
  1. package/README.md +333 -28
  2. package/dist/admin/ArrayRowLabel.d.ts +12 -0
  3. package/dist/admin/ArrayRowLabel.js +21 -0
  4. package/dist/admin/client.d.ts +1 -0
  5. package/dist/admin/client.js +2 -0
  6. package/dist/admin/editor/Editor.js +8 -1
  7. package/dist/admin/editor/Inspector.js +23 -17
  8. package/dist/admin/editor/Library.d.ts +2 -2
  9. package/dist/admin/editor/Library.js +7 -5
  10. package/dist/admin/editor/Outline.js +20 -12
  11. package/dist/admin/editor/Overlay.js +6 -4
  12. package/dist/admin/editor/actions.d.ts +7 -2
  13. package/dist/admin/editor/actions.js +44 -12
  14. package/dist/admin/editor/assistant/AssistantPanel.js +32 -20
  15. package/dist/admin/editor/comments/controller.d.ts +7 -0
  16. package/dist/admin/editor/comments/controller.js +12 -0
  17. package/dist/admin/editor/comments/useCommentFromURL.d.ts +2 -0
  18. package/dist/admin/editor/comments/useCommentFromURL.js +36 -0
  19. package/dist/admin/editor/dnd/smooth.js +1 -1
  20. package/dist/admin/editor/editor.scss +28 -0
  21. package/dist/admin/editor/fields/ArrayField.js +3 -3
  22. package/dist/admin/editor/fields/FormBlock.d.ts +20 -0
  23. package/dist/admin/editor/fields/FormBlock.js +167 -0
  24. package/dist/admin/editor/fields/FormFieldName.d.ts +25 -0
  25. package/dist/admin/editor/fields/FormFieldName.js +58 -0
  26. package/dist/admin/editor/fields/GroupField.d.ts +22 -2
  27. package/dist/admin/editor/fields/GroupField.js +30 -1
  28. package/dist/admin/editor/fields/MergeTags.d.ts +8 -0
  29. package/dist/admin/editor/fields/MergeTags.js +62 -0
  30. package/dist/admin/editor/fields/arrayRows.d.ts +0 -2
  31. package/dist/admin/editor/fields/arrayRows.js +0 -4
  32. package/dist/admin/editor/fields/fields.scss +150 -0
  33. package/dist/admin/editor/fields/values.d.ts +0 -2
  34. package/dist/admin/editor/fields/values.js +0 -14
  35. package/dist/admin/editor/icons.d.ts +3 -0
  36. package/dist/admin/editor/icons.js +3 -0
  37. package/dist/admin/editor/insert/InsertHandle.js +1 -1
  38. package/dist/admin/editor/insert/picker.d.ts +3 -2
  39. package/dist/admin/editor/insert/picker.js +9 -3
  40. package/dist/admin/editor/layout/LeftPanel.js +5 -3
  41. package/dist/admin/editor/layout/leftTabs.d.ts +15 -0
  42. package/dist/admin/editor/layout/leftTabs.js +18 -0
  43. package/dist/admin/editor/media/ImageEditor.d.ts +8 -2
  44. package/dist/admin/editor/media/ImageEditor.js +16 -10
  45. package/dist/admin/editor/media/media.scss +12 -2
  46. package/dist/admin/editor/media/model.d.ts +10 -1
  47. package/dist/admin/editor/media/model.js +19 -0
  48. package/dist/admin/editor/menu/blockMenu.js +3 -3
  49. package/dist/admin/editor/names.d.ts +9 -0
  50. package/dist/admin/editor/names.js +18 -0
  51. package/dist/admin/editor/regions/RegionFields.d.ts +6 -0
  52. package/dist/admin/editor/regions/RegionFields.js +122 -0
  53. package/dist/admin/editor/regions/RegionPane.js +5 -3
  54. package/dist/admin/editor/regions/RegionRows.js +2 -2
  55. package/dist/admin/editor/regions/required.d.ts +16 -0
  56. package/dist/admin/editor/regions/required.js +43 -0
  57. package/dist/admin/editor/renderField.d.ts +9 -2
  58. package/dist/admin/editor/renderField.js +86 -32
  59. package/dist/admin/editor/runtime.d.ts +3 -2
  60. package/dist/admin/editor/runtime.js +12 -18
  61. package/dist/admin/editor/styles/StylesPanel.js +4 -2
  62. package/dist/admin/editor/styles/context.js +9 -4
  63. package/dist/admin/editor/topbar/ContentModeToggle.js +1 -1
  64. package/dist/admin/editor/topbar/DocumentActions.js +4 -3
  65. package/dist/admin/editor/topbar/contentMode.scss +0 -9
  66. package/dist/admin/editor/topbar/topbar.scss +1 -41
  67. package/dist/admin/editor/ui/Tabs.d.ts +17 -0
  68. package/dist/admin/editor/ui/Tabs.js +17 -0
  69. package/dist/admin/forms/SubmissionCells.d.ts +6 -0
  70. package/dist/admin/forms/SubmissionCells.js +31 -0
  71. package/dist/admin/forms/SubmissionView.d.ts +5 -0
  72. package/dist/admin/forms/SubmissionView.js +106 -0
  73. package/dist/admin/forms/submissionData.d.ts +32 -0
  74. package/dist/admin/forms/submissionData.js +63 -0
  75. package/dist/admin/forms/submissions.scss +197 -0
  76. package/dist/admin/rsc.d.ts +2 -0
  77. package/dist/admin/rsc.js +5 -0
  78. package/dist/ai/endpoint.d.ts +5 -0
  79. package/dist/ai/endpoint.js +18 -8
  80. package/dist/ai/prompt.d.ts +8 -0
  81. package/dist/ai/prompt.js +30 -0
  82. package/dist/ai/tools.d.ts +6 -1
  83. package/dist/ai/tools.js +90 -6
  84. package/dist/blocks/builtins/carousel.js +10 -1
  85. package/dist/blocks/index.d.ts +1 -1
  86. package/dist/blocks/index.js +1 -1
  87. package/dist/blocks/payload.d.ts +7 -0
  88. package/dist/blocks/payload.js +16 -2
  89. package/dist/comments/shared.d.ts +5 -0
  90. package/dist/comments/shared.js +5 -0
  91. package/dist/core/blocks.d.ts +17 -3
  92. package/dist/core/blocks.js +92 -10
  93. package/dist/core/classes.d.ts +5 -0
  94. package/dist/core/classes.js +26 -1
  95. package/dist/core/conditions.d.ts +7 -0
  96. package/dist/core/conditions.js +22 -0
  97. package/dist/core/dropTarget.d.ts +3 -2
  98. package/dist/core/dropTarget.js +7 -1
  99. package/dist/core/index.d.ts +6 -3
  100. package/dist/core/index.js +6 -3
  101. package/dist/core/payloadBlocks.js +25 -3
  102. package/dist/core/references.d.ts +7 -0
  103. package/dist/core/references.js +29 -11
  104. package/dist/core/regions.d.ts +14 -0
  105. package/dist/core/regions.js +17 -1
  106. package/dist/core/rowLabel.d.ts +8 -0
  107. package/dist/core/rowLabel.js +98 -0
  108. package/dist/core/starter.d.ts +10 -0
  109. package/dist/core/starter.js +40 -0
  110. package/dist/core/tree.js +4 -0
  111. package/dist/core/types.d.ts +96 -0
  112. package/dist/core/uniqueProps.d.ts +40 -0
  113. package/dist/core/uniqueProps.js +156 -0
  114. package/dist/core/validate.js +43 -3
  115. package/dist/forms/answers.d.ts +21 -0
  116. package/dist/forms/answers.js +82 -0
  117. package/dist/forms/blocks.d.ts +33 -0
  118. package/dist/forms/blocks.js +178 -0
  119. package/dist/forms/conditions.d.ts +78 -0
  120. package/dist/forms/conditions.js +184 -0
  121. package/dist/forms/emails.d.ts +23 -0
  122. package/dist/forms/emails.js +64 -0
  123. package/dist/forms/fieldConfigs.d.ts +35 -0
  124. package/dist/forms/fieldConfigs.js +167 -0
  125. package/dist/forms/fieldOps.d.ts +78 -0
  126. package/dist/forms/fieldOps.js +378 -0
  127. package/dist/forms/fieldSettings.d.ts +81 -0
  128. package/dist/forms/fieldSettings.js +239 -0
  129. package/dist/forms/files.d.ts +54 -0
  130. package/dist/forms/files.js +128 -0
  131. package/dist/forms/index.d.ts +16 -0
  132. package/dist/forms/index.js +18 -0
  133. package/dist/forms/mergeTags.d.ts +103 -0
  134. package/dist/forms/mergeTags.js +150 -0
  135. package/dist/forms/noScript.d.ts +60 -0
  136. package/dist/forms/noScript.js +109 -0
  137. package/dist/forms/pageForms.d.ts +99 -0
  138. package/dist/forms/pageForms.js +294 -0
  139. package/dist/forms/pageSync.d.ts +91 -0
  140. package/dist/forms/pageSync.js +325 -0
  141. package/dist/forms/rateLimit.d.ts +21 -0
  142. package/dist/forms/rateLimit.js +50 -0
  143. package/dist/forms/settings.d.ts +13 -0
  144. package/dist/forms/settings.js +92 -0
  145. package/dist/forms/spam.d.ts +100 -0
  146. package/dist/forms/spam.js +218 -0
  147. package/dist/forms/spamSettings.d.ts +43 -0
  148. package/dist/forms/spamSettings.js +25 -0
  149. package/dist/forms/submission.d.ts +34 -0
  150. package/dist/forms/submission.js +89 -0
  151. package/dist/forms/submissionsAdmin.d.ts +26 -0
  152. package/dist/forms/submissionsAdmin.js +108 -0
  153. package/dist/forms/summary.d.ts +60 -0
  154. package/dist/forms/summary.js +113 -0
  155. package/dist/forms/uploads.d.ts +41 -0
  156. package/dist/forms/uploads.js +132 -0
  157. package/dist/index.d.ts +8 -2
  158. package/dist/index.js +8 -2
  159. package/dist/live/document.d.ts +12 -0
  160. package/dist/live/document.js +8 -1
  161. package/dist/live/endpoints.d.ts +1 -0
  162. package/dist/live/session.js +3 -1
  163. package/dist/live/storage.d.ts +13 -2
  164. package/dist/live/storage.js +18 -7
  165. package/dist/mcp/forms.d.ts +26 -0
  166. package/dist/mcp/forms.js +280 -0
  167. package/dist/mcp/index.d.ts +7 -0
  168. package/dist/mcp/index.js +29 -10
  169. package/dist/mcp/shared.d.ts +1 -0
  170. package/dist/mcp/shared.js +1 -0
  171. package/dist/plugin/forms.d.ts +30 -0
  172. package/dist/plugin/forms.js +63 -0
  173. package/dist/plugin/formsSync.d.ts +138 -0
  174. package/dist/plugin/formsSync.js +345 -0
  175. package/dist/plugin/hook.d.ts +22 -0
  176. package/dist/plugin/hook.js +19 -7
  177. package/dist/plugin/index.d.ts +14 -0
  178. package/dist/plugin/index.js +99 -28
  179. package/dist/plugin/links.d.ts +7 -2
  180. package/dist/plugin/links.js +9 -3
  181. package/dist/plugin/references.d.ts +6 -0
  182. package/dist/plugin/references.js +12 -0
  183. package/dist/react/canvas/BuilderCanvas.d.ts +2 -0
  184. package/dist/react/canvas/BuilderCanvas.js +29 -6
  185. package/dist/react/canvas/inline/mapper.d.ts +6 -1
  186. package/dist/react/canvas/inline/mapper.js +35 -4
  187. package/dist/react/render/LayoutOutlet.d.ts +7 -3
  188. package/dist/react/render/LayoutOutlet.js +9 -4
  189. package/dist/react/render/RenderLayout.js +13 -7
  190. package/dist/react/render/resolve.d.ts +1 -0
  191. package/dist/react/render/resolve.js +5 -1
  192. package/dist/react/server.js +19 -0
  193. package/package.json +5 -1
@@ -189,7 +189,7 @@ export function dropAt(runtime, p, source) {
189
189
  depth: r.depth,
190
190
  rect: { x: outline.rowX, y: outline.rect.y + r.top - outline.scrollTop, width: outline.rowWidth, height: r.height },
191
191
  }));
192
- const target = outlineDropTarget(layout, blocks, rows, p, source, OUTLINE_INDENT);
192
+ const target = outlineDropTarget(layout, blocks, rows, p, source, OUTLINE_INDENT, outline.rect);
193
193
  if (target || source.kind !== 'block')
194
194
  return { zone: 'outline', target };
195
195
  // Over its own row: the block stays where it is.
@@ -51,6 +51,11 @@
51
51
  --be-radius: var(--style-radius-s, 4px);
52
52
  --be-radius-m: var(--style-radius-m, 6px);
53
53
  --be-ease: cubic-bezier(0.2, 0.8, 0.2, 1);
54
+ // Components (live saved section copies) get their own color, as in Figma: the outline,
55
+ // the solid fill of the name tag and action bar (white text passes AA on it), and text.
56
+ --be-component: #8b5cf6;
57
+ --be-component-solid: #7c3aed;
58
+ --be-component-text: #6d28d9;
54
59
 
55
60
  display: flex;
56
61
  flex-direction: column;
@@ -987,6 +992,19 @@
987
992
  }
988
993
  }
989
994
 
995
+ // Dark admin theme: a lighter text color, so it passes AA on the dark panel.
996
+ html[data-theme='dark'] .builder-editor {
997
+ --be-component-text: #a78bfa;
998
+ }
999
+
1000
+ // A component's Layers row: its icon and name in the component color.
1001
+ .builder-editor__row[data-component] {
1002
+ .builder-editor__row-icon,
1003
+ .builder-editor__row-type {
1004
+ color: var(--be-component-text);
1005
+ }
1006
+ }
1007
+
990
1008
  .builder-editor__row--dragging {
991
1009
  opacity: 0.4;
992
1010
  }
@@ -1610,6 +1628,16 @@
1610
1628
  box-shadow: inset 0 0 0 var(--be-px) var(--be-accent);
1611
1629
  }
1612
1630
 
1631
+ // A component (`data-component`): its hover and selection outline, name tag, action bar and
1632
+ // Layers row use the component color instead of the accent.
1633
+ :is(.builder-editor__hover, .builder-editor__selection, .builder-editor__actions, .builder-editor__row)[data-component] {
1634
+ --be-accent: var(--be-component);
1635
+ --be-accent-strong: var(--be-component-solid);
1636
+ --be-accent-soft: color-mix(in srgb, var(--be-component) 14%, transparent);
1637
+ --be-accent-text: var(--be-component-text);
1638
+ --be-accent-solid: var(--be-component-solid);
1639
+ }
1640
+
1613
1641
  .builder-editor__parent-hint {
1614
1642
  outline: var(--be-px) dashed color-mix(in srgb, var(--be-accent) 55%, transparent);
1615
1643
  outline-offset: calc(var(--be-px) * -1);
@@ -7,9 +7,9 @@ import { Icon } from '../icons.js';
7
7
  import { RenderBlockFields } from '../renderField.js';
8
8
  import { Popover, usePopover } from '../styles/popover.js';
9
9
  import { AddButton } from './AddButton.js';
10
- import { arrayStateKey, cloneRow, fallbackRowLabel, moveItem, toggleId, withRowIds } from './arrayRows.js';
10
+ import { rowLabel } from '../../../core/rowLabel.js';
11
+ import { arrayStateKey, cloneRow, moveItem, toggleId, withRowIds } from './arrayRows.js';
11
12
  import { ArrayRowContext, asStructuralChange, generatedRowId, getCollapsedRows, setCollapsedRows, useCollapsedRows } from './arrayState.js';
12
- import { rowLabel } from './values.js';
13
13
  /**
14
14
  * Payload's array field, without a Form: collapsible rows with a row label, "Collapse all" and
15
15
  * "Show all", drag to reorder, and a row menu (move, add below, duplicate, remove). Every edit
@@ -66,7 +66,7 @@ const ArrayFieldRow = memo(function ArrayFieldRow({ actions, collapsed, fields,
66
66
  const rowPath = `${path}.${index}`;
67
67
  const rowKey = `${stateKey}.${row.id}`;
68
68
  const context = useMemo(() => ({ rowPath, rowKey }), [rowPath, rowKey]);
69
- const title = rowLabel(row, fields) ?? fallbackRowLabel(itemLabel, index);
69
+ const title = rowLabel(row, fields, index, itemLabel);
70
70
  return (_jsx("div", { ref: setNodeRef, className: "builder-field-array__row", "data-dragging": isDragging || undefined, style: { transform, transition, zIndex: isDragging ? 1 : undefined }, children: _jsx(Collapsible, { actions: _jsx(RowMenu, { actions: actions, full: full, index: index, rowCount: rowCount }), className: "builder-field-array__collapsible", dragHandleProps: { id: row.id, attributes: attributes, listeners }, header: _jsx("span", { className: "builder-field-array__title", children: title }), isCollapsed: collapsed, onToggle: (next) => actions.setCollapsed(row.id, next), children: !collapsed && (_jsx(ArrayRowContext, { value: context, children: _jsx("div", { className: "builder-field-array__fields", children: _jsx(RenderBlockFields, { fields: fields, data: row, path: rowPath, onChange: (next) => actions.change(row.id, next) }) }) })) }) }));
71
71
  });
72
72
  function RowMenu({ actions, full, index, rowCount, }) {
@@ -0,0 +1,20 @@
1
+ import { type ReactNode } from 'react';
2
+ export { FIELD_NAME_MARKER } from '../../../forms/blocks.js';
3
+ /** The Form block's actions, under its mode. */
4
+ export declare function FormBlockActions({ path }: {
5
+ path: string;
6
+ }): import("react").JSX.Element | null;
7
+ /**
8
+ * Under a form field's name: renaming a field of a form with submissions keeps the old
9
+ * submissions under the old name. Compares with the name the field had when it was selected.
10
+ */
11
+ export declare function FieldNameNote({ path, value, children }: {
12
+ path: string;
13
+ value: unknown;
14
+ children: ReactNode;
15
+ }): import("react").JSX.Element;
16
+ /**
17
+ * In the form builder: a form built on a page says so, with a link to that page, because the
18
+ * page's next publish of a change to the form writes its own version over changes made here.
19
+ */
20
+ export declare function FormSourceNote(): import("react").JSX.Element | null;
@@ -0,0 +1,167 @@
1
+ 'use client';
2
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ // Forms on pages, in the inspector:
4
+ // - `FormBlockActions` (`admin.custom.builderFormActions` on the Form block): for a saved form,
5
+ // "Edit form" (the form builder) and "Move into this page" (its fields become blocks inside this
6
+ // Form block); for a form built here, where its answers go.
7
+ // - `FieldNameNote` (`admin.custom.builderFieldName` on a form field's `name`): renaming a field of
8
+ // a form that has submissions warns that the old submissions keep the old name.
9
+ import { Button, useConfig } from '@payloadcms/ui';
10
+ import { useEffect, useState } from 'react';
11
+ import { findBlock } from '../../../core/tree.js';
12
+ import { FORM_SLOT, formBlockMode, formIdOf } from '../../../forms/pageForms.js';
13
+ import { formSourceOf, movedFormLook } from '../../../forms/pageSync.js';
14
+ import { getBlockDefinition } from '../../../core/blocks.js';
15
+ import { builderViewPath } from '../../../plugin/links.js';
16
+ import { withNewIds } from '../actions.js';
17
+ import { propPathOf } from '../live/field.js';
18
+ import { useRuntime } from '../runtime.js';
19
+ import { useEditor } from '../store.js';
20
+ import { formBlockAround } from './FormFieldName.js';
21
+ export { FIELD_NAME_MARKER } from '../../../forms/blocks.js';
22
+ /** Path of the claim endpoint under the live path (plugin/formsSync.ts). */
23
+ const FORM_CLAIM_PATH = 'form-claim';
24
+ /** The number of submissions of a form, or null while it loads or when it cannot be read. */
25
+ function useSubmissionCount(runtime, formId) {
26
+ const submissions = runtime.config.forms?.submissions ?? null;
27
+ const [count, setCount] = useState(null);
28
+ useEffect(() => {
29
+ if (formId === null || !submissions)
30
+ return;
31
+ const controller = new AbortController();
32
+ const url = `${runtime.api}/${submissions}?where[form][equals]=${encodeURIComponent(String(formId))}&limit=1&depth=0`;
33
+ fetch(url, { credentials: 'include', signal: controller.signal })
34
+ .then((response) => (response.ok ? response.json() : null))
35
+ .then((body) => {
36
+ if (typeof body?.totalDocs === 'number')
37
+ setCount({ id: formId, total: body.totalDocs });
38
+ })
39
+ .catch(() => { });
40
+ return () => controller.abort();
41
+ }, [formId, submissions, runtime.api]);
42
+ return count && formId !== null && count.id === formId ? count.total : null;
43
+ }
44
+ /** "Move into this page": the saved form's fields and settings become this Form block's own. One undo step. */
45
+ async function moveIntoPage(runtime, blockId, formId) {
46
+ const { collection, id } = runtime.doc.meta.get();
47
+ let body;
48
+ try {
49
+ const response = await fetch(`${runtime.config.liveEndpoint}/${collection}/${encodeURIComponent(id)}/${FORM_CLAIM_PATH}`, {
50
+ method: 'POST',
51
+ credentials: 'include',
52
+ headers: { 'Content-Type': 'application/json' },
53
+ body: JSON.stringify({ block: blockId, form: formId }),
54
+ });
55
+ body = (await response.json());
56
+ }
57
+ catch {
58
+ runtime.warn('The form could not be moved. Check your connection and try again.');
59
+ return;
60
+ }
61
+ if (!body.ok) {
62
+ runtime.warn(body.error);
63
+ return;
64
+ }
65
+ const { layout } = runtime.store.getState();
66
+ const form = findBlock(layout, blockId);
67
+ if (!form)
68
+ return;
69
+ // The saved form's fields replace what the block holds (a block switched from "Build here" may
70
+ // still hold fields): no second set of names and Submit buttons. New ids: two blocks may move the same form in.
71
+ const children = body.children.map(withNewIds);
72
+ const unset = body.kept ? [] : ['form'];
73
+ // A block that was only a box around the saved form gets the Form block's layout and part classes.
74
+ const look = movedFormLook(form, getBlockDefinition(runtime.config.blocks, form.type));
75
+ const ops = [
76
+ ...(form.slots?.[FORM_SLOT] ?? []).map((child) => ({ type: 'remove', id: child.id })),
77
+ { type: 'update', id: blockId, props: body.props, ...(unset.length > 0 ? { unsetProps: unset } : {}), ...look },
78
+ ...children.map((block, i) => ({ type: 'insert', block, to: { parentId: blockId, slot: FORM_SLOT, index: i } })),
79
+ ];
80
+ if (!runtime.store.apply(ops, { select: blockId }))
81
+ return;
82
+ runtime.notify(body.kept
83
+ ? 'The form is now part of this page. Publish the page to save changes to it; its submissions stay.'
84
+ : 'Copied the form into this page. Other pages use the saved form, so this page gets its own form when you publish.');
85
+ }
86
+ function Note({ children }) {
87
+ return _jsx("p", { className: "builder-field-note", children: children });
88
+ }
89
+ /** The Form block's actions, under its mode. */
90
+ export function FormBlockActions({ path }) {
91
+ const runtime = useRuntime();
92
+ const { config } = useConfig();
93
+ const blockId = propPathOf(path)?.blockId ?? '';
94
+ const block = useEditor(runtime.store, (s) => findBlock(s.view, blockId));
95
+ const mode = formBlockMode(block?.props, block?.slots);
96
+ const formId = formIdOf(block?.props?.form);
97
+ const count = useSubmissionCount(runtime, formId);
98
+ const [busy, setBusy] = useState(false);
99
+ const forms = runtime.config.forms;
100
+ if (!block || !forms)
101
+ return null;
102
+ const admin = config.routes.admin === '/' ? '' : config.routes.admin;
103
+ const answers = count === null ? null : count === 0 ? 'No submissions yet.' : `${count} ${count === 1 ? 'submission' : 'submissions'} so far.`;
104
+ const submissionsLink = formId !== null && forms.submissions ? `${admin}/collections/${forms.submissions}?where[form][equals]=${encodeURIComponent(String(formId))}` : null;
105
+ if (mode === 'build') {
106
+ return (_jsxs("div", { className: "builder-form-actions", children: [_jsx(Note, { children: formId === null
107
+ ? 'Visitors can send this form once the page is published. Publishing saves it to Forms.'
108
+ : `Publishing the page saves changes to this form (Forms #${String(formId)}). ${answers ?? ''}` }), submissionsLink && count ? (_jsx(Button, { el: "link", url: submissionsLink, newTab: true, buttonStyle: "secondary", size: "small", margin: false, children: "View submissions" })) : null] }));
109
+ }
110
+ if (formId === null)
111
+ return null;
112
+ return (_jsxs("div", { className: "builder-form-actions", children: [answers ? _jsx(Note, { children: answers }) : null, _jsxs("div", { className: "builder-form-actions__row", children: [_jsx(Button, { el: "link", url: builderViewPath(config.routes.admin, forms.collection, formId), newTab: true, buttonStyle: "secondary", size: "small", margin: false, children: "Edit form" }), _jsx(Button, { buttonStyle: "secondary", size: "small", margin: false, disabled: busy, tooltip: "Its fields become blocks in this form, so you edit them here", onClick: () => {
113
+ setBusy(true);
114
+ void moveIntoPage(runtime, blockId, formId).finally(() => setBusy(false));
115
+ }, children: "Move into this page" })] })] }));
116
+ }
117
+ /**
118
+ * Under a form field's name: renaming a field of a form with submissions keeps the old
119
+ * submissions under the old name. Compares with the name the field had when it was selected.
120
+ */
121
+ export function FieldNameNote({ path, value, children }) {
122
+ const runtime = useRuntime();
123
+ const blockId = propPathOf(path)?.blockId ?? '';
124
+ // The name when the field was selected (the inspector remounts per block).
125
+ const [first] = useState(value);
126
+ const form = useEditor(runtime.store, (s) => formBlockAround(s.view, blockId));
127
+ const forms = runtime.config.forms;
128
+ // On a page: the Form block's form. In the form builder: the open form itself.
129
+ const meta = runtime.doc.meta.get();
130
+ const formId = form ? formIdOf(form.props?.form) : forms && meta.collection === forms.collection ? meta.id : null;
131
+ const count = useSubmissionCount(runtime, formId);
132
+ const renamed = typeof first === 'string' && first !== '' && value !== first;
133
+ return (_jsxs(_Fragment, { children: [children, renamed && count ? (_jsxs("p", { className: "builder-field-note builder-field-note--warning", children: ["This form has ", count, " ", count === 1 ? 'submission' : 'submissions', ". They keep the old name \u201C", String(first), "\u201D; new ones use the new name."] })) : null] }));
134
+ }
135
+ /**
136
+ * In the form builder: a form built on a page says so, with a link to that page, because the
137
+ * page's next publish of a change to the form writes its own version over changes made here.
138
+ */
139
+ export function FormSourceNote() {
140
+ const runtime = useRuntime();
141
+ const { config } = useConfig();
142
+ const forms = runtime.config.forms;
143
+ const meta = runtime.doc.meta.get();
144
+ const isForm = Boolean(forms && meta.collection === forms.collection);
145
+ const [source, setSource] = useState(null);
146
+ useEffect(() => {
147
+ if (!isForm || !forms)
148
+ return;
149
+ const controller = new AbortController();
150
+ const load = async () => {
151
+ const response = await fetch(`${runtime.api}/${forms.collection}/${encodeURIComponent(meta.id)}?depth=0`, { credentials: 'include', signal: controller.signal });
152
+ const doc = response.ok ? (await response.json()) : null;
153
+ const own = doc ? formSourceOf(doc) : null;
154
+ if (!own)
155
+ return;
156
+ const page = await fetch(`${runtime.api}/${own.collection}/${encodeURIComponent(own.document)}?depth=0&draft=true`, { credentials: 'include', signal: controller.signal });
157
+ const pageDoc = page.ok ? (await page.json()) : null;
158
+ const title = typeof pageDoc?.title === 'string' && pageDoc.title ? pageDoc.title : null;
159
+ setSource({ collection: own.collection, document: own.document, title });
160
+ };
161
+ load().catch(() => { });
162
+ return () => controller.abort();
163
+ }, [isForm, forms, meta.id, runtime.api]);
164
+ if (!source)
165
+ return null;
166
+ return (_jsxs("div", { className: "builder-form-actions builder-form-actions--source", children: [_jsxs("p", { className: "builder-field-note builder-field-note--warning", children: ["This form is built on the page ", source.title ? `“${source.title}”` : `#${source.document}`, ". Edit it there: when that page publishes a change to this form, it replaces changes made here."] }), _jsx(Button, { el: "link", url: builderViewPath(config.routes.admin, source.collection, source.document), buttonStyle: "secondary", size: "small", margin: false, children: "Open the page" })] }));
167
+ }
@@ -0,0 +1,25 @@
1
+ import type { Block, Layout } from '../../../core/types.js';
2
+ type Props = {
3
+ readonly label: string;
4
+ readonly description?: string;
5
+ readonly path: string;
6
+ readonly value: unknown;
7
+ readonly required: boolean;
8
+ readonly onChange: (value: unknown) => void;
9
+ };
10
+ /**
11
+ * The named fields of the form being edited, as options: "Topic (topic)". On a page: the fields of
12
+ * the Form block that holds `except` (or the selected block); in the form builder: the layout's
13
+ * fields. `except`: the id of a field to leave out (the field itself).
14
+ */
15
+ export declare function useFormFieldOptions(except?: string): {
16
+ label: string;
17
+ value: string;
18
+ title: string;
19
+ }[];
20
+ /** The Form block that holds `blockId` (or is it), or null: the form builder's layout is one form. */
21
+ export declare function formBlockAround(layout: Layout, blockId: string | null | undefined): Block | null;
22
+ /** The fields of the form around `blockId`: its Form block's fields, else every field of the layout. */
23
+ export declare function formFieldsAround(layout: Layout, blockId: string | null | undefined): Block[];
24
+ export declare function FormFieldNameField({ label, description, path, value, required, onChange }: Props): import("react").JSX.Element;
25
+ export {};
@@ -0,0 +1,58 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ // `admin.custom.builderFormField`: a text field that names another field of the same form (a
4
+ // display condition's "Field"). The builder shows a list of the form's named fields instead of a
5
+ // text input, so a typo cannot break the condition. The value stays the field's name.
6
+ import { SelectInput } from '@payloadcms/ui';
7
+ import { useMemo } from 'react';
8
+ import { findBlock, findLocation } from '../../../core/tree.js';
9
+ import { FORM_BLOCK, formBlockFields, formFieldsIn } from '../../../forms/pageForms.js';
10
+ import { propPathOf } from '../live/field.js';
11
+ import { useRuntime } from '../runtime.js';
12
+ import { useEditor } from '../store.js';
13
+ /**
14
+ * The named fields of the form being edited, as options: "Topic (topic)". On a page: the fields of
15
+ * the Form block that holds `except` (or the selected block); in the form builder: the layout's
16
+ * fields. `except`: the id of a field to leave out (the field itself).
17
+ */
18
+ export function useFormFieldOptions(except) {
19
+ const runtime = useRuntime();
20
+ const layout = useEditor(runtime.store, (s) => s.view);
21
+ const selectedId = useEditor(runtime.store, (s) => s.selectedId);
22
+ return useMemo(() => {
23
+ const out = [];
24
+ for (const block of formFieldsAround(layout, except ?? selectedId)) {
25
+ const name = block.props?.name;
26
+ if (block.id === except || typeof name !== 'string' || !name.trim())
27
+ continue;
28
+ if (out.some((o) => o.value === name))
29
+ continue;
30
+ const text = typeof block.props?.label === 'string' && block.props.label.trim() ? block.props.label.trim() : name;
31
+ out.push({ label: text === name ? name : `${text} (${name})`, value: name, title: text });
32
+ }
33
+ return out;
34
+ }, [layout, except, selectedId]);
35
+ }
36
+ /** The Form block that holds `blockId` (or is it), or null: the form builder's layout is one form. */
37
+ export function formBlockAround(layout, blockId) {
38
+ if (!blockId)
39
+ return null;
40
+ for (let id = blockId; id; id = findLocation(layout, id)?.parentId ?? null) {
41
+ const block = findBlock(layout, id);
42
+ if (block?.type === FORM_BLOCK)
43
+ return block;
44
+ }
45
+ return null;
46
+ }
47
+ /** The fields of the form around `blockId`: its Form block's fields, else every field of the layout. */
48
+ export function formFieldsAround(layout, blockId) {
49
+ const form = formBlockAround(layout, blockId);
50
+ return form ? formBlockFields(form) : formFieldsIn(layout.blocks);
51
+ }
52
+ export function FormFieldNameField({ label, description, path, value, required, onChange }) {
53
+ const fields = useFormFieldOptions(propPathOf(path)?.blockId);
54
+ const current = typeof value === 'string' ? value : '';
55
+ // A name no field has any more stays visible, so the editor sees what to fix.
56
+ const options = current && !fields.some((o) => o.value === current) ? [...fields, { label: `${current} (not in this form)`, value: current, title: current }] : fields;
57
+ return (_jsx(SelectInput, { description: description, isClearable: !required, label: label, name: path, onChange: (option) => onChange(Array.isArray(option) ? (option[0]?.value ?? null) : (option?.value ?? null)), options: options, path: path, required: required, value: current || undefined }));
58
+ }
@@ -1,9 +1,11 @@
1
- import type { ReactNode } from 'react';
1
+ import { type ReactNode } from 'react';
2
2
  import type { Field } from 'payload';
3
3
  type Props = {
4
4
  readonly fields: Field[];
5
5
  readonly label: string;
6
6
  readonly description?: string;
7
+ /** No heading and no indent: a section around the group already names it. */
8
+ readonly plain?: boolean;
7
9
  readonly path: string;
8
10
  readonly value: unknown;
9
11
  /** The whole group object, or `undefined` when every sub-field is empty. */
@@ -21,5 +23,23 @@ export declare function FieldSection({ label, description, collapsed, children,
21
23
  readonly children: ReactNode;
22
24
  }): import("react").JSX.Element;
23
25
  /** A named group: a small heading over its sub-fields, which edit one nested object. */
24
- export declare function GroupField({ fields, label, description, path, value, onChange }: Props): import("react").JSX.Element;
26
+ export declare function GroupField({ fields, label, description, plain, path, value, onChange }: Props): import("react").JSX.Element;
27
+ /**
28
+ * A Payload `tabs` field with several tabs: a tab strip, then the fields of the chosen tab.
29
+ * `render` gives the content of one tab (by index). The choice stays while the editor is open.
30
+ */
31
+ export declare function FieldTabs({ labels, path, render }: {
32
+ readonly labels: string[];
33
+ readonly path: string;
34
+ readonly render: (index: number) => ReactNode;
35
+ }): import("react").JSX.Element;
36
+ /**
37
+ * A section of a settings panel (form settings): a heading that opens and closes it, over its
38
+ * fields. `open`: open at first.
39
+ */
40
+ export declare function PanelSection({ label, open, children }: {
41
+ readonly label: string;
42
+ readonly open?: boolean;
43
+ readonly children: ReactNode;
44
+ }): import("react").JSX.Element;
25
45
  export {};
@@ -1,6 +1,9 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useState } from 'react';
4
+ import { Icon } from '../icons.js';
3
5
  import { RenderBlockFields } from '../renderField.js';
6
+ import { Tabs } from '../ui/Tabs.js';
4
7
  /**
5
8
  * Fields that share the parent's data, under a small heading: a collapsible, or one of several
6
9
  * unnamed tabs. Looks like a group, but stores nothing of its own. `collapsed`: a collapsible with
@@ -13,6 +16,32 @@ export function FieldSection({ label, description, collapsed, children, }) {
13
16
  return (_jsxs("fieldset", { className: "builder-field-group", children: [_jsx("legend", { className: "builder-field-group__label", children: label }), _jsx("div", { className: "builder-field-group__fields", children: children })] }));
14
17
  }
15
18
  /** A named group: a small heading over its sub-fields, which edit one nested object. */
16
- export function GroupField({ fields, label, description, path, value, onChange }) {
19
+ export function GroupField({ fields, label, description, plain, path, value, onChange }) {
20
+ if (plain) {
21
+ return (_jsxs("div", { className: "builder-field-group builder-field-group--plain", children: [description && _jsx("p", { className: "builder-field-group__description", children: description }), _jsx("div", { className: "builder-field-group__fields", children: _jsx(RenderBlockFields, { fields: fields, data: value, path: path, onChange: onChange }) })] }));
22
+ }
17
23
  return (_jsxs("fieldset", { className: "builder-field-group", children: [_jsx("legend", { className: "builder-field-group__label", children: label }), description && _jsx("p", { className: "builder-field-group__description", children: description }), _jsx("div", { className: "builder-field-group__fields", children: _jsx(RenderBlockFields, { fields: fields, data: value, path: path, onChange: onChange }) })] }));
18
24
  }
25
+ /** The tab each set of tabs showed last, by its tab labels: moving to another field keeps the tab. */
26
+ const chosenTabs = new Map();
27
+ /**
28
+ * A Payload `tabs` field with several tabs: a tab strip, then the fields of the chosen tab.
29
+ * `render` gives the content of one tab (by index). The choice stays while the editor is open.
30
+ */
31
+ export function FieldTabs({ labels, path, render }) {
32
+ const key = labels.join('\u0000');
33
+ const [chosen, setChosen] = useState(() => chosenTabs.get(key) ?? labels[0]);
34
+ const index = Math.max(0, labels.indexOf(chosen));
35
+ const panelId = (label) => `${path.replace(/\W/g, '_')}-tab-${labels.indexOf(label)}`;
36
+ return (_jsxs("div", { className: "builder-field-tabs", children: [_jsx(Tabs, { variant: "segmented", value: labels[index], options: labels.map((label) => ({ id: label, label })), panelId: panelId, onChange: (label) => {
37
+ chosenTabs.set(key, label);
38
+ setChosen(label);
39
+ } }), _jsx("div", { className: "builder-field-tabs__panel", id: panelId(labels[index]), role: "tabpanel", "aria-label": labels[index], children: render(index) })] }));
40
+ }
41
+ /**
42
+ * A section of a settings panel (form settings): a heading that opens and closes it, over its
43
+ * fields. `open`: open at first.
44
+ */
45
+ export function PanelSection({ label, open = false, children }) {
46
+ return (_jsxs("details", { className: "builder-panel-section", open: open, children: [_jsxs("summary", { className: "builder-panel-section__head", children: [_jsx("span", { children: label }), _jsx(Icon, { name: "chevronDown", size: 14, className: "builder-panel-section__chevron" })] }), _jsx("div", { className: "builder-panel-section__fields", children: children })] }));
47
+ }
@@ -0,0 +1,8 @@
1
+ import { type ReactNode } from 'react';
2
+ type Props = {
3
+ readonly value: unknown;
4
+ readonly onChange: (value: unknown) => void;
5
+ readonly children: ReactNode;
6
+ };
7
+ export declare function MergeTagsFrame({ value, onChange, children }: Props): import("react").JSX.Element;
8
+ export {};
@@ -0,0 +1,62 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ // `admin.custom.builderMergeTags`: an "Insert field" menu over a text, code or rich text input of
4
+ // a form's settings (an email's To, Subject, Body). It lists the form's fields as merge tags
5
+ // (`{{email}}`) and `{{*}}` for every answer, and inserts the tag at the cursor. Text inputs get
6
+ // the new value through `onChange`; rich text gets typed text, so Lexical handles it as typing.
7
+ import { useEffect, useRef } from 'react';
8
+ import { ALL_ANSWERS_TAG, mergeTag } from '../../../forms/mergeTags.js';
9
+ import { Icon } from '../icons.js';
10
+ import { MenuButton } from '../menu/Menu.js';
11
+ import { useFormFieldOptions } from './FormFieldName.js';
12
+ const TEXT_INPUT = 'textarea, input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"])';
13
+ export function MergeTagsFrame({ value, onChange, children }) {
14
+ const fields = useFormFieldOptions();
15
+ const root = useRef(null);
16
+ // The last cursor in the rich text editor: opening the menu moves the focus away from it.
17
+ const range = useRef(null);
18
+ useEffect(() => {
19
+ const remember = () => {
20
+ const selection = document.getSelection();
21
+ const editable = root.current?.querySelector('[contenteditable="true"]');
22
+ if (editable && selection && selection.rangeCount > 0 && editable.contains(selection.anchorNode))
23
+ range.current = selection.getRangeAt(0).cloneRange();
24
+ };
25
+ document.addEventListener('selectionchange', remember);
26
+ return () => document.removeEventListener('selectionchange', remember);
27
+ }, []);
28
+ const insert = (tag) => {
29
+ const editable = root.current?.querySelector('[contenteditable="true"]');
30
+ if (editable) {
31
+ editable.focus();
32
+ const selection = document.getSelection();
33
+ if (selection && range.current && editable.contains(range.current.startContainer)) {
34
+ selection.removeAllRanges();
35
+ selection.addRange(range.current);
36
+ }
37
+ // A typed text: the rich text editor keeps its own state and undo.
38
+ // oxlint-disable-next-line typescript/no-deprecated -- the one way to type into a contenteditable as the user would
39
+ document.execCommand('insertText', false, tag);
40
+ return;
41
+ }
42
+ const input = root.current?.querySelector(TEXT_INPUT);
43
+ const text = typeof value === 'string' ? value : '';
44
+ const start = input?.selectionStart ?? text.length;
45
+ const end = input?.selectionEnd ?? start;
46
+ onChange(`${text.slice(0, start)}${tag}${text.slice(end)}`);
47
+ // The cursor goes after the tag once the new value renders.
48
+ requestAnimationFrame(() => {
49
+ if (!input)
50
+ return;
51
+ input.focus();
52
+ input.setSelectionRange(start + tag.length, start + tag.length);
53
+ });
54
+ };
55
+ return (_jsxs("div", { ref: root, className: "builder-merge-tags", children: [_jsxs(MenuButton, { className: "builder-merge-tags__button", triggerLabel: "Insert a field of the form", label: "Insert field", items: () => [
56
+ ...(fields.length > 0
57
+ ? fields.map((field) => ({ label: field.title, keys: mergeTag(field.value), ownFocus: true, run: () => insert(mergeTag(field.value)) }))
58
+ : [{ label: 'The form has no fields yet', disabled: true }]),
59
+ 'separator',
60
+ { label: 'All answers, as a table', keys: ALL_ANSWERS_TAG, ownFocus: true, run: () => insert(ALL_ANSWERS_TAG) },
61
+ ], children: [_jsx(Icon, { name: "plus", size: 12 }), "Insert field"] }), children] }));
62
+ }
@@ -24,5 +24,3 @@ export type ParentRow = {
24
24
  export declare function arrayStateKey(path: string, parent: ParentRow | null): string;
25
25
  /** A deep copy of a row with a new id, and new ids for the rows of its nested array fields. */
26
26
  export declare function cloneRow(row: ArrayRow, fields: readonly unknown[], newId: () => string): ArrayRow;
27
- /** "Item 01": Payload's fallback row label, from the singular label and the row number. */
28
- export declare function fallbackRowLabel(singular: string, index: number): string;
@@ -85,7 +85,3 @@ export function cloneRow(row, fields, newId) {
85
85
  renewNestedIds(copy, fields, newId);
86
86
  return { ...copy, id: newId() };
87
87
  }
88
- /** "Item 01": Payload's fallback row label, from the singular label and the row number. */
89
- export function fallbackRowLabel(singular, index) {
90
- return `${singular} ${String(index + 1).padStart(2, '0')}`;
91
- }