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,156 @@
|
|
|
1
|
+
// Text props that must differ between the blocks of one scope (`BlockDefinition.uniqueProps`), such
|
|
2
|
+
// as the `name` of a form field: two fields with one name would overwrite each other's answers. A
|
|
3
|
+
// block with `uniqueScope` (a Form block) is a scope of its own: its fields differ from each other,
|
|
4
|
+
// not from the fields of another form. The rest of the layout is one scope.
|
|
5
|
+
import { htmlIdsOf, withoutTakenHtmlIds } from './attrs.js';
|
|
6
|
+
import { getBlockDefinition } from './blocks.js';
|
|
7
|
+
import { createId } from './ids.js';
|
|
8
|
+
import { findBlock, findLocation, indexLayout } from './tree.js';
|
|
9
|
+
const isScope = (definitions, type) => getBlockDefinition(definitions, type)?.uniqueScope === true;
|
|
10
|
+
/**
|
|
11
|
+
* The values in use, per prop name, of every unique prop in `blocks` (at every depth), without
|
|
12
|
+
* the blocks inside a nested scope (a Form block's fields belong to that form).
|
|
13
|
+
*/
|
|
14
|
+
export function uniquePropValues(blocks, definitions, taken = new Map()) {
|
|
15
|
+
for (const block of blocks) {
|
|
16
|
+
for (const prop of getBlockDefinition(definitions, block.type)?.uniqueProps ?? []) {
|
|
17
|
+
const value = block.props?.[prop];
|
|
18
|
+
if (typeof value !== 'string' || !value)
|
|
19
|
+
continue;
|
|
20
|
+
if (!taken.has(prop))
|
|
21
|
+
taken.set(prop, new Set());
|
|
22
|
+
taken.get(prop).add(value);
|
|
23
|
+
}
|
|
24
|
+
if (isScope(definitions, block.type))
|
|
25
|
+
continue;
|
|
26
|
+
for (const list of Object.values(block.slots ?? {}))
|
|
27
|
+
uniquePropValues(list, definitions, taken);
|
|
28
|
+
}
|
|
29
|
+
return taken;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The id of the scope a block placed in `parentId` belongs to: the nearest block with `uniqueScope`
|
|
33
|
+
* from `parentId` up (itself included), else null (the layout's own scope).
|
|
34
|
+
*/
|
|
35
|
+
export function uniqueScopeOf(layout, parentId, definitions) {
|
|
36
|
+
const index = indexLayout(layout);
|
|
37
|
+
for (let id = parentId; id !== null;) {
|
|
38
|
+
const entry = index.get(id);
|
|
39
|
+
if (!entry)
|
|
40
|
+
return null;
|
|
41
|
+
if (isScope(definitions, entry.block.type))
|
|
42
|
+
return id;
|
|
43
|
+
id = entry.parentId;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The values in use in the scope of a position: a block placed in `parentId` must not reuse them.
|
|
49
|
+
* Inside a Form block: that form's fields only.
|
|
50
|
+
*/
|
|
51
|
+
export function uniquePropValuesAt(layout, parentId, definitions) {
|
|
52
|
+
const scope = uniqueScopeOf(layout, parentId, definitions);
|
|
53
|
+
if (scope === null)
|
|
54
|
+
return uniquePropValues(layout.blocks, definitions);
|
|
55
|
+
const owner = indexLayout(layout).get(scope)?.block;
|
|
56
|
+
const taken = new Map();
|
|
57
|
+
for (const list of Object.values(owner?.slots ?? {}))
|
|
58
|
+
uniquePropValues(list, definitions, taken);
|
|
59
|
+
return taken;
|
|
60
|
+
}
|
|
61
|
+
/** `value`, or the first of `value2`, `value3`, … that is not in `taken`. */
|
|
62
|
+
export function freeValue(value, taken) {
|
|
63
|
+
if (!taken.has(value))
|
|
64
|
+
return value;
|
|
65
|
+
const base = value.replace(/\d+$/, '');
|
|
66
|
+
for (let n = 2;; n++)
|
|
67
|
+
if (!taken.has(`${base}${n}`))
|
|
68
|
+
return `${base}${n}`;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The block with a free value for each of its unique props (inside its slots too) that `taken`
|
|
72
|
+
* already holds. Adds the values it keeps to `taken`. Returns the same block when nothing clashes.
|
|
73
|
+
* The children of a block with `uniqueScope` only need to differ from each other.
|
|
74
|
+
*/
|
|
75
|
+
export function withUniqueProps(block, definitions, taken) {
|
|
76
|
+
let result = block;
|
|
77
|
+
for (const prop of getBlockDefinition(definitions, block.type)?.uniqueProps ?? []) {
|
|
78
|
+
const value = result.props?.[prop];
|
|
79
|
+
if (typeof value !== 'string' || !value)
|
|
80
|
+
continue;
|
|
81
|
+
if (!taken.has(prop))
|
|
82
|
+
taken.set(prop, new Set());
|
|
83
|
+
const used = taken.get(prop);
|
|
84
|
+
const free = freeValue(value, used);
|
|
85
|
+
used.add(free);
|
|
86
|
+
if (free !== value)
|
|
87
|
+
result = { ...result, props: { ...result.props, [prop]: free } };
|
|
88
|
+
}
|
|
89
|
+
if (result.slots) {
|
|
90
|
+
const inner = isScope(definitions, block.type) ? new Map() : taken;
|
|
91
|
+
let changed = false;
|
|
92
|
+
const slots = Object.fromEntries(Object.entries(result.slots).map(([name, list]) => {
|
|
93
|
+
const next = list.map((child) => withUniqueProps(child, definitions, inner));
|
|
94
|
+
if (next.some((child, i) => child !== list[i]))
|
|
95
|
+
changed = true;
|
|
96
|
+
return [name, next];
|
|
97
|
+
}));
|
|
98
|
+
if (changed)
|
|
99
|
+
result = { ...result, slots };
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
/** The blocks that repeat a unique prop's value in their scope (the second and later ones), in tree order. */
|
|
104
|
+
export function uniquePropClashes(layout, definitions) {
|
|
105
|
+
const out = [];
|
|
106
|
+
const visit = (list, taken) => {
|
|
107
|
+
for (const block of list) {
|
|
108
|
+
for (const prop of getBlockDefinition(definitions, block.type)?.uniqueProps ?? []) {
|
|
109
|
+
const value = block.props?.[prop];
|
|
110
|
+
if (typeof value !== 'string' || !value)
|
|
111
|
+
continue;
|
|
112
|
+
if (!taken.has(prop))
|
|
113
|
+
taken.set(prop, new Set());
|
|
114
|
+
if (taken.get(prop).has(value))
|
|
115
|
+
out.push({ blockId: block.id, prop, value });
|
|
116
|
+
else
|
|
117
|
+
taken.get(prop).add(value);
|
|
118
|
+
}
|
|
119
|
+
const inner = isScope(definitions, block.type) ? new Map() : taken;
|
|
120
|
+
for (const children of Object.values(block.slots ?? {}))
|
|
121
|
+
visit(children, inner);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
visit(layout.blocks, new Map());
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The operation that duplicates block `id` as one undo step: an `insert` of a copy right after it,
|
|
129
|
+
* with new ids (`newId` for the copy, fresh ones inside), without the HTML ids the layout already
|
|
130
|
+
* uses, and with free values for the unique props of the copy and of every block inside it (a
|
|
131
|
+
* Stack of form fields inside a form gets `email2`). A copied Form block is a scope of its own: its
|
|
132
|
+
* fields keep their names. Null when the block is not in the layout.
|
|
133
|
+
*/
|
|
134
|
+
export function duplicateOperation(layout, id, newId, definitions, makeId = createId) {
|
|
135
|
+
const block = findBlock(layout, id);
|
|
136
|
+
const location = findLocation(layout, id);
|
|
137
|
+
if (!block || !location)
|
|
138
|
+
return null;
|
|
139
|
+
const used = new Set([...indexLayout(layout).keys(), newId]);
|
|
140
|
+
const fresh = () => {
|
|
141
|
+
let next = makeId();
|
|
142
|
+
while (used.has(next))
|
|
143
|
+
next = makeId();
|
|
144
|
+
used.add(next);
|
|
145
|
+
return next;
|
|
146
|
+
};
|
|
147
|
+
const withIds = (source, blockId) => {
|
|
148
|
+
const copy = { ...structuredClone(source), id: blockId };
|
|
149
|
+
if (source.slots)
|
|
150
|
+
copy.slots = Object.fromEntries(Object.entries(source.slots).map(([name, list]) => [name, list.map((child) => withIds(child, fresh()))]));
|
|
151
|
+
return copy;
|
|
152
|
+
};
|
|
153
|
+
const taken = uniquePropValuesAt(layout, location.parentId, definitions);
|
|
154
|
+
const copy = withUniqueProps(withoutTakenHtmlIds(withIds(block, newId), htmlIdsOf(layout.blocks)), definitions, taken);
|
|
155
|
+
return { type: 'insert', block: copy, to: { parentId: location.parentId, ...(location.parentId === null ? {} : { slot: location.slot }), index: location.index + 1 } };
|
|
156
|
+
}
|
package/dist/core/validate.js
CHANGED
|
@@ -12,6 +12,7 @@ import { motionProblems } from './motion.js';
|
|
|
12
12
|
import { checkSectionOrigin } from './sectionOrigin.js';
|
|
13
13
|
import { checkVisibility } from './visibility.js';
|
|
14
14
|
import { fallbackChain, localizedKeys } from './locale.js';
|
|
15
|
+
import { uniquePropClashes } from './uniqueProps.js';
|
|
15
16
|
/** Codes that block publishing but not draft saves. */
|
|
16
17
|
export const PUBLISH_ONLY_CODES = new Set(['required', 'format', 'constraint', 'validate', 'nesting', 'binding']);
|
|
17
18
|
/** True when the error never blocks a save (only logged or shown). */
|
|
@@ -41,11 +42,42 @@ export function validateLayout(layout, blocks, options = {}) {
|
|
|
41
42
|
const htmlIds = new Map();
|
|
42
43
|
// Inline blocks may go into `allowInline` slots, as in the editor, also with a site's own list.
|
|
43
44
|
const definitions = withInlineBlocks(blocks);
|
|
44
|
-
layout.blocks.forEach((block, i) => checkBlock(block, `blocks[${i}]`, null, [], definitions, seen, errors, localization, htmlIds));
|
|
45
|
+
layout.blocks.forEach((block, i) => checkBlock(block, `blocks[${i}]`, null, [], definitions, seen, errors, localization, htmlIds, []));
|
|
46
|
+
// Unique props (a form field's name) repeated in one scope. Publish-only: a copy may clash for a moment.
|
|
47
|
+
if (errors.every((e) => e.code !== 'invalid')) {
|
|
48
|
+
for (const clash of uniquePropClashes(layout, definitions)) {
|
|
49
|
+
errors.push({
|
|
50
|
+
blockId: clash.blockId,
|
|
51
|
+
path: `${pathOfBlock(layout, clash.blockId)}.props.${clash.prop}`,
|
|
52
|
+
message: `Another block here already uses the ${clash.prop} "${clash.value}". Each one must be different.`,
|
|
53
|
+
code: 'constraint',
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
45
57
|
return errors;
|
|
46
58
|
}
|
|
59
|
+
/** The data path of a block (`blocks[0].slots.children[2]`), or '' when it is not in the layout. */
|
|
60
|
+
function pathOfBlock(layout, id) {
|
|
61
|
+
const visit = (list, at) => {
|
|
62
|
+
for (let i = 0; i < list.length; i++) {
|
|
63
|
+
const block = list[i];
|
|
64
|
+
const here = `${at}[${i}]`;
|
|
65
|
+
if (block.id === id)
|
|
66
|
+
return here;
|
|
67
|
+
for (const [name, children] of Object.entries(block.slots ?? {})) {
|
|
68
|
+
const found = visit(children, `${here}.slots.${name}`);
|
|
69
|
+
if (found)
|
|
70
|
+
return found;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
};
|
|
75
|
+
return visit(layout.blocks, 'blocks') ?? '';
|
|
76
|
+
}
|
|
47
77
|
const nameOf = (value, def, type) => (typeof value.label === 'string' && value.label.trim()) || def?.label || type;
|
|
48
|
-
function checkBlock(value, path, owner, banned, blocks, seen, errors, localization, htmlIds
|
|
78
|
+
function checkBlock(value, path, owner, banned, blocks, seen, errors, localization, htmlIds,
|
|
79
|
+
/** The types of the blocks above this one, outermost first (`ancestors` rules). */
|
|
80
|
+
above) {
|
|
49
81
|
if (!isPlainObject(value)) {
|
|
50
82
|
errors.push({ path, message: 'Block must be an object', code: 'invalid' });
|
|
51
83
|
return;
|
|
@@ -79,6 +111,10 @@ function checkBlock(value, path, owner, banned, blocks, seen, errors, localizati
|
|
|
79
111
|
const parents = def.parents.map((t) => getBlockDefinition(blocks, t)?.label ?? t).join(' or ');
|
|
80
112
|
report(path, `${typeLabel} can only go inside ${parents}`, 'nesting');
|
|
81
113
|
}
|
|
114
|
+
if (type && def?.ancestors && !def.ancestors.some((t) => above.includes(t))) {
|
|
115
|
+
const ancestors = def.ancestors.map((t) => getBlockDefinition(blocks, t)?.label ?? t).join(' or ');
|
|
116
|
+
report(path, `${typeLabel} can only go inside ${ancestors}`, 'nesting');
|
|
117
|
+
}
|
|
82
118
|
const refusedBy = type ? banned.find((b) => b.type === type) : undefined;
|
|
83
119
|
if (refusedBy)
|
|
84
120
|
report(path, `${typeLabel} cannot go inside ${refusedBy.by}`, 'nesting');
|
|
@@ -178,7 +214,7 @@ function checkBlock(value, path, owner, banned, blocks, seen, errors, localizati
|
|
|
178
214
|
const label = nameOf(value, def, type ?? '');
|
|
179
215
|
const childOwner = slotDef && type ? { type, label, slot: name, def: slotDef } : null;
|
|
180
216
|
const childBanned = slotDef?.disallow?.length ? [...banned, ...slotDef.disallow.map((t) => ({ type: t, by: label }))] : banned;
|
|
181
|
-
children.forEach((child, i) => checkBlock(child, `${slotPath}[${i}]`, childOwner, childBanned, blocks, seen, errors, localization, htmlIds));
|
|
217
|
+
children.forEach((child, i) => checkBlock(child, `${slotPath}[${i}]`, childOwner, childBanned, blocks, seen, errors, localization, htmlIds, type ? [...above, type] : above));
|
|
182
218
|
}
|
|
183
219
|
}
|
|
184
220
|
/**
|
|
@@ -295,6 +331,10 @@ function checkFields(fields, data, path, report, extra = []) {
|
|
|
295
331
|
if (value === undefined || value === null)
|
|
296
332
|
continue;
|
|
297
333
|
}
|
|
334
|
+
// A group or array its condition hides is not checked inside: a leftover value there is never
|
|
335
|
+
// shown or used (Payload skips hidden fields too). A form's redirect group while it shows a message.
|
|
336
|
+
if ((field.type === 'group' || field.type === 'array') && !shown(field, data, list))
|
|
337
|
+
continue;
|
|
298
338
|
checkValue(field, value, at, report);
|
|
299
339
|
checkFormat(field, value, data, list, at, report);
|
|
300
340
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FormFieldLike, FormValue } from './conditions.js';
|
|
2
|
+
/** A form field with what its checks read. */
|
|
3
|
+
export type FormAnswerField = FormFieldLike & {
|
|
4
|
+
label?: string | null;
|
|
5
|
+
options?: ReadonlyArray<{
|
|
6
|
+
value?: string | null;
|
|
7
|
+
}> | null;
|
|
8
|
+
};
|
|
9
|
+
/** Field types whose answer is a tick (true or false). */
|
|
10
|
+
export declare const TICK_FIELD_TYPES: ReadonlySet<string>;
|
|
11
|
+
/** True for "+1 (555) 123-4567" and other phone numbers with 6 to 15 digits. */
|
|
12
|
+
export declare function isPhoneNumber(text: string): boolean;
|
|
13
|
+
/** True for "https://example.com/page" and "example.com". Only http and https. */
|
|
14
|
+
export declare function isWebAddress(text: string): boolean;
|
|
15
|
+
/** True for a real calendar date written as YYYY-MM-DD (the value of `<input type="date">`). */
|
|
16
|
+
export declare function isDateText(text: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The message for an answer, or '' when it is fine. `value` is what the visitor entered: text,
|
|
19
|
+
* or a boolean for a checkbox or consent box. A file field's value is the file name.
|
|
20
|
+
*/
|
|
21
|
+
export declare function formAnswerProblem(field: FormAnswerField, value: FormValue): string;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// The checks of one form answer: required, and the format of email, number, phone, web address,
|
|
2
|
+
// date, choice and file fields. Pure and client-safe: the site's form shows these messages under
|
|
3
|
+
// the field, and the submissions hook (`formSubmissionConditions`) runs the same checks again.
|
|
4
|
+
import { isEmptyFormValue } from './conditions.js';
|
|
5
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
6
|
+
/** Digits, spaces, "+", "-", ".", "(" and ")". At least 6 and at most 15 digits (E.164). */
|
|
7
|
+
const PHONE = /^\+?[\d\s().-]+$/;
|
|
8
|
+
const DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
9
|
+
/** Field types whose answer is a tick (true or false). */
|
|
10
|
+
export const TICK_FIELD_TYPES = new Set(['checkbox', 'consent']);
|
|
11
|
+
/** Field types that take no answer from the visitor. */
|
|
12
|
+
const NO_ANSWER = new Set(['message']);
|
|
13
|
+
/** True for "+1 (555) 123-4567" and other phone numbers with 6 to 15 digits. */
|
|
14
|
+
export function isPhoneNumber(text) {
|
|
15
|
+
if (!PHONE.test(text))
|
|
16
|
+
return false;
|
|
17
|
+
const digits = text.replace(/\D/g, '').length;
|
|
18
|
+
return digits >= 6 && digits <= 15;
|
|
19
|
+
}
|
|
20
|
+
/** True for "https://example.com/page" and "example.com". Only http and https. */
|
|
21
|
+
export function isWebAddress(text) {
|
|
22
|
+
if (/\s/.test(text))
|
|
23
|
+
return false;
|
|
24
|
+
const withScheme = /^[a-z][a-z\d+.-]*:/i.test(text) ? text : `https://${text}`;
|
|
25
|
+
try {
|
|
26
|
+
const url = new URL(withScheme);
|
|
27
|
+
return (url.protocol === 'https:' || url.protocol === 'http:') && /^[^.]+(\.[^.]+)+$/.test(url.hostname);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** True for a real calendar date written as YYYY-MM-DD (the value of `<input type="date">`). */
|
|
34
|
+
export function isDateText(text) {
|
|
35
|
+
const match = DATE.exec(text);
|
|
36
|
+
if (!match)
|
|
37
|
+
return false;
|
|
38
|
+
const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
39
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
40
|
+
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The message for an answer, or '' when it is fine. `value` is what the visitor entered: text,
|
|
44
|
+
* or a boolean for a checkbox or consent box. A file field's value is the file name.
|
|
45
|
+
*/
|
|
46
|
+
export function formAnswerProblem(field, value) {
|
|
47
|
+
const type = field.blockType ?? 'text';
|
|
48
|
+
if (NO_ANSWER.has(type))
|
|
49
|
+
return '';
|
|
50
|
+
const label = String(field.label || field.name || 'this field').toLowerCase();
|
|
51
|
+
if (TICK_FIELD_TYPES.has(type))
|
|
52
|
+
return field.required && value !== true ? 'Tick this box to continue.' : '';
|
|
53
|
+
const text = value === null || value === undefined || typeof value === 'boolean' ? '' : String(value).trim();
|
|
54
|
+
if (isEmptyFormValue(text)) {
|
|
55
|
+
if (!field.required)
|
|
56
|
+
return '';
|
|
57
|
+
if (type === 'select' || type === 'radio' || type === 'country' || type === 'state')
|
|
58
|
+
return `Choose a ${label}.`;
|
|
59
|
+
if (type === 'file')
|
|
60
|
+
return `Choose a file for ${label}.`;
|
|
61
|
+
return `Enter your ${label}.`;
|
|
62
|
+
}
|
|
63
|
+
switch (type) {
|
|
64
|
+
case 'email':
|
|
65
|
+
return EMAIL.test(text) ? '' : 'Enter an email address like name@example.com.';
|
|
66
|
+
case 'number':
|
|
67
|
+
return Number.isFinite(Number(text)) ? '' : `Enter a number for ${label}.`;
|
|
68
|
+
case 'phone':
|
|
69
|
+
return isPhoneNumber(text) ? '' : 'Enter a phone number, for example +1 555 123 4567.';
|
|
70
|
+
case 'url':
|
|
71
|
+
return isWebAddress(text) ? '' : 'Enter a web address like https://example.com.';
|
|
72
|
+
case 'date':
|
|
73
|
+
return isDateText(text) ? '' : 'Enter a date.';
|
|
74
|
+
case 'select':
|
|
75
|
+
case 'radio': {
|
|
76
|
+
const options = (field.options ?? []).map((o) => o?.value).filter((v) => typeof v === 'string');
|
|
77
|
+
return options.length === 0 || options.includes(text) ? '' : `Choose one of the options for ${label}.`;
|
|
78
|
+
}
|
|
79
|
+
default:
|
|
80
|
+
return '';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Block as PayloadBlockConfig } from 'payload';
|
|
2
|
+
import type { BlockDefinition, PartDefinition } from '../core/types.js';
|
|
3
|
+
/** `admin.custom.builderFieldName` on a field's `name`: the builder warns when a form with submissions renames it. */
|
|
4
|
+
export declare const FIELD_NAME_MARKER = "builderFieldName";
|
|
5
|
+
/** Prefix of the builder types: the form-builder block `text` is the builder type `formText`. */
|
|
6
|
+
export declare const FORM_FIELD_PREFIX = "form";
|
|
7
|
+
/** Name of the form settings region. The app's form frame marks the submit button with it. */
|
|
8
|
+
export declare const FORM_SETTINGS_REGION = "settings";
|
|
9
|
+
/** Library category of the form field blocks. */
|
|
10
|
+
export declare const FORM_FIELD_CATEGORY = "Fields";
|
|
11
|
+
/** The builder type of a form-builder field block: `text` -> `formText`. */
|
|
12
|
+
export declare function formFieldType(slug: string): string;
|
|
13
|
+
/** The form-builder block slug of a builder type: `formText` -> `text`. Null for other types. */
|
|
14
|
+
export declare function formFieldSlug(type: string): string | null;
|
|
15
|
+
/** The parts of a field block (its own classes win over the form's). */
|
|
16
|
+
export declare const FORM_FIELD_PARTS: {
|
|
17
|
+
readonly label: 'Label';
|
|
18
|
+
readonly input: 'Input';
|
|
19
|
+
readonly help: 'Help text';
|
|
20
|
+
readonly error: 'Error message';
|
|
21
|
+
readonly options: 'Options';
|
|
22
|
+
};
|
|
23
|
+
export type FormFieldPartName = keyof typeof FORM_FIELD_PARTS;
|
|
24
|
+
/** The `parts` of a field block of the plugin's type `slug`. Undefined when it has none. */
|
|
25
|
+
export declare function formFieldParts(slug: string): Record<string, PartDefinition> | undefined;
|
|
26
|
+
/** The classes of the old width presets: the generated CSS has them wherever form fields are. */
|
|
27
|
+
export declare const FORM_FIELD_CLASSES: string[];
|
|
28
|
+
/**
|
|
29
|
+
* One definition per block of the form's `fields` blocks field. Types get the `form` prefix
|
|
30
|
+
* (`formText`), so they never clash with page blocks; the stored data keeps the plugin's
|
|
31
|
+
* `blockType`. The plugin's `forms` option calls this with the forms collection's own blocks.
|
|
32
|
+
*/
|
|
33
|
+
export declare function formFieldBlocks(blocks: readonly PayloadBlockConfig[]): BlockDefinition[];
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Builder block definitions for the field blocks of a form-builder form (`forms.fields`).
|
|
2
|
+
// Client-safe: Payload block configs in, plain definitions out.
|
|
3
|
+
import { fromPayloadBlocks } from '../blocks/payload.js';
|
|
4
|
+
import { FORM_CONDITIONS_FIELD } from './conditions.js';
|
|
5
|
+
import { FORM_FIELD_WIDTH_CLASSES, withHiddenWidth, withWidthClass } from './fieldSettings.js';
|
|
6
|
+
/** `admin.custom.builderFieldName` on a field's `name`: the builder warns when a form with submissions renames it. */
|
|
7
|
+
export const FIELD_NAME_MARKER = 'builderFieldName';
|
|
8
|
+
/** Prefix of the builder types: the form-builder block `text` is the builder type `formText`. */
|
|
9
|
+
export const FORM_FIELD_PREFIX = 'form';
|
|
10
|
+
/** Name of the form settings region. The app's form frame marks the submit button with it. */
|
|
11
|
+
export const FORM_SETTINGS_REGION = 'settings';
|
|
12
|
+
/** Library category of the form field blocks. */
|
|
13
|
+
export const FORM_FIELD_CATEGORY = 'Fields';
|
|
14
|
+
/** The builder type of a form-builder field block: `text` -> `formText`. */
|
|
15
|
+
export function formFieldType(slug) {
|
|
16
|
+
return `${FORM_FIELD_PREFIX}${slug.charAt(0).toUpperCase()}${slug.slice(1)}`;
|
|
17
|
+
}
|
|
18
|
+
/** The form-builder block slug of a builder type: `formText` -> `text`. Null for other types. */
|
|
19
|
+
export function formFieldSlug(type) {
|
|
20
|
+
if (!type.startsWith(FORM_FIELD_PREFIX) || type.length === FORM_FIELD_PREFIX.length)
|
|
21
|
+
return null;
|
|
22
|
+
const rest = type.slice(FORM_FIELD_PREFIX.length);
|
|
23
|
+
return rest.charAt(0) === rest.charAt(0).toUpperCase() ? `${rest.charAt(0).toLowerCase()}${rest.slice(1)}` : null;
|
|
24
|
+
}
|
|
25
|
+
/** Labels and library tooltips of the known field types. Other blocks keep their own. */
|
|
26
|
+
const KNOWN = {
|
|
27
|
+
text: { label: 'Text', icon: 'text', description: 'A one-line answer, such as a name.' },
|
|
28
|
+
email: { label: 'Email', icon: 'mail', description: 'An email address. The form checks its format.' },
|
|
29
|
+
textarea: { label: 'Long text', icon: 'richText', description: 'A longer answer over several lines, such as a message.' },
|
|
30
|
+
number: { label: 'Number', icon: 'hash', description: 'A number.' },
|
|
31
|
+
phone: { label: 'Phone', icon: 'phone', description: 'A phone number. The form checks that it has 6 to 15 digits.' },
|
|
32
|
+
url: { label: 'Web address', icon: 'link', description: 'A web address, such as https://example.com. The form checks its format.' },
|
|
33
|
+
select: { label: 'Dropdown', icon: 'select', description: 'One choice from a list of options.' },
|
|
34
|
+
radio: { label: 'Radio buttons', icon: 'radio', description: 'One choice from a few options, all shown at once.' },
|
|
35
|
+
checkbox: { label: 'Checkbox', icon: 'check', description: 'A box to tick, for example to join the newsletter.' },
|
|
36
|
+
consent: { label: 'Consent', icon: 'check', description: 'A box to tick next to text with a link, for example "I accept the privacy policy".' },
|
|
37
|
+
country: { label: 'Country', icon: 'select', description: 'A list of countries.' },
|
|
38
|
+
state: { label: 'State', icon: 'select', description: 'A list of US states.' },
|
|
39
|
+
date: { label: 'Date', icon: 'calendar', description: 'A date, picked from a calendar.' },
|
|
40
|
+
file: { label: 'File upload', icon: 'upload', description: 'A file the visitor uploads, such as a CV. You set the file types and the size limit.' },
|
|
41
|
+
hidden: { label: 'Hidden field', icon: 'eyeOff', description: 'A value sent with every answer that visitors do not see, such as the campaign name.' },
|
|
42
|
+
message: { label: 'Message', icon: 'richText', description: 'Text between the fields, such as a note. Visitors do not fill it in.' },
|
|
43
|
+
upload: { label: 'File upload', icon: 'upload', description: 'A file the visitor uploads.' },
|
|
44
|
+
payment: { label: 'Payment', icon: 'block', description: 'A payment field.' },
|
|
45
|
+
};
|
|
46
|
+
/** The parts of a field block (its own classes win over the form's). */
|
|
47
|
+
export const FORM_FIELD_PARTS = {
|
|
48
|
+
label: 'Label',
|
|
49
|
+
input: 'Input',
|
|
50
|
+
help: 'Help text',
|
|
51
|
+
error: 'Error message',
|
|
52
|
+
options: 'Options',
|
|
53
|
+
};
|
|
54
|
+
/** The parts each field type has. Types not listed have label, input, help text and error. */
|
|
55
|
+
const FIELD_PARTS_BY_SLUG = {
|
|
56
|
+
message: [],
|
|
57
|
+
hidden: [],
|
|
58
|
+
checkbox: ['label', 'help', 'error'],
|
|
59
|
+
consent: ['label', 'help', 'error'],
|
|
60
|
+
radio: ['label', 'options', 'help', 'error'],
|
|
61
|
+
file: ['label', 'help', 'error'],
|
|
62
|
+
};
|
|
63
|
+
/** The `parts` of a field block of the plugin's type `slug`. Undefined when it has none. */
|
|
64
|
+
export function formFieldParts(slug) {
|
|
65
|
+
const names = FIELD_PARTS_BY_SLUG[slug] ?? ['label', 'input', 'help', 'error'];
|
|
66
|
+
if (names.length === 0)
|
|
67
|
+
return undefined;
|
|
68
|
+
// Until the field sets its own classes, a part uses the same part of its Form block (options have none there).
|
|
69
|
+
return Object.fromEntries(names.map((name) => [name, { label: FORM_FIELD_PARTS[name], ...(name === 'options' ? {} : { inherits: { ancestor: 'form' } }) }]));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The tab of a form field's settings, by field name. Other fields stay on the Field tab (content,
|
|
73
|
+
* label visibility, radio display). Looks are no setting: the Styles panel sets them, the width too.
|
|
74
|
+
*/
|
|
75
|
+
const TAB_OF = {
|
|
76
|
+
required: 'Validation',
|
|
77
|
+
accept: 'Validation',
|
|
78
|
+
maxSize: 'Validation',
|
|
79
|
+
multiple: 'Validation',
|
|
80
|
+
maxFiles: 'Validation',
|
|
81
|
+
uploadCollection: 'Validation',
|
|
82
|
+
mimeTypes: 'Validation',
|
|
83
|
+
maxFileSize: 'Validation',
|
|
84
|
+
[FORM_CONDITIONS_FIELD]: 'Conditions',
|
|
85
|
+
};
|
|
86
|
+
const TAB_ORDER = ['Validation', 'Conditions'];
|
|
87
|
+
/** The classes of the old width presets: the generated CSS has them wherever form fields are. */
|
|
88
|
+
export const FORM_FIELD_CLASSES = Object.values(FORM_FIELD_WIDTH_CLASSES);
|
|
89
|
+
/**
|
|
90
|
+
* One definition per block of the form's `fields` blocks field. Types get the `form` prefix
|
|
91
|
+
* (`formText`), so they never clash with page blocks; the stored data keeps the plugin's
|
|
92
|
+
* `blockType`. The plugin's `forms` option calls this with the forms collection's own blocks.
|
|
93
|
+
*/
|
|
94
|
+
export function formFieldBlocks(blocks) {
|
|
95
|
+
const overrides = Object.fromEntries(blocks.map((block) => {
|
|
96
|
+
const known = KNOWN[block.slug];
|
|
97
|
+
return [
|
|
98
|
+
block.slug,
|
|
99
|
+
{
|
|
100
|
+
category: FORM_FIELD_CATEGORY,
|
|
101
|
+
...(known ? { label: known.label, icon: known.icon } : {}),
|
|
102
|
+
ai: {
|
|
103
|
+
description: `${known?.label ?? block.slug} field of a form (form-builder "${block.slug}" block). On a page it goes inside a Form block (at any depth); \`name\` is unique in that form.`,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
];
|
|
107
|
+
}));
|
|
108
|
+
return fromPayloadBlocks(blocks, { prefix: FORM_FIELD_PREFIX, overrides, onWarning: false }).map((def) => {
|
|
109
|
+
const slug = def.payload?.slug ?? '';
|
|
110
|
+
const known = KNOWN[slug];
|
|
111
|
+
// A new field starts with a name and a label, so the form saves at once (the plugin requires
|
|
112
|
+
// a name). Only the builder's insert uses these defaults; the stored data is unchanged.
|
|
113
|
+
const fields = inTabs(withHiddenWidth(withNameMarker(withStartValues(def.fields, { name: slug, label: known?.label ?? def.label }))));
|
|
114
|
+
// Two fields with one name would overwrite each other's answers: a new or copied field gets `text2`.
|
|
115
|
+
// Parts: the label, input, help text, error and options take classes of their own (Styles panel).
|
|
116
|
+
// A stored `width` (the plugin's setting) becomes classes when the builder opens the form; until
|
|
117
|
+
// then the site draws it with the same classes, so the generated CSS lists them.
|
|
118
|
+
const parts = formFieldParts(slug);
|
|
119
|
+
return {
|
|
120
|
+
...def,
|
|
121
|
+
fields,
|
|
122
|
+
uniqueProps: ['name'],
|
|
123
|
+
classes: [...(def.classes ?? []), ...FORM_FIELD_CLASSES],
|
|
124
|
+
upgrade: withWidthClass,
|
|
125
|
+
...(parts ? { parts } : {}),
|
|
126
|
+
...(known ? { description: known.description } : {}),
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** The fields with `defaultValue` set on the top-level `name` and `label` fields (also inside rows) that have none. */
|
|
131
|
+
function withStartValues(fields, values) {
|
|
132
|
+
return fields.map((raw) => {
|
|
133
|
+
const field = raw;
|
|
134
|
+
if (Array.isArray(field.fields) && !field.name)
|
|
135
|
+
return { ...field, fields: withStartValues(field.fields, values) };
|
|
136
|
+
const name = typeof field.name === 'string' ? field.name : '';
|
|
137
|
+
if (name in values && field.defaultValue === undefined)
|
|
138
|
+
return { ...field, defaultValue: values[name] };
|
|
139
|
+
return raw;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/** The fields with the top-level `name` text (also inside rows) marked for the rename warning. */
|
|
143
|
+
function withNameMarker(fields) {
|
|
144
|
+
return fields.map((raw) => {
|
|
145
|
+
const field = raw;
|
|
146
|
+
if (Array.isArray(field.fields) && !field.name)
|
|
147
|
+
return { ...field, fields: withNameMarker(field.fields) };
|
|
148
|
+
if (field.name !== 'name' || field.type !== 'text')
|
|
149
|
+
return raw;
|
|
150
|
+
const admin = (field.admin ?? {});
|
|
151
|
+
return { ...field, admin: { ...admin, custom: { ...admin.custom, [FIELD_NAME_MARKER]: true } } };
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The fields split over the tabs Field, Validation and Conditions (unnamed tabs: the
|
|
156
|
+
* data stays as it is). Fields leave their rows; an empty row goes. A tab without fields is left
|
|
157
|
+
* out; with only the Field tab the list stays as it is.
|
|
158
|
+
*/
|
|
159
|
+
function inTabs(fields) {
|
|
160
|
+
const moved = { Validation: [], Conditions: [] };
|
|
161
|
+
const pull = (list) => list.flatMap((raw) => {
|
|
162
|
+
const field = raw;
|
|
163
|
+
if (Array.isArray(field.fields) && !field.name) {
|
|
164
|
+
const kept = pull(field.fields);
|
|
165
|
+
return kept.length > 0 ? [{ ...field, fields: kept }] : [];
|
|
166
|
+
}
|
|
167
|
+
const tab = typeof field.name === 'string' ? TAB_OF[field.name] : undefined;
|
|
168
|
+
if (!tab)
|
|
169
|
+
return [raw];
|
|
170
|
+
moved[tab].push(raw);
|
|
171
|
+
return [];
|
|
172
|
+
});
|
|
173
|
+
const main = pull(fields);
|
|
174
|
+
const others = TAB_ORDER.filter((label) => moved[label].length > 0);
|
|
175
|
+
if (others.length === 0)
|
|
176
|
+
return fields;
|
|
177
|
+
return [{ type: 'tabs', tabs: [{ label: 'Field', fields: main }, ...others.map((label) => ({ label, fields: moved[label] }))] }];
|
|
178
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Field } from 'payload';
|
|
2
|
+
export declare const FORM_CONDITION_OPERATORS: readonly ['equals', 'notEquals', 'contains', 'isEmpty', 'isNotEmpty'];
|
|
3
|
+
export type FormConditionOperator = (typeof FORM_CONDITION_OPERATORS)[number];
|
|
4
|
+
/** One test: the value of the form field named `field` against `value`. */
|
|
5
|
+
export type FormConditionRule = {
|
|
6
|
+
field?: string | null;
|
|
7
|
+
operator?: FormConditionOperator | null;
|
|
8
|
+
value?: string | null;
|
|
9
|
+
};
|
|
10
|
+
/** `match: 'all'` (the default): every rule must hold. `'any'`: one rule is enough. */
|
|
11
|
+
export type FormFieldConditions = {
|
|
12
|
+
match?: 'all' | 'any' | null;
|
|
13
|
+
rules?: FormConditionRule[] | null;
|
|
14
|
+
};
|
|
15
|
+
/** A field of a form document, as the form-builder plugin stores it. */
|
|
16
|
+
export type FormFieldLike = {
|
|
17
|
+
blockType?: string;
|
|
18
|
+
name?: string | null;
|
|
19
|
+
required?: boolean | null;
|
|
20
|
+
conditions?: FormFieldConditions | null;
|
|
21
|
+
};
|
|
22
|
+
/** A value a visitor entered. Checkboxes give booleans, other fields text. */
|
|
23
|
+
export type FormValue = string | number | boolean | null | undefined;
|
|
24
|
+
/** Name of the conditions field in every form field block. */
|
|
25
|
+
export declare const FORM_CONDITIONS_FIELD = "conditions";
|
|
26
|
+
/** Empty: no value, blank text, or a checkbox that is not ticked. */
|
|
27
|
+
export declare function isEmptyFormValue(value: FormValue): boolean;
|
|
28
|
+
/** A rule that names a field and an operator (and a value, when the operator needs one). */
|
|
29
|
+
export declare function isCompleteRule(rule: FormConditionRule | null | undefined): rule is FormConditionRule & {
|
|
30
|
+
field: string;
|
|
31
|
+
operator: FormConditionOperator;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* True when the rule holds for `value`. Text compares without case and surrounding spaces; a
|
|
35
|
+
* ticked checkbox equals "true" (also "yes", "on" or "1"), an unticked one "false".
|
|
36
|
+
*/
|
|
37
|
+
export declare function ruleMet(rule: FormConditionRule & {
|
|
38
|
+
operator: FormConditionOperator;
|
|
39
|
+
}, value: FormValue): boolean;
|
|
40
|
+
/** The complete rules of a field. A field without complete rules always shows. */
|
|
41
|
+
export declare function conditionRules(field: FormFieldLike): Array<FormConditionRule & {
|
|
42
|
+
field: string;
|
|
43
|
+
operator: FormConditionOperator;
|
|
44
|
+
}>;
|
|
45
|
+
/** True when the field shows for these values (the values of hidden fields left out). */
|
|
46
|
+
export declare function conditionsMet(field: FormFieldLike, values: Record<string, FormValue>): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The names of the fields their conditions hide. A rule that reads a hidden field sees it as
|
|
49
|
+
* empty, so a chain (C depends on B, B on A) hides C when A hides B. Rules that form a loop
|
|
50
|
+
* (A depends on B, B on A) read the entered values. Fields without a name (messages) are never
|
|
51
|
+
* in the set.
|
|
52
|
+
*/
|
|
53
|
+
export declare function hiddenFormFields(fields: readonly FormFieldLike[] | null | undefined, values: Record<string, FormValue>): Set<string>;
|
|
54
|
+
/** True when the field shows: it has no name (a message) or no condition hides it. */
|
|
55
|
+
export declare function isFormFieldShown(field: FormFieldLike, hidden: ReadonlySet<string>): boolean;
|
|
56
|
+
/** One line for editors: "Shows when Topic equals Sales and Email is not empty". */
|
|
57
|
+
export declare function describeConditions(field: FormFieldLike, labelOf?: (name: string) => string): string | null;
|
|
58
|
+
/**
|
|
59
|
+
* The `conditions` field for a form field block. Add it to the blocks of the form-builder
|
|
60
|
+
* plugin's `fields` option with `withFormConditions`. The builder edits it in its right panel;
|
|
61
|
+
* Payload's edit screen shows it as a group.
|
|
62
|
+
*
|
|
63
|
+
* `admin.custom.builderFormField` makes the builder show the rule's field as a list of the
|
|
64
|
+
* form's other fields. `admin.custom.builderCondition` hides the value for "is empty" and
|
|
65
|
+
* "is not empty" in the builder; `admin.condition` does the same in Payload's edit screen.
|
|
66
|
+
*/
|
|
67
|
+
export declare function formConditionsField(): Field;
|
|
68
|
+
type BlockLike = {
|
|
69
|
+
slug: string;
|
|
70
|
+
fields: Field[];
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* The block with the `conditions` field added, for the form-builder plugin's `fields` option:
|
|
74
|
+
* `formBuilderPlugin({ fields: { text: withFormConditions(fields.text) } })`, with `fields` from
|
|
75
|
+
* `@payloadcms/plugin-form-builder`. A block that has the field already stays as it is.
|
|
76
|
+
*/
|
|
77
|
+
export declare function withFormConditions<T extends BlockLike>(block: T): T;
|
|
78
|
+
export {};
|