@kidecms/core 0.19.1 → 0.21.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/admin/components/AssetsGrid.tsx +1 -1
- package/admin/components/BlockEditor.tsx +3 -12
- package/admin/components/CollaborationReviewBar.tsx +245 -61
- package/admin/components/CommandPalette.tsx +19 -72
- package/admin/components/ContentEditor.tsx +4 -10
- package/admin/components/DocumentCombobox.tsx +171 -0
- package/admin/components/FieldControl.astro +16 -32
- package/admin/components/InternalLinkPicker.tsx +30 -62
- package/admin/components/LinkField.tsx +6 -5
- package/admin/components/RelationField.tsx +32 -58
- package/admin/components/RepeaterInput.tsx +3 -6
- package/admin/components/SharedSectionBlockField.tsx +2 -6
- package/admin/components/TreeItemsEditor.tsx +4 -4
- package/admin/components/TreeSelect.tsx +33 -19
- package/admin/components/block-fields.tsx +21 -50
- package/admin/components/content-block-spec.ts +5 -6
- package/admin/layouts/AdminLayout.astro +41 -2
- package/admin/lib/collaboration.ts +91 -0
- package/admin/lib/edit-data.ts +84 -129
- package/admin/lib/use-document-search.ts +107 -0
- package/admin/views/EditView.astro +19 -28
- package/admin/views/ListView.astro +3 -1
- package/admin/views/NeedsYouView.astro +100 -0
- package/admin/views/RecentView.astro +32 -9
- package/admin/views/edit/EditScripts.astro +90 -15
- package/core/admin.ts +2 -0
- package/core/api.ts +31 -2
- package/core/collaboration.ts +73 -0
- package/core/generator.ts +25 -2
- package/core/index.ts +8 -2
- package/middleware/auth.ts +7 -1
- package/package.json +1 -1
- package/routes/admin/[...path].astro +19 -6
- package/routes/admin/setup.astro +3 -2
- package/routes/api/[collection]/[...path].ts +12 -5
- package/routes/api/admin/search.ts +102 -45
- package/routes/api/auth/setup.ts +7 -5
- package/routes/api/collaboration/index.ts +15 -0
- package/admin/components/Untitled +0 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Check, ChevronsUpDown } from "lucide-react";
|
|
5
|
+
import { Button } from "./ui/button";
|
|
6
|
+
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "./ui/command";
|
|
7
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
|
|
8
|
+
import { cn } from "../lib/utils";
|
|
9
|
+
import { MIN_QUERY_LENGTH, fetchDocuments, useDocumentSearch, type DocumentHit } from "../lib/use-document-search";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The one document-picking combobox: trigger button, popover, server-backed
|
|
13
|
+
* search with recent documents while the query is empty. Every relation-style
|
|
14
|
+
* picker in the admin (top-level relation fields, relation sub-fields inside
|
|
15
|
+
* blocks, internal-link pickers) renders this and differs only in how it
|
|
16
|
+
* stores the pick.
|
|
17
|
+
*/
|
|
18
|
+
export default function DocumentCombobox({
|
|
19
|
+
collections,
|
|
20
|
+
groups,
|
|
21
|
+
placeholder,
|
|
22
|
+
display,
|
|
23
|
+
isSelected,
|
|
24
|
+
onPick,
|
|
25
|
+
closeOnPick = false,
|
|
26
|
+
dimUnselected = false,
|
|
27
|
+
limit = 20,
|
|
28
|
+
size,
|
|
29
|
+
triggerId,
|
|
30
|
+
triggerClassName,
|
|
31
|
+
}: {
|
|
32
|
+
/** Collection slugs to search. */
|
|
33
|
+
collections: string[];
|
|
34
|
+
/** When set, results render grouped by collection under these labels. */
|
|
35
|
+
groups?: Array<{ collection: string; label: string }>;
|
|
36
|
+
placeholder: string;
|
|
37
|
+
/** Trigger text; empty renders the placeholder muted. */
|
|
38
|
+
display: string;
|
|
39
|
+
isSelected: (hit: DocumentHit) => boolean;
|
|
40
|
+
onPick: (hit: DocumentHit) => void;
|
|
41
|
+
/** Close the popover after a pick (single-select behavior). */
|
|
42
|
+
closeOnPick?: boolean;
|
|
43
|
+
/** Dim unselected rows, e.g. when a hasMany field is at maxItems. */
|
|
44
|
+
dimUnselected?: boolean;
|
|
45
|
+
limit?: number;
|
|
46
|
+
size?: React.ComponentProps<typeof Button>["size"];
|
|
47
|
+
triggerId?: string;
|
|
48
|
+
triggerClassName?: string;
|
|
49
|
+
}) {
|
|
50
|
+
const [open, setOpen] = React.useState(false);
|
|
51
|
+
const { query, setQuery, results, loading, tooShort, reset } = useDocumentSearch({
|
|
52
|
+
collections,
|
|
53
|
+
limit,
|
|
54
|
+
recentWhenEmpty: true,
|
|
55
|
+
enabled: open,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const handleOpenChange = (next: boolean) => {
|
|
59
|
+
setOpen(next);
|
|
60
|
+
if (!next) reset();
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const pick = (hit: DocumentHit) => {
|
|
64
|
+
onPick(hit);
|
|
65
|
+
if (closeOnPick) handleOpenChange(false);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const grouped = React.useMemo(() => {
|
|
69
|
+
if (!groups) return null;
|
|
70
|
+
const byCollection = new Map<string, DocumentHit[]>();
|
|
71
|
+
for (const hit of results) byCollection.set(hit.collection, [...(byCollection.get(hit.collection) ?? []), hit]);
|
|
72
|
+
return groups
|
|
73
|
+
.filter((group) => byCollection.has(group.collection))
|
|
74
|
+
.map((group) => ({ ...group, items: byCollection.get(group.collection)! }));
|
|
75
|
+
}, [groups, results]);
|
|
76
|
+
|
|
77
|
+
const renderHit = (hit: DocumentHit) => (
|
|
78
|
+
<CommandItem
|
|
79
|
+
key={hit.docId}
|
|
80
|
+
value={`${hit.collection}:${hit.docId}`}
|
|
81
|
+
onSelect={() => pick(hit)}
|
|
82
|
+
className={cn(dimUnselected && !isSelected(hit) && "opacity-40")}
|
|
83
|
+
>
|
|
84
|
+
<Check className={cn("size-4", isSelected(hit) ? "opacity-100" : "opacity-0")} />
|
|
85
|
+
<span className="flex-1 truncate">{hit.title}</span>
|
|
86
|
+
{hit.status && hit.status !== "published" && (
|
|
87
|
+
<span className="bg-muted text-muted-foreground rounded px-1.5 py-0.5 text-[10px] tracking-wide uppercase">
|
|
88
|
+
{hit.status}
|
|
89
|
+
</span>
|
|
90
|
+
)}
|
|
91
|
+
</CommandItem>
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<Popover open={open} onOpenChange={handleOpenChange}>
|
|
96
|
+
<PopoverTrigger asChild>
|
|
97
|
+
<Button
|
|
98
|
+
id={triggerId}
|
|
99
|
+
variant="input"
|
|
100
|
+
role="combobox"
|
|
101
|
+
aria-expanded={open}
|
|
102
|
+
size={size}
|
|
103
|
+
className={cn("w-full justify-between text-sm font-normal", triggerClassName)}
|
|
104
|
+
>
|
|
105
|
+
<span className={cn("truncate", !display && "text-muted-foreground")}>{display || placeholder}</span>
|
|
106
|
+
<ChevronsUpDown className="ml-2 size-4 shrink-0 opacity-50" />
|
|
107
|
+
</Button>
|
|
108
|
+
</PopoverTrigger>
|
|
109
|
+
<PopoverContent className="w-(--radix-popover-trigger-width) p-0" align="start">
|
|
110
|
+
<Command shouldFilter={false}>
|
|
111
|
+
<CommandInput value={query} onValueChange={setQuery} placeholder={placeholder} />
|
|
112
|
+
<CommandList>
|
|
113
|
+
{tooShort && (
|
|
114
|
+
<div className="text-muted-foreground py-6 text-center text-sm">
|
|
115
|
+
Type at least {MIN_QUERY_LENGTH} characters to search.
|
|
116
|
+
</div>
|
|
117
|
+
)}
|
|
118
|
+
{loading && results.length === 0 && (
|
|
119
|
+
<div className="text-muted-foreground py-6 text-center text-sm">Searching…</div>
|
|
120
|
+
)}
|
|
121
|
+
{!loading && !tooShort && results.length === 0 && <CommandEmpty>No documents found.</CommandEmpty>}
|
|
122
|
+
{grouped
|
|
123
|
+
? grouped.map((group) => (
|
|
124
|
+
<CommandGroup key={group.collection} heading={group.label}>
|
|
125
|
+
{group.items.map(renderHit)}
|
|
126
|
+
</CommandGroup>
|
|
127
|
+
))
|
|
128
|
+
: results.map(renderHit)}
|
|
129
|
+
</CommandList>
|
|
130
|
+
</Command>
|
|
131
|
+
</PopoverContent>
|
|
132
|
+
</Popover>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* id → label bookkeeping for relation pickers: seeded from the server-rendered
|
|
138
|
+
* page when possible, resolved through the search API once on mount for
|
|
139
|
+
* anything missing (e.g. values inside blocks), and extended as hits are
|
|
140
|
+
* picked.
|
|
141
|
+
*/
|
|
142
|
+
export function useRelationLabels(
|
|
143
|
+
collectionSlug: string,
|
|
144
|
+
initialSelected: string[],
|
|
145
|
+
seed: Array<{ value: string; label: string }> = [],
|
|
146
|
+
) {
|
|
147
|
+
const [labels, setLabels] = React.useState<Record<string, string>>(() =>
|
|
148
|
+
Object.fromEntries(seed.map((option) => [option.value, option.label])),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const remember = React.useCallback((hits: Array<{ docId: string; title: string }>) => {
|
|
152
|
+
if (hits.length === 0) return;
|
|
153
|
+
setLabels((prev) => ({ ...prev, ...Object.fromEntries(hits.map((hit) => [hit.docId, hit.title])) }));
|
|
154
|
+
}, []);
|
|
155
|
+
|
|
156
|
+
// Only the initial selection needs resolving; later picks arrive with their label.
|
|
157
|
+
React.useEffect(() => {
|
|
158
|
+
const missing = initialSelected.filter((id) => !(id in labels));
|
|
159
|
+
if (missing.length === 0 || !collectionSlug) return;
|
|
160
|
+
const controller = new AbortController();
|
|
161
|
+
fetchDocuments({ collections: [collectionSlug], ids: missing }, controller.signal)
|
|
162
|
+
.then(remember)
|
|
163
|
+
.catch(() => {});
|
|
164
|
+
return () => controller.abort();
|
|
165
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
166
|
+
}, []);
|
|
167
|
+
|
|
168
|
+
const getLabel = React.useCallback((id: string): string | undefined => labels[id], [labels]);
|
|
169
|
+
|
|
170
|
+
return { getLabel, remember };
|
|
171
|
+
}
|
|
@@ -35,11 +35,7 @@ type RelationMeta = {
|
|
|
35
35
|
labelField?: string;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
type
|
|
39
|
-
collection: string;
|
|
40
|
-
label: string;
|
|
41
|
-
items: Array<{ id: string; label: string; href: string }>;
|
|
42
|
-
};
|
|
38
|
+
type LinkableCollection = { collection: string; label: string };
|
|
43
39
|
|
|
44
40
|
type SharedSectionOption = {
|
|
45
41
|
id: string;
|
|
@@ -55,10 +51,8 @@ const {
|
|
|
55
51
|
relationOptions = [],
|
|
56
52
|
relationMeta,
|
|
57
53
|
readOnly = false,
|
|
58
|
-
|
|
59
|
-
blockRelationOptions = {},
|
|
54
|
+
linkableCollections = [],
|
|
60
55
|
sharedBlockTypes = {},
|
|
61
|
-
sharedBlockRelationOptions = {},
|
|
62
56
|
sharedSectionOptions = [],
|
|
63
57
|
} = Astro.props as {
|
|
64
58
|
name: string;
|
|
@@ -67,10 +61,8 @@ const {
|
|
|
67
61
|
relationOptions?: RelationOption[];
|
|
68
62
|
relationMeta?: RelationMeta;
|
|
69
63
|
readOnly?: boolean;
|
|
70
|
-
|
|
71
|
-
blockRelationOptions?: Record<string, RelationOption[]>;
|
|
64
|
+
linkableCollections?: LinkableCollection[];
|
|
72
65
|
sharedBlockTypes?: Record<string, Record<string, FieldConfig>>;
|
|
73
|
-
sharedBlockRelationOptions?: Record<string, RelationOption[]>;
|
|
74
66
|
sharedSectionOptions?: SharedSectionOption[];
|
|
75
67
|
};
|
|
76
68
|
|
|
@@ -305,14 +297,14 @@ const sharedBlockTypesMeta = toBlockTypesMeta(sharedBlockTypes);
|
|
|
305
297
|
/>
|
|
306
298
|
)}
|
|
307
299
|
|
|
300
|
+
{/* No name attribute — the stored value must never be submitted (or clobbered) from here. */}
|
|
308
301
|
{field.type === "relation" && (readOnly || !relationMeta) && (
|
|
309
|
-
<
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
items={relationOptions}
|
|
302
|
+
<Input
|
|
303
|
+
className={controlClass}
|
|
304
|
+
type="text"
|
|
305
|
+
id={name}
|
|
306
|
+
defaultValue={relationOptions.length > 0 ? relationOptions.map((o) => o.label).join(", ") : serializedValue}
|
|
307
|
+
readOnly
|
|
316
308
|
/>
|
|
317
309
|
)}
|
|
318
310
|
|
|
@@ -339,8 +331,7 @@ const sharedBlockTypesMeta = toBlockTypesMeta(sharedBlockTypes);
|
|
|
339
331
|
initialValue={value ? JSON.stringify(value) : ""}
|
|
340
332
|
rows={rows}
|
|
341
333
|
types={blockTypesMeta}
|
|
342
|
-
|
|
343
|
-
linkOptions={menuLinkOptions}
|
|
334
|
+
linkOptions={linkableCollections}
|
|
344
335
|
sharedSections={sharedSectionOptions}
|
|
345
336
|
fullscreen={field.fullscreen !== false}
|
|
346
337
|
/>
|
|
@@ -364,7 +355,7 @@ const sharedBlockTypesMeta = toBlockTypesMeta(sharedBlockTypes);
|
|
|
364
355
|
value={serializedValue}
|
|
365
356
|
variant="menu"
|
|
366
357
|
label={label}
|
|
367
|
-
linkOptions={
|
|
358
|
+
linkOptions={linkableCollections}
|
|
368
359
|
/>
|
|
369
360
|
)}
|
|
370
361
|
|
|
@@ -373,17 +364,11 @@ const sharedBlockTypesMeta = toBlockTypesMeta(sharedBlockTypes);
|
|
|
373
364
|
)}
|
|
374
365
|
|
|
375
366
|
{field.type === "json" && field.admin?.component === "shared-section-block" && !readOnly && (
|
|
376
|
-
<SharedSectionBlockField
|
|
377
|
-
client:load
|
|
378
|
-
name={name}
|
|
379
|
-
value={serializedValue}
|
|
380
|
-
types={sharedBlockTypesMeta}
|
|
381
|
-
blockRelationOptions={sharedBlockRelationOptions}
|
|
382
|
-
/>
|
|
367
|
+
<SharedSectionBlockField client:load name={name} value={serializedValue} types={sharedBlockTypesMeta} />
|
|
383
368
|
)}
|
|
384
369
|
|
|
385
370
|
{field.type === "json" && field.admin?.component === "link" && !readOnly && (
|
|
386
|
-
<LinkField client:load name={name} value={serializedValue} linkOptions={
|
|
371
|
+
<LinkField client:load name={name} value={serializedValue} linkOptions={linkableCollections} />
|
|
387
372
|
)}
|
|
388
373
|
|
|
389
374
|
{field.type === "json" && field.admin?.component === "repeater" && !readOnly && (
|
|
@@ -398,7 +383,7 @@ const sharedBlockTypesMeta = toBlockTypesMeta(sharedBlockTypes);
|
|
|
398
383
|
)
|
|
399
384
|
: undefined
|
|
400
385
|
}
|
|
401
|
-
linkOptions={
|
|
386
|
+
linkOptions={linkableCollections}
|
|
402
387
|
/>
|
|
403
388
|
)}
|
|
404
389
|
|
|
@@ -424,8 +409,7 @@ const sharedBlockTypesMeta = toBlockTypesMeta(sharedBlockTypes);
|
|
|
424
409
|
name={name}
|
|
425
410
|
value={serializedValue}
|
|
426
411
|
types={blockTypesMeta}
|
|
427
|
-
|
|
428
|
-
linkOptions={menuLinkOptions}
|
|
412
|
+
linkOptions={linkableCollections}
|
|
429
413
|
sharedSections={"shared" in field && field.shared === false ? [] : sharedSectionOptions}
|
|
430
414
|
sharedEnabled={!("shared" in field && field.shared === false)}
|
|
431
415
|
/>
|
|
@@ -1,84 +1,52 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
|
-
import { Check, ChevronsUpDown } from "lucide-react";
|
|
5
|
-
import { Button } from "./ui/button";
|
|
6
|
-
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "./ui/command";
|
|
7
|
-
import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
|
|
8
4
|
import { cn } from "../lib/utils";
|
|
5
|
+
import DocumentCombobox from "./DocumentCombobox";
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
label: string;
|
|
13
|
-
items: Array<{ id: string; label: string; href: string }>;
|
|
14
|
-
};
|
|
7
|
+
/** A collection whose documents can be the target of an internal link. */
|
|
8
|
+
export type LinkableCollection = { collection: string; label: string };
|
|
15
9
|
|
|
10
|
+
export type LinkTarget = { id: string; label: string; href: string };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Picks an internal document by searching the linkable collections on the
|
|
14
|
+
* server. The stored value is the document's public route (plus its title),
|
|
15
|
+
* so selection is matched by href rather than id.
|
|
16
|
+
*/
|
|
16
17
|
export default function InternalLinkPicker({
|
|
17
18
|
editHref,
|
|
18
|
-
|
|
19
|
+
editTitle,
|
|
20
|
+
collections,
|
|
19
21
|
onSelect,
|
|
20
22
|
className,
|
|
21
23
|
triggerClassName,
|
|
22
24
|
}: {
|
|
23
25
|
editHref: string;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
/** Title stored with the link — shown instead of the raw path. */
|
|
27
|
+
editTitle?: string;
|
|
28
|
+
collections: LinkableCollection[];
|
|
29
|
+
onSelect: (item: LinkTarget) => void;
|
|
26
30
|
className?: string;
|
|
27
31
|
triggerClassName?: string;
|
|
28
32
|
}) {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
const selectedLabel = React.useMemo(() => {
|
|
32
|
-
if (!editHref) return "";
|
|
33
|
-
for (const group of linkOptions) {
|
|
34
|
-
const found = group.items.find((item) => item.href === editHref);
|
|
35
|
-
if (found) return found.label;
|
|
36
|
-
}
|
|
37
|
-
return editHref;
|
|
38
|
-
}, [editHref, linkOptions]);
|
|
33
|
+
const slugs = React.useMemo(() => collections.map((c) => c.collection), [collections]);
|
|
39
34
|
|
|
40
35
|
return (
|
|
41
36
|
<div className={cn("min-w-0 flex-1", className)}>
|
|
42
|
-
<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
</PopoverTrigger>
|
|
56
|
-
<PopoverContent className="w-(--radix-popover-trigger-width) p-0" align="start">
|
|
57
|
-
<Command>
|
|
58
|
-
<CommandInput placeholder="Search documents..." />
|
|
59
|
-
<CommandList>
|
|
60
|
-
<CommandEmpty>No documents found.</CommandEmpty>
|
|
61
|
-
{linkOptions.map((group) => (
|
|
62
|
-
<CommandGroup key={group.collection} heading={group.label}>
|
|
63
|
-
{group.items.map((item) => (
|
|
64
|
-
<CommandItem
|
|
65
|
-
key={item.id}
|
|
66
|
-
value={`${item.label} ${item.href}`}
|
|
67
|
-
onSelect={() => {
|
|
68
|
-
onSelect(item);
|
|
69
|
-
setOpen(false);
|
|
70
|
-
}}
|
|
71
|
-
>
|
|
72
|
-
<Check className={cn("size-4", editHref === item.href ? "opacity-100" : "opacity-0")} />
|
|
73
|
-
{item.label}
|
|
74
|
-
</CommandItem>
|
|
75
|
-
))}
|
|
76
|
-
</CommandGroup>
|
|
77
|
-
))}
|
|
78
|
-
</CommandList>
|
|
79
|
-
</Command>
|
|
80
|
-
</PopoverContent>
|
|
81
|
-
</Popover>
|
|
37
|
+
<DocumentCombobox
|
|
38
|
+
collections={slugs}
|
|
39
|
+
groups={collections}
|
|
40
|
+
limit={10}
|
|
41
|
+
placeholder="Search documents..."
|
|
42
|
+
display={editTitle || editHref}
|
|
43
|
+
isSelected={(hit) => !!editHref && hit.href === editHref}
|
|
44
|
+
onPick={(hit) => {
|
|
45
|
+
if (hit.href) onSelect({ id: hit.docId, label: hit.title, href: hit.href });
|
|
46
|
+
}}
|
|
47
|
+
closeOnPick
|
|
48
|
+
triggerClassName={cn("h-9 min-w-0 rounded-lg px-3", triggerClassName)}
|
|
49
|
+
/>
|
|
82
50
|
</div>
|
|
83
51
|
);
|
|
84
52
|
}
|
|
@@ -5,12 +5,12 @@ import { Checkbox } from "./ui/checkbox";
|
|
|
5
5
|
import { Input } from "./ui/input";
|
|
6
6
|
import { Label } from "./ui/label";
|
|
7
7
|
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
|
|
8
|
-
import InternalLinkPicker, { type
|
|
8
|
+
import InternalLinkPicker, { type LinkableCollection } from "./InternalLinkPicker";
|
|
9
9
|
|
|
10
10
|
// A structured link control: URL + label + open-in-new-tab, stored as
|
|
11
11
|
// { type, url, label, title, newTab }. A leading "/" is treated as an internal
|
|
12
|
-
// link. When
|
|
13
|
-
// picker instead of a hand-typed path (which silently breaks on slug edits).
|
|
12
|
+
// link. When linkable collections are provided, internal links are chosen with a
|
|
13
|
+
// document picker instead of a hand-typed path (which silently breaks on slug edits).
|
|
14
14
|
// `title` is the picked document's title — renderers use it as the link text
|
|
15
15
|
// when `label` is left empty.
|
|
16
16
|
type LinkValue = { type?: string; url?: string; label?: string; title?: string; newTab?: boolean };
|
|
@@ -19,7 +19,7 @@ type Props = {
|
|
|
19
19
|
name?: string;
|
|
20
20
|
value?: string | LinkValue;
|
|
21
21
|
onChange?: (value: LinkValue) => void;
|
|
22
|
-
linkOptions?:
|
|
22
|
+
linkOptions?: LinkableCollection[];
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
function parse(v: unknown): LinkValue {
|
|
@@ -92,7 +92,8 @@ export default function LinkField({ name, value: initial, onChange, linkOptions
|
|
|
92
92
|
{linkType === "internal" ? (
|
|
93
93
|
<InternalLinkPicker
|
|
94
94
|
editHref={value.url ?? ""}
|
|
95
|
-
|
|
95
|
+
editTitle={value.title}
|
|
96
|
+
collections={linkOptions}
|
|
96
97
|
onSelect={(item) => set({ url: item.href, title: item.label })}
|
|
97
98
|
/>
|
|
98
99
|
) : (
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
4
|
import {
|
|
5
5
|
DndContext,
|
|
6
6
|
KeyboardSensor,
|
|
@@ -12,13 +12,12 @@ import {
|
|
|
12
12
|
} from "@dnd-kit/core";
|
|
13
13
|
import { SortableContext, arrayMove, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable";
|
|
14
14
|
import { CSS } from "@dnd-kit/utilities";
|
|
15
|
-
import {
|
|
15
|
+
import { GripVertical, Plus, Trash2 } from "lucide-react";
|
|
16
16
|
|
|
17
17
|
import { Button } from "./ui/button";
|
|
18
|
-
import { Command, CommandEmpty, CommandInput, CommandItem, CommandList } from "./ui/command";
|
|
19
|
-
import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
|
|
20
18
|
import { Sheet, SheetContent, SheetHeader, SheetTitle } from "./ui/sheet";
|
|
21
19
|
import { cn } from "../lib/utils";
|
|
20
|
+
import DocumentCombobox, { useRelationLabels } from "./DocumentCombobox";
|
|
22
21
|
|
|
23
22
|
type Option = { value: string; label: string };
|
|
24
23
|
|
|
@@ -27,7 +26,8 @@ type Props = {
|
|
|
27
26
|
value?: string;
|
|
28
27
|
hasMany?: boolean;
|
|
29
28
|
maxItems?: number;
|
|
30
|
-
|
|
29
|
+
/** Labels already known for selected values (optional seed — anything missing is resolved on mount). */
|
|
30
|
+
options?: Option[];
|
|
31
31
|
collectionSlug: string;
|
|
32
32
|
collectionLabel: string;
|
|
33
33
|
labelField?: string;
|
|
@@ -92,12 +92,11 @@ export default function RelationField({
|
|
|
92
92
|
value: initialValue,
|
|
93
93
|
hasMany = false,
|
|
94
94
|
maxItems,
|
|
95
|
-
options: initialOptions,
|
|
95
|
+
options: initialOptions = [],
|
|
96
96
|
collectionSlug,
|
|
97
97
|
collectionLabel,
|
|
98
98
|
labelField = "title",
|
|
99
99
|
}: Props) {
|
|
100
|
-
const [options, setOptions] = useState(initialOptions);
|
|
101
100
|
const [selected, setSelected] = useState<string[]>(() => {
|
|
102
101
|
if (!initialValue) return [];
|
|
103
102
|
if (hasMany) {
|
|
@@ -110,8 +109,9 @@ export default function RelationField({
|
|
|
110
109
|
}
|
|
111
110
|
return initialValue ? [initialValue] : [];
|
|
112
111
|
});
|
|
113
|
-
const
|
|
112
|
+
const { getLabel: findLabel, remember } = useRelationLabels(collectionSlug, selected, initialOptions);
|
|
114
113
|
const [sheetOpen, setSheetOpen] = useState(false);
|
|
114
|
+
|
|
115
115
|
const hiddenRef = useRef<HTMLInputElement>(null);
|
|
116
116
|
const iframeRef = useRef<HTMLIFrameElement>(null);
|
|
117
117
|
|
|
@@ -122,15 +122,18 @@ export default function RelationField({
|
|
|
122
122
|
hiddenRef.current?.dispatchEvent(new Event("change", { bubbles: true }));
|
|
123
123
|
}, [hiddenValue]);
|
|
124
124
|
|
|
125
|
-
const getLabel =
|
|
125
|
+
const getLabel = (id: string) => findLabel(id) ?? "…";
|
|
126
126
|
|
|
127
127
|
const atMax = hasMany && maxItems !== undefined && selected.length >= maxItems;
|
|
128
128
|
|
|
129
|
-
const displayLabel =
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
129
|
+
const displayLabel =
|
|
130
|
+
selected.length === 0
|
|
131
|
+
? ""
|
|
132
|
+
: hasMany
|
|
133
|
+
? maxItems
|
|
134
|
+
? `${selected.length}/${maxItems} selected`
|
|
135
|
+
: `${selected.length} selected`
|
|
136
|
+
: getLabel(selected[0]);
|
|
134
137
|
|
|
135
138
|
const selectItem = (id: string) => {
|
|
136
139
|
if (hasMany) {
|
|
@@ -141,7 +144,6 @@ export default function RelationField({
|
|
|
141
144
|
});
|
|
142
145
|
} else {
|
|
143
146
|
setSelected((prev) => (prev[0] === id ? [] : [id]));
|
|
144
|
-
setOpen(false);
|
|
145
147
|
}
|
|
146
148
|
};
|
|
147
149
|
|
|
@@ -171,12 +173,7 @@ export default function RelationField({
|
|
|
171
173
|
.then((doc) => {
|
|
172
174
|
if (doc) {
|
|
173
175
|
const label = String(doc[labelField] ?? doc.slug ?? docId);
|
|
174
|
-
|
|
175
|
-
const exists = prev.some((o) => o.value === docId);
|
|
176
|
-
return exists
|
|
177
|
-
? prev.map((o) => (o.value === docId ? { ...o, label } : o))
|
|
178
|
-
: [{ value: docId, label }, ...prev];
|
|
179
|
-
});
|
|
176
|
+
remember([{ docId, title: label }]);
|
|
180
177
|
if (hasMany) {
|
|
181
178
|
setSelected((prev) => (prev.includes(docId) ? prev : [...prev, docId]));
|
|
182
179
|
} else {
|
|
@@ -189,7 +186,7 @@ export default function RelationField({
|
|
|
189
186
|
|
|
190
187
|
window.addEventListener("message", handleMessage);
|
|
191
188
|
return () => window.removeEventListener("message", handleMessage);
|
|
192
|
-
}, [sheetOpen, collectionSlug, hasMany, labelField]);
|
|
189
|
+
}, [sheetOpen, collectionSlug, hasMany, labelField, remember]);
|
|
193
190
|
|
|
194
191
|
return (
|
|
195
192
|
<div className="space-y-2">
|
|
@@ -208,42 +205,19 @@ export default function RelationField({
|
|
|
208
205
|
</DndContext>
|
|
209
206
|
)}
|
|
210
207
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
<ChevronsUpDown className="ml-2 size-4 shrink-0 opacity-50" />
|
|
225
|
-
</Button>
|
|
226
|
-
</PopoverTrigger>
|
|
227
|
-
<PopoverContent className="w-(--radix-popover-trigger-width) p-0" align="start">
|
|
228
|
-
<Command>
|
|
229
|
-
<CommandInput placeholder={`Search ${collectionLabel.toLowerCase()}...`} />
|
|
230
|
-
<CommandList>
|
|
231
|
-
<CommandEmpty>No results found.</CommandEmpty>
|
|
232
|
-
{options.map((o) => (
|
|
233
|
-
<CommandItem
|
|
234
|
-
key={o.value}
|
|
235
|
-
value={o.label}
|
|
236
|
-
onSelect={() => selectItem(o.value)}
|
|
237
|
-
className={cn(atMax && !selected.includes(o.value) && "opacity-40")}
|
|
238
|
-
>
|
|
239
|
-
<Check className={cn("ml-1 size-4", selected.includes(o.value) ? "opacity-100" : "opacity-0")} />
|
|
240
|
-
{o.label}
|
|
241
|
-
</CommandItem>
|
|
242
|
-
))}
|
|
243
|
-
</CommandList>
|
|
244
|
-
</Command>
|
|
245
|
-
</PopoverContent>
|
|
246
|
-
</Popover>
|
|
208
|
+
<DocumentCombobox
|
|
209
|
+
collections={[collectionSlug]}
|
|
210
|
+
placeholder={`Search ${collectionLabel.toLowerCase()}...`}
|
|
211
|
+
display={displayLabel}
|
|
212
|
+
isSelected={(hit) => selected.includes(hit.docId)}
|
|
213
|
+
onPick={(hit) => {
|
|
214
|
+
remember([hit]);
|
|
215
|
+
selectItem(hit.docId);
|
|
216
|
+
}}
|
|
217
|
+
closeOnPick={!hasMany}
|
|
218
|
+
dimUnselected={atMax}
|
|
219
|
+
size="lg"
|
|
220
|
+
/>
|
|
247
221
|
|
|
248
222
|
{/* Create new button */}
|
|
249
223
|
<Button
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useState } from "react";
|
|
2
2
|
|
|
3
|
-
import { RepeaterField, type
|
|
4
|
-
import type {
|
|
3
|
+
import { RepeaterField, type SubFieldMeta } from "./block-fields";
|
|
4
|
+
import type { LinkableCollection } from "./InternalLinkPicker";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Standalone form control for top-level repeater fields —
|
|
@@ -14,14 +14,12 @@ export default function RepeaterInput({
|
|
|
14
14
|
name,
|
|
15
15
|
value,
|
|
16
16
|
itemFields,
|
|
17
|
-
relationOptions,
|
|
18
17
|
linkOptions,
|
|
19
18
|
}: {
|
|
20
19
|
name: string;
|
|
21
20
|
value?: string;
|
|
22
21
|
itemFields?: Record<string, SubFieldMeta>;
|
|
23
|
-
|
|
24
|
-
linkOptions?: LinkOptionGroup[];
|
|
22
|
+
linkOptions?: LinkableCollection[];
|
|
25
23
|
}) {
|
|
26
24
|
const [items, setItems] = useState<unknown>(() => {
|
|
27
25
|
try {
|
|
@@ -36,7 +34,6 @@ export default function RepeaterInput({
|
|
|
36
34
|
<RepeaterField
|
|
37
35
|
blockKey={name}
|
|
38
36
|
itemFields={itemFields}
|
|
39
|
-
relationOptions={relationOptions}
|
|
40
37
|
linkOptions={linkOptions}
|
|
41
38
|
value={items}
|
|
42
39
|
onChange={setItems}
|