@kizunacms/ui 0.0.0-stage → 0.1.1
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/LICENSE.MD +9 -0
- package/README.md +18 -2
- package/dist/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/admin.css +2158 -0
- package/dist/client.cjs +99 -0
- package/dist/client.d.cts +46 -0
- package/dist/client.d.mts +46 -0
- package/dist/client.mjs +45 -0
- package/dist/controls/AdminLink.cjs +21 -0
- package/dist/controls/AdminLink.d.cts +16 -0
- package/dist/controls/AdminLink.d.mts +16 -0
- package/dist/controls/AdminLink.mjs +20 -0
- package/dist/controls/Avatar.cjs +15 -0
- package/dist/controls/Avatar.d.cts +17 -0
- package/dist/controls/Avatar.d.mts +17 -0
- package/dist/controls/Avatar.mjs +14 -0
- package/dist/controls/Button.cjs +20 -0
- package/dist/controls/Button.d.cts +18 -0
- package/dist/controls/Button.d.mts +18 -0
- package/dist/controls/Button.mjs +18 -0
- package/dist/controls/ButtonTip.cjs +40 -0
- package/dist/controls/ButtonTip.mjs +39 -0
- package/dist/controls/Checkbox.cjs +27 -0
- package/dist/controls/Checkbox.d.cts +15 -0
- package/dist/controls/Checkbox.d.mts +15 -0
- package/dist/controls/Checkbox.mjs +26 -0
- package/dist/controls/ChipsInput.cjs +145 -0
- package/dist/controls/ChipsInput.mjs +143 -0
- package/dist/controls/ConfirmDialog.cjs +63 -0
- package/dist/controls/ConfirmDialog.d.cts +38 -0
- package/dist/controls/ConfirmDialog.d.mts +38 -0
- package/dist/controls/ConfirmDialog.mjs +62 -0
- package/dist/controls/Counter.cjs +18 -0
- package/dist/controls/Counter.mjs +17 -0
- package/dist/controls/InfoTip.cjs +29 -0
- package/dist/controls/InfoTip.d.cts +11 -0
- package/dist/controls/InfoTip.d.mts +11 -0
- package/dist/controls/InfoTip.mjs +28 -0
- package/dist/controls/LocalTime.cjs +62 -0
- package/dist/controls/LocalTime.d.cts +30 -0
- package/dist/controls/LocalTime.d.mts +30 -0
- package/dist/controls/LocalTime.mjs +61 -0
- package/dist/controls/Logo.cjs +56 -0
- package/dist/controls/Logo.d.cts +13 -0
- package/dist/controls/Logo.d.mts +13 -0
- package/dist/controls/Logo.mjs +54 -0
- package/dist/controls/MenuButton.cjs +118 -0
- package/dist/controls/MenuButton.d.cts +88 -0
- package/dist/controls/MenuButton.d.mts +88 -0
- package/dist/controls/MenuButton.mjs +117 -0
- package/dist/controls/Select.cjs +67 -0
- package/dist/controls/Select.d.cts +42 -0
- package/dist/controls/Select.d.mts +42 -0
- package/dist/controls/Select.mjs +66 -0
- package/dist/controls/Status.cjs +31 -0
- package/dist/controls/Status.d.cts +14 -0
- package/dist/controls/Status.d.mts +14 -0
- package/dist/controls/Status.mjs +30 -0
- package/dist/controls/TrailingButton.cjs +22 -0
- package/dist/controls/TrailingButton.d.cts +18 -0
- package/dist/controls/TrailingButton.d.mts +18 -0
- package/dist/controls/TrailingButton.mjs +21 -0
- package/dist/controls/icon.cjs +42 -0
- package/dist/controls/icon.mjs +38 -0
- package/dist/inputs/Affixed.cjs +49 -0
- package/dist/inputs/Affixed.mjs +47 -0
- package/dist/inputs/CheckboxField.cjs +22 -0
- package/dist/inputs/CheckboxField.d.cts +10 -0
- package/dist/inputs/CheckboxField.d.mts +10 -0
- package/dist/inputs/CheckboxField.mjs +21 -0
- package/dist/inputs/DateField.cjs +22 -0
- package/dist/inputs/DateField.d.cts +10 -0
- package/dist/inputs/DateField.d.mts +10 -0
- package/dist/inputs/DateField.mjs +21 -0
- package/dist/inputs/DateTimeField.cjs +56 -0
- package/dist/inputs/DateTimeField.d.cts +10 -0
- package/dist/inputs/DateTimeField.d.mts +10 -0
- package/dist/inputs/DateTimeField.mjs +55 -0
- package/dist/inputs/EmailField.cjs +20 -0
- package/dist/inputs/EmailField.d.cts +10 -0
- package/dist/inputs/EmailField.d.mts +10 -0
- package/dist/inputs/EmailField.mjs +19 -0
- package/dist/inputs/FallbackReset.cjs +23 -0
- package/dist/inputs/FallbackReset.mjs +21 -0
- package/dist/inputs/FieldFor.cjs +48 -0
- package/dist/inputs/FieldFor.d.cts +16 -0
- package/dist/inputs/FieldFor.d.mts +16 -0
- package/dist/inputs/FieldFor.mjs +46 -0
- package/dist/inputs/FieldFrame.cjs +97 -0
- package/dist/inputs/FieldFrame.d.cts +29 -0
- package/dist/inputs/FieldFrame.d.mts +29 -0
- package/dist/inputs/FieldFrame.mjs +95 -0
- package/dist/inputs/FieldList.cjs +39 -0
- package/dist/inputs/FieldList.d.cts +16 -0
- package/dist/inputs/FieldList.d.mts +16 -0
- package/dist/inputs/FieldList.mjs +38 -0
- package/dist/inputs/FileField.cjs +363 -0
- package/dist/inputs/FileField.d.cts +10 -0
- package/dist/inputs/FileField.d.mts +10 -0
- package/dist/inputs/FileField.mjs +362 -0
- package/dist/inputs/FocalPointPicker.cjs +114 -0
- package/dist/inputs/FocalPointPicker.mjs +113 -0
- package/dist/inputs/JsonField.cjs +21 -0
- package/dist/inputs/JsonField.d.cts +10 -0
- package/dist/inputs/JsonField.d.mts +10 -0
- package/dist/inputs/JsonField.mjs +20 -0
- package/dist/inputs/NumberField.cjs +42 -0
- package/dist/inputs/NumberField.d.cts +10 -0
- package/dist/inputs/NumberField.d.mts +10 -0
- package/dist/inputs/NumberField.mjs +41 -0
- package/dist/inputs/PasswordField.cjs +41 -0
- package/dist/inputs/PasswordField.d.cts +26 -0
- package/dist/inputs/PasswordField.d.mts +26 -0
- package/dist/inputs/PasswordField.mjs +39 -0
- package/dist/inputs/PickerButton.cjs +56 -0
- package/dist/inputs/PickerButton.mjs +55 -0
- package/dist/inputs/RelationshipField.cjs +162 -0
- package/dist/inputs/RelationshipField.d.cts +10 -0
- package/dist/inputs/RelationshipField.d.mts +10 -0
- package/dist/inputs/RelationshipField.mjs +161 -0
- package/dist/inputs/SelectField.cjs +79 -0
- package/dist/inputs/SelectField.d.cts +10 -0
- package/dist/inputs/SelectField.d.mts +10 -0
- package/dist/inputs/SelectField.mjs +78 -0
- package/dist/inputs/SlugField.cjs +71 -0
- package/dist/inputs/SlugField.d.cts +11 -0
- package/dist/inputs/SlugField.d.mts +11 -0
- package/dist/inputs/SlugField.mjs +69 -0
- package/dist/inputs/TextBox.cjs +41 -0
- package/dist/inputs/TextBox.d.cts +35 -0
- package/dist/inputs/TextBox.d.mts +35 -0
- package/dist/inputs/TextBox.mjs +39 -0
- package/dist/inputs/TextField.cjs +61 -0
- package/dist/inputs/TextField.d.cts +10 -0
- package/dist/inputs/TextField.d.mts +10 -0
- package/dist/inputs/TextField.mjs +60 -0
- package/dist/inputs/TitleField.cjs +76 -0
- package/dist/inputs/TitleField.mjs +75 -0
- package/dist/inputs/UrlField.cjs +20 -0
- package/dist/inputs/UrlField.d.cts +10 -0
- package/dist/inputs/UrlField.d.mts +10 -0
- package/dist/inputs/UrlField.mjs +19 -0
- package/dist/inputs/values.cjs +4 -0
- package/dist/inputs/values.d.cts +19 -0
- package/dist/inputs/values.d.mts +19 -0
- package/dist/inputs/values.mjs +4 -0
- package/dist/screens/AccountView.cjs +173 -0
- package/dist/screens/AccountView.d.cts +18 -0
- package/dist/screens/AccountView.d.mts +18 -0
- package/dist/screens/AccountView.mjs +172 -0
- package/dist/screens/AdminShell.cjs +136 -0
- package/dist/screens/AdminShell.d.cts +31 -0
- package/dist/screens/AdminShell.d.mts +31 -0
- package/dist/screens/AdminShell.mjs +135 -0
- package/dist/screens/AuthViews.cjs +260 -0
- package/dist/screens/AuthViews.d.cts +48 -0
- package/dist/screens/AuthViews.d.mts +48 -0
- package/dist/screens/AuthViews.mjs +255 -0
- package/dist/screens/CreateView.cjs +37 -0
- package/dist/screens/CreateView.d.cts +12 -0
- package/dist/screens/CreateView.d.mts +12 -0
- package/dist/screens/CreateView.mjs +36 -0
- package/dist/screens/DashboardView.cjs +39 -0
- package/dist/screens/DashboardView.d.cts +12 -0
- package/dist/screens/DashboardView.d.mts +12 -0
- package/dist/screens/DashboardView.mjs +38 -0
- package/dist/screens/DocumentForm.cjs +214 -0
- package/dist/screens/DocumentForm.d.cts +77 -0
- package/dist/screens/DocumentForm.d.mts +77 -0
- package/dist/screens/DocumentForm.mjs +213 -0
- package/dist/screens/DocumentView.cjs +53 -0
- package/dist/screens/DocumentView.d.cts +17 -0
- package/dist/screens/DocumentView.d.mts +17 -0
- package/dist/screens/DocumentView.mjs +52 -0
- package/dist/screens/FileView.cjs +74 -0
- package/dist/screens/FileView.d.cts +17 -0
- package/dist/screens/FileView.d.mts +17 -0
- package/dist/screens/FileView.mjs +73 -0
- package/dist/screens/FilesView.cjs +212 -0
- package/dist/screens/FilesView.d.cts +34 -0
- package/dist/screens/FilesView.d.mts +34 -0
- package/dist/screens/FilesView.mjs +211 -0
- package/dist/screens/GlobalView.cjs +28 -0
- package/dist/screens/GlobalView.d.cts +16 -0
- package/dist/screens/GlobalView.d.mts +16 -0
- package/dist/screens/GlobalView.mjs +27 -0
- package/dist/screens/ListParts.cjs +138 -0
- package/dist/screens/ListParts.mjs +135 -0
- package/dist/screens/ListView.cjs +155 -0
- package/dist/screens/ListView.d.cts +28 -0
- package/dist/screens/ListView.d.mts +28 -0
- package/dist/screens/ListView.mjs +154 -0
- package/dist/screens/LogoutView.cjs +45 -0
- package/dist/screens/LogoutView.d.cts +10 -0
- package/dist/screens/LogoutView.d.mts +10 -0
- package/dist/screens/LogoutView.mjs +44 -0
- package/dist/screens/MessageView.cjs +13 -0
- package/dist/screens/MessageView.d.cts +12 -0
- package/dist/screens/MessageView.d.mts +12 -0
- package/dist/screens/MessageView.mjs +12 -0
- package/dist/screens/ScreenHeader.cjs +101 -0
- package/dist/screens/ScreenHeader.d.cts +33 -0
- package/dist/screens/ScreenHeader.d.mts +33 -0
- package/dist/screens/ScreenHeader.mjs +98 -0
- package/dist/shared.cjs +55 -0
- package/dist/shared.d.cts +9 -0
- package/dist/shared.d.mts +9 -0
- package/dist/shared.mjs +9 -0
- package/dist/state/AdminProvider.cjs +188 -0
- package/dist/state/AdminProvider.d.cts +106 -0
- package/dist/state/AdminProvider.d.mts +106 -0
- package/dist/state/AdminProvider.mjs +186 -0
- package/dist/state/admin-path.cjs +132 -0
- package/dist/state/admin-path.d.cts +101 -0
- package/dist/state/admin-path.d.mts +101 -0
- package/dist/state/admin-path.mjs +131 -0
- package/dist/state/admin-sections.cjs +49 -0
- package/dist/state/admin-sections.d.cts +39 -0
- package/dist/state/admin-sections.d.mts +39 -0
- package/dist/state/admin-sections.mjs +48 -0
- package/dist/state/choice-search.cjs +95 -0
- package/dist/state/choice-search.mjs +94 -0
- package/dist/state/definition.cjs +53 -0
- package/dist/state/definition.d.cts +123 -0
- package/dist/state/definition.d.mts +123 -0
- package/dist/state/definition.mjs +48 -0
- package/dist/state/form-data.cjs +266 -0
- package/dist/state/form-data.d.cts +123 -0
- package/dist/state/form-data.d.mts +123 -0
- package/dist/state/form-data.mjs +245 -0
- package/dist/state/form-field.cjs +60 -0
- package/dist/state/form-field.d.cts +135 -0
- package/dist/state/form-field.d.mts +135 -0
- package/dist/state/form-field.mjs +56 -0
- package/dist/state/form-state.cjs +11 -0
- package/dist/state/form-state.d.cts +61 -0
- package/dist/state/form-state.d.mts +61 -0
- package/dist/state/form-state.mjs +11 -0
- package/dist/state/kept-focus.cjs +42 -0
- package/dist/state/kept-focus.mjs +41 -0
- package/dist/state/list-query.cjs +116 -0
- package/dist/state/list-query.d.cts +57 -0
- package/dist/state/list-query.d.mts +57 -0
- package/dist/state/list-query.mjs +108 -0
- package/dist/state/picker.cjs +48 -0
- package/dist/state/picker.mjs +47 -0
- package/dist/state/sidebar.cjs +15 -0
- package/dist/state/sidebar.d.cts +7 -0
- package/dist/state/sidebar.d.mts +7 -0
- package/dist/state/sidebar.mjs +14 -0
- package/dist/state/unsaved-guard.cjs +56 -0
- package/dist/state/unsaved-guard.mjs +54 -0
- package/dist/state/uploads.cjs +104 -0
- package/dist/state/uploads.mjs +102 -0
- package/package.json +89 -3
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
//#region src/state/form-data.ts
|
|
2
|
+
/**
|
|
3
|
+
* The `$intent` of the form that logs an editor in.
|
|
4
|
+
*/
|
|
5
|
+
const LOGIN_INTENT = "login";
|
|
6
|
+
/**
|
|
7
|
+
* The `$intent` of the form that logs the editor out.
|
|
8
|
+
*/
|
|
9
|
+
const LOGOUT_INTENT = "logout";
|
|
10
|
+
/**
|
|
11
|
+
* The `$intent` of the form that creates the first account.
|
|
12
|
+
*/
|
|
13
|
+
const SETUP_INTENT = "setup";
|
|
14
|
+
/**
|
|
15
|
+
* The `$intent` of the form that asks for a link to set a new password.
|
|
16
|
+
*/
|
|
17
|
+
const FORGOT_INTENT = "forgot";
|
|
18
|
+
/**
|
|
19
|
+
* The `$intent` of the form that sets a new password with the link from an email.
|
|
20
|
+
*/
|
|
21
|
+
const RESET_INTENT = "reset";
|
|
22
|
+
/**
|
|
23
|
+
* The `$intent` of the form that changes the editor's own account.
|
|
24
|
+
*/
|
|
25
|
+
const UPDATE_ACCOUNT_INTENT = "updateAccount";
|
|
26
|
+
/**
|
|
27
|
+
* The `$intent` of the form that changes the editor's own password.
|
|
28
|
+
*/
|
|
29
|
+
const CHANGE_PASSWORD_INTENT = "changePassword";
|
|
30
|
+
/**
|
|
31
|
+
* The `$intent` of the form that switches the admin's color theme.
|
|
32
|
+
*/
|
|
33
|
+
const THEME_INTENT = "theme";
|
|
34
|
+
/**
|
|
35
|
+
* The `$intent` of the form that emails an account a link to set its password.
|
|
36
|
+
*/
|
|
37
|
+
const INVITE_INTENT = "invite";
|
|
38
|
+
/**
|
|
39
|
+
* The `$intent` that asks for one page of what a relationship or file field picks from.
|
|
40
|
+
*/
|
|
41
|
+
const SEARCH_INTENT = "search";
|
|
42
|
+
/**
|
|
43
|
+
* The `$intent` that asks where to send a new file's bytes.
|
|
44
|
+
*/
|
|
45
|
+
const START_UPLOAD_INTENT = "startUpload";
|
|
46
|
+
/**
|
|
47
|
+
* The `$intent` that records a file once its bytes are sent.
|
|
48
|
+
*/
|
|
49
|
+
const RECORD_UPLOAD_INTENT = "recordUpload";
|
|
50
|
+
/**
|
|
51
|
+
* The names of the inputs that hold an image's focal point.
|
|
52
|
+
*/
|
|
53
|
+
const FOCAL_POINT_INPUTS = {
|
|
54
|
+
x: "$focalPoint.x",
|
|
55
|
+
y: "$focalPoint.y"
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* The name of the field a login collection's form sets a password with.
|
|
59
|
+
*/
|
|
60
|
+
const PASSWORD_INPUT = "password";
|
|
61
|
+
const MIN_PASSWORD_LENGTH = 12;
|
|
62
|
+
/**
|
|
63
|
+
* Returns the theme a value names, or `undefined` when it names none.
|
|
64
|
+
*/
|
|
65
|
+
const themeOf = (value) => value === "light" || value === "dark" ? value : void 0;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the name of the hidden input that holds a `dateTime` field's value in UTC.
|
|
68
|
+
*/
|
|
69
|
+
const utcInput = (name) => `$utc.${name}`;
|
|
70
|
+
const textOf = (formData, name) => {
|
|
71
|
+
const value = formData.get(name);
|
|
72
|
+
return typeof value === "string" ? value : "";
|
|
73
|
+
};
|
|
74
|
+
const listOf = (formData, name) => formData.getAll(name).filter((value) => typeof value === "string");
|
|
75
|
+
const parse = (field, raw) => {
|
|
76
|
+
switch (field.input) {
|
|
77
|
+
case "number": {
|
|
78
|
+
const value = Number(raw);
|
|
79
|
+
return Number.isFinite(value) ? { value } : { error: "Enter a number." };
|
|
80
|
+
}
|
|
81
|
+
case "json": try {
|
|
82
|
+
return { value: JSON.parse(raw) };
|
|
83
|
+
} catch {
|
|
84
|
+
return { error: "Enter valid JSON." };
|
|
85
|
+
}
|
|
86
|
+
case "file": return { value: { file: raw } };
|
|
87
|
+
case "relationships": return { value: raw.split(/[\n,]/).map((id) => id.trim()).filter((id) => id !== "") };
|
|
88
|
+
default: return { value: raw };
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const rawOf = (field, formData) => textOf(formData, field.input === "dateTime" ? utcInput(field.name) : field.name);
|
|
92
|
+
/**
|
|
93
|
+
* Reads a document's form into the body its create or update route takes. On update, an empty optional field is sent
|
|
94
|
+
* as `null`, which clears it.
|
|
95
|
+
*/
|
|
96
|
+
const readDocumentForm = (fields, formData, options) => {
|
|
97
|
+
const body = {};
|
|
98
|
+
const errors = {};
|
|
99
|
+
const values = {};
|
|
100
|
+
for (const field of fields) {
|
|
101
|
+
if (field.readOnly) continue;
|
|
102
|
+
if (field.input === "selectMany") {
|
|
103
|
+
const chosen = listOf(formData, field.name);
|
|
104
|
+
values[field.name] = chosen;
|
|
105
|
+
body[field.name] = chosen;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (field.input === "relationships") {
|
|
109
|
+
const ids = listOf(formData, field.name).flatMap((entry) => entry.split(/[\n,]/)).map((id) => id.trim()).filter((id) => id !== "");
|
|
110
|
+
values[field.name] = ids.join("\n");
|
|
111
|
+
if (ids.length > 0) body[field.name] = ids;
|
|
112
|
+
else if (field.required) errors[field.name] = "Required.";
|
|
113
|
+
else if (options.mode === "update") body[field.name] = null;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const raw = rawOf(field, formData);
|
|
117
|
+
if (field.input === "password") {
|
|
118
|
+
if (raw === "") continue;
|
|
119
|
+
if (raw.length < MIN_PASSWORD_LENGTH) errors[field.name] = `Use at least ${MIN_PASSWORD_LENGTH} characters.`;
|
|
120
|
+
else body[field.name] = raw;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
values[field.name] = raw;
|
|
124
|
+
if (field.input === "checkbox") {
|
|
125
|
+
body[field.name] = raw !== "";
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (raw.trim() === "") {
|
|
129
|
+
const madeOnSave = field.input === "slug" && field.slug?.made === true;
|
|
130
|
+
if (field.required && !madeOnSave) errors[field.name] = "Required.";
|
|
131
|
+
else if (options.mode === "update" && !madeOnSave) body[field.name] = null;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const parsed = parse(field, raw);
|
|
135
|
+
if ("error" in parsed) errors[field.name] = parsed.error;
|
|
136
|
+
else body[field.name] = parsed.value;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
body: nested(body),
|
|
140
|
+
errors,
|
|
141
|
+
values
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Turns dotted field names into nested objects at any depth, so `meta.robots.index` lands in `meta.robots`.
|
|
146
|
+
*/
|
|
147
|
+
const nested = (body) => {
|
|
148
|
+
const result = {};
|
|
149
|
+
for (const [name, value] of Object.entries(body)) {
|
|
150
|
+
const path = name.split(".");
|
|
151
|
+
let target = result;
|
|
152
|
+
for (const key of path.slice(0, -1)) {
|
|
153
|
+
target[key] ??= {};
|
|
154
|
+
target = target[key];
|
|
155
|
+
}
|
|
156
|
+
target[path.at(-1)] = value;
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Reads a file's form into the body its update route takes, with the focal point in whole percents of the image.
|
|
162
|
+
*/
|
|
163
|
+
const readFileForm = (fields, formData) => {
|
|
164
|
+
const form = readDocumentForm(fields, formData, { mode: "update" });
|
|
165
|
+
const x = Number(textOf(formData, FOCAL_POINT_INPUTS.x) || NaN);
|
|
166
|
+
const y = Number(textOf(formData, FOCAL_POINT_INPUTS.y) || NaN);
|
|
167
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) return form;
|
|
168
|
+
const percent = (value) => Math.min(100, Math.max(0, Math.round(value)));
|
|
169
|
+
return {
|
|
170
|
+
...form,
|
|
171
|
+
body: {
|
|
172
|
+
...form.body,
|
|
173
|
+
focalPoint: {
|
|
174
|
+
x: percent(x),
|
|
175
|
+
y: percent(y)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Returns what a form holds as text, to tell later whether anything in it changed. The order of the inputs does not
|
|
182
|
+
* matter.
|
|
183
|
+
*/
|
|
184
|
+
const formSnapshotOf = (formData) => JSON.stringify([...formData.entries()].map(([name, value]) => [name, typeof value === "string" ? value : `${value.name}:${value.size}`]).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0));
|
|
185
|
+
const requiredText = (formData, names) => {
|
|
186
|
+
const values = {};
|
|
187
|
+
const errors = {};
|
|
188
|
+
for (const name of names) {
|
|
189
|
+
const value = textOf(formData, name);
|
|
190
|
+
values[name] = value;
|
|
191
|
+
if (value.trim() === "") errors[name] = "Required.";
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
values,
|
|
195
|
+
errors
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* Reads the account form, whose `name` is required.
|
|
200
|
+
*/
|
|
201
|
+
const readAccountForm = (formData) => {
|
|
202
|
+
const { values, errors } = requiredText(formData, ["name"]);
|
|
203
|
+
return {
|
|
204
|
+
body: values,
|
|
205
|
+
errors,
|
|
206
|
+
values
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Reads the password form, which needs the current password and a new one. Neither password is kept to show again.
|
|
211
|
+
*/
|
|
212
|
+
const readPasswordForm = (formData) => {
|
|
213
|
+
const { values, errors } = requiredText(formData, ["currentPassword", "password"]);
|
|
214
|
+
return {
|
|
215
|
+
body: values,
|
|
216
|
+
errors,
|
|
217
|
+
values: {}
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
const isIssue = (value) => typeof value === "object" && value !== null && Array.isArray(value.path) && typeof value.message === "string";
|
|
221
|
+
/**
|
|
222
|
+
* Returns an error body from the API as messages for the named fields, and one for the whole form.
|
|
223
|
+
*/
|
|
224
|
+
const problemOf = (problem, fieldNames) => {
|
|
225
|
+
const body = typeof problem === "object" && problem !== null ? problem : {};
|
|
226
|
+
const fields = {};
|
|
227
|
+
const formMessages = [];
|
|
228
|
+
if (Array.isArray(body.errors)) for (const issue of body.errors) {
|
|
229
|
+
if (!isIssue(issue)) continue;
|
|
230
|
+
const path = issue.path.map(String).join(".");
|
|
231
|
+
const name = fieldNames.filter((candidate) => path === candidate || path.startsWith(`${candidate}.`)).reduce((longest, candidate) => longest === void 0 || candidate.length > longest.length ? candidate : longest, void 0);
|
|
232
|
+
if (name === void 0) formMessages.push(issue.message);
|
|
233
|
+
else fields[name] ??= issue.message;
|
|
234
|
+
}
|
|
235
|
+
if (formMessages.length === 0 && Object.keys(fields).length === 0) {
|
|
236
|
+
const message = typeof body.detail === "string" ? body.detail : typeof body.title === "string" ? body.title : "The change was not saved.";
|
|
237
|
+
formMessages.push(message);
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
fields,
|
|
241
|
+
...formMessages.length === 0 ? {} : { form: formMessages.join(" ") }
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
//#endregion
|
|
245
|
+
exports.CHANGE_PASSWORD_INTENT = CHANGE_PASSWORD_INTENT;
|
|
246
|
+
exports.FOCAL_POINT_INPUTS = FOCAL_POINT_INPUTS;
|
|
247
|
+
exports.FORGOT_INTENT = FORGOT_INTENT;
|
|
248
|
+
exports.INVITE_INTENT = INVITE_INTENT;
|
|
249
|
+
exports.LOGIN_INTENT = LOGIN_INTENT;
|
|
250
|
+
exports.LOGOUT_INTENT = LOGOUT_INTENT;
|
|
251
|
+
exports.PASSWORD_INPUT = PASSWORD_INPUT;
|
|
252
|
+
exports.RECORD_UPLOAD_INTENT = RECORD_UPLOAD_INTENT;
|
|
253
|
+
exports.RESET_INTENT = RESET_INTENT;
|
|
254
|
+
exports.SEARCH_INTENT = SEARCH_INTENT;
|
|
255
|
+
exports.SETUP_INTENT = SETUP_INTENT;
|
|
256
|
+
exports.START_UPLOAD_INTENT = START_UPLOAD_INTENT;
|
|
257
|
+
exports.THEME_INTENT = THEME_INTENT;
|
|
258
|
+
exports.UPDATE_ACCOUNT_INTENT = UPDATE_ACCOUNT_INTENT;
|
|
259
|
+
exports.formSnapshotOf = formSnapshotOf;
|
|
260
|
+
exports.problemOf = problemOf;
|
|
261
|
+
exports.readAccountForm = readAccountForm;
|
|
262
|
+
exports.readDocumentForm = readDocumentForm;
|
|
263
|
+
exports.readFileForm = readFileForm;
|
|
264
|
+
exports.readPasswordForm = readPasswordForm;
|
|
265
|
+
exports.themeOf = themeOf;
|
|
266
|
+
exports.utcInput = utcInput;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { FormField, FormValues } from "./form-field.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/state/form-data.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The `$intent` of the form that logs an editor in.
|
|
6
|
+
*/
|
|
7
|
+
declare const LOGIN_INTENT = "login";
|
|
8
|
+
/**
|
|
9
|
+
* The `$intent` of the form that logs the editor out.
|
|
10
|
+
*/
|
|
11
|
+
declare const LOGOUT_INTENT = "logout";
|
|
12
|
+
/**
|
|
13
|
+
* The `$intent` of the form that creates the first account.
|
|
14
|
+
*/
|
|
15
|
+
declare const SETUP_INTENT = "setup";
|
|
16
|
+
/**
|
|
17
|
+
* The `$intent` of the form that asks for a link to set a new password.
|
|
18
|
+
*/
|
|
19
|
+
declare const FORGOT_INTENT = "forgot";
|
|
20
|
+
/**
|
|
21
|
+
* The `$intent` of the form that sets a new password with the link from an email.
|
|
22
|
+
*/
|
|
23
|
+
declare const RESET_INTENT = "reset";
|
|
24
|
+
/**
|
|
25
|
+
* The `$intent` of the form that changes the editor's own account.
|
|
26
|
+
*/
|
|
27
|
+
declare const UPDATE_ACCOUNT_INTENT = "updateAccount";
|
|
28
|
+
/**
|
|
29
|
+
* The `$intent` of the form that changes the editor's own password.
|
|
30
|
+
*/
|
|
31
|
+
declare const CHANGE_PASSWORD_INTENT = "changePassword";
|
|
32
|
+
/**
|
|
33
|
+
* The `$intent` of the form that switches the admin's color theme.
|
|
34
|
+
*/
|
|
35
|
+
declare const THEME_INTENT = "theme";
|
|
36
|
+
/**
|
|
37
|
+
* The `$intent` of the form that emails an account a link to set its password.
|
|
38
|
+
*/
|
|
39
|
+
declare const INVITE_INTENT = "invite";
|
|
40
|
+
/**
|
|
41
|
+
* The `$intent` that asks for one page of what a relationship or file field picks from.
|
|
42
|
+
*/
|
|
43
|
+
declare const SEARCH_INTENT = "search";
|
|
44
|
+
/**
|
|
45
|
+
* The `$intent` that asks where to send a new file's bytes.
|
|
46
|
+
*/
|
|
47
|
+
declare const START_UPLOAD_INTENT = "startUpload";
|
|
48
|
+
/**
|
|
49
|
+
* The `$intent` that records a file once its bytes are sent.
|
|
50
|
+
*/
|
|
51
|
+
declare const RECORD_UPLOAD_INTENT = "recordUpload";
|
|
52
|
+
/**
|
|
53
|
+
* The names of the inputs that hold an image's focal point.
|
|
54
|
+
*/
|
|
55
|
+
declare const FOCAL_POINT_INPUTS: {
|
|
56
|
+
x: string;
|
|
57
|
+
y: string;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The name of the field a login collection's form sets a password with.
|
|
61
|
+
*/
|
|
62
|
+
declare const PASSWORD_INPUT = "password";
|
|
63
|
+
/**
|
|
64
|
+
* The admin's color theme. Without one, the admin follows the system's light or dark setting.
|
|
65
|
+
*/
|
|
66
|
+
type AdminTheme = 'light' | 'dark';
|
|
67
|
+
/**
|
|
68
|
+
* Returns the theme a value names, or `undefined` when it names none.
|
|
69
|
+
*/
|
|
70
|
+
declare const themeOf: (value: unknown) => AdminTheme | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the name of the hidden input that holds a `dateTime` field's value in UTC.
|
|
73
|
+
*/
|
|
74
|
+
declare const utcInput: (name: string) => string;
|
|
75
|
+
/**
|
|
76
|
+
* A form as read for the API: the `body` to send, the `errors` to show and the `values` to show again.
|
|
77
|
+
*/
|
|
78
|
+
interface ReadForm {
|
|
79
|
+
body: Record<string, unknown>;
|
|
80
|
+
errors: Record<string, string>;
|
|
81
|
+
values: FormValues;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Whether a form creates a document or updates one.
|
|
85
|
+
*/
|
|
86
|
+
type FormMode = 'create' | 'update';
|
|
87
|
+
/**
|
|
88
|
+
* Reads a document's form into the body its create or update route takes. On update, an empty optional field is sent
|
|
89
|
+
* as `null`, which clears it.
|
|
90
|
+
*/
|
|
91
|
+
declare const readDocumentForm: (fields: readonly FormField[], formData: FormData, options: {
|
|
92
|
+
mode: FormMode;
|
|
93
|
+
}) => ReadForm;
|
|
94
|
+
/**
|
|
95
|
+
* Reads a file's form into the body its update route takes, with the focal point in whole percents of the image.
|
|
96
|
+
*/
|
|
97
|
+
declare const readFileForm: (fields: readonly FormField[], formData: FormData) => ReadForm;
|
|
98
|
+
/**
|
|
99
|
+
* Returns what a form holds as text, to tell later whether anything in it changed. The order of the inputs does not
|
|
100
|
+
* matter.
|
|
101
|
+
*/
|
|
102
|
+
declare const formSnapshotOf: (formData: FormData) => string;
|
|
103
|
+
/**
|
|
104
|
+
* Reads the account form, whose `name` is required.
|
|
105
|
+
*/
|
|
106
|
+
declare const readAccountForm: (formData: FormData) => ReadForm;
|
|
107
|
+
/**
|
|
108
|
+
* Reads the password form, which needs the current password and a new one. Neither password is kept to show again.
|
|
109
|
+
*/
|
|
110
|
+
declare const readPasswordForm: (formData: FormData) => ReadForm;
|
|
111
|
+
/**
|
|
112
|
+
* An error from the API as messages for single fields and one for the whole form.
|
|
113
|
+
*/
|
|
114
|
+
interface FormProblem {
|
|
115
|
+
fields: Record<string, string>;
|
|
116
|
+
form?: string;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns an error body from the API as messages for the named fields, and one for the whole form.
|
|
120
|
+
*/
|
|
121
|
+
declare const problemOf: (problem: unknown, fieldNames: readonly string[]) => FormProblem;
|
|
122
|
+
//#endregion
|
|
123
|
+
export { AdminTheme, CHANGE_PASSWORD_INTENT, FOCAL_POINT_INPUTS, FORGOT_INTENT, FormMode, FormProblem, INVITE_INTENT, LOGIN_INTENT, LOGOUT_INTENT, PASSWORD_INPUT, RECORD_UPLOAD_INTENT, RESET_INTENT, ReadForm, SEARCH_INTENT, SETUP_INTENT, START_UPLOAD_INTENT, THEME_INTENT, UPDATE_ACCOUNT_INTENT, formSnapshotOf, problemOf, readAccountForm, readDocumentForm, readFileForm, readPasswordForm, themeOf, utcInput };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { FormField, FormValues } from "./form-field.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/state/form-data.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The `$intent` of the form that logs an editor in.
|
|
6
|
+
*/
|
|
7
|
+
declare const LOGIN_INTENT = "login";
|
|
8
|
+
/**
|
|
9
|
+
* The `$intent` of the form that logs the editor out.
|
|
10
|
+
*/
|
|
11
|
+
declare const LOGOUT_INTENT = "logout";
|
|
12
|
+
/**
|
|
13
|
+
* The `$intent` of the form that creates the first account.
|
|
14
|
+
*/
|
|
15
|
+
declare const SETUP_INTENT = "setup";
|
|
16
|
+
/**
|
|
17
|
+
* The `$intent` of the form that asks for a link to set a new password.
|
|
18
|
+
*/
|
|
19
|
+
declare const FORGOT_INTENT = "forgot";
|
|
20
|
+
/**
|
|
21
|
+
* The `$intent` of the form that sets a new password with the link from an email.
|
|
22
|
+
*/
|
|
23
|
+
declare const RESET_INTENT = "reset";
|
|
24
|
+
/**
|
|
25
|
+
* The `$intent` of the form that changes the editor's own account.
|
|
26
|
+
*/
|
|
27
|
+
declare const UPDATE_ACCOUNT_INTENT = "updateAccount";
|
|
28
|
+
/**
|
|
29
|
+
* The `$intent` of the form that changes the editor's own password.
|
|
30
|
+
*/
|
|
31
|
+
declare const CHANGE_PASSWORD_INTENT = "changePassword";
|
|
32
|
+
/**
|
|
33
|
+
* The `$intent` of the form that switches the admin's color theme.
|
|
34
|
+
*/
|
|
35
|
+
declare const THEME_INTENT = "theme";
|
|
36
|
+
/**
|
|
37
|
+
* The `$intent` of the form that emails an account a link to set its password.
|
|
38
|
+
*/
|
|
39
|
+
declare const INVITE_INTENT = "invite";
|
|
40
|
+
/**
|
|
41
|
+
* The `$intent` that asks for one page of what a relationship or file field picks from.
|
|
42
|
+
*/
|
|
43
|
+
declare const SEARCH_INTENT = "search";
|
|
44
|
+
/**
|
|
45
|
+
* The `$intent` that asks where to send a new file's bytes.
|
|
46
|
+
*/
|
|
47
|
+
declare const START_UPLOAD_INTENT = "startUpload";
|
|
48
|
+
/**
|
|
49
|
+
* The `$intent` that records a file once its bytes are sent.
|
|
50
|
+
*/
|
|
51
|
+
declare const RECORD_UPLOAD_INTENT = "recordUpload";
|
|
52
|
+
/**
|
|
53
|
+
* The names of the inputs that hold an image's focal point.
|
|
54
|
+
*/
|
|
55
|
+
declare const FOCAL_POINT_INPUTS: {
|
|
56
|
+
x: string;
|
|
57
|
+
y: string;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The name of the field a login collection's form sets a password with.
|
|
61
|
+
*/
|
|
62
|
+
declare const PASSWORD_INPUT = "password";
|
|
63
|
+
/**
|
|
64
|
+
* The admin's color theme. Without one, the admin follows the system's light or dark setting.
|
|
65
|
+
*/
|
|
66
|
+
type AdminTheme = 'light' | 'dark';
|
|
67
|
+
/**
|
|
68
|
+
* Returns the theme a value names, or `undefined` when it names none.
|
|
69
|
+
*/
|
|
70
|
+
declare const themeOf: (value: unknown) => AdminTheme | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the name of the hidden input that holds a `dateTime` field's value in UTC.
|
|
73
|
+
*/
|
|
74
|
+
declare const utcInput: (name: string) => string;
|
|
75
|
+
/**
|
|
76
|
+
* A form as read for the API: the `body` to send, the `errors` to show and the `values` to show again.
|
|
77
|
+
*/
|
|
78
|
+
interface ReadForm {
|
|
79
|
+
body: Record<string, unknown>;
|
|
80
|
+
errors: Record<string, string>;
|
|
81
|
+
values: FormValues;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Whether a form creates a document or updates one.
|
|
85
|
+
*/
|
|
86
|
+
type FormMode = 'create' | 'update';
|
|
87
|
+
/**
|
|
88
|
+
* Reads a document's form into the body its create or update route takes. On update, an empty optional field is sent
|
|
89
|
+
* as `null`, which clears it.
|
|
90
|
+
*/
|
|
91
|
+
declare const readDocumentForm: (fields: readonly FormField[], formData: FormData, options: {
|
|
92
|
+
mode: FormMode;
|
|
93
|
+
}) => ReadForm;
|
|
94
|
+
/**
|
|
95
|
+
* Reads a file's form into the body its update route takes, with the focal point in whole percents of the image.
|
|
96
|
+
*/
|
|
97
|
+
declare const readFileForm: (fields: readonly FormField[], formData: FormData) => ReadForm;
|
|
98
|
+
/**
|
|
99
|
+
* Returns what a form holds as text, to tell later whether anything in it changed. The order of the inputs does not
|
|
100
|
+
* matter.
|
|
101
|
+
*/
|
|
102
|
+
declare const formSnapshotOf: (formData: FormData) => string;
|
|
103
|
+
/**
|
|
104
|
+
* Reads the account form, whose `name` is required.
|
|
105
|
+
*/
|
|
106
|
+
declare const readAccountForm: (formData: FormData) => ReadForm;
|
|
107
|
+
/**
|
|
108
|
+
* Reads the password form, which needs the current password and a new one. Neither password is kept to show again.
|
|
109
|
+
*/
|
|
110
|
+
declare const readPasswordForm: (formData: FormData) => ReadForm;
|
|
111
|
+
/**
|
|
112
|
+
* An error from the API as messages for single fields and one for the whole form.
|
|
113
|
+
*/
|
|
114
|
+
interface FormProblem {
|
|
115
|
+
fields: Record<string, string>;
|
|
116
|
+
form?: string;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns an error body from the API as messages for the named fields, and one for the whole form.
|
|
120
|
+
*/
|
|
121
|
+
declare const problemOf: (problem: unknown, fieldNames: readonly string[]) => FormProblem;
|
|
122
|
+
//#endregion
|
|
123
|
+
export { AdminTheme, CHANGE_PASSWORD_INTENT, FOCAL_POINT_INPUTS, FORGOT_INTENT, FormMode, FormProblem, INVITE_INTENT, LOGIN_INTENT, LOGOUT_INTENT, PASSWORD_INPUT, RECORD_UPLOAD_INTENT, RESET_INTENT, ReadForm, SEARCH_INTENT, SETUP_INTENT, START_UPLOAD_INTENT, THEME_INTENT, UPDATE_ACCOUNT_INTENT, formSnapshotOf, problemOf, readAccountForm, readDocumentForm, readFileForm, readPasswordForm, themeOf, utcInput };
|