payload-canvas 0.5.2 → 0.7.0-next.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 +350 -45
- package/dist/admin/AiKeyField.d.ts +4 -3
- package/dist/admin/AiKeyField.js +3 -3
- package/dist/admin/ArrayRowLabel.d.ts +12 -0
- package/dist/admin/ArrayRowLabel.js +21 -0
- package/dist/admin/BuilderTab.js +1 -1
- package/dist/admin/DocumentEditView.d.ts +1 -1
- package/dist/admin/DocumentEditView.js +2 -3
- package/dist/admin/LayoutField.d.ts +1 -1
- package/dist/admin/LayoutField.js +2 -2
- package/dist/admin/TemplateConditionsField.d.ts +2 -2
- package/dist/admin/TemplateConditionsField.js +2 -2
- package/dist/admin/aiKeyField.css +58 -0
- package/dist/admin/client.d.ts +1 -0
- package/dist/admin/client.js +2 -0
- package/dist/admin/diff/LayoutDiff.js +3 -3
- package/dist/admin/diff/LayoutJsonDiff.d.ts +1 -1
- package/dist/admin/diff/LayoutJsonDiff.js +1 -1
- package/dist/admin/diff/layoutDiff.css +180 -0
- package/dist/admin/editor/Editor.d.ts +2 -2
- package/dist/admin/editor/Editor.js +10 -3
- package/dist/admin/editor/Inspector.js +23 -17
- package/dist/admin/editor/Library.d.ts +2 -2
- package/dist/admin/editor/Library.js +8 -7
- 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.d.ts +1 -1
- package/dist/admin/editor/assistant/AssistantPanel.js +33 -21
- package/dist/admin/editor/assistant/{assistant.scss → assistant.css} +322 -315
- package/dist/admin/editor/classes/ClassManager.d.ts +1 -1
- package/dist/admin/editor/classes/ClassManager.js +1 -1
- package/dist/admin/editor/classes/manager.css +258 -0
- package/dist/admin/editor/comments/CommentLayer.d.ts +1 -1
- package/dist/admin/editor/comments/CommentLayer.js +1 -1
- package/dist/admin/editor/comments/CommentsPanel.d.ts +1 -1
- package/dist/admin/editor/comments/CommentsPanel.js +1 -1
- package/dist/admin/editor/comments/{comments.scss → comments.css} +121 -140
- 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/{dnd.scss → dnd.css} +78 -80
- package/dist/admin/editor/dnd/smooth.js +1 -1
- package/dist/admin/editor/{editor.scss → editor.css} +798 -751
- package/dist/admin/editor/empty/EmptyStart.d.ts +1 -1
- package/dist/admin/editor/empty/EmptyStart.js +3 -3
- package/dist/admin/editor/empty/{empty.scss → empty.css} +12 -13
- 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.css +786 -0
- package/dist/admin/editor/fields/values.d.ts +0 -2
- package/dist/admin/editor/fields/values.js +0 -14
- package/dist/admin/editor/generate/GenerateImage.d.ts +1 -1
- package/dist/admin/editor/generate/GenerateImage.js +1 -1
- package/dist/admin/editor/generate/{generate.scss → generate.css} +83 -82
- 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/layout.css +243 -0
- package/dist/admin/editor/layout/leftTabs.d.ts +15 -0
- package/dist/admin/editor/layout/leftTabs.js +18 -0
- package/dist/admin/editor/layout/panels.js +1 -1
- package/dist/admin/editor/live/PresenceUI.d.ts +1 -1
- package/dist/admin/editor/live/PresenceUI.js +2 -2
- package/dist/admin/editor/live/{presence.scss → presence.css} +5 -4
- package/dist/admin/editor/locale/LocaleField.d.ts +1 -1
- package/dist/admin/editor/locale/LocaleField.js +1 -1
- package/dist/admin/editor/locale/LocaleSwitcher.d.ts +1 -1
- package/dist/admin/editor/locale/LocaleSwitcher.js +1 -1
- package/dist/admin/editor/locale/locale.css +153 -0
- package/dist/admin/editor/media/ImageEditor.d.ts +9 -3
- package/dist/admin/editor/media/ImageEditor.js +17 -11
- package/dist/admin/editor/media/{media.scss → media.css} +70 -60
- package/dist/admin/editor/media/model.d.ts +10 -1
- package/dist/admin/editor/media/model.js +19 -0
- package/dist/admin/editor/menu/Menu.d.ts +1 -1
- package/dist/admin/editor/menu/Menu.js +1 -1
- package/dist/admin/editor/menu/blockMenu.js +3 -3
- package/dist/admin/editor/menu/menu.css +132 -0
- package/dist/admin/editor/motion/MotionPanel.d.ts +2 -2
- package/dist/admin/editor/motion/MotionPanel.js +2 -2
- package/dist/admin/editor/motion/{motion.scss → motion.css} +24 -21
- 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 +10 -3
- package/dist/admin/editor/renderField.js +87 -33
- package/dist/admin/editor/runtime.d.ts +3 -2
- package/dist/admin/editor/runtime.js +12 -18
- package/dist/admin/editor/sections/ComponentFields.d.ts +1 -1
- package/dist/admin/editor/sections/ComponentFields.js +2 -3
- package/dist/admin/editor/sections/PropertyControls.d.ts +1 -1
- package/dist/admin/editor/sections/PropertyControls.js +1 -1
- package/dist/admin/editor/sections/components.css +244 -0
- package/dist/admin/editor/sections/variants.css +93 -0
- package/dist/admin/editor/sections/variants.d.ts +1 -1
- package/dist/admin/editor/sections/variants.js +1 -1
- package/dist/admin/editor/styles/ClassBar.d.ts +1 -1
- package/dist/admin/editor/styles/ClassBar.js +1 -1
- package/dist/admin/editor/styles/StylesPanel.d.ts +1 -1
- package/dist/admin/editor/styles/StylesPanel.js +5 -3
- package/dist/admin/editor/styles/{classes.scss → classes.css} +128 -125
- package/dist/admin/editor/styles/context.js +9 -4
- package/dist/admin/editor/styles/{styles.scss → styles.css} +368 -370
- package/dist/admin/editor/templates/Bindable.d.ts +1 -1
- package/dist/admin/editor/templates/Bindable.js +1 -1
- package/dist/admin/editor/templates/binding.js +4 -1
- package/dist/admin/editor/templates/{templates.scss → templates.css} +207 -206
- package/dist/admin/editor/tooltip/Tooltips.d.ts +1 -1
- package/dist/admin/editor/tooltip/Tooltips.js +1 -1
- package/dist/admin/editor/tooltip/{tooltip.scss → tooltip.css} +11 -10
- package/dist/admin/editor/topbar/ContentModeToggle.d.ts +1 -1
- package/dist/admin/editor/topbar/ContentModeToggle.js +2 -2
- package/dist/admin/editor/topbar/DocumentActions.js +5 -5
- package/dist/admin/editor/topbar/TopBar.d.ts +1 -1
- package/dist/admin/editor/topbar/TopBar.js +2 -3
- package/dist/admin/editor/topbar/contentMode.css +25 -0
- package/dist/admin/editor/topbar/screens/DrawerRouter.d.ts +1 -1
- package/dist/admin/editor/topbar/screens/DrawerRouter.js +52 -40
- package/dist/admin/editor/topbar/screens/ScreenDrawer.d.ts +1 -1
- package/dist/admin/editor/topbar/screens/ScreenDrawer.js +4 -5
- package/dist/admin/editor/topbar/screens/drawerRouterSupport.d.ts +5 -5
- package/dist/admin/editor/topbar/screens/drawerRouterSupport.js +7 -7
- package/dist/admin/editor/topbar/screens/screens.css +139 -0
- package/dist/admin/editor/topbar/settingsDrawer.css +33 -0
- package/dist/admin/editor/topbar/settingsDrawer.d.ts +1 -1
- package/dist/admin/editor/topbar/settingsDrawer.js +3 -3
- package/dist/admin/editor/topbar/{topbar.scss → topbar.css} +189 -231
- package/dist/admin/editor/ui/Select.d.ts +1 -1
- package/dist/admin/editor/ui/Select.js +1 -1
- package/dist/admin/editor/ui/Slider.d.ts +1 -1
- package/dist/admin/editor/ui/Slider.js +1 -1
- package/dist/admin/editor/ui/Tabs.d.ts +17 -0
- package/dist/admin/editor/ui/Tabs.js +17 -0
- package/dist/admin/editor/ui/controls.css +301 -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.css +200 -0
- package/dist/admin/layoutField.css +70 -0
- package/dist/admin/rsc.d.ts +2 -0
- package/dist/admin/rsc.js +5 -0
- package/dist/admin/screen/BuilderScreen.d.ts +1 -1
- package/dist/admin/screen/BuilderScreen.js +1 -1
- package/dist/admin/templateConditions.css +25 -0
- package/dist/ai/endpoint.d.ts +5 -0
- package/dist/ai/endpoint.js +18 -8
- package/dist/ai/images/service.js +2 -2
- package/dist/ai/prompt.d.ts +8 -0
- package/dist/ai/prompt.js +30 -0
- package/dist/ai/settings/index.js +3 -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/classes/collection.js +1 -0
- package/dist/classes/sheet.js +2 -0
- package/dist/comments/collection.js +3 -0
- 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/fieldHooks.d.ts +1 -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/draftAccess.js +1 -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 +101 -30
- package/dist/plugin/links.d.ts +7 -2
- package/dist/plugin/links.js +9 -3
- package/dist/plugin/references.d.ts +6 -0
- package/dist/plugin/references.js +12 -0
- package/dist/plugin/sections.js +3 -0
- package/dist/plugin/templates.js +5 -1
- package/dist/react/canvas/BuilderCanvas.d.ts +2 -0
- package/dist/react/canvas/BuilderCanvas.js +29 -6
- package/dist/react/canvas/inline/candidates.js +2 -2
- package/dist/react/canvas/inline/mapper.d.ts +6 -1
- package/dist/react/canvas/inline/mapper.js +35 -4
- package/dist/react/canvas/inline/rich/editor.js +9 -1
- package/dist/react/components/Image.d.ts +1 -1
- package/dist/react/components/Image.js +2 -2
- 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 +7 -1
- package/dist/react/render/templates.js +3 -1
- package/dist/react/server.js +19 -0
- package/dist/react/theme/ThemeStyle.js +1 -1
- package/dist/theme/admin/ColorField.d.ts +4 -3
- package/dist/theme/admin/ColorField.js +1 -1
- package/dist/theme/admin/FontField.d.ts +4 -3
- package/dist/theme/admin/FontField.js +1 -1
- package/dist/theme/admin/SaveSignal.js +1 -1
- package/dist/theme/admin/SliderField.d.ts +1 -1
- package/dist/theme/admin/SliderField.js +1 -1
- package/dist/theme/admin/Variables.d.ts +4 -3
- package/dist/theme/admin/Variables.js +2 -2
- package/dist/theme/admin/fields.css +327 -0
- package/dist/theme/global.js +2 -0
- package/package.json +29 -24
- package/dist/admin/aiKeyField.scss +0 -58
- package/dist/admin/diff/layoutDiff.scss +0 -181
- package/dist/admin/editor/classes/manager.scss +0 -262
- package/dist/admin/editor/fields/fields.scss +0 -628
- package/dist/admin/editor/layout/layout.scss +0 -240
- package/dist/admin/editor/locale/locale.scss +0 -154
- package/dist/admin/editor/menu/menu.scss +0 -132
- package/dist/admin/editor/sections/components.scss +0 -238
- package/dist/admin/editor/sections/variants.scss +0 -90
- package/dist/admin/editor/topbar/contentMode.scss +0 -35
- package/dist/admin/editor/topbar/screens/screens.scss +0 -134
- package/dist/admin/editor/topbar/settingsDrawer.scss +0 -32
- package/dist/admin/editor/ui/controls.scss +0 -320
- package/dist/admin/layoutField.scss +0 -69
- package/dist/admin/templateConditions.scss +0 -24
- package/dist/theme/admin/fields.scss +0 -363
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type CollectionAfterChangeHook, type CollectionBeforeChangeHook, type CollectionBeforeValidateHook, type Endpoint, type Field, type PayloadRequest } from 'payload';
|
|
2
|
+
import { type LiveEndpointOptions } from '../live/endpoints.js';
|
|
3
|
+
import type { LiveRuntime } from '../live/runtime.js';
|
|
4
|
+
import { type FormSource } from '../forms/pageSync.js';
|
|
5
|
+
import { type FormSync } from './hook.js';
|
|
6
|
+
import type { Block } from '../core/types.js';
|
|
7
|
+
/** `context` flag of the plugin's own writes to the forms collection. */
|
|
8
|
+
export declare const FORM_SYNC_CONTEXT = "builderFormSync";
|
|
9
|
+
/** The hidden group on the forms collection that names the page block a form was made from. */
|
|
10
|
+
export declare function formSourceField(): Field;
|
|
11
|
+
/** True when a field list has a `localized` field at any depth (groups, rows, tabs, blocks, arrays). */
|
|
12
|
+
export declare function hasLocalizedField(fields: readonly unknown[] | undefined): boolean;
|
|
13
|
+
/** Where a form is used: the builder documents that reference it. Null when the references index is off (usage unknown). */
|
|
14
|
+
export type FormUsage = (req: PayloadRequest, formId: string | number) => Promise<Array<{
|
|
15
|
+
collection: string;
|
|
16
|
+
id: string | number;
|
|
17
|
+
}> | null>;
|
|
18
|
+
export type FormSyncOptions = {
|
|
19
|
+
/** The page collection's layout field (a refused form is reported on it). */
|
|
20
|
+
layoutField: string;
|
|
21
|
+
/** The page collection. */
|
|
22
|
+
collection: string;
|
|
23
|
+
/** Slug of the forms collection. */
|
|
24
|
+
forms: string;
|
|
25
|
+
/** The forms collection's setting names (`formSettingNames`). */
|
|
26
|
+
settings: readonly string[];
|
|
27
|
+
/** Where a form is used. Default: the references index (`formUsage`). */
|
|
28
|
+
usage?: FormUsage;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The save hook step (`HookOptions.formSync`) for one page collection. One query finds the forms
|
|
32
|
+
* of the page's Form blocks by `source`. A block without one takes over the form it names when
|
|
33
|
+
* that form has no source and no other document uses it ("Move into this page"; never on create),
|
|
34
|
+
* else gets a new form (a new block, a duplicated block, a copied page). A form is written only
|
|
35
|
+
* when its data changed. With localization the default locale is written explicitly, and when the
|
|
36
|
+
* forms collection has localized fields each other locale too. Writes run as the system (access
|
|
37
|
+
* off) in the page save's transaction. Deleted blocks keep their forms: their submissions point at them.
|
|
38
|
+
*/
|
|
39
|
+
export declare function pageFormSync(options: FormSyncOptions): FormSync;
|
|
40
|
+
export type ClaimResult = {
|
|
41
|
+
ok: true;
|
|
42
|
+
kept: boolean;
|
|
43
|
+
doc: Record<string, unknown>;
|
|
44
|
+
} | {
|
|
45
|
+
ok: false;
|
|
46
|
+
status: number;
|
|
47
|
+
error: string;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* "Move into this page", the answer only: whether the Form block `block` of `collection`/`document`
|
|
51
|
+
* may keep form `formId` (`kept`: no other document uses it, and no other block owns it), and the
|
|
52
|
+
* form to copy. Writes nothing: the page's next publish takes the form over (`pageFormSync`), so an
|
|
53
|
+
* undo or an unsaved page leaves the form as it was. The form is read with the user's access.
|
|
54
|
+
* Without the references index usage is unknown: never kept. The caller checks that the user may
|
|
55
|
+
* update the page and that `block` is a Form block of it.
|
|
56
|
+
*/
|
|
57
|
+
export declare function claimForm(req: PayloadRequest, args: {
|
|
58
|
+
forms: string;
|
|
59
|
+
formId: string | number;
|
|
60
|
+
source: Omit<FormSource, 'hash'>;
|
|
61
|
+
usage: FormUsage;
|
|
62
|
+
}): Promise<ClaimResult>;
|
|
63
|
+
/** Path of the claim endpoint, under the live path: `POST {api}/builder/live/:collection/:id/form-claim`. */
|
|
64
|
+
export declare const FORM_CLAIM_PATH = "form-claim";
|
|
65
|
+
/** The answer of the claim endpoint: the Form block's new content, and whether it keeps the form's id. */
|
|
66
|
+
export type FormClaimResponse = {
|
|
67
|
+
ok: true;
|
|
68
|
+
kept: boolean;
|
|
69
|
+
props: Record<string, unknown>;
|
|
70
|
+
children: Block[];
|
|
71
|
+
} | {
|
|
72
|
+
ok: false;
|
|
73
|
+
error: string;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* `POST {api}/builder/live/:collection/:id/form-claim` with `{ block, form }`: "Move into this page"
|
|
77
|
+
* for the Form block `block`. Needs update access to the page, and `block` must be a Form block of
|
|
78
|
+
* it. Answers with the block's content made from the form (`formBlockFromDoc`); without `kept`, the
|
|
79
|
+
* content names no form, so the page gets a new one when it is published. Writes nothing.
|
|
80
|
+
*/
|
|
81
|
+
export declare function formClaimEndpoint(args: {
|
|
82
|
+
forms: string;
|
|
83
|
+
settings: readonly string[];
|
|
84
|
+
collections: LiveEndpointOptions['collections'];
|
|
85
|
+
runtime: LiveRuntime;
|
|
86
|
+
}): Endpoint;
|
|
87
|
+
/** The documents whose layouts use a form (published or latest draft): the references index ("Used in"). Null without it. */
|
|
88
|
+
export declare function formUsage(forms: string): FormUsage;
|
|
89
|
+
/** Where a builder collection keeps the blocks a form source names: the layout field, or a saved section's `blocks`. */
|
|
90
|
+
export type SourceField = {
|
|
91
|
+
field: string;
|
|
92
|
+
blocks: 'layout' | 'list';
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* After a document is created: writes its forms, which the save hook could not do without the new
|
|
96
|
+
* id (a page created published, a copied page in a collection without drafts, a new saved
|
|
97
|
+
* section). Runs when the new document is live (published, or saved without drafts). A create
|
|
98
|
+
* never takes over another document's form: every block gets a new one. The ids go into the
|
|
99
|
+
* document with a second save that keeps the layout as sent.
|
|
100
|
+
*/
|
|
101
|
+
export declare function formSyncAfterCreate(args: {
|
|
102
|
+
collection: string;
|
|
103
|
+
source: SourceField;
|
|
104
|
+
formSync: FormSync;
|
|
105
|
+
}): CollectionAfterChangeHook;
|
|
106
|
+
/**
|
|
107
|
+
* Saved sections: a `beforeChange` hook after the section's blocks are set (`syncSectionBlocks`).
|
|
108
|
+
* Writes the forms of the Form blocks in the section's `blocks` (one form per Form block, shared
|
|
109
|
+
* by every page that shows the section) and stores the form ids in the blocks and the layout. The
|
|
110
|
+
* collection has no drafts: every save of a section writes its changed forms. Open editors get the
|
|
111
|
+
* ids like the props' hook changes.
|
|
112
|
+
*/
|
|
113
|
+
export declare function sectionFormSync(args: {
|
|
114
|
+
collection: string;
|
|
115
|
+
layoutField: string;
|
|
116
|
+
formSync: FormSync;
|
|
117
|
+
}): CollectionBeforeChangeHook;
|
|
118
|
+
/** The message for a submission to a page form that is no longer on the site. */
|
|
119
|
+
export declare const FORM_GONE_MESSAGE = "This form is no longer on the site.";
|
|
120
|
+
/**
|
|
121
|
+
* True when the document a form was built on still shows it: published (when its collection has
|
|
122
|
+
* drafts), and its blocks (the published layout, or a saved section's blocks) still hold a "Build
|
|
123
|
+
* here" Form block with the source's block id. Pure.
|
|
124
|
+
*/
|
|
125
|
+
export declare function formSourceLive(doc: Record<string, unknown> | null, source: Pick<FormSource, 'block'>, options: {
|
|
126
|
+
drafts: boolean;
|
|
127
|
+
field: SourceField;
|
|
128
|
+
}): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* The submissions `beforeValidate` hook that refuses a new submission to a form built on a page
|
|
131
|
+
* or a saved section (`source`) when that page or section no longer shows it (removed block,
|
|
132
|
+
* unpublished or deleted page). The form stays, for its old submissions. Forms made in the Forms
|
|
133
|
+
* collection (no `source`) are not checked. Two reads by id: the form's source, then the document.
|
|
134
|
+
*/
|
|
135
|
+
export declare function formLiveCheck(args: {
|
|
136
|
+
forms: string;
|
|
137
|
+
sourceField: (collection: string) => SourceField | null;
|
|
138
|
+
}): CollectionBeforeValidateHook;
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
// Forms on pages, on the server: the forms collection's `source` field, the save hook step that
|
|
2
|
+
// writes each "Build here" Form block to its forms document, the "Move into this page" claim, and
|
|
3
|
+
// the check that a page form is still on the site before it takes a submission.
|
|
4
|
+
// See docs/architecture.md, "Forms on pages".
|
|
5
|
+
import { addDataAndFileToRequest, APIError, ValidationError, } from 'payload';
|
|
6
|
+
import { LIVE_PATH, targetOf } from '../live/endpoints.js';
|
|
7
|
+
import { localeOverlay, localeSettingsOf } from '../core/locale.js';
|
|
8
|
+
import { findBlock, isPlainObject, normalizeLayout } from '../core/tree.js';
|
|
9
|
+
import { FORM_BLOCK, formBlockMode, formIdOf } from '../forms/pageForms.js';
|
|
10
|
+
import { clearedSettings, formBlockFromDoc, FORM_SOURCE_FIELD, formDataFromBlock, formDataHash, formSourceOf, pageFormBlocks, withFormIds, } from '../forms/pageSync.js';
|
|
11
|
+
import { HOOK_CHANGES_CONTEXT, KEEP_LAYOUT_CONTEXT, docEntry, setDocEntry, storedLayout } from './hook.js';
|
|
12
|
+
import { findReferrers, referencesConfigOf } from './references.js';
|
|
13
|
+
/** `context` flag of the plugin's own writes to the forms collection. */
|
|
14
|
+
export const FORM_SYNC_CONTEXT = 'builderFormSync';
|
|
15
|
+
/** The hidden group on the forms collection that names the page block a form was made from. */
|
|
16
|
+
export function formSourceField() {
|
|
17
|
+
return {
|
|
18
|
+
name: FORM_SOURCE_FIELD,
|
|
19
|
+
type: 'group',
|
|
20
|
+
label: 'Built on a page',
|
|
21
|
+
admin: { hidden: true },
|
|
22
|
+
fields: [
|
|
23
|
+
{ name: 'collection', type: 'text', index: true },
|
|
24
|
+
{ name: 'document', type: 'text', index: true },
|
|
25
|
+
{ name: 'block', type: 'text', index: true },
|
|
26
|
+
// The hash of the data the block wrote last: a publish without changes writes nothing.
|
|
27
|
+
{ name: 'hash', type: 'text' },
|
|
28
|
+
],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const api = (req) => req.payload;
|
|
32
|
+
const collectionOf = (req, slug) => req.payload.collections[slug];
|
|
33
|
+
/** The title of a page, for a form without a title of its own: "Contact form". */
|
|
34
|
+
function fallbackTitle(doc, useAsTitle) {
|
|
35
|
+
const title = [useAsTitle, 'title', 'name'].map((key) => (key ? doc[key] : undefined)).find((v) => typeof v === 'string' && v.trim() !== '');
|
|
36
|
+
return title ? `${title.trim()} form` : 'Page form';
|
|
37
|
+
}
|
|
38
|
+
/** The messages of a refused forms write: Payload's field errors ("Confirmation Message: This field is required."), else its message. */
|
|
39
|
+
function writeProblems(error) {
|
|
40
|
+
const data = error.data;
|
|
41
|
+
const lines = (data?.errors ?? []).map((e) => `${e.label ?? e.path ?? 'Field'}: ${e.message ?? 'not valid'}`);
|
|
42
|
+
return lines.length > 0 ? lines.join(' ') : error instanceof Error ? error.message : String(error);
|
|
43
|
+
}
|
|
44
|
+
/** True when a field list has a `localized` field at any depth (groups, rows, tabs, blocks, arrays). */
|
|
45
|
+
export function hasLocalizedField(fields) {
|
|
46
|
+
return (fields ?? []).some((raw) => {
|
|
47
|
+
if (!isPlainObject(raw))
|
|
48
|
+
return false;
|
|
49
|
+
if (raw.localized === true)
|
|
50
|
+
return true;
|
|
51
|
+
if (Array.isArray(raw.fields) && hasLocalizedField(raw.fields))
|
|
52
|
+
return true;
|
|
53
|
+
if (Array.isArray(raw.tabs) && raw.tabs.some((tab) => isPlainObject(tab) && hasLocalizedField(tab.fields)))
|
|
54
|
+
return true;
|
|
55
|
+
return Array.isArray(raw.blocks) && raw.blocks.some((block) => isPlainObject(block) && hasLocalizedField(block.fields));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/** The Local API `locale` of a write: none without localization. */
|
|
59
|
+
const inLocale = (locale) => (locale ? { locale } : {});
|
|
60
|
+
/** The data each locale writes, and the hash over all of them. One entry without localized forms. */
|
|
61
|
+
function formWrites(block, options) {
|
|
62
|
+
const writes = options.locales.map((locale, i) => {
|
|
63
|
+
// The first locale is the default: its values are the block's props. The others are its view in that locale.
|
|
64
|
+
const view = i === 0 ? block : localeOverlay({ version: 1, blocks: [block] }, locale, []).view.blocks[0];
|
|
65
|
+
return { locale, data: formDataFromBlock(view, { settings: options.settings, fallbackTitle: options.fallbackTitle }) };
|
|
66
|
+
});
|
|
67
|
+
const hash = writes.length === 1 ? formDataHash(writes[0].data) : formDataHash(Object.fromEntries(writes.map((w) => [w.locale, w.data])));
|
|
68
|
+
return { writes, hash };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The save hook step (`HookOptions.formSync`) for one page collection. One query finds the forms
|
|
72
|
+
* of the page's Form blocks by `source`. A block without one takes over the form it names when
|
|
73
|
+
* that form has no source and no other document uses it ("Move into this page"; never on create),
|
|
74
|
+
* else gets a new form (a new block, a duplicated block, a copied page). A form is written only
|
|
75
|
+
* when its data changed. With localization the default locale is written explicitly, and when the
|
|
76
|
+
* forms collection has localized fields each other locale too. Writes run as the system (access
|
|
77
|
+
* off) in the page save's transaction. Deleted blocks keep their forms: their submissions point at them.
|
|
78
|
+
*/
|
|
79
|
+
export function pageFormSync(options) {
|
|
80
|
+
const usage = options.usage ?? formUsage(options.forms);
|
|
81
|
+
return async (layout, { req, id, data, originalDoc, create }) => {
|
|
82
|
+
const blocks = pageFormBlocks(layout);
|
|
83
|
+
if (blocks.length === 0)
|
|
84
|
+
return layout;
|
|
85
|
+
const document = String(id);
|
|
86
|
+
const where = {
|
|
87
|
+
and: [
|
|
88
|
+
{ [`${FORM_SOURCE_FIELD}.collection`]: { equals: options.collection } },
|
|
89
|
+
{ [`${FORM_SOURCE_FIELD}.document`]: { equals: document } },
|
|
90
|
+
{ [`${FORM_SOURCE_FIELD}.block`]: { in: blocks.map((b) => b.id) } },
|
|
91
|
+
],
|
|
92
|
+
};
|
|
93
|
+
const { docs } = await api(req).find({ collection: options.forms, where, depth: 0, limit: blocks.length, pagination: false, overrideAccess: true, req });
|
|
94
|
+
const byBlock = new Map(docs.flatMap((doc) => (formSourceOf(doc) ? [[formSourceOf(doc).block, doc]] : [])));
|
|
95
|
+
const title = fallbackTitle({ ...originalDoc, ...data }, collectionOf(req, options.collection)?.config.admin?.useAsTitle);
|
|
96
|
+
const localization = localeSettingsOf(req.payload.config.localization);
|
|
97
|
+
const localized = Boolean(localization) && hasLocalizedField(collectionOf(req, options.forms)?.config.fields);
|
|
98
|
+
const locales = localization ? (localized ? [localization.defaultLocale, ...localization.locales.filter((l) => l !== localization.defaultLocale)] : [localization.defaultLocale]) : [''];
|
|
99
|
+
const ids = new Map();
|
|
100
|
+
for (const block of blocks) {
|
|
101
|
+
const { writes, hash } = formWrites(block, { settings: options.settings, fallbackTitle: title, locales });
|
|
102
|
+
const next = writes[0].data;
|
|
103
|
+
const source = { collection: options.collection, document, block: block.id, hash };
|
|
104
|
+
const stored = byBlock.get(block.id) ?? (create ? null : await adoptable(req, block, { forms: options.forms, collection: options.collection, document, usage }));
|
|
105
|
+
const common = { collection: options.forms, depth: 0, overrideAccess: true, req, context: { [FORM_SYNC_CONTEXT]: true } };
|
|
106
|
+
try {
|
|
107
|
+
let formId;
|
|
108
|
+
if (stored) {
|
|
109
|
+
formId = formIdOf(stored);
|
|
110
|
+
ids.set(block.id, formId);
|
|
111
|
+
if (formSourceOf(stored)?.hash === hash)
|
|
112
|
+
continue;
|
|
113
|
+
await api(req).update({ ...common, ...inLocale(writes[0].locale), id: formId, data: { ...clearedSettings(stored, next, options.settings), ...next, [FORM_SOURCE_FIELD]: source } });
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const created = await api(req).create({ ...common, ...inLocale(writes[0].locale), data: { ...next, [FORM_SOURCE_FIELD]: source } });
|
|
117
|
+
formId = formIdOf(created);
|
|
118
|
+
ids.set(block.id, formId);
|
|
119
|
+
}
|
|
120
|
+
for (const write of writes.slice(1))
|
|
121
|
+
await api(req).update({ ...common, ...inLocale(write.locale), id: formId, data: write.data });
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
// The page is not published: the problem names the form, so the editor can fix its settings.
|
|
125
|
+
const message = `Form "${String(next.title)}" could not be saved: ${writeProblems(error)}`;
|
|
126
|
+
throw new ValidationError({ collection: options.collection, errors: [{ path: options.layoutField, message }], req }, req.t);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return withFormIds(layout, ids);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The form a Form block names (`props.form`, set by "Move into this page") when the block may
|
|
134
|
+
* take it over: the form has no source (made in the Forms collection) and no other document uses
|
|
135
|
+
* it. Null otherwise, also when usage is unknown (references off): then the block gets a copy.
|
|
136
|
+
*/
|
|
137
|
+
async function adoptable(req, block, args) {
|
|
138
|
+
const formId = formIdOf(block.props?.form);
|
|
139
|
+
if (formId === null)
|
|
140
|
+
return null;
|
|
141
|
+
const doc = await api(req).findByID({ collection: args.forms, id: formId, depth: 0, overrideAccess: true, req }).catch(() => null);
|
|
142
|
+
if (!doc || formSourceOf(doc) !== null)
|
|
143
|
+
return null;
|
|
144
|
+
return (await usedElsewhere(req, formId, args)) ? null : doc;
|
|
145
|
+
}
|
|
146
|
+
/** True when a document other than `collection`/`document` uses the form, or when that cannot be known. */
|
|
147
|
+
async function usedElsewhere(req, formId, args) {
|
|
148
|
+
const users = await args.usage(req, formId);
|
|
149
|
+
if (users === null)
|
|
150
|
+
return true;
|
|
151
|
+
return users.some((u) => !(u.collection === args.collection && String(u.id) === args.document));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* "Move into this page", the answer only: whether the Form block `block` of `collection`/`document`
|
|
155
|
+
* may keep form `formId` (`kept`: no other document uses it, and no other block owns it), and the
|
|
156
|
+
* form to copy. Writes nothing: the page's next publish takes the form over (`pageFormSync`), so an
|
|
157
|
+
* undo or an unsaved page leaves the form as it was. The form is read with the user's access.
|
|
158
|
+
* Without the references index usage is unknown: never kept. The caller checks that the user may
|
|
159
|
+
* update the page and that `block` is a Form block of it.
|
|
160
|
+
*/
|
|
161
|
+
export async function claimForm(req, args) {
|
|
162
|
+
let doc;
|
|
163
|
+
try {
|
|
164
|
+
doc = await api(req).findByID({ collection: args.forms, id: args.formId, depth: 0, overrideAccess: false, user: req.user, req });
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return { ok: false, status: 404, error: 'This form no longer exists, or you may not read it.' };
|
|
168
|
+
}
|
|
169
|
+
const owner = formSourceOf(doc);
|
|
170
|
+
const ownedElsewhere = owner !== null && !(owner.collection === args.source.collection && owner.document === args.source.document && owner.block === args.source.block);
|
|
171
|
+
if (ownedElsewhere)
|
|
172
|
+
return { ok: true, kept: false, doc };
|
|
173
|
+
const elsewhere = await usedElsewhere(req, args.formId, { collection: args.source.collection, document: args.source.document, usage: args.usage });
|
|
174
|
+
return { ok: true, kept: !elsewhere, doc };
|
|
175
|
+
}
|
|
176
|
+
const json = (body, status = 200) => Response.json(body, { status, headers: { 'Cache-Control': 'private, no-store' } });
|
|
177
|
+
/** Path of the claim endpoint, under the live path: `POST {api}/builder/live/:collection/:id/form-claim`. */
|
|
178
|
+
export const FORM_CLAIM_PATH = 'form-claim';
|
|
179
|
+
/** The page's current layout: the open live session's, else the latest stored version. */
|
|
180
|
+
async function currentLayout(req, runtime, target) {
|
|
181
|
+
const open = runtime.sessions.peek(target.collection, target.id);
|
|
182
|
+
if (open)
|
|
183
|
+
return open.layout;
|
|
184
|
+
const doc = await api(req)
|
|
185
|
+
.findByID({ collection: target.collection, id: target.id, depth: 0, draft: true, overrideAccess: false, user: req.user, req })
|
|
186
|
+
.catch(() => null);
|
|
187
|
+
return doc && isPlainObject(doc[target.field]) ? normalizeLayout(doc[target.field]) : null;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* `POST {api}/builder/live/:collection/:id/form-claim` with `{ block, form }`: "Move into this page"
|
|
191
|
+
* for the Form block `block`. Needs update access to the page, and `block` must be a Form block of
|
|
192
|
+
* it. Answers with the block's content made from the form (`formBlockFromDoc`); without `kept`, the
|
|
193
|
+
* content names no form, so the page gets a new one when it is published. Writes nothing.
|
|
194
|
+
*/
|
|
195
|
+
export function formClaimEndpoint(args) {
|
|
196
|
+
return {
|
|
197
|
+
path: `${LIVE_PATH}/:collection/:id/${FORM_CLAIM_PATH}`,
|
|
198
|
+
method: 'post',
|
|
199
|
+
handler: async (req) => {
|
|
200
|
+
if (!req.user)
|
|
201
|
+
return json({ ok: false, error: 'Unauthorized' }, 401);
|
|
202
|
+
const target = targetOf(req, args.collections);
|
|
203
|
+
if (target instanceof Response)
|
|
204
|
+
return target;
|
|
205
|
+
if (!(await args.runtime.canUpdate(req, target.collection, target.id)))
|
|
206
|
+
return json({ ok: false, error: 'You are not allowed to change this page.' }, 403);
|
|
207
|
+
await addDataAndFileToRequest(req).catch(() => undefined);
|
|
208
|
+
const body = (req.data ?? {});
|
|
209
|
+
const block = typeof body.block === 'string' ? body.block : '';
|
|
210
|
+
const form = typeof body.form === 'string' || typeof body.form === 'number' ? body.form : null;
|
|
211
|
+
if (!block || form === null)
|
|
212
|
+
return json({ ok: false, error: 'Send the Form block id as `block` and the form id as `form`.' }, 400);
|
|
213
|
+
const layout = await currentLayout(req, args.runtime, target);
|
|
214
|
+
if (findBlock(layout ?? { version: 1, blocks: [] }, block)?.type !== FORM_BLOCK)
|
|
215
|
+
return json({ ok: false, error: 'This Form block is not on the page. Reload the editor and try again.' }, 400);
|
|
216
|
+
const result = await claimForm(req, { forms: args.forms, formId: form, source: { collection: target.collection, document: String(target.id), block }, usage: formUsage(args.forms) });
|
|
217
|
+
if (!result.ok)
|
|
218
|
+
return json({ ok: false, error: result.error }, result.status);
|
|
219
|
+
const fields = collectionOf(req, args.forms)?.config.fields ?? [];
|
|
220
|
+
const { props, children } = formBlockFromDoc(result.doc, fields, args.settings);
|
|
221
|
+
if (!result.kept)
|
|
222
|
+
delete props.form;
|
|
223
|
+
return json({ ok: true, kept: result.kept, props, children });
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/** The documents whose layouts use a form (published or latest draft): the references index ("Used in"). Null without it. */
|
|
228
|
+
export function formUsage(forms) {
|
|
229
|
+
return async (req, formId) => {
|
|
230
|
+
if (!referencesConfigOf(req.payload)?.relationTo.includes(forms))
|
|
231
|
+
return null;
|
|
232
|
+
const { docs } = await findReferrers(req.payload, { relationTo: forms, value: formId }, { req, limit: 20 });
|
|
233
|
+
return docs.map((doc) => ({ collection: doc.collection, id: doc.id }));
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* After a document is created: writes its forms, which the save hook could not do without the new
|
|
238
|
+
* id (a page created published, a copied page in a collection without drafts, a new saved
|
|
239
|
+
* section). Runs when the new document is live (published, or saved without drafts). A create
|
|
240
|
+
* never takes over another document's form: every block gets a new one. The ids go into the
|
|
241
|
+
* document with a second save that keeps the layout as sent.
|
|
242
|
+
*/
|
|
243
|
+
export function formSyncAfterCreate(args) {
|
|
244
|
+
return async ({ collection, doc, operation, req }) => {
|
|
245
|
+
if (operation !== 'create' || doc?.id === undefined)
|
|
246
|
+
return doc;
|
|
247
|
+
if (collection.versions?.drafts && doc._status !== 'published')
|
|
248
|
+
return doc;
|
|
249
|
+
const id = doc.id;
|
|
250
|
+
const layout = args.source.blocks === 'list'
|
|
251
|
+
? normalizeLayout({ version: 1, blocks: Array.isArray(doc[args.source.field]) ? doc[args.source.field] : [] })
|
|
252
|
+
: (req.payload.config.localization ? await storedLayout(req, collection, id, args.source.field) : null) ??
|
|
253
|
+
(isPlainObject(doc[args.source.field]) ? normalizeLayout(doc[args.source.field]) : null);
|
|
254
|
+
if (!layout || pageFormBlocks(layout).length === 0)
|
|
255
|
+
return doc;
|
|
256
|
+
const next = await args.formSync(layout, { req, id, data: doc, originalDoc: undefined, create: true });
|
|
257
|
+
if (next === layout)
|
|
258
|
+
return doc;
|
|
259
|
+
const value = args.source.blocks === 'list' ? next.blocks : next;
|
|
260
|
+
await req.payload.update({
|
|
261
|
+
collection: args.collection,
|
|
262
|
+
id,
|
|
263
|
+
data: { [args.source.field]: value },
|
|
264
|
+
depth: 0,
|
|
265
|
+
overrideAccess: true,
|
|
266
|
+
req,
|
|
267
|
+
context: { [KEEP_LAYOUT_CONTEXT]: true, [FORM_SYNC_CONTEXT]: true },
|
|
268
|
+
});
|
|
269
|
+
return { ...doc, [args.source.field]: value };
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Saved sections: a `beforeChange` hook after the section's blocks are set (`syncSectionBlocks`).
|
|
274
|
+
* Writes the forms of the Form blocks in the section's `blocks` (one form per Form block, shared
|
|
275
|
+
* by every page that shows the section) and stores the form ids in the blocks and the layout. The
|
|
276
|
+
* collection has no drafts: every save of a section writes its changed forms. Open editors get the
|
|
277
|
+
* ids like the props' hook changes.
|
|
278
|
+
*/
|
|
279
|
+
export function sectionFormSync(args) {
|
|
280
|
+
return async ({ context, data, operation, originalDoc, req }) => {
|
|
281
|
+
const id = originalDoc?.id;
|
|
282
|
+
if (operation !== 'update' || id === undefined || !data || !Array.isArray(data.blocks))
|
|
283
|
+
return data;
|
|
284
|
+
const layout = normalizeLayout({ version: 1, blocks: data.blocks });
|
|
285
|
+
const next = await args.formSync(layout, { req, id, data, originalDoc });
|
|
286
|
+
if (next === layout)
|
|
287
|
+
return data;
|
|
288
|
+
data.blocks = next.blocks;
|
|
289
|
+
data[args.layoutField] = next;
|
|
290
|
+
// The editor's layout gets the form ids (session.ts, `adoptSaved`).
|
|
291
|
+
const earlier = docEntry(context, HOOK_CHANGES_CONTEXT, args.collection, id);
|
|
292
|
+
setDocEntry(context, HOOK_CHANGES_CONTEXT, args.collection, id, { input: earlier?.input ?? layout, output: structuredClone(next) });
|
|
293
|
+
return data;
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
/** The message for a submission to a page form that is no longer on the site. */
|
|
297
|
+
export const FORM_GONE_MESSAGE = 'This form is no longer on the site.';
|
|
298
|
+
/**
|
|
299
|
+
* True when the document a form was built on still shows it: published (when its collection has
|
|
300
|
+
* drafts), and its blocks (the published layout, or a saved section's blocks) still hold a "Build
|
|
301
|
+
* here" Form block with the source's block id. Pure.
|
|
302
|
+
*/
|
|
303
|
+
export function formSourceLive(doc, source, options) {
|
|
304
|
+
if (!doc)
|
|
305
|
+
return false;
|
|
306
|
+
if (options.drafts && doc._status !== 'published')
|
|
307
|
+
return false;
|
|
308
|
+
const value = doc[options.field.field];
|
|
309
|
+
const blocks = options.field.blocks === 'list' ? value : isPlainObject(value) ? value.blocks : null;
|
|
310
|
+
if (!Array.isArray(blocks))
|
|
311
|
+
return false;
|
|
312
|
+
const block = findBlock(normalizeLayout({ version: 1, blocks }), source.block);
|
|
313
|
+
return block?.type === FORM_BLOCK && formBlockMode(block.props, block.slots) === 'build';
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* The submissions `beforeValidate` hook that refuses a new submission to a form built on a page
|
|
317
|
+
* or a saved section (`source`) when that page or section no longer shows it (removed block,
|
|
318
|
+
* unpublished or deleted page). The form stays, for its old submissions. Forms made in the Forms
|
|
319
|
+
* collection (no `source`) are not checked. Two reads by id: the form's source, then the document.
|
|
320
|
+
*/
|
|
321
|
+
export function formLiveCheck(args) {
|
|
322
|
+
return async ({ data, operation, req }) => {
|
|
323
|
+
if (operation !== 'create' || !data)
|
|
324
|
+
return data;
|
|
325
|
+
const formId = formIdOf(data.form);
|
|
326
|
+
if (formId === null)
|
|
327
|
+
return data;
|
|
328
|
+
const form = await api(req)
|
|
329
|
+
.findByID({ collection: args.forms, id: formId, depth: 0, overrideAccess: true, req, select: { [FORM_SOURCE_FIELD]: true } })
|
|
330
|
+
.catch(() => null);
|
|
331
|
+
const source = form ? formSourceOf(form) : null;
|
|
332
|
+
if (!source)
|
|
333
|
+
return data;
|
|
334
|
+
const field = args.sourceField(source.collection);
|
|
335
|
+
const drafts = Boolean(collectionOf(req, source.collection)?.config.versions?.drafts);
|
|
336
|
+
const doc = field
|
|
337
|
+
? await api(req)
|
|
338
|
+
.findByID({ collection: source.collection, id: source.document, depth: 0, draft: false, overrideAccess: true, req, select: { [field.field]: true, ...(drafts ? { _status: true } : {}) } })
|
|
339
|
+
.catch(() => null)
|
|
340
|
+
: null;
|
|
341
|
+
if (field && formSourceLive(doc, source, { drafts, field }))
|
|
342
|
+
return data;
|
|
343
|
+
throw new APIError(FORM_GONE_MESSAGE, 410, null, true);
|
|
344
|
+
};
|
|
345
|
+
}
|
package/dist/plugin/hook.d.ts
CHANGED
|
@@ -36,7 +36,24 @@ type HookOptions = {
|
|
|
36
36
|
fieldRegistry?: FieldRegistry;
|
|
37
37
|
/** The locales of localized props. Null or left out: the layout is not localized. */
|
|
38
38
|
localization?: LocaleSettings | null;
|
|
39
|
+
/**
|
|
40
|
+
* Forms on pages: writes each "Build here" Form block to the forms collection and returns the
|
|
41
|
+
* layout with the form ids. Runs when the document is published (collections with drafts) or
|
|
42
|
+
* saved (without drafts), after the layout passed its checks. Its changes reach open editors
|
|
43
|
+
* like the props' hook changes.
|
|
44
|
+
*/
|
|
45
|
+
formSync?: FormSync;
|
|
39
46
|
};
|
|
47
|
+
/** See `HookOptions.formSync`. Returns the same layout when nothing changed. */
|
|
48
|
+
export type FormSync = (layout: Layout,
|
|
49
|
+
/** `create`: the document was just created (`formSyncAfterCreate`): no block takes over an existing form. */
|
|
50
|
+
args: {
|
|
51
|
+
req: PayloadRequest;
|
|
52
|
+
id: string | number;
|
|
53
|
+
data: Record<string, unknown>;
|
|
54
|
+
originalDoc: Record<string, unknown> | undefined;
|
|
55
|
+
create?: boolean;
|
|
56
|
+
}) => Promise<Layout>;
|
|
40
57
|
/** `context` flag of the session's own draft saves. */
|
|
41
58
|
export declare const SESSION_SAVE_CONTEXT = "builderSession";
|
|
42
59
|
/**
|
|
@@ -61,6 +78,11 @@ export declare const GUARD_SESSION_CONTEXT = "builderSessionId";
|
|
|
61
78
|
export declare const HOOK_CHANGES_CONTEXT = "builderHookChanges";
|
|
62
79
|
/** The entry of `collection`/`id` in a per-document `context` value (GUARD_SEQ_CONTEXT, HOOK_CHANGES_CONTEXT). */
|
|
63
80
|
export declare function docEntry(context: Record<string, unknown> | undefined, name: string, collection: string, id: unknown): unknown;
|
|
81
|
+
/**
|
|
82
|
+
* Sets (or with `undefined`, removes) the entry of `collection`/`id`. A new object each time: the
|
|
83
|
+
* copies of the context that nested saves made keep their own entries.
|
|
84
|
+
*/
|
|
85
|
+
export declare function setDocEntry(context: Record<string, unknown> | undefined, name: string, collection: string, id: string | number, value: unknown): void;
|
|
64
86
|
/** The session seq the guard wrote into this save of `collection`/`id`, or undefined. */
|
|
65
87
|
export declare function guardSeqOf(context: Record<string, unknown> | undefined, collection: string, id: unknown): number | undefined;
|
|
66
88
|
/**
|
package/dist/plugin/hook.js
CHANGED
|
@@ -52,7 +52,7 @@ export function docEntry(context, name, collection, id) {
|
|
|
52
52
|
* Sets (or with `undefined`, removes) the entry of `collection`/`id`. A new object each time: the
|
|
53
53
|
* copies of the context that nested saves made keep their own entries.
|
|
54
54
|
*/
|
|
55
|
-
function setDocEntry(context, name, collection, id, value) {
|
|
55
|
+
export function setDocEntry(context, name, collection, id, value) {
|
|
56
56
|
if (!context)
|
|
57
57
|
return;
|
|
58
58
|
const entries = isPlainObject(context[name]) ? context[name] : {};
|
|
@@ -313,6 +313,13 @@ export function layoutBeforeChange(options) {
|
|
|
313
313
|
const lines = describeLayoutErrors(layout, blocking, blocks, { localization }).map((issue) => issue.message);
|
|
314
314
|
throw new ValidationError({ collection: slug, errors: [{ path: field, message: lines.join('\n') }], req }, req.t);
|
|
315
315
|
};
|
|
316
|
+
// Forms on pages go to the forms collection when the page is published (or saved, without drafts).
|
|
317
|
+
const syncForms = options.formSync && docId !== undefined && (publishing || !collection.versions?.drafts) ? options.formSync : null;
|
|
318
|
+
// A publish without the layout (REST `{ _status: 'published' }`) still writes the page's forms:
|
|
319
|
+
// it goes on with the stored layout.
|
|
320
|
+
if (data[field] === undefined && syncForms && publishing && isPlainObject(originalDoc?.[field])) {
|
|
321
|
+
data[field] = (localization && docId !== undefined ? await storedLayout(req, collection, docId, field) : null) ?? normalizeLayout(originalDoc[field]);
|
|
322
|
+
}
|
|
316
323
|
// Partial update without the layout: keep the stored layout and CSS. A publish of that kind
|
|
317
324
|
// (REST `{ _status: 'published' }`) still runs the props' own `validate` functions.
|
|
318
325
|
if (data[field] === undefined) {
|
|
@@ -349,6 +356,9 @@ export function layoutBeforeChange(options) {
|
|
|
349
356
|
if (problem)
|
|
350
357
|
throw new APIError(restoring ? `${RESTORE_DESIGN_MESSAGE} ${problem}` : problem, 403, undefined, true);
|
|
351
358
|
}
|
|
359
|
+
// The layout before the props' hooks and the form sync: their changes go to open editors.
|
|
360
|
+
const propHooks = Boolean(registry && (hasPropHooks(registry, 'beforeValidate') || hasPropHooks(registry, 'beforeChange')));
|
|
361
|
+
const beforeHooks = propHooks || syncForms ? structuredClone(layout) : null;
|
|
352
362
|
if (registry) {
|
|
353
363
|
// Field access. The live session checks every edit when it arrives (session.ts), so its
|
|
354
364
|
// saves and the layouts the guard put in are not checked again. Other saves (REST, the
|
|
@@ -361,19 +371,21 @@ export function layoutBeforeChange(options) {
|
|
|
361
371
|
}
|
|
362
372
|
}
|
|
363
373
|
// Prop hooks, in Payload's order: beforeValidate, then beforeChange, then validation.
|
|
364
|
-
if (
|
|
365
|
-
const input = structuredClone(layout);
|
|
374
|
+
if (propHooks) {
|
|
366
375
|
await runPropHooks('beforeValidate', layout, { blocks, registry, previous: savedLayout, ctx, localization });
|
|
367
376
|
await runPropHooks('beforeChange', layout, { blocks, registry, previous: savedLayout, ctx, localization });
|
|
368
|
-
if (docId !== undefined) {
|
|
369
|
-
const changes = sameJson(input, layout) ? undefined : { input, output: structuredClone(layout) };
|
|
370
|
-
setDocEntry(context, HOOK_CHANGES_CONTEXT, slug, docId, changes);
|
|
371
|
-
}
|
|
372
377
|
}
|
|
373
378
|
}
|
|
374
379
|
const { blocking, warnings } = await checkLayout(layout, { blocks, publishing, bindings, doc: merged, fields: fieldCheck(savedLayout), localization });
|
|
375
380
|
if (blocking.length > 0)
|
|
376
381
|
reject(layout, blocking);
|
|
382
|
+
// Only a layout that passed its checks writes forms (same transaction: a failed save writes none).
|
|
383
|
+
if (syncForms && docId !== undefined)
|
|
384
|
+
layout = await syncForms(layout, { req, id: docId, data, originalDoc });
|
|
385
|
+
if (beforeHooks && docId !== undefined) {
|
|
386
|
+
const changes = sameJson(beforeHooks, layout) ? undefined : { input: beforeHooks, output: structuredClone(layout) };
|
|
387
|
+
setDocEntry(context, HOOK_CHANGES_CONTEXT, slug, docId, changes);
|
|
388
|
+
}
|
|
377
389
|
const logged = warnings.filter(isLayoutWarning);
|
|
378
390
|
if (logged.length > 0) {
|
|
379
391
|
req.payload.logger.warn(`[websiteBuilder] ${slug}.${field} saved with warnings:\n${formatErrors(logged)}`);
|
package/dist/plugin/index.d.ts
CHANGED
|
@@ -8,7 +8,9 @@ import { type SavedSectionsOptions } from './sections.js';
|
|
|
8
8
|
import { type ReferencesOptions } from './references.js';
|
|
9
9
|
import { type ClassesOptions } from '../classes/index.js';
|
|
10
10
|
import { type CommentsOptions } from '../comments/index.js';
|
|
11
|
+
import { type FormsOptions } from './forms.js';
|
|
11
12
|
export type { GeneratedCss } from './hook.js';
|
|
13
|
+
export type { FormsOptions } from './forms.js';
|
|
12
14
|
export type { SavedSectionsOptions } from './sections.js';
|
|
13
15
|
export type { ReferencesOptions } from './references.js';
|
|
14
16
|
/**
|
|
@@ -74,6 +76,11 @@ export type BuilderCollectionOptions = {
|
|
|
74
76
|
* A separate layout per locale (`localized: true` on the whole field) is not supported yet.
|
|
75
77
|
*/
|
|
76
78
|
localization?: 'props' | false;
|
|
79
|
+
/**
|
|
80
|
+
* `false`: the editor of this collection offers no sections: no Sections tab and no "Save as
|
|
81
|
+
* section…". The `forms` option sets it, because form fields are not page sections.
|
|
82
|
+
*/
|
|
83
|
+
sections?: false;
|
|
77
84
|
};
|
|
78
85
|
export type TemplatesOptions = {
|
|
79
86
|
/** Slug of the templates collection. Default "builder-templates". */
|
|
@@ -170,6 +177,13 @@ export type WebsiteBuilderOptions = {
|
|
|
170
177
|
* in the layout. On by default; `false` turns it off. See the README's "Comments" section.
|
|
171
178
|
*/
|
|
172
179
|
comments?: CommentsOptions | boolean;
|
|
180
|
+
/**
|
|
181
|
+
* The form builder: a full-screen builder for the forms of `@payloadcms/plugin-form-builder`
|
|
182
|
+
* (add that plugin before this one). The form's fields are its own `fields` blocks field; the
|
|
183
|
+
* right panel edits the form's settings when no field is selected. The canvas shows the form
|
|
184
|
+
* through the app's page frame (`createCanvasServer({ frame })`). See the README's "Form builder".
|
|
185
|
+
*/
|
|
186
|
+
forms?: FormsOptions | boolean;
|
|
173
187
|
};
|
|
174
188
|
/** Key of the full-screen builder view in `admin.components.views`. */
|
|
175
189
|
export declare const BUILDER_VIEW_KEY = "websiteBuilder";
|