@kidecms/core 0.1.11 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/LICENSE +21 -0
  2. package/admin/components/AssetsGrid.tsx +366 -173
  3. package/admin/components/BlockEditor.tsx +260 -679
  4. package/admin/components/CheckboxField.tsx +2 -2
  5. package/admin/components/CollaborationReviewBar.tsx +384 -0
  6. package/admin/components/ColorField.tsx +87 -0
  7. package/admin/components/CommandPalette.tsx +169 -0
  8. package/admin/components/ContentEditor.tsx +687 -0
  9. package/admin/components/DocumentActions.tsx +4 -2
  10. package/admin/components/DocumentsDataTable.tsx +91 -10
  11. package/admin/components/FieldControl.astro +151 -26
  12. package/admin/components/FocalPointSelector.tsx +27 -1
  13. package/admin/components/InternalLinkPicker.tsx +4 -3
  14. package/admin/components/LinkField.tsx +126 -0
  15. package/admin/components/RelationField.tsx +105 -26
  16. package/admin/components/RepeaterInput.tsx +46 -0
  17. package/admin/components/RichTextEditor.tsx +44 -9
  18. package/admin/components/SharedSectionBlockField.tsx +93 -0
  19. package/admin/components/SidebarUserMenu.tsx +1 -1
  20. package/admin/components/Toast.astro +1 -1
  21. package/admin/components/TreeItemsEditor.tsx +20 -19
  22. package/admin/components/TreeSelect.tsx +2 -2
  23. package/admin/components/YoutubeField.tsx +48 -0
  24. package/admin/components/block-fields.tsx +777 -0
  25. package/admin/components/content-block-spec.ts +114 -0
  26. package/admin/components/slash-command.tsx +242 -0
  27. package/admin/components/tree-utils.ts +14 -2
  28. package/admin/components/ui/badge.tsx +2 -3
  29. package/admin/components/ui/button.tsx +5 -4
  30. package/admin/components/ui/checkbox.tsx +1 -1
  31. package/admin/components/ui/input.tsx +1 -1
  32. package/admin/components/ui/select.tsx +1 -1
  33. package/admin/components/ui/table.tsx +1 -1
  34. package/admin/components/ui/textarea.tsx +1 -1
  35. package/admin/layouts/AdminLayout.astro +323 -123
  36. package/admin/lib/access.ts +54 -0
  37. package/admin/lib/collaboration.ts +178 -0
  38. package/admin/lib/edit-data.ts +448 -0
  39. package/admin/views/AccessDeniedView.astro +8 -0
  40. package/admin/views/EditView.astro +706 -0
  41. package/admin/views/ListView.astro +202 -0
  42. package/admin/views/RecentView.astro +76 -0
  43. package/admin/views/SinglesView.astro +65 -0
  44. package/admin/views/edit/EditScripts.astro +145 -0
  45. package/admin/views/edit/ReverseReferencesList.astro +68 -0
  46. package/core/admin.ts +193 -0
  47. package/core/ai.ts +87 -0
  48. package/core/api.ts +1392 -0
  49. package/core/assets.ts +336 -0
  50. package/core/audit.ts +84 -0
  51. package/core/auth-config.ts +44 -0
  52. package/core/auth.ts +303 -0
  53. package/core/collaboration.ts +276 -0
  54. package/core/content.ts +27 -0
  55. package/core/create-admin.ts +28 -0
  56. package/core/define.ts +625 -0
  57. package/core/field-model.ts +172 -0
  58. package/core/generator.ts +519 -0
  59. package/core/http.ts +42 -0
  60. package/core/image.ts +219 -0
  61. package/core/index.ts +211 -0
  62. package/core/locks.ts +52 -0
  63. package/core/log.ts +37 -0
  64. package/core/migrate.ts +278 -0
  65. package/core/rate-limit.ts +110 -0
  66. package/core/request-scope.ts +33 -0
  67. package/core/richtext.ts +1 -0
  68. package/core/runtime.ts +63 -0
  69. package/core/schema.ts +32 -0
  70. package/core/search.ts +489 -0
  71. package/core/seed.ts +101 -0
  72. package/core/shared-sections.ts +96 -0
  73. package/core/tasks.ts +176 -0
  74. package/core/values.ts +433 -0
  75. package/core/webhooks.ts +85 -0
  76. package/core/youtube.ts +39 -0
  77. package/internals/cli.mjs +34 -0
  78. package/internals/context.ts +67 -0
  79. package/internals/create-admin.ts +40 -0
  80. package/internals/describe.ts +25 -0
  81. package/internals/eject.ts +144 -0
  82. package/internals/generator.ts +11 -0
  83. package/internals/integration.ts +481 -0
  84. package/internals/mcp.ts +397 -0
  85. package/internals/project.ts +26 -0
  86. package/internals/push.ts +160 -0
  87. package/internals/reindex.ts +11 -0
  88. package/internals/restore.ts +257 -0
  89. package/internals/seed.data.ts +7 -0
  90. package/internals/seed.ts +11 -0
  91. package/internals/upgrade.ts +776 -0
  92. package/{virtual.d.ts → internals/virtual.d.ts} +39 -5
  93. package/middleware/auth.ts +130 -17
  94. package/middleware/push.ts +121 -0
  95. package/package.json +55 -67
  96. package/platform/cloudflare/cf-env.ts +60 -0
  97. package/platform/cloudflare/database.ts +28 -0
  98. package/platform/cloudflare/globals.d.ts +5 -0
  99. package/platform/cloudflare/storage.ts +33 -0
  100. package/platform/cloudflare/uploads-route.ts +37 -0
  101. package/platform/node/database.ts +39 -0
  102. package/platform/node/storage.ts +52 -0
  103. package/routes/admin/[...path].astro +71 -0
  104. package/routes/{pages/admin → admin}/assets/[id].astro +25 -19
  105. package/routes/admin/assets/index.astro +90 -0
  106. package/routes/{pages/admin/login.astro → admin/forgot-password.astro} +21 -19
  107. package/routes/{pages/admin → admin}/invite.astro +6 -6
  108. package/routes/admin/login.astro +103 -0
  109. package/routes/admin/reset-password.astro +73 -0
  110. package/routes/{pages/admin → admin}/setup.astro +31 -8
  111. package/routes/api/{cms/[collection] → [collection]}/[...path].ts +66 -14
  112. package/routes/api/admin/search.ts +89 -0
  113. package/routes/api/{cms/assets → assets}/[id].ts +24 -14
  114. package/routes/api/{cms/assets → assets}/index.ts +4 -2
  115. package/routes/api/assets/upload.ts +147 -0
  116. package/routes/api/auth/forgot-password.ts +77 -0
  117. package/routes/api/{cms/auth → auth}/invite.ts +41 -9
  118. package/routes/api/auth/login.ts +167 -0
  119. package/routes/api/{cms/auth → auth}/logout.ts +11 -2
  120. package/routes/api/auth/reset-password.ts +85 -0
  121. package/routes/api/auth/setup.ts +110 -0
  122. package/routes/api/auth/sso/[provider]/start.ts +34 -0
  123. package/routes/api/collaboration/index.ts +106 -0
  124. package/routes/api/cron/_authorize.ts +25 -0
  125. package/routes/api/{cms/cron → cron}/publish.ts +3 -9
  126. package/routes/api/cron/tasks.ts +33 -0
  127. package/routes/api/forms/submit/[slug].ts +143 -0
  128. package/routes/api/img/[...path].ts +65 -0
  129. package/routes/api/{cms/preview → preview}/render.ts +8 -3
  130. package/routes/api/{cms/references → references}/[collection]/[id].ts +31 -1
  131. package/routes/api/webhooks/[provider].ts +54 -0
  132. package/README.md +0 -28
  133. package/dist/admin.d.ts +0 -33
  134. package/dist/admin.js +0 -92
  135. package/dist/ai.d.ts +0 -15
  136. package/dist/ai.js +0 -70
  137. package/dist/api.d.ts +0 -14
  138. package/dist/api.js +0 -833
  139. package/dist/assets.d.ts +0 -51
  140. package/dist/assets.js +0 -163
  141. package/dist/auth.d.ts +0 -30
  142. package/dist/auth.js +0 -132
  143. package/dist/content.d.ts +0 -3
  144. package/dist/content.js +0 -29
  145. package/dist/create-admin.d.ts +0 -5
  146. package/dist/create-admin.js +0 -23
  147. package/dist/define.d.ts +0 -254
  148. package/dist/define.js +0 -36
  149. package/dist/generator.d.ts +0 -9
  150. package/dist/generator.js +0 -370
  151. package/dist/image.d.ts +0 -9
  152. package/dist/image.js +0 -69
  153. package/dist/index.d.ts +0 -28
  154. package/dist/index.js +0 -16
  155. package/dist/integration.d.ts +0 -14
  156. package/dist/integration.js +0 -327
  157. package/dist/locks.d.ts +0 -7
  158. package/dist/locks.js +0 -37
  159. package/dist/richtext.d.ts +0 -1
  160. package/dist/richtext.js +0 -1
  161. package/dist/runtime.d.ts +0 -24
  162. package/dist/runtime.js +0 -26
  163. package/dist/schema.d.ts +0 -17
  164. package/dist/schema.js +0 -13
  165. package/dist/seed.d.ts +0 -2
  166. package/dist/seed.js +0 -84
  167. package/dist/values.d.ts +0 -8
  168. package/dist/values.js +0 -102
  169. package/dist/webhooks.d.ts +0 -2
  170. package/dist/webhooks.js +0 -73
  171. package/routes/api/cms/assets/upload.ts +0 -112
  172. package/routes/api/cms/auth/login.ts +0 -124
  173. package/routes/api/cms/auth/setup.ts +0 -77
  174. package/routes/api/cms/img/[...path].ts +0 -24
  175. package/routes/pages/admin/[...path].astro +0 -1107
  176. package/routes/pages/admin/assets/index.astro +0 -63
  177. /package/routes/api/{cms/ai → ai}/alt-text.ts +0 -0
  178. /package/routes/api/{cms/ai → ai}/seo.ts +0 -0
  179. /package/routes/api/{cms/ai → ai}/translate.ts +0 -0
  180. /package/routes/api/{cms/assets → assets}/folders.ts +0 -0
  181. /package/routes/api/{cms/locks → locks}/[...path].ts +0 -0
@@ -0,0 +1,706 @@
1
+ ---
2
+ import { ArrowLeft, ArrowUpRight, Dot, Lock } from "lucide-react";
3
+
4
+ import AdminCard from "../components/AdminCard.astro";
5
+ import AiGenerateButton from "../components/AiGenerateButton";
6
+ import CollaborationReviewBar from "../components/CollaborationReviewBar";
7
+ import DocumentActions from "../components/DocumentActions";
8
+ import DocumentLock from "../components/DocumentLock";
9
+ import LiveHeading from "../components/LiveHeading";
10
+ import UnsavedGuard from "../components/UnsavedGuard";
11
+ import FieldControl from "../components/FieldControl.astro";
12
+ import { StatusBadge } from "../components/ui/badge";
13
+ import { buttonVariants } from "../components/ui/button";
14
+ import config from "virtual:kide/config";
15
+ import { cms } from "virtual:kide/api";
16
+ import { isAiEnabled, collaboration } from "virtual:kide/runtime";
17
+ import { loadCollaborationData } from "../lib/collaboration";
18
+ import {
19
+ formatDate,
20
+ getFieldGroups,
21
+ getFieldSets,
22
+ getLabelField,
23
+ getSharedBlockTypes,
24
+ isApprover,
25
+ resolveCollaboration,
26
+ } from "../../core";
27
+ import {
28
+ getFormAction,
29
+ getVisualStatus,
30
+ isFieldHidden as isFieldHiddenFn,
31
+ isFieldReadOnly as isFieldReadOnlyFn,
32
+ } from "../lib/access";
33
+ import {
34
+ loadDocument,
35
+ loadMenuLinkOptions,
36
+ loadParentFormTitle,
37
+ loadRelationOptions,
38
+ loadReverseRefs,
39
+ loadSharedBlockRelationOptions,
40
+ loadSharedSectionOptions,
41
+ } from "../lib/edit-data";
42
+ import EditScripts from "./edit/EditScripts.astro";
43
+ import ReverseReferencesList from "./edit/ReverseReferencesList.astro";
44
+
45
+ type CollectionLike = {
46
+ slug: string;
47
+ labels: { singular: string; plural: string };
48
+ singleton?: boolean;
49
+ drafts?: boolean;
50
+ preview?: boolean | string;
51
+ pathPrefix?: string;
52
+ fields: Record<string, any>;
53
+ };
54
+
55
+ const {
56
+ collection,
57
+ mode,
58
+ documentId,
59
+ variant = "editorial",
60
+ } = Astro.props as {
61
+ collection: CollectionLike;
62
+ mode: "new" | "edit";
63
+ documentId?: string;
64
+ // "editorial" = sticky bar + flowing fields (content collections & singletons);
65
+ // "record" = plain header + card-wrapped fields (library/system collections).
66
+ variant?: "editorial" | "record";
67
+ };
68
+
69
+ const user = Astro.locals.user;
70
+ const runtimeContext = user ? { user: { id: user.id, role: user.role, email: user.email } } : {};
71
+ const cmsRuntime = cms as Record<string, any> & { meta: typeof cms.meta };
72
+ const locales = config.locales?.supported ?? [];
73
+ const defaultLocale = config.locales?.default ?? "en";
74
+ const requestedLocale = Astro.url.searchParams.get("locale") ?? defaultLocale;
75
+ const aiEnabled = isAiEnabled();
76
+ const collectionApi = cmsRuntime[collection.slug];
77
+ const isSingleton = collection.singleton === true;
78
+
79
+ const isFieldHidden = (fieldName: string) => isFieldHiddenFn(collection, user, fieldName);
80
+ const isFieldReadOnly = (fieldName: string) => isFieldReadOnlyFn(collection, user, fieldName);
81
+
82
+ // --- Data loading ---
83
+ let doc: Record<string, unknown> | null = null;
84
+ let baseDoc: Record<string, unknown> | null = null;
85
+ let versions: Array<{ version: number; createdAt: string }> = [];
86
+ if (mode === "edit" && documentId) {
87
+ try {
88
+ const loaded = await loadDocument(collectionApi, documentId, requestedLocale, defaultLocale, runtimeContext);
89
+ if (!loaded.doc) return Astro.redirect(`/admin/${collection.slug}`);
90
+ doc = loaded.doc;
91
+ baseDoc = loaded.baseDoc;
92
+ versions = loaded.versions;
93
+ } catch {
94
+ return Astro.redirect(`/admin/${collection.slug}?_toast=error&_msg=Access+denied`);
95
+ }
96
+ }
97
+
98
+ const { relationOptionsByField, relationMetaByField } = await loadRelationOptions(
99
+ collection,
100
+ config,
101
+ user,
102
+ cmsRuntime,
103
+ defaultLocale,
104
+ runtimeContext,
105
+ );
106
+ const sharedBlockTypes = getSharedBlockTypes(config);
107
+ const [sharedBlockRelationOptions, sharedSectionOptions] = await Promise.all([
108
+ loadSharedBlockRelationOptions(config, user, cmsRuntime, defaultLocale, runtimeContext),
109
+ loadSharedSectionOptions(config, user, cmsRuntime, defaultLocale, runtimeContext),
110
+ ]);
111
+ // Loaded for every collection: menu trees and any link field (standalone or
112
+ // inside blocks) offer the same internal-document picker.
113
+ const menuLinkOptions = await loadMenuLinkOptions(config, user, cmsRuntime, defaultLocale, runtimeContext);
114
+ const reverseRefs =
115
+ mode === "edit" && documentId
116
+ ? await loadReverseRefs(collection, documentId, config, user, cmsRuntime, defaultLocale, runtimeContext)
117
+ : [];
118
+
119
+ // Submissions live in their own sidebar section now, not as a tab on the form.
120
+ const formTab = "edit" as "edit" | "submissions";
121
+
122
+ // --- Form-submissions-specific: custom read-only view ---
123
+ const isSubmissionView = collection.slug === "form-submissions" && mode === "edit";
124
+ const parentFormTitle =
125
+ isSubmissionView && doc?.form ? await loadParentFormTitle(String(doc.form), cmsRuntime, runtimeContext) : null;
126
+ const submissionData = (isSubmissionView && doc?.data ? doc.data : {}) as Record<string, unknown>;
127
+ const { _context: submissionContext, ...submittedFields } = submissionData;
128
+ const submissionContextEntries =
129
+ submissionContext && typeof submissionContext === "object"
130
+ ? Object.entries(submissionContext as Record<string, unknown>)
131
+ : [];
132
+ const formatSubmissionValue = (value: unknown): string => {
133
+ if (value === null || value === undefined) return "—";
134
+ if (typeof value === "boolean") return value ? "Yes" : "No";
135
+ if (typeof value === "string") return value;
136
+ return JSON.stringify(value);
137
+ };
138
+
139
+ // --- Layout / derived state ---
140
+ const selectedLocale = mode === "new" ? defaultLocale : requestedLocale;
141
+ const translatableFields = Object.entries(collection.fields)
142
+ .filter(([, field]: [string, any]) => field.translatable)
143
+ .map(([fieldName]) => fieldName);
144
+ const translationMode = mode === "edit" && selectedLocale !== defaultLocale && translatableFields.length > 0;
145
+ const fieldSets = getFieldSets(collection);
146
+ const primaryFieldSets = fieldSets.filter((set: { position?: string }) => set.position !== "sidebar");
147
+ const sidebarFieldSets = fieldSets.filter((set: { position?: string }) => set.position === "sidebar");
148
+ const mainFieldSets = primaryFieldSets.length > 0 ? primaryFieldSets : fieldSets.slice(0, 1);
149
+ const effectiveSidebarFieldSets =
150
+ sidebarFieldSets.length > 0 ? sidebarFieldSets : fieldSets.slice(mainFieldSets.length);
151
+
152
+ // Document metadata (created/updated, scheduled publish) shown at the bottom of
153
+ // the sidebar under the label info.
154
+ const hasDocMeta = Boolean(mode === "edit" && doc && collection.drafts);
155
+
156
+ // Editable fields that live in the sidebar (slug, category, etc.). The meta
157
+ // group (Referenced by / Info) only draws a top divider when these exist above it.
158
+ const hasSidebarFields = effectiveSidebarFieldSets.some((set) =>
159
+ set.fields.some((fieldName) => collection.fields[fieldName] && !isFieldHidden(fieldName)),
160
+ );
161
+ const hasSidebarMeta = reverseRefs.length > 0 || hasDocMeta;
162
+
163
+ // Whether to render the right-hand sidebar column at all (avoids a stray sticky
164
+ // border on collections with no sidebar fields and no incoming references).
165
+ const hasSidebarColumn = !isSubmissionView && (hasSidebarFields || hasSidebarMeta);
166
+
167
+ const hasPreview = !!(mode === "edit" && (collection.preview || (collection.pathPrefix && doc?.slug)));
168
+ const previewUrl = hasPreview
169
+ ? typeof collection.preview === "string"
170
+ ? `${collection.preview}?preview=true`
171
+ : `${cms.meta.getRouteForDocument(collection.slug, doc)}?preview=true`
172
+ : null;
173
+
174
+ const getCurrentValue = (fieldName: string) => (doc ? doc[fieldName] : undefined);
175
+ const getSharedValue = (fieldName: string) => (baseDoc ? baseDoc[fieldName] : undefined);
176
+ const blockRelationOptions: Record<string, Array<{ value: string; label: string }>> = {};
177
+ for (const [key, opts] of Object.entries(relationOptionsByField)) {
178
+ if (key.startsWith("block:")) blockRelationOptions[key] = opts;
179
+ }
180
+
181
+ // Editorial collaboration UI — shown only for collections in the collaboration
182
+ // list, and only on normal document edits (not submissions or the translation form).
183
+ const collab = resolveCollaboration(config, collection.slug);
184
+ const showCollab = collab.enabled && mode === "edit" && !isSubmissionView && !translationMode;
185
+ const collabData =
186
+ showCollab && documentId
187
+ ? await loadCollaborationData({
188
+ collaboration,
189
+ cmsRuntime,
190
+ runtimeContext,
191
+ collection: collection.slug,
192
+ documentId,
193
+ currentUser: user,
194
+ })
195
+ : null;
196
+
197
+ // Publish gate: when collaboration.requireApproval is on, non-admins can't
198
+ // publish a draft-enabled document until its review is approved. New documents
199
+ // (no review yet) count as unapproved.
200
+ const requireApproval = collab.enabled && collab.requireApproval && !!collection.drafts && !isSubmissionView;
201
+ const publishBlocked =
202
+ requireApproval && !isApprover(config, user?.role) && (collabData?.reviewState ?? "in_progress") !== "approved";
203
+ ---
204
+
205
+ <section class="space-y-14">
206
+ {
207
+ /* ── Sticky action bar ─────────────────────────────────────────────────
208
+ Title + status on the left, primary actions on the right. Stays pinned so
209
+ Save / Publish stay reachable while editing long documents. */
210
+ }
211
+ <div
212
+ class:list={[
213
+ "border-b px-4 py-3 lg:py-4 lg:pr-8 lg:pl-8",
214
+ variant === "editorial" && "bg-background sticky top-0 z-30 backdrop-blur-md",
215
+ ]}
216
+ >
217
+ <div class="flex items-center justify-between gap-3">
218
+ <div class="min-w-0">
219
+ <div class="flex items-center gap-2.5">
220
+ <h1 class="truncate text-lg font-semibold tracking-tight md:text-xl">
221
+ {mode === "new" && <LiveHeading client:load initial={`New ${collection.labels.singular}`} />}
222
+ {
223
+ mode === "edit" && (
224
+ <LiveHeading
225
+ client:load
226
+ initial={
227
+ collection.singleton
228
+ ? collection.labels.singular
229
+ : String(doc?.[getLabelField(collection)] ?? doc?.slug ?? collection.labels.singular)
230
+ }
231
+ />
232
+ )
233
+ }
234
+ </h1>
235
+ {mode === "edit" && doc && collection.drafts && <StatusBadge status={getVisualStatus(doc)} styled />}
236
+ </div>
237
+ {
238
+ isSubmissionView && doc && (
239
+ <div class="text-muted-foreground mt-1 flex flex-wrap items-center gap-x-1 text-xs">
240
+ {parentFormTitle && <span>{parentFormTitle}</span>}
241
+ {parentFormTitle && <Dot className="text-muted-foreground/70 size-4" />}
242
+ <span>Submitted {formatDate(String(doc._createdAt ?? ""))}</span>
243
+ </div>
244
+ )
245
+ }
246
+ </div>
247
+
248
+ {
249
+ formTab !== "submissions" && (
250
+ <div class="flex flex-shrink-0 flex-wrap items-center justify-end gap-2">
251
+ {hasPreview && previewUrl && !translationMode && (
252
+ <>
253
+ <a href={previewUrl} target="_blank" class={buttonVariants({ variant: "outline" })}>
254
+ Preview
255
+ <ArrowUpRight className="size-4" />
256
+ </a>
257
+ <div class="bg-border mx-1 h-6 w-px" />
258
+ </>
259
+ )}
260
+ {locales.length > 1 &&
261
+ mode === "edit" &&
262
+ translatableFields.length > 0 &&
263
+ locales.map((locale) => (
264
+ <a
265
+ href={`/admin/${collection.slug}/${documentId}?locale=${locale}`}
266
+ class:list={[
267
+ buttonVariants({ variant: "outline", size: "sm" }),
268
+ selectedLocale === locale
269
+ ? "border-primary/50 bg-background ring-ring/30 font-medium ring-2"
270
+ : "text-muted-foreground",
271
+ ]}
272
+ >
273
+ {locale.toUpperCase()}
274
+ </a>
275
+ ))}
276
+ {translationMode ? (
277
+ <button
278
+ class={buttonVariants({ variant: "outline" })}
279
+ type="submit"
280
+ form="translation-form"
281
+ value="save"
282
+ disabled
283
+ >
284
+ Save translation
285
+ </button>
286
+ ) : (
287
+ <>
288
+ <button
289
+ class={buttonVariants({ variant: "outline" })}
290
+ type="submit"
291
+ form="document-form"
292
+ name="_intent"
293
+ value="save"
294
+ disabled={mode === "edit"}
295
+ >
296
+ {collection.slug === "users" && mode === "new"
297
+ ? "Send invite"
298
+ : collection.drafts
299
+ ? "Save draft"
300
+ : "Save"}
301
+ </button>
302
+ {collection.drafts &&
303
+ (publishBlocked ? (
304
+ <button
305
+ class={buttonVariants({ variant: "outline" })}
306
+ type="button"
307
+ disabled
308
+ title="Publish unlocks once the review is approved"
309
+ >
310
+ <Lock className="size-3.5" />
311
+ Needs approval
312
+ </button>
313
+ ) : (
314
+ <button
315
+ class={buttonVariants({ variant: "publish" })}
316
+ type="submit"
317
+ form="document-form"
318
+ name="_intent"
319
+ value="publish"
320
+ disabled={
321
+ mode === "edit" &&
322
+ !(
323
+ doc?._status === "draft" ||
324
+ doc?._status === "scheduled" ||
325
+ (doc && getVisualStatus(doc) === "changed")
326
+ )
327
+ }
328
+ >
329
+ Publish
330
+ </button>
331
+ ))}
332
+ </>
333
+ )}
334
+ {mode === "edit" && !translationMode && documentId && (
335
+ <DocumentActions
336
+ client:load
337
+ formId="document-form"
338
+ collectionSlug={collection.slug}
339
+ documentId={documentId}
340
+ showUnpublish={collection.drafts && doc?._status === "published"}
341
+ showDiscardDraft={Boolean(collection.drafts && doc?._status === "published" && doc?._published)}
342
+ showDelete={!isSingleton}
343
+ showDuplicate={!isSingleton}
344
+ showSchedule={!!collection.drafts}
345
+ showCancelSchedule={collection.drafts && doc?._status === "scheduled"}
346
+ currentPublishAt={doc?._publishAt ? String(doc._publishAt) : undefined}
347
+ currentUnpublishAt={doc?._unpublishAt ? String(doc._unpublishAt) : undefined}
348
+ versions={versions}
349
+ restoreEndpoint={getFormAction(collection.slug, documentId)}
350
+ redirectTo={Astro.url.pathname + Astro.url.search}
351
+ />
352
+ )}
353
+ </div>
354
+ )
355
+ }
356
+ </div>
357
+ </div>
358
+
359
+ {/* Body — everything below the sticky bar, inset from the page edges */}
360
+ <div class="space-y-6 px-4 pb-8 lg:pr-8 lg:pb-12 lg:pl-20">
361
+ {
362
+ /* Collaboration review strip. Margins live on this wrapper, which only
363
+ renders when collaboration is enabled, so non-collab edit views are
364
+ untouched. The body already sits 3.5rem (section space-y-14) below the
365
+ header, so -mt-6 trims the top gap to ~2rem and mb-8 balances the bottom.
366
+ lg:-ml-12 pulls it to the header's left edge (full width). */
367
+ }
368
+ {
369
+ showCollab && collabData && documentId && (
370
+ <div class="-mt-6 mb-8 lg:-ml-12">
371
+ <CollaborationReviewBar
372
+ client:load
373
+ collection={collection.slug}
374
+ documentId={documentId}
375
+ reviewState={collabData.reviewState}
376
+ editor={collabData.editor}
377
+ assignableUsers={collabData.assignableUsers}
378
+ comments={collabData.comments}
379
+ activity={collabData.activity}
380
+ currentUser={collabData.currentUser}
381
+ canApprove={isApprover(config, user?.role)}
382
+ />
383
+ </div>
384
+ )
385
+ }
386
+ {
387
+ formTab !== "submissions" && (
388
+ <>
389
+ {mode === "edit" && documentId && (
390
+ <DocumentLock client:load collection={collection.slug} documentId={documentId} />
391
+ )}
392
+ <UnsavedGuard
393
+ client:load
394
+ formId={translationMode ? "translation-form" : "document-form"}
395
+ isNew={mode === "new"}
396
+ isDraft={Boolean(
397
+ !translationMode &&
398
+ collection.drafts &&
399
+ (doc?._status === "draft" || doc?._status === "scheduled" || (doc && getVisualStatus(doc) === "changed")),
400
+ )}
401
+ />
402
+ <div id="document-edit-area">
403
+ {collection.slug === "users" && mode === "edit" && Astro.url.searchParams.get("inviteToken") && (
404
+ <AdminCard title={Astro.url.searchParams.get("emailSent") === "true" ? "Invitation sent" : "Invite link"}>
405
+ {Astro.url.searchParams.get("emailSent") === "true" ? (
406
+ <p class="text-muted-foreground text-sm">
407
+ An invitation email has been sent. The user can also use this link:
408
+ </p>
409
+ ) : (
410
+ <p class="text-muted-foreground text-sm">
411
+ Email is not configured. Share this link with the user to complete their registration:
412
+ </p>
413
+ )}
414
+ <code class="bg-muted mt-2 block rounded-md px-3 py-2 text-sm break-all select-all">
415
+ {Astro.url.origin}/admin/invite?token={Astro.url.searchParams.get("inviteToken")}
416
+ </code>
417
+ </AdminCard>
418
+ )}
419
+
420
+ {!translationMode && (
421
+ <form
422
+ id="document-form"
423
+ method="post"
424
+ action={getFormAction(collection.slug, mode === "edit" ? documentId : undefined)}
425
+ class:list={[
426
+ "grid items-start gap-y-10",
427
+ hasSidebarColumn ? "2xl:grid-cols-[minmax(0,1fr)_minmax(320px,400px)] 2xl:gap-x-20" : "max-w-3xl",
428
+ ]}
429
+ >
430
+ <input type="hidden" name="_action" value={mode === "new" ? "create" : "update"} />
431
+ <input type="hidden" name="redirectTo" value={Astro.url.pathname + Astro.url.search} />
432
+
433
+ <div class="min-w-0 space-y-7">
434
+ {isSubmissionView ? (
435
+ <AdminCard title="Submission">
436
+ <table class="w-full text-sm">
437
+ <tbody>
438
+ {Object.entries(submittedFields).map(([key, value]) => (
439
+ <tr class="border-b last:border-b-0">
440
+ <td class="text-muted-foreground w-1/3 py-2 pr-4 align-top font-medium">{key}</td>
441
+ <td class="py-2 align-top break-words whitespace-pre-wrap">
442
+ {formatSubmissionValue(value)}
443
+ </td>
444
+ </tr>
445
+ ))}
446
+ {submissionContextEntries.map(([key, value]) => (
447
+ <tr class="border-b last:border-b-0">
448
+ <td class="text-muted-foreground w-1/3 py-2 pr-4 align-top font-medium">{key}</td>
449
+ <td class="py-2 align-top break-words whitespace-pre-wrap">
450
+ {formatSubmissionValue(value)}
451
+ </td>
452
+ </tr>
453
+ ))}
454
+ {Object.keys(submittedFields).length === 0 && submissionContextEntries.length === 0 && (
455
+ <tr>
456
+ <td class="text-muted-foreground py-2" colspan="2">
457
+ No data.
458
+ </td>
459
+ </tr>
460
+ )}
461
+ </tbody>
462
+ </table>
463
+ </AdminCard>
464
+ ) : (
465
+ mainFieldSets.map((set) => {
466
+ const visibleFields = set.fields.filter(
467
+ (fieldName) => collection.fields[fieldName] && !isFieldHidden(fieldName),
468
+ );
469
+ const renderField = (fieldName: string) => (
470
+ <FieldControl
471
+ name={fieldName}
472
+ field={collection.fields[fieldName]}
473
+ value={getCurrentValue(fieldName)}
474
+ readOnly={isFieldReadOnly(fieldName)}
475
+ relationOptions={relationOptionsByField[fieldName] ?? []}
476
+ relationMeta={relationMetaByField[fieldName]}
477
+ menuLinkOptions={menuLinkOptions}
478
+ blockRelationOptions={blockRelationOptions}
479
+ sharedBlockTypes={sharedBlockTypes}
480
+ sharedBlockRelationOptions={sharedBlockRelationOptions}
481
+ sharedSectionOptions={sharedSectionOptions}
482
+ />
483
+ );
484
+ const fields = (
485
+ <div class:list={["grid", variant === "record" ? "gap-6" : "gap-8"]}>
486
+ {getFieldGroups(collection, visibleFields).map((run) =>
487
+ run.group && run.collapsible ? (
488
+ <details
489
+ class="group/fieldset overflow-hidden rounded-lg border"
490
+ open={run.collapsible !== "collapsed"}
491
+ data-fieldset-key={`${collection.slug}:${run.group}`}
492
+ >
493
+ <summary class="bg-muted-strong/70 text-foreground/80 hover:bg-muted-strong/90 flex items-center justify-between px-4 py-2.5 text-sm font-medium transition-colors select-none group-open/fieldset:border-b [&::-webkit-details-marker]:hidden [&::marker]:content-none">
494
+ {run.group}
495
+ <svg
496
+ width="16"
497
+ height="16"
498
+ viewBox="0 0 24 24"
499
+ fill="none"
500
+ stroke="currentColor"
501
+ stroke-width="2"
502
+ stroke-linecap="round"
503
+ stroke-linejoin="round"
504
+ class="text-muted-foreground transition-transform group-open/fieldset:rotate-90"
505
+ aria-hidden="true"
506
+ >
507
+ <path d="m9 18 6-6-6-6" />
508
+ </svg>
509
+ </summary>
510
+ <div class="grid gap-6 p-4">{run.fields.map(renderField)}</div>
511
+ </details>
512
+ ) : run.group ? (
513
+ <section class="overflow-hidden rounded-lg border">
514
+ <h3 class="bg-muted-strong/70 text-foreground/80 border-b px-4 py-2.5 text-sm font-medium">
515
+ {run.group}
516
+ </h3>
517
+ <div class="grid gap-6 p-4">{run.fields.map(renderField)}</div>
518
+ </section>
519
+ ) : (
520
+ run.fields.map(renderField)
521
+ ),
522
+ )}
523
+ </div>
524
+ );
525
+ return variant === "record" ? (
526
+ <AdminCard title={collection.labels.singular}>{fields}</AdminCard>
527
+ ) : (
528
+ fields
529
+ );
530
+ })
531
+ )}
532
+ </div>
533
+
534
+ {hasSidebarColumn && (
535
+ <div class="text-sm 2xl:border-l 2xl:pl-8 [&_button]:text-sm [&_input]:text-sm [&_select]:text-sm [&_textarea]:text-sm">
536
+ <div class="space-y-6">
537
+ {effectiveSidebarFieldSets.map((set) => {
538
+ const fields = (
539
+ <div class="grid gap-6">
540
+ {set.fields
541
+ .filter((fieldName) => collection.fields[fieldName] && !isFieldHidden(fieldName))
542
+ .map((fieldName) => (
543
+ <div>
544
+ <FieldControl
545
+ name={fieldName}
546
+ field={collection.fields[fieldName]}
547
+ value={getCurrentValue(fieldName)}
548
+ readOnly={isFieldReadOnly(fieldName)}
549
+ relationOptions={relationOptionsByField[fieldName] ?? []}
550
+ relationMeta={relationMetaByField[fieldName]}
551
+ menuLinkOptions={menuLinkOptions}
552
+ blockRelationOptions={blockRelationOptions}
553
+ sharedBlockTypes={sharedBlockTypes}
554
+ sharedBlockRelationOptions={sharedBlockRelationOptions}
555
+ sharedSectionOptions={sharedSectionOptions}
556
+ />
557
+ {aiEnabled && fieldName === "seoDescription" && !isFieldReadOnly(fieldName) && (
558
+ <div class="mt-1.5">
559
+ <AiGenerateButton
560
+ client:load
561
+ endpoint="/api/cms/ai/seo"
562
+ payload={{
563
+ title: getCurrentValue("title") ?? "",
564
+ excerpt: getCurrentValue("excerpt") ?? "",
565
+ }}
566
+ targetField={fieldName}
567
+ label="Generate description"
568
+ />
569
+ </div>
570
+ )}
571
+ </div>
572
+ ))}
573
+ </div>
574
+ );
575
+ return variant === "record" ? <AdminCard title="Settings">{fields}</AdminCard> : fields;
576
+ })}
577
+ {hasSidebarMeta && (
578
+ <div
579
+ class:list={[
580
+ variant === "record" ? "space-y-6" : "grid gap-6",
581
+ variant !== "record" && hasSidebarFields && "border-t pt-6",
582
+ ]}
583
+ >
584
+ <ReverseReferencesList reverseRefs={reverseRefs} variant={variant} />
585
+ {hasDocMeta &&
586
+ doc &&
587
+ (() => {
588
+ const meta = (
589
+ <dl class="text-muted-foreground grid gap-1 text-xs">
590
+ {doc._status === "scheduled" && doc._publishAt && (
591
+ <>
592
+ <div class="flex justify-between gap-2">
593
+ <dt>Publishes</dt>
594
+ <dd class="text-foreground/70">{formatDate(doc._publishAt)}</dd>
595
+ </div>
596
+ {doc._unpublishAt && (
597
+ <div class="flex justify-between gap-2">
598
+ <dt>Unpublishes</dt>
599
+ <dd class="text-foreground/70">{formatDate(doc._unpublishAt)}</dd>
600
+ </div>
601
+ )}
602
+ </>
603
+ )}
604
+ <div class="flex justify-between gap-2">
605
+ <dt>Created</dt>
606
+ <dd class="text-foreground/70">{formatDate(doc._createdAt)}</dd>
607
+ </div>
608
+ <div class="flex justify-between gap-2">
609
+ <dt>Updated</dt>
610
+ <dd class="text-foreground/70">{formatDate(doc._updatedAt)}</dd>
611
+ </div>
612
+ </dl>
613
+ );
614
+
615
+ return variant === "record" ? (
616
+ <AdminCard title="Info">{meta}</AdminCard>
617
+ ) : (
618
+ <div class:list={["grid gap-2", reverseRefs.length > 0 && "border-t pt-6"]}>
619
+ <span class="text-foreground/80 text-sm leading-none font-medium">Info</span>
620
+ {meta}
621
+ </div>
622
+ );
623
+ })()}
624
+ </div>
625
+ )}
626
+ </div>
627
+ </div>
628
+ )}
629
+ </form>
630
+ )}
631
+
632
+ {translationMode && (
633
+ <div class="space-y-6">
634
+ <form
635
+ id="translation-form"
636
+ method="post"
637
+ action={getFormAction(collection.slug, documentId)}
638
+ class="max-w-3xl"
639
+ >
640
+ <input type="hidden" name="_action" value="save-translation" />
641
+ <input type="hidden" name="locale" value={selectedLocale} />
642
+ <input type="hidden" name="redirectTo" value={Astro.url.pathname + Astro.url.search} />
643
+
644
+ <div class="grid gap-7">
645
+ {translatableFields
646
+ .filter((fn) => !isFieldHidden(fn))
647
+ .map((fieldName) => {
648
+ const fieldDef = collection.fields[fieldName];
649
+ const fieldType =
650
+ fieldDef.type === "richText" ? "richText" : fieldDef.type === "slug" ? "slug" : "text";
651
+ const sourceValue = getSharedValue(fieldName);
652
+ const sourceText =
653
+ typeof sourceValue === "object" && sourceValue !== null
654
+ ? JSON.stringify(sourceValue)
655
+ : String(sourceValue ?? "");
656
+ return (
657
+ <div>
658
+ <FieldControl
659
+ name={fieldName}
660
+ field={fieldDef}
661
+ value={getCurrentValue(fieldName)}
662
+ readOnly={isFieldReadOnly(fieldName)}
663
+ relationOptions={relationOptionsByField[fieldName] ?? []}
664
+ relationMeta={relationMetaByField[fieldName]}
665
+ menuLinkOptions={menuLinkOptions}
666
+ blockRelationOptions={blockRelationOptions}
667
+ sharedBlockTypes={sharedBlockTypes}
668
+ sharedBlockRelationOptions={sharedBlockRelationOptions}
669
+ sharedSectionOptions={sharedSectionOptions}
670
+ />
671
+ {aiEnabled &&
672
+ sourceText &&
673
+ fieldDef.type !== "slug" &&
674
+ fieldDef.type !== "blocks" &&
675
+ fieldDef.type !== "content" && (
676
+ <div class="mt-1.5">
677
+ <AiGenerateButton
678
+ client:load
679
+ endpoint="/api/cms/ai/translate"
680
+ payload={{
681
+ text: sourceText,
682
+ sourceLocale: defaultLocale,
683
+ targetLocale: selectedLocale,
684
+ fieldName,
685
+ fieldType,
686
+ }}
687
+ targetField={fieldName}
688
+ label={`Translate from ${defaultLocale.toUpperCase()}`}
689
+ />
690
+ </div>
691
+ )}
692
+ </div>
693
+ );
694
+ })}
695
+ </div>
696
+ </form>
697
+ </div>
698
+ )}
699
+ </div>
700
+ </>
701
+ )
702
+ }
703
+ </div>
704
+ </section>
705
+
706
+ <EditScripts />