@kidecms/core 0.1.10 → 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/admin/forgot-password.astro +60 -0
- package/routes/{pages/admin → admin}/invite.astro +7 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +32 -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/pages/admin/login.astro +0 -57
- /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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { useState, useCallback, useEffect,
|
|
4
|
-
import { GripVertical, ChevronRight, Plus, Trash2 } from "lucide-react";
|
|
3
|
+
import { useState, useCallback, useEffect, useRef } from "react";
|
|
4
|
+
import { ArrowUpRight, GripVertical, ChevronRight, Link2, Plus, Save, Trash2, Unlink } from "lucide-react";
|
|
5
5
|
import { cn } from "../lib/utils";
|
|
6
6
|
import {
|
|
7
7
|
DndContext,
|
|
@@ -15,27 +15,20 @@ import {
|
|
|
15
15
|
import { SortableContext, useSortable, verticalListSortingStrategy, arrayMove } from "@dnd-kit/sortable";
|
|
16
16
|
import { CSS } from "@dnd-kit/utilities";
|
|
17
17
|
import { Button } from "./ui/button";
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
defaultValue?: unknown;
|
|
33
|
-
of?: { type: string };
|
|
34
|
-
collection?: string;
|
|
35
|
-
hasMany?: boolean;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
type BlockTypesMeta = Record<string, Record<string, SubFieldMeta>>;
|
|
18
|
+
import { buttonVariants } from "./ui/button";
|
|
19
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
|
|
20
|
+
import { Command, CommandEmpty, CommandInput, CommandItem, CommandList } from "./ui/command";
|
|
21
|
+
import {
|
|
22
|
+
SubField,
|
|
23
|
+
blankBlockFields,
|
|
24
|
+
generateKey,
|
|
25
|
+
getPreviewText,
|
|
26
|
+
humanize,
|
|
27
|
+
type BlockTypesMeta,
|
|
28
|
+
type RelationOption,
|
|
29
|
+
type SubFieldMeta,
|
|
30
|
+
} from "./block-fields";
|
|
31
|
+
import type { LinkOptionGroup } from "./InternalLinkPicker";
|
|
39
32
|
|
|
40
33
|
type Block = {
|
|
41
34
|
_key: string;
|
|
@@ -43,38 +36,26 @@ type Block = {
|
|
|
43
36
|
[field: string]: unknown;
|
|
44
37
|
};
|
|
45
38
|
|
|
46
|
-
type
|
|
39
|
+
type SharedSectionOption = {
|
|
40
|
+
id: string;
|
|
41
|
+
title: string;
|
|
42
|
+
blockType: string;
|
|
43
|
+
status?: string;
|
|
44
|
+
};
|
|
47
45
|
|
|
48
46
|
type Props = {
|
|
49
47
|
name: string;
|
|
50
48
|
value?: string;
|
|
51
49
|
types: BlockTypesMeta;
|
|
52
50
|
blockRelationOptions?: Record<string, RelationOption[]>;
|
|
51
|
+
linkOptions?: LinkOptionGroup[];
|
|
52
|
+
sharedSections?: SharedSectionOption[];
|
|
53
|
+
sharedEnabled?: boolean;
|
|
53
54
|
};
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
return "blk_" + Math.random().toString(36).slice(2, 9);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function humanize(value: string) {
|
|
60
|
-
return value
|
|
61
|
-
.replace(/^_+/, "")
|
|
62
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
63
|
-
.replace(/[-_]/g, " ")
|
|
64
|
-
.replace(/\s+/g, " ")
|
|
65
|
-
.trim()
|
|
66
|
-
.replace(/^\w/, (char) => char.toUpperCase());
|
|
67
|
-
}
|
|
56
|
+
const SHARED_BLOCK_TYPE = "__shared";
|
|
68
57
|
|
|
69
|
-
|
|
70
|
-
for (const [key, meta] of Object.entries(fieldsMeta)) {
|
|
71
|
-
if (meta.type === "text" && block[key]) {
|
|
72
|
-
const text = String(block[key]);
|
|
73
|
-
return text.length > 60 ? text.slice(0, 60) + "..." : text;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return "";
|
|
77
|
-
}
|
|
58
|
+
const isSharedBlock = (block: Block) => block.type === SHARED_BLOCK_TYPE && typeof block.ref === "string";
|
|
78
59
|
|
|
79
60
|
function parseBlocks(value: string | undefined, types: BlockTypesMeta): Block[] {
|
|
80
61
|
if (!value) return [];
|
|
@@ -107,8 +88,13 @@ function SortableBlock({
|
|
|
107
88
|
onAutoFocused,
|
|
108
89
|
onToggle,
|
|
109
90
|
onRemove,
|
|
91
|
+
onDetach,
|
|
92
|
+
onSaveShared,
|
|
93
|
+
sharedEnabled,
|
|
110
94
|
onUpdateField,
|
|
111
95
|
getRelationOptions,
|
|
96
|
+
linkOptions = [],
|
|
97
|
+
sharedSection,
|
|
112
98
|
}: {
|
|
113
99
|
block: Block;
|
|
114
100
|
fieldsMeta: Record<string, SubFieldMeta>;
|
|
@@ -117,8 +103,13 @@ function SortableBlock({
|
|
|
117
103
|
onAutoFocused?: () => void;
|
|
118
104
|
onToggle: () => void;
|
|
119
105
|
onRemove: () => void;
|
|
106
|
+
onDetach: () => void;
|
|
107
|
+
onSaveShared: () => void;
|
|
108
|
+
sharedEnabled: boolean;
|
|
120
109
|
onUpdateField: (fieldName: string, value: unknown) => void;
|
|
121
110
|
getRelationOptions: (blockType: string, fieldName: string) => RelationOption[];
|
|
111
|
+
linkOptions?: LinkOptionGroup[];
|
|
112
|
+
sharedSection?: SharedSectionOption;
|
|
122
113
|
}) {
|
|
123
114
|
const contentRef = useRef<HTMLDivElement>(null);
|
|
124
115
|
const { attributes, listeners, setNodeRef, setActivatorNodeRef, transform, transition, isDragging } = useSortable({
|
|
@@ -140,17 +131,21 @@ function SortableBlock({
|
|
|
140
131
|
transition,
|
|
141
132
|
};
|
|
142
133
|
|
|
143
|
-
const
|
|
134
|
+
const shared = isSharedBlock(block);
|
|
135
|
+
const preview = shared
|
|
136
|
+
? String(sharedSection?.title ?? block.title ?? "Shared section")
|
|
137
|
+
: getPreviewText(block, fieldsMeta);
|
|
138
|
+
const sharedBlockType = String(sharedSection?.blockType ?? block.blockType ?? "");
|
|
144
139
|
|
|
145
140
|
return (
|
|
146
141
|
<div
|
|
147
142
|
ref={setNodeRef}
|
|
148
143
|
style={style}
|
|
149
|
-
className={cn("overflow-hidden rounded-lg border", isDragging && "z-10 opacity-90 shadow-lg")}
|
|
144
|
+
className={cn("bg-card overflow-hidden rounded-lg border", isDragging && "z-10 opacity-90 shadow-lg")}
|
|
150
145
|
>
|
|
151
146
|
{/* Header — entire row is clickable to expand/collapse */}
|
|
152
147
|
<div
|
|
153
|
-
className="group/row hover:bg-muted/
|
|
148
|
+
className="group/row bg-muted-strong/50 hover:bg-muted-strong/70 flex items-center gap-2 px-3 py-2 transition-colors select-none"
|
|
154
149
|
onClick={onToggle}
|
|
155
150
|
>
|
|
156
151
|
{/* Drag handle */}
|
|
@@ -172,17 +167,40 @@ function SortableBlock({
|
|
|
172
167
|
)}
|
|
173
168
|
/>
|
|
174
169
|
|
|
175
|
-
<span
|
|
176
|
-
{
|
|
170
|
+
<span
|
|
171
|
+
className={cn(
|
|
172
|
+
"rounded px-2 py-0.5 text-xs font-medium",
|
|
173
|
+
shared ? "bg-primary/10 text-primary border-primary/20 border" : "bg-card text-secondary-foreground border",
|
|
174
|
+
)}
|
|
175
|
+
>
|
|
176
|
+
{shared ? "Shared" : humanize(block.type)}
|
|
177
177
|
</span>
|
|
178
178
|
|
|
179
|
+
{shared && sharedBlockType && <span className="text-foreground/70 text-xs">{humanize(sharedBlockType)}</span>}
|
|
180
|
+
|
|
179
181
|
{!isExpanded && preview && (
|
|
180
|
-
<span className="text-
|
|
182
|
+
<span className="text-foreground/70 group-hover/row:text-foreground/85 min-w-0 truncate text-sm transition-colors">
|
|
181
183
|
{preview}
|
|
182
184
|
</span>
|
|
183
185
|
)}
|
|
184
186
|
|
|
185
187
|
<div className="ml-auto flex shrink-0 items-center">
|
|
188
|
+
{(shared || sharedEnabled) && (
|
|
189
|
+
<Button
|
|
190
|
+
type="button"
|
|
191
|
+
variant="ghost"
|
|
192
|
+
size="icon"
|
|
193
|
+
title={shared ? "Detach shared section" : "Save as shared section"}
|
|
194
|
+
className="text-muted-foreground hover:text-foreground size-7"
|
|
195
|
+
onClick={(e) => {
|
|
196
|
+
e.stopPropagation();
|
|
197
|
+
if (shared) onDetach();
|
|
198
|
+
else onSaveShared();
|
|
199
|
+
}}
|
|
200
|
+
>
|
|
201
|
+
{shared ? <Unlink className="size-3.5" /> : <Save className="size-3.5" />}
|
|
202
|
+
</Button>
|
|
203
|
+
)}
|
|
186
204
|
<Button
|
|
187
205
|
type="button"
|
|
188
206
|
variant="ghost"
|
|
@@ -202,17 +220,43 @@ function SortableBlock({
|
|
|
202
220
|
{/* Content */}
|
|
203
221
|
{isExpanded && (
|
|
204
222
|
<div ref={contentRef} className="space-y-4 border-t px-4 py-4">
|
|
205
|
-
{
|
|
206
|
-
<
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
223
|
+
{shared ? (
|
|
224
|
+
<div className="space-y-3">
|
|
225
|
+
<div>
|
|
226
|
+
<p className="font-medium">{preview}</p>
|
|
227
|
+
<p className="text-muted-foreground text-sm">
|
|
228
|
+
Editing the source updates every page that uses this shared section.
|
|
229
|
+
</p>
|
|
230
|
+
</div>
|
|
231
|
+
<div className="flex flex-wrap gap-2">
|
|
232
|
+
<a
|
|
233
|
+
href={`/admin/shared-sections/${block.ref}`}
|
|
234
|
+
target="_blank"
|
|
235
|
+
className={buttonVariants({ variant: "outline", size: "sm" })}
|
|
236
|
+
>
|
|
237
|
+
<ArrowUpRight className="size-3.5" />
|
|
238
|
+
Edit source
|
|
239
|
+
</a>
|
|
240
|
+
<Button type="button" variant="outline" size="sm" onClick={onDetach}>
|
|
241
|
+
<Unlink className="size-3.5" />
|
|
242
|
+
Detach
|
|
243
|
+
</Button>
|
|
244
|
+
</div>
|
|
245
|
+
</div>
|
|
246
|
+
) : (
|
|
247
|
+
Object.entries(fieldsMeta).map(([fieldName, meta]) => (
|
|
248
|
+
<SubField
|
|
249
|
+
key={fieldName}
|
|
250
|
+
blockKey={block._key}
|
|
251
|
+
fieldName={fieldName}
|
|
252
|
+
meta={meta}
|
|
253
|
+
value={block[fieldName]}
|
|
254
|
+
onChange={(v) => onUpdateField(fieldName, v)}
|
|
255
|
+
relationOptions={meta.type === "relation" ? getRelationOptions(block.type, fieldName) : []}
|
|
256
|
+
linkOptions={linkOptions}
|
|
257
|
+
/>
|
|
258
|
+
))
|
|
259
|
+
)}
|
|
216
260
|
</div>
|
|
217
261
|
)}
|
|
218
262
|
</div>
|
|
@@ -223,15 +267,29 @@ function SortableBlock({
|
|
|
223
267
|
// Main editor
|
|
224
268
|
// -----------------------------------------------
|
|
225
269
|
|
|
226
|
-
export default function BlockEditor({
|
|
270
|
+
export default function BlockEditor({
|
|
271
|
+
name,
|
|
272
|
+
value,
|
|
273
|
+
types,
|
|
274
|
+
blockRelationOptions = {},
|
|
275
|
+
linkOptions = [],
|
|
276
|
+
sharedSections = [],
|
|
277
|
+
sharedEnabled = true,
|
|
278
|
+
}: Props) {
|
|
227
279
|
const [blocks, setBlocks] = useState<Block[]>(() => parseBlocks(value, types));
|
|
228
280
|
const [expandedKeys, setExpandedKeys] = useState<Set<string>>(() => new Set());
|
|
229
281
|
const [newBlockKey, setNewBlockKey] = useState<string | null>(null);
|
|
282
|
+
const [sharedOpen, setSharedOpen] = useState(false);
|
|
283
|
+
const [localSharedSections, setLocalSharedSections] = useState<SharedSectionOption[]>(sharedSections);
|
|
230
284
|
const savedExpandedRef = useRef<Set<string> | null>(null);
|
|
231
285
|
const hiddenRef = useRef<HTMLInputElement>(null);
|
|
232
286
|
const previewChannelRef = useRef<BroadcastChannel | null>(null);
|
|
233
287
|
|
|
234
288
|
const typeNames = Object.keys(types);
|
|
289
|
+
// Any shared section can be inserted — it renders and is edited through its own block
|
|
290
|
+
// type, independent of the block types this field declares.
|
|
291
|
+
const insertableSharedSections = sharedEnabled ? localSharedSections : [];
|
|
292
|
+
const sharedSectionsById = new Map(localSharedSections.map((section) => [section.id, section]));
|
|
235
293
|
|
|
236
294
|
const sensors = useSensors(
|
|
237
295
|
useSensor(PointerSensor, { activationConstraint: { distance: 5 } }),
|
|
@@ -287,18 +345,7 @@ export default function BlockEditor({ name, value, types, blockRelationOptions =
|
|
|
287
345
|
const addBlock = useCallback(
|
|
288
346
|
(typeName: string) => {
|
|
289
347
|
const fieldsMeta = types[typeName] ?? {};
|
|
290
|
-
const newBlock: Block = { _key: generateKey(), type: typeName };
|
|
291
|
-
for (const [fieldName, meta] of Object.entries(fieldsMeta)) {
|
|
292
|
-
if (meta.defaultValue !== undefined) {
|
|
293
|
-
newBlock[fieldName] = meta.defaultValue;
|
|
294
|
-
} else if (meta.type === "boolean") {
|
|
295
|
-
newBlock[fieldName] = false;
|
|
296
|
-
} else if (meta.type === "array") {
|
|
297
|
-
newBlock[fieldName] = [];
|
|
298
|
-
} else {
|
|
299
|
-
newBlock[fieldName] = "";
|
|
300
|
-
}
|
|
301
|
-
}
|
|
348
|
+
const newBlock: Block = { _key: generateKey(), type: typeName, ...blankBlockFields(fieldsMeta) };
|
|
302
349
|
setNewBlockKey(newBlock._key);
|
|
303
350
|
updateBlocks((prev) => [...prev, newBlock]);
|
|
304
351
|
setExpandedKeys((prev) => new Set(prev).add(newBlock._key));
|
|
@@ -306,6 +353,24 @@ export default function BlockEditor({ name, value, types, blockRelationOptions =
|
|
|
306
353
|
[types, updateBlocks],
|
|
307
354
|
);
|
|
308
355
|
|
|
356
|
+
const addSharedBlock = useCallback(
|
|
357
|
+
(sectionId: string) => {
|
|
358
|
+
const section = localSharedSections.find((item) => item.id === sectionId);
|
|
359
|
+
if (!section) return;
|
|
360
|
+
const newBlock: Block = {
|
|
361
|
+
_key: generateKey(),
|
|
362
|
+
type: SHARED_BLOCK_TYPE,
|
|
363
|
+
ref: section.id,
|
|
364
|
+
title: section.title,
|
|
365
|
+
blockType: section.blockType,
|
|
366
|
+
};
|
|
367
|
+
setNewBlockKey(newBlock._key);
|
|
368
|
+
updateBlocks((prev) => [...prev, newBlock]);
|
|
369
|
+
setExpandedKeys((prev) => new Set(prev).add(newBlock._key));
|
|
370
|
+
},
|
|
371
|
+
[localSharedSections, updateBlocks],
|
|
372
|
+
);
|
|
373
|
+
|
|
309
374
|
const removeBlock = useCallback(
|
|
310
375
|
(key: string) => {
|
|
311
376
|
updateBlocks((prev) => prev.filter((b) => b._key !== key));
|
|
@@ -320,6 +385,82 @@ export default function BlockEditor({ name, value, types, blockRelationOptions =
|
|
|
320
385
|
[updateBlocks],
|
|
321
386
|
);
|
|
322
387
|
|
|
388
|
+
const detachSharedBlock = useCallback(
|
|
389
|
+
async (key: string) => {
|
|
390
|
+
const block = blocks.find((item) => item._key === key);
|
|
391
|
+
if (!block || !isSharedBlock(block)) return;
|
|
392
|
+
|
|
393
|
+
const response = await fetch(`/api/cms/shared-sections/${block.ref}?status=any`, { credentials: "same-origin" });
|
|
394
|
+
if (!response.ok) {
|
|
395
|
+
window.alert("Could not load the shared section to detach.");
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const shared = (await response.json()) as { block?: Record<string, unknown>; title?: string };
|
|
400
|
+
if (!shared.block || typeof shared.block !== "object") {
|
|
401
|
+
window.alert("The shared section has no block content to detach.");
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
updateBlocks((prev) =>
|
|
406
|
+
prev.map((item) => (item._key === key ? ({ _key: key, ...shared.block } as Block) : item)),
|
|
407
|
+
);
|
|
408
|
+
},
|
|
409
|
+
[blocks, updateBlocks],
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
const saveAsShared = useCallback(
|
|
413
|
+
async (key: string) => {
|
|
414
|
+
if (!sharedEnabled) return;
|
|
415
|
+
const block = blocks.find((item) => item._key === key);
|
|
416
|
+
if (!block || isSharedBlock(block)) return;
|
|
417
|
+
|
|
418
|
+
const title = window.prompt("Name this shared section", getPreviewText(block, types[block.type] ?? {}));
|
|
419
|
+
if (!title) return;
|
|
420
|
+
|
|
421
|
+
const { _key: _ignored, ...blockData } = block;
|
|
422
|
+
const response = await fetch("/api/cms/shared-sections", {
|
|
423
|
+
method: "POST",
|
|
424
|
+
credentials: "same-origin",
|
|
425
|
+
headers: { "Content-Type": "application/json" },
|
|
426
|
+
body: JSON.stringify({
|
|
427
|
+
title,
|
|
428
|
+
blockType: block.type,
|
|
429
|
+
block: blockData,
|
|
430
|
+
_status: "published",
|
|
431
|
+
}),
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
if (!response.ok) {
|
|
435
|
+
window.alert("Could not create the shared section.");
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const created = (await response.json()) as Record<string, unknown>;
|
|
440
|
+
const section: SharedSectionOption = {
|
|
441
|
+
id: String(created._id),
|
|
442
|
+
title: String(created.title ?? title),
|
|
443
|
+
blockType: String(created.blockType ?? block.type),
|
|
444
|
+
status: String(created._status ?? "published"),
|
|
445
|
+
};
|
|
446
|
+
setLocalSharedSections((prev) => [...prev, section]);
|
|
447
|
+
updateBlocks((prev) =>
|
|
448
|
+
prev.map((item) =>
|
|
449
|
+
item._key === key
|
|
450
|
+
? {
|
|
451
|
+
_key: key,
|
|
452
|
+
type: SHARED_BLOCK_TYPE,
|
|
453
|
+
ref: section.id,
|
|
454
|
+
title: section.title,
|
|
455
|
+
blockType: section.blockType,
|
|
456
|
+
}
|
|
457
|
+
: item,
|
|
458
|
+
),
|
|
459
|
+
);
|
|
460
|
+
},
|
|
461
|
+
[blocks, sharedEnabled, types, updateBlocks],
|
|
462
|
+
);
|
|
463
|
+
|
|
323
464
|
const toggleExpanded = useCallback((key: string) => {
|
|
324
465
|
setExpandedKeys((prev) => {
|
|
325
466
|
const next = new Set(prev);
|
|
@@ -350,7 +491,8 @@ export default function BlockEditor({ name, value, types, blockRelationOptions =
|
|
|
350
491
|
<SortableContext items={blocks.map((b) => b._key)} strategy={verticalListSortingStrategy}>
|
|
351
492
|
<div className="space-y-3">
|
|
352
493
|
{blocks.map((block) => {
|
|
353
|
-
const fieldsMeta = types[block.type] ?? {};
|
|
494
|
+
const fieldsMeta = isSharedBlock(block) ? {} : (types[block.type] ?? {});
|
|
495
|
+
const sharedSection = isSharedBlock(block) ? sharedSectionsById.get(String(block.ref)) : undefined;
|
|
354
496
|
return (
|
|
355
497
|
<SortableBlock
|
|
356
498
|
key={block._key}
|
|
@@ -361,10 +503,15 @@ export default function BlockEditor({ name, value, types, blockRelationOptions =
|
|
|
361
503
|
onAutoFocused={() => setNewBlockKey(null)}
|
|
362
504
|
onToggle={() => toggleExpanded(block._key)}
|
|
363
505
|
onRemove={() => removeBlock(block._key)}
|
|
506
|
+
onDetach={() => detachSharedBlock(block._key)}
|
|
507
|
+
onSaveShared={() => saveAsShared(block._key)}
|
|
508
|
+
sharedEnabled={sharedEnabled}
|
|
364
509
|
onUpdateField={(fn, v) => updateField(block._key, fn, v)}
|
|
365
510
|
getRelationOptions={(blockType, fieldName) =>
|
|
366
|
-
blockRelationOptions[`block:${blockType}:${fieldName}`] ?? []
|
|
511
|
+
blockRelationOptions[`block:${name}:${blockType}:${fieldName}`] ?? []
|
|
367
512
|
}
|
|
513
|
+
linkOptions={linkOptions}
|
|
514
|
+
sharedSection={sharedSection}
|
|
368
515
|
/>
|
|
369
516
|
);
|
|
370
517
|
})}
|
|
@@ -387,610 +534,44 @@ export default function BlockEditor({ name, value, types, blockRelationOptions =
|
|
|
387
534
|
{humanize(typeName)}
|
|
388
535
|
</Button>
|
|
389
536
|
))}
|
|
390
|
-
</div>
|
|
391
|
-
</div>
|
|
392
|
-
);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// -----------------------------------------------
|
|
396
|
-
// Sub-field renderer
|
|
397
|
-
// -----------------------------------------------
|
|
398
|
-
|
|
399
|
-
function SubField({
|
|
400
|
-
blockKey,
|
|
401
|
-
fieldName,
|
|
402
|
-
meta,
|
|
403
|
-
value,
|
|
404
|
-
onChange,
|
|
405
|
-
relationOptions = [],
|
|
406
|
-
}: {
|
|
407
|
-
blockKey: string;
|
|
408
|
-
fieldName: string;
|
|
409
|
-
meta: SubFieldMeta;
|
|
410
|
-
value: unknown;
|
|
411
|
-
onChange: (value: unknown) => void;
|
|
412
|
-
relationOptions?: RelationOption[];
|
|
413
|
-
}) {
|
|
414
|
-
const label = meta.label ?? humanize(fieldName);
|
|
415
|
-
const fieldId = `${blockKey}_${fieldName}`;
|
|
416
|
-
|
|
417
|
-
return (
|
|
418
|
-
<div className="grid gap-2">
|
|
419
|
-
<Label htmlFor={fieldId}>
|
|
420
|
-
{label}
|
|
421
|
-
{meta.required ? " *" : ""}
|
|
422
|
-
</Label>
|
|
423
|
-
{meta.admin?.help && <p className="text-muted-foreground text-xs leading-5">{meta.admin.help}</p>}
|
|
424
|
-
<SubFieldControl
|
|
425
|
-
fieldId={fieldId}
|
|
426
|
-
meta={meta}
|
|
427
|
-
value={value}
|
|
428
|
-
onChange={onChange}
|
|
429
|
-
relationOptions={relationOptions}
|
|
430
|
-
/>
|
|
431
|
-
</div>
|
|
432
|
-
);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
function JsonTextarea({
|
|
436
|
-
fieldId,
|
|
437
|
-
rows,
|
|
438
|
-
value,
|
|
439
|
-
fallback,
|
|
440
|
-
onChange,
|
|
441
|
-
}: {
|
|
442
|
-
fieldId: string;
|
|
443
|
-
rows: number;
|
|
444
|
-
value: unknown;
|
|
445
|
-
fallback: unknown;
|
|
446
|
-
onChange: (value: unknown) => void;
|
|
447
|
-
}) {
|
|
448
|
-
const strValue = value == null ? "" : String(value);
|
|
449
|
-
return (
|
|
450
|
-
<Textarea
|
|
451
|
-
id={fieldId}
|
|
452
|
-
rows={rows}
|
|
453
|
-
value={typeof value === "string" ? strValue : JSON.stringify(value ?? fallback, null, 2)}
|
|
454
|
-
onChange={(e) => {
|
|
455
|
-
try {
|
|
456
|
-
onChange(JSON.parse(e.target.value));
|
|
457
|
-
} catch {
|
|
458
|
-
onChange(e.target.value);
|
|
459
|
-
}
|
|
460
|
-
}}
|
|
461
|
-
/>
|
|
462
|
-
);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
function parseSelected(meta: SubFieldMeta, value: unknown): string[] {
|
|
466
|
-
if (!meta.hasMany) return value ? [String(value)] : [];
|
|
467
|
-
if (Array.isArray(value)) return value as string[];
|
|
468
|
-
if (typeof value === "string" && value) {
|
|
469
|
-
try {
|
|
470
|
-
return JSON.parse(value) as string[];
|
|
471
|
-
} catch {
|
|
472
|
-
return [];
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
return [];
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
function RelationControl({
|
|
479
|
-
fieldId,
|
|
480
|
-
meta,
|
|
481
|
-
value,
|
|
482
|
-
onChange,
|
|
483
|
-
relationOptions,
|
|
484
|
-
}: {
|
|
485
|
-
fieldId: string;
|
|
486
|
-
meta: SubFieldMeta;
|
|
487
|
-
value: unknown;
|
|
488
|
-
onChange: (value: unknown) => void;
|
|
489
|
-
relationOptions: RelationOption[];
|
|
490
|
-
}) {
|
|
491
|
-
const selected = parseSelected(meta, value);
|
|
492
|
-
const getLabel = (id: string) => relationOptions?.find((o) => o.value === id)?.label ?? id;
|
|
493
|
-
const toggle = (id: string) => {
|
|
494
|
-
if (meta.hasMany) {
|
|
495
|
-
const next = selected.includes(id) ? selected.filter((v) => v !== id) : [...selected, id];
|
|
496
|
-
onChange(next);
|
|
497
|
-
} else {
|
|
498
|
-
onChange(selected[0] === id ? "" : id);
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
return (
|
|
502
|
-
<div className="space-y-2">
|
|
503
|
-
{meta.hasMany && selected.length > 0 && (
|
|
504
|
-
<div className="flex flex-wrap gap-1.5">
|
|
505
|
-
{selected.map((id) => (
|
|
506
|
-
<span
|
|
507
|
-
key={id}
|
|
508
|
-
className="bg-secondary text-secondary-foreground inline-flex items-center gap-1 rounded-md border px-2 py-0.5 text-sm"
|
|
509
|
-
>
|
|
510
|
-
{getLabel(id)}
|
|
511
|
-
<button
|
|
512
|
-
type="button"
|
|
513
|
-
onClick={() => toggle(id)}
|
|
514
|
-
className="text-muted-foreground hover:text-foreground -mr-0.5 rounded p-0.5"
|
|
515
|
-
>
|
|
516
|
-
<Trash2 className="size-3" />
|
|
517
|
-
</button>
|
|
518
|
-
</span>
|
|
519
|
-
))}
|
|
520
|
-
</div>
|
|
521
|
-
)}
|
|
522
|
-
<SelectField
|
|
523
|
-
name={fieldId}
|
|
524
|
-
value={meta.hasMany ? "" : (selected[0] ?? "")}
|
|
525
|
-
placeholder={`Select ${meta.label?.toLowerCase() ?? "item"}...`}
|
|
526
|
-
items={(relationOptions ?? []).map((o) => ({ value: o.value, label: o.label }))}
|
|
527
|
-
onChange={(v) => {
|
|
528
|
-
if (meta.hasMany && v) {
|
|
529
|
-
if (!selected.includes(v)) onChange([...selected, v]);
|
|
530
|
-
} else {
|
|
531
|
-
onChange(v);
|
|
532
|
-
}
|
|
533
|
-
}}
|
|
534
|
-
/>
|
|
535
|
-
</div>
|
|
536
|
-
);
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
function ArrayControl({
|
|
540
|
-
fieldId,
|
|
541
|
-
meta,
|
|
542
|
-
value,
|
|
543
|
-
onChange,
|
|
544
|
-
}: {
|
|
545
|
-
fieldId: string;
|
|
546
|
-
meta: SubFieldMeta;
|
|
547
|
-
value: unknown;
|
|
548
|
-
onChange: (value: unknown) => void;
|
|
549
|
-
}) {
|
|
550
|
-
if (meta.of?.type === "image") {
|
|
551
|
-
return <ArrayImageField fieldId={fieldId} value={value} onChange={onChange} />;
|
|
552
|
-
}
|
|
553
|
-
if (meta.of?.type === "text") {
|
|
554
|
-
const items = Array.isArray(value) ? value : [];
|
|
555
|
-
return (
|
|
556
|
-
<Input
|
|
557
|
-
id={fieldId}
|
|
558
|
-
value={items.join(", ")}
|
|
559
|
-
placeholder={meta.admin?.placeholder ?? "item1, item2, item3"}
|
|
560
|
-
onChange={(e) => {
|
|
561
|
-
const parts = e.target.value
|
|
562
|
-
.split(",")
|
|
563
|
-
.map((s: string) => s.trim())
|
|
564
|
-
.filter(Boolean);
|
|
565
|
-
onChange(parts);
|
|
566
|
-
}}
|
|
567
|
-
/>
|
|
568
|
-
);
|
|
569
|
-
}
|
|
570
|
-
return (
|
|
571
|
-
<JsonTextarea fieldId={fieldId} rows={meta.admin?.rows ?? 5} value={value} fallback={[]} onChange={onChange} />
|
|
572
|
-
);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
function SubFieldControl({
|
|
576
|
-
fieldId,
|
|
577
|
-
meta,
|
|
578
|
-
value,
|
|
579
|
-
onChange,
|
|
580
|
-
relationOptions = [],
|
|
581
|
-
}: {
|
|
582
|
-
fieldId: string;
|
|
583
|
-
meta: SubFieldMeta;
|
|
584
|
-
value: unknown;
|
|
585
|
-
onChange: (value: unknown) => void;
|
|
586
|
-
relationOptions?: RelationOption[];
|
|
587
|
-
}) {
|
|
588
|
-
const strValue = value == null ? "" : String(value);
|
|
589
|
-
|
|
590
|
-
switch (meta.type) {
|
|
591
|
-
case "text":
|
|
592
|
-
case "email":
|
|
593
|
-
case "date":
|
|
594
|
-
return (
|
|
595
|
-
<Input
|
|
596
|
-
id={fieldId}
|
|
597
|
-
type={meta.type === "email" ? "email" : meta.type === "date" ? "date" : "text"}
|
|
598
|
-
value={strValue}
|
|
599
|
-
placeholder={meta.admin?.placeholder ?? ""}
|
|
600
|
-
required={meta.required}
|
|
601
|
-
onChange={(e) => onChange(e.target.value)}
|
|
602
|
-
/>
|
|
603
|
-
);
|
|
604
|
-
|
|
605
|
-
case "number":
|
|
606
|
-
return (
|
|
607
|
-
<Input
|
|
608
|
-
id={fieldId}
|
|
609
|
-
type="number"
|
|
610
|
-
value={strValue}
|
|
611
|
-
required={meta.required}
|
|
612
|
-
onChange={(e) => onChange(e.target.value === "" ? "" : Number(e.target.value))}
|
|
613
|
-
/>
|
|
614
|
-
);
|
|
615
|
-
|
|
616
|
-
case "boolean":
|
|
617
|
-
return (
|
|
618
|
-
<span className="border-input bg-background inline-flex items-center gap-3 rounded-md border px-3 py-2 text-sm">
|
|
619
|
-
<input
|
|
620
|
-
id={fieldId}
|
|
621
|
-
type="checkbox"
|
|
622
|
-
className="border-input text-primary focus:ring-primary size-4 rounded"
|
|
623
|
-
checked={Boolean(value)}
|
|
624
|
-
onChange={(e) => onChange(e.target.checked)}
|
|
625
|
-
/>
|
|
626
|
-
<span className="text-muted-foreground">{value ? "Enabled" : "Disabled"}</span>
|
|
627
|
-
</span>
|
|
628
|
-
);
|
|
629
|
-
|
|
630
|
-
case "select":
|
|
631
|
-
return (
|
|
632
|
-
<SelectField
|
|
633
|
-
name={fieldId}
|
|
634
|
-
value={strValue}
|
|
635
|
-
placeholder="Select an option"
|
|
636
|
-
items={(meta.options ?? []).map((o) => ({ value: o, label: o }))}
|
|
637
|
-
onChange={(v) => onChange(v)}
|
|
638
|
-
/>
|
|
639
|
-
);
|
|
640
|
-
|
|
641
|
-
case "richText":
|
|
642
|
-
return (
|
|
643
|
-
<RichTextEditor
|
|
644
|
-
name={fieldId}
|
|
645
|
-
initialValue={typeof value === "string" ? strValue : JSON.stringify(value ?? "")}
|
|
646
|
-
rows={meta.admin?.rows ?? 8}
|
|
647
|
-
onChange={(v) => {
|
|
648
|
-
try {
|
|
649
|
-
onChange(JSON.parse(v));
|
|
650
|
-
} catch {
|
|
651
|
-
onChange(v);
|
|
652
|
-
}
|
|
653
|
-
}}
|
|
654
|
-
/>
|
|
655
|
-
);
|
|
656
|
-
|
|
657
|
-
case "image":
|
|
658
|
-
return (
|
|
659
|
-
<ImagePicker
|
|
660
|
-
name={fieldId}
|
|
661
|
-
value={strValue}
|
|
662
|
-
placeholder={meta.admin?.placeholder}
|
|
663
|
-
onChange={(v) => onChange(v)}
|
|
664
|
-
/>
|
|
665
|
-
);
|
|
666
|
-
|
|
667
|
-
case "relation":
|
|
668
|
-
return (
|
|
669
|
-
<RelationControl
|
|
670
|
-
fieldId={fieldId}
|
|
671
|
-
meta={meta}
|
|
672
|
-
value={value}
|
|
673
|
-
onChange={onChange}
|
|
674
|
-
relationOptions={relationOptions}
|
|
675
|
-
/>
|
|
676
|
-
);
|
|
677
|
-
|
|
678
|
-
case "array":
|
|
679
|
-
return <ArrayControl fieldId={fieldId} meta={meta} value={value} onChange={onChange} />;
|
|
680
|
-
|
|
681
|
-
case "json":
|
|
682
|
-
if (meta.admin?.component === "repeater") {
|
|
683
|
-
return <RepeaterField fieldId={fieldId} value={value} onChange={onChange} />;
|
|
684
|
-
}
|
|
685
|
-
return (
|
|
686
|
-
<JsonTextarea fieldId={fieldId} rows={meta.admin?.rows ?? 5} value={value} fallback={{}} onChange={onChange} />
|
|
687
|
-
);
|
|
688
|
-
|
|
689
|
-
default:
|
|
690
|
-
return (
|
|
691
|
-
<JsonTextarea fieldId={fieldId} rows={meta.admin?.rows ?? 5} value={value} fallback="" onChange={onChange} />
|
|
692
|
-
);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
// -----------------------------------------------
|
|
697
|
-
// Array of images sub-component
|
|
698
|
-
// -----------------------------------------------
|
|
699
|
-
|
|
700
|
-
function getRepeaterPreview(item: Record<string, string>, fieldKeys: string[]): string {
|
|
701
|
-
for (const key of fieldKeys) {
|
|
702
|
-
if (item[key]) {
|
|
703
|
-
const text = String(item[key]);
|
|
704
|
-
return text.length > 60 ? text.slice(0, 60) + "..." : text;
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
return "";
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
function SortableRepeaterItem({
|
|
711
|
-
item,
|
|
712
|
-
fieldKeys,
|
|
713
|
-
index,
|
|
714
|
-
isExpanded,
|
|
715
|
-
autoFocus,
|
|
716
|
-
onAutoFocused,
|
|
717
|
-
onToggle,
|
|
718
|
-
onRemove,
|
|
719
|
-
onUpdate,
|
|
720
|
-
}: {
|
|
721
|
-
item: Record<string, string>;
|
|
722
|
-
fieldKeys: string[];
|
|
723
|
-
index: number;
|
|
724
|
-
isExpanded: boolean;
|
|
725
|
-
autoFocus?: boolean;
|
|
726
|
-
onAutoFocused?: () => void;
|
|
727
|
-
onToggle: () => void;
|
|
728
|
-
onRemove: () => void;
|
|
729
|
-
onUpdate: (key: string, val: string) => void;
|
|
730
|
-
}) {
|
|
731
|
-
const contentRef = useRef<HTMLDivElement>(null);
|
|
732
|
-
const { attributes, listeners, setNodeRef, setActivatorNodeRef, transform, transition, isDragging } = useSortable({
|
|
733
|
-
id: item._key,
|
|
734
|
-
});
|
|
735
|
-
|
|
736
|
-
useEffect(() => {
|
|
737
|
-
if (autoFocus && isExpanded && contentRef.current) {
|
|
738
|
-
const input = contentRef.current.querySelector<HTMLElement>("input, textarea");
|
|
739
|
-
if (input) {
|
|
740
|
-
input.focus();
|
|
741
|
-
onAutoFocused?.();
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
}, [autoFocus, isExpanded, onAutoFocused]);
|
|
745
|
-
|
|
746
|
-
const style = {
|
|
747
|
-
transform: CSS.Transform.toString(transform),
|
|
748
|
-
transition,
|
|
749
|
-
};
|
|
750
|
-
|
|
751
|
-
const preview = getRepeaterPreview(item, fieldKeys);
|
|
752
|
-
|
|
753
|
-
return (
|
|
754
|
-
<div
|
|
755
|
-
ref={setNodeRef}
|
|
756
|
-
style={style}
|
|
757
|
-
className={cn("overflow-hidden rounded-lg border", isDragging && "z-10 opacity-90 shadow-lg")}
|
|
758
|
-
>
|
|
759
|
-
<div
|
|
760
|
-
className="group/row hover:bg-muted/80 flex items-center gap-2 px-3 py-2 transition-colors select-none"
|
|
761
|
-
onClick={onToggle}
|
|
762
|
-
>
|
|
763
|
-
<button
|
|
764
|
-
type="button"
|
|
765
|
-
ref={setActivatorNodeRef}
|
|
766
|
-
className="text-muted-foreground/50 hover:text-muted-foreground -ml-1 cursor-grab touch-none rounded p-1 transition-colors active:cursor-grabbing"
|
|
767
|
-
{...attributes}
|
|
768
|
-
{...listeners}
|
|
769
|
-
onClick={(e) => e.stopPropagation()}
|
|
770
|
-
>
|
|
771
|
-
<GripVertical className="size-4" />
|
|
772
|
-
</button>
|
|
773
|
-
|
|
774
|
-
<ChevronRight
|
|
775
|
-
className={cn(
|
|
776
|
-
"text-muted-foreground group-hover/row:text-foreground/70 size-4 shrink-0 transition-[color,transform]",
|
|
777
|
-
isExpanded && "rotate-90",
|
|
778
|
-
)}
|
|
779
|
-
/>
|
|
780
537
|
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
538
|
+
{/* Shared sections — same button shape, distinct colour, always last;
|
|
539
|
+
opens a searchable combobox popover to pick a section. */}
|
|
540
|
+
{insertableSharedSections.length > 0 && (
|
|
541
|
+
<Popover open={sharedOpen} onOpenChange={setSharedOpen}>
|
|
542
|
+
<PopoverTrigger asChild>
|
|
543
|
+
<Button type="button" variant="outline" size="sm" className="text-foreground/70">
|
|
544
|
+
<Link2 className="mr-1 size-3.5" />
|
|
545
|
+
Shared section
|
|
546
|
+
</Button>
|
|
547
|
+
</PopoverTrigger>
|
|
548
|
+
<PopoverContent className="w-80 p-0" align="start">
|
|
549
|
+
<Command>
|
|
550
|
+
<CommandInput placeholder="Search shared sections…" />
|
|
551
|
+
<CommandList>
|
|
552
|
+
<CommandEmpty>No shared sections found.</CommandEmpty>
|
|
553
|
+
{insertableSharedSections.map((section) => (
|
|
554
|
+
<CommandItem
|
|
555
|
+
key={section.id}
|
|
556
|
+
value={`${section.title} ${section.blockType}`}
|
|
557
|
+
onSelect={() => {
|
|
558
|
+
addSharedBlock(section.id);
|
|
559
|
+
setSharedOpen(false);
|
|
560
|
+
}}
|
|
561
|
+
className="cursor-pointer px-3 py-2"
|
|
562
|
+
>
|
|
563
|
+
<span className="flex-1 truncate">{section.title}</span>
|
|
564
|
+
<span className="text-muted-foreground ml-2 shrink-0 text-xs uppercase">
|
|
565
|
+
{humanize(section.blockType)}
|
|
566
|
+
</span>
|
|
567
|
+
</CommandItem>
|
|
568
|
+
))}
|
|
569
|
+
</CommandList>
|
|
570
|
+
</Command>
|
|
571
|
+
</PopoverContent>
|
|
572
|
+
</Popover>
|
|
789
573
|
)}
|
|
790
|
-
|
|
791
|
-
<div className="ml-auto">
|
|
792
|
-
<Button
|
|
793
|
-
type="button"
|
|
794
|
-
variant="ghost"
|
|
795
|
-
size="icon-xs"
|
|
796
|
-
title="Remove item"
|
|
797
|
-
className="text-muted-foreground hover:text-destructive"
|
|
798
|
-
onClick={(e) => {
|
|
799
|
-
e.stopPropagation();
|
|
800
|
-
onRemove();
|
|
801
|
-
}}
|
|
802
|
-
>
|
|
803
|
-
<Trash2 className="size-3.5" />
|
|
804
|
-
</Button>
|
|
805
|
-
</div>
|
|
806
574
|
</div>
|
|
807
|
-
|
|
808
|
-
{isExpanded && (
|
|
809
|
-
<div ref={contentRef} className="space-y-2 border-t px-4 py-3">
|
|
810
|
-
{fieldKeys.map((key) => (
|
|
811
|
-
<div key={key} className="grid gap-1">
|
|
812
|
-
<Label className="text-xs">{humanize(key)}</Label>
|
|
813
|
-
{key.includes("description") ||
|
|
814
|
-
key.includes("body") ||
|
|
815
|
-
key.includes("content") ||
|
|
816
|
-
key.includes("answer") ? (
|
|
817
|
-
<Textarea rows={3} value={item[key] ?? ""} onChange={(e) => onUpdate(key, e.target.value)} />
|
|
818
|
-
) : (
|
|
819
|
-
<Input value={item[key] ?? ""} onChange={(e) => onUpdate(key, e.target.value)} />
|
|
820
|
-
)}
|
|
821
|
-
</div>
|
|
822
|
-
))}
|
|
823
|
-
</div>
|
|
824
|
-
)}
|
|
825
|
-
</div>
|
|
826
|
-
);
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
function parseArray(value: unknown): unknown[] {
|
|
830
|
-
if (Array.isArray(value)) return value;
|
|
831
|
-
if (typeof value === "string") {
|
|
832
|
-
try {
|
|
833
|
-
const p = JSON.parse(value);
|
|
834
|
-
if (Array.isArray(p)) return p;
|
|
835
|
-
} catch {}
|
|
836
|
-
}
|
|
837
|
-
return [];
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
function RepeaterField({ value, onChange }: { fieldId: string; value: unknown; onChange: (value: unknown) => void }) {
|
|
841
|
-
const [newItemKey, setNewItemKey] = useState<string | null>(null);
|
|
842
|
-
const [expandedKeys, setExpandedKeys] = useState<Set<string>>(() => new Set());
|
|
843
|
-
const savedExpandedRef = useRef<Set<string> | null>(null);
|
|
844
|
-
const [items, setItems] = useState<Array<Record<string, string>>>(() => {
|
|
845
|
-
const raw = parseArray(value);
|
|
846
|
-
return raw.map((item: unknown) => {
|
|
847
|
-
const obj = item as Record<string, string>;
|
|
848
|
-
return obj._key ? obj : { ...obj, _key: generateKey() };
|
|
849
|
-
});
|
|
850
|
-
});
|
|
851
|
-
|
|
852
|
-
useEffect(() => {
|
|
853
|
-
onChange(items);
|
|
854
|
-
}, [items]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
855
|
-
|
|
856
|
-
// Detect fields from first item or default to title + description
|
|
857
|
-
const fieldKeys = items.length > 0 ? Object.keys(items[0]).filter((k) => k !== "_key") : ["title", "description"];
|
|
858
|
-
|
|
859
|
-
const sensors = useSensors(
|
|
860
|
-
useSensor(PointerSensor, { activationConstraint: { distance: 5 } }),
|
|
861
|
-
useSensor(KeyboardSensor),
|
|
862
|
-
);
|
|
863
|
-
|
|
864
|
-
const addItem = () => {
|
|
865
|
-
const key = generateKey();
|
|
866
|
-
const blank: Record<string, string> = { _key: key };
|
|
867
|
-
for (const k of fieldKeys) blank[k] = "";
|
|
868
|
-
setNewItemKey(key);
|
|
869
|
-
setItems((prev) => [...prev, blank]);
|
|
870
|
-
setExpandedKeys((prev) => new Set(prev).add(key));
|
|
871
|
-
};
|
|
872
|
-
|
|
873
|
-
const removeItem = (index: number) => {
|
|
874
|
-
setItems((prev) => prev.filter((_, i) => i !== index));
|
|
875
|
-
};
|
|
876
|
-
|
|
877
|
-
const updateItem = (index: number, key: string, val: string) => {
|
|
878
|
-
setItems((prev) => prev.map((item, i) => (i === index ? { ...item, [key]: val } : item)));
|
|
879
|
-
};
|
|
880
|
-
|
|
881
|
-
const toggleExpanded = (key: string) => {
|
|
882
|
-
setExpandedKeys((prev) => {
|
|
883
|
-
const next = new Set(prev);
|
|
884
|
-
if (next.has(key)) next.delete(key);
|
|
885
|
-
else next.add(key);
|
|
886
|
-
return next;
|
|
887
|
-
});
|
|
888
|
-
};
|
|
889
|
-
|
|
890
|
-
const handleDragStart = () => {
|
|
891
|
-
savedExpandedRef.current = new Set(expandedKeys);
|
|
892
|
-
setExpandedKeys(new Set());
|
|
893
|
-
};
|
|
894
|
-
|
|
895
|
-
const handleDragEnd = (event: DragEndEvent) => {
|
|
896
|
-
if (savedExpandedRef.current) {
|
|
897
|
-
setExpandedKeys(savedExpandedRef.current);
|
|
898
|
-
savedExpandedRef.current = null;
|
|
899
|
-
}
|
|
900
|
-
const { active, over } = event;
|
|
901
|
-
if (!over || active.id === over.id) return;
|
|
902
|
-
setItems((prev) => {
|
|
903
|
-
const oldIndex = prev.findIndex((item) => item._key === active.id);
|
|
904
|
-
const newIndex = prev.findIndex((item) => item._key === over.id);
|
|
905
|
-
if (oldIndex === -1 || newIndex === -1) return prev;
|
|
906
|
-
return arrayMove(prev, oldIndex, newIndex);
|
|
907
|
-
});
|
|
908
|
-
};
|
|
909
|
-
|
|
910
|
-
return (
|
|
911
|
-
<div className="space-y-3">
|
|
912
|
-
<DndContext
|
|
913
|
-
sensors={sensors}
|
|
914
|
-
collisionDetection={closestCenter}
|
|
915
|
-
onDragStart={handleDragStart}
|
|
916
|
-
onDragEnd={handleDragEnd}
|
|
917
|
-
>
|
|
918
|
-
<SortableContext items={items.map((item) => item._key)} strategy={verticalListSortingStrategy}>
|
|
919
|
-
{items.map((item, index) => (
|
|
920
|
-
<SortableRepeaterItem
|
|
921
|
-
key={item._key ?? index}
|
|
922
|
-
item={item}
|
|
923
|
-
fieldKeys={fieldKeys}
|
|
924
|
-
index={index}
|
|
925
|
-
isExpanded={expandedKeys.has(item._key)}
|
|
926
|
-
autoFocus={newItemKey === item._key}
|
|
927
|
-
onAutoFocused={() => setNewItemKey(null)}
|
|
928
|
-
onToggle={() => toggleExpanded(item._key)}
|
|
929
|
-
onRemove={() => removeItem(index)}
|
|
930
|
-
onUpdate={(key, val) => updateItem(index, key, val)}
|
|
931
|
-
/>
|
|
932
|
-
))}
|
|
933
|
-
</SortableContext>
|
|
934
|
-
</DndContext>
|
|
935
|
-
<Button type="button" variant="outline" size="sm" className="text-foreground/70" onClick={addItem}>
|
|
936
|
-
<Plus className="size-3.5" />
|
|
937
|
-
Add item
|
|
938
|
-
</Button>
|
|
939
|
-
</div>
|
|
940
|
-
);
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
function ArrayImageField({
|
|
944
|
-
fieldId,
|
|
945
|
-
value,
|
|
946
|
-
onChange,
|
|
947
|
-
}: {
|
|
948
|
-
fieldId: string;
|
|
949
|
-
value: unknown;
|
|
950
|
-
onChange: (value: unknown) => void;
|
|
951
|
-
}) {
|
|
952
|
-
const items: string[] = useMemo(() => (Array.isArray(value) ? value.map(String) : []), [value]);
|
|
953
|
-
|
|
954
|
-
const addImage = useCallback(() => {
|
|
955
|
-
onChange([...items, ""]);
|
|
956
|
-
}, [items, onChange]);
|
|
957
|
-
|
|
958
|
-
const removeImage = useCallback(
|
|
959
|
-
(index: number) => {
|
|
960
|
-
onChange(items.filter((_, i) => i !== index));
|
|
961
|
-
},
|
|
962
|
-
[items, onChange],
|
|
963
|
-
);
|
|
964
|
-
|
|
965
|
-
const updateImage = useCallback(
|
|
966
|
-
(index: number, v: string) => {
|
|
967
|
-
const next = [...items];
|
|
968
|
-
next[index] = v;
|
|
969
|
-
onChange(next);
|
|
970
|
-
},
|
|
971
|
-
[items, onChange],
|
|
972
|
-
);
|
|
973
|
-
|
|
974
|
-
return (
|
|
975
|
-
<div className="space-y-3">
|
|
976
|
-
{items.map((img, i) => (
|
|
977
|
-
<div key={`${fieldId}_img_${i}`} className="relative">
|
|
978
|
-
<ImagePicker name={`${fieldId}_img_${i}`} value={img} onChange={(v) => updateImage(i, v)} />
|
|
979
|
-
<Button
|
|
980
|
-
type="button"
|
|
981
|
-
variant="ghost"
|
|
982
|
-
size="icon"
|
|
983
|
-
className="text-muted-foreground hover:text-destructive absolute top-0 right-0 size-7"
|
|
984
|
-
onClick={() => removeImage(i)}
|
|
985
|
-
>
|
|
986
|
-
<Trash2 className="size-3.5" />
|
|
987
|
-
</Button>
|
|
988
|
-
</div>
|
|
989
|
-
))}
|
|
990
|
-
<Button type="button" variant="outline" size="sm" onClick={addImage}>
|
|
991
|
-
<Plus className="mr-1 size-3.5" />
|
|
992
|
-
Add Image
|
|
993
|
-
</Button>
|
|
994
575
|
</div>
|
|
995
576
|
);
|
|
996
577
|
}
|