@kizunacms/ui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.MD +9 -0
- package/README.md +20 -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,245 @@
|
|
|
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
|
+
export { CHANGE_PASSWORD_INTENT, FOCAL_POINT_INPUTS, FORGOT_INTENT, INVITE_INTENT, LOGIN_INTENT, LOGOUT_INTENT, PASSWORD_INPUT, RECORD_UPLOAD_INTENT, RESET_INTENT, SEARCH_INTENT, SETUP_INTENT, START_UPLOAD_INTENT, THEME_INTENT, UPDATE_ACCOUNT_INTENT, formSnapshotOf, problemOf, readAccountForm, readDocumentForm, readFileForm, readPasswordForm, themeOf, utcInput };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//#region src/state/form-field.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns a stored value as the field's input holds it.
|
|
4
|
+
*/
|
|
5
|
+
const inputValueOf = (field, value) => {
|
|
6
|
+
if (value === void 0 || value === null) return field.input === "selectMany" ? [] : "";
|
|
7
|
+
switch (field.input) {
|
|
8
|
+
case "checkbox": return value === true ? "on" : "";
|
|
9
|
+
case "selectMany": return Array.isArray(value) ? value.map(String) : [String(value)];
|
|
10
|
+
case "relationships": return Array.isArray(value) ? value.map(String).join("\n") : String(value);
|
|
11
|
+
case "file": return typeof value === "object" && "id" in value ? String(value.id) : typeof value === "object" && "file" in value ? String(value.file) : "";
|
|
12
|
+
case "json": return JSON.stringify(value, null, 4);
|
|
13
|
+
default: return String(value);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Returns a document's values as the form's inputs hold them.
|
|
18
|
+
*/
|
|
19
|
+
const formValuesOf = (fields, document) => Object.fromEntries(fields.map((field) => [field.name, inputValueOf(field, valueAtPath(document, field.name))]));
|
|
20
|
+
/**
|
|
21
|
+
* Returns the value at a dotted path, like `meta.title`.
|
|
22
|
+
*/
|
|
23
|
+
const valueAtPath = (document, name) => name.split(".").reduce((value, key) => value !== null && typeof value === "object" ? value[key] : void 0, document);
|
|
24
|
+
/**
|
|
25
|
+
* Returns a stored image or file as a choice for a file field, or `undefined` when it holds no id.
|
|
26
|
+
*/
|
|
27
|
+
const fileOptionOf = (file) => {
|
|
28
|
+
if (typeof file !== "object" || file === null) return void 0;
|
|
29
|
+
const record = file;
|
|
30
|
+
const id = record["id"] ?? record["file"];
|
|
31
|
+
if (typeof id !== "string") return void 0;
|
|
32
|
+
const original = typeof record["original"] === "object" && record["original"] !== null ? record["original"] : record;
|
|
33
|
+
const filename = typeof record["filename"] === "string" ? record["filename"] : void 0;
|
|
34
|
+
const isImage = String(record["contentType"] ?? "").startsWith("image/") || record["original"] !== void 0;
|
|
35
|
+
const url = typeof original["url"] === "string" ? original["url"] : void 0;
|
|
36
|
+
const width = original["width"];
|
|
37
|
+
const height = original["height"];
|
|
38
|
+
const sized = typeof width === "number" && typeof height === "number" && width > 0 && height > 0;
|
|
39
|
+
return {
|
|
40
|
+
value: id,
|
|
41
|
+
label: typeof record["alt"] === "string" && record["alt"] !== "" ? record["alt"] : filename ?? id,
|
|
42
|
+
...isImage && url !== void 0 ? { preview: url } : {},
|
|
43
|
+
...filename === void 0 ? {} : { filename },
|
|
44
|
+
...sized ? { detail: `${width} × ${height}` } : {}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Returns a file's size as people read it, like `240 KB` or `1.2 MB`.
|
|
49
|
+
*/
|
|
50
|
+
const fileSizeOf = (bytes) => {
|
|
51
|
+
if (bytes < 1e3) return `${bytes} bytes`;
|
|
52
|
+
if (bytes < 1e6) return `${Math.round(bytes / 1e3)} KB`;
|
|
53
|
+
return `${Number((bytes / 1e6).toFixed(1))} MB`;
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.fileOptionOf = fileOptionOf;
|
|
57
|
+
exports.fileSizeOf = fileSizeOf;
|
|
58
|
+
exports.formValuesOf = formValuesOf;
|
|
59
|
+
exports.inputValueOf = inputValueOf;
|
|
60
|
+
exports.valueAtPath = valueAtPath;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
//#region src/state/form-field.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The kind of input a form field shows.
|
|
4
|
+
*/
|
|
5
|
+
type FormInput = 'text' | 'textarea' | 'number' | 'checkbox' | 'url' | 'email' | 'slug' | 'select' | 'selectMany' | 'date' | 'dateTime' | 'relationship' | 'relationships' | 'file' | 'json' | 'password';
|
|
6
|
+
/**
|
|
7
|
+
* One choice of a select, relationship or file field.
|
|
8
|
+
*/
|
|
9
|
+
interface FormOption {
|
|
10
|
+
value: string;
|
|
11
|
+
label: string;
|
|
12
|
+
/**
|
|
13
|
+
* The URL of an image shown beside the choice.
|
|
14
|
+
*/
|
|
15
|
+
preview?: string;
|
|
16
|
+
filename?: string;
|
|
17
|
+
/**
|
|
18
|
+
* A short line shown beside the label, like an image's width and height.
|
|
19
|
+
*/
|
|
20
|
+
detail?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A field as the admin's form shows it, as plain data that passes from the server to the browser.
|
|
24
|
+
*/
|
|
25
|
+
interface FormField {
|
|
26
|
+
name: string;
|
|
27
|
+
label: string;
|
|
28
|
+
/**
|
|
29
|
+
* Help text shown under the input.
|
|
30
|
+
*/
|
|
31
|
+
description?: string;
|
|
32
|
+
input: FormInput;
|
|
33
|
+
required: boolean;
|
|
34
|
+
readOnly: boolean;
|
|
35
|
+
options?: FormOption[];
|
|
36
|
+
/**
|
|
37
|
+
* How many documents a relationship can pick from, when `options` holds only the first page of them.
|
|
38
|
+
*/
|
|
39
|
+
optionCount?: number;
|
|
40
|
+
/**
|
|
41
|
+
* The documents or the file the field points at now, which the first page of `options` may leave out.
|
|
42
|
+
*/
|
|
43
|
+
selected?: FormOption[];
|
|
44
|
+
min?: number;
|
|
45
|
+
max?: number;
|
|
46
|
+
integer?: boolean;
|
|
47
|
+
minLength?: number;
|
|
48
|
+
maxLength?: number;
|
|
49
|
+
/**
|
|
50
|
+
* The collection a relationship points into.
|
|
51
|
+
*/
|
|
52
|
+
to?: string;
|
|
53
|
+
/**
|
|
54
|
+
* The store an image or file field uploads to.
|
|
55
|
+
*/
|
|
56
|
+
store?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Whether a file field takes only images.
|
|
59
|
+
*/
|
|
60
|
+
image?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* A heading shown above the field. The checkboxes right after it share the heading.
|
|
63
|
+
*/
|
|
64
|
+
heading?: string;
|
|
65
|
+
/**
|
|
66
|
+
* The field whose value this one falls back to while it is empty.
|
|
67
|
+
*/
|
|
68
|
+
placeholderFrom?: string;
|
|
69
|
+
/**
|
|
70
|
+
* The text shown while the input is empty.
|
|
71
|
+
*/
|
|
72
|
+
placeholder?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Grey text inside a one-line input, before the value.
|
|
75
|
+
*/
|
|
76
|
+
prefix?: string;
|
|
77
|
+
/**
|
|
78
|
+
* Grey text inside a one-line input, after the value.
|
|
79
|
+
*/
|
|
80
|
+
suffix?: string;
|
|
81
|
+
/**
|
|
82
|
+
* The label of the field the value comes from, shown in a tip beside the label.
|
|
83
|
+
*/
|
|
84
|
+
source?: string;
|
|
85
|
+
/**
|
|
86
|
+
* How many characters a search result shows in full. A counter beside the label counts against it.
|
|
87
|
+
*/
|
|
88
|
+
showsInFull?: number;
|
|
89
|
+
/**
|
|
90
|
+
* The text shown after the value, and the name of the checkbox field that leaves it out.
|
|
91
|
+
*/
|
|
92
|
+
ending?: {
|
|
93
|
+
text: string;
|
|
94
|
+
leaveOutWith: string;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* The field that shows this one inside itself, which keeps it out of the list of fields.
|
|
98
|
+
*/
|
|
99
|
+
drawnBy?: string;
|
|
100
|
+
slug?: {
|
|
101
|
+
/**
|
|
102
|
+
* Whether the CMS fills in an empty slug when the document is saved.
|
|
103
|
+
*/
|
|
104
|
+
made?: boolean;
|
|
105
|
+
generateFrom?: string;
|
|
106
|
+
locale?: string;
|
|
107
|
+
replacements?: [string, string][];
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A form's values by field name. A checked checkbox is `'on'`, and a relationship with `many` puts one id per line.
|
|
112
|
+
*/
|
|
113
|
+
type FormValues = Record<string, string | string[]>;
|
|
114
|
+
/**
|
|
115
|
+
* Returns a stored value as the field's input holds it.
|
|
116
|
+
*/
|
|
117
|
+
declare const inputValueOf: (field: FormField, value: unknown) => string | string[];
|
|
118
|
+
/**
|
|
119
|
+
* Returns a document's values as the form's inputs hold them.
|
|
120
|
+
*/
|
|
121
|
+
declare const formValuesOf: (fields: readonly FormField[], document: Record<string, unknown>) => FormValues;
|
|
122
|
+
/**
|
|
123
|
+
* Returns the value at a dotted path, like `meta.title`.
|
|
124
|
+
*/
|
|
125
|
+
declare const valueAtPath: (document: Record<string, unknown>, name: string) => unknown;
|
|
126
|
+
/**
|
|
127
|
+
* Returns a stored image or file as a choice for a file field, or `undefined` when it holds no id.
|
|
128
|
+
*/
|
|
129
|
+
declare const fileOptionOf: (file: unknown) => FormOption | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Returns a file's size as people read it, like `240 KB` or `1.2 MB`.
|
|
132
|
+
*/
|
|
133
|
+
declare const fileSizeOf: (bytes: number) => string;
|
|
134
|
+
//#endregion
|
|
135
|
+
export { FormField, FormInput, FormOption, FormValues, fileOptionOf, fileSizeOf, formValuesOf, inputValueOf, valueAtPath };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
//#region src/state/form-field.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The kind of input a form field shows.
|
|
4
|
+
*/
|
|
5
|
+
type FormInput = 'text' | 'textarea' | 'number' | 'checkbox' | 'url' | 'email' | 'slug' | 'select' | 'selectMany' | 'date' | 'dateTime' | 'relationship' | 'relationships' | 'file' | 'json' | 'password';
|
|
6
|
+
/**
|
|
7
|
+
* One choice of a select, relationship or file field.
|
|
8
|
+
*/
|
|
9
|
+
interface FormOption {
|
|
10
|
+
value: string;
|
|
11
|
+
label: string;
|
|
12
|
+
/**
|
|
13
|
+
* The URL of an image shown beside the choice.
|
|
14
|
+
*/
|
|
15
|
+
preview?: string;
|
|
16
|
+
filename?: string;
|
|
17
|
+
/**
|
|
18
|
+
* A short line shown beside the label, like an image's width and height.
|
|
19
|
+
*/
|
|
20
|
+
detail?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A field as the admin's form shows it, as plain data that passes from the server to the browser.
|
|
24
|
+
*/
|
|
25
|
+
interface FormField {
|
|
26
|
+
name: string;
|
|
27
|
+
label: string;
|
|
28
|
+
/**
|
|
29
|
+
* Help text shown under the input.
|
|
30
|
+
*/
|
|
31
|
+
description?: string;
|
|
32
|
+
input: FormInput;
|
|
33
|
+
required: boolean;
|
|
34
|
+
readOnly: boolean;
|
|
35
|
+
options?: FormOption[];
|
|
36
|
+
/**
|
|
37
|
+
* How many documents a relationship can pick from, when `options` holds only the first page of them.
|
|
38
|
+
*/
|
|
39
|
+
optionCount?: number;
|
|
40
|
+
/**
|
|
41
|
+
* The documents or the file the field points at now, which the first page of `options` may leave out.
|
|
42
|
+
*/
|
|
43
|
+
selected?: FormOption[];
|
|
44
|
+
min?: number;
|
|
45
|
+
max?: number;
|
|
46
|
+
integer?: boolean;
|
|
47
|
+
minLength?: number;
|
|
48
|
+
maxLength?: number;
|
|
49
|
+
/**
|
|
50
|
+
* The collection a relationship points into.
|
|
51
|
+
*/
|
|
52
|
+
to?: string;
|
|
53
|
+
/**
|
|
54
|
+
* The store an image or file field uploads to.
|
|
55
|
+
*/
|
|
56
|
+
store?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Whether a file field takes only images.
|
|
59
|
+
*/
|
|
60
|
+
image?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* A heading shown above the field. The checkboxes right after it share the heading.
|
|
63
|
+
*/
|
|
64
|
+
heading?: string;
|
|
65
|
+
/**
|
|
66
|
+
* The field whose value this one falls back to while it is empty.
|
|
67
|
+
*/
|
|
68
|
+
placeholderFrom?: string;
|
|
69
|
+
/**
|
|
70
|
+
* The text shown while the input is empty.
|
|
71
|
+
*/
|
|
72
|
+
placeholder?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Grey text inside a one-line input, before the value.
|
|
75
|
+
*/
|
|
76
|
+
prefix?: string;
|
|
77
|
+
/**
|
|
78
|
+
* Grey text inside a one-line input, after the value.
|
|
79
|
+
*/
|
|
80
|
+
suffix?: string;
|
|
81
|
+
/**
|
|
82
|
+
* The label of the field the value comes from, shown in a tip beside the label.
|
|
83
|
+
*/
|
|
84
|
+
source?: string;
|
|
85
|
+
/**
|
|
86
|
+
* How many characters a search result shows in full. A counter beside the label counts against it.
|
|
87
|
+
*/
|
|
88
|
+
showsInFull?: number;
|
|
89
|
+
/**
|
|
90
|
+
* The text shown after the value, and the name of the checkbox field that leaves it out.
|
|
91
|
+
*/
|
|
92
|
+
ending?: {
|
|
93
|
+
text: string;
|
|
94
|
+
leaveOutWith: string;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* The field that shows this one inside itself, which keeps it out of the list of fields.
|
|
98
|
+
*/
|
|
99
|
+
drawnBy?: string;
|
|
100
|
+
slug?: {
|
|
101
|
+
/**
|
|
102
|
+
* Whether the CMS fills in an empty slug when the document is saved.
|
|
103
|
+
*/
|
|
104
|
+
made?: boolean;
|
|
105
|
+
generateFrom?: string;
|
|
106
|
+
locale?: string;
|
|
107
|
+
replacements?: [string, string][];
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A form's values by field name. A checked checkbox is `'on'`, and a relationship with `many` puts one id per line.
|
|
112
|
+
*/
|
|
113
|
+
type FormValues = Record<string, string | string[]>;
|
|
114
|
+
/**
|
|
115
|
+
* Returns a stored value as the field's input holds it.
|
|
116
|
+
*/
|
|
117
|
+
declare const inputValueOf: (field: FormField, value: unknown) => string | string[];
|
|
118
|
+
/**
|
|
119
|
+
* Returns a document's values as the form's inputs hold them.
|
|
120
|
+
*/
|
|
121
|
+
declare const formValuesOf: (fields: readonly FormField[], document: Record<string, unknown>) => FormValues;
|
|
122
|
+
/**
|
|
123
|
+
* Returns the value at a dotted path, like `meta.title`.
|
|
124
|
+
*/
|
|
125
|
+
declare const valueAtPath: (document: Record<string, unknown>, name: string) => unknown;
|
|
126
|
+
/**
|
|
127
|
+
* Returns a stored image or file as a choice for a file field, or `undefined` when it holds no id.
|
|
128
|
+
*/
|
|
129
|
+
declare const fileOptionOf: (file: unknown) => FormOption | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Returns a file's size as people read it, like `240 KB` or `1.2 MB`.
|
|
132
|
+
*/
|
|
133
|
+
declare const fileSizeOf: (bytes: number) => string;
|
|
134
|
+
//#endregion
|
|
135
|
+
export { FormField, FormInput, FormOption, FormValues, fileOptionOf, fileSizeOf, formValuesOf, inputValueOf, valueAtPath };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region src/state/form-field.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns a stored value as the field's input holds it.
|
|
4
|
+
*/
|
|
5
|
+
const inputValueOf = (field, value) => {
|
|
6
|
+
if (value === void 0 || value === null) return field.input === "selectMany" ? [] : "";
|
|
7
|
+
switch (field.input) {
|
|
8
|
+
case "checkbox": return value === true ? "on" : "";
|
|
9
|
+
case "selectMany": return Array.isArray(value) ? value.map(String) : [String(value)];
|
|
10
|
+
case "relationships": return Array.isArray(value) ? value.map(String).join("\n") : String(value);
|
|
11
|
+
case "file": return typeof value === "object" && "id" in value ? String(value.id) : typeof value === "object" && "file" in value ? String(value.file) : "";
|
|
12
|
+
case "json": return JSON.stringify(value, null, 4);
|
|
13
|
+
default: return String(value);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Returns a document's values as the form's inputs hold them.
|
|
18
|
+
*/
|
|
19
|
+
const formValuesOf = (fields, document) => Object.fromEntries(fields.map((field) => [field.name, inputValueOf(field, valueAtPath(document, field.name))]));
|
|
20
|
+
/**
|
|
21
|
+
* Returns the value at a dotted path, like `meta.title`.
|
|
22
|
+
*/
|
|
23
|
+
const valueAtPath = (document, name) => name.split(".").reduce((value, key) => value !== null && typeof value === "object" ? value[key] : void 0, document);
|
|
24
|
+
/**
|
|
25
|
+
* Returns a stored image or file as a choice for a file field, or `undefined` when it holds no id.
|
|
26
|
+
*/
|
|
27
|
+
const fileOptionOf = (file) => {
|
|
28
|
+
if (typeof file !== "object" || file === null) return void 0;
|
|
29
|
+
const record = file;
|
|
30
|
+
const id = record["id"] ?? record["file"];
|
|
31
|
+
if (typeof id !== "string") return void 0;
|
|
32
|
+
const original = typeof record["original"] === "object" && record["original"] !== null ? record["original"] : record;
|
|
33
|
+
const filename = typeof record["filename"] === "string" ? record["filename"] : void 0;
|
|
34
|
+
const isImage = String(record["contentType"] ?? "").startsWith("image/") || record["original"] !== void 0;
|
|
35
|
+
const url = typeof original["url"] === "string" ? original["url"] : void 0;
|
|
36
|
+
const width = original["width"];
|
|
37
|
+
const height = original["height"];
|
|
38
|
+
const sized = typeof width === "number" && typeof height === "number" && width > 0 && height > 0;
|
|
39
|
+
return {
|
|
40
|
+
value: id,
|
|
41
|
+
label: typeof record["alt"] === "string" && record["alt"] !== "" ? record["alt"] : filename ?? id,
|
|
42
|
+
...isImage && url !== void 0 ? { preview: url } : {},
|
|
43
|
+
...filename === void 0 ? {} : { filename },
|
|
44
|
+
...sized ? { detail: `${width} × ${height}` } : {}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Returns a file's size as people read it, like `240 KB` or `1.2 MB`.
|
|
49
|
+
*/
|
|
50
|
+
const fileSizeOf = (bytes) => {
|
|
51
|
+
if (bytes < 1e3) return `${bytes} bytes`;
|
|
52
|
+
if (bytes < 1e6) return `${Math.round(bytes / 1e3)} KB`;
|
|
53
|
+
return `${Number((bytes / 1e6).toFixed(1))} MB`;
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { fileOptionOf, fileSizeOf, formValuesOf, inputValueOf, valueAtPath };
|