@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,105 @@
1
+ /**
2
+ * The dashboard menu as administrators arrange it: which of the built-in modules show, in what
3
+ * order, and the links they add. Stored in the `dashboard.menu` setting; the application builds
4
+ * the default navigation for the signed-in user and applies this on top, so a module the user
5
+ * may not see stays away whatever the order says. Pure shapes and checks.
6
+ */
7
+
8
+ export interface DashboardMenuLink {
9
+ /** Letters, digits, `-` and `_`; unique among the links. */
10
+ readonly id: string;
11
+ readonly label: string;
12
+ /** A same-site path (`/dashboard/...`, `/...`) or an http(s) URL. */
13
+ readonly href: string;
14
+ /** An icon of the dashboard's set; the application falls back to a link icon. */
15
+ readonly icon?: string;
16
+ }
17
+
18
+ export interface DashboardMenu {
19
+ /** Module IDs (built-in `collection-posts`, `media`, ..., or `link-<id>`) in the order they show; unlisted ones follow in the default order. */
20
+ readonly order: readonly string[];
21
+ /** Module IDs that are not shown. */
22
+ readonly hidden: readonly string[];
23
+ readonly links: readonly DashboardMenuLink[];
24
+ }
25
+
26
+ export const EMPTY_DASHBOARD_MENU: DashboardMenu = Object.freeze({ order: [], hidden: [], links: [] });
27
+ export const DASHBOARD_MENU_LIMITS = Object.freeze({ links: 30, label: 60, href: 2000, ids: 200 });
28
+
29
+ const LINK_ID = /^[A-Za-z0-9_-]{1,40}$/;
30
+ const MODULE_ID = /^[A-Za-z0-9_.:/-]{1,120}$/;
31
+
32
+ /** The ID of the module a custom link becomes. */
33
+ export function dashboardLinkModuleId(link: Pick<DashboardMenuLink, 'id'>): string {
34
+ return `link-${link.id}`;
35
+ }
36
+
37
+ /** Whether a link's address stays on this site or names an http(s) URL. */
38
+ export function dashboardLinkHrefProblem(href: unknown): string | undefined {
39
+ if (typeof href !== 'string' || !href.trim()) return 'needs an address.';
40
+ if (href.length > DASHBOARD_MENU_LIMITS.href) return `is longer than ${DASHBOARD_MENU_LIMITS.href} characters.`;
41
+ if (/[\s<>"']/.test(href)) return 'has spaces or quotes.';
42
+ if (href.startsWith('/')) return href.startsWith('//') ? 'must not start with //.' : undefined;
43
+ try {
44
+ const url = new URL(href);
45
+ return url.protocol === 'http:' || url.protocol === 'https:' ? undefined : 'must be a path on this site or an http(s) URL.';
46
+ } catch {
47
+ return 'must be a path on this site or an http(s) URL.';
48
+ }
49
+ }
50
+
51
+ /** What is wrong with a menu, as sentences; empty when it can be saved. */
52
+ export function dashboardMenuProblems(input: unknown): string[] {
53
+ if (input === null || input === undefined) return [];
54
+ if (!input || typeof input !== 'object' || Array.isArray(input)) return ['The dashboard menu must be an object.'];
55
+ const value = input as Record<string, unknown>;
56
+ const problems: string[] = [];
57
+ for (const key of ['order', 'hidden'] as const) {
58
+ const list = value[key];
59
+ if (list === undefined) continue;
60
+ if (!Array.isArray(list) || list.length > DASHBOARD_MENU_LIMITS.ids || list.some(id => typeof id !== 'string' || !MODULE_ID.test(id))) problems.push(`${key} lists up to ${DASHBOARD_MENU_LIMITS.ids} module IDs.`);
61
+ else if (new Set(list).size !== list.length) problems.push(`${key} repeats a module.`);
62
+ }
63
+ const links = value.links;
64
+ if (links !== undefined) {
65
+ if (!Array.isArray(links) || links.length > DASHBOARD_MENU_LIMITS.links) problems.push(`links holds up to ${DASHBOARD_MENU_LIMITS.links} links.`);
66
+ else {
67
+ const ids = new Set<string>();
68
+ links.forEach((link, index) => {
69
+ const at = `Link ${index + 1}`;
70
+ if (!link || typeof link !== 'object') { problems.push(`${at} must be an object.`); return; }
71
+ const { id, label, href, icon } = link as Record<string, unknown>;
72
+ if (typeof id !== 'string' || !LINK_ID.test(id)) problems.push(`${at}: the ID is letters, digits, - and _.`);
73
+ else if (ids.has(id)) problems.push(`${at}: the ID ${id} is used twice.`);
74
+ else ids.add(id);
75
+ if (typeof label !== 'string' || !label.trim() || label.length > DASHBOARD_MENU_LIMITS.label) problems.push(`${at}: a label of 1 to ${DASHBOARD_MENU_LIMITS.label} characters.`);
76
+ const problem = dashboardLinkHrefProblem(href);
77
+ if (problem) problems.push(`${at}: the address ${problem}`);
78
+ if (icon !== undefined && (typeof icon !== 'string' || icon.length > 64)) problems.push(`${at}: the icon is a name of the dashboard's set.`);
79
+ });
80
+ }
81
+ }
82
+ for (const key of Object.keys(value)) if (!['order', 'hidden', 'links'].includes(key)) problems.push(`${key} is not part of the dashboard menu.`);
83
+ return problems;
84
+ }
85
+
86
+ /** A stored value as a menu; anything unusable is the empty menu, so a bad setting never hides the dashboard. */
87
+ export function dashboardMenuOf(value: unknown): DashboardMenu {
88
+ if (!value || typeof value !== 'object' || dashboardMenuProblems(value).length) return EMPTY_DASHBOARD_MENU;
89
+ const stored = value as Partial<DashboardMenu>;
90
+ return { order: [...(stored.order ?? [])], hidden: [...(stored.hidden ?? [])], links: (stored.links ?? []).map(link => ({ ...link })) };
91
+ }
92
+
93
+ /**
94
+ * The modules as the menu arranges them: the links become modules (through `link`), hidden ones
95
+ * leave, and the listed ones come first in the menu's order, the rest after in their own order.
96
+ */
97
+ export function applyDashboardMenu<T extends { readonly id: string }>(modules: readonly T[], menu: DashboardMenu, link: (entry: DashboardMenuLink) => T): T[] {
98
+ const all = [...modules, ...menu.links.map(link)];
99
+ const hidden = new Set(menu.hidden);
100
+ const rank = new Map(menu.order.map((id, index) => [id, index]));
101
+ return all.filter(module => !hidden.has(module.id))
102
+ .map((module, index) => ({ module, key: rank.get(module.id) ?? menu.order.length + index }))
103
+ .sort((a, b) => a.key - b.key)
104
+ .map(entry => entry.module);
105
+ }
package/src/diff.ts ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Revision comparison: a word-level diff of text, and a field-by-field comparison of two
3
+ * record snapshots. Client-safe; the revisions screen renders the parts.
4
+ */
5
+ import type { FieldDeclaration } from './fields';
6
+ import type { RecordShape } from './records';
7
+ import { richTextPlain, type RichTextDoc } from './richtext';
8
+
9
+ export interface DiffPart {
10
+ readonly kind: 'same' | 'added' | 'removed';
11
+ readonly text: string;
12
+ }
13
+
14
+ export interface FieldChange {
15
+ readonly name: string;
16
+ readonly label: string;
17
+ /** Text fields are diffed by word; other values are shown before and after. */
18
+ readonly kind: 'text' | 'value';
19
+ /** JSON fields: the texts are JSON documents, best compared line by line. Blocks keep the word diff, since their text is prose. */
20
+ readonly structured: boolean;
21
+ readonly before: string;
22
+ readonly after: string;
23
+ readonly parts: readonly DiffPart[];
24
+ }
25
+
26
+ /** Tokens: words, runs of spaces, and line breaks, so a diff keeps the text's layout. */
27
+ function tokens(text: string): string[] {
28
+ return text.match(/\n|[^\S\n]+|[^\s]+/g) ?? [];
29
+ }
30
+
31
+ /**
32
+ * The word-level difference between two texts (Myers' algorithm). Texts over `limit` tokens,
33
+ * or with more than `maxEdits` changed tokens, are reported as replaced, to bound the work.
34
+ */
35
+ export function diffWords(before: string, after: string, limit = 20_000, maxEdits = 4_000): DiffPart[] {
36
+ const replaced = () => merge([...(before ? [{ kind: 'removed' as const, text: before }] : []), ...(after ? [{ kind: 'added' as const, text: after }] : [])]);
37
+ const a = tokens(before);
38
+ const b = tokens(after);
39
+ if (a.length + b.length > limit) return replaced();
40
+ const max = Math.min(a.length + b.length, maxEdits);
41
+ const offset = max + 1;
42
+ const frontier = new Int32Array(2 * max + 3);
43
+ // trace[d] holds the frontier before round d, for diagonals -d-1..d+1.
44
+ const trace: Int32Array[] = [];
45
+ let found = -1;
46
+ for (let d = 0; d <= max && found < 0; d++) {
47
+ trace.push(frontier.slice(offset - d - 1, offset + d + 2));
48
+ for (let k = -d; k <= d; k += 2) {
49
+ let x = k === -d || (k !== d && frontier[offset + k - 1]! < frontier[offset + k + 1]!) ? frontier[offset + k + 1]! : frontier[offset + k - 1]! + 1;
50
+ let y = x - k;
51
+ while (x < a.length && y < b.length && a[x] === b[y]) { x++; y++; }
52
+ frontier[offset + k] = x;
53
+ if (x >= a.length && y >= b.length) { found = d; break; }
54
+ }
55
+ }
56
+ if (found < 0) return replaced();
57
+ // Walk the trace back from the end, collecting edits in reverse.
58
+ const parts: DiffPart[] = [];
59
+ let x = a.length;
60
+ let y = b.length;
61
+ for (let d = found; d > 0; d--) {
62
+ const previous = trace[d]!;
63
+ const at = (k: number) => previous[k + d + 1]!;
64
+ const k = x - y;
65
+ const down = k === -d || (k !== d && at(k - 1) < at(k + 1));
66
+ const priorK = down ? k + 1 : k - 1;
67
+ const priorX = at(priorK);
68
+ const priorY = priorX - priorK;
69
+ while (x > priorX && y > priorY) { parts.push({ kind: 'same', text: a[--x]! }); y--; }
70
+ if (down) parts.push({ kind: 'added', text: b[--y]! });
71
+ else parts.push({ kind: 'removed', text: a[--x]! });
72
+ }
73
+ while (x > 0 && y > 0) { parts.push({ kind: 'same', text: a[--x]! }); y--; }
74
+ return merge(parts.reverse());
75
+ }
76
+
77
+ /** Join neighbouring parts of the same kind. */
78
+ function merge(parts: readonly DiffPart[]): DiffPart[] {
79
+ const merged: DiffPart[] = [];
80
+ for (const part of parts) {
81
+ const last = merged.at(-1);
82
+ if (last && last.kind === part.kind) merged[merged.length - 1] = { kind: part.kind, text: last.text + part.text };
83
+ else if (part.text) merged.push(part);
84
+ }
85
+ return merged;
86
+ }
87
+
88
+ /** A value as the comparison shows it: rich text as its plain text, lists one per line. */
89
+ export function comparableValue(declaration: FieldDeclaration, value: unknown): string {
90
+ if (value === null || value === undefined) return '';
91
+ if (declaration.kind === 'richtext') return richTextPlain(value as RichTextDoc);
92
+ if (declaration.kind === 'boolean') return value ? 'Yes' : 'No';
93
+ if (declaration.kind === 'datetime' && typeof value === 'number') return new Date(value).toISOString().replace('T', ' ').slice(0, 16) + ' UTC';
94
+ if (Array.isArray(value) && declaration.multiple) return value.map(item => comparableValue({ ...declaration, multiple: false }, item)).join('\n');
95
+ if (typeof value === 'object') return JSON.stringify(value, null, 2);
96
+ return String(value);
97
+ }
98
+
99
+ /** Fields that differ between two snapshots, in declaration order. */
100
+ export function compareRecords(fields: { readonly [name: string]: FieldDeclaration }, before: RecordShape | null, after: RecordShape): FieldChange[] {
101
+ const changes: FieldChange[] = [];
102
+ for (const [name, declaration] of Object.entries(fields)) {
103
+ const from = comparableValue(declaration, before?.[name]);
104
+ const to = comparableValue(declaration, after[name]);
105
+ if (from === to) continue;
106
+ const text = ['string', 'text', 'richtext', 'json', 'blocks'].includes(declaration.kind) || declaration.multiple === true;
107
+ changes.push({
108
+ name, label: declaration.label ?? name, kind: text ? 'text' : 'value', structured: declaration.kind === 'json', before: from, after: to,
109
+ parts: text ? diffWords(from, to) : [...(from ? [{ kind: 'removed' as const, text: from }] : []), ...(to ? [{ kind: 'added' as const, text: to }] : [])],
110
+ });
111
+ }
112
+ return changes;
113
+ }
@@ -0,0 +1,316 @@
1
+ /**
2
+ * Editor layout: how a collection's entry editor is arranged. The editor is made of boxes
3
+ * (the Publish box, the content, the SEO box, each taxonomy, each field group, …), and a
4
+ * layout places them: tabs for the main column, and the sidebar every tab shares. A layout is
5
+ * a block tree of editor-only blocks (`editor/tab`, `editor/section`, `editor/box`) and the
6
+ * core layout blocks, so the collection manager edits it with the block tree. A collection
7
+ * without a stored layout renders `defaultEditorLayout`, which is what the editor showed
8
+ * before layouts existed. Pure functions, shared by the dashboard and the host.
9
+ */
10
+ import { CORE_BLOCKS, flattenBlocks, validateBlocks, type Block, type BlockDeclaration } from './blocks';
11
+ import type { CollectionDefinition } from './collections';
12
+ import { field, slugify, SLUG_PATTERN, type FieldDeclaration } from './fields';
13
+ import type { FieldGroup } from './field-groups';
14
+
15
+ // --- Boxes --------------------------------------------------------------------------------
16
+
17
+ /**
18
+ * A meta box the entry editor shows (a box, in the code). `field:<name>` is one field of the entry (a feature such as
19
+ * the content or the featured image, or a field a module or plugin declares), `group:<id>` a
20
+ * field group attached to the collection, `taxonomy:<name>` the terms of one taxonomy, and
21
+ * the rest are the editor's own: `seo` (search and sharing), `attributes` (parent and order),
22
+ * `languages`, `template`, `discussion` and `revisions`. The title with its permalink and the
23
+ * Publish box are not boxes: they are pinned.
24
+ */
25
+ export const EDITOR_BOX = /^(?:field:[a-z][a-zA-Z0-9_]{0,62}|group:[A-Za-z0-9_-]{1,40}|taxonomy:[a-z][a-zA-Z0-9_]{0,40}|seo|attributes|authors|languages|template|discussion|revisions)$/;
26
+
27
+ /** The editor's own boxes, with their default place. */
28
+ export const EDITOR_OWN_BOXES: readonly { readonly id: string; readonly label: string; readonly placement: 'main' | 'sidebar'; readonly note: string }[] = Object.freeze([
29
+ { id: 'seo', label: 'Search and sharing', placement: 'main', note: 'Shown when entries are on the site.' },
30
+ { id: 'attributes', label: 'Attributes', placement: 'sidebar', note: 'Parent and order; shown when entries nest.' },
31
+ { id: 'authors', label: 'Authors', placement: 'sidebar', note: 'Who wrote the entry; the first is its main author.' },
32
+ { id: 'languages', label: 'Languages', placement: 'sidebar', note: 'Shown when the site has several languages.' },
33
+ { id: 'template', label: 'Template', placement: 'sidebar', note: 'Shown when the active theme offers templates for entries.' },
34
+ { id: 'discussion', label: 'Discussion', placement: 'sidebar', note: 'Shown when comments are on.' },
35
+ { id: 'revisions', label: 'Revisions', placement: 'sidebar', note: 'Shown once the entry has revisions.' },
36
+ { id: 'assistant', label: 'Assistant', placement: 'sidebar', note: 'Shown when the application has an AI provider.' },
37
+ ]);
38
+
39
+ export interface EditorBox {
40
+ readonly id: string;
41
+ readonly label: string;
42
+ /** Where the default layout puts it. */
43
+ readonly placement: 'main' | 'sidebar';
44
+ /** When the editor shows it, for boxes that depend on the site or the entry. */
45
+ readonly note?: string;
46
+ /** The plugin whose group it is. */
47
+ readonly plugin?: string;
48
+ }
49
+
50
+ /** Where the editor shows a field by default: its declared place, else the sidebar for a checkbox, a single file or a reference. */
51
+ export function fieldPlacement(declaration: FieldDeclaration): 'main' | 'sidebar' {
52
+ if (declaration.placement) return declaration.placement;
53
+ const sidebar = declaration.kind === 'boolean' || (declaration.media === true && declaration.kind === 'string' && !declaration.multiple) || declaration.kind === 'reference';
54
+ return sidebar ? 'sidebar' : 'main';
55
+ }
56
+
57
+ export interface EditorBoxesInput {
58
+ readonly collection: Pick<CollectionDefinition, 'hierarchical' | 'comments' | 'path' | 'features'>;
59
+ /** The resource's fields: the core fields, the features', the declared ones and the groups'. */
60
+ readonly fields: { readonly [name: string]: FieldDeclaration };
61
+ /** The groups attached to the collection, in order. */
62
+ readonly groups: readonly Pick<FieldGroup, 'id' | 'title' | 'placement' | 'blocks' | 'plugin'>[];
63
+ /** The collection's taxonomies, with labels. */
64
+ readonly taxonomies: readonly { readonly name: string; readonly label: string }[];
65
+ /** Fields the editor shows nowhere or lays out itself: the core fields (`CORE_FIELDS`), or a plugin resource's title field. */
66
+ readonly special: readonly string[];
67
+ }
68
+
69
+ /**
70
+ * The boxes an editor of the collection has, in the order the default layout shows them:
71
+ * the main column's, then the sidebar's. Fields the groups lay out are the groups' boxes.
72
+ */
73
+ export function editorBoxes(input: EditorBoxesInput): EditorBox[] {
74
+ const laidOut = new Set(input.groups.flatMap(group => flattenBlocks(group.blocks).filter(block => block.type.startsWith('field/')).map(block => String(block.data.name ?? ''))));
75
+ const special = new Set([...input.special, ...input.taxonomies.map(taxonomy => taxonomy.name)]);
76
+ const fields = Object.entries(input.fields).filter(([name, declaration]) => !special.has(name) && !laidOut.has(name) && !declaration.taxonomy)
77
+ .map(([name, declaration]) => ({ id: `field:${name}`, label: declaration.label ?? name, placement: fieldPlacement(declaration) }));
78
+ const groups = input.groups.map(group => ({ id: `group:${group.id}`, label: group.title, placement: group.placement ?? 'main' as const, ...(group.plugin ? { plugin: group.plugin } : {}) }));
79
+ const own = (id: string) => EDITOR_OWN_BOXES.find(box => box.id === id)!;
80
+ const main: EditorBox[] = [
81
+ ...fields.filter(box => box.placement === 'main'),
82
+ ...groups.filter(box => box.placement === 'main'),
83
+ ...(input.collection.path !== null && input.collection.path !== undefined ? [own('seo')] : []),
84
+ ];
85
+ const sidebar: EditorBox[] = [
86
+ ...(input.collection.hierarchical ? [own('attributes')] : []),
87
+ own('authors'),
88
+ own('languages'),
89
+ ...(input.fields.template ? [own('template')] : []),
90
+ ...input.taxonomies.map(taxonomy => ({ id: `taxonomy:${taxonomy.name}`, label: taxonomy.label, placement: 'sidebar' as const })),
91
+ ...fields.filter(box => box.placement === 'sidebar'),
92
+ ...groups.filter(box => box.placement === 'sidebar'),
93
+ ...(input.collection.comments ? [own('discussion')] : []),
94
+ ...(input.collection.features?.revisions !== false ? [own('revisions')] : []),
95
+ ];
96
+ return [...main, ...sidebar];
97
+ }
98
+
99
+ // --- Blocks -------------------------------------------------------------------------------
100
+
101
+ const BOX = field.string({ required: true, max: 120, pattern: EDITOR_BOX.source.slice(1, -1), label: 'Meta box' });
102
+
103
+ /** The editor-only blocks a layout is built from. A box (a meta box, on screen) names one of `editorBoxes`. */
104
+ export const EDITOR_LAYOUT_BLOCKS: readonly BlockDeclaration[] = Object.freeze<BlockDeclaration[]>([
105
+ { name: 'editor/tab', label: 'Tab', icon: 'widget-2', category: 'design', description: 'A tab of the main column.', fields: {
106
+ label: field.string({ required: true, max: 80, label: 'Label' }),
107
+ icon: field.string({ max: 60, pattern: SLUG_PATTERN, label: 'Icon', description: 'One of the dashboard\'s icons, shown before the label.' }),
108
+ }, children: { allowed: ['editor/section', 'editor/box', 'core/columns', 'core/grid', 'core/paragraph'] } },
109
+ { name: 'editor/section', label: 'Section', icon: 'layers', category: 'design', description: 'A titled part of a tab or the sidebar, holding meta boxes.', fields: {
110
+ title: field.string({ required: true, max: 120, label: 'Title' }),
111
+ description: field.string({ max: 500, label: 'Description' }),
112
+ }, children: { allowed: ['editor/section', 'editor/box', 'core/columns', 'core/grid', 'core/paragraph'] } },
113
+ { name: 'editor/box', label: 'Meta box', icon: 'card', category: 'design', description: 'One of the editor\'s meta boxes: a field, a field group, a taxonomy, or the editor\'s own.', fields: { box: BOX } },
114
+ ]);
115
+
116
+ /** Core layout blocks a layout may use beside the editor's own. */
117
+ export const EDITOR_LAYOUT_CORE: readonly string[] = Object.freeze(['core/columns', 'core/column', 'core/grid', 'core/paragraph']);
118
+
119
+ /** What goes inside a tab, a section, a column or a grid cell: everything but a tab, so tabs stay at the top of the main column. */
120
+ export const EDITOR_LAYOUT_INSIDE: readonly string[] = Object.freeze(['editor/section', 'editor/box', 'core/columns', 'core/grid', 'core/paragraph']);
121
+
122
+ /** Every block type a layout may hold. */
123
+ export const EDITOR_LAYOUT_TYPES: ReadonlySet<string> = new Set([...EDITOR_LAYOUT_BLOCKS.map(block => block.name), ...EDITOR_LAYOUT_CORE]);
124
+
125
+ /**
126
+ * The block types a layout is validated and edited against. With `boxes`, the Box block's
127
+ * `box` is a choice among them (and the IDs in `keep`, which a stored layout names although
128
+ * they are not available now), so an editor offers a list instead of a text field.
129
+ */
130
+ export function editorLayoutTypes(boxes?: readonly EditorBox[], keep: readonly string[] = []): Map<string, BlockDeclaration> {
131
+ const types = new Map<string, BlockDeclaration>();
132
+ for (const name of EDITOR_LAYOUT_CORE) {
133
+ const block = CORE_BLOCKS.find(entry => entry.name === name)!;
134
+ // A column or a grid cell holds what a tab holds, never a tab.
135
+ types.set(name, block.children && !block.children.allowed ? { ...block, children: { ...block.children, allowed: EDITOR_LAYOUT_INSIDE } } : block);
136
+ }
137
+ for (const block of EDITOR_LAYOUT_BLOCKS) types.set(block.name, block);
138
+ if (boxes) {
139
+ const ids = [...new Set([...boxes.map(box => box.id), ...keep.filter(id => EDITOR_BOX.test(id))])];
140
+ const choices = Object.fromEntries(ids.map(id => [id, boxes.find(box => box.id === id)?.label ?? `${id} (not available)`]));
141
+ const box = types.get('editor/box')!;
142
+ types.set('editor/box', { ...box, fields: { box: field.enum(ids, { required: true, label: 'Meta box', choices }) } });
143
+ }
144
+ return types;
145
+ }
146
+
147
+ // --- Layout -------------------------------------------------------------------------------
148
+
149
+ export interface EditorLayout {
150
+ /** The main column's tabs: `editor/tab` blocks, the first with the ID `edit`. JSON follows them. */
151
+ readonly tabs: readonly Block[];
152
+ /** The sidebar under the Publish box, shared by every tab. */
153
+ readonly sidebar: readonly Block[];
154
+ }
155
+
156
+ /** The first tab, which cannot be removed or moved. */
157
+ export const EDIT_TAB = 'edit';
158
+ export const EDITOR_LAYOUT_LIMITS = Object.freeze({ tabs: 12, blocks: 200 });
159
+
160
+ /** A tab's ID, which the editor's address names (`?tab=`): a slug of at most 40 characters. */
161
+ export const EDITOR_TAB_ID = new RegExp(`^(?=.{1,40}$)${SLUG_PATTERN}$`);
162
+
163
+ /** Tab IDs the editor keeps for itself: its JSON view, and the sidebar the layout editor shows beside the tabs. */
164
+ export const EDITOR_TAB_RESERVED: readonly string[] = Object.freeze(['json', 'sidebar']);
165
+
166
+ /**
167
+ * A tab ID from a label: its slug, with `-2`, `-3`, … when `taken` (or the reserved IDs) has
168
+ * it already; `tab` when the label has no letters or digits.
169
+ */
170
+ export function editorTabId(label: string, taken: Iterable<string> = []): string {
171
+ const used = new Set([...taken, ...EDITOR_TAB_RESERVED]);
172
+ const base = slugify(label, 36) || 'tab';
173
+ if (!used.has(base)) return base;
174
+ for (let suffix = 2; ; suffix += 1) if (!used.has(`${base}-${suffix}`)) return `${base}-${suffix}`;
175
+ }
176
+
177
+ /** A box block. */
178
+ export function boxBlock(id: string): Block {
179
+ return { _id: `box-${id.replace(/[^A-Za-z0-9_-]+/g, '-')}`, type: 'editor/box', data: { box: id } };
180
+ }
181
+
182
+ /** The boxes a tree names, in document order, with the block each sits in. */
183
+ export function layoutBoxes(blocks: readonly Block[]): { id: string; block: Block }[] {
184
+ return flattenBlocks(blocks).filter(block => block.type === 'editor/box' && typeof block.data.box === 'string').map(block => ({ id: block.data.box as string, block }));
185
+ }
186
+
187
+ /**
188
+ * The layout the editor showed before layouts existed: one Edit tab with the main column's
189
+ * boxes, and the sidebar's boxes in their order.
190
+ */
191
+ export function defaultEditorLayout(boxes: readonly EditorBox[], options: { readonly editLabel?: string } = {}): EditorLayout {
192
+ return {
193
+ tabs: [{ _id: EDIT_TAB, type: 'editor/tab', data: { label: options.editLabel ?? 'Edit' }, children: boxes.filter(box => box.placement === 'main').map(box => boxBlock(box.id)) }],
194
+ sidebar: boxes.filter(box => box.placement === 'sidebar').map(box => boxBlock(box.id)),
195
+ };
196
+ }
197
+
198
+ /** Whether a value has a layout's shape, for definitions read as data. */
199
+ export function editorLayoutShapeProblem(value: unknown): string | undefined {
200
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return 'is tabs and a sidebar.';
201
+ const layout = value as { tabs?: unknown; sidebar?: unknown };
202
+ for (const key of ['tabs', 'sidebar'] as const) {
203
+ const list = layout[key];
204
+ if (!Array.isArray(list)) return `${key} is a list of blocks.`;
205
+ if (list.some(block => !block || typeof block !== 'object' || typeof block._id !== 'string' || typeof block.type !== 'string')) return `${key}: every block needs an _id and a type.`;
206
+ }
207
+ if ((layout.tabs as unknown[]).length > EDITOR_LAYOUT_LIMITS.tabs) return `holds at most ${EDITOR_LAYOUT_LIMITS.tabs} tabs.`;
208
+ const count = flattenBlocks(layout.tabs as Block[]).length + flattenBlocks(layout.sidebar as Block[]).length;
209
+ if (count > EDITOR_LAYOUT_LIMITS.blocks) return `holds at most ${EDITOR_LAYOUT_LIMITS.blocks} blocks.`;
210
+ return undefined;
211
+ }
212
+
213
+ /**
214
+ * Problems in a layout, and the layout with defaults applied: the tabs are `editor/tab`
215
+ * blocks alone, the first the Edit tab (`partial` layouts, such as a plugin's, need not have
216
+ * it); nothing else holds a tab; every block is an editor layout block; a box is named once.
217
+ */
218
+ export function editorLayoutProblems(layout: EditorLayout, options: { readonly partial?: boolean; readonly types?: ReadonlyMap<string, BlockDeclaration> } = {}): { problems: string[]; layout: EditorLayout } {
219
+ const problems: string[] = [];
220
+ const shape = editorLayoutShapeProblem(layout);
221
+ if (shape) return { problems: [`The layout ${shape}`], layout: { tabs: [], sidebar: [] } };
222
+ const types = options.types ?? editorLayoutTypes();
223
+ const check = (blocks: readonly Block[], where: string, allowed?: readonly string[]) => {
224
+ const { blocks: checked, errors } = validateBlocks(blocks, types, allowed ? { allowed } : {});
225
+ problems.push(...Object.entries(errors).map(([path, message]) => `${where} block ${path}: ${message}`));
226
+ for (const block of flattenBlocks(checked)) if (!EDITOR_LAYOUT_TYPES.has(block.type)) problems.push(`${where} block ${block._id}: ${block.type} does not go in an editor layout.`);
227
+ return checked;
228
+ };
229
+ const tabs = check(layout.tabs, 'Tab', ['editor/tab']);
230
+ const sidebar = check(layout.sidebar, 'Sidebar', EDITOR_LAYOUT_INSIDE);
231
+ if (!options.partial && tabs[0]?._id !== EDIT_TAB) problems.push(`The first tab is ${EDIT_TAB}.`);
232
+ const ids = tabs.map(tab => tab._id);
233
+ for (const [index, id] of ids.entries()) {
234
+ if (ids.indexOf(id) !== index) problems.push(`Tab ${id} is listed twice.`);
235
+ else if (EDITOR_TAB_RESERVED.includes(id)) problems.push(`Tab ${id}: the ID ${id} is the editor's own.`);
236
+ else if (!EDITOR_TAB_ID.test(id)) problems.push(`Tab ${id}: an ID is lowercase letters, digits and single hyphens, at most 40 characters, for the address (?tab=).`);
237
+ }
238
+ const seen = new Set<string>();
239
+ for (const { id, block } of [...layoutBoxes(tabs), ...layoutBoxes(sidebar)]) {
240
+ if (seen.has(id)) problems.push(`Block ${block._id}: the box ${id} is placed twice.`);
241
+ seen.add(id);
242
+ }
243
+ return { problems, layout: { tabs, sidebar } };
244
+ }
245
+
246
+ /** A plugin's addition to a collection's layout: tabs and sidebar blocks it appends, named by the plugin. */
247
+ export interface EditorLayoutAddition {
248
+ readonly plugin: string;
249
+ readonly layout: EditorLayout;
250
+ }
251
+
252
+ export interface ResolvedEditorLayout extends EditorLayout {
253
+ /** Box IDs the layout names that are not available now: a detached group's, an inactive plugin's, a feature turned off. */
254
+ readonly missing: string[];
255
+ /** Tab IDs each plugin added. */
256
+ readonly added: { [tab: string]: string };
257
+ }
258
+
259
+ /**
260
+ * The layout an editor renders: the stored one, or the default, with the plugins' additions
261
+ * and every box the layout does not name appended (main boxes to the first tab, sidebar boxes
262
+ * to the sidebar), and the boxes that are not available left out. A plugin's tab whose ID the
263
+ * stored layout already has is the stored one, so moving it keeps it where it was put; a
264
+ * plugin's box the stored layout places stays there too.
265
+ */
266
+ export function resolveEditorLayout(stored: EditorLayout | undefined, boxes: readonly EditorBox[], additions: readonly EditorLayoutAddition[] = [], options: { readonly editLabel?: string; /** Keep the boxes that are not available, for an editor of the layout. */ readonly keep?: boolean } = {}): ResolvedEditorLayout {
267
+ // A plugin's addition places boxes the default would otherwise put on the Edit tab or the sidebar.
268
+ const addedBoxes = new Set(additions.flatMap(addition => [...layoutBoxes(addition.layout.tabs), ...layoutBoxes(addition.layout.sidebar)].map(entry => entry.id)));
269
+ const base = stored ?? defaultEditorLayout(boxes.filter(box => !addedBoxes.has(box.id)), options);
270
+ let tabs = [...base.tabs];
271
+ let sidebar = [...base.sidebar];
272
+ const added: { [tab: string]: string } = {};
273
+ const placed = () => new Set([...layoutBoxes(tabs), ...layoutBoxes(sidebar)].map(entry => entry.id));
274
+ for (const addition of additions) {
275
+ const known = placed();
276
+ const prefix = (block: Block): Block => ({ ...block, _id: block._id.startsWith(`${addition.plugin}-`) ? block._id : `${addition.plugin}-${block._id}` });
277
+ const without = (blocks: readonly Block[]): Block[] => blocks.flatMap(block => block.type === 'editor/box' && known.has(String(block.data.box)) ? [] : [block.children ? { ...block, children: without(block.children) } : block]);
278
+ for (const tab of addition.layout.tabs) {
279
+ const next = prefix(tab);
280
+ if (tabs.some(entry => entry._id === next._id)) continue;
281
+ tabs.push({ ...next, children: without(next.children ?? []) });
282
+ added[next._id] = addition.plugin;
283
+ }
284
+ sidebar.push(...without(addition.layout.sidebar.map(prefix)));
285
+ }
286
+ const available = new Map(boxes.map(box => [box.id, box]));
287
+ const named = placed();
288
+ const missing = [...named].filter(id => !available.has(id));
289
+ const prune = (blocks: readonly Block[]): Block[] => options.keep ? [...blocks] : blocks.flatMap(block => block.type === 'editor/box' ? (available.has(String(block.data.box)) ? [block] : []) : [block.children ? { ...block, children: prune(block.children) } : block]);
290
+ tabs = tabs.map(tab => ({ ...tab, children: prune(tab.children ?? []) }));
291
+ sidebar = prune(sidebar);
292
+ const rest = boxes.filter(box => !named.has(box.id));
293
+ if (!tabs.length) tabs = [{ _id: EDIT_TAB, type: 'editor/tab', data: { label: options.editLabel ?? 'Edit' }, children: [] }];
294
+ tabs[0] = { ...tabs[0]!, children: [...(tabs[0]!.children ?? []), ...rest.filter(box => box.placement === 'main').map(box => boxBlock(box.id))] };
295
+ sidebar = [...sidebar, ...rest.filter(box => box.placement === 'sidebar').map(box => boxBlock(box.id))];
296
+ return { tabs, sidebar, missing, added };
297
+ }
298
+
299
+ /** The names of the fields a box edits, so an error can find the tab its box is on. */
300
+ export function boxFields(id: string, groups: readonly Pick<FieldGroup, 'id' | 'blocks'>[]): string[] {
301
+ if (id.startsWith('field:')) return [id.slice('field:'.length)];
302
+ if (id.startsWith('taxonomy:')) return [id.slice('taxonomy:'.length)];
303
+ if (id.startsWith('group:')) {
304
+ const group = groups.find(entry => entry.id === id.slice('group:'.length));
305
+ return group ? flattenBlocks(group.blocks).filter(block => block.type.startsWith('field/')).map(block => String(block.data.name ?? '')).filter(Boolean) : [];
306
+ }
307
+ switch (id) {
308
+ case 'seo': return ['seo', 'summary'];
309
+ case 'attributes': return ['parent', 'menuOrder'];
310
+ case 'authors': return ['authors'];
311
+ case 'languages': return ['locale', 'translationGroup'];
312
+ case 'template': return ['template'];
313
+ case 'discussion': return ['commentsOpen'];
314
+ default: return [];
315
+ }
316
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,50 @@
1
+ import { ClientVisibleError } from '@cossackframework/core';
2
+
3
+ export type CmsErrorCode =
4
+ | 'CMS_UNAUTHENTICATED'
5
+ | 'CMS_FORBIDDEN'
6
+ | 'CMS_NOT_FOUND'
7
+ | 'CMS_CONFLICT'
8
+ | 'CMS_VALIDATION'
9
+ | 'CMS_UNAVAILABLE'
10
+ | 'CMS_UNCERTAIN'
11
+ | 'CMS_LIMIT'
12
+ /** The database was made by another build of the CMS: a setup mistake to fix, never a page to wait for. */
13
+ | 'CMS_SCHEMA_MISMATCH';
14
+
15
+ const STATUS: { [C in CmsErrorCode]: number } = {
16
+ CMS_UNAUTHENTICATED: 401, CMS_FORBIDDEN: 403, CMS_NOT_FOUND: 404, CMS_CONFLICT: 409,
17
+ CMS_VALIDATION: 422, CMS_UNAVAILABLE: 503, CMS_UNCERTAIN: 503, CMS_LIMIT: 413, CMS_SCHEMA_MISMATCH: 500,
18
+ };
19
+
20
+ /**
21
+ * A CMS failure with a stable code, an HTTP status and safe, user-facing text. It is a
22
+ * `ClientVisibleError`, so a page's server action reports the message to the browser.
23
+ */
24
+ export class CmsError extends ClientVisibleError {
25
+ readonly code: CmsErrorCode;
26
+ readonly status: number;
27
+ /** Field errors for `CMS_VALIDATION`. */
28
+ readonly fields?: { readonly [field: string]: string };
29
+ /** Request ID of a write saved for recovery (`CMS_UNCERTAIN`). */
30
+ readonly requestId?: string;
31
+ /**
32
+ * A `CMS_UNAVAILABLE` that lasts until someone acts (an inactive plugin, a pending schema
33
+ * migration), not a passing database error: callers keeping an earlier answer refuse instead.
34
+ */
35
+ readonly lasting?: boolean;
36
+ constructor(code: CmsErrorCode, message: string, details: { fields?: { [field: string]: string }; requestId?: string; cause?: unknown; lasting?: boolean } = {}) {
37
+ super(message);
38
+ if (details.cause !== undefined) Object.defineProperty(this, 'cause', { value: details.cause, configurable: true, writable: true });
39
+ this.name = 'CmsError';
40
+ this.code = code;
41
+ this.status = STATUS[code];
42
+ if (details.fields) this.fields = Object.freeze({ ...details.fields });
43
+ if (details.requestId) this.requestId = details.requestId;
44
+ if (details.lasting) this.lasting = true;
45
+ }
46
+ }
47
+
48
+ export function isCmsError(error: unknown, code?: CmsErrorCode): error is CmsError {
49
+ return error instanceof CmsError && (code === undefined || error.code === code);
50
+ }