@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.
Files changed (51) hide show
  1. package/README.md +30 -11
  2. package/admin-dist/assets/index-CEMJnHOP.js +22 -0
  3. package/admin-dist/assets/index-Di8sAvwO.css +2 -0
  4. package/admin-dist/assets/marked.esm-BBttRptN.js +77 -0
  5. package/admin-dist/assets/purify.es-Bz-uQoIK.js +3 -0
  6. package/admin-dist/index.html +2 -2
  7. package/dist/config.d.ts +9 -2
  8. package/dist/config.js +1 -0
  9. package/dist/core/backup.server.d.ts +5 -2
  10. package/dist/core/backup.server.js +346 -12
  11. package/dist/core/db.server.js +29 -1
  12. package/dist/core/schema-codegen.server.d.ts +17 -0
  13. package/dist/core/schema-codegen.server.js +155 -0
  14. package/dist/core/schema.js +8 -1
  15. package/dist/core/types.d.ts +15 -4
  16. package/dist/core/validation.js +61 -0
  17. package/dist/ui/admin/collection-manager.js +1 -1
  18. package/dist/ui/admin/display.js +7 -0
  19. package/dist/ui/admin/field-control.d.ts +3 -1
  20. package/dist/ui/admin/field-control.js +10 -2
  21. package/dist/ui/admin/i18n.js +30 -4
  22. package/dist/ui/admin/object-list-control.d.ts +15 -0
  23. package/dist/ui/admin/object-list-control.js +42 -0
  24. package/dist/ui/admin/rich-text-control.d.ts +15 -0
  25. package/dist/ui/admin/rich-text-control.js +62 -0
  26. package/dist/ui/admin/rich-text-icons.d.ts +8 -0
  27. package/dist/ui/admin/rich-text-icons.js +18 -0
  28. package/dist/ui/index.d.ts +1 -0
  29. package/dist/ui/index.js +1 -0
  30. package/dist/ui/markdown-preview.d.ts +10 -0
  31. package/dist/ui/markdown-preview.js +34 -0
  32. package/package.json +4 -1
  33. package/src/admin/styles.css +152 -0
  34. package/src/config.ts +6 -1
  35. package/src/core/backup.server.ts +435 -17
  36. package/src/core/db.server.ts +34 -1
  37. package/src/core/schema-codegen.server.ts +187 -0
  38. package/src/core/schema.ts +8 -1
  39. package/src/core/types.ts +18 -3
  40. package/src/core/validation.ts +71 -0
  41. package/src/ui/admin/collection-manager.tsx +1 -0
  42. package/src/ui/admin/display.tsx +13 -1
  43. package/src/ui/admin/field-control.tsx +43 -2
  44. package/src/ui/admin/i18n.tsx +32 -4
  45. package/src/ui/admin/object-list-control.tsx +154 -0
  46. package/src/ui/admin/rich-text-control.tsx +173 -0
  47. package/src/ui/admin/rich-text-icons.tsx +125 -0
  48. package/src/ui/index.ts +1 -0
  49. package/src/ui/markdown-preview.tsx +42 -0
  50. package/admin-dist/assets/index-B3EgqtT6.js +0 -9
  51. package/admin-dist/assets/index-D2m0Eb4k.css +0 -2
@@ -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
+ };
@@ -0,0 +1,173 @@
1
+ "use client";
2
+
3
+ import "@github/markdown-toolbar-element";
4
+
5
+ import { createElement, useEffect, useRef, useState } from "react";
6
+
7
+ import type { FieldDef } from "../../core/types.js";
8
+ import { FieldWrapper } from "../field-wrapper.js";
9
+ import { MarkdownPreview } from "../markdown-preview.js";
10
+ import { Text } from "../text.js";
11
+ import {
12
+ BoldIcon,
13
+ HeaderIcon,
14
+ ItalicIcon,
15
+ LinkIcon,
16
+ OrderedListIcon,
17
+ QuoteIcon,
18
+ TaskListIcon,
19
+ UnorderedListIcon,
20
+ } from "./rich-text-icons.js";
21
+ import { useI18n } from "./i18n.js";
22
+
23
+ const TOOLBAR_BUTTONS = [
24
+ { tag: "md-header", label: "richtext.header", icon: HeaderIcon },
25
+ { tag: "md-bold", label: "richtext.bold", icon: BoldIcon },
26
+ { tag: "md-italic", label: "richtext.italic", icon: ItalicIcon },
27
+ { tag: "md-quote", label: "richtext.quote", icon: QuoteIcon },
28
+ { tag: "md-link", label: "richtext.link", icon: LinkIcon },
29
+ {
30
+ tag: "md-unordered-list",
31
+ label: "richtext.unorderedList",
32
+ icon: UnorderedListIcon,
33
+ },
34
+ {
35
+ tag: "md-ordered-list",
36
+ label: "richtext.orderedList",
37
+ icon: OrderedListIcon,
38
+ },
39
+ { tag: "md-task-list", label: "richtext.taskList", icon: TaskListIcon },
40
+ ] as const;
41
+
42
+ export type RichTextControlProps = {
43
+ field: FieldDef;
44
+ id: string;
45
+ value: string;
46
+ error?: string | undefined;
47
+ disabled?: boolean | undefined;
48
+ onChange: (value: string) => void;
49
+ };
50
+
51
+ /**
52
+ * Markdown editor with Write/Preview tabs and the GitHub markdown-toolbar
53
+ * custom elements on the right.
54
+ */
55
+ export const RichTextControl = ({
56
+ field,
57
+ id,
58
+ value,
59
+ error,
60
+ disabled,
61
+ onChange,
62
+ }: RichTextControlProps) => {
63
+ const { t } = useI18n();
64
+ const [tab, setTab] = useState<"write" | "preview">("write");
65
+ const toolbarRef = useRef<HTMLElement>(null);
66
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
67
+
68
+ useEffect(() => {
69
+ toolbarRef.current?.setAttribute("for", id);
70
+ }, [id]);
71
+
72
+ return (
73
+ <FieldWrapper
74
+ label={field.label}
75
+ htmlFor={id}
76
+ required={field.required}
77
+ help={field.help}
78
+ error={error}
79
+ >
80
+ <div
81
+ className={`overflow-hidden rounded-md border bg-white focus-within:border-gray-900 focus-within:ring-1 focus-within:ring-gray-900 ${
82
+ error ? "border-red-400" : "border-gray-300"
83
+ } ${disabled ? "pointer-events-none opacity-60" : ""}`}
84
+ onClick={() => {
85
+ const element = textareaRef.current;
86
+ if (element && element.value !== value) onChange(element.value);
87
+ }}
88
+ onKeyUp={() => {
89
+ const element = textareaRef.current;
90
+ if (element && element.value !== value) onChange(element.value);
91
+ }}
92
+ >
93
+ <div className="flex flex-wrap items-center justify-between gap-2 border-b border-gray-200 bg-gray-50 px-2 py-1">
94
+ <div
95
+ className="flex items-center gap-1"
96
+ role="group"
97
+ aria-label={t("richtext.tabs")}
98
+ >
99
+ {(["write", "preview"] as const).map((name) => {
100
+ const active = tab === name;
101
+ return (
102
+ <button
103
+ key={name}
104
+ type="button"
105
+ aria-pressed={active}
106
+ onClick={() => setTab(name)}
107
+ className={`rounded-md px-3 py-1.5 text-xs font-medium transition-colors ${
108
+ active
109
+ ? "bg-white text-gray-900 shadow-sm"
110
+ : "text-gray-500 hover:text-gray-900"
111
+ }`}
112
+ >
113
+ {t(`richtext.${name}`)}
114
+ </button>
115
+ );
116
+ })}
117
+ </div>
118
+
119
+ <div
120
+ className={`min-w-0 ${
121
+ tab === "preview" ? "pointer-events-none opacity-40" : ""
122
+ }`}
123
+ aria-hidden={tab === "preview"}
124
+ inert={tab === "preview" ? true : undefined}
125
+ >
126
+ {createElement(
127
+ "markdown-toolbar",
128
+ {
129
+ ref: toolbarRef,
130
+ className: "px-1",
131
+ },
132
+ TOOLBAR_BUTTONS.map((button) =>
133
+ createElement(
134
+ button.tag,
135
+ {
136
+ key: button.tag,
137
+ role: "button",
138
+ "aria-label": t(button.label),
139
+ title: t(button.label),
140
+ },
141
+ createElement(button.icon),
142
+ ),
143
+ ),
144
+ )}
145
+ </div>
146
+ </div>
147
+
148
+ {tab === "write" ? (
149
+ <textarea
150
+ ref={textareaRef}
151
+ id={id}
152
+ rows={10}
153
+ value={value}
154
+ disabled={disabled}
155
+ aria-invalid={Boolean(error)}
156
+ placeholder={field.placeholder}
157
+ onInput={(event) => onChange(event.currentTarget.value)}
158
+ 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"
159
+ />
160
+ ) : (
161
+ <div className="min-h-[15rem] bg-white px-4 py-3 text-sm text-gray-800">
162
+ <MarkdownPreview value={value} />
163
+ {value.trim() ? null : (
164
+ <Text size="caption" className="text-gray-400">
165
+ {t("richtext.previewEmpty")}
166
+ </Text>
167
+ )}
168
+ </div>
169
+ )}
170
+ </div>
171
+ </FieldWrapper>
172
+ );
173
+ };
@@ -0,0 +1,125 @@
1
+ import type { ReactNode } from "react";
2
+
3
+ const SCALE = 256 / 24;
4
+
5
+ /**
6
+ * Every icon is authored in a 24x24 coordinate grid (4px padding, artwork in
7
+ * 4..20), then scaled into a 256x256 SVG. CSS renders the toolbar icon at the
8
+ * same 24px size as before.
9
+ */
10
+ function Icon({ children }: { children: ReactNode }) {
11
+ return (
12
+ <svg
13
+ width={256}
14
+ height={256}
15
+ viewBox="0 0 256 256"
16
+ fill="none"
17
+ stroke="currentColor"
18
+ strokeWidth={1.6}
19
+ strokeLinecap="round"
20
+ strokeLinejoin="round"
21
+ aria-hidden="true"
22
+ >
23
+ <g transform={`scale(${SCALE})`}>{children}</g>
24
+ </svg>
25
+ );
26
+ }
27
+
28
+ export const BoldIcon = () => (
29
+ <Icon>
30
+ <path d="M7 5h5.4a3.3 3.3 0 0 1 0 6.6H7z" />
31
+ <path d="M7 11.6h6.2a3.7 3.7 0 0 1 0 7.4H7z" />
32
+ </Icon>
33
+ );
34
+
35
+ export const HeaderIcon = () => (
36
+ <Icon>
37
+ <path d="M5 5v12M12 5v12M5 11h7" />
38
+ <text
39
+ x="13.2"
40
+ y="18.5"
41
+ fill="currentColor"
42
+ stroke="none"
43
+ fontSize="6.5"
44
+ fontWeight="700"
45
+ fontFamily="ui-sans-serif, system-ui, sans-serif"
46
+ >
47
+ 3
48
+ </text>
49
+ </Icon>
50
+ );
51
+
52
+ export const ItalicIcon = () => (
53
+ <Icon>
54
+ <path d="M10 5h7M7 19h7M13.5 5l-3 14" />
55
+ </Icon>
56
+ );
57
+
58
+ export const QuoteIcon = () => (
59
+ <Icon>
60
+ <path d="M6 7h5v5H8l-2 4zM13 7h5v5h-3l-2 4z" />
61
+ </Icon>
62
+ );
63
+
64
+ export const LinkIcon = () => (
65
+ <Icon>
66
+ <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" />
67
+ <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" />
68
+ </Icon>
69
+ );
70
+
71
+ export const UnorderedListIcon = () => (
72
+ <Icon>
73
+ <circle cx="6" cy="7" r="1" fill="currentColor" stroke="none" />
74
+ <circle cx="6" cy="12" r="1" fill="currentColor" stroke="none" />
75
+ <circle cx="6" cy="17" r="1" fill="currentColor" stroke="none" />
76
+ <path d="M10 7h8M10 12h8M10 17h8" />
77
+ </Icon>
78
+ );
79
+
80
+ export const OrderedListIcon = () => (
81
+ <Icon>
82
+ <path d="M10 7h8M10 12h8M10 17h8" />
83
+ <text
84
+ x="5"
85
+ y="9"
86
+ fill="currentColor"
87
+ stroke="none"
88
+ fontSize="5"
89
+ fontFamily="ui-monospace, monospace"
90
+ >
91
+ 1
92
+ </text>
93
+ <text
94
+ x="5"
95
+ y="14"
96
+ fill="currentColor"
97
+ stroke="none"
98
+ fontSize="5"
99
+ fontFamily="ui-monospace, monospace"
100
+ >
101
+ 2
102
+ </text>
103
+ <text
104
+ x="5"
105
+ y="19"
106
+ fill="currentColor"
107
+ stroke="none"
108
+ fontSize="5"
109
+ fontFamily="ui-monospace, monospace"
110
+ >
111
+ 3
112
+ </text>
113
+ </Icon>
114
+ );
115
+
116
+ export const TaskListIcon = () => (
117
+ <Icon>
118
+ <rect x="5" y="5.5" width="4" height="4" rx="1" />
119
+ <path d="M6 7.5l1 1 2-2M11.5 7.5h7" />
120
+ <rect x="5" y="10.5" width="4" height="4" rx="1" />
121
+ <path d="M11.5 12.5h7" />
122
+ <rect x="5" y="15.5" width="4" height="4" rx="1" />
123
+ <path d="M11.5 17.5h7" />
124
+ </Icon>
125
+ );
package/src/ui/index.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";
@@ -0,0 +1,42 @@
1
+ "use client";
2
+
3
+ import { useDeferredValue, useEffect, useState } from "react";
4
+
5
+ export type MarkdownPreviewProps = {
6
+ value: string;
7
+ className?: string;
8
+ };
9
+
10
+ /**
11
+ * Client-only Markdown preview. `marked` and `dompurify` are loaded lazily so
12
+ * server rendering never touches the DOM, and the generated HTML is sanitized
13
+ * before it reaches the page.
14
+ */
15
+ export const MarkdownPreview = ({
16
+ value,
17
+ className = "serenojs-markdown",
18
+ }: MarkdownPreviewProps) => {
19
+ const [html, setHtml] = useState("");
20
+ const deferredValue = useDeferredValue(value);
21
+
22
+ useEffect(() => {
23
+ let cancelled = false;
24
+ void (async () => {
25
+ try {
26
+ const [{ marked }, { default: DOMPurify }] = await Promise.all([
27
+ import("marked"),
28
+ import("dompurify"),
29
+ ]);
30
+ const raw = await marked.parse(deferredValue);
31
+ if (!cancelled) setHtml(DOMPurify.sanitize(raw));
32
+ } catch {
33
+ if (!cancelled) setHtml("");
34
+ }
35
+ })();
36
+ return () => {
37
+ cancelled = true;
38
+ };
39
+ }, [deferredValue]);
40
+
41
+ return <div className={className} dangerouslySetInnerHTML={{ __html: html }} />;
42
+ };