payload-canvas 0.5.1 → 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.
- package/README.md +333 -28
- package/dist/admin/ArrayRowLabel.d.ts +12 -0
- package/dist/admin/ArrayRowLabel.js +21 -0
- package/dist/admin/DocumentEditView.js +9 -2
- package/dist/admin/client.d.ts +1 -0
- package/dist/admin/client.js +2 -0
- package/dist/admin/editor/Editor.js +8 -1
- package/dist/admin/editor/Inspector.js +23 -17
- package/dist/admin/editor/Library.d.ts +2 -2
- package/dist/admin/editor/Library.js +7 -5
- package/dist/admin/editor/Outline.js +20 -12
- package/dist/admin/editor/Overlay.js +6 -4
- package/dist/admin/editor/actions.d.ts +7 -2
- package/dist/admin/editor/actions.js +44 -12
- package/dist/admin/editor/assistant/AssistantPanel.js +32 -20
- package/dist/admin/editor/comments/controller.d.ts +7 -0
- package/dist/admin/editor/comments/controller.js +12 -0
- package/dist/admin/editor/comments/useCommentFromURL.d.ts +2 -0
- package/dist/admin/editor/comments/useCommentFromURL.js +36 -0
- package/dist/admin/editor/dnd/smooth.js +1 -1
- package/dist/admin/editor/editor.scss +28 -0
- package/dist/admin/editor/fields/ArrayField.js +3 -3
- package/dist/admin/editor/fields/FormBlock.d.ts +20 -0
- package/dist/admin/editor/fields/FormBlock.js +167 -0
- package/dist/admin/editor/fields/FormFieldName.d.ts +25 -0
- package/dist/admin/editor/fields/FormFieldName.js +58 -0
- package/dist/admin/editor/fields/GroupField.d.ts +22 -2
- package/dist/admin/editor/fields/GroupField.js +30 -1
- package/dist/admin/editor/fields/MergeTags.d.ts +8 -0
- package/dist/admin/editor/fields/MergeTags.js +62 -0
- package/dist/admin/editor/fields/arrayRows.d.ts +0 -2
- package/dist/admin/editor/fields/arrayRows.js +0 -4
- package/dist/admin/editor/fields/fields.scss +150 -0
- package/dist/admin/editor/fields/values.d.ts +0 -2
- package/dist/admin/editor/fields/values.js +0 -14
- package/dist/admin/editor/icons.d.ts +3 -0
- package/dist/admin/editor/icons.js +3 -0
- package/dist/admin/editor/insert/InsertHandle.js +1 -1
- package/dist/admin/editor/insert/picker.d.ts +3 -2
- package/dist/admin/editor/insert/picker.js +9 -3
- package/dist/admin/editor/layout/LeftPanel.js +5 -3
- package/dist/admin/editor/layout/leftTabs.d.ts +15 -0
- package/dist/admin/editor/layout/leftTabs.js +18 -0
- package/dist/admin/editor/media/ImageEditor.d.ts +8 -2
- package/dist/admin/editor/media/ImageEditor.js +16 -10
- package/dist/admin/editor/media/media.scss +12 -2
- package/dist/admin/editor/media/model.d.ts +10 -1
- package/dist/admin/editor/media/model.js +19 -0
- package/dist/admin/editor/menu/blockMenu.js +3 -3
- package/dist/admin/editor/names.d.ts +9 -0
- package/dist/admin/editor/names.js +18 -0
- package/dist/admin/editor/regions/RegionFields.d.ts +6 -0
- package/dist/admin/editor/regions/RegionFields.js +122 -0
- package/dist/admin/editor/regions/RegionPane.js +5 -3
- package/dist/admin/editor/regions/RegionRows.js +2 -2
- package/dist/admin/editor/regions/required.d.ts +16 -0
- package/dist/admin/editor/regions/required.js +43 -0
- package/dist/admin/editor/renderField.d.ts +9 -2
- package/dist/admin/editor/renderField.js +86 -32
- package/dist/admin/editor/runtime.d.ts +3 -2
- package/dist/admin/editor/runtime.js +12 -18
- package/dist/admin/editor/styles/StylesPanel.js +4 -2
- package/dist/admin/editor/styles/context.js +9 -4
- package/dist/admin/editor/topbar/ContentModeToggle.js +1 -1
- package/dist/admin/editor/topbar/DocumentActions.js +4 -3
- package/dist/admin/editor/topbar/contentMode.scss +0 -9
- package/dist/admin/editor/topbar/topbar.scss +1 -41
- package/dist/admin/editor/ui/Tabs.d.ts +17 -0
- package/dist/admin/editor/ui/Tabs.js +17 -0
- package/dist/admin/forms/SubmissionCells.d.ts +6 -0
- package/dist/admin/forms/SubmissionCells.js +31 -0
- package/dist/admin/forms/SubmissionView.d.ts +5 -0
- package/dist/admin/forms/SubmissionView.js +106 -0
- package/dist/admin/forms/submissionData.d.ts +32 -0
- package/dist/admin/forms/submissionData.js +63 -0
- package/dist/admin/forms/submissions.scss +197 -0
- package/dist/admin/rsc.d.ts +2 -0
- package/dist/admin/rsc.js +5 -0
- package/dist/ai/endpoint.d.ts +5 -0
- package/dist/ai/endpoint.js +18 -8
- package/dist/ai/prompt.d.ts +8 -0
- package/dist/ai/prompt.js +30 -0
- package/dist/ai/tools.d.ts +6 -1
- package/dist/ai/tools.js +90 -6
- package/dist/blocks/builtins/carousel.js +10 -1
- package/dist/blocks/index.d.ts +1 -1
- package/dist/blocks/index.js +1 -1
- package/dist/blocks/payload.d.ts +7 -0
- package/dist/blocks/payload.js +16 -2
- package/dist/comments/shared.d.ts +5 -0
- package/dist/comments/shared.js +5 -0
- package/dist/core/blocks.d.ts +17 -3
- package/dist/core/blocks.js +92 -10
- package/dist/core/classes.d.ts +5 -0
- package/dist/core/classes.js +26 -1
- package/dist/core/conditions.d.ts +7 -0
- package/dist/core/conditions.js +22 -0
- package/dist/core/dropTarget.d.ts +3 -2
- package/dist/core/dropTarget.js +7 -1
- package/dist/core/index.d.ts +6 -3
- package/dist/core/index.js +6 -3
- package/dist/core/payloadBlocks.js +25 -3
- package/dist/core/references.d.ts +7 -0
- package/dist/core/references.js +29 -11
- package/dist/core/regions.d.ts +14 -0
- package/dist/core/regions.js +17 -1
- package/dist/core/rowLabel.d.ts +8 -0
- package/dist/core/rowLabel.js +98 -0
- package/dist/core/starter.d.ts +10 -0
- package/dist/core/starter.js +40 -0
- package/dist/core/tree.js +4 -0
- package/dist/core/types.d.ts +96 -0
- package/dist/core/uniqueProps.d.ts +40 -0
- package/dist/core/uniqueProps.js +156 -0
- package/dist/core/validate.js +43 -3
- package/dist/forms/answers.d.ts +21 -0
- package/dist/forms/answers.js +82 -0
- package/dist/forms/blocks.d.ts +33 -0
- package/dist/forms/blocks.js +178 -0
- package/dist/forms/conditions.d.ts +78 -0
- package/dist/forms/conditions.js +184 -0
- package/dist/forms/emails.d.ts +23 -0
- package/dist/forms/emails.js +64 -0
- package/dist/forms/fieldConfigs.d.ts +35 -0
- package/dist/forms/fieldConfigs.js +167 -0
- package/dist/forms/fieldOps.d.ts +78 -0
- package/dist/forms/fieldOps.js +378 -0
- package/dist/forms/fieldSettings.d.ts +81 -0
- package/dist/forms/fieldSettings.js +239 -0
- package/dist/forms/files.d.ts +54 -0
- package/dist/forms/files.js +128 -0
- package/dist/forms/index.d.ts +16 -0
- package/dist/forms/index.js +18 -0
- package/dist/forms/mergeTags.d.ts +103 -0
- package/dist/forms/mergeTags.js +150 -0
- package/dist/forms/noScript.d.ts +60 -0
- package/dist/forms/noScript.js +109 -0
- package/dist/forms/pageForms.d.ts +99 -0
- package/dist/forms/pageForms.js +294 -0
- package/dist/forms/pageSync.d.ts +91 -0
- package/dist/forms/pageSync.js +325 -0
- package/dist/forms/rateLimit.d.ts +21 -0
- package/dist/forms/rateLimit.js +50 -0
- package/dist/forms/settings.d.ts +13 -0
- package/dist/forms/settings.js +92 -0
- package/dist/forms/spam.d.ts +100 -0
- package/dist/forms/spam.js +218 -0
- package/dist/forms/spamSettings.d.ts +43 -0
- package/dist/forms/spamSettings.js +25 -0
- package/dist/forms/submission.d.ts +34 -0
- package/dist/forms/submission.js +89 -0
- package/dist/forms/submissionsAdmin.d.ts +26 -0
- package/dist/forms/submissionsAdmin.js +108 -0
- package/dist/forms/summary.d.ts +60 -0
- package/dist/forms/summary.js +113 -0
- package/dist/forms/uploads.d.ts +41 -0
- package/dist/forms/uploads.js +132 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.js +8 -2
- package/dist/live/document.d.ts +12 -0
- package/dist/live/document.js +8 -1
- package/dist/live/endpoints.d.ts +1 -0
- package/dist/live/session.js +3 -1
- package/dist/live/storage.d.ts +13 -2
- package/dist/live/storage.js +18 -7
- package/dist/mcp/forms.d.ts +26 -0
- package/dist/mcp/forms.js +280 -0
- package/dist/mcp/index.d.ts +7 -0
- package/dist/mcp/index.js +29 -10
- package/dist/mcp/shared.d.ts +1 -0
- package/dist/mcp/shared.js +1 -0
- package/dist/plugin/forms.d.ts +30 -0
- package/dist/plugin/forms.js +63 -0
- package/dist/plugin/formsSync.d.ts +138 -0
- package/dist/plugin/formsSync.js +345 -0
- package/dist/plugin/hook.d.ts +22 -0
- package/dist/plugin/hook.js +19 -7
- package/dist/plugin/index.d.ts +14 -0
- package/dist/plugin/index.js +99 -28
- package/dist/plugin/links.d.ts +21 -2
- package/dist/plugin/links.js +32 -3
- package/dist/plugin/references.d.ts +6 -0
- package/dist/plugin/references.js +12 -0
- package/dist/react/canvas/BuilderCanvas.d.ts +2 -0
- package/dist/react/canvas/BuilderCanvas.js +29 -6
- package/dist/react/canvas/inline/mapper.d.ts +6 -1
- package/dist/react/canvas/inline/mapper.js +35 -4
- package/dist/react/render/LayoutOutlet.d.ts +7 -3
- package/dist/react/render/LayoutOutlet.js +9 -4
- package/dist/react/render/RenderLayout.js +13 -7
- package/dist/react/render/resolve.d.ts +1 -0
- package/dist/react/render/resolve.js +5 -1
- package/dist/react/server.js +19 -0
- package/package.json +5 -1
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Forms on pages: the Form block and the Submit button block. A Form block either holds its own
|
|
2
|
+
// fields ("Build here": the form field blocks, a Submit button and layout blocks in its slot) or
|
|
3
|
+
// shows a form of the form-builder plugin's forms collection ("Use a saved form").
|
|
4
|
+
//
|
|
5
|
+
// `formBlocks()` gives the definitions an app lists in its blocks. They are client-safe and light:
|
|
6
|
+
// the plugin (`websiteBuilder({ forms })`) completes the Form block with the forms collection's
|
|
7
|
+
// settings fields and adds the field blocks (`formText`, …) made from the collection's own field
|
|
8
|
+
// blocks, so there is one definition of each field and of each setting. Pure: no Payload runtime.
|
|
9
|
+
import { getBlockDefinition } from '../core/blocks.js';
|
|
10
|
+
import { FORM_FIELD_CATEGORY, FORM_FIELD_CLASSES, formFieldParts, formFieldSlug, formFieldType } from './blocks.js';
|
|
11
|
+
import { FORM_FIELD_WIDTH_CLASSES } from './fieldSettings.js';
|
|
12
|
+
export { FORM_FIELD_PARTS, formFieldParts } from './blocks.js';
|
|
13
|
+
/** The Form block's type. */
|
|
14
|
+
export const FORM_BLOCK = 'form';
|
|
15
|
+
/** The Submit button block's type. */
|
|
16
|
+
export const FORM_SUBMIT_BLOCK = 'formSubmit';
|
|
17
|
+
/** The Form block's slot for its content. */
|
|
18
|
+
export const FORM_SLOT = 'children';
|
|
19
|
+
/** `admin.custom.builderFormActions`: the inspector shows "Edit form" and "Move into this page" here. */
|
|
20
|
+
export const FORM_ACTIONS_MARKER = 'builderFormActions';
|
|
21
|
+
/** `admin.custom` key of a collapsible the inspector draws as a settings panel section, like the form builder's. */
|
|
22
|
+
export const PANEL_SECTION_MARKER = 'builderPanelSection';
|
|
23
|
+
/** `admin.custom` key of a group drawn without its own heading: the section around it already names it. */
|
|
24
|
+
export const PLAIN_GROUP_MARKER = 'builderPlainGroup';
|
|
25
|
+
/** The classes of a field that takes half a row of a wrapping form (the Form block's default layout). */
|
|
26
|
+
const HALF_ROW = FORM_FIELD_WIDTH_CLASSES[50];
|
|
27
|
+
/** Layout blocks a Form block takes next to its fields, when the site has them. */
|
|
28
|
+
export const FORM_LAYOUT_TYPES = ['stack', 'grid', 'heading', 'text', 'richText', 'divider', 'image'];
|
|
29
|
+
/** The form-wide parts: every field inside uses these classes unless it sets its own. */
|
|
30
|
+
export const FORM_PARTS = {
|
|
31
|
+
label: 'Labels',
|
|
32
|
+
input: 'Inputs',
|
|
33
|
+
help: 'Help texts',
|
|
34
|
+
error: 'Error messages',
|
|
35
|
+
button: 'Submit button',
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* The mode of a Form block: its `mode`; else "build" when it holds blocks (a Form block that MCP or
|
|
39
|
+
* the assistant inserted without a mode, also after the publish gave it its form id); else "saved"
|
|
40
|
+
* when it names a form (layouts from before the modes); else "build".
|
|
41
|
+
*/
|
|
42
|
+
export function formBlockMode(props, slots) {
|
|
43
|
+
if (props?.mode === 'build' || props?.mode === 'saved')
|
|
44
|
+
return props.mode;
|
|
45
|
+
if ((slots?.[FORM_SLOT]?.length ?? 0) > 0)
|
|
46
|
+
return 'build';
|
|
47
|
+
return props?.form !== undefined && props.form !== null && props.form !== '' ? 'saved' : 'build';
|
|
48
|
+
}
|
|
49
|
+
/** The id of the form a Form block names (stored as an id, or a loaded document). Null without one. */
|
|
50
|
+
export function formIdOf(value) {
|
|
51
|
+
const id = value && typeof value === 'object' ? value.id : value;
|
|
52
|
+
return typeof id === 'string' || typeof id === 'number' ? id : null;
|
|
53
|
+
}
|
|
54
|
+
/** True for the builder type of a form field block (`formText`), never the Form or Submit block. */
|
|
55
|
+
export const isFormFieldType = (type) => type !== FORM_SUBMIT_BLOCK && formFieldSlug(type) !== null;
|
|
56
|
+
/**
|
|
57
|
+
* The form field blocks in `blocks`, in tree order: inside layout blocks too, never inside another
|
|
58
|
+
* Form block (its fields belong to that form).
|
|
59
|
+
*/
|
|
60
|
+
export function formFieldsIn(blocks) {
|
|
61
|
+
const out = [];
|
|
62
|
+
const visit = (list) => {
|
|
63
|
+
for (const block of list) {
|
|
64
|
+
if (isFormFieldType(block.type))
|
|
65
|
+
out.push(block);
|
|
66
|
+
if (block.type === FORM_BLOCK)
|
|
67
|
+
continue;
|
|
68
|
+
for (const children of Object.values(block.slots ?? {}))
|
|
69
|
+
visit(children);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
visit(blocks);
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
/** The field blocks of a Form block, in tree order. */
|
|
76
|
+
export function formBlockFields(form) {
|
|
77
|
+
return formFieldsIn(form.slots?.[FORM_SLOT] ?? []);
|
|
78
|
+
}
|
|
79
|
+
/** The Submit button blocks of a Form block, in tree order. */
|
|
80
|
+
export function formSubmitBlocks(form) {
|
|
81
|
+
const out = [];
|
|
82
|
+
const visit = (list) => {
|
|
83
|
+
for (const block of list) {
|
|
84
|
+
if (block.type === FORM_SUBMIT_BLOCK)
|
|
85
|
+
out.push(block);
|
|
86
|
+
if (block.type !== FORM_BLOCK)
|
|
87
|
+
for (const children of Object.values(block.slots ?? {}))
|
|
88
|
+
visit(children);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
visit(form.slots?.[FORM_SLOT] ?? []);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/** The new Form block's content: a ready contact form. */
|
|
95
|
+
const STARTER_CHILDREN = [
|
|
96
|
+
{ type: formFieldType('text'), props: { name: 'name', label: 'Name', required: true }, className: HALF_ROW },
|
|
97
|
+
{ type: formFieldType('email'), props: { name: 'email', label: 'Email', required: true }, className: HALF_ROW },
|
|
98
|
+
{ type: formFieldType('textarea'), props: { name: 'message', label: 'Message', required: true } },
|
|
99
|
+
{ type: FORM_SUBMIT_BLOCK, props: { label: 'Send message' } },
|
|
100
|
+
];
|
|
101
|
+
const FORM_AI = [
|
|
102
|
+
'A form visitors fill in and send; answers are stored as form submissions.',
|
|
103
|
+
'Two modes (`mode`): "build" (default for a new form): the form\'s own fields are its children, in slot "children";',
|
|
104
|
+
'"saved": shows the form in `form` (an id of the forms collection) as it is, and children are not used.',
|
|
105
|
+
'In "build" mode put form field blocks (formText, formEmail, formTextarea, formSelect, formRadio, formCheckbox, …), one formSubmit block,',
|
|
106
|
+
'and layout blocks (stack, grid, heading, text, richText, divider, image) in "children"; fields may sit inside a stack or grid of the form.',
|
|
107
|
+
'Every field needs a `name` (letters, digits, "_"), unique in this form, and usually a `label`.',
|
|
108
|
+
`Fields have no width setting: set it with classes. The Form block's own classes lay out its children (e.g. "flex flex-wrap gap-x-4 gap-y-5"); a field with className "${HALF_ROW}" takes half a row from the sm breakpoint (full width without a width class). Or put fields in a grid block.`,
|
|
109
|
+
'Form field blocks and formSubmit go only inside a Form block. Settings (title, submitButtonLabel, confirmationType, confirmationMessage, redirect, emails, spamProtection) are props of the Form block.',
|
|
110
|
+
'The form is saved to the forms collection when the page is published; do not set `form` in "build" mode.',
|
|
111
|
+
].join(' ');
|
|
112
|
+
/**
|
|
113
|
+
* The Form block and the Submit button block, for the app's block list. The plugin completes the
|
|
114
|
+
* Form block (settings, the slot's block types) and adds the field blocks, when `websiteBuilder({ forms })`
|
|
115
|
+
* is on and the list has a Form block.
|
|
116
|
+
*/
|
|
117
|
+
export function formBlocks(options = {}) {
|
|
118
|
+
const classes = options.partClassNames ?? {};
|
|
119
|
+
const parts = Object.fromEntries(Object.keys(FORM_PARTS).map((name) => [name, { label: FORM_PARTS[name], ...(classes[name] ? { defaultClassName: classes[name] } : {}) }]));
|
|
120
|
+
const form = {
|
|
121
|
+
type: FORM_BLOCK,
|
|
122
|
+
label: 'Form',
|
|
123
|
+
icon: 'form',
|
|
124
|
+
category: 'Interactive',
|
|
125
|
+
description: 'A form visitors fill in and send. Build its fields right here, or show a saved form.',
|
|
126
|
+
fields: formModeFields(options.collection ?? 'forms'),
|
|
127
|
+
slots: { [FORM_SLOT]: { label: 'Fields', disallow: [FORM_BLOCK], description: 'Form fields, the submit button and layout blocks go here.' } },
|
|
128
|
+
uniqueScope: true,
|
|
129
|
+
parts,
|
|
130
|
+
// A saved form's fields on the canvas: their stored widths draw with these (see FORM_FIELD_CLASSES).
|
|
131
|
+
classes: FORM_FIELD_CLASSES,
|
|
132
|
+
...(options.defaultClassName ? { defaultClassName: options.defaultClassName } : {}),
|
|
133
|
+
starter: { props: { mode: 'build' }, slots: { [FORM_SLOT]: STARTER_CHILDREN } },
|
|
134
|
+
ai: {
|
|
135
|
+
description: FORM_AI,
|
|
136
|
+
example: {
|
|
137
|
+
type: FORM_BLOCK,
|
|
138
|
+
props: { mode: 'build', title: 'Contact', submitButtonLabel: 'Send' },
|
|
139
|
+
className: 'flex flex-wrap gap-x-4 gap-y-5',
|
|
140
|
+
slots: {
|
|
141
|
+
[FORM_SLOT]: [
|
|
142
|
+
{ id: 'f1', type: formFieldType('text'), props: { name: 'name', label: 'Name', required: true }, className: HALF_ROW },
|
|
143
|
+
{ id: 'f2', type: formFieldType('email'), props: { name: 'email', label: 'Email', required: true }, className: HALF_ROW },
|
|
144
|
+
{ id: 'f3', type: FORM_SUBMIT_BLOCK, props: { label: 'Send' } },
|
|
145
|
+
],
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
const submit = {
|
|
151
|
+
type: FORM_SUBMIT_BLOCK,
|
|
152
|
+
label: 'Submit button',
|
|
153
|
+
icon: 'button',
|
|
154
|
+
category: FORM_FIELD_CATEGORY,
|
|
155
|
+
description: 'The button that sends the form. Goes inside a Form block.',
|
|
156
|
+
ancestors: [FORM_BLOCK],
|
|
157
|
+
// The block is the row around the button (the check and a refused submission's problems show there too).
|
|
158
|
+
parts: { button: { label: 'Button' } },
|
|
159
|
+
fields: [
|
|
160
|
+
{
|
|
161
|
+
name: 'label',
|
|
162
|
+
type: 'text',
|
|
163
|
+
label: 'Label',
|
|
164
|
+
localized: true,
|
|
165
|
+
admin: { placeholder: 'The form’s submit button label', description: 'Empty: the label in the form’s settings.' },
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
ai: { description: 'The send button of the Form block it sits in. `label` is its text. Its classes style the button; empty classes use the form\'s button classes.' },
|
|
169
|
+
};
|
|
170
|
+
return [form, submit];
|
|
171
|
+
}
|
|
172
|
+
/** The Form block's own fields: the mode, the saved form, and the actions. */
|
|
173
|
+
function formModeFields(collection) {
|
|
174
|
+
return [
|
|
175
|
+
{
|
|
176
|
+
name: 'mode',
|
|
177
|
+
type: 'radio',
|
|
178
|
+
label: 'Form fields',
|
|
179
|
+
options: [
|
|
180
|
+
{ label: 'Build here', value: 'build' },
|
|
181
|
+
{ label: 'Use a saved form', value: 'saved' },
|
|
182
|
+
],
|
|
183
|
+
admin: {
|
|
184
|
+
layout: 'horizontal',
|
|
185
|
+
description: 'Build here: the fields are blocks inside this form. Use a saved form: show a form from the Forms collection.',
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: 'form',
|
|
190
|
+
type: 'relationship',
|
|
191
|
+
relationTo: collection,
|
|
192
|
+
label: 'Saved form',
|
|
193
|
+
admin: {
|
|
194
|
+
description: 'The form to show. Its fields are edited in the form builder.',
|
|
195
|
+
// Layouts from before the modes have no `mode`: they show a saved form.
|
|
196
|
+
custom: { builderCondition: { field: 'mode', notEquals: 'build' } },
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
{ name: 'formActions', type: 'ui', admin: { custom: { [FORM_ACTIONS_MARKER]: true } } },
|
|
200
|
+
];
|
|
201
|
+
}
|
|
202
|
+
/** The settings sections of a form (the form builder's panel and the Form block), with the forms collection's own setting fields. */
|
|
203
|
+
export const FORM_SETTINGS_SECTIONS = [
|
|
204
|
+
{ label: 'General', fields: ['title', 'submitButtonLabel'] },
|
|
205
|
+
{ label: 'After submit', fields: ['confirmationType', 'confirmationMessage', 'redirect'] },
|
|
206
|
+
{ label: 'Mail', fields: ['emails'] },
|
|
207
|
+
];
|
|
208
|
+
/** The forms collection's settings in the order of the sections, then its other groups (spam protection). */
|
|
209
|
+
export function formSettingNames(fields) {
|
|
210
|
+
const names = new Set(fields.flatMap((f) => ('name' in f && typeof f.name === 'string' ? [f.name] : [])));
|
|
211
|
+
const known = new Set(FORM_SETTINGS_SECTIONS.flatMap((s) => s.fields));
|
|
212
|
+
const sections = FORM_SETTINGS_SECTIONS.flatMap((s) => s.fields.filter((name) => names.has(name)));
|
|
213
|
+
const groups = fields.flatMap((f) => (f.type === 'group' && 'name' in f && typeof f.name === 'string' && f.name && !known.has(f.name) && !f.admin?.hidden ? [f.name] : []));
|
|
214
|
+
return [...sections, ...groups];
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* A setting field as a prop of the Form block: never required (the sync fills the title), and
|
|
218
|
+
* without virtual fields inside (the Turnstile site key is a value Payload computes on read of the
|
|
219
|
+
* form, never part of the block).
|
|
220
|
+
*/
|
|
221
|
+
function asBlockSetting(field) {
|
|
222
|
+
const { required: _required, ...rest } = field;
|
|
223
|
+
if (Array.isArray(rest.fields))
|
|
224
|
+
rest.fields = rest.fields.filter((f) => !f.virtual).map((f) => asBlockSetting(f));
|
|
225
|
+
return rest;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The Form block's settings fields: the sections General, After submit and Mail, then one section
|
|
229
|
+
* per other group (spam protection), all shown only in "build" mode. `fields` are the forms
|
|
230
|
+
* collection's top-level fields (functions already turned into builder conditions).
|
|
231
|
+
*/
|
|
232
|
+
export function formSettingsFields(fields) {
|
|
233
|
+
const byName = new Map(fields.flatMap((f) => ('name' in f && typeof f.name === 'string' ? [[f.name, f]] : [])));
|
|
234
|
+
const shown = { builderCondition: { field: 'mode', notEquals: 'saved' }, [PANEL_SECTION_MARKER]: true };
|
|
235
|
+
const sections = FORM_SETTINGS_SECTIONS.flatMap((section, index) => {
|
|
236
|
+
const list = section.fields.flatMap((name) => (byName.has(name) ? [asBlockSetting(byName.get(name))] : []));
|
|
237
|
+
if (list.length === 0)
|
|
238
|
+
return [];
|
|
239
|
+
return [{ type: 'collapsible', label: section.label, fields: list, admin: { initCollapsed: index > 0, custom: shown } }];
|
|
240
|
+
});
|
|
241
|
+
const known = new Set(FORM_SETTINGS_SECTIONS.flatMap((s) => s.fields));
|
|
242
|
+
const groups = formSettingNames(fields).filter((name) => !known.has(name));
|
|
243
|
+
for (const name of groups) {
|
|
244
|
+
const group = byName.get(name);
|
|
245
|
+
const label = typeof group.label === 'string' ? group.label : name.replace(/([A-Z])/g, ' $1').replace(/^./, (c) => c.toUpperCase());
|
|
246
|
+
sections.push({ type: 'collapsible', label, fields: [plainGroup(asBlockSetting(group))], admin: { initCollapsed: true, custom: shown } });
|
|
247
|
+
}
|
|
248
|
+
return sections;
|
|
249
|
+
}
|
|
250
|
+
/** The group without its own heading (the section names it). */
|
|
251
|
+
function plainGroup(field) {
|
|
252
|
+
const loose = field;
|
|
253
|
+
const admin = (loose.admin ?? {});
|
|
254
|
+
return { ...loose, admin: { ...admin, custom: { ...admin.custom, [PLAIN_GROUP_MARKER]: true } } };
|
|
255
|
+
}
|
|
256
|
+
/** The parts of a field definition for pages and the form builder, with its type's parts. */
|
|
257
|
+
export function withFieldParts(def) {
|
|
258
|
+
const slug = def.payload?.slug ?? formFieldSlug(def.type);
|
|
259
|
+
const parts = slug ? formFieldParts(slug) : undefined;
|
|
260
|
+
return parts ? { ...def, parts } : def;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* The app's block list with forms on pages completed: the Form block gets its settings, the saved
|
|
264
|
+
* form's collection and the block types its slot takes; the field blocks join the list (replacing
|
|
265
|
+
* any of the same type) and go only inside a Form block. Returns the list as it is when it has no
|
|
266
|
+
* Form block with a children slot.
|
|
267
|
+
*/
|
|
268
|
+
export function withPageForms(blocks, input) {
|
|
269
|
+
const form = getBlockDefinition(blocks, FORM_BLOCK);
|
|
270
|
+
if (!form?.slots?.[FORM_SLOT])
|
|
271
|
+
return [...blocks];
|
|
272
|
+
const fieldTypes = input.fields.map((def) => def.type);
|
|
273
|
+
const layout = FORM_LAYOUT_TYPES.filter((type) => blocks.some((b) => b.type === type));
|
|
274
|
+
const settingNames = new Set(input.settings.flatMap(dataNamesOf));
|
|
275
|
+
const ownFields = form.fields
|
|
276
|
+
.filter((f) => !('name' in f) || !settingNames.has(f.name))
|
|
277
|
+
.map((f) => ('name' in f && f.name === 'form' && f.type === 'relationship' ? { ...f, relationTo: input.collection } : f));
|
|
278
|
+
const completed = {
|
|
279
|
+
...form,
|
|
280
|
+
fields: [...ownFields, ...input.settings],
|
|
281
|
+
slots: { ...form.slots, [FORM_SLOT]: { ...form.slots[FORM_SLOT], allow: [...fieldTypes, FORM_SUBMIT_BLOCK, ...layout] } },
|
|
282
|
+
};
|
|
283
|
+
const pageFields = input.fields.map((def) => ({ ...withFieldParts(def), ancestors: [FORM_BLOCK] }));
|
|
284
|
+
const out = blocks.filter((b) => !fieldTypes.includes(b.type)).map((b) => (b.type === FORM_BLOCK ? completed : b.type === FORM_SUBMIT_BLOCK ? { ...b, ancestors: [FORM_BLOCK] } : b));
|
|
285
|
+
return [...out, ...pageFields];
|
|
286
|
+
}
|
|
287
|
+
/** The data names of a field (inside collapsibles and rows too). */
|
|
288
|
+
function dataNamesOf(field) {
|
|
289
|
+
if ('name' in field && typeof field.name === 'string' && field.name)
|
|
290
|
+
return [field.name];
|
|
291
|
+
if ('fields' in field && Array.isArray(field.fields))
|
|
292
|
+
return field.fields.flatMap(dataNamesOf);
|
|
293
|
+
return [];
|
|
294
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { Field } from 'payload';
|
|
2
|
+
import type { Block, BlockDefinition, Layout } from '../core/types.js';
|
|
3
|
+
/** Name of the hidden group on the forms collection that names the block a form was made from. */
|
|
4
|
+
export declare const FORM_SOURCE_FIELD = "source";
|
|
5
|
+
/** The block a form document was made from. */
|
|
6
|
+
export type FormSource = {
|
|
7
|
+
collection: string;
|
|
8
|
+
document: string;
|
|
9
|
+
block: string;
|
|
10
|
+
hash?: string;
|
|
11
|
+
};
|
|
12
|
+
/** The message after a sent form when the Form block has none: the same text the site shows then. */
|
|
13
|
+
export declare const DEFAULT_CONFIRMATION = "Thank you. We will reply soon.";
|
|
14
|
+
/** The "Build here" Form blocks of a layout, in tree order (also inside other blocks). */
|
|
15
|
+
export declare function pageFormBlocks(layout: Layout): Block[];
|
|
16
|
+
/** One field block as a row of the plugin's `fields` blocks field. Classes go to the class keys. */
|
|
17
|
+
export declare function fieldRowOf(block: Block): Record<string, unknown>;
|
|
18
|
+
/** One row of the plugin's `fields` blocks field as a field block. */
|
|
19
|
+
export declare function fieldBlockOf(row: Record<string, unknown>): Block | null;
|
|
20
|
+
export type FormDataOptions = {
|
|
21
|
+
/** The forms collection's setting names (`formSettingNames`): title, submit button, confirmation, emails, groups. */
|
|
22
|
+
settings: readonly string[];
|
|
23
|
+
/** The title when the block has none, e.g. "Contact form" from the page title. */
|
|
24
|
+
fallbackTitle: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The forms document data of a "Build here" Form block: its fields in tree order (as the plugin's
|
|
28
|
+
* rows, row id = block id) and its settings. Empty settings stay out, so the collection's defaults
|
|
29
|
+
* apply. The title falls back to `fallbackTitle`; the submit button label to the first Submit
|
|
30
|
+
* button block's label.
|
|
31
|
+
*/
|
|
32
|
+
export declare function formDataFromBlock(form: Block, options: FormDataOptions): Record<string, unknown>;
|
|
33
|
+
/**
|
|
34
|
+
* The Form block's content made from a forms document ("Move into this page"): the settings as
|
|
35
|
+
* props, "build" mode, the form's id kept, and the fields (row id = block id) followed by a Submit
|
|
36
|
+
* button. `fields` are the forms collection's fields: the values of virtual fields (the spam settings'
|
|
37
|
+
* Turnstile site key, which Payload computes on read) never become block props. `settings`: the
|
|
38
|
+
* setting names, by default all of them (`formSettingNames`).
|
|
39
|
+
*/
|
|
40
|
+
export declare function formBlockFromDoc(stored: Record<string, unknown>, fields: readonly Field[], settings?: readonly string[]): {
|
|
41
|
+
props: Record<string, unknown>;
|
|
42
|
+
children: Block[];
|
|
43
|
+
};
|
|
44
|
+
/** The document without the values of virtual fields, also inside named groups (read-only values Payload computes on read). */
|
|
45
|
+
export declare function withoutVirtualFields(doc: Record<string, unknown>, fields: readonly Field[]): Record<string, unknown>;
|
|
46
|
+
/**
|
|
47
|
+
* A short hash of the form data a Form block writes (`formDataFromBlock`), stored on the form as
|
|
48
|
+
* `source.hash`. The next publish writes the form only when the hash differs, so Payload's own
|
|
49
|
+
* defaults in the stored document (empty condition groups, a default redirect) never count as a
|
|
50
|
+
* change. Keys are sorted and empty values left out; FNV-1a over the JSON.
|
|
51
|
+
*/
|
|
52
|
+
export declare function formDataHash(data: Record<string, unknown>): string;
|
|
53
|
+
/**
|
|
54
|
+
* The data that clears the settings `stored` has and `next` lacks, so Payload removes them (an
|
|
55
|
+
* update keeps keys it is not sent): `[]` for a list (emails), `null` for text and rich text.
|
|
56
|
+
* Groups (redirect, spam protection) stay: Payload's field hooks expect a group value.
|
|
57
|
+
*/
|
|
58
|
+
export declare function clearedSettings(stored: Record<string, unknown>, next: Record<string, unknown>, settings: readonly string[]): Record<string, null | []>;
|
|
59
|
+
/** The source of a stored form, or null when it has none. */
|
|
60
|
+
export declare function formSourceOf(doc: Record<string, unknown>): FormSource | null;
|
|
61
|
+
/** True when a stored form was made from this block of this document. */
|
|
62
|
+
export declare function isFormOf(doc: Record<string, unknown>, source: FormSource): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* The layout with `props.form` of each Form block in `ids` (block id -> form id) set, and its
|
|
65
|
+
* `mode` set to "build" (a block without a mode that names a form would read as "saved"). Returns
|
|
66
|
+
* the same layout when nothing changes.
|
|
67
|
+
*/
|
|
68
|
+
export declare function withFormIds(layout: Layout, ids: ReadonlyMap<string, string | number>): Layout;
|
|
69
|
+
/** The children slot of a Form block. */
|
|
70
|
+
export declare const formChildren: (form: Block) => Block[];
|
|
71
|
+
/**
|
|
72
|
+
* The layout with each "Use a saved form" Form block whose `form` is a loaded document holding
|
|
73
|
+
* that form's fields as its children (then a Submit button), so the site renders a saved form with
|
|
74
|
+
* the same field blocks as a form built on the page, and the page's CSS covers the fields' classes.
|
|
75
|
+
* The site runs it after loading the layout's documents (`loadLayoutData`). Returns the same layout
|
|
76
|
+
* when there is no such block.
|
|
77
|
+
*/
|
|
78
|
+
export declare function withSavedFormFields(layout: Layout): Layout;
|
|
79
|
+
/** The ids of the forms the "Use a saved form" Form blocks of `layouts` show, without repeats. */
|
|
80
|
+
export declare function savedFormIds(layouts: readonly Layout[]): Array<string | number>;
|
|
81
|
+
/** Every class of the fields of saved forms (their own classes and part classes), split on spaces. */
|
|
82
|
+
export declare function savedFormsClasses(docs: ReadonlyArray<Record<string, unknown>>): string[];
|
|
83
|
+
/**
|
|
84
|
+
* The look a Form block takes on when a saved form moves into it ("Move into this page"): the
|
|
85
|
+
* Form block's default layout classes when its own classes lay out nothing (a block that was only
|
|
86
|
+
* a box around the saved form), and the default classes of each part it has none for.
|
|
87
|
+
*/
|
|
88
|
+
export declare function movedFormLook(block: Block, def: BlockDefinition | undefined): {
|
|
89
|
+
addClassName?: string;
|
|
90
|
+
partClassName?: Record<string, string>;
|
|
91
|
+
};
|