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,378 @@
1
+ // Form field edits for AI agents: the MCP tools (mcp/forms.ts) and the editor's assistant
2
+ // (ai/tools.ts). An agent names fields by their `name` (or block id) and sends small changes
3
+ // ("add an email field", "make phone required", "show company only when topic is sales").
4
+ // `formFieldOps` turns them into the builder's normal operations, so they reach the form's live
5
+ // session like any editor change, and an open form builder shows them at once.
6
+ // Pure: no Payload, no I/O.
7
+ import { isRichText, plainToRichText, richTextToPlain } from '../core/bindings.js';
8
+ import { dataFields } from '../core/fields.js';
9
+ import { createId } from '../core/ids.js';
10
+ import { isPlainObject } from '../core/tree.js';
11
+ import { describeConditions, FORM_CONDITION_OPERATORS, FORM_CONDITIONS_FIELD } from './conditions.js';
12
+ import { formFieldSlug, formFieldType } from './blocks.js';
13
+ import { FORM_FIELD_CLASS_KEY, FORM_FIELD_WIDTH_CLASSES, FORM_FIELD_WIDTH_KEY } from './fieldSettings.js';
14
+ /** The format guide for agents: field types, props and display conditions. */
15
+ export const FORM_FIELDS_GUIDE = [
16
+ 'FORM FIELDS. A form is a list of fields, shown in order. Each field has a `type` and props:',
17
+ '- name (text, required): the key of the answer in a submission. Letters, digits and _ only, unique in the form (e.g. "email", "company_size").',
18
+ '- label (text): the question people see. required (boolean): the form refuses to send without an answer.',
19
+ '- placeholder (text, where the type has it).',
20
+ '- Where the site has them (see `fieldTypes`): description (help text under the field), labelVisibility ("visible" or "hidden": screen readers still read a hidden label), display for radio ("list", "inline", "buttons", "cards"), multiple and maxFiles for file (several files, at most maxFiles).',
21
+ `- className (Tailwind classes of the box around the field; null clears them). Looks are classes, the width too: there is no width setting. The fields wrap in a row with a 1rem gap, so "${FORM_FIELD_WIDTH_CLASSES[50]}" is half a row from the sm breakpoint (two fields side by side), "${FORM_FIELD_WIDTH_CLASSES[33]}" a third, "${FORM_FIELD_WIDTH_CLASSES[67]}" two thirds. No width class: full width. update replaces all of the field's classes, so send the ones to keep too.`,
22
+ '- defaultValue: text for text-like fields, a number for "number", true/false for "checkbox".',
23
+ '- options (select, radio): [{ label, value }]. You may send plain strings; each becomes { label, value } with a value made from the label.',
24
+ '- message ("message" type only): text shown between the fields. Send plain text (one paragraph per line); it is stored as rich text. A message has no name and collects no answer.',
25
+ 'Types (the form\'s `fieldTypes` lists the ones this site has and their props): text (one line), email (checks the address), textarea (long text), number, select (dropdown), radio, checkbox (true/false, e.g. accept the terms), country, state (US states), date, message, upload, payment.',
26
+ `DISPLAY CONDITIONS. A field may show only when other fields have some values: conditions = { match: "all" | "any", rules: [{ field, operator, value }] }. field = the name of another field of the same form. operator: ${FORM_CONDITION_OPERATORS.join(', ')}. value is text, needed for equals, notEquals and contains (a checkbox compares with "true" or "false"). Text compares without case. match "all" (default) needs every rule, "any" one rule. A hidden field is never required, and its answer is not sent. A rule that reads a hidden field sees it as empty. null or no rules: the field always shows.`,
27
+ 'Example: show "company" only when "topic" equals "sales": { "action": "setConditions", "field": "company", "conditions": { "rules": [{ "field": "topic", "operator": "equals", "value": "sales" }] } }.',
28
+ ].join('\n');
29
+ const KNOWN_KEYS = new Set(['id', 'type']);
30
+ /** The fields of a form layout (the decoded `fields` blocks), in order. */
31
+ export function formFieldsOf(layout) {
32
+ return layout.blocks.map((block) => formFieldView(block, layout.blocks));
33
+ }
34
+ function formFieldView(block, all) {
35
+ const props = (block.props ?? {});
36
+ const out = { id: block.id, type: formFieldSlug(block.type) ?? block.type };
37
+ if (block.className)
38
+ out.className = block.className;
39
+ for (const [key, value] of Object.entries(props)) {
40
+ if (KNOWN_KEYS.has(key) || key === FORM_FIELD_WIDTH_KEY || value === null || value === undefined || value === '')
41
+ continue;
42
+ if (key === 'message') {
43
+ out.message = isRichText(value) ? richTextToPlain(value) : String(value);
44
+ continue;
45
+ }
46
+ if (key === FORM_CONDITIONS_FIELD) {
47
+ const conditions = cleanConditions(value);
48
+ if (!conditions)
49
+ continue;
50
+ out.conditions = conditions;
51
+ const labels = new Map(all.map((b) => [String(b.props?.name ?? ''), String(b.props?.label ?? b.props?.name ?? '')]));
52
+ const shows = describeConditions({ conditions }, (name) => labels.get(name) || name);
53
+ if (shows)
54
+ out.shows = shows;
55
+ continue;
56
+ }
57
+ if (Array.isArray(value)) {
58
+ // Array rows keep their values, without Payload's row ids.
59
+ out[key] = value.map((row) => (isPlainObject(row) ? Object.fromEntries(Object.entries(row).filter(([k]) => k !== 'id')) : row));
60
+ continue;
61
+ }
62
+ out[key] = value;
63
+ }
64
+ return out;
65
+ }
66
+ /** Conditions with rules, without row ids. Null when there is no rule. */
67
+ function cleanConditions(value) {
68
+ if (!isPlainObject(value))
69
+ return null;
70
+ const rules = Array.isArray(value.rules)
71
+ ? value.rules.filter(isPlainObject).map((rule) => ({
72
+ field: typeof rule.field === 'string' ? rule.field : null,
73
+ operator: (typeof rule.operator === 'string' ? rule.operator : 'equals'),
74
+ ...(typeof rule.value === 'string' && rule.value !== '' ? { value: rule.value } : {}),
75
+ }))
76
+ : [];
77
+ if (rules.length === 0)
78
+ return null;
79
+ return { ...(value.match === 'any' ? { match: 'any' } : {}), rules };
80
+ }
81
+ /** The field types a form offers, with their props, for agents. */
82
+ export function formFieldTypes(definitions) {
83
+ return definitions.flatMap((def) => {
84
+ const slug = formFieldSlug(def.type);
85
+ if (!slug)
86
+ return [];
87
+ // Hidden fields (the plugin's old `width`) are no setting to offer.
88
+ const props = dataFields(def.fields)
89
+ .filter((f) => f.name !== FORM_CONDITIONS_FIELD && !f.admin?.hidden)
90
+ .map((f) => ({ name: f.name, type: f.type, ...(f.required ? { required: true } : {}) }));
91
+ return [{ type: slug, label: def.label, ...(def.description ? { description: def.description } : {}), props }];
92
+ });
93
+ }
94
+ const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
95
+ /** Lowercase ASCII words of a label joined by `separator`, at most 40 characters ("Été 2026" -> "ete_2026"). */
96
+ function slugOf(label, separator) {
97
+ return label
98
+ .normalize('NFKD')
99
+ .replace(/[\u0300-\u036f]/g, '')
100
+ .toLowerCase()
101
+ .replace(/[^a-z0-9]+/g, separator)
102
+ .slice(0, 40)
103
+ .split(separator)
104
+ .filter(Boolean)
105
+ .join(separator);
106
+ }
107
+ /** A field name made from a label: "Company size" -> "company_size". */
108
+ export function nameFromLabel(label) {
109
+ const base = slugOf(label, '_');
110
+ return base === '' ? 'field' : /^\d/.test(base) ? `field_${base}` : base;
111
+ }
112
+ /** `base`, else `base2`, `base3`, … : the first name no field has. */
113
+ function freeName(base, taken) {
114
+ if (!taken.has(base))
115
+ return base;
116
+ let n = 2;
117
+ while (taken.has(`${base}${n}`))
118
+ n++;
119
+ return `${base}${n}`;
120
+ }
121
+ /** An option value made from its label: "Not sure" -> "not-sure", "5k to 20k" -> "5k-to-20k". */
122
+ const optionValue = (label) => slugOf(label, '-') || 'option';
123
+ /** Why a field change may not set `width`. */
124
+ const WIDTH_PROBLEM = `fields have no \`width\` setting: set the width with classes, e.g. { "className": "${FORM_FIELD_WIDTH_CLASSES[50]}" } for half a row.`;
125
+ /** The classes a change sets: `className` (or `cssClass`, the plugin's name). Undefined: no change. */
126
+ function classNameOf(values) {
127
+ const value = values.className !== undefined ? values.className : values[FORM_FIELD_CLASS_KEY];
128
+ if (value === undefined)
129
+ return undefined;
130
+ return typeof value === 'string' && value.trim() ? value.trim().replace(/\s+/g, ' ') : null;
131
+ }
132
+ /** The values with the shapes agents send fixed: options as strings, a message as plain text, conditions. */
133
+ function normalizeValues(values) {
134
+ const out = {};
135
+ for (const [key, value] of Object.entries(values)) {
136
+ if (key === 'id' || key === 'blockType' || key === 'type' || key === 'className' || key === FORM_FIELD_CLASS_KEY)
137
+ continue;
138
+ if (key === FORM_FIELD_WIDTH_KEY)
139
+ return WIDTH_PROBLEM;
140
+ if (key === 'options' && Array.isArray(value)) {
141
+ out.options = value.map((option) => {
142
+ if (typeof option === 'string')
143
+ return { label: option, value: optionValue(option) };
144
+ if (isPlainObject(option) && typeof option.label === 'string' && (option.value === undefined || option.value === '')) {
145
+ return { ...option, value: optionValue(option.label) };
146
+ }
147
+ return option;
148
+ });
149
+ continue;
150
+ }
151
+ if (key === 'message' && typeof value === 'string') {
152
+ out.message = plainToRichText(value);
153
+ continue;
154
+ }
155
+ if (key === FORM_CONDITIONS_FIELD) {
156
+ const conditions = conditionsValue(value);
157
+ if (typeof conditions === 'string')
158
+ return conditions;
159
+ out[key] = conditions;
160
+ continue;
161
+ }
162
+ out[key] = value;
163
+ }
164
+ return out;
165
+ }
166
+ /** Checks the conditions' shape. Null clears them. */
167
+ function conditionsValue(value) {
168
+ if (value === null || value === undefined)
169
+ return null;
170
+ if (!isPlainObject(value))
171
+ return '`conditions` must be { match?, rules: [{ field, operator, value? }] } or null.';
172
+ if (value.match !== undefined && value.match !== null && value.match !== 'all' && value.match !== 'any')
173
+ return '`conditions.match` must be "all" or "any".';
174
+ const rules = value.rules ?? [];
175
+ if (!Array.isArray(rules))
176
+ return '`conditions.rules` must be a list.';
177
+ const out = [];
178
+ for (const [i, rule] of rules.entries()) {
179
+ if (!isPlainObject(rule) || typeof rule.field !== 'string' || !rule.field.trim())
180
+ return `Rule ${i + 1} needs \`field\`: the name of another field.`;
181
+ const operator = rule.operator ?? 'equals';
182
+ if (typeof operator !== 'string' || !FORM_CONDITION_OPERATORS.includes(operator)) {
183
+ return `Rule ${i + 1}: \`operator\` must be one of ${FORM_CONDITION_OPERATORS.join(', ')}.`;
184
+ }
185
+ const needsValue = operator === 'equals' || operator === 'notEquals' || operator === 'contains';
186
+ if (needsValue && (rule.value === undefined || rule.value === null))
187
+ return `Rule ${i + 1}: "${operator}" needs a \`value\`.`;
188
+ out.push({ field: rule.field.trim(), operator: operator, ...(needsValue ? { value: String(rule.value) } : {}) });
189
+ }
190
+ if (out.length === 0)
191
+ return null;
192
+ return { match: value.match === 'any' ? 'any' : 'all', rules: out };
193
+ }
194
+ /**
195
+ * The builder operations for a list of field changes, applied in order (each change sees the
196
+ * ones before it). `definitions` are the form's field block definitions: they give the types and
197
+ * the start values of a new field. Checks names (unique, valid) and conditions (rules name other
198
+ * fields). Renaming a field rewrites the rules that read it. The operations themselves are checked
199
+ * again by the live session (prop types, required props on publish).
200
+ */
201
+ export function formFieldOps(layout, changes, definitions) {
202
+ // A working list: id, type and props of each field, so later changes see earlier ones.
203
+ let fields = layout.blocks.map((b) => ({ id: b.id, type: b.type, props: { ...b.props } }));
204
+ const ops = [];
205
+ const notes = [];
206
+ const ids = new Set(fields.map((f) => f.id));
207
+ const nameOf = (f) => (typeof f.props.name === 'string' ? f.props.name : '');
208
+ const find = (ref) => {
209
+ if (typeof ref !== 'string' || !ref)
210
+ return undefined;
211
+ return fields.find((f) => f.id === ref) ?? fields.find((f) => nameOf(f) === ref);
212
+ };
213
+ const missing = (ref, i) => `Change ${i + 1}: there is no field "${String(ref)}". Fields: ${fields.map((f) => nameOf(f) || f.id).join(', ') || 'none'}.`;
214
+ const checkRules = (conditions, self, i) => {
215
+ if (!isPlainObject(conditions) || !Array.isArray(conditions.rules))
216
+ return null;
217
+ for (const rule of conditions.rules) {
218
+ if (rule.field === self)
219
+ return `Change ${i + 1}: a field cannot depend on itself ("${self}").`;
220
+ if (!fields.some((f) => nameOf(f) === rule.field))
221
+ return `Change ${i + 1}: the rule names "${rule.field}", which is not a field of this form.`;
222
+ }
223
+ return null;
224
+ };
225
+ for (const [i, change] of changes.entries()) {
226
+ if (!isPlainObject(change))
227
+ return { ok: false, error: `Change ${i + 1} must be an object.` };
228
+ switch (change.action) {
229
+ case 'add': {
230
+ const blockType = typeof change.type === 'string' ? (formFieldSlug(change.type) ? change.type : formFieldType(change.type)) : '';
231
+ const def = definitions.find((d) => d.type === blockType);
232
+ if (!def) {
233
+ const known = definitions.map((d) => formFieldSlug(d.type)).filter(Boolean);
234
+ return { ok: false, error: `Change ${i + 1}: unknown field type "${String(change.type)}". This form offers: ${known.join(', ')}.` };
235
+ }
236
+ const values = normalizeValues(isPlainObject(change.values) ? change.values : {});
237
+ if (typeof values === 'string')
238
+ return { ok: false, error: `Change ${i + 1}: ${values}` };
239
+ const named = dataFields(def.fields).some((f) => f.name === 'name');
240
+ const props = {};
241
+ // The type's start values (a label, and `name`), as the editor's insert gives them.
242
+ for (const f of dataFields(def.fields))
243
+ if (f.defaultValue !== undefined && typeof f.defaultValue !== 'function')
244
+ props[f.name] = f.defaultValue;
245
+ Object.assign(props, values);
246
+ if (named) {
247
+ const taken = new Set(fields.map(nameOf).filter(Boolean));
248
+ if (typeof values.name === 'string') {
249
+ if (!NAME.test(values.name))
250
+ return { ok: false, error: `Change ${i + 1}: the name "${values.name}" must use letters, digits and _ only.` };
251
+ if (taken.has(values.name))
252
+ return { ok: false, error: `Change ${i + 1}: another field is already named "${values.name}". Names must be unique.` };
253
+ }
254
+ else {
255
+ const base = typeof values.label === 'string' && values.label.trim() ? nameFromLabel(values.label) : (formFieldSlug(def.type) ?? 'field');
256
+ props.name = freeName(base, taken);
257
+ }
258
+ }
259
+ const problem = checkRules(props[FORM_CONDITIONS_FIELD], String(props.name ?? ''), i);
260
+ if (problem)
261
+ return { ok: false, error: problem };
262
+ let id = createId();
263
+ while (ids.has(id))
264
+ id = createId();
265
+ ids.add(id);
266
+ const index = typeof change.index === 'number' && Number.isInteger(change.index) ? Math.max(0, Math.min(change.index, fields.length)) : fields.length;
267
+ const clean = Object.fromEntries(Object.entries(props).filter(([, v]) => v !== null && v !== undefined));
268
+ const className = classNameOf(isPlainObject(change.values) ? change.values : {});
269
+ ops.push({ type: 'insert', block: { id, type: def.type, props: clean, ...(className ? { className } : {}) }, to: { parentId: null, slot: 'children', index } });
270
+ fields = [...fields.slice(0, index), { id, type: def.type, props: clean }, ...fields.slice(index)];
271
+ if (!named)
272
+ break;
273
+ notes.push(`Added "${String(clean.name)}" (${formFieldSlug(def.type)}) with id ${id}.`);
274
+ break;
275
+ }
276
+ case 'update': {
277
+ const field = find(change.field);
278
+ if (!field)
279
+ return { ok: false, error: missing(change.field, i) };
280
+ const values = normalizeValues(isPlainObject(change.values) ? change.values : {});
281
+ if (typeof values === 'string')
282
+ return { ok: false, error: `Change ${i + 1}: ${values}` };
283
+ const unset = Array.isArray(change.unset) ? change.unset.filter((k) => typeof k === 'string' && k !== 'name') : [];
284
+ const oldName = nameOf(field);
285
+ if ('name' in values) {
286
+ const next = values.name;
287
+ if (typeof next !== 'string' || !NAME.test(next))
288
+ return { ok: false, error: `Change ${i + 1}: the name must use letters, digits and _ only.` };
289
+ if (next !== oldName && fields.some((f) => f !== field && nameOf(f) === next))
290
+ return { ok: false, error: `Change ${i + 1}: another field is already named "${next}".` };
291
+ }
292
+ if (FORM_CONDITIONS_FIELD in values) {
293
+ const problem = checkRules(values[FORM_CONDITIONS_FIELD], typeof values.name === 'string' ? values.name : oldName, i);
294
+ if (problem)
295
+ return { ok: false, error: problem };
296
+ }
297
+ const props = Object.fromEntries(Object.entries(values).filter(([, v]) => v !== undefined));
298
+ const className = classNameOf(isPlainObject(change.values) ? change.values : {});
299
+ if (Object.keys(props).length === 0 && unset.length === 0 && className === undefined)
300
+ return { ok: false, error: `Change ${i + 1}: \`values\` or \`unset\` is needed.` };
301
+ ops.push({
302
+ type: 'update',
303
+ id: field.id,
304
+ ...(Object.keys(props).length > 0 ? { props } : {}),
305
+ ...(unset.length > 0 ? { unsetProps: unset } : {}),
306
+ ...(className !== undefined ? { className } : {}),
307
+ });
308
+ field.props = { ...field.props, ...props };
309
+ for (const key of unset)
310
+ delete field.props[key];
311
+ // A rename: rules that read the old name now read the new one.
312
+ const newName = nameOf(field);
313
+ if (oldName && newName !== oldName) {
314
+ for (const other of fields) {
315
+ const conditions = other.props[FORM_CONDITIONS_FIELD];
316
+ if (other === field || !isPlainObject(conditions) || !Array.isArray(conditions.rules))
317
+ continue;
318
+ if (!conditions.rules.some((r) => isPlainObject(r) && r.field === oldName))
319
+ continue;
320
+ const next = { ...conditions, rules: conditions.rules.map((r) => (isPlainObject(r) && r.field === oldName ? { ...r, field: newName } : r)) };
321
+ ops.push({ type: 'update', id: other.id, props: { [FORM_CONDITIONS_FIELD]: next } });
322
+ other.props = { ...other.props, [FORM_CONDITIONS_FIELD]: next };
323
+ notes.push(`The conditions of "${nameOf(other)}" now read "${newName}".`);
324
+ }
325
+ }
326
+ break;
327
+ }
328
+ case 'move': {
329
+ const field = find(change.field);
330
+ if (!field)
331
+ return { ok: false, error: missing(change.field, i) };
332
+ if (typeof change.index !== 'number' || !Number.isInteger(change.index) || change.index < 0)
333
+ return { ok: false, error: `Change ${i + 1}: \`index\` must be a whole number from 0.` };
334
+ const rest = fields.filter((f) => f !== field);
335
+ const index = Math.min(change.index, rest.length);
336
+ ops.push({ type: 'move', id: field.id, to: { parentId: null, slot: 'children', index } });
337
+ fields = [...rest.slice(0, index), field, ...rest.slice(index)];
338
+ break;
339
+ }
340
+ case 'remove': {
341
+ const field = find(change.field);
342
+ if (!field)
343
+ return { ok: false, error: missing(change.field, i) };
344
+ ops.push({ type: 'remove', id: field.id });
345
+ fields = fields.filter((f) => f !== field);
346
+ const name = nameOf(field);
347
+ const readers = name
348
+ ? fields.filter((f) => {
349
+ const c = f.props[FORM_CONDITIONS_FIELD];
350
+ return isPlainObject(c) && Array.isArray(c.rules) && c.rules.some((r) => isPlainObject(r) && r.field === name);
351
+ })
352
+ : [];
353
+ if (readers.length > 0)
354
+ notes.push(`Rules of ${readers.map((f) => `"${nameOf(f)}"`).join(', ')} still read "${name}", which is gone: such a rule sees an empty value. Change or clear them with setConditions.`);
355
+ break;
356
+ }
357
+ case 'setConditions': {
358
+ const field = find(change.field);
359
+ if (!field)
360
+ return { ok: false, error: missing(change.field, i) };
361
+ const conditions = conditionsValue(change.conditions);
362
+ if (typeof conditions === 'string')
363
+ return { ok: false, error: `Change ${i + 1}: ${conditions}` };
364
+ const problem = checkRules(conditions, nameOf(field), i);
365
+ if (problem)
366
+ return { ok: false, error: problem };
367
+ // No rules: the group stays, empty, as Payload's form leaves it.
368
+ const value = conditions ?? { match: 'all', rules: [] };
369
+ ops.push({ type: 'update', id: field.id, props: { [FORM_CONDITIONS_FIELD]: value } });
370
+ field.props = { ...field.props, [FORM_CONDITIONS_FIELD]: value };
371
+ break;
372
+ }
373
+ default:
374
+ return { ok: false, error: `Change ${i + 1}: \`action\` must be add, update, move, remove or setConditions.` };
375
+ }
376
+ }
377
+ return { ok: true, ops, notes };
378
+ }
@@ -0,0 +1,81 @@
1
+ import type { Field } from 'payload';
2
+ import type { Block } from '../core/types.js';
3
+ /** The row key of a field's own classes (the box around label and input). Older forms typed them as "CSS class". */
4
+ export declare const FORM_FIELD_CLASS_KEY = "cssClass";
5
+ /** The row key of a field's part classes (label, input, help text, error, options). */
6
+ export declare const FORM_FIELD_PARTS_KEY = "partClasses";
7
+ /** The plugin's `width` setting: a percentage of the row. The builder sets a field's width with classes instead. */
8
+ export declare const FORM_FIELD_WIDTH_KEY = "width";
9
+ /**
10
+ * The classes of the old width presets (percent of the row -> the field's width from the `sm`
11
+ * breakpoint, less its share of the 1rem gap between fields, so the fields of one row fit). Full
12
+ * width has no class. The site drew these before the field's width moved to the Styles panel.
13
+ */
14
+ export declare const FORM_FIELD_WIDTH_CLASSES: {
15
+ readonly 75: 'sm:w-[calc(75%-0.25rem)]';
16
+ readonly 67: 'sm:w-[calc(66.667%-0.333rem)]';
17
+ readonly 50: 'sm:w-[calc(50%-0.5rem)]';
18
+ readonly 33: 'sm:w-[calc(33.333%-0.667rem)]';
19
+ readonly 25: 'sm:w-[calc(25%-0.75rem)]';
20
+ };
21
+ /**
22
+ * The class of a stored width: the smallest preset that holds it (40 -> the 50% class). No width,
23
+ * 0, above 75 or not a number: '' (full width).
24
+ */
25
+ export declare function formFieldWidthClass(width: unknown): string;
26
+ /** True when the classes set the box's width (at any breakpoint or state). */
27
+ export declare function setsWidth(className: string | null | undefined): boolean;
28
+ /**
29
+ * The field's classes with the class of its stored `width` added, unless they set a width
30
+ * themselves. The same classes when there is nothing to add. The site renders old fields with it,
31
+ * and `withWidthClass` stores it.
32
+ */
33
+ export declare function withStoredWidth(className: string | null | undefined, width: unknown): string;
34
+ /**
35
+ * A form field block of the time before widths were classes: its stored `width` becomes classes
36
+ * (see `withStoredWidth`) and `width` becomes `null`, so the next save clears the plugin's value
37
+ * and only the classes decide the width. The same block when it has no width. The builder runs it
38
+ * when it opens a layout (`BlockDefinition.upgrade`).
39
+ */
40
+ export declare function withWidthClass(block: Block): Block;
41
+ /** How radio buttons show: one per line, on one line, as joined buttons, or as cards. */
42
+ export declare const RADIO_DISPLAYS: readonly ['list', 'inline', 'buttons', 'cards'];
43
+ export type RadioDisplay = (typeof RADIO_DISPLAYS)[number];
44
+ /** The display of a radio field. Unknown or missing: "list". */
45
+ export declare function radioDisplay(value: unknown): RadioDisplay;
46
+ /** Most files a file field takes when it allows several and sets no number. */
47
+ export declare const DEFAULT_MAX_FILES = 5;
48
+ /** Upper limit of `maxFiles`. */
49
+ export declare const MAX_FILES_LIMIT = 20;
50
+ /** How many files a file field takes: 1, or its `maxFiles` (default 5, at most 20) when it allows several. */
51
+ export declare function maxFileCount(field: {
52
+ multiple?: boolean | null;
53
+ maxFiles?: number | null;
54
+ }): number;
55
+ /** True when the field's label shows. "hidden" keeps it for screen readers only. */
56
+ export declare const isLabelShown: (field: {
57
+ labelVisibility?: string | null;
58
+ }) => boolean;
59
+ /** A file field's "Allow several files" box and its "Most files" number. */
60
+ export declare function fileCountFields(): Field[];
61
+ type BlockLike = {
62
+ slug: string;
63
+ fields: Field[];
64
+ };
65
+ /**
66
+ * The fields with the plugin's `width` number (also inside rows) hidden: a field's width is its
67
+ * classes now (Styles panel). The stored value stays readable (`withWidthClass`). The same list
68
+ * when there is no visible width.
69
+ */
70
+ export declare function withHiddenWidth<T>(fields: T[]): T[];
71
+ /**
72
+ * The block with the settings it lacks for its type: description, placeholder, default value,
73
+ * label visibility, radio display, several files, and the hidden class fields (the field's classes
74
+ * and part classes, edited in the builder's Styles panel; the old "CSS class" values stay). The
75
+ * description goes right after the label; the other settings go before the display conditions.
76
+ * The plugin's `width` is hidden (`withHiddenWidth`): the width is classes. Known types only; a
77
+ * setting the block has already stays as it is. For the form-builder plugin's `fields` option:
78
+ * `withFormFieldSettings(fields.text)`. The plugin stores form fields as JSON, so no migration is needed.
79
+ */
80
+ export declare function withFormFieldSettings<T extends BlockLike>(block: T): T;
81
+ export {};