@eutobias/serenojs 0.1.1 → 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 +30 -11
- 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 +9 -2
- package/dist/config.js +1 -0
- package/dist/core/backup.server.d.ts +5 -2
- package/dist/core/backup.server.js +346 -12
- package/dist/core/db.server.js +29 -1
- package/dist/core/schema-codegen.server.d.ts +17 -0
- package/dist/core/schema-codegen.server.js +155 -0
- 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 +30 -4
- 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 +6 -1
- package/src/core/backup.server.ts +435 -17
- package/src/core/db.server.ts +34 -1
- package/src/core/schema-codegen.server.ts +187 -0
- 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 +32 -4
- 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-B3EgqtT6.js +0 -9
- package/admin-dist/assets/index-D2m0Eb4k.css +0 -2
package/dist/core/types.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type CollectionIconName = "archive" | "box" | "fileText" | "folder" | "gr
|
|
|
6
6
|
* The whole admin panel is generated from the collection descriptors in
|
|
7
7
|
* `src/schemas`. These types describe those descriptors.
|
|
8
8
|
*/
|
|
9
|
-
export type FieldType = "text" | "email" | "textarea" | "number" | "boolean" | "select" | "date" | "relation" | "media" | "media-list" | "list";
|
|
9
|
+
export type FieldType = "text" | "email" | "textarea" | "richtext" | "number" | "boolean" | "select" | "date" | "relation" | "media" | "media-list" | "list" | "object-list";
|
|
10
10
|
export type SelectOption = {
|
|
11
11
|
label: string;
|
|
12
12
|
value: string;
|
|
@@ -40,11 +40,18 @@ export type FieldDef = {
|
|
|
40
40
|
accept?: string[];
|
|
41
41
|
/** Only for `media` fields. Maximum upload size in bytes. */
|
|
42
42
|
maxSize?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Only for `object-list` fields. Subfields of each stored object. Supported
|
|
45
|
+
* types: `text`, `email`, `textarea`, `number`, `boolean`, `select`,
|
|
46
|
+
* `date` and `list`. Subfields are never localized individually.
|
|
47
|
+
*/
|
|
48
|
+
fields?: FieldDef[];
|
|
43
49
|
/**
|
|
44
50
|
* Whether the field is stored per locale (`name_pt`, `name_en`). When
|
|
45
51
|
* omitted it defaults to `true` for editorial types (`text`, `textarea`,
|
|
46
|
-
* `email`, `list`) and `false` for structural ones. Set
|
|
47
|
-
* a text field (e.g. `slug`, `icon`) to stay
|
|
52
|
+
* `richtext`, `email`, `list`) and `false` for structural ones. Set
|
|
53
|
+
* explicitly to force a text field (e.g. `slug`, `icon`) to stay
|
|
54
|
+
* language-independent.
|
|
48
55
|
*/
|
|
49
56
|
localized?: boolean;
|
|
50
57
|
defaultValue?: string | number | boolean;
|
|
@@ -110,12 +117,16 @@ export type MediaItem = {
|
|
|
110
117
|
/** At most one item per list is the principal (cover/highlight). */
|
|
111
118
|
primary?: boolean;
|
|
112
119
|
};
|
|
120
|
+
/** A single entry of an `object-list` field, keyed by subfield name. */
|
|
121
|
+
export type ObjectItem = {
|
|
122
|
+
[field: string]: FieldValue;
|
|
123
|
+
};
|
|
113
124
|
/** An ordered list of translated values, keyed by locale code. */
|
|
114
125
|
export type LocalizedValue = {
|
|
115
126
|
[locale: string]: FieldValue;
|
|
116
127
|
};
|
|
117
128
|
/** Primitive value a field can hold. Relation values are numeric ids. */
|
|
118
|
-
export type FieldValue = string | number | boolean | null | number[] | string[] | MediaItem[] | LocalizedValue;
|
|
129
|
+
export type FieldValue = string | number | boolean | null | number[] | string[] | MediaItem[] | ObjectItem[] | LocalizedValue;
|
|
119
130
|
/** An option shown by a relation control and used to render labels. */
|
|
120
131
|
export type RelationOption = {
|
|
121
132
|
id: number;
|
package/dist/core/validation.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { collectionLocales, fieldDefault, isLocalizedField, relationIsMany, } from "./schema.js";
|
|
2
2
|
import { isMediaUrl } from "./media.js";
|
|
3
3
|
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
4
|
+
/** Types accepted inside an `object-list` item. */
|
|
5
|
+
const OBJECT_SUBFIELD_TYPES = new Set([
|
|
6
|
+
"text",
|
|
7
|
+
"email",
|
|
8
|
+
"textarea",
|
|
9
|
+
"number",
|
|
10
|
+
"boolean",
|
|
11
|
+
"select",
|
|
12
|
+
"date",
|
|
13
|
+
"list",
|
|
14
|
+
]);
|
|
4
15
|
function asRecord(input) {
|
|
5
16
|
if (input && typeof input === "object" && !Array.isArray(input)) {
|
|
6
17
|
return input;
|
|
@@ -125,6 +136,56 @@ function coerceField(field, raw) {
|
|
|
125
136
|
}
|
|
126
137
|
return { value: items };
|
|
127
138
|
}
|
|
139
|
+
case "object-list": {
|
|
140
|
+
const subfields = field.fields ?? [];
|
|
141
|
+
if (subfields.length === 0) {
|
|
142
|
+
return { error: "Campo sem subcampos definidos." };
|
|
143
|
+
}
|
|
144
|
+
const source = raw === null || raw === undefined
|
|
145
|
+
? []
|
|
146
|
+
: Array.isArray(raw)
|
|
147
|
+
? raw
|
|
148
|
+
: null;
|
|
149
|
+
if (source === null) {
|
|
150
|
+
return { error: "Envie uma lista de itens." };
|
|
151
|
+
}
|
|
152
|
+
const items = [];
|
|
153
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
154
|
+
const entry = source[index];
|
|
155
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
|
|
156
|
+
return { error: `Item ${index + 1}: valor inválido.` };
|
|
157
|
+
}
|
|
158
|
+
const record = entry;
|
|
159
|
+
const item = {};
|
|
160
|
+
for (const subfield of subfields) {
|
|
161
|
+
if (!OBJECT_SUBFIELD_TYPES.has(subfield.type)) {
|
|
162
|
+
return {
|
|
163
|
+
error: `Item ${index + 1} — ${subfield.label}: tipo de subcampo não suportado (${subfield.type}).`,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const present = Object.prototype.hasOwnProperty.call(record, subfield.name);
|
|
167
|
+
const result = coerceField(subfield, present ? record[subfield.name] : fieldDefault(subfield));
|
|
168
|
+
if ("error" in result) {
|
|
169
|
+
return {
|
|
170
|
+
error: `Item ${index + 1} — ${subfield.label}: ${result.error}`,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
item[subfield.name] = result.value;
|
|
174
|
+
}
|
|
175
|
+
items.push(item);
|
|
176
|
+
}
|
|
177
|
+
if (field.required && items.length === 0) {
|
|
178
|
+
return { error: "Adicione ao menos um item." };
|
|
179
|
+
}
|
|
180
|
+
return { value: items };
|
|
181
|
+
}
|
|
182
|
+
case "richtext": {
|
|
183
|
+
const value = raw === null || raw === undefined ? "" : String(raw);
|
|
184
|
+
if (!value.trim() && field.required) {
|
|
185
|
+
return { error: "Campo obrigatório." };
|
|
186
|
+
}
|
|
187
|
+
return { value };
|
|
188
|
+
}
|
|
128
189
|
case "email": {
|
|
129
190
|
const value = raw === null || raw === undefined ? "" : String(raw).trim();
|
|
130
191
|
if (!value) {
|
|
@@ -145,7 +145,7 @@ export const CollectionManager = ({ collection, initial, relationOptions, }) =>
|
|
|
145
145
|
const fieldColumn = (field) => ({
|
|
146
146
|
key: field.name,
|
|
147
147
|
header: field.label,
|
|
148
|
-
render: (row) => renderFieldValue(field, valueForLocale(collection, field, row), relationOptions[field.name] ?? []),
|
|
148
|
+
render: (row) => renderFieldValue(field, valueForLocale(collection, field, row), relationOptions[field.name] ?? [], locale),
|
|
149
149
|
});
|
|
150
150
|
const idColumn = {
|
|
151
151
|
key: "id",
|
package/dist/ui/admin/display.js
CHANGED
|
@@ -37,6 +37,13 @@ export function renderFieldValue(field, value, options = [], locale = "en") {
|
|
|
37
37
|
}
|
|
38
38
|
return (_jsxs(Box, { className: "flex-wrap items-center gap-1", children: [items.slice(0, 2).map((item, index) => (_jsx(Badge, { tone: "gray", children: item }, `${item}-${index}`))), items.length > 2 ? _jsxs(Badge, { tone: "gray", children: ["+", items.length - 2] }) : null] }));
|
|
39
39
|
}
|
|
40
|
+
if (field.type === "object-list") {
|
|
41
|
+
const items = Array.isArray(value) ? value : [];
|
|
42
|
+
if (items.length === 0) {
|
|
43
|
+
return _jsx(Text, { as: "span", size: "caption", className: "text-gray-300", children: "\u2014" });
|
|
44
|
+
}
|
|
45
|
+
return (_jsx(Badge, { tone: "gray", children: translate(locale, "list.count", { count: items.length }) }));
|
|
46
|
+
}
|
|
40
47
|
if (field.type === "media-list") {
|
|
41
48
|
const items = Array.isArray(value) ? value : [];
|
|
42
49
|
if (items.length === 0) {
|
|
@@ -6,9 +6,11 @@ export type FieldControlProps = {
|
|
|
6
6
|
error?: string | undefined;
|
|
7
7
|
disabled?: boolean | undefined;
|
|
8
8
|
relationOptions?: RelationOption[] | undefined;
|
|
9
|
+
/** Optional prefix so nested/subfield controls get unique DOM ids. */
|
|
10
|
+
idPrefix?: string | undefined;
|
|
9
11
|
onChange: (value: FieldValue) => void;
|
|
10
12
|
};
|
|
11
13
|
/**
|
|
12
14
|
* Renders a single descriptor field as the right input control.
|
|
13
15
|
*/
|
|
14
|
-
export declare const FieldControl: ({ field, collectionSlug, value, error, disabled, relationOptions, onChange, }: FieldControlProps) => import("react").JSX.Element;
|
|
16
|
+
export declare const FieldControl: ({ field, collectionSlug, value, error, disabled, relationOptions, idPrefix, onChange, }: FieldControlProps) => import("react").JSX.Element;
|
|
@@ -5,14 +5,16 @@ 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
|
/**
|
|
11
13
|
* Renders a single descriptor field as the right input control.
|
|
12
14
|
*/
|
|
13
|
-
export const FieldControl = ({ field, collectionSlug, value, error, disabled, relationOptions, onChange, }) => {
|
|
15
|
+
export const FieldControl = ({ field, collectionSlug, value, error, disabled, relationOptions, idPrefix, onChange, }) => {
|
|
14
16
|
const { t } = useI18n();
|
|
15
|
-
const id =
|
|
17
|
+
const id = `${idPrefix ?? ""}field-${field.name}`;
|
|
16
18
|
if (field.type === "media") {
|
|
17
19
|
return (_jsx(MediaControl, { field: field, collectionSlug: collectionSlug, value: typeof value === "string" ? value : "", error: error, disabled: disabled, onChange: onChange }));
|
|
18
20
|
}
|
|
@@ -22,6 +24,12 @@ export const FieldControl = ({ field, collectionSlug, value, error, disabled, re
|
|
|
22
24
|
if (field.type === "list") {
|
|
23
25
|
return (_jsx(ListControl, { field: field, value: Array.isArray(value) ? value : [], error: error, disabled: disabled, onChange: onChange }));
|
|
24
26
|
}
|
|
27
|
+
if (field.type === "richtext") {
|
|
28
|
+
return (_jsx(RichTextControl, { field: field, id: id, value: String(value ?? ""), error: error, disabled: disabled, onChange: onChange }));
|
|
29
|
+
}
|
|
30
|
+
if (field.type === "object-list") {
|
|
31
|
+
return (_jsx(ObjectListControl, { field: field, value: Array.isArray(value) ? value : [], error: error, disabled: disabled, onChange: onChange, renderField: (subfield, subvalue, onSubChange, subPrefix) => (_jsx(FieldControl, { field: subfield, collectionSlug: collectionSlug, value: subvalue, disabled: disabled, idPrefix: subPrefix, onChange: onSubChange }, subfield.name)) }));
|
|
32
|
+
}
|
|
25
33
|
if (field.type === "relation") {
|
|
26
34
|
return (_jsx(RelationControl, { field: field, value: value, options: relationOptions ?? [], error: error, disabled: disabled, onChange: onChange }));
|
|
27
35
|
}
|
package/dist/ui/admin/i18n.js
CHANGED
|
@@ -88,10 +88,23 @@ const en = {
|
|
|
88
88
|
"relation.empty": "No items available to relate.",
|
|
89
89
|
"list.empty": "No items.",
|
|
90
90
|
"list.item": "Item {index}",
|
|
91
|
+
"list.count": "{count} item(s)",
|
|
91
92
|
"list.moveUp": "Move up",
|
|
92
93
|
"list.moveDown": "Move down",
|
|
93
94
|
"list.remove": "Remove",
|
|
94
95
|
"list.add": "Add item",
|
|
96
|
+
"richtext.bold": "Bold",
|
|
97
|
+
"richtext.header": "Header",
|
|
98
|
+
"richtext.italic": "Italic",
|
|
99
|
+
"richtext.quote": "Quote",
|
|
100
|
+
"richtext.link": "Link",
|
|
101
|
+
"richtext.unorderedList": "Bullet list",
|
|
102
|
+
"richtext.orderedList": "Numbered list",
|
|
103
|
+
"richtext.taskList": "Task list",
|
|
104
|
+
"richtext.write": "Write",
|
|
105
|
+
"richtext.preview": "Preview",
|
|
106
|
+
"richtext.tabs": "Editor mode",
|
|
107
|
+
"richtext.previewEmpty": "The preview appears as you type.",
|
|
95
108
|
"pagination.range": "{from}–{to} of {total} items",
|
|
96
109
|
"pagination.previous": "Previous",
|
|
97
110
|
"pagination.next": "Next",
|
|
@@ -118,12 +131,12 @@ const en = {
|
|
|
118
131
|
"users.removeError": "Could not remove the user.",
|
|
119
132
|
"users.networkRemove": "Network error while removing the user.",
|
|
120
133
|
"backup.title": "Backup",
|
|
121
|
-
"backup.subtitle": "Download or restore
|
|
134
|
+
"backup.subtitle": "Download or restore schemas, database and system images.",
|
|
122
135
|
"backup.refresh": "Refresh",
|
|
123
136
|
"backup.downloadCurrent": "Download current",
|
|
124
137
|
"backup.restoreAction": "Restore",
|
|
125
138
|
"backup.create": "Create backup",
|
|
126
|
-
"backup.description": "The backup bundles a consistent SQLite snapshot and every file under public/images and public/files. Restoring replaces the current database and rewrites the images.",
|
|
139
|
+
"backup.description": "The backup bundles the collection schemas, a consistent SQLite snapshot and every file under public/images and public/files. Restoring rewrites the schema modules, replaces the current database and rewrites the images.",
|
|
127
140
|
"backup.file": "File",
|
|
128
141
|
"backup.size": "Size",
|
|
129
142
|
"backup.download": "Download",
|
|
@@ -225,10 +238,23 @@ const pt = {
|
|
|
225
238
|
"relation.empty": "Nenhum item disponível para relacionar.",
|
|
226
239
|
"list.empty": "Nenhum item.",
|
|
227
240
|
"list.item": "Item {index}",
|
|
241
|
+
"list.count": "{count} item(ns)",
|
|
228
242
|
"list.moveUp": "Mover para cima",
|
|
229
243
|
"list.moveDown": "Mover para baixo",
|
|
230
244
|
"list.remove": "Remover",
|
|
231
245
|
"list.add": "Adicionar item",
|
|
246
|
+
"richtext.bold": "Negrito",
|
|
247
|
+
"richtext.header": "Título",
|
|
248
|
+
"richtext.italic": "Itálico",
|
|
249
|
+
"richtext.quote": "Citação",
|
|
250
|
+
"richtext.link": "Link",
|
|
251
|
+
"richtext.unorderedList": "Lista com marcadores",
|
|
252
|
+
"richtext.orderedList": "Lista numerada",
|
|
253
|
+
"richtext.taskList": "Lista de tarefas",
|
|
254
|
+
"richtext.write": "Escrever",
|
|
255
|
+
"richtext.preview": "Prévia",
|
|
256
|
+
"richtext.tabs": "Modo do editor",
|
|
257
|
+
"richtext.previewEmpty": "A prévia aparece enquanto você digita.",
|
|
232
258
|
"pagination.range": "{from}–{to} de {total} itens",
|
|
233
259
|
"pagination.previous": "Anterior",
|
|
234
260
|
"pagination.next": "Próxima",
|
|
@@ -255,12 +281,12 @@ const pt = {
|
|
|
255
281
|
"users.removeError": "Não foi possível remover o usuário.",
|
|
256
282
|
"users.networkRemove": "Erro de rede ao remover o usuário.",
|
|
257
283
|
"backup.title": "Backup",
|
|
258
|
-
"backup.subtitle": "Baixe ou restaure o banco de dados e as imagens do sistema.",
|
|
284
|
+
"backup.subtitle": "Baixe ou restaure os schemas, o banco de dados e as imagens do sistema.",
|
|
259
285
|
"backup.refresh": "Atualizar",
|
|
260
286
|
"backup.downloadCurrent": "Baixar atual",
|
|
261
287
|
"backup.restoreAction": "Restaurar",
|
|
262
288
|
"backup.create": "Criar backup",
|
|
263
|
-
"backup.description": "O backup reúne um snapshot consistente do banco SQLite e todos os arquivos de public/images e public/files. Restaurar substitui o banco atual e regrava as imagens.",
|
|
289
|
+
"backup.description": "O backup reúne os schemas das coleções, um snapshot consistente do banco SQLite e todos os arquivos de public/images e public/files. Restaurar regrava os módulos de schema, substitui o banco atual e regrava as imagens.",
|
|
264
290
|
"backup.file": "Arquivo",
|
|
265
291
|
"backup.size": "Tamanho",
|
|
266
292
|
"backup.download": "Baixar",
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { FieldDef, FieldValue, ObjectItem } from "../../core/types.js";
|
|
3
|
+
export type ObjectListControlProps = {
|
|
4
|
+
field: FieldDef;
|
|
5
|
+
value: ObjectItem[];
|
|
6
|
+
error?: string | undefined;
|
|
7
|
+
disabled?: boolean | undefined;
|
|
8
|
+
onChange: (value: ObjectItem[]) => void;
|
|
9
|
+
renderField: (field: FieldDef, value: FieldValue, onChange: (value: FieldValue) => void, idPrefix: string) => ReactNode;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Repeater for `object-list` fields: each item is an object rendered from its
|
|
13
|
+
* subfield descriptors, with add/remove/reorder controls.
|
|
14
|
+
*/
|
|
15
|
+
export declare const ObjectListControl: ({ field, value, error, disabled, onChange, renderField, }: ObjectListControlProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { FiArrowDown, FiArrowUp, FiPlus, FiTrash2 } from "react-icons/fi";
|
|
4
|
+
import { fieldDefault } from "../../core/schema.js";
|
|
5
|
+
import { Box } from "../box.js";
|
|
6
|
+
import { Button, IconButton } from "../button.js";
|
|
7
|
+
import { FieldWrapper } from "../field-wrapper.js";
|
|
8
|
+
import { Text } from "../text.js";
|
|
9
|
+
import { useI18n } from "./i18n.js";
|
|
10
|
+
function emptyItem(field) {
|
|
11
|
+
const item = {};
|
|
12
|
+
for (const subfield of field.fields ?? []) {
|
|
13
|
+
item[subfield.name] = fieldDefault(subfield);
|
|
14
|
+
}
|
|
15
|
+
return item;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Repeater for `object-list` fields: each item is an object rendered from its
|
|
19
|
+
* subfield descriptors, with add/remove/reorder controls.
|
|
20
|
+
*/
|
|
21
|
+
export const ObjectListControl = ({ field, value, error, disabled, onChange, renderField, }) => {
|
|
22
|
+
const { t } = useI18n();
|
|
23
|
+
const subfields = field.fields ?? [];
|
|
24
|
+
const update = (index, name, next) => {
|
|
25
|
+
onChange(value.map((item, i) => i === index ? { ...item, [name]: next } : item));
|
|
26
|
+
};
|
|
27
|
+
const remove = (index) => onChange(value.filter((_, i) => i !== index));
|
|
28
|
+
const move = (index, direction) => {
|
|
29
|
+
const target = index + direction;
|
|
30
|
+
if (target < 0 || target >= value.length)
|
|
31
|
+
return;
|
|
32
|
+
const next = [...value];
|
|
33
|
+
const current = next[index];
|
|
34
|
+
const swap = next[target];
|
|
35
|
+
if (current === undefined || swap === undefined)
|
|
36
|
+
return;
|
|
37
|
+
next[index] = swap;
|
|
38
|
+
next[target] = current;
|
|
39
|
+
onChange(next);
|
|
40
|
+
};
|
|
41
|
+
return (_jsx(FieldWrapper, { label: field.label, required: field.required, help: field.help, error: error, children: _jsxs(Box, { className: "flex-col gap-3", children: [value.length === 0 ? (_jsx(Text, { size: "caption", className: "text-gray-400", children: t("list.empty") })) : null, value.map((item, index) => (_jsxs(Box, { className: "flex-col gap-3 rounded-md border border-gray-200 bg-gray-50 p-3", children: [_jsxs(Box, { className: "items-center justify-between gap-2", children: [_jsx(Text, { size: "caption", className: "font-semibold uppercase tracking-wide text-gray-500", children: t("list.item", { index: index + 1 }) }), _jsxs(Box, { className: "items-center gap-1", children: [_jsx(IconButton, { type: "button", label: t("list.moveUp"), disabled: disabled || index === 0, onClick: () => move(index, -1), children: _jsx(FiArrowUp, { "aria-hidden": true }) }), _jsx(IconButton, { type: "button", label: t("list.moveDown"), disabled: disabled || index === value.length - 1, onClick: () => move(index, 1), children: _jsx(FiArrowDown, { "aria-hidden": true }) }), _jsx(IconButton, { type: "button", label: t("list.remove"), className: "text-red-600 hover:bg-red-50 hover:text-red-700", disabled: disabled, onClick: () => remove(index), children: _jsx(FiTrash2, { "aria-hidden": true }) })] })] }), subfields.map((subfield) => renderField(subfield, item[subfield.name] ?? null, (next) => update(index, subfield.name, next), `${field.name}-${index}-`))] }, index))), _jsxs(Button, { type: "button", variant: "secondary", size: "sm", disabled: disabled, onClick: () => onChange([...value, emptyItem(field)]), children: [_jsx(FiPlus, { "aria-hidden": true, className: "mr-1.5" }), t("list.add")] })] }) }));
|
|
42
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import "@github/markdown-toolbar-element";
|
|
2
|
+
import type { FieldDef } from "../../core/types.js";
|
|
3
|
+
export type RichTextControlProps = {
|
|
4
|
+
field: FieldDef;
|
|
5
|
+
id: string;
|
|
6
|
+
value: string;
|
|
7
|
+
error?: string | undefined;
|
|
8
|
+
disabled?: boolean | undefined;
|
|
9
|
+
onChange: (value: string) => void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Markdown editor with Write/Preview tabs and the GitHub markdown-toolbar
|
|
13
|
+
* custom elements on the right.
|
|
14
|
+
*/
|
|
15
|
+
export declare const RichTextControl: ({ field, id, value, error, disabled, onChange, }: RichTextControlProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import "@github/markdown-toolbar-element";
|
|
4
|
+
import { createElement, useEffect, useRef, useState } from "react";
|
|
5
|
+
import { FieldWrapper } from "../field-wrapper.js";
|
|
6
|
+
import { MarkdownPreview } from "../markdown-preview.js";
|
|
7
|
+
import { Text } from "../text.js";
|
|
8
|
+
import { BoldIcon, HeaderIcon, ItalicIcon, LinkIcon, OrderedListIcon, QuoteIcon, TaskListIcon, UnorderedListIcon, } from "./rich-text-icons.js";
|
|
9
|
+
import { useI18n } from "./i18n.js";
|
|
10
|
+
const TOOLBAR_BUTTONS = [
|
|
11
|
+
{ tag: "md-header", label: "richtext.header", icon: HeaderIcon },
|
|
12
|
+
{ tag: "md-bold", label: "richtext.bold", icon: BoldIcon },
|
|
13
|
+
{ tag: "md-italic", label: "richtext.italic", icon: ItalicIcon },
|
|
14
|
+
{ tag: "md-quote", label: "richtext.quote", icon: QuoteIcon },
|
|
15
|
+
{ tag: "md-link", label: "richtext.link", icon: LinkIcon },
|
|
16
|
+
{
|
|
17
|
+
tag: "md-unordered-list",
|
|
18
|
+
label: "richtext.unorderedList",
|
|
19
|
+
icon: UnorderedListIcon,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
tag: "md-ordered-list",
|
|
23
|
+
label: "richtext.orderedList",
|
|
24
|
+
icon: OrderedListIcon,
|
|
25
|
+
},
|
|
26
|
+
{ tag: "md-task-list", label: "richtext.taskList", icon: TaskListIcon },
|
|
27
|
+
];
|
|
28
|
+
/**
|
|
29
|
+
* Markdown editor with Write/Preview tabs and the GitHub markdown-toolbar
|
|
30
|
+
* custom elements on the right.
|
|
31
|
+
*/
|
|
32
|
+
export const RichTextControl = ({ field, id, value, error, disabled, onChange, }) => {
|
|
33
|
+
const { t } = useI18n();
|
|
34
|
+
const [tab, setTab] = useState("write");
|
|
35
|
+
const toolbarRef = useRef(null);
|
|
36
|
+
const textareaRef = useRef(null);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
toolbarRef.current?.setAttribute("for", id);
|
|
39
|
+
}, [id]);
|
|
40
|
+
return (_jsx(FieldWrapper, { label: field.label, htmlFor: id, required: field.required, help: field.help, error: error, children: _jsxs("div", { className: `overflow-hidden rounded-md border bg-white focus-within:border-gray-900 focus-within:ring-1 focus-within:ring-gray-900 ${error ? "border-red-400" : "border-gray-300"} ${disabled ? "pointer-events-none opacity-60" : ""}`, onClick: () => {
|
|
41
|
+
const element = textareaRef.current;
|
|
42
|
+
if (element && element.value !== value)
|
|
43
|
+
onChange(element.value);
|
|
44
|
+
}, onKeyUp: () => {
|
|
45
|
+
const element = textareaRef.current;
|
|
46
|
+
if (element && element.value !== value)
|
|
47
|
+
onChange(element.value);
|
|
48
|
+
}, children: [_jsxs("div", { className: "flex flex-wrap items-center justify-between gap-2 border-b border-gray-200 bg-gray-50 px-2 py-1", children: [_jsx("div", { className: "flex items-center gap-1", role: "group", "aria-label": t("richtext.tabs"), children: ["write", "preview"].map((name) => {
|
|
49
|
+
const active = tab === name;
|
|
50
|
+
return (_jsx("button", { type: "button", "aria-pressed": active, onClick: () => setTab(name), className: `rounded-md px-3 py-1.5 text-xs font-medium transition-colors ${active
|
|
51
|
+
? "bg-white text-gray-900 shadow-sm"
|
|
52
|
+
: "text-gray-500 hover:text-gray-900"}`, children: t(`richtext.${name}`) }, name));
|
|
53
|
+
}) }), _jsx("div", { className: `min-w-0 ${tab === "preview" ? "pointer-events-none opacity-40" : ""}`, "aria-hidden": tab === "preview", inert: tab === "preview" ? true : undefined, children: createElement("markdown-toolbar", {
|
|
54
|
+
ref: toolbarRef,
|
|
55
|
+
className: "px-1",
|
|
56
|
+
}, TOOLBAR_BUTTONS.map((button) => createElement(button.tag, {
|
|
57
|
+
key: button.tag,
|
|
58
|
+
role: "button",
|
|
59
|
+
"aria-label": t(button.label),
|
|
60
|
+
title: t(button.label),
|
|
61
|
+
}, createElement(button.icon)))) })] }), tab === "write" ? (_jsx("textarea", { ref: textareaRef, id: id, rows: 10, value: value, disabled: disabled, "aria-invalid": Boolean(error), placeholder: field.placeholder, onInput: (event) => onChange(event.currentTarget.value), className: "w-full resize-y border-0 bg-white px-3 py-2.5 font-mono text-[13px] leading-6 text-gray-900 outline-none placeholder:text-gray-400 disabled:bg-gray-50 disabled:text-gray-500" })) : (_jsxs("div", { className: "min-h-[15rem] bg-white px-4 py-3 text-sm text-gray-800", children: [_jsx(MarkdownPreview, { value: value }), value.trim() ? null : (_jsx(Text, { size: "caption", className: "text-gray-400", children: t("richtext.previewEmpty") }))] }))] }) }));
|
|
62
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const BoldIcon: () => import("react").JSX.Element;
|
|
2
|
+
export declare const HeaderIcon: () => import("react").JSX.Element;
|
|
3
|
+
export declare const ItalicIcon: () => import("react").JSX.Element;
|
|
4
|
+
export declare const QuoteIcon: () => import("react").JSX.Element;
|
|
5
|
+
export declare const LinkIcon: () => import("react").JSX.Element;
|
|
6
|
+
export declare const UnorderedListIcon: () => import("react").JSX.Element;
|
|
7
|
+
export declare const OrderedListIcon: () => import("react").JSX.Element;
|
|
8
|
+
export declare const TaskListIcon: () => import("react").JSX.Element;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
const SCALE = 256 / 24;
|
|
3
|
+
/**
|
|
4
|
+
* Every icon is authored in a 24x24 coordinate grid (4px padding, artwork in
|
|
5
|
+
* 4..20), then scaled into a 256x256 SVG. CSS renders the toolbar icon at the
|
|
6
|
+
* same 24px size as before.
|
|
7
|
+
*/
|
|
8
|
+
function Icon({ children }) {
|
|
9
|
+
return (_jsx("svg", { width: 256, height: 256, viewBox: "0 0 256 256", fill: "none", stroke: "currentColor", strokeWidth: 1.6, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: _jsx("g", { transform: `scale(${SCALE})`, children: children }) }));
|
|
10
|
+
}
|
|
11
|
+
export const BoldIcon = () => (_jsxs(Icon, { children: [_jsx("path", { d: "M7 5h5.4a3.3 3.3 0 0 1 0 6.6H7z" }), _jsx("path", { d: "M7 11.6h6.2a3.7 3.7 0 0 1 0 7.4H7z" })] }));
|
|
12
|
+
export const HeaderIcon = () => (_jsxs(Icon, { children: [_jsx("path", { d: "M5 5v12M12 5v12M5 11h7" }), _jsx("text", { x: "13.2", y: "18.5", fill: "currentColor", stroke: "none", fontSize: "6.5", fontWeight: "700", fontFamily: "ui-sans-serif, system-ui, sans-serif", children: "3" })] }));
|
|
13
|
+
export const ItalicIcon = () => (_jsx(Icon, { children: _jsx("path", { d: "M10 5h7M7 19h7M13.5 5l-3 14" }) }));
|
|
14
|
+
export const QuoteIcon = () => (_jsx(Icon, { children: _jsx("path", { d: "M6 7h5v5H8l-2 4zM13 7h5v5h-3l-2 4z" }) }));
|
|
15
|
+
export const LinkIcon = () => (_jsxs(Icon, { children: [_jsx("path", { d: "M10.5 13.5a3.5 3.5 0 0 0 5 0l2-2a3.5 3.5 0 0 0-5-5l-1 1" }), _jsx("path", { d: "M13.5 10.5a3.5 3.5 0 0 0-5 0l-2 2a3.5 3.5 0 0 0 5 5l1-1" })] }));
|
|
16
|
+
export const UnorderedListIcon = () => (_jsxs(Icon, { children: [_jsx("circle", { cx: "6", cy: "7", r: "1", fill: "currentColor", stroke: "none" }), _jsx("circle", { cx: "6", cy: "12", r: "1", fill: "currentColor", stroke: "none" }), _jsx("circle", { cx: "6", cy: "17", r: "1", fill: "currentColor", stroke: "none" }), _jsx("path", { d: "M10 7h8M10 12h8M10 17h8" })] }));
|
|
17
|
+
export const OrderedListIcon = () => (_jsxs(Icon, { children: [_jsx("path", { d: "M10 7h8M10 12h8M10 17h8" }), _jsx("text", { x: "5", y: "9", fill: "currentColor", stroke: "none", fontSize: "5", fontFamily: "ui-monospace, monospace", children: "1" }), _jsx("text", { x: "5", y: "14", fill: "currentColor", stroke: "none", fontSize: "5", fontFamily: "ui-monospace, monospace", children: "2" }), _jsx("text", { x: "5", y: "19", fill: "currentColor", stroke: "none", fontSize: "5", fontFamily: "ui-monospace, monospace", children: "3" })] }));
|
|
18
|
+
export const TaskListIcon = () => (_jsxs(Icon, { children: [_jsx("rect", { x: "5", y: "5.5", width: "4", height: "4", rx: "1" }), _jsx("path", { d: "M6 7.5l1 1 2-2M11.5 7.5h7" }), _jsx("rect", { x: "5", y: "10.5", width: "4", height: "4", rx: "1" }), _jsx("path", { d: "M11.5 12.5h7" }), _jsx("rect", { x: "5", y: "15.5", width: "4", height: "4", rx: "1" }), _jsx("path", { d: "M11.5 17.5h7" })] }));
|
package/dist/ui/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export { Card } from "./card.js";
|
|
|
5
5
|
export { Container } from "./container.js";
|
|
6
6
|
export { Drawer } from "./drawer.js";
|
|
7
7
|
export { FieldWrapper, inputClass } from "./field-wrapper.js";
|
|
8
|
+
export { MarkdownPreview } from "./markdown-preview.js";
|
|
8
9
|
export { Text } from "./text.js";
|
package/dist/ui/index.js
CHANGED
|
@@ -5,4 +5,5 @@ export { Card } from "./card.js";
|
|
|
5
5
|
export { Container } from "./container.js";
|
|
6
6
|
export { Drawer } from "./drawer.js";
|
|
7
7
|
export { FieldWrapper, inputClass } from "./field-wrapper.js";
|
|
8
|
+
export { MarkdownPreview } from "./markdown-preview.js";
|
|
8
9
|
export { Text } from "./text.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type MarkdownPreviewProps = {
|
|
2
|
+
value: string;
|
|
3
|
+
className?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Client-only Markdown preview. `marked` and `dompurify` are loaded lazily so
|
|
7
|
+
* server rendering never touches the DOM, and the generated HTML is sanitized
|
|
8
|
+
* before it reaches the page.
|
|
9
|
+
*/
|
|
10
|
+
export declare const MarkdownPreview: ({ value, className, }: MarkdownPreviewProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useDeferredValue, useEffect, useState } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* Client-only Markdown preview. `marked` and `dompurify` are loaded lazily so
|
|
6
|
+
* server rendering never touches the DOM, and the generated HTML is sanitized
|
|
7
|
+
* before it reaches the page.
|
|
8
|
+
*/
|
|
9
|
+
export const MarkdownPreview = ({ value, className = "serenojs-markdown", }) => {
|
|
10
|
+
const [html, setHtml] = useState("");
|
|
11
|
+
const deferredValue = useDeferredValue(value);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
let cancelled = false;
|
|
14
|
+
void (async () => {
|
|
15
|
+
try {
|
|
16
|
+
const [{ marked }, { default: DOMPurify }] = await Promise.all([
|
|
17
|
+
import("marked"),
|
|
18
|
+
import("dompurify"),
|
|
19
|
+
]);
|
|
20
|
+
const raw = await marked.parse(deferredValue);
|
|
21
|
+
if (!cancelled)
|
|
22
|
+
setHtml(DOMPurify.sanitize(raw));
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
if (!cancelled)
|
|
26
|
+
setHtml("");
|
|
27
|
+
}
|
|
28
|
+
})();
|
|
29
|
+
return () => {
|
|
30
|
+
cancelled = true;
|
|
31
|
+
};
|
|
32
|
+
}, [deferredValue]);
|
|
33
|
+
return _jsx("div", { className: className, dangerouslySetInnerHTML: { __html: html } });
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eutobias/serenojs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "SerenoJS — schema-driven CMS for Waku: auto-generated admin SPA + Hono REST API.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -69,7 +69,10 @@
|
|
|
69
69
|
"waku": "1.0.0-rc.1"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
+
"@github/markdown-toolbar-element": "^2.2.3",
|
|
73
|
+
"dompurify": "^3.5.1",
|
|
72
74
|
"hono": "^4.12.14",
|
|
75
|
+
"marked": "^18.4.0",
|
|
73
76
|
"react-icons": "^5.7.0"
|
|
74
77
|
},
|
|
75
78
|
"publishConfig": {
|