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.
Files changed (325) hide show
  1. package/README.md +350 -45
  2. package/dist/admin/AiKeyField.d.ts +4 -3
  3. package/dist/admin/AiKeyField.js +3 -3
  4. package/dist/admin/ArrayRowLabel.d.ts +12 -0
  5. package/dist/admin/ArrayRowLabel.js +21 -0
  6. package/dist/admin/BuilderTab.js +1 -1
  7. package/dist/admin/DocumentEditView.d.ts +1 -1
  8. package/dist/admin/DocumentEditView.js +2 -3
  9. package/dist/admin/LayoutField.d.ts +1 -1
  10. package/dist/admin/LayoutField.js +2 -2
  11. package/dist/admin/TemplateConditionsField.d.ts +2 -2
  12. package/dist/admin/TemplateConditionsField.js +2 -2
  13. package/dist/admin/aiKeyField.css +58 -0
  14. package/dist/admin/client.d.ts +1 -0
  15. package/dist/admin/client.js +2 -0
  16. package/dist/admin/diff/LayoutDiff.js +3 -3
  17. package/dist/admin/diff/LayoutJsonDiff.d.ts +1 -1
  18. package/dist/admin/diff/LayoutJsonDiff.js +1 -1
  19. package/dist/admin/diff/layoutDiff.css +180 -0
  20. package/dist/admin/editor/Editor.d.ts +2 -2
  21. package/dist/admin/editor/Editor.js +10 -3
  22. package/dist/admin/editor/Inspector.js +23 -17
  23. package/dist/admin/editor/Library.d.ts +2 -2
  24. package/dist/admin/editor/Library.js +8 -7
  25. package/dist/admin/editor/Outline.js +20 -12
  26. package/dist/admin/editor/Overlay.js +6 -4
  27. package/dist/admin/editor/actions.d.ts +7 -2
  28. package/dist/admin/editor/actions.js +44 -12
  29. package/dist/admin/editor/assistant/AssistantPanel.d.ts +1 -1
  30. package/dist/admin/editor/assistant/AssistantPanel.js +33 -21
  31. package/dist/admin/editor/assistant/{assistant.scss → assistant.css} +322 -315
  32. package/dist/admin/editor/classes/ClassManager.d.ts +1 -1
  33. package/dist/admin/editor/classes/ClassManager.js +1 -1
  34. package/dist/admin/editor/classes/manager.css +258 -0
  35. package/dist/admin/editor/comments/CommentLayer.d.ts +1 -1
  36. package/dist/admin/editor/comments/CommentLayer.js +1 -1
  37. package/dist/admin/editor/comments/CommentsPanel.d.ts +1 -1
  38. package/dist/admin/editor/comments/CommentsPanel.js +1 -1
  39. package/dist/admin/editor/comments/{comments.scss → comments.css} +121 -140
  40. package/dist/admin/editor/comments/controller.d.ts +7 -0
  41. package/dist/admin/editor/comments/controller.js +12 -0
  42. package/dist/admin/editor/comments/useCommentFromURL.d.ts +2 -0
  43. package/dist/admin/editor/comments/useCommentFromURL.js +36 -0
  44. package/dist/admin/editor/dnd/{dnd.scss → dnd.css} +78 -80
  45. package/dist/admin/editor/dnd/smooth.js +1 -1
  46. package/dist/admin/editor/{editor.scss → editor.css} +798 -751
  47. package/dist/admin/editor/empty/EmptyStart.d.ts +1 -1
  48. package/dist/admin/editor/empty/EmptyStart.js +3 -3
  49. package/dist/admin/editor/empty/{empty.scss → empty.css} +12 -13
  50. package/dist/admin/editor/fields/ArrayField.js +3 -3
  51. package/dist/admin/editor/fields/FormBlock.d.ts +20 -0
  52. package/dist/admin/editor/fields/FormBlock.js +167 -0
  53. package/dist/admin/editor/fields/FormFieldName.d.ts +25 -0
  54. package/dist/admin/editor/fields/FormFieldName.js +58 -0
  55. package/dist/admin/editor/fields/GroupField.d.ts +22 -2
  56. package/dist/admin/editor/fields/GroupField.js +30 -1
  57. package/dist/admin/editor/fields/MergeTags.d.ts +8 -0
  58. package/dist/admin/editor/fields/MergeTags.js +62 -0
  59. package/dist/admin/editor/fields/arrayRows.d.ts +0 -2
  60. package/dist/admin/editor/fields/arrayRows.js +0 -4
  61. package/dist/admin/editor/fields/fields.css +786 -0
  62. package/dist/admin/editor/fields/values.d.ts +0 -2
  63. package/dist/admin/editor/fields/values.js +0 -14
  64. package/dist/admin/editor/generate/GenerateImage.d.ts +1 -1
  65. package/dist/admin/editor/generate/GenerateImage.js +1 -1
  66. package/dist/admin/editor/generate/{generate.scss → generate.css} +83 -82
  67. package/dist/admin/editor/icons.d.ts +3 -0
  68. package/dist/admin/editor/icons.js +3 -0
  69. package/dist/admin/editor/insert/InsertHandle.js +1 -1
  70. package/dist/admin/editor/insert/picker.d.ts +3 -2
  71. package/dist/admin/editor/insert/picker.js +9 -3
  72. package/dist/admin/editor/layout/LeftPanel.js +5 -3
  73. package/dist/admin/editor/layout/layout.css +243 -0
  74. package/dist/admin/editor/layout/leftTabs.d.ts +15 -0
  75. package/dist/admin/editor/layout/leftTabs.js +18 -0
  76. package/dist/admin/editor/layout/panels.js +1 -1
  77. package/dist/admin/editor/live/PresenceUI.d.ts +1 -1
  78. package/dist/admin/editor/live/PresenceUI.js +2 -2
  79. package/dist/admin/editor/live/{presence.scss → presence.css} +5 -4
  80. package/dist/admin/editor/locale/LocaleField.d.ts +1 -1
  81. package/dist/admin/editor/locale/LocaleField.js +1 -1
  82. package/dist/admin/editor/locale/LocaleSwitcher.d.ts +1 -1
  83. package/dist/admin/editor/locale/LocaleSwitcher.js +1 -1
  84. package/dist/admin/editor/locale/locale.css +153 -0
  85. package/dist/admin/editor/media/ImageEditor.d.ts +9 -3
  86. package/dist/admin/editor/media/ImageEditor.js +17 -11
  87. package/dist/admin/editor/media/{media.scss → media.css} +70 -60
  88. package/dist/admin/editor/media/model.d.ts +10 -1
  89. package/dist/admin/editor/media/model.js +19 -0
  90. package/dist/admin/editor/menu/Menu.d.ts +1 -1
  91. package/dist/admin/editor/menu/Menu.js +1 -1
  92. package/dist/admin/editor/menu/blockMenu.js +3 -3
  93. package/dist/admin/editor/menu/menu.css +132 -0
  94. package/dist/admin/editor/motion/MotionPanel.d.ts +2 -2
  95. package/dist/admin/editor/motion/MotionPanel.js +2 -2
  96. package/dist/admin/editor/motion/{motion.scss → motion.css} +24 -21
  97. package/dist/admin/editor/names.d.ts +9 -0
  98. package/dist/admin/editor/names.js +18 -0
  99. package/dist/admin/editor/regions/RegionFields.d.ts +6 -0
  100. package/dist/admin/editor/regions/RegionFields.js +122 -0
  101. package/dist/admin/editor/regions/RegionPane.js +5 -3
  102. package/dist/admin/editor/regions/RegionRows.js +2 -2
  103. package/dist/admin/editor/regions/required.d.ts +16 -0
  104. package/dist/admin/editor/regions/required.js +43 -0
  105. package/dist/admin/editor/renderField.d.ts +10 -3
  106. package/dist/admin/editor/renderField.js +87 -33
  107. package/dist/admin/editor/runtime.d.ts +3 -2
  108. package/dist/admin/editor/runtime.js +12 -18
  109. package/dist/admin/editor/sections/ComponentFields.d.ts +1 -1
  110. package/dist/admin/editor/sections/ComponentFields.js +2 -3
  111. package/dist/admin/editor/sections/PropertyControls.d.ts +1 -1
  112. package/dist/admin/editor/sections/PropertyControls.js +1 -1
  113. package/dist/admin/editor/sections/components.css +244 -0
  114. package/dist/admin/editor/sections/variants.css +93 -0
  115. package/dist/admin/editor/sections/variants.d.ts +1 -1
  116. package/dist/admin/editor/sections/variants.js +1 -1
  117. package/dist/admin/editor/styles/ClassBar.d.ts +1 -1
  118. package/dist/admin/editor/styles/ClassBar.js +1 -1
  119. package/dist/admin/editor/styles/StylesPanel.d.ts +1 -1
  120. package/dist/admin/editor/styles/StylesPanel.js +5 -3
  121. package/dist/admin/editor/styles/{classes.scss → classes.css} +128 -125
  122. package/dist/admin/editor/styles/context.js +9 -4
  123. package/dist/admin/editor/styles/{styles.scss → styles.css} +368 -370
  124. package/dist/admin/editor/templates/Bindable.d.ts +1 -1
  125. package/dist/admin/editor/templates/Bindable.js +1 -1
  126. package/dist/admin/editor/templates/binding.js +4 -1
  127. package/dist/admin/editor/templates/{templates.scss → templates.css} +207 -206
  128. package/dist/admin/editor/tooltip/Tooltips.d.ts +1 -1
  129. package/dist/admin/editor/tooltip/Tooltips.js +1 -1
  130. package/dist/admin/editor/tooltip/{tooltip.scss → tooltip.css} +11 -10
  131. package/dist/admin/editor/topbar/ContentModeToggle.d.ts +1 -1
  132. package/dist/admin/editor/topbar/ContentModeToggle.js +2 -2
  133. package/dist/admin/editor/topbar/DocumentActions.js +5 -5
  134. package/dist/admin/editor/topbar/TopBar.d.ts +1 -1
  135. package/dist/admin/editor/topbar/TopBar.js +2 -3
  136. package/dist/admin/editor/topbar/contentMode.css +25 -0
  137. package/dist/admin/editor/topbar/screens/DrawerRouter.d.ts +1 -1
  138. package/dist/admin/editor/topbar/screens/DrawerRouter.js +52 -40
  139. package/dist/admin/editor/topbar/screens/ScreenDrawer.d.ts +1 -1
  140. package/dist/admin/editor/topbar/screens/ScreenDrawer.js +4 -5
  141. package/dist/admin/editor/topbar/screens/drawerRouterSupport.d.ts +5 -5
  142. package/dist/admin/editor/topbar/screens/drawerRouterSupport.js +7 -7
  143. package/dist/admin/editor/topbar/screens/screens.css +139 -0
  144. package/dist/admin/editor/topbar/settingsDrawer.css +33 -0
  145. package/dist/admin/editor/topbar/settingsDrawer.d.ts +1 -1
  146. package/dist/admin/editor/topbar/settingsDrawer.js +3 -3
  147. package/dist/admin/editor/topbar/{topbar.scss → topbar.css} +189 -231
  148. package/dist/admin/editor/ui/Select.d.ts +1 -1
  149. package/dist/admin/editor/ui/Select.js +1 -1
  150. package/dist/admin/editor/ui/Slider.d.ts +1 -1
  151. package/dist/admin/editor/ui/Slider.js +1 -1
  152. package/dist/admin/editor/ui/Tabs.d.ts +17 -0
  153. package/dist/admin/editor/ui/Tabs.js +17 -0
  154. package/dist/admin/editor/ui/controls.css +301 -0
  155. package/dist/admin/forms/SubmissionCells.d.ts +6 -0
  156. package/dist/admin/forms/SubmissionCells.js +31 -0
  157. package/dist/admin/forms/SubmissionView.d.ts +5 -0
  158. package/dist/admin/forms/SubmissionView.js +106 -0
  159. package/dist/admin/forms/submissionData.d.ts +32 -0
  160. package/dist/admin/forms/submissionData.js +63 -0
  161. package/dist/admin/forms/submissions.css +200 -0
  162. package/dist/admin/layoutField.css +70 -0
  163. package/dist/admin/rsc.d.ts +2 -0
  164. package/dist/admin/rsc.js +5 -0
  165. package/dist/admin/screen/BuilderScreen.d.ts +1 -1
  166. package/dist/admin/screen/BuilderScreen.js +1 -1
  167. package/dist/admin/templateConditions.css +25 -0
  168. package/dist/ai/endpoint.d.ts +5 -0
  169. package/dist/ai/endpoint.js +18 -8
  170. package/dist/ai/images/service.js +2 -2
  171. package/dist/ai/prompt.d.ts +8 -0
  172. package/dist/ai/prompt.js +30 -0
  173. package/dist/ai/settings/index.js +3 -0
  174. package/dist/ai/tools.d.ts +6 -1
  175. package/dist/ai/tools.js +90 -6
  176. package/dist/blocks/builtins/carousel.js +10 -1
  177. package/dist/blocks/index.d.ts +1 -1
  178. package/dist/blocks/index.js +1 -1
  179. package/dist/blocks/payload.d.ts +7 -0
  180. package/dist/blocks/payload.js +16 -2
  181. package/dist/classes/collection.js +1 -0
  182. package/dist/classes/sheet.js +2 -0
  183. package/dist/comments/collection.js +3 -0
  184. package/dist/comments/shared.d.ts +5 -0
  185. package/dist/comments/shared.js +5 -0
  186. package/dist/core/blocks.d.ts +17 -3
  187. package/dist/core/blocks.js +92 -10
  188. package/dist/core/classes.d.ts +5 -0
  189. package/dist/core/classes.js +26 -1
  190. package/dist/core/conditions.d.ts +7 -0
  191. package/dist/core/conditions.js +22 -0
  192. package/dist/core/dropTarget.d.ts +3 -2
  193. package/dist/core/dropTarget.js +7 -1
  194. package/dist/core/fieldHooks.d.ts +1 -1
  195. package/dist/core/index.d.ts +6 -3
  196. package/dist/core/index.js +6 -3
  197. package/dist/core/payloadBlocks.js +25 -3
  198. package/dist/core/references.d.ts +7 -0
  199. package/dist/core/references.js +29 -11
  200. package/dist/core/regions.d.ts +14 -0
  201. package/dist/core/regions.js +17 -1
  202. package/dist/core/rowLabel.d.ts +8 -0
  203. package/dist/core/rowLabel.js +98 -0
  204. package/dist/core/starter.d.ts +10 -0
  205. package/dist/core/starter.js +40 -0
  206. package/dist/core/tree.js +4 -0
  207. package/dist/core/types.d.ts +96 -0
  208. package/dist/core/uniqueProps.d.ts +40 -0
  209. package/dist/core/uniqueProps.js +156 -0
  210. package/dist/core/validate.js +43 -3
  211. package/dist/forms/answers.d.ts +21 -0
  212. package/dist/forms/answers.js +82 -0
  213. package/dist/forms/blocks.d.ts +33 -0
  214. package/dist/forms/blocks.js +178 -0
  215. package/dist/forms/conditions.d.ts +78 -0
  216. package/dist/forms/conditions.js +184 -0
  217. package/dist/forms/emails.d.ts +23 -0
  218. package/dist/forms/emails.js +64 -0
  219. package/dist/forms/fieldConfigs.d.ts +35 -0
  220. package/dist/forms/fieldConfigs.js +167 -0
  221. package/dist/forms/fieldOps.d.ts +78 -0
  222. package/dist/forms/fieldOps.js +378 -0
  223. package/dist/forms/fieldSettings.d.ts +81 -0
  224. package/dist/forms/fieldSettings.js +239 -0
  225. package/dist/forms/files.d.ts +54 -0
  226. package/dist/forms/files.js +128 -0
  227. package/dist/forms/index.d.ts +16 -0
  228. package/dist/forms/index.js +18 -0
  229. package/dist/forms/mergeTags.d.ts +103 -0
  230. package/dist/forms/mergeTags.js +150 -0
  231. package/dist/forms/noScript.d.ts +60 -0
  232. package/dist/forms/noScript.js +109 -0
  233. package/dist/forms/pageForms.d.ts +99 -0
  234. package/dist/forms/pageForms.js +294 -0
  235. package/dist/forms/pageSync.d.ts +91 -0
  236. package/dist/forms/pageSync.js +325 -0
  237. package/dist/forms/rateLimit.d.ts +21 -0
  238. package/dist/forms/rateLimit.js +50 -0
  239. package/dist/forms/settings.d.ts +13 -0
  240. package/dist/forms/settings.js +92 -0
  241. package/dist/forms/spam.d.ts +100 -0
  242. package/dist/forms/spam.js +218 -0
  243. package/dist/forms/spamSettings.d.ts +43 -0
  244. package/dist/forms/spamSettings.js +25 -0
  245. package/dist/forms/submission.d.ts +34 -0
  246. package/dist/forms/submission.js +89 -0
  247. package/dist/forms/submissionsAdmin.d.ts +26 -0
  248. package/dist/forms/submissionsAdmin.js +108 -0
  249. package/dist/forms/summary.d.ts +60 -0
  250. package/dist/forms/summary.js +113 -0
  251. package/dist/forms/uploads.d.ts +41 -0
  252. package/dist/forms/uploads.js +132 -0
  253. package/dist/index.d.ts +8 -2
  254. package/dist/index.js +8 -2
  255. package/dist/live/document.d.ts +12 -0
  256. package/dist/live/document.js +8 -1
  257. package/dist/live/draftAccess.js +1 -1
  258. package/dist/live/endpoints.d.ts +1 -0
  259. package/dist/live/session.js +3 -1
  260. package/dist/live/storage.d.ts +13 -2
  261. package/dist/live/storage.js +18 -7
  262. package/dist/mcp/forms.d.ts +26 -0
  263. package/dist/mcp/forms.js +280 -0
  264. package/dist/mcp/index.d.ts +7 -0
  265. package/dist/mcp/index.js +29 -10
  266. package/dist/mcp/shared.d.ts +1 -0
  267. package/dist/mcp/shared.js +1 -0
  268. package/dist/plugin/forms.d.ts +30 -0
  269. package/dist/plugin/forms.js +63 -0
  270. package/dist/plugin/formsSync.d.ts +138 -0
  271. package/dist/plugin/formsSync.js +345 -0
  272. package/dist/plugin/hook.d.ts +22 -0
  273. package/dist/plugin/hook.js +19 -7
  274. package/dist/plugin/index.d.ts +14 -0
  275. package/dist/plugin/index.js +101 -30
  276. package/dist/plugin/links.d.ts +7 -2
  277. package/dist/plugin/links.js +9 -3
  278. package/dist/plugin/references.d.ts +6 -0
  279. package/dist/plugin/references.js +12 -0
  280. package/dist/plugin/sections.js +3 -0
  281. package/dist/plugin/templates.js +5 -1
  282. package/dist/react/canvas/BuilderCanvas.d.ts +2 -0
  283. package/dist/react/canvas/BuilderCanvas.js +29 -6
  284. package/dist/react/canvas/inline/candidates.js +2 -2
  285. package/dist/react/canvas/inline/mapper.d.ts +6 -1
  286. package/dist/react/canvas/inline/mapper.js +35 -4
  287. package/dist/react/canvas/inline/rich/editor.js +9 -1
  288. package/dist/react/components/Image.d.ts +1 -1
  289. package/dist/react/components/Image.js +2 -2
  290. package/dist/react/render/LayoutOutlet.d.ts +7 -3
  291. package/dist/react/render/LayoutOutlet.js +9 -4
  292. package/dist/react/render/RenderLayout.js +13 -7
  293. package/dist/react/render/resolve.d.ts +1 -0
  294. package/dist/react/render/resolve.js +7 -1
  295. package/dist/react/render/templates.js +3 -1
  296. package/dist/react/server.js +19 -0
  297. package/dist/react/theme/ThemeStyle.js +1 -1
  298. package/dist/theme/admin/ColorField.d.ts +4 -3
  299. package/dist/theme/admin/ColorField.js +1 -1
  300. package/dist/theme/admin/FontField.d.ts +4 -3
  301. package/dist/theme/admin/FontField.js +1 -1
  302. package/dist/theme/admin/SaveSignal.js +1 -1
  303. package/dist/theme/admin/SliderField.d.ts +1 -1
  304. package/dist/theme/admin/SliderField.js +1 -1
  305. package/dist/theme/admin/Variables.d.ts +4 -3
  306. package/dist/theme/admin/Variables.js +2 -2
  307. package/dist/theme/admin/fields.css +327 -0
  308. package/dist/theme/global.js +2 -0
  309. package/package.json +29 -24
  310. package/dist/admin/aiKeyField.scss +0 -58
  311. package/dist/admin/diff/layoutDiff.scss +0 -181
  312. package/dist/admin/editor/classes/manager.scss +0 -262
  313. package/dist/admin/editor/fields/fields.scss +0 -628
  314. package/dist/admin/editor/layout/layout.scss +0 -240
  315. package/dist/admin/editor/locale/locale.scss +0 -154
  316. package/dist/admin/editor/menu/menu.scss +0 -132
  317. package/dist/admin/editor/sections/components.scss +0 -238
  318. package/dist/admin/editor/sections/variants.scss +0 -90
  319. package/dist/admin/editor/topbar/contentMode.scss +0 -35
  320. package/dist/admin/editor/topbar/screens/screens.scss +0 -134
  321. package/dist/admin/editor/topbar/settingsDrawer.scss +0 -32
  322. package/dist/admin/editor/ui/controls.scss +0 -320
  323. package/dist/admin/layoutField.scss +0 -69
  324. package/dist/admin/templateConditions.scss +0 -24
  325. package/dist/theme/admin/fields.scss +0 -363
@@ -0,0 +1,184 @@
1
+ // Display conditions for the fields of a form made with `@payloadcms/plugin-form-builder`.
2
+ // A form field may show only when other fields of the same form have some values:
3
+ // conditions: { match: 'all' | 'any', rules: [{ field, operator, value }] }
4
+ // Pure and client-safe: the site's form renderer, the submission hook and the builder use it.
5
+ export const FORM_CONDITION_OPERATORS = ['equals', 'notEquals', 'contains', 'isEmpty', 'isNotEmpty'];
6
+ /** Name of the conditions field in every form field block. */
7
+ export const FORM_CONDITIONS_FIELD = 'conditions';
8
+ /** Operators that compare with a value. The others test only whether the field is empty. */
9
+ const NEEDS_VALUE = new Set(['equals', 'notEquals', 'contains']);
10
+ const OPERATOR_LABELS = {
11
+ equals: 'equals',
12
+ notEquals: 'does not equal',
13
+ contains: 'contains',
14
+ isEmpty: 'is empty',
15
+ isNotEmpty: 'is not empty',
16
+ };
17
+ const text = (value) => (value === null || value === undefined ? '' : String(value).trim());
18
+ /** Empty: no value, blank text, or a checkbox that is not ticked. */
19
+ export function isEmptyFormValue(value) {
20
+ return value === false || text(value) === '';
21
+ }
22
+ /** A rule that names a field and an operator (and a value, when the operator needs one). */
23
+ export function isCompleteRule(rule) {
24
+ if (!rule || typeof rule.field !== 'string' || !rule.field.trim())
25
+ return false;
26
+ return typeof rule.operator === 'string' && FORM_CONDITION_OPERATORS.includes(rule.operator);
27
+ }
28
+ /**
29
+ * True when the rule holds for `value`. Text compares without case and surrounding spaces; a
30
+ * ticked checkbox equals "true" (also "yes", "on" or "1"), an unticked one "false".
31
+ */
32
+ export function ruleMet(rule, value) {
33
+ if (rule.operator === 'isEmpty')
34
+ return isEmptyFormValue(value);
35
+ if (rule.operator === 'isNotEmpty')
36
+ return !isEmptyFormValue(value);
37
+ const actual = typeof value === 'boolean' ? String(value) : text(value).toLowerCase();
38
+ let expected = text(rule.value).toLowerCase();
39
+ if (typeof value === 'boolean' && ['yes', 'on', '1', 'checked', 'ticked'].includes(expected))
40
+ expected = 'true';
41
+ if (typeof value === 'boolean' && ['no', 'off', '0', 'unchecked'].includes(expected))
42
+ expected = 'false';
43
+ if (rule.operator === 'contains')
44
+ return expected === '' || actual.includes(expected);
45
+ const equal = actual === expected;
46
+ return rule.operator === 'equals' ? equal : !equal;
47
+ }
48
+ /** The complete rules of a field. A field without complete rules always shows. */
49
+ export function conditionRules(field) {
50
+ return (field.conditions?.rules ?? []).filter(isCompleteRule);
51
+ }
52
+ /** True when the field shows for these values (the values of hidden fields left out). */
53
+ export function conditionsMet(field, values) {
54
+ const rules = conditionRules(field);
55
+ if (rules.length === 0)
56
+ return true;
57
+ const met = (rule) => ruleMet(rule, values[rule.field.trim()]);
58
+ return field.conditions?.match === 'any' ? rules.some(met) : rules.every(met);
59
+ }
60
+ /**
61
+ * The names of the fields their conditions hide. A rule that reads a hidden field sees it as
62
+ * empty, so a chain (C depends on B, B on A) hides C when A hides B. Rules that form a loop
63
+ * (A depends on B, B on A) read the entered values. Fields without a name (messages) are never
64
+ * in the set.
65
+ */
66
+ export function hiddenFormFields(fields, values) {
67
+ const byName = new Map();
68
+ for (const field of fields ?? [])
69
+ if (typeof field?.name === 'string' && field.name !== '' && !byName.has(field.name))
70
+ byName.set(field.name, field);
71
+ const shown = new Map();
72
+ const visiting = new Set();
73
+ const isShown = (name) => {
74
+ const known = shown.get(name);
75
+ if (known !== undefined)
76
+ return known;
77
+ const field = byName.get(name);
78
+ // Not a field of the form, or a loop: the entered value counts.
79
+ if (!field || visiting.has(name))
80
+ return true;
81
+ visiting.add(name);
82
+ const seen = {};
83
+ for (const rule of conditionRules(field)) {
84
+ const other = rule.field.trim();
85
+ seen[other] = isShown(other) ? values[other] : undefined;
86
+ }
87
+ visiting.delete(name);
88
+ const result = conditionsMet(field, seen);
89
+ shown.set(name, result);
90
+ return result;
91
+ };
92
+ return new Set([...byName.keys()].filter((name) => !isShown(name)));
93
+ }
94
+ /** True when the field shows: it has no name (a message) or no condition hides it. */
95
+ export function isFormFieldShown(field, hidden) {
96
+ return !field.name || !hidden.has(field.name);
97
+ }
98
+ /** One line for editors: "Shows when Topic equals Sales and Email is not empty". */
99
+ export function describeConditions(field, labelOf = (name) => name) {
100
+ const rules = conditionRules(field);
101
+ if (rules.length === 0)
102
+ return null;
103
+ const parts = rules.map((rule) => {
104
+ const head = `${labelOf(rule.field.trim())} ${OPERATOR_LABELS[rule.operator]}`;
105
+ return NEEDS_VALUE.has(rule.operator) ? `${head} “${text(rule.value)}”` : head;
106
+ });
107
+ return `Shows when ${parts.join(field.conditions?.match === 'any' ? ' or ' : ' and ')}`;
108
+ }
109
+ // ---------------------------------------------------------------------------
110
+ // The Payload field
111
+ // ---------------------------------------------------------------------------
112
+ /**
113
+ * The `conditions` field for a form field block. Add it to the blocks of the form-builder
114
+ * plugin's `fields` option with `withFormConditions`. The builder edits it in its right panel;
115
+ * Payload's edit screen shows it as a group.
116
+ *
117
+ * `admin.custom.builderFormField` makes the builder show the rule's field as a list of the
118
+ * form's other fields. `admin.custom.builderCondition` hides the value for "is empty" and
119
+ * "is not empty" in the builder; `admin.condition` does the same in Payload's edit screen.
120
+ */
121
+ export function formConditionsField() {
122
+ return {
123
+ name: FORM_CONDITIONS_FIELD,
124
+ type: 'group',
125
+ label: 'Display conditions',
126
+ admin: {
127
+ description: 'Show this field only when other fields of the form have some values. Without rules it always shows.',
128
+ },
129
+ fields: [
130
+ {
131
+ name: 'match',
132
+ type: 'select',
133
+ label: 'Show when',
134
+ defaultValue: 'all',
135
+ options: [
136
+ { label: 'All rules match', value: 'all' },
137
+ { label: 'Any rule matches', value: 'any' },
138
+ ],
139
+ },
140
+ {
141
+ name: 'rules',
142
+ type: 'array',
143
+ label: 'Rules',
144
+ labels: { singular: 'Rule', plural: 'Rules' },
145
+ fields: [
146
+ {
147
+ // Not required: a rule without a field is ignored, and the form keeps saving while it is filled in.
148
+ name: 'field',
149
+ type: 'text',
150
+ label: 'Field',
151
+ admin: { description: 'The name of another field of this form.', custom: { builderFormField: true } },
152
+ },
153
+ {
154
+ name: 'operator',
155
+ type: 'select',
156
+ label: 'Test',
157
+ defaultValue: 'equals',
158
+ options: FORM_CONDITION_OPERATORS.map((value) => ({ label: OPERATOR_LABELS[value], value })),
159
+ },
160
+ {
161
+ name: 'value',
162
+ type: 'text',
163
+ label: 'Value',
164
+ admin: {
165
+ description: 'For a checkbox: "true" when ticked, "false" when not.',
166
+ condition: (_, siblings) => siblings?.operator !== 'isEmpty' && siblings?.operator !== 'isNotEmpty',
167
+ custom: { builderCondition: { field: 'operator', notEquals: ['isEmpty', 'isNotEmpty'] } },
168
+ },
169
+ },
170
+ ],
171
+ },
172
+ ],
173
+ };
174
+ }
175
+ /**
176
+ * The block with the `conditions` field added, for the form-builder plugin's `fields` option:
177
+ * `formBuilderPlugin({ fields: { text: withFormConditions(fields.text) } })`, with `fields` from
178
+ * `@payloadcms/plugin-form-builder`. A block that has the field already stays as it is.
179
+ */
180
+ export function withFormConditions(block) {
181
+ if (block.fields.some((f) => 'name' in f && f.name === FORM_CONDITIONS_FIELD))
182
+ return block;
183
+ return { ...block, fields: [...block.fields, formConditionsField()] };
184
+ }
@@ -0,0 +1,23 @@
1
+ import type { PayloadRequest } from 'payload';
2
+ /** An email as the plugin formats it before `beforeEmail`. */
3
+ type PluginEmail = {
4
+ to?: string;
5
+ from?: string;
6
+ };
7
+ /** What the plugin passes: the submission's `afterChange` arguments (its types call them `beforeChangeParams`). */
8
+ type HookArgs = {
9
+ data?: Record<string, any>;
10
+ doc?: Record<string, any>;
11
+ req: PayloadRequest;
12
+ };
13
+ export type FormEmailsOptions = {
14
+ /** Slug of the forms collection. Default "forms". */
15
+ forms?: string;
16
+ };
17
+ /**
18
+ * The `beforeEmail` hook: `formBuilderPlugin({ beforeEmail: formEmails() })`. It reads the form
19
+ * again and builds every email from the stored answers (hidden fields already dropped). An email
20
+ * without a To address is not sent.
21
+ */
22
+ export declare function formEmails(options?: FormEmailsOptions): <T extends PluginEmail>(emails: T[], args: HookArgs) => Promise<T[]>;
23
+ export {};
@@ -0,0 +1,64 @@
1
+ // Server: the form-builder plugin's `beforeEmail` hook that builds each email of a form from the
2
+ // fields of `formEmailFields` (from name, custom HTML body, merge tags with `{{*}}` as a table).
3
+ // The plugin sends what this returns.
4
+ import { convertLexicalToHTML } from '@payloadcms/richtext-lexical/html';
5
+ import { formAnswers, formEmailMessage, tablesOutsideParagraphs } from './mergeTags.js';
6
+ const idOf = (value) => {
7
+ if (typeof value === 'string' || typeof value === 'number')
8
+ return value;
9
+ if (value && typeof value === 'object' && 'id' in value)
10
+ return idOf(value.id);
11
+ return null;
12
+ };
13
+ function richTextHtml(value) {
14
+ if (!value || typeof value !== 'object' || !('root' in value))
15
+ return '';
16
+ try {
17
+ // The answers table may not sit in a <p>.
18
+ return tablesOutsideParagraphs(convertLexicalToHTML({ data: value, disableContainer: true }));
19
+ }
20
+ catch {
21
+ return '';
22
+ }
23
+ }
24
+ /**
25
+ * The `beforeEmail` hook: `formBuilderPlugin({ beforeEmail: formEmails() })`. It reads the form
26
+ * again and builds every email from the stored answers (hidden fields already dropped). An email
27
+ * without a To address is not sent.
28
+ */
29
+ export function formEmails(options = {}) {
30
+ const formsSlug = options.forms ?? 'forms';
31
+ return async (emails, args) => {
32
+ const req = args.req;
33
+ const id = idOf(args.doc?.form ?? args.data?.form);
34
+ if (id === null)
35
+ return emails;
36
+ const form = (await req.payload
37
+ .findByID({ collection: formsSlug, id, depth: 0, req, overrideAccess: true, locale: req.locale ?? undefined })
38
+ .catch(() => null));
39
+ const configs = form?.emails ?? [];
40
+ // The plugin made one email per entry, in order. Anything else: leave its emails alone.
41
+ if (!form || configs.length !== emails.length)
42
+ return emails;
43
+ const rows = (args.doc?.submissionData ?? args.data?.submissionData);
44
+ const answers = formAnswers(form.fields, rows, new Set(['formSubmissionID']));
45
+ const fieldNames = new Set((form.fields ?? []).flatMap((f) => (f.name ? [f.name] : [])));
46
+ const adapter = req.payload.email;
47
+ const defaults = { fromName: adapter?.defaultFromName ?? '', fromAddress: adapter?.defaultFromAddress ?? '' };
48
+ const extra = { formSubmissionID: String(args.doc?.id ?? ''), formTitle: form.title ?? '' };
49
+ return configs
50
+ .map((config, index) => {
51
+ const plugin = emails[index];
52
+ const message = formEmailMessage(config, {
53
+ answers,
54
+ fieldNames,
55
+ extra,
56
+ richTextHtml: config.bodyType === 'html' ? '' : richTextHtml(config.message),
57
+ defaults: { ...defaults, to: typeof plugin.to === 'string' ? plugin.to : '' },
58
+ });
59
+ // The plugin's own keys stay (attachments a later hook adds); ours win.
60
+ return { ...plugin, ...message };
61
+ })
62
+ .filter((email) => email.to);
63
+ };
64
+ }
@@ -0,0 +1,35 @@
1
+ import type { Block, Field } from 'payload';
2
+ /** `admin.custom.builderMergeTags`: the builder adds an "Insert field" menu with the form's merge tags. */
3
+ export declare const MERGE_TAGS_MARKER = "builderMergeTags";
4
+ /** A phone number. The form checks that it has 6 to 15 digits. */
5
+ export declare const phoneFormField: Block;
6
+ /** A web address. The form checks that it is an http or https address. */
7
+ export declare const urlFormField: Block;
8
+ /** A value sent with every answer that visitors do not see, for example the campaign. */
9
+ export declare const hiddenFormField: Block;
10
+ /** A box to tick whose text is rich text, so it can link to the privacy policy. */
11
+ export declare const consentFormField: Block;
12
+ /** Default and upper limit of a file field's size, in MB. */
13
+ export declare const FILE_FIELD_MAX_MB = 10;
14
+ /** Files the visitor uploads: one, or several when "Allow several files" is on. `formFileUploads` stores them in a private upload collection. */
15
+ export declare const fileFormField: Block;
16
+ /** The extra field types by slug, for the plugin's `fields` option. */
17
+ export declare const moreFormFields: {
18
+ readonly phone: Block;
19
+ readonly url: Block;
20
+ readonly hidden: Block;
21
+ readonly consent: Block;
22
+ readonly file: Block;
23
+ };
24
+ /**
25
+ * The fields of one form email: To, From name, From email, Reply-To, CC, BCC, Subject and the body
26
+ * (rich text or custom HTML). The plugin's own names stay, so stored emails keep working; the
27
+ * from name, the body type and the HTML body are new. Every line and the body take merge tags.
28
+ */
29
+ export declare function formEmailFields(): Field[];
30
+ /**
31
+ * The forms collection's fields with the emails of `formEmailFields`, for
32
+ * `formBuilderPlugin({ formOverrides: { fields: ({ defaultFields }) => withFormEmailFields(defaultFields) } })`.
33
+ * The `emails` array keeps its access and description.
34
+ */
35
+ export declare function withFormEmailFields(fields: readonly Field[]): Field[];
@@ -0,0 +1,167 @@
1
+ // More field types for `@payloadcms/plugin-form-builder`, as Payload block configs for the
2
+ // plugin's `fields` option: phone, web address, hidden field, consent box and file upload. And the
3
+ // email fields of a form, Contact Form 7 style, for the plugin's `formOverrides.fields`.
4
+ // Client-safe. The app renders the field types; `formSubmissionConditions` checks their answers
5
+ // and `formFileUploads` stores the files.
6
+ import { fileCountFields } from './fieldSettings.js';
7
+ /** `admin.custom.builderMergeTags`: the builder adds an "Insert field" menu with the form's merge tags. */
8
+ export const MERGE_TAGS_MARKER = 'builderMergeTags';
9
+ const mergeTags = { [MERGE_TAGS_MARKER]: true };
10
+ const name = { name: 'name', type: 'text', label: 'Name', required: true, admin: { description: 'Lowercase, no spaces. Emails show the answer with {{name}}.' } };
11
+ const label = { name: 'label', type: 'text', label: 'Label', localized: true };
12
+ const width = { name: 'width', type: 'number', label: 'Field width (percentage)', min: 10, max: 100 };
13
+ const required = { name: 'required', type: 'checkbox', label: 'Required' };
14
+ const placeholder = { name: 'placeholder', type: 'text', label: 'Placeholder', localized: true };
15
+ const textLike = (slug, singular, plural) => ({
16
+ slug,
17
+ labels: { singular, plural },
18
+ fields: [
19
+ { type: 'row', fields: [name, label] },
20
+ { type: 'row', fields: [width, { name: 'defaultValue', type: 'text', label: 'Default value', localized: true }] },
21
+ placeholder,
22
+ required,
23
+ ],
24
+ });
25
+ /** A phone number. The form checks that it has 6 to 15 digits. */
26
+ export const phoneFormField = textLike('phone', 'Phone', 'Phones');
27
+ /** A web address. The form checks that it is an http or https address. */
28
+ export const urlFormField = textLike('url', 'Web address', 'Web addresses');
29
+ /** A value sent with every answer that visitors do not see, for example the campaign. */
30
+ export const hiddenFormField = {
31
+ slug: 'hidden',
32
+ labels: { singular: 'Hidden field', plural: 'Hidden fields' },
33
+ fields: [
34
+ { type: 'row', fields: [name, { ...label, admin: { description: 'For emails and submissions only. Visitors do not see it.' } }] },
35
+ { name: 'defaultValue', type: 'text', label: 'Value', admin: { description: 'Sent with every answer.' } },
36
+ ],
37
+ };
38
+ /** A box to tick whose text is rich text, so it can link to the privacy policy. */
39
+ export const consentFormField = {
40
+ slug: 'consent',
41
+ labels: { singular: 'Consent', plural: 'Consents' },
42
+ fields: [
43
+ { type: 'row', fields: [name, { ...label, admin: { description: 'A short name for emails and submissions.' } }] },
44
+ {
45
+ name: 'text',
46
+ type: 'richText',
47
+ label: 'Text',
48
+ localized: true,
49
+ admin: { description: 'The text next to the box. Select words and add a link, for example to the privacy policy.' },
50
+ },
51
+ width,
52
+ { ...required, defaultValue: true },
53
+ ],
54
+ };
55
+ /** Default and upper limit of a file field's size, in MB. */
56
+ export const FILE_FIELD_MAX_MB = 10;
57
+ /** Files the visitor uploads: one, or several when "Allow several files" is on. `formFileUploads` stores them in a private upload collection. */
58
+ export const fileFormField = {
59
+ slug: 'file',
60
+ labels: { singular: 'File upload', plural: 'File uploads' },
61
+ fields: [
62
+ { type: 'row', fields: [name, label] },
63
+ width,
64
+ required,
65
+ {
66
+ name: 'accept',
67
+ type: 'text',
68
+ label: 'Allowed file types',
69
+ defaultValue: 'image/*, application/pdf',
70
+ admin: { description: 'File types or endings, separated by commas, for example "image/*, application/pdf, .docx". Empty: images, PDF, office documents, text and CSV. HTML, SVG, scripts and XML are never taken.' },
71
+ },
72
+ {
73
+ name: 'maxSize',
74
+ type: 'number',
75
+ label: 'Largest file (MB)',
76
+ defaultValue: 5,
77
+ min: 0.1,
78
+ max: FILE_FIELD_MAX_MB,
79
+ admin: { description: `For each file. At most ${FILE_FIELD_MAX_MB} MB.` },
80
+ },
81
+ ...fileCountFields(),
82
+ ],
83
+ };
84
+ /** The extra field types by slug, for the plugin's `fields` option. */
85
+ export const moreFormFields = {
86
+ phone: phoneFormField,
87
+ url: urlFormField,
88
+ hidden: hiddenFormField,
89
+ consent: consentFormField,
90
+ file: fileFormField,
91
+ };
92
+ const tagHelp = 'Type {{name}} for a field’s answer, or use Insert field.';
93
+ /**
94
+ * The fields of one form email: To, From name, From email, Reply-To, CC, BCC, Subject and the body
95
+ * (rich text or custom HTML). The plugin's own names stay, so stored emails keep working; the
96
+ * from name, the body type and the HTML body are new. Every line and the body take merge tags.
97
+ */
98
+ export function formEmailFields() {
99
+ return [
100
+ {
101
+ name: 'emailTo',
102
+ type: 'text',
103
+ label: 'To',
104
+ admin: { placeholder: 'owner@example.com or {{email}}', description: `Addresses separated by commas. {{email}} sends to the visitor. ${tagHelp}`, custom: mergeTags },
105
+ },
106
+ {
107
+ type: 'row',
108
+ fields: [
109
+ { name: 'fromName', type: 'text', label: 'From name', admin: { placeholder: 'Your site', custom: mergeTags } },
110
+ { name: 'emailFrom', type: 'text', label: 'From email', admin: { placeholder: 'noreply@example.com', description: 'Empty: the site’s address.' } },
111
+ ],
112
+ },
113
+ { name: 'replyTo', type: 'text', label: 'Reply-To', admin: { placeholder: '{{email}}', custom: mergeTags } },
114
+ {
115
+ type: 'row',
116
+ fields: [
117
+ { name: 'cc', type: 'text', label: 'CC', admin: { custom: mergeTags } },
118
+ { name: 'bcc', type: 'text', label: 'BCC', admin: { custom: mergeTags } },
119
+ ],
120
+ },
121
+ { name: 'subject', type: 'text', label: 'Subject', localized: true, required: true, defaultValue: 'New message: {{formTitle}}', admin: { custom: mergeTags } },
122
+ {
123
+ name: 'bodyType',
124
+ type: 'radio',
125
+ label: 'Body',
126
+ defaultValue: 'richText',
127
+ options: [
128
+ { label: 'Rich text', value: 'richText' },
129
+ { label: 'Custom HTML', value: 'html' },
130
+ ],
131
+ admin: { layout: 'horizontal' },
132
+ },
133
+ {
134
+ name: 'message',
135
+ type: 'richText',
136
+ label: 'Message',
137
+ localized: true,
138
+ admin: {
139
+ description: `${tagHelp} {{*}} adds every answer as a table.`,
140
+ condition: (_, siblings) => siblings?.bodyType !== 'html',
141
+ custom: { ...mergeTags, builderCondition: { field: 'bodyType', notEquals: 'html' } },
142
+ },
143
+ },
144
+ {
145
+ name: 'html',
146
+ type: 'code',
147
+ label: 'HTML body',
148
+ localized: true,
149
+ admin: {
150
+ language: 'html',
151
+ description: `Your own HTML. ${tagHelp} {{*}} adds every answer as a table. Answers are escaped.`,
152
+ condition: (_, siblings) => siblings?.bodyType === 'html',
153
+ custom: { ...mergeTags, builderCondition: { field: 'bodyType', equals: 'html' } },
154
+ },
155
+ },
156
+ ];
157
+ }
158
+ /**
159
+ * The forms collection's fields with the emails of `formEmailFields`, for
160
+ * `formBuilderPlugin({ formOverrides: { fields: ({ defaultFields }) => withFormEmailFields(defaultFields) } })`.
161
+ * The `emails` array keeps its access and description.
162
+ */
163
+ export function withFormEmailFields(fields) {
164
+ return fields.map((field) => 'name' in field && field.name === 'emails' && field.type === 'array'
165
+ ? { ...field, label: 'Emails', labels: { singular: 'Email', plural: 'Emails' }, fields: formEmailFields() }
166
+ : field);
167
+ }
@@ -0,0 +1,78 @@
1
+ import type { BlockDefinition, Layout, Operation } from '../core/types.js';
2
+ import { type FormFieldConditions } from './conditions.js';
3
+ /** The format guide for agents: field types, props and display conditions. */
4
+ export declare const FORM_FIELDS_GUIDE: string;
5
+ /** One field of a form, as agents read it. */
6
+ export type FormFieldView = {
7
+ id: string;
8
+ type: string;
9
+ name?: string;
10
+ label?: string;
11
+ required?: boolean;
12
+ placeholder?: string;
13
+ /** The field's own classes (its box), width included. */
14
+ className?: string;
15
+ defaultValue?: unknown;
16
+ options?: unknown;
17
+ /** "message" fields: the text. */
18
+ message?: string;
19
+ conditions?: FormFieldConditions;
20
+ /** The conditions in one sentence: "Shows when Topic equals “sales”". */
21
+ shows?: string;
22
+ /** Any other props the field type has. */
23
+ [key: string]: unknown;
24
+ };
25
+ /** One change of a form's fields. `field` is the field's name or its id. */
26
+ export type FormFieldChange = {
27
+ action: 'add';
28
+ type: string;
29
+ values?: Record<string, unknown>;
30
+ index?: number;
31
+ } | {
32
+ action: 'update';
33
+ field: string;
34
+ values?: Record<string, unknown>;
35
+ unset?: string[];
36
+ } | {
37
+ action: 'move';
38
+ field: string;
39
+ index: number;
40
+ } | {
41
+ action: 'remove';
42
+ field: string;
43
+ } | {
44
+ action: 'setConditions';
45
+ field: string;
46
+ conditions: FormFieldConditions | null;
47
+ };
48
+ /** The fields of a form layout (the decoded `fields` blocks), in order. */
49
+ export declare function formFieldsOf(layout: Layout): FormFieldView[];
50
+ /** The field types a form offers, with their props, for agents. */
51
+ export declare function formFieldTypes(definitions: readonly BlockDefinition[]): {
52
+ type: string;
53
+ label: string;
54
+ description?: string | undefined;
55
+ props: {
56
+ name: string;
57
+ type: string;
58
+ required?: boolean | undefined;
59
+ }[];
60
+ }[];
61
+ /** A field name made from a label: "Company size" -> "company_size". */
62
+ export declare function nameFromLabel(label: string): string;
63
+ export type FormFieldOpsResult = {
64
+ ok: true;
65
+ ops: Operation[];
66
+ notes: string[];
67
+ } | {
68
+ ok: false;
69
+ error: string;
70
+ };
71
+ /**
72
+ * The builder operations for a list of field changes, applied in order (each change sees the
73
+ * ones before it). `definitions` are the form's field block definitions: they give the types and
74
+ * the start values of a new field. Checks names (unique, valid) and conditions (rules name other
75
+ * fields). Renaming a field rewrites the rules that read it. The operations themselves are checked
76
+ * again by the live session (prop types, required props on publish).
77
+ */
78
+ export declare function formFieldOps(layout: Layout, changes: readonly FormFieldChange[], definitions: readonly BlockDefinition[]): FormFieldOpsResult;