@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.
- package/LICENSE +21 -0
- package/admin/components/AssetsGrid.tsx +366 -173
- package/admin/components/BlockEditor.tsx +260 -679
- package/admin/components/CheckboxField.tsx +2 -2
- package/admin/components/CollaborationReviewBar.tsx +384 -0
- package/admin/components/ColorField.tsx +87 -0
- package/admin/components/CommandPalette.tsx +169 -0
- package/admin/components/ContentEditor.tsx +687 -0
- package/admin/components/DocumentActions.tsx +4 -2
- package/admin/components/DocumentsDataTable.tsx +91 -10
- package/admin/components/FieldControl.astro +151 -26
- package/admin/components/FocalPointSelector.tsx +27 -1
- package/admin/components/InternalLinkPicker.tsx +4 -3
- package/admin/components/LinkField.tsx +126 -0
- package/admin/components/RelationField.tsx +105 -26
- package/admin/components/RepeaterInput.tsx +46 -0
- package/admin/components/RichTextEditor.tsx +44 -9
- package/admin/components/SharedSectionBlockField.tsx +93 -0
- package/admin/components/SidebarUserMenu.tsx +1 -1
- package/admin/components/Toast.astro +1 -1
- package/admin/components/TreeItemsEditor.tsx +20 -19
- package/admin/components/TreeSelect.tsx +2 -2
- package/admin/components/YoutubeField.tsx +48 -0
- package/admin/components/block-fields.tsx +777 -0
- package/admin/components/content-block-spec.ts +114 -0
- package/admin/components/slash-command.tsx +242 -0
- package/admin/components/tree-utils.ts +14 -2
- package/admin/components/ui/badge.tsx +2 -3
- package/admin/components/ui/button.tsx +5 -4
- package/admin/components/ui/checkbox.tsx +1 -1
- package/admin/components/ui/input.tsx +1 -1
- package/admin/components/ui/select.tsx +1 -1
- package/admin/components/ui/table.tsx +1 -1
- package/admin/components/ui/textarea.tsx +1 -1
- package/admin/layouts/AdminLayout.astro +323 -123
- package/admin/lib/access.ts +54 -0
- package/admin/lib/collaboration.ts +178 -0
- package/admin/lib/edit-data.ts +448 -0
- package/admin/views/AccessDeniedView.astro +8 -0
- package/admin/views/EditView.astro +706 -0
- package/admin/views/ListView.astro +202 -0
- package/admin/views/RecentView.astro +76 -0
- package/admin/views/SinglesView.astro +65 -0
- package/admin/views/edit/EditScripts.astro +145 -0
- package/admin/views/edit/ReverseReferencesList.astro +68 -0
- package/core/admin.ts +193 -0
- package/core/ai.ts +87 -0
- package/core/api.ts +1392 -0
- package/core/assets.ts +336 -0
- package/core/audit.ts +84 -0
- package/core/auth-config.ts +44 -0
- package/core/auth.ts +303 -0
- package/core/collaboration.ts +276 -0
- package/core/content.ts +27 -0
- package/core/create-admin.ts +28 -0
- package/core/define.ts +625 -0
- package/core/field-model.ts +172 -0
- package/core/generator.ts +519 -0
- package/core/http.ts +42 -0
- package/core/image.ts +219 -0
- package/core/index.ts +211 -0
- package/core/locks.ts +52 -0
- package/core/log.ts +37 -0
- package/core/migrate.ts +278 -0
- package/core/rate-limit.ts +110 -0
- package/core/request-scope.ts +33 -0
- package/core/richtext.ts +1 -0
- package/core/runtime.ts +63 -0
- package/core/schema.ts +32 -0
- package/core/search.ts +489 -0
- package/core/seed.ts +101 -0
- package/core/shared-sections.ts +96 -0
- package/core/tasks.ts +176 -0
- package/core/values.ts +433 -0
- package/core/webhooks.ts +85 -0
- package/core/youtube.ts +39 -0
- package/internals/cli.mjs +34 -0
- package/internals/context.ts +67 -0
- package/internals/create-admin.ts +40 -0
- package/internals/describe.ts +25 -0
- package/internals/eject.ts +144 -0
- package/internals/generator.ts +11 -0
- package/internals/integration.ts +481 -0
- package/internals/mcp.ts +397 -0
- package/internals/project.ts +26 -0
- package/internals/push.ts +160 -0
- package/internals/reindex.ts +11 -0
- package/internals/restore.ts +257 -0
- package/internals/seed.data.ts +7 -0
- package/internals/seed.ts +11 -0
- package/internals/upgrade.ts +776 -0
- package/{virtual.d.ts → internals/virtual.d.ts} +39 -5
- package/middleware/auth.ts +130 -17
- package/middleware/push.ts +121 -0
- package/package.json +55 -67
- package/platform/cloudflare/cf-env.ts +60 -0
- package/platform/cloudflare/database.ts +28 -0
- package/platform/cloudflare/globals.d.ts +5 -0
- package/platform/cloudflare/storage.ts +33 -0
- package/platform/cloudflare/uploads-route.ts +37 -0
- package/platform/node/database.ts +39 -0
- package/platform/node/storage.ts +52 -0
- package/routes/admin/[...path].astro +71 -0
- package/routes/{pages/admin → admin}/assets/[id].astro +25 -19
- package/routes/admin/assets/index.astro +90 -0
- package/routes/{pages/admin/login.astro → admin/forgot-password.astro} +21 -19
- package/routes/{pages/admin → admin}/invite.astro +6 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +31 -8
- package/routes/api/{cms/[collection] → [collection]}/[...path].ts +66 -14
- package/routes/api/admin/search.ts +89 -0
- package/routes/api/{cms/assets → assets}/[id].ts +24 -14
- package/routes/api/{cms/assets → assets}/index.ts +4 -2
- package/routes/api/assets/upload.ts +147 -0
- package/routes/api/auth/forgot-password.ts +77 -0
- package/routes/api/{cms/auth → auth}/invite.ts +41 -9
- package/routes/api/auth/login.ts +167 -0
- package/routes/api/{cms/auth → auth}/logout.ts +11 -2
- package/routes/api/auth/reset-password.ts +85 -0
- package/routes/api/auth/setup.ts +110 -0
- package/routes/api/auth/sso/[provider]/start.ts +34 -0
- package/routes/api/collaboration/index.ts +106 -0
- package/routes/api/cron/_authorize.ts +25 -0
- package/routes/api/{cms/cron → cron}/publish.ts +3 -9
- package/routes/api/cron/tasks.ts +33 -0
- package/routes/api/forms/submit/[slug].ts +143 -0
- package/routes/api/img/[...path].ts +65 -0
- package/routes/api/{cms/preview → preview}/render.ts +8 -3
- package/routes/api/{cms/references → references}/[collection]/[id].ts +31 -1
- package/routes/api/webhooks/[provider].ts +54 -0
- package/README.md +0 -28
- package/dist/admin.d.ts +0 -33
- package/dist/admin.js +0 -92
- package/dist/ai.d.ts +0 -15
- package/dist/ai.js +0 -70
- package/dist/api.d.ts +0 -14
- package/dist/api.js +0 -833
- package/dist/assets.d.ts +0 -51
- package/dist/assets.js +0 -163
- package/dist/auth.d.ts +0 -30
- package/dist/auth.js +0 -132
- package/dist/content.d.ts +0 -3
- package/dist/content.js +0 -29
- package/dist/create-admin.d.ts +0 -5
- package/dist/create-admin.js +0 -23
- package/dist/define.d.ts +0 -254
- package/dist/define.js +0 -36
- package/dist/generator.d.ts +0 -9
- package/dist/generator.js +0 -370
- package/dist/image.d.ts +0 -9
- package/dist/image.js +0 -69
- package/dist/index.d.ts +0 -28
- package/dist/index.js +0 -16
- package/dist/integration.d.ts +0 -14
- package/dist/integration.js +0 -327
- package/dist/locks.d.ts +0 -7
- package/dist/locks.js +0 -37
- package/dist/richtext.d.ts +0 -1
- package/dist/richtext.js +0 -1
- package/dist/runtime.d.ts +0 -24
- package/dist/runtime.js +0 -26
- package/dist/schema.d.ts +0 -17
- package/dist/schema.js +0 -13
- package/dist/seed.d.ts +0 -2
- package/dist/seed.js +0 -84
- package/dist/values.d.ts +0 -8
- package/dist/values.js +0 -102
- package/dist/webhooks.d.ts +0 -2
- package/dist/webhooks.js +0 -73
- package/routes/api/cms/assets/upload.ts +0 -112
- package/routes/api/cms/auth/login.ts +0 -124
- package/routes/api/cms/auth/setup.ts +0 -77
- package/routes/api/cms/img/[...path].ts +0 -24
- package/routes/pages/admin/[...path].astro +0 -1107
- package/routes/pages/admin/assets/index.astro +0 -63
- /package/routes/api/{cms/ai → ai}/alt-text.ts +0 -0
- /package/routes/api/{cms/ai → ai}/seo.ts +0 -0
- /package/routes/api/{cms/ai → ai}/translate.ts +0 -0
- /package/routes/api/{cms/assets → assets}/folders.ts +0 -0
- /package/routes/api/{cms/locks → locks}/[...path].ts +0 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { ReviewState } from "../../core";
|
|
2
|
+
|
|
3
|
+
// Display helpers for the collaboration UI (edit strip + list columns).
|
|
4
|
+
|
|
5
|
+
export type CollabUser = { id: string; name: string; initials: string; color: string };
|
|
6
|
+
export type CollabComment = {
|
|
7
|
+
id: string;
|
|
8
|
+
author: CollabUser;
|
|
9
|
+
time: string;
|
|
10
|
+
field: string | null;
|
|
11
|
+
body: string;
|
|
12
|
+
resolved: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type CollabActivity = { actor: CollabUser; text: string; time: string };
|
|
15
|
+
|
|
16
|
+
export type CollaborationData = {
|
|
17
|
+
reviewState: ReviewState;
|
|
18
|
+
editor: CollabUser | null;
|
|
19
|
+
assignableUsers: CollabUser[];
|
|
20
|
+
comments: CollabComment[];
|
|
21
|
+
activity: CollabActivity[];
|
|
22
|
+
currentUser: CollabUser | null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const PALETTE = [
|
|
26
|
+
"bg-rose-500",
|
|
27
|
+
"bg-sky-500",
|
|
28
|
+
"bg-violet-500",
|
|
29
|
+
"bg-emerald-500",
|
|
30
|
+
"bg-amber-500",
|
|
31
|
+
"bg-indigo-500",
|
|
32
|
+
"bg-pink-500",
|
|
33
|
+
"bg-teal-500",
|
|
34
|
+
"bg-cyan-500",
|
|
35
|
+
"bg-orange-500",
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const hashString = (value: string): number => {
|
|
39
|
+
let hash = 0;
|
|
40
|
+
for (let i = 0; i < value.length; i += 1) hash = (hash * 31 + value.charCodeAt(i)) | 0;
|
|
41
|
+
return Math.abs(hash);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const colorFor = (seed: string): string => PALETTE[hashString(seed || "seed") % PALETTE.length]!;
|
|
45
|
+
|
|
46
|
+
const initialsFrom = (name: string, email: string): string => {
|
|
47
|
+
const source = (name || email || "?").trim();
|
|
48
|
+
const parts = source.split(/[\s._-]+/).filter(Boolean);
|
|
49
|
+
if (parts.length >= 2) return (parts[0]![0]! + parts[1]![0]!).toUpperCase();
|
|
50
|
+
return source.slice(0, 2).toUpperCase();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const REVIEW_STATE_META: Record<ReviewState, { label: string; badge: string; dot: string }> = {
|
|
54
|
+
in_progress: {
|
|
55
|
+
label: "In progress",
|
|
56
|
+
badge: "border-border bg-muted text-muted-foreground",
|
|
57
|
+
dot: "bg-muted-foreground",
|
|
58
|
+
},
|
|
59
|
+
ready_for_review: {
|
|
60
|
+
label: "Ready for review",
|
|
61
|
+
badge: "border-amber-500/30 bg-amber-500/10 text-amber-700 dark:text-amber-400",
|
|
62
|
+
dot: "bg-amber-500",
|
|
63
|
+
},
|
|
64
|
+
changes_requested: {
|
|
65
|
+
label: "Changes requested",
|
|
66
|
+
badge: "border-orange-500/30 bg-orange-500/10 text-orange-700 dark:text-orange-400",
|
|
67
|
+
dot: "bg-orange-500",
|
|
68
|
+
},
|
|
69
|
+
approved: {
|
|
70
|
+
label: "Approved",
|
|
71
|
+
badge: "border-emerald-500/30 bg-emerald-500/10 text-emerald-700 dark:text-emerald-400",
|
|
72
|
+
dot: "bg-emerald-500",
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// Map an audit-log action to a human sentence fragment for the activity feed.
|
|
77
|
+
const ACTION_LABELS: Record<string, string> = {
|
|
78
|
+
"collab.review.in_progress": "moved this to In progress",
|
|
79
|
+
"collab.review.ready_for_review": "submitted for review",
|
|
80
|
+
"collab.review.changes_requested": "requested changes",
|
|
81
|
+
"collab.review.approved": "approved this",
|
|
82
|
+
"collab.editor.set": "changed the assignee",
|
|
83
|
+
"collab.editor.clear": "cleared the assignee",
|
|
84
|
+
"collab.comment": "commented",
|
|
85
|
+
"collab.comment.resolve": "resolved a comment",
|
|
86
|
+
"collab.comment.reopen": "reopened a comment",
|
|
87
|
+
"collab.comment.delete": "deleted a comment",
|
|
88
|
+
"content.create": "created this document",
|
|
89
|
+
"content.update": "saved changes",
|
|
90
|
+
"content.publish": "published this",
|
|
91
|
+
"content.unpublish": "unpublished this",
|
|
92
|
+
"content.schedule": "scheduled this",
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const actionLabel = (action: string): string =>
|
|
96
|
+
ACTION_LABELS[action] ?? action.replace(/^[a-z]+\./, "").replace(/[._]/g, " ");
|
|
97
|
+
|
|
98
|
+
const relativeTime = (input: string | number): string => {
|
|
99
|
+
const t = typeof input === "number" ? input : Date.parse(input);
|
|
100
|
+
if (Number.isNaN(t)) return "";
|
|
101
|
+
const diff = Date.now() - t;
|
|
102
|
+
const s = Math.floor(diff / 1000);
|
|
103
|
+
if (s < 45) return "just now";
|
|
104
|
+
const m = Math.floor(s / 60);
|
|
105
|
+
if (m < 60) return `${m}m ago`;
|
|
106
|
+
const h = Math.floor(m / 60);
|
|
107
|
+
if (h < 24) return `${h}h ago`;
|
|
108
|
+
const d = Math.floor(h / 24);
|
|
109
|
+
if (d < 7) return `${d}d ago`;
|
|
110
|
+
return new Date(t).toLocaleDateString();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
type CollabModule = {
|
|
114
|
+
getState: (collection: string, id: string) => Promise<{ reviewState: ReviewState; editor: string | null }>;
|
|
115
|
+
listComments: (collection: string, id: string) => Promise<any[]>;
|
|
116
|
+
getActivity: (collection: string, id: string) => Promise<any[]>;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
type CurrentUser = { id: string; email?: string; role?: string; name?: string } | null | undefined;
|
|
120
|
+
|
|
121
|
+
// Load and fully resolve collaboration data for one document.
|
|
122
|
+
export async function loadCollaborationData(opts: {
|
|
123
|
+
collaboration: CollabModule;
|
|
124
|
+
cmsRuntime: Record<string, any>;
|
|
125
|
+
runtimeContext: unknown;
|
|
126
|
+
collection: string;
|
|
127
|
+
documentId: string;
|
|
128
|
+
currentUser: CurrentUser;
|
|
129
|
+
}): Promise<CollaborationData> {
|
|
130
|
+
const { collaboration, cmsRuntime, runtimeContext, collection, documentId, currentUser } = opts;
|
|
131
|
+
|
|
132
|
+
const users: any[] = await cmsRuntime.users?.find({ status: "any", limit: 100 }, runtimeContext).catch(() => []);
|
|
133
|
+
const userMap = new Map<string, { name: string; email: string }>();
|
|
134
|
+
for (const u of users ?? []) {
|
|
135
|
+
userMap.set(String(u._id), { name: String(u.name ?? u.email ?? "User"), email: String(u.email ?? "") });
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const toUser = (id: string | null | undefined, emailFallback?: string | null): CollabUser => {
|
|
139
|
+
const record = id ? userMap.get(String(id)) : null;
|
|
140
|
+
const email = record?.email || emailFallback || "";
|
|
141
|
+
const name = record?.name || (email ? email.split("@")[0]! : "Unknown");
|
|
142
|
+
const seed = String(id || email || name);
|
|
143
|
+
return { id: String(id ?? ""), name, initials: initialsFrom(name, email), color: colorFor(seed) };
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const [state, comments, activity] = await Promise.all([
|
|
147
|
+
collaboration.getState(collection, documentId),
|
|
148
|
+
collaboration.listComments(collection, documentId),
|
|
149
|
+
collaboration.getActivity(collection, documentId),
|
|
150
|
+
]);
|
|
151
|
+
|
|
152
|
+
const resolvedComments: CollabComment[] = comments
|
|
153
|
+
.map((c) => ({
|
|
154
|
+
id: String(c._id),
|
|
155
|
+
author: toUser(c.authorId, c.authorEmail),
|
|
156
|
+
time: relativeTime(c.createdAt),
|
|
157
|
+
field: c.field ?? null,
|
|
158
|
+
body: String(c.body),
|
|
159
|
+
resolved: Boolean(c.resolved),
|
|
160
|
+
}))
|
|
161
|
+
// listComments is newest-first; show comments oldest-first like a thread.
|
|
162
|
+
.reverse();
|
|
163
|
+
|
|
164
|
+
const resolvedActivity: CollabActivity[] = activity.map((a) => ({
|
|
165
|
+
actor: toUser(a.actorId, a.actorEmail),
|
|
166
|
+
text: actionLabel(String(a.action)),
|
|
167
|
+
time: relativeTime(a.timestamp),
|
|
168
|
+
}));
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
reviewState: state.reviewState,
|
|
172
|
+
editor: state.editor ? toUser(state.editor) : null,
|
|
173
|
+
assignableUsers: (users ?? []).map((u) => toUser(String(u._id), String(u.email ?? ""))),
|
|
174
|
+
comments: resolvedComments,
|
|
175
|
+
activity: resolvedActivity,
|
|
176
|
+
currentUser: currentUser ? toUser(currentUser.id, currentUser.email) : null,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SHARED_SECTIONS_COLLECTION,
|
|
3
|
+
extractSharedSectionRefsFromDocument,
|
|
4
|
+
getLabelField,
|
|
5
|
+
getSharedBlockTypes,
|
|
6
|
+
type SharedSectionOption,
|
|
7
|
+
} from "../../core";
|
|
8
|
+
import { canRead } from "./access";
|
|
9
|
+
|
|
10
|
+
type User = { id: string; role?: string; email?: string } | null | undefined;
|
|
11
|
+
type CmsRuntime = Record<string, any> & { meta: { getRouteForDocument: (slug: string, doc: any) => string } };
|
|
12
|
+
type RuntimeContext = Record<string, unknown>;
|
|
13
|
+
type CollectionLike = { slug: string; fields: Record<string, any> };
|
|
14
|
+
|
|
15
|
+
type LoadedDocument = {
|
|
16
|
+
doc: Record<string, unknown> | null;
|
|
17
|
+
baseDoc: Record<string, unknown> | null;
|
|
18
|
+
versions: Array<{ version: number; createdAt: string }>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Load the primary doc, base-locale doc (for translation source), and versions list. */
|
|
22
|
+
export async function loadDocument(
|
|
23
|
+
collectionApi: any,
|
|
24
|
+
documentId: string,
|
|
25
|
+
requestedLocale: string,
|
|
26
|
+
defaultLocale: string,
|
|
27
|
+
runtimeContext: RuntimeContext,
|
|
28
|
+
): Promise<LoadedDocument> {
|
|
29
|
+
const doc = await collectionApi.findById(documentId, { status: "any", locale: requestedLocale }, runtimeContext);
|
|
30
|
+
if (!doc) return { doc: null, baseDoc: null, versions: [] };
|
|
31
|
+
const baseDoc =
|
|
32
|
+
requestedLocale === defaultLocale
|
|
33
|
+
? doc
|
|
34
|
+
: await collectionApi.findById(documentId, { status: "any", locale: defaultLocale }, runtimeContext);
|
|
35
|
+
const versions = await collectionApi.versions(documentId, runtimeContext);
|
|
36
|
+
return { doc, baseDoc, versions };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type RelationMeta = {
|
|
40
|
+
collectionSlug: string;
|
|
41
|
+
collectionLabel: string;
|
|
42
|
+
hasMany: boolean;
|
|
43
|
+
labelField?: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type RelationOptions = {
|
|
47
|
+
relationOptionsByField: Record<string, Array<{ value: string; label: string }>>;
|
|
48
|
+
relationMetaByField: Record<string, RelationMeta>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const loadRelationOptionList = async (
|
|
52
|
+
collectionSlug: string,
|
|
53
|
+
config: any,
|
|
54
|
+
cmsRuntime: CmsRuntime,
|
|
55
|
+
defaultLocale: string,
|
|
56
|
+
runtimeContext: RuntimeContext,
|
|
57
|
+
) => {
|
|
58
|
+
const relatedDocs = await cmsRuntime[collectionSlug].find(
|
|
59
|
+
{ status: "any", limit: 100, sort: { field: "_updatedAt", direction: "desc" }, locale: defaultLocale },
|
|
60
|
+
runtimeContext,
|
|
61
|
+
);
|
|
62
|
+
const relatedCollection = config.collections.find((c: any) => c.slug === collectionSlug);
|
|
63
|
+
const labelField = relatedCollection ? getLabelField(relatedCollection) : "title";
|
|
64
|
+
return relatedDocs.map((item: Record<string, unknown>) => ({
|
|
65
|
+
value: String(item._id),
|
|
66
|
+
label: String(item[labelField] ?? item.slug ?? item._id),
|
|
67
|
+
}));
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** Fetch relation option lists for top-level relation fields and any relations nested inside blocks. */
|
|
71
|
+
export async function loadRelationOptions(
|
|
72
|
+
collection: CollectionLike,
|
|
73
|
+
config: any,
|
|
74
|
+
user: User,
|
|
75
|
+
cmsRuntime: CmsRuntime,
|
|
76
|
+
defaultLocale: string,
|
|
77
|
+
runtimeContext: RuntimeContext,
|
|
78
|
+
): Promise<RelationOptions> {
|
|
79
|
+
const relationOptionsByField: Record<string, Array<{ value: string; label: string }>> = {};
|
|
80
|
+
const relationMetaByField: Record<string, RelationMeta> = {};
|
|
81
|
+
|
|
82
|
+
for (const [fieldName, field] of Object.entries(collection.fields) as [string, any][]) {
|
|
83
|
+
if (field.type === "relation" && canRead(config, user, field.collection)) {
|
|
84
|
+
const relatedCollection = config.collections.find((c: any) => c.slug === field.collection);
|
|
85
|
+
relationOptionsByField[fieldName] = await loadRelationOptionList(
|
|
86
|
+
field.collection,
|
|
87
|
+
config,
|
|
88
|
+
cmsRuntime,
|
|
89
|
+
defaultLocale,
|
|
90
|
+
runtimeContext,
|
|
91
|
+
);
|
|
92
|
+
if (relatedCollection) {
|
|
93
|
+
relationMetaByField[fieldName] = {
|
|
94
|
+
collectionSlug: field.collection,
|
|
95
|
+
collectionLabel: relatedCollection.labels.singular,
|
|
96
|
+
hasMany: field.hasMany ?? false,
|
|
97
|
+
labelField: getLabelField(relatedCollection),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Relations nested inside block types — both standalone `blocks` fields and inline
|
|
102
|
+
// blocks in a `content` field use the same `block:<field>:<type>:<subField>` key scheme.
|
|
103
|
+
// The parent field name keeps two block-bearing fields that share a block-type name
|
|
104
|
+
// (but target different collections) from colliding.
|
|
105
|
+
const blockTypes = field.type === "blocks" ? field.types : field.type === "content" ? field.blocks : null;
|
|
106
|
+
if (blockTypes) {
|
|
107
|
+
for (const [typeName, typeFields] of Object.entries(blockTypes)) {
|
|
108
|
+
for (const [subFieldName, subField] of Object.entries(typeFields as Record<string, any>) as [string, any][]) {
|
|
109
|
+
if (subField.type === "relation" && canRead(config, user, subField.collection)) {
|
|
110
|
+
const key = `block:${fieldName}:${typeName}:${subFieldName}`;
|
|
111
|
+
if (!relationOptionsByField[key]) {
|
|
112
|
+
relationOptionsByField[key] = await loadRelationOptionList(
|
|
113
|
+
subField.collection,
|
|
114
|
+
config,
|
|
115
|
+
cmsRuntime,
|
|
116
|
+
defaultLocale,
|
|
117
|
+
runtimeContext,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return { relationOptionsByField, relationMetaByField };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function loadSharedBlockRelationOptions(
|
|
130
|
+
config: any,
|
|
131
|
+
user: User,
|
|
132
|
+
cmsRuntime: CmsRuntime,
|
|
133
|
+
defaultLocale: string,
|
|
134
|
+
runtimeContext: RuntimeContext,
|
|
135
|
+
): Promise<Record<string, Array<{ value: string; label: string }>>> {
|
|
136
|
+
const relationOptions: Record<string, Array<{ value: string; label: string }>> = {};
|
|
137
|
+
const sharedBlockTypes = getSharedBlockTypes(config);
|
|
138
|
+
|
|
139
|
+
for (const [typeName, typeFields] of Object.entries(sharedBlockTypes)) {
|
|
140
|
+
for (const [fieldName, field] of Object.entries(typeFields) as [string, any][]) {
|
|
141
|
+
if (field.type !== "relation" || !canRead(config, user, field.collection)) continue;
|
|
142
|
+
relationOptions[`shared:${typeName}:${fieldName}`] = await loadRelationOptionList(
|
|
143
|
+
field.collection,
|
|
144
|
+
config,
|
|
145
|
+
cmsRuntime,
|
|
146
|
+
defaultLocale,
|
|
147
|
+
runtimeContext,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return relationOptions;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export async function loadSharedSectionOptions(
|
|
156
|
+
config: any,
|
|
157
|
+
user: User,
|
|
158
|
+
cmsRuntime: CmsRuntime,
|
|
159
|
+
defaultLocale: string,
|
|
160
|
+
runtimeContext: RuntimeContext,
|
|
161
|
+
): Promise<SharedSectionOption[]> {
|
|
162
|
+
if (!config.collections.some((c: any) => c.slug === SHARED_SECTIONS_COLLECTION)) return [];
|
|
163
|
+
if (!canRead(config, user, SHARED_SECTIONS_COLLECTION)) return [];
|
|
164
|
+
const api = cmsRuntime[SHARED_SECTIONS_COLLECTION];
|
|
165
|
+
if (!api) return [];
|
|
166
|
+
|
|
167
|
+
const docs = await api.find(
|
|
168
|
+
{ status: "any", limit: 500, sort: { field: "title", direction: "asc" }, locale: defaultLocale },
|
|
169
|
+
runtimeContext,
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return docs.map((doc: Record<string, unknown>) => ({
|
|
173
|
+
id: String(doc._id),
|
|
174
|
+
title: String(doc.title ?? doc._id),
|
|
175
|
+
blockType: String(doc.blockType ?? (doc.block as Record<string, unknown> | undefined)?.type ?? ""),
|
|
176
|
+
status: String(doc._status ?? "draft"),
|
|
177
|
+
}));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export async function loadSharedSectionUsage(
|
|
181
|
+
sectionId: string,
|
|
182
|
+
config: any,
|
|
183
|
+
user: User,
|
|
184
|
+
cmsRuntime: CmsRuntime,
|
|
185
|
+
defaultLocale: string,
|
|
186
|
+
runtimeContext: RuntimeContext,
|
|
187
|
+
): Promise<ReverseRef[]> {
|
|
188
|
+
const reverseRefs: ReverseRef[] = [];
|
|
189
|
+
|
|
190
|
+
for (const otherCollection of config.collections) {
|
|
191
|
+
if (otherCollection.slug === SHARED_SECTIONS_COLLECTION || !canRead(config, user, otherCollection.slug)) continue;
|
|
192
|
+
const hasSharedCapableField = Object.values(otherCollection.fields).some(
|
|
193
|
+
(field: any) => (field.type === "blocks" && field.shared !== false) || field.type === "content",
|
|
194
|
+
);
|
|
195
|
+
if (!hasSharedCapableField) continue;
|
|
196
|
+
|
|
197
|
+
const otherApi = cmsRuntime[otherCollection.slug];
|
|
198
|
+
if (!otherApi) continue;
|
|
199
|
+
const otherLabelField = getLabelField(otherCollection);
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
const docs = await otherApi.find({ status: "any", limit: 500, locale: defaultLocale }, runtimeContext);
|
|
203
|
+
const matched = docs.filter((doc: Record<string, unknown>) =>
|
|
204
|
+
extractSharedSectionRefsFromDocument(otherCollection, doc).includes(sectionId),
|
|
205
|
+
);
|
|
206
|
+
if (matched.length > 0) {
|
|
207
|
+
reverseRefs.push({
|
|
208
|
+
collectionLabel: otherCollection.singleton ? "Singleton" : otherCollection.labels.plural,
|
|
209
|
+
collectionSlug: otherCollection.slug,
|
|
210
|
+
docs: matched.map((doc: Record<string, unknown>) => ({
|
|
211
|
+
_id: String(doc._id),
|
|
212
|
+
label: otherCollection.singleton
|
|
213
|
+
? otherCollection.labels.singular
|
|
214
|
+
: String(doc[otherLabelField] ?? doc.slug ?? doc._id),
|
|
215
|
+
})),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
} catch {
|
|
219
|
+
// ignore individual collection failures
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return reverseRefs;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export async function loadSharedSectionUsageCounts(
|
|
227
|
+
sectionIds: string[],
|
|
228
|
+
config: any,
|
|
229
|
+
user: User,
|
|
230
|
+
cmsRuntime: CmsRuntime,
|
|
231
|
+
defaultLocale: string,
|
|
232
|
+
runtimeContext: RuntimeContext,
|
|
233
|
+
): Promise<Record<string, number>> {
|
|
234
|
+
const counts = Object.fromEntries(sectionIds.map((id) => [id, 0]));
|
|
235
|
+
const sectionIdSet = new Set(sectionIds);
|
|
236
|
+
|
|
237
|
+
if (sectionIds.length === 0) return counts;
|
|
238
|
+
|
|
239
|
+
for (const otherCollection of config.collections) {
|
|
240
|
+
if (otherCollection.slug === SHARED_SECTIONS_COLLECTION || !canRead(config, user, otherCollection.slug)) continue;
|
|
241
|
+
const hasSharedCapableField = Object.values(otherCollection.fields).some(
|
|
242
|
+
(field: any) => (field.type === "blocks" && field.shared !== false) || field.type === "content",
|
|
243
|
+
);
|
|
244
|
+
if (!hasSharedCapableField) continue;
|
|
245
|
+
|
|
246
|
+
const otherApi = cmsRuntime[otherCollection.slug];
|
|
247
|
+
if (!otherApi) continue;
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
const docs = await otherApi.find({ status: "any", limit: 500, locale: defaultLocale }, runtimeContext);
|
|
251
|
+
for (const doc of docs as Array<Record<string, unknown>>) {
|
|
252
|
+
const refs = new Set(extractSharedSectionRefsFromDocument(otherCollection, doc));
|
|
253
|
+
for (const ref of refs) {
|
|
254
|
+
if (sectionIdSet.has(ref)) counts[ref] = (counts[ref] ?? 0) + 1;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
} catch {
|
|
258
|
+
// ignore individual collection failures
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return counts;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export type MenuLinkGroup = {
|
|
266
|
+
collection: string;
|
|
267
|
+
label: string;
|
|
268
|
+
items: Array<{ id: string; label: string; href: string }>;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/** For menu editing: list linkable published documents across content collections. */
|
|
272
|
+
export async function loadMenuLinkOptions(
|
|
273
|
+
config: any,
|
|
274
|
+
user: User,
|
|
275
|
+
cmsRuntime: CmsRuntime,
|
|
276
|
+
defaultLocale: string,
|
|
277
|
+
runtimeContext: RuntimeContext,
|
|
278
|
+
): Promise<MenuLinkGroup[]> {
|
|
279
|
+
const linkableCollections = config.collections.filter(
|
|
280
|
+
(c: any) =>
|
|
281
|
+
!c.singleton &&
|
|
282
|
+
!["users", "menus", "taxonomies", "authors"].includes(c.slug) &&
|
|
283
|
+
c.fields.slug &&
|
|
284
|
+
canRead(config, user, c.slug),
|
|
285
|
+
);
|
|
286
|
+
const menuLinkOptions: MenuLinkGroup[] = [];
|
|
287
|
+
for (const lc of linkableCollections) {
|
|
288
|
+
const lcApi = cmsRuntime[lc.slug];
|
|
289
|
+
const lcDocs = await lcApi.find(
|
|
290
|
+
{ status: "published", limit: 200, sort: { field: "_updatedAt", direction: "desc" }, locale: defaultLocale },
|
|
291
|
+
runtimeContext,
|
|
292
|
+
);
|
|
293
|
+
menuLinkOptions.push({
|
|
294
|
+
collection: lc.slug,
|
|
295
|
+
label: lc.labels.plural,
|
|
296
|
+
items: lcDocs.map((d: Record<string, unknown>) => ({
|
|
297
|
+
id: String(d._id),
|
|
298
|
+
label: String(d[getLabelField(lc)] ?? d.slug ?? d._id),
|
|
299
|
+
href: cmsRuntime.meta.getRouteForDocument(lc.slug, d),
|
|
300
|
+
})),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
return menuLinkOptions;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export type ReverseRef = {
|
|
307
|
+
collectionLabel: string;
|
|
308
|
+
collectionSlug: string;
|
|
309
|
+
docs: Array<{ _id: string; label: string }>;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/** Find documents in other collections that link to this doc via relation fields. */
|
|
313
|
+
export async function loadReverseRefs(
|
|
314
|
+
collection: CollectionLike,
|
|
315
|
+
documentId: string,
|
|
316
|
+
config: any,
|
|
317
|
+
user: User,
|
|
318
|
+
cmsRuntime: CmsRuntime,
|
|
319
|
+
defaultLocale: string,
|
|
320
|
+
runtimeContext: RuntimeContext,
|
|
321
|
+
): Promise<ReverseRef[]> {
|
|
322
|
+
if (collection.slug === SHARED_SECTIONS_COLLECTION) {
|
|
323
|
+
return loadSharedSectionUsage(documentId, config, user, cmsRuntime, defaultLocale, runtimeContext);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const reverseRefs: ReverseRef[] = [];
|
|
327
|
+
for (const otherCollection of config.collections) {
|
|
328
|
+
if (otherCollection.slug === collection.slug || !canRead(config, user, otherCollection.slug)) continue;
|
|
329
|
+
const relationFields = Object.entries(otherCollection.fields).filter(
|
|
330
|
+
([, f]) => (f as any).type === "relation" && (f as any).collection === collection.slug,
|
|
331
|
+
);
|
|
332
|
+
if (relationFields.length === 0) continue;
|
|
333
|
+
const otherApi = cmsRuntime[otherCollection.slug];
|
|
334
|
+
if (!otherApi) continue;
|
|
335
|
+
const otherLabelField = getLabelField(otherCollection);
|
|
336
|
+
for (const [fieldName, field] of relationFields) {
|
|
337
|
+
try {
|
|
338
|
+
const isMany = (field as any).hasMany;
|
|
339
|
+
if (isMany) {
|
|
340
|
+
const allDocs = await otherApi.find({ status: "any", limit: 200, locale: defaultLocale }, runtimeContext);
|
|
341
|
+
const matched = allDocs.filter((r: Record<string, unknown>) => {
|
|
342
|
+
const val = r[fieldName];
|
|
343
|
+
if (Array.isArray(val)) return val.includes(documentId);
|
|
344
|
+
if (typeof val === "string") return val.includes(documentId);
|
|
345
|
+
return false;
|
|
346
|
+
});
|
|
347
|
+
if (matched.length > 0) {
|
|
348
|
+
reverseRefs.push({
|
|
349
|
+
collectionLabel: otherCollection.singleton ? "Singleton" : otherCollection.labels.plural,
|
|
350
|
+
collectionSlug: otherCollection.slug,
|
|
351
|
+
docs: matched.map((r: Record<string, unknown>) => ({
|
|
352
|
+
_id: String(r._id),
|
|
353
|
+
label: otherCollection.singleton
|
|
354
|
+
? otherCollection.labels.singular
|
|
355
|
+
: String(r[otherLabelField] ?? r.slug ?? r._id),
|
|
356
|
+
})),
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
} else {
|
|
360
|
+
const refs = await otherApi.find(
|
|
361
|
+
{ status: "any", where: { [fieldName]: documentId }, limit: 50, locale: defaultLocale },
|
|
362
|
+
runtimeContext,
|
|
363
|
+
);
|
|
364
|
+
if (refs.length > 0) {
|
|
365
|
+
reverseRefs.push({
|
|
366
|
+
collectionLabel: otherCollection.singleton ? "Singleton" : otherCollection.labels.plural,
|
|
367
|
+
collectionSlug: otherCollection.slug,
|
|
368
|
+
docs: refs.map((r: Record<string, unknown>) => ({
|
|
369
|
+
_id: String(r._id),
|
|
370
|
+
label: otherCollection.singleton
|
|
371
|
+
? otherCollection.labels.singular
|
|
372
|
+
: String(r[otherLabelField] ?? r.slug ?? r._id),
|
|
373
|
+
})),
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
} catch {
|
|
378
|
+
// ignore individual relation fetch failures
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return reverseRefs;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export type FormSubmissionRow = {
|
|
386
|
+
id: string;
|
|
387
|
+
editHref: string;
|
|
388
|
+
status: string;
|
|
389
|
+
locales: string[];
|
|
390
|
+
searchText: string;
|
|
391
|
+
values: Record<string, string>;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/** Load submissions for a specific form, formatted for DocumentsDataTable rendering. */
|
|
395
|
+
export async function loadFormSubmissions(
|
|
396
|
+
formDocumentId: string,
|
|
397
|
+
defaultLocale: string,
|
|
398
|
+
cmsRuntime: CmsRuntime,
|
|
399
|
+
runtimeContext: RuntimeContext,
|
|
400
|
+
formatDate: (v: unknown) => string,
|
|
401
|
+
): Promise<{ submissionRows: FormSubmissionRow[]; submissionCount: number }> {
|
|
402
|
+
try {
|
|
403
|
+
const submissions = (await cmsRuntime["form-submissions"].find(
|
|
404
|
+
{ where: { form: formDocumentId }, sort: { field: "_createdAt", direction: "desc" }, limit: 500 },
|
|
405
|
+
runtimeContext,
|
|
406
|
+
)) as Array<Record<string, unknown>>;
|
|
407
|
+
const submissionRows: FormSubmissionRow[] = submissions.map((entry) => {
|
|
408
|
+
const data = (entry.data ?? {}) as Record<string, unknown>;
|
|
409
|
+
const summary = Object.entries(data)
|
|
410
|
+
.slice(0, 2)
|
|
411
|
+
.map(([k, v]) => `${k}: ${typeof v === "string" ? v : JSON.stringify(v)}`)
|
|
412
|
+
.join(" · ");
|
|
413
|
+
return {
|
|
414
|
+
id: String(entry._id),
|
|
415
|
+
editHref: `/admin/form-submissions/${entry._id}`,
|
|
416
|
+
status: String(entry.status ?? "new"),
|
|
417
|
+
locales: [defaultLocale],
|
|
418
|
+
searchText: summary,
|
|
419
|
+
values: {
|
|
420
|
+
submittedAt: formatDate(entry._createdAt),
|
|
421
|
+
status: String(entry.status ?? "new"),
|
|
422
|
+
summary: summary || "—",
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
});
|
|
426
|
+
return { submissionRows, submissionCount: submissions.length };
|
|
427
|
+
} catch {
|
|
428
|
+
return { submissionRows: [], submissionCount: 0 };
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** Resolve the parent form title shown on a form-submission detail page. */
|
|
433
|
+
export async function loadParentFormTitle(
|
|
434
|
+
formId: string,
|
|
435
|
+
cmsRuntime: CmsRuntime,
|
|
436
|
+
runtimeContext: RuntimeContext,
|
|
437
|
+
): Promise<string | null> {
|
|
438
|
+
try {
|
|
439
|
+
const parentForm = (await cmsRuntime.forms.findOne({ where: { _id: formId } }, runtimeContext)) as Record<
|
|
440
|
+
string,
|
|
441
|
+
unknown
|
|
442
|
+
> | null;
|
|
443
|
+
if (!parentForm) return null;
|
|
444
|
+
return String(parentForm.title ?? parentForm.slug ?? "");
|
|
445
|
+
} catch {
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
}
|