@cossackframework/cms 0.0.0 → 0.9.1

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 (427) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +145 -2
  3. package/agent-docs.json +259 -0
  4. package/app/README.md +78 -0
  5. package/app/cossack-recipe.json +43 -0
  6. package/app/src/auth-bot-check.ts +32 -0
  7. package/app/src/auth-bot-token.ts +11 -0
  8. package/app/src/auth-input.ts +35 -0
  9. package/app/src/auth-options.ts +18 -0
  10. package/app/src/auth-providers.ts +16 -0
  11. package/app/src/auth.ts +1082 -0
  12. package/app/src/brand.ts +68 -0
  13. package/app/src/cms/ai.ts +54 -0
  14. package/app/src/cms/analytics.ts +33 -0
  15. package/app/src/cms/block-errors.ts +55 -0
  16. package/app/src/cms/block-inserter.ts +111 -0
  17. package/app/src/cms/block-panel.ts +68 -0
  18. package/app/src/cms/block-rows.ts +44 -0
  19. package/app/src/cms/block-tree.ts +585 -0
  20. package/app/src/cms/blocks.css +708 -0
  21. package/app/src/cms/builder.ts +126 -0
  22. package/app/src/cms/cache.ts +44 -0
  23. package/app/src/cms/carousel.client.ts +52 -0
  24. package/app/src/cms/collab/presence.ts +69 -0
  25. package/app/src/cms/collab/rich-text-collab.client.ts +197 -0
  26. package/app/src/cms/collab/rich-text-notes.client.ts +138 -0
  27. package/app/src/cms/collab/room.ts +881 -0
  28. package/app/src/cms/compare.client.ts +47 -0
  29. package/app/src/cms/condition-control.ts +109 -0
  30. package/app/src/cms/content-api.ts +143 -0
  31. package/app/src/cms/content.ts +524 -0
  32. package/app/src/cms/countdown.client.ts +37 -0
  33. package/app/src/cms/editor-layout-editor.ts +260 -0
  34. package/app/src/cms/editor-layout-view.ts +37 -0
  35. package/app/src/cms/editor.ts +381 -0
  36. package/app/src/cms/entry.ts +26 -0
  37. package/app/src/cms/feeds.ts +145 -0
  38. package/app/src/cms/field-controls.ts +222 -0
  39. package/app/src/cms/field-group-editor.ts +105 -0
  40. package/app/src/cms/field-group-view.ts +211 -0
  41. package/app/src/cms/form-view.ts +203 -0
  42. package/app/src/cms/form.client.ts +203 -0
  43. package/app/src/cms/guards.ts +27 -0
  44. package/app/src/cms/handoff.ts +202 -0
  45. package/app/src/cms/headings.ts +51 -0
  46. package/app/src/cms/icons.ts +80 -0
  47. package/app/src/cms/import.client.ts +16 -0
  48. package/app/src/cms/index.ts +191 -0
  49. package/app/src/cms/lightbox.client.ts +109 -0
  50. package/app/src/cms/machine.ts +118 -0
  51. package/app/src/cms/maintenance.ts +97 -0
  52. package/app/src/cms/manage.ts +1236 -0
  53. package/app/src/cms/mcp-tools.ts +388 -0
  54. package/app/src/cms/mcp.ts +242 -0
  55. package/app/src/cms/media-chooser.ts +123 -0
  56. package/app/src/cms/media-usage.ts +57 -0
  57. package/app/src/cms/middleware.ts +199 -0
  58. package/app/src/cms/middlewares.ts +13 -0
  59. package/app/src/cms/network-ui.ts +36 -0
  60. package/app/src/cms/network-view.ts +38 -0
  61. package/app/src/cms/notes-announce.ts +65 -0
  62. package/app/src/cms/notes-rail.ts +207 -0
  63. package/app/src/cms/notification-rules.ts +66 -0
  64. package/app/src/cms/recovery.ts +82 -0
  65. package/app/src/cms/replace.ts +63 -0
  66. package/app/src/cms/reserved-paths.ts +19 -0
  67. package/app/src/cms/rich-text-field.ts +325 -0
  68. package/app/src/cms/rich-text-schema.ts +91 -0
  69. package/app/src/cms/rich-text.client.ts +255 -0
  70. package/app/src/cms/search.client.ts +116 -0
  71. package/app/src/cms/seo-head.ts +78 -0
  72. package/app/src/cms/seo.ts +241 -0
  73. package/app/src/cms/settings-draft.ts +218 -0
  74. package/app/src/cms/settings-file.ts +127 -0
  75. package/app/src/cms/settings-form.ts +180 -0
  76. package/app/src/cms/settings-languages.ts +125 -0
  77. package/app/src/cms/settings-nav.ts +102 -0
  78. package/app/src/cms/settings-network.ts +151 -0
  79. package/app/src/cms/settings-permalinks.ts +114 -0
  80. package/app/src/cms/settings-scroll.ts +10 -0
  81. package/app/src/cms/settings-search.ts +77 -0
  82. package/app/src/cms/settings-section.ts +38 -0
  83. package/app/src/cms/share.client.ts +27 -0
  84. package/app/src/cms/site-editing.ts +219 -0
  85. package/app/src/cms/site-editor.client.ts +1008 -0
  86. package/app/src/cms/site-editor.ts +99 -0
  87. package/app/src/cms/site-export.ts +60 -0
  88. package/app/src/cms/site-forms.ts +143 -0
  89. package/app/src/cms/site-import.ts +28 -0
  90. package/app/src/cms/site-session.ts +36 -0
  91. package/app/src/cms/site-taskbar.ts +77 -0
  92. package/app/src/cms/site-view.ts +1014 -0
  93. package/app/src/cms/site.ts +1322 -0
  94. package/app/src/cms/sitemaps.ts +131 -0
  95. package/app/src/cms/stat.client.ts +56 -0
  96. package/app/src/cms/style-controls.ts +252 -0
  97. package/app/src/cms/tabs.client.ts +36 -0
  98. package/app/src/cms/taskbar.ts +134 -0
  99. package/app/src/cms/term-icons.ts +10 -0
  100. package/app/src/cms/traffic.ts +26 -0
  101. package/app/src/cms/ui.ts +170 -0
  102. package/app/src/cms/updates.ts +124 -0
  103. package/app/src/cms/uploads.ts +26 -0
  104. package/app/src/cms/worker.ts +13 -0
  105. package/app/src/config/auth.ts +30 -0
  106. package/app/src/config/cms.ts +18 -0
  107. package/app/src/dashboard/access.ts +344 -0
  108. package/app/src/dashboard/agent-icons.ts +44 -0
  109. package/app/src/dashboard/app-passwords.ts +74 -0
  110. package/app/src/dashboard/avatar-field.ts +58 -0
  111. package/app/src/dashboard/bulk.ts +53 -0
  112. package/app/src/dashboard/head.ts +29 -0
  113. package/app/src/dashboard/icons.ts +15 -0
  114. package/app/src/dashboard/list-table.ts +120 -0
  115. package/app/src/dashboard/modules/roles.ts +15 -0
  116. package/app/src/dashboard/modules/sessions.ts +15 -0
  117. package/app/src/dashboard/modules/settings.ts +15 -0
  118. package/app/src/dashboard/modules/users.ts +18 -0
  119. package/app/src/dashboard/page.ts +67 -0
  120. package/app/src/dashboard/registry.ts +13 -0
  121. package/app/src/dashboard/session-list.ts +98 -0
  122. package/app/src/dashboard/tabs.ts +26 -0
  123. package/app/src/dashboard/time-ago.ts +16 -0
  124. package/app/src/dashboard/types.ts +40 -0
  125. package/app/src/dashboard/user-sections.ts +172 -0
  126. package/app/src/lib/permissions.ts +84 -0
  127. package/app/src/middlewares/auth.ts +117 -0
  128. package/app/src/pages/(site)/[...path]/index.ts +609 -0
  129. package/app/src/pages/(site)/feed/[...rest]/index.ts +13 -0
  130. package/app/src/pages/(site)/feed/index.ts +12 -0
  131. package/app/src/pages/(site)/index.ts +11 -0
  132. package/app/src/pages/(site)/indexnow/[file]/index.ts +18 -0
  133. package/app/src/pages/(site)/layout.ts +219 -0
  134. package/app/src/pages/(site)/llms.txt/index.ts +23 -0
  135. package/app/src/pages/(site)/manifest.webmanifest/index.ts +29 -0
  136. package/app/src/pages/(site)/media/[id]/[name]/index.ts +59 -0
  137. package/app/src/pages/(site)/preview/[collection]/[id]/index.ts +115 -0
  138. package/app/src/pages/(site)/robots.txt/index.ts +29 -0
  139. package/app/src/pages/(site)/search/index.ts +36 -0
  140. package/app/src/pages/(site)/sitemap.xml/index.ts +26 -0
  141. package/app/src/pages/(site)/sitemaps/[file]/index.ts +33 -0
  142. package/app/src/pages/api/auth/session.ts +48 -0
  143. package/app/src/pages/api/cms/appearance/areas/[name].ts +12 -0
  144. package/app/src/pages/api/cms/appearance/areas/index.ts +6 -0
  145. package/app/src/pages/api/cms/appearance/locations/[location].ts +6 -0
  146. package/app/src/pages/api/cms/appearance/locations/index.ts +6 -0
  147. package/app/src/pages/api/cms/appearance/menus/[id].ts +12 -0
  148. package/app/src/pages/api/cms/appearance/menus/index.ts +13 -0
  149. package/app/src/pages/api/cms/appearance/themes/[name]/activate.ts +6 -0
  150. package/app/src/pages/api/cms/appearance/themes/index.ts +6 -0
  151. package/app/src/pages/api/cms/cache/purge.ts +6 -0
  152. package/app/src/pages/api/cms/collections/index.ts +6 -0
  153. package/app/src/pages/api/cms/commands/[plugin]/[command].ts +26 -0
  154. package/app/src/pages/api/cms/comments/[id].ts +9 -0
  155. package/app/src/pages/api/cms/comments/index.ts +126 -0
  156. package/app/src/pages/api/cms/comments/queue.ts +6 -0
  157. package/app/src/pages/api/cms/content/[collection]/[entry]/autosave.ts +27 -0
  158. package/app/src/pages/api/cms/content/[collection]/[entry]/blocks/[block].ts +16 -0
  159. package/app/src/pages/api/cms/content/[collection]/[entry]/notes.ts +18 -0
  160. package/app/src/pages/api/cms/content/[collection]/[entry]/publish.ts +11 -0
  161. package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/index.ts +6 -0
  162. package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/restore.ts +6 -0
  163. package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/index.ts +6 -0
  164. package/app/src/pages/api/cms/content/[collection]/[entry]/unpublish.ts +6 -0
  165. package/app/src/pages/api/cms/content/[collection]/[entry].ts +42 -0
  166. package/app/src/pages/api/cms/content/[collection]/index.ts +24 -0
  167. package/app/src/pages/api/cms/export/[collection].ts +37 -0
  168. package/app/src/pages/api/cms/forms/[slug]/index.ts +173 -0
  169. package/app/src/pages/api/cms/forms/[slug]/submissions/[submission]/files/[file].ts +25 -0
  170. package/app/src/pages/api/cms/forms/[slug]/submissions/export.ts +34 -0
  171. package/app/src/pages/api/cms/forms/[slug]/submissions/index.ts +24 -0
  172. package/app/src/pages/api/cms/import/[collection].ts +23 -0
  173. package/app/src/pages/api/cms/mcp.ts +29 -0
  174. package/app/src/pages/api/cms/me/index.ts +6 -0
  175. package/app/src/pages/api/cms/me/recent-blocks.ts +37 -0
  176. package/app/src/pages/api/cms/me/tokens/[id].ts +6 -0
  177. package/app/src/pages/api/cms/me/tokens/index.ts +13 -0
  178. package/app/src/pages/api/cms/media/[id].ts +6 -0
  179. package/app/src/pages/api/cms/media.ts +26 -0
  180. package/app/src/pages/api/cms/menus/[location].ts +14 -0
  181. package/app/src/pages/api/cms/network/sites/[id]/export.ts +15 -0
  182. package/app/src/pages/api/cms/network/sites/[id]/index.ts +9 -0
  183. package/app/src/pages/api/cms/network/sites/index.ts +14 -0
  184. package/app/src/pages/api/cms/notes/[id].ts +13 -0
  185. package/app/src/pages/api/cms/notes/index.ts +6 -0
  186. package/app/src/pages/api/cms/payments/webhook.ts +28 -0
  187. package/app/src/pages/api/cms/plugins/[name]/grant.ts +6 -0
  188. package/app/src/pages/api/cms/plugins/[name]/revoke.ts +6 -0
  189. package/app/src/pages/api/cms/plugins/index.ts +6 -0
  190. package/app/src/pages/api/cms/recovery.ts +29 -0
  191. package/app/src/pages/api/cms/redirects/index.ts +9 -0
  192. package/app/src/pages/api/cms/roles/index.ts +6 -0
  193. package/app/src/pages/api/cms/schema/[collection].ts +13 -0
  194. package/app/src/pages/api/cms/search.ts +16 -0
  195. package/app/src/pages/api/cms/settings/[group].ts +9 -0
  196. package/app/src/pages/api/cms/settings/index.ts +9 -0
  197. package/app/src/pages/api/cms/site-export.ts +34 -0
  198. package/app/src/pages/api/cms/taxonomies/index.ts +6 -0
  199. package/app/src/pages/api/cms/terms/[taxonomy]/[id].ts +12 -0
  200. package/app/src/pages/api/cms/terms/[taxonomy]/index.ts +9 -0
  201. package/app/src/pages/api/cms/uploads/[id]/complete.ts +15 -0
  202. package/app/src/pages/api/cms/uploads/[id]/index.ts +15 -0
  203. package/app/src/pages/api/cms/uploads/index.ts +26 -0
  204. package/app/src/pages/api/cms/users/[id]/index.ts +12 -0
  205. package/app/src/pages/api/cms/users/[id]/meta.ts +40 -0
  206. package/app/src/pages/api/cms/users/index.ts +12 -0
  207. package/app/src/pages/api/cms/webhooks/[id].ts +6 -0
  208. package/app/src/pages/api/cms/webhooks/index.ts +9 -0
  209. package/app/src/pages/auth/[provider]/callback.ts +11 -0
  210. package/app/src/pages/auth/[provider]/redirect.ts +9 -0
  211. package/app/src/pages/auth/forgot-password/index.ts +75 -0
  212. package/app/src/pages/auth/layout.ts +32 -0
  213. package/app/src/pages/auth/login/index.ts +198 -0
  214. package/app/src/pages/auth/magic/index.ts +68 -0
  215. package/app/src/pages/auth/register/index.ts +117 -0
  216. package/app/src/pages/auth/reset-password/index.ts +65 -0
  217. package/app/src/pages/auth/setup/index.ts +194 -0
  218. package/app/src/pages/auth/two-factor/index.ts +67 -0
  219. package/app/src/pages/auth/verify/index.ts +62 -0
  220. package/app/src/pages/dashboard/(cms)/appearance/customize/index.ts +263 -0
  221. package/app/src/pages/dashboard/(cms)/appearance/dashboard-menu/index.ts +241 -0
  222. package/app/src/pages/dashboard/(cms)/appearance/index.ts +89 -0
  223. package/app/src/pages/dashboard/(cms)/appearance/menus/[id]/index.ts +697 -0
  224. package/app/src/pages/dashboard/(cms)/appearance/menus/index.ts +145 -0
  225. package/app/src/pages/dashboard/(cms)/appearance/widgets/index.ts +279 -0
  226. package/app/src/pages/dashboard/(cms)/blocks/index.ts +84 -0
  227. package/app/src/pages/dashboard/(cms)/calendar/index.ts +92 -0
  228. package/app/src/pages/dashboard/(cms)/collections/[name]/index.ts +525 -0
  229. package/app/src/pages/dashboard/(cms)/collections/index.ts +72 -0
  230. package/app/src/pages/dashboard/(cms)/collections/new/index.ts +66 -0
  231. package/app/src/pages/dashboard/(cms)/comments/index.ts +237 -0
  232. package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/index.ts +2382 -0
  233. package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/revisions/index.ts +126 -0
  234. package/app/src/pages/dashboard/(cms)/content/[collection]/index.ts +693 -0
  235. package/app/src/pages/dashboard/(cms)/field-groups/[id]/index.ts +193 -0
  236. package/app/src/pages/dashboard/(cms)/field-groups/index.ts +65 -0
  237. package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/[submission]/index.ts +279 -0
  238. package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/index.ts +170 -0
  239. package/app/src/pages/dashboard/(cms)/forms/index.ts +171 -0
  240. package/app/src/pages/dashboard/(cms)/media/[id]/index.ts +317 -0
  241. package/app/src/pages/dashboard/(cms)/media/index.ts +156 -0
  242. package/app/src/pages/dashboard/(cms)/media/new/index.ts +9 -0
  243. package/app/src/pages/dashboard/(cms)/media/summary/index.ts +74 -0
  244. package/app/src/pages/dashboard/(cms)/my-sites/index.ts +63 -0
  245. package/app/src/pages/dashboard/(cms)/network/index.ts +53 -0
  246. package/app/src/pages/dashboard/(cms)/network/plugins/index.ts +110 -0
  247. package/app/src/pages/dashboard/(cms)/network/sites/[id]/index.ts +243 -0
  248. package/app/src/pages/dashboard/(cms)/network/sites/index.ts +186 -0
  249. package/app/src/pages/dashboard/(cms)/network/sites/new/index.ts +77 -0
  250. package/app/src/pages/dashboard/(cms)/network/themes/index.ts +82 -0
  251. package/app/src/pages/dashboard/(cms)/network/updates/index.ts +48 -0
  252. package/app/src/pages/dashboard/(cms)/network/users/[id]/index.ts +176 -0
  253. package/app/src/pages/dashboard/(cms)/network/users/index.ts +174 -0
  254. package/app/src/pages/dashboard/(cms)/network/users/new/index.ts +66 -0
  255. package/app/src/pages/dashboard/(cms)/plugins/index.ts +172 -0
  256. package/app/src/pages/dashboard/(cms)/profile/public/index.ts +105 -0
  257. package/app/src/pages/dashboard/(cms)/registrations/index.ts +70 -0
  258. package/app/src/pages/dashboard/(cms)/settings/index.ts +359 -0
  259. package/app/src/pages/dashboard/(cms)/settings/json/index.ts +202 -0
  260. package/app/src/pages/dashboard/(cms)/settings/layout.ts +67 -0
  261. package/app/src/pages/dashboard/(cms)/taxonomies/[name]/index.ts +85 -0
  262. package/app/src/pages/dashboard/(cms)/taxonomies/index.ts +98 -0
  263. package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/[id]/index.ts +244 -0
  264. package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/index.ts +148 -0
  265. package/app/src/pages/dashboard/(cms)/tools/activity/index.ts +94 -0
  266. package/app/src/pages/dashboard/(cms)/tools/automations/index.ts +131 -0
  267. package/app/src/pages/dashboard/(cms)/tools/cache/index.ts +92 -0
  268. package/app/src/pages/dashboard/(cms)/tools/export/index.ts +99 -0
  269. package/app/src/pages/dashboard/(cms)/tools/health/index.ts +189 -0
  270. package/app/src/pages/dashboard/(cms)/tools/jobs/index.ts +93 -0
  271. package/app/src/pages/dashboard/(cms)/tools/links/index.ts +115 -0
  272. package/app/src/pages/dashboard/(cms)/tools/redirects/index.ts +161 -0
  273. package/app/src/pages/dashboard/(cms)/tools/replace/index.ts +124 -0
  274. package/app/src/pages/dashboard/(cms)/tools/schema/index.ts +54 -0
  275. package/app/src/pages/dashboard/(cms)/tools/seo/index.ts +156 -0
  276. package/app/src/pages/dashboard/(cms)/tools/updates/index.ts +114 -0
  277. package/app/src/pages/dashboard/(cms)/tools/webhooks/index.ts +182 -0
  278. package/app/src/pages/dashboard/index.ts +214 -0
  279. package/app/src/pages/dashboard/layout.ts +320 -0
  280. package/app/src/pages/dashboard/profile/index.ts +211 -0
  281. package/app/src/pages/dashboard/profile/security/index.ts +255 -0
  282. package/app/src/pages/dashboard/roles/[id]/index.ts +116 -0
  283. package/app/src/pages/dashboard/roles/index.ts +134 -0
  284. package/app/src/pages/dashboard/roles/new/index.ts +95 -0
  285. package/app/src/pages/dashboard/sessions/index.ts +105 -0
  286. package/app/src/pages/dashboard/users/[id]/index.ts +472 -0
  287. package/app/src/pages/dashboard/users/index.ts +299 -0
  288. package/app/src/pages/dashboard/users/new/index.ts +123 -0
  289. package/app/src/passkeys.client.ts +18 -0
  290. package/app/src/plugins/default-theme/assets/cover.svg +31 -0
  291. package/app/src/plugins/default-theme/assets/logo.svg +8 -0
  292. package/app/src/plugins/default-theme/parts/comments.ts +66 -0
  293. package/app/src/plugins/default-theme/parts/list.ts +38 -0
  294. package/app/src/plugins/default-theme/parts/media.ts +15 -0
  295. package/app/src/plugins/default-theme/parts/options.ts +45 -0
  296. package/app/src/plugins/default-theme/plugin.json +275 -0
  297. package/app/src/plugins/default-theme/templates/archive.ts +14 -0
  298. package/app/src/plugins/default-theme/templates/entry-blank.ts +13 -0
  299. package/app/src/plugins/default-theme/templates/entry.ts +27 -0
  300. package/app/src/plugins/default-theme/templates/home.ts +8 -0
  301. package/app/src/plugins/default-theme/templates/layout.ts +40 -0
  302. package/app/src/plugins/default-theme/templates/not-found.ts +10 -0
  303. package/app/src/plugins/default-theme/templates/search.ts +32 -0
  304. package/app/src/services/rbac.ts +65 -0
  305. package/app/src/services/roles.ts +89 -0
  306. package/app/src/services/users.ts +173 -0
  307. package/package.json +114 -2
  308. package/src/automations.ts +95 -0
  309. package/src/bindings.ts +138 -0
  310. package/src/block-references.ts +239 -0
  311. package/src/block-shape.ts +155 -0
  312. package/src/blocks.ts +1157 -0
  313. package/src/cache.ts +54 -0
  314. package/src/checks.ts +198 -0
  315. package/src/cloudflare-workers.d.ts +13 -0
  316. package/src/cloudflare.ts +61 -0
  317. package/src/collab.ts +372 -0
  318. package/src/collections.ts +421 -0
  319. package/src/comments.ts +46 -0
  320. package/src/cron.ts +95 -0
  321. package/src/dashboard-menu.ts +105 -0
  322. package/src/diff.ts +113 -0
  323. package/src/editor-layout.ts +316 -0
  324. package/src/errors.ts +50 -0
  325. package/src/field-groups.ts +498 -0
  326. package/src/fields.ts +411 -0
  327. package/src/form-builder.ts +1053 -0
  328. package/src/forms.ts +109 -0
  329. package/src/highlight.ts +134 -0
  330. package/src/hooks.ts +510 -0
  331. package/src/icon-catalog.ts +55 -0
  332. package/src/icons.ts +128 -0
  333. package/src/images.ts +149 -0
  334. package/src/index.ts +100 -0
  335. package/src/isolate-core.ts +245 -0
  336. package/src/isolate.ts +19 -0
  337. package/src/isolation.ts +69 -0
  338. package/src/mail.ts +182 -0
  339. package/src/manifest.ts +476 -0
  340. package/src/media.ts +193 -0
  341. package/src/menus.ts +287 -0
  342. package/src/module.ts +32 -0
  343. package/src/modules/content.ts +64 -0
  344. package/src/modules/forms.ts +35 -0
  345. package/src/node.ts +66 -0
  346. package/src/notes.ts +237 -0
  347. package/src/permissions.ts +139 -0
  348. package/src/plugin-settings.ts +114 -0
  349. package/src/records.ts +56 -0
  350. package/src/redirects.ts +59 -0
  351. package/src/resource.ts +245 -0
  352. package/src/richtext.ts +402 -0
  353. package/src/schema-json.ts +112 -0
  354. package/src/search.ts +89 -0
  355. package/src/seo.ts +477 -0
  356. package/src/server/accounts.ts +418 -0
  357. package/src/server/ai.ts +33 -0
  358. package/src/server/api-tokens.ts +91 -0
  359. package/src/server/areas.ts +116 -0
  360. package/src/server/audit.ts +94 -0
  361. package/src/server/auth.ts +89 -0
  362. package/src/server/automations.ts +190 -0
  363. package/src/server/autosaves.ts +69 -0
  364. package/src/server/brand-icons.ts +50 -0
  365. package/src/server/brands.ts +86 -0
  366. package/src/server/budget.ts +97 -0
  367. package/src/server/cache.ts +104 -0
  368. package/src/server/capabilities.ts +333 -0
  369. package/src/server/cloudflare.ts +92 -0
  370. package/src/server/comments.ts +237 -0
  371. package/src/server/drafts.ts +176 -0
  372. package/src/server/field-groups.ts +88 -0
  373. package/src/server/files.ts +780 -0
  374. package/src/server/forms.ts +726 -0
  375. package/src/server/handoffs.ts +107 -0
  376. package/src/server/host-schema.ts +312 -0
  377. package/src/server/host.ts +2892 -0
  378. package/src/server/images.ts +34 -0
  379. package/src/server/index.ts +59 -0
  380. package/src/server/indexnow.ts +91 -0
  381. package/src/server/isolation.ts +183 -0
  382. package/src/server/locked.ts +45 -0
  383. package/src/server/mail.ts +93 -0
  384. package/src/server/markdown.ts +24 -0
  385. package/src/server/menus.ts +286 -0
  386. package/src/server/network-policy.ts +60 -0
  387. package/src/server/network.ts +665 -0
  388. package/src/server/notes.ts +275 -0
  389. package/src/server/outbox.ts +213 -0
  390. package/src/server/path-moves.ts +33 -0
  391. package/src/server/payments.ts +59 -0
  392. package/src/server/previews.ts +62 -0
  393. package/src/server/purge.ts +140 -0
  394. package/src/server/reconcile.ts +38 -0
  395. package/src/server/redirects.ts +183 -0
  396. package/src/server/repository.ts +1107 -0
  397. package/src/server/schedules.ts +105 -0
  398. package/src/server/schema.ts +562 -0
  399. package/src/server/security.ts +391 -0
  400. package/src/server/seed.ts +52 -0
  401. package/src/server/shared-fields.ts +34 -0
  402. package/src/server/site-export.ts +151 -0
  403. package/src/server/site-import.ts +482 -0
  404. package/src/server/site-search.ts +289 -0
  405. package/src/server/site-settings.ts +286 -0
  406. package/src/server/sites.ts +243 -0
  407. package/src/server/stores.ts +314 -0
  408. package/src/server/stripe.ts +173 -0
  409. package/src/server/taxonomies.ts +415 -0
  410. package/src/server/tokens.ts +25 -0
  411. package/src/server/traffic.ts +118 -0
  412. package/src/server/users.ts +653 -0
  413. package/src/server/webhooks.ts +245 -0
  414. package/src/settings-controls.ts +54 -0
  415. package/src/settings-document.ts +111 -0
  416. package/src/settings.ts +426 -0
  417. package/src/storage/cloudflare.ts +94 -0
  418. package/src/storage/index.ts +30 -0
  419. package/src/storage/node.ts +114 -0
  420. package/src/taxonomy.ts +51 -0
  421. package/src/theme.ts +1135 -0
  422. package/src/to-markdown.ts +179 -0
  423. package/src/updates.ts +126 -0
  424. package/src/version.ts +2 -0
  425. package/src/vite.d.ts +25 -0
  426. package/src/vite.js +199 -0
  427. package/src/webhooks.ts +107 -0
@@ -0,0 +1,498 @@
1
+ /**
2
+ * Field groups: meta boxes built from blocks. A group lays typed fields out with the core
3
+ * layout blocks and attaches to collections, taxonomies, users or menu items; its field
4
+ * blocks compile to field declarations, so storage, the API and themes see fields, never
5
+ * layout. A repeater is a field block whose children are the fields of its rows, nested
6
+ * without limit; a field block with Multiple on holds several values of its kind. Pure
7
+ * functions, shared by the dashboard and the host.
8
+ */
9
+ import { field, fieldProblems, FIELD_NAME, ITEM_LIMITS, ROW_ID, ROW_LIMITS, validateValue, type FieldDeclaration, type FieldKind } from './fields';
10
+ import { FIELD_BLOCK_KINDS, flattenBlocks, validateBlocks, type Block, type BlockDeclaration, type FieldBlockKind } from './blocks';
11
+ import { conditionShows, type FormValue, type FormValues } from './form-builder';
12
+
13
+ // --- Field blocks -------------------------------------------------------------------------
14
+
15
+ /** The builder's field types as the picker shows them. */
16
+ export const FIELD_BLOCK_TYPES: readonly { readonly kind: FieldBlockKind; readonly label: string; readonly icon: string; readonly description: string }[] = Object.freeze([
17
+ { kind: 'text', label: 'Single line', icon: 'text-field', description: 'One line of text, such as a subtitle or SKU.' },
18
+ { kind: 'textarea', label: 'Long text', icon: 'text-square', description: 'Plain paragraphs.' },
19
+ { kind: 'markdown', label: 'Rich text', icon: 'text-bold-square', description: 'Markdown with a toolbar and preview.' },
20
+ { kind: 'number', label: 'Number', icon: 'hashtag-square', description: 'Prices, ratings and measurements.' },
21
+ { kind: 'integer', label: 'Whole number', icon: 'hashtag', description: 'Counts and positions.' },
22
+ { kind: 'boolean', label: 'Yes / no', icon: 'check-square', description: 'A checkbox.' },
23
+ { kind: 'date', label: 'Date and time', icon: 'calendar', description: 'Stored in UTC.' },
24
+ { kind: 'choice', label: 'Choice', icon: 'list', description: 'One of a fixed list.' },
25
+ { kind: 'media', label: 'Image or file', icon: 'gallery', description: 'A file from the media library; a gallery with Multiple on.' },
26
+ { kind: 'reference', label: 'Reference', icon: 'link-round', description: 'An entry of another collection.' },
27
+ { kind: 'blocks', label: 'Blocks', icon: 'widget-4', description: 'Content built from blocks, such as headings, images and buttons.' },
28
+ { kind: 'json', label: 'JSON', icon: 'code-square', description: 'Structured data for developers.' },
29
+ { kind: 'repeater', label: 'Repeater', icon: 'layers-minimalistic', description: 'Rows of fields, added as needed. Rows can hold repeaters of their own.' },
30
+ ]);
31
+
32
+ const LABEL = field.string({ required: true, max: 200, label: 'Label' });
33
+ const NAME = field.string({ max: 63, pattern: FIELD_NAME.source.slice(1, -1), label: 'Field name', description: 'The API, themes and filters use this name. Taken from the label when empty.' });
34
+ const HELP = field.string({ max: 500, label: 'Help text' });
35
+ const REQUIRED = field.boolean({ default: false, label: 'Required' });
36
+ const WIDTH = field.enum(['full', 'half', 'third'], { default: 'full', label: 'Width', choices: { full: 'Full width', half: 'Half', third: 'A third' } });
37
+ const INDEX = field.boolean({ default: false, label: 'Filterable', description: 'Keeps an index, so lists can filter on this field.' });
38
+ const SEARCH = field.boolean({ default: false, label: 'Searchable' });
39
+ const DEFAULT = field.string({ max: 500, label: 'Default value' });
40
+ const MAX_LENGTH = field.integer({ min: 1, max: 100_000, label: 'Maximum length' });
41
+ /** Several values of the kind: a text with Multiple on is a list of texts, an image a gallery. */
42
+ const MULTIPLE_SETTINGS: { readonly [name: string]: FieldDeclaration } = {
43
+ multiple: field.boolean({ default: false, label: 'Multiple', description: 'Several values, added one at a time.' }),
44
+ minItems: field.integer({ min: 0, max: ITEM_LIMITS.max, label: 'Fewest values' }),
45
+ maxItems: field.integer({ min: 1, max: ITEM_LIMITS.max, label: 'Most values' }),
46
+ addLabel: field.string({ max: 60, label: 'Add button', description: 'The button that adds a value; "Add new" when empty.' }),
47
+ };
48
+ /** Translations of an entry share the field's value instead of translating it. */
49
+ const SHARED = field.boolean({ default: false, label: 'Same in every language', description: 'One value for an entry and its translations: saving one copies it to the others.' });
50
+ /** Field kinds that are one value by nature. */
51
+ const SINGLE_KINDS: ReadonlySet<FieldBlockKind> = new Set<FieldBlockKind>(['boolean', 'blocks', 'repeater']);
52
+
53
+ /** The fields every field block has, then its own, then the Multiple settings unless the kind is one value by nature. */
54
+ export function fieldBlockFields(own: { readonly [name: string]: FieldDeclaration } = {}, options: { readonly multiple?: boolean } = {}): { [name: string]: FieldDeclaration } {
55
+ return { label: LABEL, name: NAME, description: HELP, required: REQUIRED, width: WIDTH, ...own, ...(options.multiple === false ? {} : MULTIPLE_SETTINGS) };
56
+ }
57
+
58
+ const declare = (kind: FieldBlockKind, own: { readonly [name: string]: FieldDeclaration } = {}, extra: Partial<BlockDeclaration> = {}): BlockDeclaration => {
59
+ const type = FIELD_BLOCK_TYPES.find(entry => entry.kind === kind)!;
60
+ return { name: `field/${kind}`, label: type.label, icon: type.icon, category: 'field', description: type.description, field: { kind }, fields: fieldBlockFields(own, { multiple: !SINGLE_KINDS.has(kind) }), ...extra };
61
+ };
62
+
63
+
64
+ /** Built-in field blocks, one per builder type. */
65
+ export const FIELD_BLOCKS: readonly BlockDeclaration[] = Object.freeze([
66
+ declare('text', { max: MAX_LENGTH, unique: field.boolean({ default: false, label: 'Unique', description: 'No two entries share a value.' }), index: INDEX, search: SEARCH, defaultValue: DEFAULT, shared: SHARED }),
67
+ declare('textarea', { max: MAX_LENGTH, search: SEARCH }),
68
+ declare('markdown', { max: MAX_LENGTH, search: SEARCH }),
69
+ declare('number', { min: field.number({ label: 'Minimum' }), max: field.number({ label: 'Maximum' }), index: INDEX, defaultValue: DEFAULT, shared: SHARED }),
70
+ declare('integer', { min: field.integer({ label: 'Minimum' }), max: field.integer({ label: 'Maximum' }), index: INDEX, defaultValue: DEFAULT, shared: SHARED }),
71
+ declare('boolean', { defaultValue: field.boolean({ default: false, label: 'Checked by default' }), shared: SHARED }),
72
+ declare('date', { index: INDEX, shared: SHARED }),
73
+ declare('choice', { choices: field.list('string', { label: 'Choices' }), index: INDEX, defaultValue: DEFAULT, shared: SHARED }),
74
+ declare('media', { shared: SHARED }),
75
+ declare('reference', { target: field.string({ required: true, max: 40, pattern: '[a-z][a-z0-9_]*', label: 'Collection' }), index: INDEX, shared: SHARED }),
76
+ declare('blocks', { blocks: field.list('string', { label: 'Allowed blocks', description: 'Block type names; empty allows all.' }) }),
77
+ declare('json', { shared: SHARED }),
78
+ declare('repeater', {
79
+ min: field.integer({ min: 0, max: ROW_LIMITS.max, label: 'Fewest rows' }),
80
+ max: field.integer({ min: 1, max: ROW_LIMITS.max, label: 'Most rows' }),
81
+ summary: field.string({ max: 63, pattern: FIELD_NAME.source.slice(1, -1), label: 'Row title', description: 'The field whose value names a collapsed row; the first text field when empty.' }),
82
+ }, { children: {} }),
83
+ ]);
84
+
85
+ /** Kinds a row field can have an index on: scalar values an array index can hold. */
86
+ const INDEXABLE_ROW_KINDS = new Set<FieldKind>(['string', 'enum', 'integer', 'number', 'boolean', 'datetime', 'reference']);
87
+
88
+ /** The declaration one field block describes. Its repeater children are the rows' fields. */
89
+ export function fieldBlockDeclaration(block: Block, types: ReadonlyMap<string, BlockDeclaration>): FieldDeclaration | null {
90
+ const kind = types.get(block.type)?.field?.kind;
91
+ if (!kind) return null;
92
+ const data = block.data;
93
+ const text = (key: string) => typeof data[key] === 'string' ? (data[key] as string).trim() : '';
94
+ const number = (key: string) => typeof data[key] === 'number' && Number.isFinite(data[key]) ? data[key] as number : undefined;
95
+ const flag = (key: string) => data[key] === true;
96
+ const required = flag('required');
97
+ const multiple = flag('multiple') && !SINGLE_KINDS.has(kind);
98
+ // A unique text, or a searched list of texts, is always translated.
99
+ const shared = flag('shared') && !['textarea', 'markdown', 'blocks', 'repeater'].includes(kind) && !(kind === 'text' && (flag('unique') && !multiple || multiple && flag('search')));
100
+ const common: Partial<FieldDeclaration> = {
101
+ label: text('label') || text('name'),
102
+ ...(shared ? { translatable: false } : {}),
103
+ ...(text('description') ? { description: text('description') } : {}),
104
+ ...(required ? { required: true } : {}),
105
+ ...(multiple ? {
106
+ multiple: true,
107
+ ...(number('minItems') !== undefined ? { minItems: number('minItems') } : {}),
108
+ ...(number('maxItems') !== undefined ? { maxItems: number('maxItems') } : {}),
109
+ ...(text('addLabel') ? { addLabel: text('addLabel') } : {}),
110
+ } : {}),
111
+ };
112
+ const max = number('max');
113
+ const min = number('min');
114
+ const bounds = { ...(max !== undefined ? { max } : {}), ...(min !== undefined ? { min } : {}) };
115
+ const index = flag('index') ? { index: true } : {};
116
+ const search = flag('search') ? { search: true } : {};
117
+ // A multiple field's default is one value per line; an empty line is no value.
118
+ const defaultText = text('defaultValue');
119
+ const defaults = multiple ? defaultText.split('\n').map(line => line.trim()).filter(Boolean) : defaultText ? [defaultText] : [];
120
+ const fallback = <T extends string | number>(values: readonly T[]): { default?: T | readonly T[] } => !values.length ? {} : multiple ? { default: values } : { default: values[0]! };
121
+ // A single optional value may be null; a multiple one is an empty list.
122
+ const optional = multiple ? {} : { nullable: !required };
123
+ switch (kind) {
124
+ case 'text': return field.string({ ...common, ...(max !== undefined ? { max } : {}), ...(flag('unique') && !multiple ? { unique: true } : index), ...search, ...fallback(defaults) });
125
+ case 'textarea': return field.text({ ...common, ...(max !== undefined ? { max } : {}), ...search });
126
+ case 'markdown': return field.text({ ...common, format: 'markdown', ...(max !== undefined ? { max } : {}), ...search });
127
+ case 'number': return field.number({ ...common, ...bounds, ...optional, ...index, ...fallback(defaults.map(Number).filter(value => Number.isFinite(value))) });
128
+ case 'integer': return field.integer({ ...common, ...bounds, ...optional, ...index, ...fallback(defaults.map(Number).filter(value => Number.isSafeInteger(value))) });
129
+ case 'boolean': return field.boolean({ ...common, default: data.defaultValue === true });
130
+ case 'date': return field.datetime({ ...common, ...optional, ...index });
131
+ case 'choice': {
132
+ const values = (Array.isArray(data.choices) ? data.choices as unknown[] : []).map(value => String(value).trim()).filter(Boolean);
133
+ return field.enum(values, { ...common, ...optional, ...index, ...fallback(defaults.filter(value => values.includes(value))) });
134
+ }
135
+ case 'media': return field.media({ ...common, ...optional });
136
+ case 'reference': return field.reference(`cms/${text('target')}`, { ...common, ...optional, ...index });
137
+ case 'blocks': {
138
+ const blocks = (Array.isArray(data.blocks) ? data.blocks as unknown[] : []).map(value => String(value).trim()).filter(Boolean);
139
+ return field.blocks({ ...common, ...(blocks.length ? { blocks } : {}) });
140
+ }
141
+ case 'json': return field.json(common);
142
+ case 'repeater': return field.rows(fieldGroupDeclarations(block.children ?? [], types), { ...common, ...bounds });
143
+ }
144
+ }
145
+
146
+ /**
147
+ * The fields a group's blocks declare, in document order: field blocks become declarations,
148
+ * layout blocks contribute what they hold, and a repeater's children are its rows' fields.
149
+ */
150
+ export function fieldGroupDeclarations(blocks: readonly Block[], types: ReadonlyMap<string, BlockDeclaration>): { [name: string]: FieldDeclaration } {
151
+ const fields: { [name: string]: FieldDeclaration } = {};
152
+ const visit = (list: readonly Block[]) => {
153
+ for (const block of list) {
154
+ const type = types.get(block.type);
155
+ if (type?.field) {
156
+ const declaration = fieldBlockDeclaration(block, types);
157
+ const name = typeof block.data.name === 'string' ? block.data.name.trim() : '';
158
+ if (declaration && name) fields[name] = declaration;
159
+ } else if (block.children) visit(block.children);
160
+ }
161
+ };
162
+ visit(blocks);
163
+ return fields;
164
+ }
165
+
166
+ /** The field blocks of a group at any depth, repeater children included. */
167
+ export function flattenGroupFields(blocks: readonly Block[]): Block[] {
168
+ return flattenBlocks(blocks).filter(block => block.type.startsWith('field/'));
169
+ }
170
+
171
+ /** The field block kind a declaration is edited as. */
172
+ export function fieldBlockKindOf(declaration: FieldDeclaration): FieldBlockKind {
173
+ switch (declaration.kind) {
174
+ case 'string': return declaration.media ? 'media' : 'text';
175
+ case 'text': return declaration.format === 'markdown' ? 'markdown' : 'textarea';
176
+ case 'number': return 'number';
177
+ case 'integer': case 'bigint': return 'integer';
178
+ case 'boolean': return 'boolean';
179
+ case 'datetime': return 'date';
180
+ case 'enum': return 'choice';
181
+ case 'reference': return 'reference';
182
+ case 'blocks': case 'richtext': return 'blocks';
183
+ case 'rows': return 'repeater';
184
+ default: return 'json';
185
+ }
186
+ }
187
+
188
+ /** The field block that edits a declaration, with its rows' fields as children. */
189
+ export function fieldBlockOf(name: string, declaration: FieldDeclaration, id = name): Block {
190
+ const kind = fieldBlockKindOf(declaration);
191
+ const data: { [key: string]: unknown } = { name, label: declaration.label ?? name };
192
+ if (declaration.description) data.description = declaration.description;
193
+ if (declaration.required) data.required = true;
194
+ if (declaration.max !== undefined && kind !== 'media') data.max = declaration.max;
195
+ if (declaration.min !== undefined) data.min = declaration.min;
196
+ if (declaration.unique) data.unique = true;
197
+ if (declaration.index && kind !== 'media') data.index = true;
198
+ if (declaration.multiple) {
199
+ data.multiple = true;
200
+ if (declaration.minItems !== undefined) data.minItems = declaration.minItems;
201
+ if (declaration.maxItems !== undefined) data.maxItems = declaration.maxItems;
202
+ if (declaration.addLabel) data.addLabel = declaration.addLabel;
203
+ }
204
+ if (declaration.search) data.search = true;
205
+ if (declaration.translatable === false) data.shared = true;
206
+ if (kind === 'choice') data.choices = [...(declaration.values ?? [])];
207
+ if (kind === 'reference') data.target = declaration.target?.startsWith('cms/') ? declaration.target.slice(4) : declaration.target ?? '';
208
+ if (kind === 'blocks' && declaration.blocks?.length) data.blocks = [...declaration.blocks];
209
+ if (declaration.default !== undefined && declaration.default !== null) data.defaultValue = kind === 'boolean' ? declaration.default === true : Array.isArray(declaration.default) ? declaration.default.map(String).join('\n') : String(declaration.default);
210
+ const children = kind === 'repeater' ? Object.entries(declaration.fields ?? {}).map(([sub, nested]) => fieldBlockOf(sub, nested, `${id}-${sub}`)) : undefined;
211
+ return { _id: id, type: `field/${kind}`, data, ...(children ? { children } : {}) };
212
+ }
213
+
214
+ /** A layout for declared fields: one field block each, in order. */
215
+ export function layoutOf(fields: { readonly [name: string]: FieldDeclaration }, order: readonly string[] = []): Block[] {
216
+ const names = [...order.filter(name => Object.hasOwn(fields, name)), ...Object.keys(fields).filter(name => !order.includes(name))];
217
+ return names.map(name => fieldBlockOf(name, fields[name]!));
218
+ }
219
+
220
+ // --- Groups -------------------------------------------------------------------------------
221
+
222
+ /** Where a field group shows. `order` places the box among the target's groups; the group's own `order` is the fallback. */
223
+ export type FieldGroupTarget = { readonly kind: 'collection' | 'taxonomy'; readonly name: string; readonly order?: number } | { readonly kind: 'users' | 'menus'; readonly order?: number };
224
+
225
+ export interface FieldGroup {
226
+ /** 1-40 letters, digits, `_` or `-`. */
227
+ readonly id: string;
228
+ readonly title: string;
229
+ readonly description?: string;
230
+ /** Where the box shows in an entry editor. Terms, users and menu items have one column. */
231
+ readonly placement?: 'main' | 'sidebar';
232
+ /** Boxes of one target in ascending order, for targets without their own `order`. */
233
+ readonly order?: number;
234
+ readonly targets: readonly FieldGroupTarget[];
235
+ readonly blocks: readonly Block[];
236
+ /** The plugin whose manifest ships the group: seeded when the plugin is granted, read-only in the dashboard. */
237
+ readonly plugin?: string;
238
+ }
239
+
240
+ export const FIELD_GROUP_ID = /^[A-Za-z0-9_-]{1,40}$/;
241
+ export const FIELD_GROUP_LIMITS = Object.freeze({ title: 120, description: 500, targets: 50 });
242
+
243
+ /** Whether two targets are the same place; their order does not count. */
244
+ export function sameTarget(a: FieldGroupTarget, b: FieldGroupTarget): boolean {
245
+ return a.kind === b.kind && ('name' in a ? a.name : '') === ('name' in b ? b.name : '');
246
+ }
247
+
248
+ /** A group's place among a target's boxes: the target's order, or the group's. */
249
+ export function targetOrder(group: FieldGroup, target: FieldGroupTarget): number {
250
+ return group.targets.find(entry => sameTarget(entry, target))?.order ?? group.order ?? 0;
251
+ }
252
+
253
+ /** The groups attached to a target, in their order there, then by the group's order and ID. */
254
+ export function groupsForTarget(groups: readonly FieldGroup[], target: FieldGroupTarget): FieldGroup[] {
255
+ return groups.filter(group => group.targets.some(entry => sameTarget(entry, target)))
256
+ .sort((a, b) => targetOrder(a, target) - targetOrder(b, target) || (a.order ?? 0) - (b.order ?? 0) || a.id.localeCompare(b.id));
257
+ }
258
+
259
+ /** The group with the target attached at `order` (in its place when already there), or without it. */
260
+ export function withTarget(group: FieldGroup, target: FieldGroupTarget, attached: boolean, order?: number): FieldGroup {
261
+ const { order: _order, ...place } = target;
262
+ const next = { ...place, ...(order !== undefined ? { order } : {}) };
263
+ const index = group.targets.findIndex(entry => sameTarget(entry, target));
264
+ if (!attached) return { ...group, targets: group.targets.filter(entry => !sameTarget(entry, target)) };
265
+ return { ...group, targets: index === -1 ? [...group.targets, next] : group.targets.map((entry, at) => at === index ? next : entry) };
266
+ }
267
+
268
+ /**
269
+ * Problems in a group, with its blocks checked as a field group: empty when valid. `scope`
270
+ * names the target's own fields, which the group's cannot take and conditions may test.
271
+ * Returns the checked blocks too, with names filled in from labels.
272
+ */
273
+ export function fieldGroupProblems(group: FieldGroup, types: ReadonlyMap<string, BlockDeclaration>, scope: readonly string[] = []): { problems: string[]; blocks: Block[]; fields: { [name: string]: FieldDeclaration } } {
274
+ const problems: string[] = [];
275
+ if (typeof group?.id !== 'string' || !FIELD_GROUP_ID.test(group.id)) problems.push('The ID must be 1-40 letters, digits, underscores or hyphens.');
276
+ if (typeof group?.title !== 'string' || !group.title.trim() || group.title.length > FIELD_GROUP_LIMITS.title) problems.push(`The title is required and at most ${FIELD_GROUP_LIMITS.title} characters.`);
277
+ if (group?.description !== undefined && (typeof group.description !== 'string' || group.description.length > FIELD_GROUP_LIMITS.description)) problems.push(`The description is at most ${FIELD_GROUP_LIMITS.description} characters.`);
278
+ if (group?.placement !== undefined && !['main', 'sidebar'].includes(group.placement)) problems.push('Placement is main or sidebar.');
279
+ if (group?.order !== undefined && !Number.isSafeInteger(group.order)) problems.push('Order is a whole number.');
280
+ if (group?.plugin !== undefined && (typeof group.plugin !== 'string' || !/^[a-z][a-z0-9-]{0,63}$/.test(group.plugin))) problems.push('Plugin is a plugin name.');
281
+ const targets = Array.isArray(group?.targets) ? group.targets : [];
282
+ if (!Array.isArray(group?.targets) || targets.length > FIELD_GROUP_LIMITS.targets) problems.push(`Targets list where the group shows, at most ${FIELD_GROUP_LIMITS.targets}.`);
283
+ for (const [index, target] of targets.entries()) {
284
+ const valid = target && typeof target === 'object' && (((target.kind === 'collection' || target.kind === 'taxonomy') && typeof (target as { name?: unknown }).name === 'string' && /^[a-z][a-zA-Z0-9_]{0,40}$/.test((target as { name: string }).name))
285
+ || ((target.kind === 'users' || target.kind === 'menus') && !('name' in target)));
286
+ if (!valid) problems.push(`Target ${index + 1} is not a collection, taxonomy, users or menus.`);
287
+ else if (target.order !== undefined && !Number.isSafeInteger(target.order)) problems.push(`Target ${index + 1}: the order is a whole number.`);
288
+ else if (targets.slice(0, index).some(other => sameTarget(other, target))) problems.push(`Target ${index + 1} is listed twice.`);
289
+ }
290
+ if (!Array.isArray(group?.blocks)) return { problems: [...problems, 'Blocks are required.'], blocks: [], fields: {} };
291
+ const checked = validateBlocks(group.blocks, types, { fieldGroup: { scope } });
292
+ problems.push(...Object.entries(checked.errors).map(([path, message]) => `Block ${path}: ${message}`));
293
+ const fields = fieldGroupDeclarations(checked.blocks, types);
294
+ for (const [name, declaration] of Object.entries(fields)) problems.push(...fieldProblems(name, declaration), ...rowIndexProblems(name, declaration));
295
+ return { problems, blocks: checked.blocks, fields };
296
+ }
297
+
298
+ /** A row field with an index must hold a scalar an array index can hold. */
299
+ function rowIndexProblems(name: string, declaration: FieldDeclaration): string[] {
300
+ if (declaration.kind !== 'rows') return [];
301
+ return Object.entries(declaration.fields ?? {}).flatMap(([sub, nested]) => [
302
+ ...((nested.index || nested.unique) && (!INDEXABLE_ROW_KINDS.has(nested.kind) || nested.multiple) ? [`field ${name}.${sub}: ${nested.multiple ? 'multiple' : nested.kind} row fields cannot be filterable.`] : []),
303
+ ...rowIndexProblems(`${name}.${sub}`, nested),
304
+ ]);
305
+ }
306
+
307
+ // --- Rows ---------------------------------------------------------------------------------
308
+
309
+ /** A row of a rows field, as stored. */
310
+ export type Row = { readonly _id?: string; readonly [field: string]: unknown };
311
+
312
+ /** Rows with an `_id` on each, from `next` when missing; nested rows too. */
313
+ export function withRowIds(declaration: FieldDeclaration, value: unknown, next: () => string): unknown {
314
+ if (declaration.kind !== 'rows' || !Array.isArray(value)) return value;
315
+ const taken = new Set(value.map(row => (row as Row)?._id).filter((id): id is string => typeof id === 'string'));
316
+ return value.map(row => {
317
+ if (!row || typeof row !== 'object' || Array.isArray(row)) return row;
318
+ const entries = row as Row;
319
+ let id = entries._id;
320
+ while (typeof id !== 'string' || !ROW_ID.test(id)) { id = next(); if (taken.has(id)) id = undefined; }
321
+ taken.add(id);
322
+ const nested = Object.fromEntries(Object.entries(declaration.fields ?? {}).filter(([sub]) => entries[sub] !== undefined).map(([sub, inner]) => [sub, withRowIds(inner, entries[sub], next)]));
323
+ return { ...entries, ...nested, _id: id };
324
+ });
325
+ }
326
+
327
+ /** Paths of the indexed or unique row fields at any depth, as field names from the top-level rows field. */
328
+ export function indexedRowPaths(fields: { readonly [name: string]: FieldDeclaration }): string[][] {
329
+ const paths: string[][] = [];
330
+ const visit = (declaration: FieldDeclaration, path: readonly string[]) => {
331
+ if (declaration.kind !== 'rows') return;
332
+ for (const [sub, nested] of Object.entries(declaration.fields ?? {})) {
333
+ const at = [...path, sub];
334
+ if ((nested.index || nested.unique) && INDEXABLE_ROW_KINDS.has(nested.kind) && !nested.multiple) paths.push(at);
335
+ visit(nested, at);
336
+ }
337
+ };
338
+ for (const [name, declaration] of Object.entries(fields)) visit(declaration, [name]);
339
+ return paths;
340
+ }
341
+
342
+ /** The indexed or unique row field a path such as `['team', 'links', 'kind']` names, or null. */
343
+ export function indexedRowField(fields: { readonly [name: string]: FieldDeclaration }, path: readonly string[]): FieldDeclaration | null {
344
+ let declaration = fields[path[0]!];
345
+ for (const name of path.slice(1)) {
346
+ if (declaration?.kind !== 'rows' || !Object.hasOwn(declaration.fields ?? {}, name)) return null;
347
+ declaration = declaration.fields![name];
348
+ }
349
+ return path.length > 1 && declaration && (declaration.index || declaration.unique) && INDEXABLE_ROW_KINDS.has(declaration.kind) && !declaration.multiple ? declaration : null;
350
+ }
351
+
352
+ /** The RomeDB array index path of a row field: `team.links.kind` steps into each row as `team.*.links.*.kind`. */
353
+ export function rowIndexPath(path: readonly string[]): string {
354
+ return path.join('.*.');
355
+ }
356
+
357
+ /** Whether a rows field has a searchable field at any depth, so its text is kept beside the document. */
358
+ export function rowsSearchable(declaration: FieldDeclaration): boolean {
359
+ return declaration.kind === 'rows' && Object.values(declaration.fields ?? {}).some(nested => nested.search === true || rowsSearchable(nested));
360
+ }
361
+
362
+ /** The text of a rows value's searchable fields, at any depth. */
363
+ export function rowsPlain(declaration: FieldDeclaration, value: unknown): string {
364
+ if (declaration.kind !== 'rows' || !Array.isArray(value)) return '';
365
+ const parts: string[] = [];
366
+ for (const row of value) {
367
+ if (!row || typeof row !== 'object') continue;
368
+ for (const [sub, nested] of Object.entries(declaration.fields ?? {})) {
369
+ const entry = (row as Row)[sub];
370
+ if (nested.kind === 'rows') parts.push(rowsPlain(nested, entry));
371
+ else if (nested.search && typeof entry === 'string' && entry.trim()) parts.push(entry.trim());
372
+ else if (nested.search && Array.isArray(entry)) parts.push(...entry.filter((item): item is string => typeof item === 'string' && !!item.trim()).map(item => item.trim()));
373
+ }
374
+ }
375
+ return parts.filter(Boolean).join('\n');
376
+ }
377
+
378
+ /** Whether a rows value repeats a value of a unique row field, at any depth. The first repeated path. */
379
+ export function rowUniqueProblem(name: string, declaration: FieldDeclaration, value: unknown): string | undefined {
380
+ if (declaration.kind !== 'rows') return undefined;
381
+ for (const [sub, nested] of Object.entries(declaration.fields ?? {})) {
382
+ if (nested.unique) {
383
+ const values = Array.isArray(value) ? value.map(row => (row as Row)?.[sub]).filter(entry => entry !== undefined && entry !== null && entry !== '') : [];
384
+ if (new Set(values.map(entry => String(entry))).size !== values.length) return `${name}: ${nested.label ?? sub} must be different in every row.`;
385
+ }
386
+ if (nested.kind === 'rows' && Array.isArray(value)) {
387
+ for (const row of value) { const problem = rowUniqueProblem(`${name}.${sub}`, nested, (row as Row)?.[sub]); if (problem) return problem; }
388
+ }
389
+ }
390
+ return undefined;
391
+ }
392
+
393
+ /**
394
+ * Problems in a target's meta values (terms, users, menu items) against its groups' fields:
395
+ * unknown names, invalid values, missing required fields. The values come back with row IDs.
396
+ */
397
+ /** A key of meta the host keeps beside the fields, such as a user's recent blocks. */
398
+ export const SYSTEM_META = /^_[a-z][a-z0-9_]{0,39}$/;
399
+
400
+ export function metaProblems(fields: { readonly [name: string]: FieldDeclaration }, value: unknown, next: () => string): { errors: { [name: string]: string }; values: { [name: string]: unknown } } {
401
+ const errors: { [name: string]: string } = {};
402
+ const values: { [name: string]: unknown } = {};
403
+ if (value === undefined || value === null) return { errors, values };
404
+ if (typeof value !== 'object' || Array.isArray(value)) return { errors: { meta: 'must be an object of fields.' }, values };
405
+ for (const [name, entry] of Object.entries(value as { [name: string]: unknown })) {
406
+ // `_` keys are the host's own, such as `_recent_blocks`: field names cannot start with `_`, so they are kept as they are.
407
+ if (SYSTEM_META.test(name)) { if (entry !== undefined && entry !== null) values[name] = entry; continue; }
408
+ // Own keys only: `constructor` or `__proto__` must not find Object's members.
409
+ const declaration = Object.hasOwn(fields, name) ? fields[name] : undefined;
410
+ if (!declaration) { errors[name] = 'is not a field here.'; continue; }
411
+ if (entry === undefined || entry === null || entry === '') continue;
412
+ const error = validateValue(name, declaration, entry);
413
+ if (error) { errors[name] = error; continue; }
414
+ values[name] = withRowIds(declaration, entry, next);
415
+ const unique = rowUniqueProblem(name, declaration, values[name]);
416
+ if (unique) errors[name] = unique.slice(name.length + 2);
417
+ }
418
+ for (const [name, declaration] of Object.entries(fields)) if (declaration.required && values[name] === undefined && !errors[name]) errors[name] = 'is required.';
419
+ return { errors, values };
420
+ }
421
+
422
+ // --- Conditions ---------------------------------------------------------------------------
423
+
424
+ /** Entry values as conditions read them: text, numbers, yes or no and lists of text; the rest does not count. */
425
+ export function conditionValues(values: { readonly [name: string]: unknown }): FormValues {
426
+ const result: { [name: string]: FormValue } = {};
427
+ for (const [name, value] of Object.entries(values)) {
428
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') result[name] = value;
429
+ else if (Array.isArray(value) && value.every(entry => typeof entry === 'string')) result[name] = value as string[];
430
+ }
431
+ return result;
432
+ }
433
+
434
+ /** A hidden field's key: the block's ID at the top, `<repeater>=<row id>/…/<block id>` in a row. */
435
+ export function hiddenKey(rowPath: readonly { name: string; id: string }[], blockId: string): string {
436
+ return [...rowPath.map(row => `${row.name}=${row.id}`), blockId].join('/');
437
+ }
438
+
439
+ /**
440
+ * Which blocks of a group its conditions hide for these values, as `hiddenKey`s, and the
441
+ * values with hidden fields cleared, rows included. A field in a row sees the row's values
442
+ * over the rows above it over the top level; hidden fields count as empty, so conditions
443
+ * that depend on them follow. Settles in a few rounds; a cycle stops at the last.
444
+ */
445
+ export function fieldVisibility(blocks: readonly Block[], types: ReadonlyMap<string, BlockDeclaration>, values: { readonly [name: string]: unknown }, scope: FormValues = {}): { hidden: ReadonlySet<string>; values: { [name: string]: unknown } } {
446
+ const fieldCount = flattenBlocks(blocks).filter(block => types.get(block.type)?.field).length;
447
+ let current: { [name: string]: unknown } = { ...values };
448
+ let hidden = new Set<string>();
449
+ for (let round = 0; round <= fieldCount; round++) {
450
+ const next = new Set<string>();
451
+ const detect = (list: readonly Block[], own: { readonly [name: string]: unknown }, visible: FormValues, rowPath: readonly { name: string; id: string }[], inside: boolean) => {
452
+ for (const block of list) {
453
+ const type = types.get(block.type);
454
+ const off = inside || !conditionShows(block.when, visible);
455
+ if (off) next.add(hiddenKey(rowPath, block._id));
456
+ if (!block.children) continue;
457
+ const name = type?.field && typeof block.data.name === 'string' ? block.data.name : '';
458
+ if (type?.field?.kind === 'repeater' && name) {
459
+ for (const row of (Array.isArray(own[name]) ? own[name] : []) as unknown[]) {
460
+ if (!row || typeof row !== 'object') continue;
461
+ const entries = row as Row;
462
+ detect(block.children, entries, { ...visible, ...conditionValues(entries) }, [...rowPath, { name, id: typeof entries._id === 'string' ? entries._id : '' }], off);
463
+ }
464
+ } else detect(block.children, own, visible, rowPath, off);
465
+ }
466
+ };
467
+ detect(blocks, current, { ...scope, ...conditionValues(current) }, [], false);
468
+ const cleared = clearHidden(blocks, types, values, next);
469
+ const settled = next.size === hidden.size && [...next].every(key => hidden.has(key));
470
+ hidden = next;
471
+ current = cleared;
472
+ if (settled) break;
473
+ }
474
+ return { hidden, values: current };
475
+ }
476
+
477
+ /** The values without the fields `hidden` names, rows included. */
478
+ export function clearHidden(blocks: readonly Block[], types: ReadonlyMap<string, BlockDeclaration>, values: { readonly [name: string]: unknown }, hidden: ReadonlySet<string>): { [name: string]: unknown } {
479
+ const clear = (list: readonly Block[], own: { [name: string]: unknown }, rowPath: readonly { name: string; id: string }[]) => {
480
+ for (const block of list) {
481
+ const type = types.get(block.type);
482
+ const name = type?.field && typeof block.data.name === 'string' ? block.data.name : '';
483
+ if (hidden.has(hiddenKey(rowPath, block._id))) { if (name) delete own[name]; continue; }
484
+ if (!block.children) continue;
485
+ if (type?.field?.kind === 'repeater' && name) {
486
+ if (Array.isArray(own[name])) own[name] = (own[name] as unknown[]).map(row => {
487
+ if (!row || typeof row !== 'object') return row;
488
+ const copy: { [name: string]: unknown } = { ...(row as Row) };
489
+ clear(block.children!, copy, [...rowPath, { name, id: typeof copy._id === 'string' ? copy._id : '' }]);
490
+ return copy;
491
+ });
492
+ } else clear(block.children, own, rowPath);
493
+ }
494
+ };
495
+ const result: { [name: string]: unknown } = { ...values };
496
+ clear(blocks, result, []);
497
+ return result;
498
+ }