@kidecms/core 0.20.0 → 0.22.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/BlockEditor.tsx +4 -12
- package/admin/components/CommandPalette.tsx +19 -72
- package/admin/components/ContentEditor.tsx +5 -10
- package/admin/components/DocumentCombobox.tsx +171 -0
- package/admin/components/FieldControl.astro +17 -32
- package/admin/components/InternalLinkPicker.tsx +30 -62
- package/admin/components/LinkField.tsx +26 -12
- package/admin/components/RelationField.tsx +32 -58
- package/admin/components/RepeaterInput.tsx +3 -6
- package/admin/components/SharedSectionBlockField.tsx +3 -6
- package/admin/components/TreeItemsEditor.tsx +4 -4
- package/admin/components/TreeSelect.tsx +33 -19
- package/admin/components/block-fields.tsx +37 -50
- package/admin/components/content-block-spec.ts +5 -6
- package/admin/lib/edit-data.ts +84 -129
- package/admin/lib/use-document-search.ts +107 -0
- package/admin/views/EditView.astro +18 -27
- package/admin/views/ListView.astro +3 -1
- package/admin/views/RecentView.astro +32 -9
- package/core/api.ts +3 -1
- package/core/generator.ts +20 -10
- package/core/index.ts +2 -0
- package/core/migrate.ts +17 -1
- package/core/values.ts +32 -0
- package/internals/context.ts +4 -2
- package/internals/push.ts +8 -2
- package/package.json +1 -1
- package/routes/api/admin/search.ts +102 -45
|
@@ -5,21 +5,30 @@ 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
|
-
// { type, url, label, title, newTab }. A leading "/" is
|
|
12
|
-
// link. When
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
|
|
11
|
+
// { type, url, label, title, newTab, docId, collection }. A leading "/" is
|
|
12
|
+
// treated as an internal link. When linkable collections are provided, internal
|
|
13
|
+
// links are chosen with a document picker; the pick stores the document
|
|
14
|
+
// reference (docId + collection) so renderers can resolve the current route via
|
|
15
|
+
// resolveLinkUrl(), with `url` kept as a cached fallback. `title` is the picked
|
|
16
|
+
// document's title — renderers use it as the link text when `label` is left empty.
|
|
17
|
+
type LinkValue = {
|
|
18
|
+
type?: string;
|
|
19
|
+
url?: string;
|
|
20
|
+
label?: string;
|
|
21
|
+
title?: string;
|
|
22
|
+
newTab?: boolean;
|
|
23
|
+
docId?: string;
|
|
24
|
+
collection?: string;
|
|
25
|
+
};
|
|
17
26
|
|
|
18
27
|
type Props = {
|
|
19
28
|
name?: string;
|
|
20
29
|
value?: string | LinkValue;
|
|
21
30
|
onChange?: (value: LinkValue) => void;
|
|
22
|
-
linkOptions?:
|
|
31
|
+
linkOptions?: LinkableCollection[];
|
|
23
32
|
};
|
|
24
33
|
|
|
25
34
|
function parse(v: unknown): LinkValue {
|
|
@@ -92,14 +101,19 @@ export default function LinkField({ name, value: initial, onChange, linkOptions
|
|
|
92
101
|
{linkType === "internal" ? (
|
|
93
102
|
<InternalLinkPicker
|
|
94
103
|
editHref={value.url ?? ""}
|
|
95
|
-
|
|
96
|
-
|
|
104
|
+
editTitle={value.title}
|
|
105
|
+
collections={linkOptions}
|
|
106
|
+
onSelect={(item) =>
|
|
107
|
+
set({ url: item.href, title: item.label, docId: item.id, collection: item.collection })
|
|
108
|
+
}
|
|
97
109
|
/>
|
|
98
110
|
) : (
|
|
99
111
|
<Input
|
|
100
112
|
value={value.url ?? ""}
|
|
101
113
|
placeholder="https://example.com"
|
|
102
|
-
onChange={(e) =>
|
|
114
|
+
onChange={(e) =>
|
|
115
|
+
set({ url: e.target.value, title: undefined, docId: undefined, collection: undefined })
|
|
116
|
+
}
|
|
103
117
|
/>
|
|
104
118
|
)}
|
|
105
119
|
</div>
|
|
@@ -107,7 +121,7 @@ export default function LinkField({ name, value: initial, onChange, linkOptions
|
|
|
107
121
|
<Input
|
|
108
122
|
value={value.url ?? ""}
|
|
109
123
|
placeholder="https://example.com or /about"
|
|
110
|
-
onChange={(e) => set({ url: e.target.value, title: undefined })}
|
|
124
|
+
onChange={(e) => set({ url: e.target.value, title: undefined, docId: undefined, collection: undefined })}
|
|
111
125
|
/>
|
|
112
126
|
)}
|
|
113
127
|
</div>
|
|
@@ -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}
|
|
@@ -2,14 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
import { useMemo, useState } from "react";
|
|
4
4
|
|
|
5
|
-
import { SubField, blankBlockFields, humanize, type BlockTypesMeta
|
|
5
|
+
import { SubField, blankBlockFields, humanize, type BlockTypesMeta } from "./block-fields";
|
|
6
6
|
import SelectField from "./SelectField";
|
|
7
7
|
|
|
8
8
|
type Props = {
|
|
9
9
|
name: string;
|
|
10
10
|
value?: string;
|
|
11
11
|
types: BlockTypesMeta;
|
|
12
|
-
blockRelationOptions?: Record<string, RelationOption[]>;
|
|
13
12
|
};
|
|
14
13
|
|
|
15
14
|
type SharedBlock = {
|
|
@@ -35,7 +34,7 @@ function parseInitialBlock(value: string | undefined, types: BlockTypesMeta): Sh
|
|
|
35
34
|
}
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
export default function SharedSectionBlockField({ name, value, types
|
|
37
|
+
export default function SharedSectionBlockField({ name, value, types }: Props) {
|
|
39
38
|
const [block, setBlock] = useState<SharedBlock>(() => parseInitialBlock(value, types));
|
|
40
39
|
const typeNames = Object.keys(types);
|
|
41
40
|
const selectedType = block.type;
|
|
@@ -50,8 +49,6 @@ export default function SharedSectionBlockField({ name, value, types, blockRelat
|
|
|
50
49
|
setBlock((prev) => ({ ...prev, [fieldName]: fieldValue }));
|
|
51
50
|
};
|
|
52
51
|
|
|
53
|
-
const relationOptionsFor = (fieldName: string) => blockRelationOptions[`shared:${selectedType}:${fieldName}`] ?? [];
|
|
54
|
-
|
|
55
52
|
if (typeNames.length === 0) {
|
|
56
53
|
return (
|
|
57
54
|
<div className="bg-field-subtle text-muted-foreground rounded-lg border border-dashed px-4 py-3 text-sm">
|
|
@@ -83,7 +80,7 @@ export default function SharedSectionBlockField({ name, value, types, blockRelat
|
|
|
83
80
|
meta={meta}
|
|
84
81
|
value={block[fieldName]}
|
|
85
82
|
onChange={(v) => updateField(fieldName, v)}
|
|
86
|
-
|
|
83
|
+
siblings={block}
|
|
87
84
|
/>
|
|
88
85
|
))}
|
|
89
86
|
</div>
|
|
@@ -30,7 +30,7 @@ import { Input } from "./ui/input";
|
|
|
30
30
|
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
|
|
31
31
|
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from "./ui/collapsible";
|
|
32
32
|
import { cn } from "../lib/utils";
|
|
33
|
-
import InternalLinkPicker, { type
|
|
33
|
+
import InternalLinkPicker, { type LinkableCollection } from "./InternalLinkPicker";
|
|
34
34
|
import {
|
|
35
35
|
type TreeItem,
|
|
36
36
|
generateId,
|
|
@@ -67,7 +67,7 @@ type Props = {
|
|
|
67
67
|
value?: string;
|
|
68
68
|
variant: "menu" | "taxonomy";
|
|
69
69
|
label?: string;
|
|
70
|
-
linkOptions?:
|
|
70
|
+
linkOptions?: LinkableCollection[];
|
|
71
71
|
};
|
|
72
72
|
|
|
73
73
|
function blankEdit(id: string): EditState {
|
|
@@ -298,7 +298,7 @@ export default function TreeItemsEditor({ name, value, variant, label, linkOptio
|
|
|
298
298
|
|
|
299
299
|
const startEdit = (item: TreeItem) => {
|
|
300
300
|
if (variant === "menu") {
|
|
301
|
-
const isInternal =
|
|
301
|
+
const isInternal = String(item.href ?? "").startsWith("/");
|
|
302
302
|
setEditing({
|
|
303
303
|
...blankEdit(item.id),
|
|
304
304
|
label: String(item.label ?? ""),
|
|
@@ -470,7 +470,7 @@ export default function TreeItemsEditor({ name, value, variant, label, linkOptio
|
|
|
470
470
|
) : (
|
|
471
471
|
<InternalLinkPicker
|
|
472
472
|
editHref={editing.href}
|
|
473
|
-
|
|
473
|
+
collections={linkOptions}
|
|
474
474
|
className="flex-3"
|
|
475
475
|
triggerClassName="bg-background h-7 rounded-md"
|
|
476
476
|
onSelect={(item) => {
|
|
@@ -78,6 +78,7 @@ export default function TreeSelect({
|
|
|
78
78
|
}: Props) {
|
|
79
79
|
const [value, setValue] = useState(initialValue ?? "");
|
|
80
80
|
const [open, setOpen] = useState(false);
|
|
81
|
+
const [query, setQuery] = useState("");
|
|
81
82
|
const hiddenRef = useRef<HTMLInputElement>(null);
|
|
82
83
|
const isInitial = useRef(true);
|
|
83
84
|
|
|
@@ -91,10 +92,21 @@ export default function TreeSelect({
|
|
|
91
92
|
|
|
92
93
|
const selected = items.find((i) => i.value === value);
|
|
93
94
|
|
|
95
|
+
// Filtering is ours, not cmdk's: cmdk reorders the DOM while filtering (and
|
|
96
|
+
// doesn't restore it when the query clears), which would bump the pinned
|
|
97
|
+
// "None" row around — and "None" is a clear-action, not a match for any query.
|
|
98
|
+
const q = query.trim().toLowerCase();
|
|
99
|
+
const filtered = q ? items.filter((item) => item.path.join(" / ").toLowerCase().includes(q)) : items;
|
|
100
|
+
|
|
101
|
+
const handleOpenChange = (next: boolean) => {
|
|
102
|
+
setOpen(next);
|
|
103
|
+
if (!next) setQuery("");
|
|
104
|
+
};
|
|
105
|
+
|
|
94
106
|
return (
|
|
95
107
|
<div className="space-y-2">
|
|
96
108
|
<input ref={hiddenRef} type="hidden" name={name} value={value} />
|
|
97
|
-
<Popover open={open} onOpenChange={
|
|
109
|
+
<Popover open={open} onOpenChange={handleOpenChange}>
|
|
98
110
|
<PopoverTrigger asChild>
|
|
99
111
|
<Button
|
|
100
112
|
variant="input"
|
|
@@ -122,31 +134,33 @@ export default function TreeSelect({
|
|
|
122
134
|
</Button>
|
|
123
135
|
</PopoverTrigger>
|
|
124
136
|
<PopoverContent className="w-(--radix-popover-trigger-width) p-0" align="start">
|
|
125
|
-
<Command>
|
|
126
|
-
<CommandInput placeholder={searchPlaceholder} />
|
|
137
|
+
<Command shouldFilter={false}>
|
|
138
|
+
<CommandInput value={query} onValueChange={setQuery} placeholder={searchPlaceholder} />
|
|
127
139
|
<CommandList>
|
|
128
|
-
<CommandEmpty>{emptyMessage}</CommandEmpty>
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
<
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
{filtered.length === 0 && <CommandEmpty>{emptyMessage}</CommandEmpty>}
|
|
141
|
+
{!q && (
|
|
142
|
+
<CommandItem
|
|
143
|
+
value="__none__"
|
|
144
|
+
onSelect={() => {
|
|
145
|
+
setValue("");
|
|
146
|
+
onChangeProp?.("");
|
|
147
|
+
handleOpenChange(false);
|
|
148
|
+
}}
|
|
149
|
+
>
|
|
150
|
+
<div className="flex items-center">
|
|
151
|
+
<Check className={cn("mr-2 ml-1 size-4 shrink-0", !value ? "opacity-100" : "opacity-0")} />
|
|
152
|
+
<span className="text-muted-foreground">None</span>
|
|
153
|
+
</div>
|
|
154
|
+
</CommandItem>
|
|
155
|
+
)}
|
|
156
|
+
{filtered.map((item) => (
|
|
143
157
|
<CommandItem
|
|
144
158
|
key={item.value}
|
|
145
159
|
value={item.path.join(" / ")}
|
|
146
160
|
onSelect={() => {
|
|
147
161
|
setValue(item.value === value ? "" : item.value);
|
|
148
162
|
onChangeProp?.(item.value === value ? "" : item.value);
|
|
149
|
-
|
|
163
|
+
handleOpenChange(false);
|
|
150
164
|
}}
|
|
151
165
|
>
|
|
152
166
|
<div className="flex items-center" style={{ paddingLeft: `${item.depth * 1.25}rem` }}>
|
|
@@ -24,7 +24,8 @@ import ImagePicker from "./ImagePicker";
|
|
|
24
24
|
import SelectField from "./SelectField";
|
|
25
25
|
import ColorField from "./ColorField";
|
|
26
26
|
import LinkField from "./LinkField";
|
|
27
|
-
import type {
|
|
27
|
+
import type { LinkableCollection } from "./InternalLinkPicker";
|
|
28
|
+
import DocumentCombobox, { useRelationLabels } from "./DocumentCombobox";
|
|
28
29
|
import YoutubeField from "./YoutubeField";
|
|
29
30
|
|
|
30
31
|
// -----------------------------------------------
|
|
@@ -45,13 +46,21 @@ export type SubFieldMeta = {
|
|
|
45
46
|
of?: { type: string };
|
|
46
47
|
collection?: string;
|
|
47
48
|
hasMany?: boolean;
|
|
49
|
+
condition?: { field: string; value: string | string[] | boolean };
|
|
48
50
|
/** Typed repeater row schema (json + admin.component "repeater"). */
|
|
49
51
|
itemFields?: Record<string, SubFieldMeta>;
|
|
50
52
|
};
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
/** Same semantics as the edit form's conditional-visibility script (EditScripts). */
|
|
55
|
+
export function matchesCondition(expected: string | string[] | boolean, current: unknown): boolean {
|
|
56
|
+
if (Array.isArray(expected)) return expected.some((v) => String(v) === String(current ?? ""));
|
|
57
|
+
if (typeof expected === "boolean") {
|
|
58
|
+
return expected ? current === true || current === "true" : !current || current === "false";
|
|
59
|
+
}
|
|
60
|
+
return String(expected) === String(current ?? "");
|
|
61
|
+
}
|
|
53
62
|
|
|
54
|
-
export type
|
|
63
|
+
export type BlockTypesMeta = Record<string, Record<string, SubFieldMeta>>;
|
|
55
64
|
|
|
56
65
|
export function generateKey() {
|
|
57
66
|
return "blk_" + Math.random().toString(36).slice(2, 9);
|
|
@@ -100,7 +109,7 @@ export function SubField({
|
|
|
100
109
|
meta,
|
|
101
110
|
value,
|
|
102
111
|
onChange,
|
|
103
|
-
|
|
112
|
+
siblings,
|
|
104
113
|
linkOptions = [],
|
|
105
114
|
}: {
|
|
106
115
|
blockKey: string;
|
|
@@ -108,12 +117,15 @@ export function SubField({
|
|
|
108
117
|
meta: SubFieldMeta;
|
|
109
118
|
value: unknown;
|
|
110
119
|
onChange: (value: unknown) => void;
|
|
111
|
-
|
|
112
|
-
|
|
120
|
+
/** The block's other field values — evaluated against `meta.condition`. */
|
|
121
|
+
siblings?: Record<string, unknown>;
|
|
122
|
+
linkOptions?: LinkableCollection[];
|
|
113
123
|
}) {
|
|
114
124
|
const label = meta.label ?? humanize(fieldName);
|
|
115
125
|
const fieldId = `${blockKey}_${fieldName}`;
|
|
116
126
|
|
|
127
|
+
if (meta.condition && !matchesCondition(meta.condition.value, siblings?.[meta.condition.field])) return null;
|
|
128
|
+
|
|
117
129
|
return (
|
|
118
130
|
<div className="grid gap-2">
|
|
119
131
|
<Label htmlFor={fieldId}>
|
|
@@ -121,14 +133,7 @@ export function SubField({
|
|
|
121
133
|
{meta.required ? " *" : ""}
|
|
122
134
|
</Label>
|
|
123
135
|
{meta.admin?.help && <p className="text-muted-foreground text-xs leading-5">{meta.admin.help}</p>}
|
|
124
|
-
<SubFieldControl
|
|
125
|
-
fieldId={fieldId}
|
|
126
|
-
meta={meta}
|
|
127
|
-
value={value}
|
|
128
|
-
onChange={onChange}
|
|
129
|
-
relationOptions={relationOptions}
|
|
130
|
-
linkOptions={linkOptions}
|
|
131
|
-
/>
|
|
136
|
+
<SubFieldControl fieldId={fieldId} meta={meta} value={value} onChange={onChange} linkOptions={linkOptions} />
|
|
132
137
|
</div>
|
|
133
138
|
);
|
|
134
139
|
}
|
|
@@ -181,16 +186,15 @@ function RelationControl({
|
|
|
181
186
|
meta,
|
|
182
187
|
value,
|
|
183
188
|
onChange,
|
|
184
|
-
relationOptions,
|
|
185
189
|
}: {
|
|
186
190
|
fieldId: string;
|
|
187
191
|
meta: SubFieldMeta;
|
|
188
192
|
value: unknown;
|
|
189
193
|
onChange: (value: unknown) => void;
|
|
190
|
-
relationOptions: RelationOption[];
|
|
191
194
|
}) {
|
|
192
195
|
const selected = parseSelected(meta, value);
|
|
193
|
-
const
|
|
196
|
+
const collectionSlug = meta.collection ?? "";
|
|
197
|
+
const { getLabel, remember } = useRelationLabels(collectionSlug, selected);
|
|
194
198
|
const toggle = (id: string) => {
|
|
195
199
|
if (meta.hasMany) {
|
|
196
200
|
const next = selected.includes(id) ? selected.filter((v) => v !== id) : [...selected, id];
|
|
@@ -208,7 +212,7 @@ function RelationControl({
|
|
|
208
212
|
key={id}
|
|
209
213
|
className="bg-card text-secondary-foreground inline-flex items-center gap-1 rounded-md border px-2 py-0.5 text-sm"
|
|
210
214
|
>
|
|
211
|
-
{getLabel(id)}
|
|
215
|
+
{getLabel(id) ?? "…"}
|
|
212
216
|
<button
|
|
213
217
|
type="button"
|
|
214
218
|
onClick={() => toggle(id)}
|
|
@@ -220,18 +224,17 @@ function RelationControl({
|
|
|
220
224
|
))}
|
|
221
225
|
</div>
|
|
222
226
|
)}
|
|
223
|
-
<
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
} else {
|
|
232
|
-
onChange(v);
|
|
233
|
-
}
|
|
227
|
+
<DocumentCombobox
|
|
228
|
+
collections={[collectionSlug]}
|
|
229
|
+
placeholder={`Search ${meta.label?.toLowerCase() ?? "documents"}...`}
|
|
230
|
+
display={meta.hasMany ? "" : selected[0] ? (getLabel(selected[0]) ?? "…") : ""}
|
|
231
|
+
isSelected={(hit) => selected.includes(hit.docId)}
|
|
232
|
+
onPick={(hit) => {
|
|
233
|
+
remember([hit]);
|
|
234
|
+
toggle(hit.docId);
|
|
234
235
|
}}
|
|
236
|
+
closeOnPick={!meta.hasMany}
|
|
237
|
+
triggerId={fieldId}
|
|
235
238
|
/>
|
|
236
239
|
</div>
|
|
237
240
|
);
|
|
@@ -278,15 +281,13 @@ export function SubFieldControl({
|
|
|
278
281
|
meta,
|
|
279
282
|
value,
|
|
280
283
|
onChange,
|
|
281
|
-
relationOptions = [],
|
|
282
284
|
linkOptions = [],
|
|
283
285
|
}: {
|
|
284
286
|
fieldId: string;
|
|
285
287
|
meta: SubFieldMeta;
|
|
286
288
|
value: unknown;
|
|
287
289
|
onChange: (value: unknown) => void;
|
|
288
|
-
|
|
289
|
-
linkOptions?: LinkOptionGroup[];
|
|
290
|
+
linkOptions?: LinkableCollection[];
|
|
290
291
|
}) {
|
|
291
292
|
const strValue = value == null ? "" : String(value);
|
|
292
293
|
|
|
@@ -384,15 +385,7 @@ export function SubFieldControl({
|
|
|
384
385
|
);
|
|
385
386
|
|
|
386
387
|
case "relation":
|
|
387
|
-
return
|
|
388
|
-
<RelationControl
|
|
389
|
-
fieldId={fieldId}
|
|
390
|
-
meta={meta}
|
|
391
|
-
value={value}
|
|
392
|
-
onChange={onChange}
|
|
393
|
-
relationOptions={relationOptions}
|
|
394
|
-
/>
|
|
395
|
-
);
|
|
388
|
+
return <RelationControl fieldId={fieldId} meta={meta} value={value} onChange={onChange} />;
|
|
396
389
|
|
|
397
390
|
case "array":
|
|
398
391
|
return <ArrayControl fieldId={fieldId} meta={meta} value={value} onChange={onChange} />;
|
|
@@ -403,7 +396,6 @@ export function SubFieldControl({
|
|
|
403
396
|
<RepeaterField
|
|
404
397
|
blockKey={fieldId}
|
|
405
398
|
itemFields={meta.itemFields}
|
|
406
|
-
relationOptions={relationOptions}
|
|
407
399
|
linkOptions={linkOptions}
|
|
408
400
|
value={value}
|
|
409
401
|
onChange={onChange}
|
|
@@ -440,7 +432,6 @@ function SortableRepeaterItem({
|
|
|
440
432
|
item,
|
|
441
433
|
fieldKeys,
|
|
442
434
|
itemFields,
|
|
443
|
-
relationOptions,
|
|
444
435
|
linkOptions,
|
|
445
436
|
index,
|
|
446
437
|
isExpanded,
|
|
@@ -454,8 +445,7 @@ function SortableRepeaterItem({
|
|
|
454
445
|
item: Record<string, unknown>;
|
|
455
446
|
fieldKeys: string[];
|
|
456
447
|
itemFields?: Record<string, SubFieldMeta>;
|
|
457
|
-
|
|
458
|
-
linkOptions?: LinkOptionGroup[];
|
|
448
|
+
linkOptions?: LinkableCollection[];
|
|
459
449
|
index: number;
|
|
460
450
|
isExpanded: boolean;
|
|
461
451
|
autoFocus?: boolean;
|
|
@@ -552,7 +542,7 @@ function SortableRepeaterItem({
|
|
|
552
542
|
meta={itemFields[key]}
|
|
553
543
|
value={item[key]}
|
|
554
544
|
onChange={(v) => onUpdate(key, v)}
|
|
555
|
-
|
|
545
|
+
siblings={item}
|
|
556
546
|
linkOptions={linkOptions}
|
|
557
547
|
/>
|
|
558
548
|
))
|
|
@@ -593,15 +583,13 @@ function parseArray(value: unknown): unknown[] {
|
|
|
593
583
|
export function RepeaterField({
|
|
594
584
|
blockKey,
|
|
595
585
|
itemFields,
|
|
596
|
-
relationOptions,
|
|
597
586
|
linkOptions,
|
|
598
587
|
value,
|
|
599
588
|
onChange,
|
|
600
589
|
}: {
|
|
601
590
|
blockKey: string;
|
|
602
591
|
itemFields?: Record<string, SubFieldMeta>;
|
|
603
|
-
|
|
604
|
-
linkOptions?: LinkOptionGroup[];
|
|
592
|
+
linkOptions?: LinkableCollection[];
|
|
605
593
|
value: unknown;
|
|
606
594
|
onChange: (value: unknown) => void;
|
|
607
595
|
}) {
|
|
@@ -707,7 +695,6 @@ export function RepeaterField({
|
|
|
707
695
|
item={item}
|
|
708
696
|
fieldKeys={fieldKeys}
|
|
709
697
|
itemFields={itemFields}
|
|
710
|
-
relationOptions={relationOptions}
|
|
711
698
|
linkOptions={linkOptions}
|
|
712
699
|
index={index}
|
|
713
700
|
isExpanded={expandedKeys.has(String(item._key))}
|