@eutobias/serenojs 0.1.2 → 0.1.4
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/README.md +21 -6
- package/admin-dist/assets/index-CEMJnHOP.js +22 -0
- package/admin-dist/assets/index-Di8sAvwO.css +2 -0
- package/admin-dist/assets/marked.esm-BBttRptN.js +77 -0
- package/admin-dist/assets/purify.es-Bz-uQoIK.js +3 -0
- package/admin-dist/index.html +2 -2
- package/dist/config.d.ts +2 -2
- package/dist/core/backup.server.js +71 -4
- package/dist/core/db.server.js +29 -1
- package/dist/core/schema.js +8 -1
- package/dist/core/types.d.ts +15 -4
- package/dist/core/validation.js +61 -0
- package/dist/ui/admin/collection-manager.js +1 -1
- package/dist/ui/admin/display.js +7 -0
- package/dist/ui/admin/field-control.d.ts +3 -1
- package/dist/ui/admin/field-control.js +10 -2
- package/dist/ui/admin/i18n.js +26 -0
- package/dist/ui/admin/object-list-control.d.ts +15 -0
- package/dist/ui/admin/object-list-control.js +42 -0
- package/dist/ui/admin/rich-text-control.d.ts +15 -0
- package/dist/ui/admin/rich-text-control.js +62 -0
- package/dist/ui/admin/rich-text-icons.d.ts +8 -0
- package/dist/ui/admin/rich-text-icons.js +18 -0
- package/dist/ui/index.d.ts +1 -0
- package/dist/ui/index.js +1 -0
- package/dist/ui/markdown-preview.d.ts +10 -0
- package/dist/ui/markdown-preview.js +34 -0
- package/package.json +4 -1
- package/src/admin/styles.css +152 -0
- package/src/config.ts +2 -1
- package/src/core/backup.server.ts +81 -4
- package/src/core/db.server.ts +34 -1
- package/src/core/schema.ts +8 -1
- package/src/core/types.ts +18 -3
- package/src/core/validation.ts +71 -0
- package/src/ui/admin/collection-manager.tsx +1 -0
- package/src/ui/admin/display.tsx +13 -1
- package/src/ui/admin/field-control.tsx +43 -2
- package/src/ui/admin/i18n.tsx +28 -0
- package/src/ui/admin/object-list-control.tsx +154 -0
- package/src/ui/admin/rich-text-control.tsx +173 -0
- package/src/ui/admin/rich-text-icons.tsx +125 -0
- package/src/ui/index.ts +1 -0
- package/src/ui/markdown-preview.tsx +42 -0
- package/admin-dist/assets/index-D2m0Eb4k.css +0 -2
- package/admin-dist/assets/index-Ddgw58Iy.js +0 -9
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
type SchemaWriteResult,
|
|
26
26
|
} from "./schema-codegen.server.js";
|
|
27
27
|
import { configureCms, getCmsConfig } from "../runtime.js";
|
|
28
|
+
import { isMediaUrl } from "./media.js";
|
|
28
29
|
import type { CollectionDef, FieldDef, FieldType } from "./types.js";
|
|
29
30
|
|
|
30
31
|
export const BACKUP_FORMAT = "serenojs-backup";
|
|
@@ -472,11 +473,26 @@ function inferFieldType(sqlType: string, samples: unknown[]): FieldType {
|
|
|
472
473
|
(entry) =>
|
|
473
474
|
Boolean(entry) &&
|
|
474
475
|
typeof entry === "object" &&
|
|
475
|
-
|
|
476
|
+
!Array.isArray(entry) &&
|
|
477
|
+
typeof (entry as { url?: unknown }).url === "string" &&
|
|
478
|
+
isMediaUrl((entry as { url: string }).url) &&
|
|
479
|
+
Object.keys(entry as Record<string, unknown>).every((key) =>
|
|
480
|
+
["url", "alt", "primary"].includes(key),
|
|
481
|
+
),
|
|
476
482
|
)
|
|
477
483
|
) {
|
|
478
484
|
return "media-list";
|
|
479
485
|
}
|
|
486
|
+
if (
|
|
487
|
+
parsed.every(
|
|
488
|
+
(entry) =>
|
|
489
|
+
Boolean(entry) &&
|
|
490
|
+
typeof entry === "object" &&
|
|
491
|
+
!Array.isArray(entry),
|
|
492
|
+
)
|
|
493
|
+
) {
|
|
494
|
+
return "object-list";
|
|
495
|
+
}
|
|
480
496
|
if (parsed.every((entry) => typeof entry === "string")) {
|
|
481
497
|
return "list";
|
|
482
498
|
}
|
|
@@ -489,6 +505,57 @@ function inferFieldType(sqlType: string, samples: unknown[]): FieldType {
|
|
|
489
505
|
return "text";
|
|
490
506
|
}
|
|
491
507
|
|
|
508
|
+
/** Approximate type of a value found inside an inferred `object-list`. */
|
|
509
|
+
function inferScalarType(samples: unknown[]): FieldType {
|
|
510
|
+
if (samples.length > 0 && samples.every((value) => typeof value === "boolean")) {
|
|
511
|
+
return "boolean";
|
|
512
|
+
}
|
|
513
|
+
if (samples.length > 0 && samples.every((value) => typeof value === "number")) {
|
|
514
|
+
return "number";
|
|
515
|
+
}
|
|
516
|
+
if (
|
|
517
|
+
samples.length > 0 &&
|
|
518
|
+
samples.every(
|
|
519
|
+
(value) =>
|
|
520
|
+
Array.isArray(value) &&
|
|
521
|
+
value.every((entry) => typeof entry === "string"),
|
|
522
|
+
)
|
|
523
|
+
) {
|
|
524
|
+
return "list";
|
|
525
|
+
}
|
|
526
|
+
return "text";
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** Reconstruct the subfields of an inferred `object-list` from raw samples. */
|
|
530
|
+
function inferObjectFields(samples: unknown[]): FieldDef[] {
|
|
531
|
+
const values = new Map<string, unknown[]>();
|
|
532
|
+
for (const sample of samples) {
|
|
533
|
+
if (typeof sample !== "string") continue;
|
|
534
|
+
try {
|
|
535
|
+
const parsed: unknown = JSON.parse(sample);
|
|
536
|
+
if (!Array.isArray(parsed)) continue;
|
|
537
|
+
for (const entry of parsed) {
|
|
538
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
|
|
539
|
+
for (const [key, value] of Object.entries(
|
|
540
|
+
entry as Record<string, unknown>,
|
|
541
|
+
)) {
|
|
542
|
+
const list = values.get(key) ?? [];
|
|
543
|
+
if (value !== null && value !== undefined) list.push(value);
|
|
544
|
+
values.set(key, list);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
} catch {
|
|
548
|
+
// Not JSON; ignore this sample.
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return [...values.entries()].map(([name, entries]) => ({
|
|
552
|
+
name,
|
|
553
|
+
label: humanize(name),
|
|
554
|
+
type: inferScalarType(entries),
|
|
555
|
+
localized: false,
|
|
556
|
+
}));
|
|
557
|
+
}
|
|
558
|
+
|
|
492
559
|
/**
|
|
493
560
|
* Reconstruct descriptors from the backup database for archives created before
|
|
494
561
|
* schemas were embedded. Types are inferred from SQLite columns and samples.
|
|
@@ -557,12 +624,22 @@ function inferSchemas(
|
|
|
557
624
|
const fields: FieldDef[] = [];
|
|
558
625
|
const usedLocales = new Set<string>();
|
|
559
626
|
for (const group of groups.values()) {
|
|
560
|
-
|
|
627
|
+
const type = inferFieldType(group.sqlType, group.samples.slice(0, 50));
|
|
628
|
+
const field: FieldDef = {
|
|
561
629
|
name: group.base,
|
|
562
630
|
label: humanize(group.base),
|
|
563
|
-
type
|
|
631
|
+
type,
|
|
564
632
|
localized: group.localized,
|
|
565
|
-
}
|
|
633
|
+
};
|
|
634
|
+
if (type === "object-list") {
|
|
635
|
+
const inferred = inferObjectFields(group.samples.slice(0, 50));
|
|
636
|
+
if (inferred.length > 0) {
|
|
637
|
+
field.fields = inferred;
|
|
638
|
+
} else {
|
|
639
|
+
field.type = "text";
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
fields.push(field);
|
|
566
643
|
for (const code of group.locales) usedLocales.add(code);
|
|
567
644
|
}
|
|
568
645
|
|
package/src/core/db.server.ts
CHANGED
|
@@ -24,6 +24,7 @@ import type {
|
|
|
24
24
|
Item,
|
|
25
25
|
ListResult,
|
|
26
26
|
MediaItem,
|
|
27
|
+
ObjectItem,
|
|
27
28
|
} from "./types.js";
|
|
28
29
|
|
|
29
30
|
import { getCmsConfig } from "../runtime.js";
|
|
@@ -269,6 +270,15 @@ function serialize(field: FieldDef, value: FieldValue): string | number | null {
|
|
|
269
270
|
.filter(Boolean);
|
|
270
271
|
return entries.length > 0 ? JSON.stringify(entries) : null;
|
|
271
272
|
}
|
|
273
|
+
case "object-list": {
|
|
274
|
+
const items = Array.isArray(value)
|
|
275
|
+
? value.filter(
|
|
276
|
+
(entry): entry is ObjectItem =>
|
|
277
|
+
Boolean(entry) && typeof entry === "object" && !Array.isArray(entry),
|
|
278
|
+
)
|
|
279
|
+
: [];
|
|
280
|
+
return items.length > 0 ? JSON.stringify(items) : null;
|
|
281
|
+
}
|
|
272
282
|
default:
|
|
273
283
|
return String(value);
|
|
274
284
|
}
|
|
@@ -308,6 +318,20 @@ function parseStringList(raw: unknown): string[] {
|
|
|
308
318
|
}
|
|
309
319
|
}
|
|
310
320
|
|
|
321
|
+
function parseObjectList(raw: unknown): ObjectItem[] {
|
|
322
|
+
if (typeof raw !== "string" || !raw.trim()) return [];
|
|
323
|
+
try {
|
|
324
|
+
const parsed = JSON.parse(raw);
|
|
325
|
+
if (!Array.isArray(parsed)) return [];
|
|
326
|
+
return parsed.filter(
|
|
327
|
+
(entry): entry is ObjectItem =>
|
|
328
|
+
Boolean(entry) && typeof entry === "object" && !Array.isArray(entry),
|
|
329
|
+
);
|
|
330
|
+
} catch {
|
|
331
|
+
return [];
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
311
335
|
/** Decode a single raw column value into a field value. */
|
|
312
336
|
function decodeFieldValue(field: FieldDef, raw: unknown): FieldValue {
|
|
313
337
|
switch (field.type) {
|
|
@@ -325,6 +349,8 @@ function decodeFieldValue(field: FieldDef, raw: unknown): FieldValue {
|
|
|
325
349
|
return parseMediaList(raw);
|
|
326
350
|
case "list":
|
|
327
351
|
return parseStringList(raw);
|
|
352
|
+
case "object-list":
|
|
353
|
+
return parseObjectList(raw);
|
|
328
354
|
default:
|
|
329
355
|
return raw === null || raw === undefined ? "" : String(raw);
|
|
330
356
|
}
|
|
@@ -402,7 +428,14 @@ export function listItems(
|
|
|
402
428
|
const search = q.trim();
|
|
403
429
|
if (search) {
|
|
404
430
|
const searchColumns = storedColumns(collection).filter((entry) =>
|
|
405
|
-
[
|
|
431
|
+
[
|
|
432
|
+
"text",
|
|
433
|
+
"email",
|
|
434
|
+
"textarea",
|
|
435
|
+
"richtext",
|
|
436
|
+
"select",
|
|
437
|
+
"date",
|
|
438
|
+
].includes(entry.field.type),
|
|
406
439
|
);
|
|
407
440
|
if (searchColumns.length > 0) {
|
|
408
441
|
where =
|
package/src/core/schema.ts
CHANGED
|
@@ -8,7 +8,13 @@ import {
|
|
|
8
8
|
import type { CollectionDef, FieldDef, FieldValue, LocalizedValue } from "./types.js";
|
|
9
9
|
|
|
10
10
|
/** Field types that are localized by default. */
|
|
11
|
-
const LOCALIZED_TYPES = new Set([
|
|
11
|
+
const LOCALIZED_TYPES = new Set([
|
|
12
|
+
"text",
|
|
13
|
+
"textarea",
|
|
14
|
+
"richtext",
|
|
15
|
+
"email",
|
|
16
|
+
"list",
|
|
17
|
+
]);
|
|
12
18
|
|
|
13
19
|
/** Locales stored by a collection (falls back to the global default). */
|
|
14
20
|
export function collectionLocales(collection: CollectionDef): string[] {
|
|
@@ -92,6 +98,7 @@ export function fieldDefault(field: FieldDef): FieldValue {
|
|
|
92
98
|
case "media-list":
|
|
93
99
|
return [];
|
|
94
100
|
case "list":
|
|
101
|
+
case "object-list":
|
|
95
102
|
return [];
|
|
96
103
|
case "select":
|
|
97
104
|
return field.options?.[0]?.value ?? "";
|
package/src/core/types.ts
CHANGED
|
@@ -24,6 +24,7 @@ export type FieldType =
|
|
|
24
24
|
| "text"
|
|
25
25
|
| "email"
|
|
26
26
|
| "textarea"
|
|
27
|
+
| "richtext"
|
|
27
28
|
| "number"
|
|
28
29
|
| "boolean"
|
|
29
30
|
| "select"
|
|
@@ -31,7 +32,8 @@ export type FieldType =
|
|
|
31
32
|
| "relation"
|
|
32
33
|
| "media"
|
|
33
34
|
| "media-list"
|
|
34
|
-
| "list"
|
|
35
|
+
| "list"
|
|
36
|
+
| "object-list";
|
|
35
37
|
|
|
36
38
|
export type SelectOption = {
|
|
37
39
|
label: string;
|
|
@@ -72,11 +74,18 @@ export type FieldDef = {
|
|
|
72
74
|
accept?: string[];
|
|
73
75
|
/** Only for `media` fields. Maximum upload size in bytes. */
|
|
74
76
|
maxSize?: number;
|
|
77
|
+
/**
|
|
78
|
+
* Only for `object-list` fields. Subfields of each stored object. Supported
|
|
79
|
+
* types: `text`, `email`, `textarea`, `number`, `boolean`, `select`,
|
|
80
|
+
* `date` and `list`. Subfields are never localized individually.
|
|
81
|
+
*/
|
|
82
|
+
fields?: FieldDef[];
|
|
75
83
|
/**
|
|
76
84
|
* Whether the field is stored per locale (`name_pt`, `name_en`). When
|
|
77
85
|
* omitted it defaults to `true` for editorial types (`text`, `textarea`,
|
|
78
|
-
* `email`, `list`) and `false` for structural ones. Set
|
|
79
|
-
* a text field (e.g. `slug`, `icon`) to stay
|
|
86
|
+
* `richtext`, `email`, `list`) and `false` for structural ones. Set
|
|
87
|
+
* explicitly to force a text field (e.g. `slug`, `icon`) to stay
|
|
88
|
+
* language-independent.
|
|
80
89
|
*/
|
|
81
90
|
localized?: boolean;
|
|
82
91
|
defaultValue?: string | number | boolean;
|
|
@@ -147,6 +156,11 @@ export type MediaItem = {
|
|
|
147
156
|
primary?: boolean;
|
|
148
157
|
};
|
|
149
158
|
|
|
159
|
+
/** A single entry of an `object-list` field, keyed by subfield name. */
|
|
160
|
+
export type ObjectItem = {
|
|
161
|
+
[field: string]: FieldValue;
|
|
162
|
+
};
|
|
163
|
+
|
|
150
164
|
/** An ordered list of translated values, keyed by locale code. */
|
|
151
165
|
export type LocalizedValue = {
|
|
152
166
|
[locale: string]: FieldValue;
|
|
@@ -161,6 +175,7 @@ export type FieldValue =
|
|
|
161
175
|
| number[]
|
|
162
176
|
| string[]
|
|
163
177
|
| MediaItem[]
|
|
178
|
+
| ObjectItem[]
|
|
164
179
|
| LocalizedValue;
|
|
165
180
|
|
|
166
181
|
/** An option shown by a relation control and used to render labels. */
|
package/src/core/validation.ts
CHANGED
|
@@ -8,9 +8,11 @@ import { isMediaUrl } from "./media.js";
|
|
|
8
8
|
import type {
|
|
9
9
|
CollectionDef,
|
|
10
10
|
FieldDef,
|
|
11
|
+
FieldType,
|
|
11
12
|
FieldValue,
|
|
12
13
|
LocalizedValue,
|
|
13
14
|
MediaItem,
|
|
15
|
+
ObjectItem,
|
|
14
16
|
} from "./types.js";
|
|
15
17
|
|
|
16
18
|
export type ValidationResult = {
|
|
@@ -21,6 +23,18 @@ export type ValidationResult = {
|
|
|
21
23
|
|
|
22
24
|
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
23
25
|
|
|
26
|
+
/** Types accepted inside an `object-list` item. */
|
|
27
|
+
const OBJECT_SUBFIELD_TYPES = new Set<FieldType>([
|
|
28
|
+
"text",
|
|
29
|
+
"email",
|
|
30
|
+
"textarea",
|
|
31
|
+
"number",
|
|
32
|
+
"boolean",
|
|
33
|
+
"select",
|
|
34
|
+
"date",
|
|
35
|
+
"list",
|
|
36
|
+
]);
|
|
37
|
+
|
|
24
38
|
function asRecord(input: unknown): Record<string, unknown> {
|
|
25
39
|
if (input && typeof input === "object" && !Array.isArray(input)) {
|
|
26
40
|
return input as Record<string, unknown>;
|
|
@@ -146,6 +160,63 @@ function coerceField(
|
|
|
146
160
|
}
|
|
147
161
|
return { value: items };
|
|
148
162
|
}
|
|
163
|
+
case "object-list": {
|
|
164
|
+
const subfields = field.fields ?? [];
|
|
165
|
+
if (subfields.length === 0) {
|
|
166
|
+
return { error: "Campo sem subcampos definidos." };
|
|
167
|
+
}
|
|
168
|
+
const source =
|
|
169
|
+
raw === null || raw === undefined
|
|
170
|
+
? []
|
|
171
|
+
: Array.isArray(raw)
|
|
172
|
+
? raw
|
|
173
|
+
: null;
|
|
174
|
+
if (source === null) {
|
|
175
|
+
return { error: "Envie uma lista de itens." };
|
|
176
|
+
}
|
|
177
|
+
const items: ObjectItem[] = [];
|
|
178
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
179
|
+
const entry = source[index];
|
|
180
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
|
|
181
|
+
return { error: `Item ${index + 1}: valor inválido.` };
|
|
182
|
+
}
|
|
183
|
+
const record = entry as Record<string, unknown>;
|
|
184
|
+
const item: ObjectItem = {};
|
|
185
|
+
for (const subfield of subfields) {
|
|
186
|
+
if (!OBJECT_SUBFIELD_TYPES.has(subfield.type)) {
|
|
187
|
+
return {
|
|
188
|
+
error: `Item ${index + 1} — ${subfield.label}: tipo de subcampo não suportado (${subfield.type}).`,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
const present = Object.prototype.hasOwnProperty.call(
|
|
192
|
+
record,
|
|
193
|
+
subfield.name,
|
|
194
|
+
);
|
|
195
|
+
const result = coerceField(
|
|
196
|
+
subfield,
|
|
197
|
+
present ? record[subfield.name] : fieldDefault(subfield),
|
|
198
|
+
);
|
|
199
|
+
if ("error" in result) {
|
|
200
|
+
return {
|
|
201
|
+
error: `Item ${index + 1} — ${subfield.label}: ${result.error}`,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
item[subfield.name] = result.value;
|
|
205
|
+
}
|
|
206
|
+
items.push(item);
|
|
207
|
+
}
|
|
208
|
+
if (field.required && items.length === 0) {
|
|
209
|
+
return { error: "Adicione ao menos um item." };
|
|
210
|
+
}
|
|
211
|
+
return { value: items };
|
|
212
|
+
}
|
|
213
|
+
case "richtext": {
|
|
214
|
+
const value = raw === null || raw === undefined ? "" : String(raw);
|
|
215
|
+
if (!value.trim() && field.required) {
|
|
216
|
+
return { error: "Campo obrigatório." };
|
|
217
|
+
}
|
|
218
|
+
return { value };
|
|
219
|
+
}
|
|
149
220
|
case "email": {
|
|
150
221
|
const value = raw === null || raw === undefined ? "" : String(raw).trim();
|
|
151
222
|
if (!value) {
|
package/src/ui/admin/display.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import type { ReactNode } from "react";
|
|
|
2
2
|
|
|
3
3
|
import { relationIsMany } from "../../core/schema.js";
|
|
4
4
|
import { isImageUrl } from "../../core/media.js";
|
|
5
|
-
import type { FieldDef, FieldValue, MediaItem, RelationOption } from "../../core/types.js";
|
|
5
|
+
import type { FieldDef, FieldValue, MediaItem, ObjectItem, RelationOption } from "../../core/types.js";
|
|
6
6
|
import { Badge } from "../badge.js";
|
|
7
7
|
import { Box } from "../box.js";
|
|
8
8
|
import { Text } from "../text.js";
|
|
@@ -75,6 +75,18 @@ export function renderFieldValue(
|
|
|
75
75
|
);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
if (field.type === "object-list") {
|
|
79
|
+
const items = Array.isArray(value) ? (value as ObjectItem[]) : [];
|
|
80
|
+
if (items.length === 0) {
|
|
81
|
+
return <Text as="span" size="caption" className="text-gray-300">—</Text>;
|
|
82
|
+
}
|
|
83
|
+
return (
|
|
84
|
+
<Badge tone="gray">
|
|
85
|
+
{translate(locale, "list.count", { count: items.length })}
|
|
86
|
+
</Badge>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
78
90
|
if (field.type === "media-list") {
|
|
79
91
|
const items = Array.isArray(value) ? (value as MediaItem[]) : [];
|
|
80
92
|
if (items.length === 0) {
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import type { FieldDef, FieldValue, MediaItem, RelationOption } from "../../core/types.js";
|
|
1
|
+
import type { FieldDef, FieldValue, MediaItem, ObjectItem, RelationOption } from "../../core/types.js";
|
|
2
2
|
import { Box } from "../box.js";
|
|
3
3
|
import { FieldWrapper, inputClass } from "../field-wrapper.js";
|
|
4
4
|
import { Text } from "../text.js";
|
|
5
5
|
import { MediaControl } from "./media-control.js";
|
|
6
6
|
import { MediaListControl } from "./media-list-control.js";
|
|
7
7
|
import { ListControl } from "./list-control.js";
|
|
8
|
+
import { ObjectListControl } from "./object-list-control.js";
|
|
9
|
+
import { RichTextControl } from "./rich-text-control.js";
|
|
8
10
|
import { RelationControl } from "./relation-control.js";
|
|
9
11
|
import { useI18n } from "./i18n.js";
|
|
10
12
|
|
|
@@ -15,6 +17,8 @@ export type FieldControlProps = {
|
|
|
15
17
|
error?: string | undefined;
|
|
16
18
|
disabled?: boolean | undefined;
|
|
17
19
|
relationOptions?: RelationOption[] | undefined;
|
|
20
|
+
/** Optional prefix so nested/subfield controls get unique DOM ids. */
|
|
21
|
+
idPrefix?: string | undefined;
|
|
18
22
|
onChange: (value: FieldValue) => void;
|
|
19
23
|
};
|
|
20
24
|
|
|
@@ -28,10 +32,11 @@ export const FieldControl = ({
|
|
|
28
32
|
error,
|
|
29
33
|
disabled,
|
|
30
34
|
relationOptions,
|
|
35
|
+
idPrefix,
|
|
31
36
|
onChange,
|
|
32
37
|
}: FieldControlProps) => {
|
|
33
38
|
const { t } = useI18n();
|
|
34
|
-
const id =
|
|
39
|
+
const id = `${idPrefix ?? ""}field-${field.name}`;
|
|
35
40
|
|
|
36
41
|
if (field.type === "media") {
|
|
37
42
|
return (
|
|
@@ -71,6 +76,42 @@ export const FieldControl = ({
|
|
|
71
76
|
);
|
|
72
77
|
}
|
|
73
78
|
|
|
79
|
+
if (field.type === "richtext") {
|
|
80
|
+
return (
|
|
81
|
+
<RichTextControl
|
|
82
|
+
field={field}
|
|
83
|
+
id={id}
|
|
84
|
+
value={String(value ?? "")}
|
|
85
|
+
error={error}
|
|
86
|
+
disabled={disabled}
|
|
87
|
+
onChange={onChange}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (field.type === "object-list") {
|
|
93
|
+
return (
|
|
94
|
+
<ObjectListControl
|
|
95
|
+
field={field}
|
|
96
|
+
value={Array.isArray(value) ? (value as ObjectItem[]) : []}
|
|
97
|
+
error={error}
|
|
98
|
+
disabled={disabled}
|
|
99
|
+
onChange={onChange}
|
|
100
|
+
renderField={(subfield, subvalue, onSubChange, subPrefix) => (
|
|
101
|
+
<FieldControl
|
|
102
|
+
key={subfield.name}
|
|
103
|
+
field={subfield}
|
|
104
|
+
collectionSlug={collectionSlug}
|
|
105
|
+
value={subvalue}
|
|
106
|
+
disabled={disabled}
|
|
107
|
+
idPrefix={subPrefix}
|
|
108
|
+
onChange={onSubChange}
|
|
109
|
+
/>
|
|
110
|
+
)}
|
|
111
|
+
/>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
74
115
|
if (field.type === "relation") {
|
|
75
116
|
return (
|
|
76
117
|
<RelationControl
|
package/src/ui/admin/i18n.tsx
CHANGED
|
@@ -120,11 +120,25 @@ const en: Dictionary = {
|
|
|
120
120
|
|
|
121
121
|
"list.empty": "No items.",
|
|
122
122
|
"list.item": "Item {index}",
|
|
123
|
+
"list.count": "{count} item(s)",
|
|
123
124
|
"list.moveUp": "Move up",
|
|
124
125
|
"list.moveDown": "Move down",
|
|
125
126
|
"list.remove": "Remove",
|
|
126
127
|
"list.add": "Add item",
|
|
127
128
|
|
|
129
|
+
"richtext.bold": "Bold",
|
|
130
|
+
"richtext.header": "Header",
|
|
131
|
+
"richtext.italic": "Italic",
|
|
132
|
+
"richtext.quote": "Quote",
|
|
133
|
+
"richtext.link": "Link",
|
|
134
|
+
"richtext.unorderedList": "Bullet list",
|
|
135
|
+
"richtext.orderedList": "Numbered list",
|
|
136
|
+
"richtext.taskList": "Task list",
|
|
137
|
+
"richtext.write": "Write",
|
|
138
|
+
"richtext.preview": "Preview",
|
|
139
|
+
"richtext.tabs": "Editor mode",
|
|
140
|
+
"richtext.previewEmpty": "The preview appears as you type.",
|
|
141
|
+
|
|
128
142
|
"pagination.range": "{from}–{to} of {total} items",
|
|
129
143
|
"pagination.previous": "Previous",
|
|
130
144
|
"pagination.next": "Next",
|
|
@@ -275,11 +289,25 @@ const pt: Dictionary = {
|
|
|
275
289
|
|
|
276
290
|
"list.empty": "Nenhum item.",
|
|
277
291
|
"list.item": "Item {index}",
|
|
292
|
+
"list.count": "{count} item(ns)",
|
|
278
293
|
"list.moveUp": "Mover para cima",
|
|
279
294
|
"list.moveDown": "Mover para baixo",
|
|
280
295
|
"list.remove": "Remover",
|
|
281
296
|
"list.add": "Adicionar item",
|
|
282
297
|
|
|
298
|
+
"richtext.bold": "Negrito",
|
|
299
|
+
"richtext.header": "Título",
|
|
300
|
+
"richtext.italic": "Itálico",
|
|
301
|
+
"richtext.quote": "Citação",
|
|
302
|
+
"richtext.link": "Link",
|
|
303
|
+
"richtext.unorderedList": "Lista com marcadores",
|
|
304
|
+
"richtext.orderedList": "Lista numerada",
|
|
305
|
+
"richtext.taskList": "Lista de tarefas",
|
|
306
|
+
"richtext.write": "Escrever",
|
|
307
|
+
"richtext.preview": "Prévia",
|
|
308
|
+
"richtext.tabs": "Modo do editor",
|
|
309
|
+
"richtext.previewEmpty": "A prévia aparece enquanto você digita.",
|
|
310
|
+
|
|
283
311
|
"pagination.range": "{from}–{to} de {total} itens",
|
|
284
312
|
"pagination.previous": "Anterior",
|
|
285
313
|
"pagination.next": "Próxima",
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
import { FiArrowDown, FiArrowUp, FiPlus, FiTrash2 } from "react-icons/fi";
|
|
6
|
+
|
|
7
|
+
import { fieldDefault } from "../../core/schema.js";
|
|
8
|
+
import type { FieldDef, FieldValue, ObjectItem } from "../../core/types.js";
|
|
9
|
+
import { Box } from "../box.js";
|
|
10
|
+
import { Button, IconButton } from "../button.js";
|
|
11
|
+
import { FieldWrapper } from "../field-wrapper.js";
|
|
12
|
+
import { Text } from "../text.js";
|
|
13
|
+
import { useI18n } from "./i18n.js";
|
|
14
|
+
|
|
15
|
+
export type ObjectListControlProps = {
|
|
16
|
+
field: FieldDef;
|
|
17
|
+
value: ObjectItem[];
|
|
18
|
+
error?: string | undefined;
|
|
19
|
+
disabled?: boolean | undefined;
|
|
20
|
+
onChange: (value: ObjectItem[]) => void;
|
|
21
|
+
renderField: (
|
|
22
|
+
field: FieldDef,
|
|
23
|
+
value: FieldValue,
|
|
24
|
+
onChange: (value: FieldValue) => void,
|
|
25
|
+
idPrefix: string,
|
|
26
|
+
) => ReactNode;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function emptyItem(field: FieldDef): ObjectItem {
|
|
30
|
+
const item: ObjectItem = {};
|
|
31
|
+
for (const subfield of field.fields ?? []) {
|
|
32
|
+
item[subfield.name] = fieldDefault(subfield);
|
|
33
|
+
}
|
|
34
|
+
return item;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Repeater for `object-list` fields: each item is an object rendered from its
|
|
39
|
+
* subfield descriptors, with add/remove/reorder controls.
|
|
40
|
+
*/
|
|
41
|
+
export const ObjectListControl = ({
|
|
42
|
+
field,
|
|
43
|
+
value,
|
|
44
|
+
error,
|
|
45
|
+
disabled,
|
|
46
|
+
onChange,
|
|
47
|
+
renderField,
|
|
48
|
+
}: ObjectListControlProps) => {
|
|
49
|
+
const { t } = useI18n();
|
|
50
|
+
const subfields = field.fields ?? [];
|
|
51
|
+
|
|
52
|
+
const update = (index: number, name: string, next: FieldValue) => {
|
|
53
|
+
onChange(
|
|
54
|
+
value.map((item, i) =>
|
|
55
|
+
i === index ? { ...item, [name]: next } : item,
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const remove = (index: number) =>
|
|
61
|
+
onChange(value.filter((_, i) => i !== index));
|
|
62
|
+
|
|
63
|
+
const move = (index: number, direction: -1 | 1) => {
|
|
64
|
+
const target = index + direction;
|
|
65
|
+
if (target < 0 || target >= value.length) return;
|
|
66
|
+
const next = [...value];
|
|
67
|
+
const current = next[index];
|
|
68
|
+
const swap = next[target];
|
|
69
|
+
if (current === undefined || swap === undefined) return;
|
|
70
|
+
next[index] = swap;
|
|
71
|
+
next[target] = current;
|
|
72
|
+
onChange(next);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<FieldWrapper
|
|
77
|
+
label={field.label}
|
|
78
|
+
required={field.required}
|
|
79
|
+
help={field.help}
|
|
80
|
+
error={error}
|
|
81
|
+
>
|
|
82
|
+
<Box className="flex-col gap-3">
|
|
83
|
+
{value.length === 0 ? (
|
|
84
|
+
<Text size="caption" className="text-gray-400">
|
|
85
|
+
{t("list.empty")}
|
|
86
|
+
</Text>
|
|
87
|
+
) : null}
|
|
88
|
+
|
|
89
|
+
{value.map((item, index) => (
|
|
90
|
+
<Box
|
|
91
|
+
key={index}
|
|
92
|
+
className="flex-col gap-3 rounded-md border border-gray-200 bg-gray-50 p-3"
|
|
93
|
+
>
|
|
94
|
+
<Box className="items-center justify-between gap-2">
|
|
95
|
+
<Text
|
|
96
|
+
size="caption"
|
|
97
|
+
className="font-semibold uppercase tracking-wide text-gray-500"
|
|
98
|
+
>
|
|
99
|
+
{t("list.item", { index: index + 1 })}
|
|
100
|
+
</Text>
|
|
101
|
+
<Box className="items-center gap-1">
|
|
102
|
+
<IconButton
|
|
103
|
+
type="button"
|
|
104
|
+
label={t("list.moveUp")}
|
|
105
|
+
disabled={disabled || index === 0}
|
|
106
|
+
onClick={() => move(index, -1)}
|
|
107
|
+
>
|
|
108
|
+
<FiArrowUp aria-hidden />
|
|
109
|
+
</IconButton>
|
|
110
|
+
<IconButton
|
|
111
|
+
type="button"
|
|
112
|
+
label={t("list.moveDown")}
|
|
113
|
+
disabled={disabled || index === value.length - 1}
|
|
114
|
+
onClick={() => move(index, 1)}
|
|
115
|
+
>
|
|
116
|
+
<FiArrowDown aria-hidden />
|
|
117
|
+
</IconButton>
|
|
118
|
+
<IconButton
|
|
119
|
+
type="button"
|
|
120
|
+
label={t("list.remove")}
|
|
121
|
+
className="text-red-600 hover:bg-red-50 hover:text-red-700"
|
|
122
|
+
disabled={disabled}
|
|
123
|
+
onClick={() => remove(index)}
|
|
124
|
+
>
|
|
125
|
+
<FiTrash2 aria-hidden />
|
|
126
|
+
</IconButton>
|
|
127
|
+
</Box>
|
|
128
|
+
</Box>
|
|
129
|
+
|
|
130
|
+
{subfields.map((subfield) =>
|
|
131
|
+
renderField(
|
|
132
|
+
subfield,
|
|
133
|
+
item[subfield.name] ?? null,
|
|
134
|
+
(next) => update(index, subfield.name, next),
|
|
135
|
+
`${field.name}-${index}-`,
|
|
136
|
+
),
|
|
137
|
+
)}
|
|
138
|
+
</Box>
|
|
139
|
+
))}
|
|
140
|
+
|
|
141
|
+
<Button
|
|
142
|
+
type="button"
|
|
143
|
+
variant="secondary"
|
|
144
|
+
size="sm"
|
|
145
|
+
disabled={disabled}
|
|
146
|
+
onClick={() => onChange([...value, emptyItem(field)])}
|
|
147
|
+
>
|
|
148
|
+
<FiPlus aria-hidden className="mr-1.5" />
|
|
149
|
+
{t("list.add")}
|
|
150
|
+
</Button>
|
|
151
|
+
</Box>
|
|
152
|
+
</FieldWrapper>
|
|
153
|
+
);
|
|
154
|
+
};
|