@wervt/cms 0.0.4 → 0.0.6
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 +177 -18
- package/dist/editor/assets/Button-DrsXdow7.js +6 -0
- package/dist/editor/assets/RichTextInput-BrNTSUXL.js +271 -0
- package/dist/editor/assets/Toast-DsP3Sdxg.js +13 -0
- package/dist/editor/assets/index-CWQLsBfF.js +16 -0
- package/dist/editor/assets/index-esGfkiTV.css +2 -0
- package/dist/editor/index.html +3 -3
- package/dist/editor-theme.d.ts +3 -0
- package/dist/editor-theme.js +18 -1
- package/dist/module.d.ts +18 -1
- package/dist/module.js +95 -7
- package/dist/routes.js +16 -0
- package/dist/runtime/app/edit-button.d.ts +7 -0
- package/dist/runtime/app/edit-button.js +56 -0
- package/dist/runtime/app/preview.client.js +12 -5
- package/dist/runtime/app/useCmsCollection.d.ts +24 -0
- package/dist/runtime/app/useCmsCollection.js +74 -0
- package/dist/runtime/app/useCmsSeo.d.ts +7 -0
- package/dist/runtime/app/useCmsSeo.js +31 -0
- package/dist/runtime/collections.d.ts +25 -0
- package/dist/runtime/collections.js +81 -0
- package/dist/runtime/editor-link.d.ts +19 -0
- package/dist/runtime/editor-link.js +31 -0
- package/dist/runtime/fields.d.ts +33 -1
- package/dist/runtime/fields.js +143 -7
- package/dist/runtime/pages.d.ts +11 -2
- package/dist/runtime/pages.js +37 -7
- package/dist/runtime/preview.d.ts +5 -2
- package/dist/runtime/preview.js +2 -2
- package/dist/runtime/schema.d.ts +187 -13
- package/dist/runtime/schema.js +115 -1
- package/dist/runtime/seo.d.ts +22 -0
- package/dist/runtime/seo.js +33 -0
- package/dist/runtime/server/access.get.d.ts +4 -0
- package/dist/runtime/server/access.get.js +8 -0
- package/dist/runtime/server/access.js +1 -1
- package/dist/runtime/server/collection.get.d.ts +12 -0
- package/dist/runtime/server/collection.get.js +28 -0
- package/dist/runtime/server/dev-images.d.ts +4 -1
- package/dist/runtime/server/dev-images.js +28 -2
- package/dist/runtime/server/entries.d.ts +53 -0
- package/dist/runtime/server/entries.js +83 -1
- package/dist/runtime/server/entry.delete.d.ts +4 -0
- package/dist/runtime/server/entry.delete.js +31 -0
- package/dist/runtime/server/entry.get.js +6 -6
- package/dist/runtime/server/entry.put.d.ts +1 -0
- package/dist/runtime/server/entry.put.js +20 -8
- package/dist/runtime/server/file-store.d.ts +14 -3
- package/dist/runtime/server/file-store.js +44 -9
- package/dist/runtime/server/image.delete.d.ts +4 -0
- package/dist/runtime/server/image.delete.js +24 -0
- package/dist/runtime/server/images.get.d.ts +5 -0
- package/dist/runtime/server/images.get.js +11 -0
- package/dist/runtime/server/library.d.ts +14 -0
- package/dist/runtime/server/library.js +72 -0
- package/dist/runtime/server/links.get.d.ts +7 -0
- package/dist/runtime/server/links.get.js +35 -0
- package/dist/runtime/server/media.d.ts +74 -0
- package/dist/runtime/server/media.js +88 -0
- package/dist/runtime/server/schema.get.d.ts +8 -0
- package/dist/runtime/server/schema.get.js +17 -7
- package/dist/runtime/server/store.d.ts +11 -2
- package/dist/runtime/server/store.js +110 -20
- package/dist/runtime/server/upload.post.js +3 -2
- package/dist/runtime/server/versions.get.d.ts +20 -0
- package/dist/runtime/server/versions.get.js +36 -0
- package/package.json +3 -2
- package/dist/editor/assets/Button-Dai6Ph2c.js +0 -6
- package/dist/editor/assets/RichTextInput-CdL9ZFoP.js +0 -271
- package/dist/editor/assets/Toast-BaUcosq6.js +0 -13
- package/dist/editor/assets/index-CP6dGIyl.css +0 -2
- package/dist/editor/assets/index-Du-lvx2V.js +0 -16
package/dist/runtime/fields.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isCmsDate } from "./schema.js";
|
|
1
2
|
// src/runtime/fields.ts
|
|
2
3
|
function isField(value) {
|
|
3
4
|
return typeof value.type === "string";
|
|
@@ -24,17 +25,38 @@ function resolveField(field, stored, images) {
|
|
|
24
25
|
case "text":
|
|
25
26
|
case "richText": return typeof stored === "string" ? stored : field.default;
|
|
26
27
|
case "toggle": return typeof stored === "boolean" ? stored : field.default;
|
|
28
|
+
case "number":
|
|
29
|
+
if (stored === null && !field.required) return null;
|
|
30
|
+
return isNumber(stored) ? stored : field.default;
|
|
31
|
+
case "date":
|
|
32
|
+
if (stored === null && !field.required) return null;
|
|
33
|
+
return isCmsDate(stored, field.time) ? stored : field.default;
|
|
34
|
+
case "select": return field.options.some((option) => option.value === stored) ? stored : field.default;
|
|
27
35
|
case "link": return toLink(stored) ?? field.default;
|
|
28
36
|
case "image": {
|
|
29
37
|
const image = stored === null ? null : toImage(stored);
|
|
30
38
|
if (image === null && !field.required) return null;
|
|
31
|
-
const shown = image &&
|
|
39
|
+
const shown = image && showImage(image, images);
|
|
32
40
|
if (shown) return shown;
|
|
33
|
-
return field.default && (
|
|
41
|
+
return field.default && (showImage(field.default, images) ?? null);
|
|
34
42
|
}
|
|
35
43
|
case "list": return (Array.isArray(stored) ? stored : field.default).map((item) => resolveValue(field.item, item, images));
|
|
44
|
+
case "group": return resolveValue(field.fields, stored, images);
|
|
36
45
|
}
|
|
37
46
|
}
|
|
47
|
+
function showImage(image, images) {
|
|
48
|
+
const shown = images(image);
|
|
49
|
+
return shown && withFocus(shown, image.focus);
|
|
50
|
+
}
|
|
51
|
+
/** The image with `focus` and the `position` the page crops by, or without both. */
|
|
52
|
+
function withFocus(image, focus) {
|
|
53
|
+
const { focus: _, position: __, ...rest } = image;
|
|
54
|
+
return focus ? {
|
|
55
|
+
...rest,
|
|
56
|
+
focus,
|
|
57
|
+
position: `${focus.x}% ${focus.y}%`
|
|
58
|
+
} : rest;
|
|
59
|
+
}
|
|
38
60
|
var InvalidValue = class extends Error {};
|
|
39
61
|
/**
|
|
40
62
|
* Checks an edit against the schema and brings it into the form it's stored in. Takes what GET
|
|
@@ -55,15 +77,39 @@ const MAX_ITEMS = 500;
|
|
|
55
77
|
function normalizeField(field, input, at) {
|
|
56
78
|
switch (field.type) {
|
|
57
79
|
case "text":
|
|
58
|
-
case "richText":
|
|
80
|
+
case "richText": {
|
|
59
81
|
if (typeof input !== "string") throw new InvalidValue(`${at}: expected text`);
|
|
82
|
+
const problem = fieldProblem(field, input);
|
|
83
|
+
if (problem) throw new InvalidValue(`${at}: ${describeProblem(problem)}`);
|
|
60
84
|
return input;
|
|
85
|
+
}
|
|
61
86
|
case "toggle":
|
|
62
87
|
if (typeof input !== "boolean") throw new InvalidValue(`${at}: expected true or false`);
|
|
63
88
|
return input;
|
|
89
|
+
case "number": {
|
|
90
|
+
if (input === null && !field.required) return null;
|
|
91
|
+
if (input === null) throw new InvalidValue(`${at}: required`);
|
|
92
|
+
if (!isNumber(input)) throw new InvalidValue(`${at}: expected a number`);
|
|
93
|
+
const problem = fieldProblem(field, input);
|
|
94
|
+
if (problem) throw new InvalidValue(`${at}: ${describeProblem(problem)}`);
|
|
95
|
+
return input;
|
|
96
|
+
}
|
|
97
|
+
case "date": {
|
|
98
|
+
if (input === null && !field.required) return null;
|
|
99
|
+
if (input === null) throw new InvalidValue(`${at}: required`);
|
|
100
|
+
if (!isCmsDate(input, field.time)) throw new InvalidValue(`${at}: expected a date as ${field.time ? "YYYY-MM-DDTHH:mm" : "YYYY-MM-DD"}`);
|
|
101
|
+
const problem = fieldProblem(field, input);
|
|
102
|
+
if (problem) throw new InvalidValue(`${at}: ${describeProblem(problem)}`);
|
|
103
|
+
return input;
|
|
104
|
+
}
|
|
105
|
+
case "select":
|
|
106
|
+
if (!field.options.some((option) => option.value === input)) throw new InvalidValue(`${at}: expected one of ${field.options.map((o) => JSON.stringify(o.value)).join(", ")}`);
|
|
107
|
+
return input;
|
|
64
108
|
case "link": {
|
|
65
109
|
const value = toLink(input);
|
|
66
110
|
if (!value) throw new InvalidValue(`${at}: expected { label, href } with a safe href`);
|
|
111
|
+
const problem = fieldProblem(field, value);
|
|
112
|
+
if (problem) throw new InvalidValue(`${at}: ${describeProblem(problem)}`);
|
|
67
113
|
return value;
|
|
68
114
|
}
|
|
69
115
|
case "image": {
|
|
@@ -76,8 +122,84 @@ function normalizeField(field, input, at) {
|
|
|
76
122
|
if (!Array.isArray(input)) throw new InvalidValue(`${at}: expected a list`);
|
|
77
123
|
if (input.length > MAX_ITEMS) throw new InvalidValue(`${at}: at most ${MAX_ITEMS} items`);
|
|
78
124
|
return input.map((item, i) => normalizeValue(field.item, item, `${at}[${i}]`));
|
|
125
|
+
case "group": return normalizeValue(field.fields, input, at);
|
|
79
126
|
}
|
|
80
127
|
}
|
|
128
|
+
const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
129
|
+
let graphemes;
|
|
130
|
+
/** Characters as people count them: 👋🏽 is one, not four UTF-16 units. */
|
|
131
|
+
function textLength(value) {
|
|
132
|
+
graphemes ??= new Intl.Segmenter();
|
|
133
|
+
let length = 0;
|
|
134
|
+
for (const _ of graphemes.segment(value)) length++;
|
|
135
|
+
return length;
|
|
136
|
+
}
|
|
137
|
+
/** The rule a field's value breaks, if any. Values of the wrong type are `normalizeValue`'s job. */
|
|
138
|
+
function fieldProblem(field, value) {
|
|
139
|
+
if (field.type === "text" || field.type === "richText") {
|
|
140
|
+
if (typeof value !== "string") return void 0;
|
|
141
|
+
if (field.required && !value.trim()) return { kind: "required" };
|
|
142
|
+
const length = textLength(value);
|
|
143
|
+
if (field.maxLength !== void 0 && length > field.maxLength) return {
|
|
144
|
+
kind: "tooLong",
|
|
145
|
+
max: field.maxLength,
|
|
146
|
+
length
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
if (field.type === "number") {
|
|
150
|
+
if (value === null || value === void 0) return field.required ? { kind: "required" } : void 0;
|
|
151
|
+
if (!isNumber(value)) return void 0;
|
|
152
|
+
if (field.min !== void 0 && value < field.min) return {
|
|
153
|
+
kind: "tooSmall",
|
|
154
|
+
min: field.min
|
|
155
|
+
};
|
|
156
|
+
if (field.max !== void 0 && value > field.max) return {
|
|
157
|
+
kind: "tooLarge",
|
|
158
|
+
max: field.max
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (field.type === "date") {
|
|
162
|
+
if (value === null || value === void 0 || value === "") return field.required ? { kind: "required" } : void 0;
|
|
163
|
+
if (!isCmsDate(value, field.time)) return void 0;
|
|
164
|
+
if (field.min !== void 0 && value < field.min) return {
|
|
165
|
+
kind: "tooEarly",
|
|
166
|
+
min: field.min
|
|
167
|
+
};
|
|
168
|
+
if (field.max !== void 0 && value > field.max) return {
|
|
169
|
+
kind: "tooLate",
|
|
170
|
+
max: field.max
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
if (field.type === "link" && field.required) {
|
|
174
|
+
if (!(isPlain(value) && typeof value.href === "string" ? value.href.trim() : "")) return { kind: "required" };
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function describeProblem(problem) {
|
|
178
|
+
switch (problem.kind) {
|
|
179
|
+
case "required": return "required";
|
|
180
|
+
case "tooLong": return `at most ${problem.max} characters`;
|
|
181
|
+
case "tooSmall": return `at least ${problem.min}`;
|
|
182
|
+
case "tooLarge": return `at most ${problem.max}`;
|
|
183
|
+
case "tooEarly": return `not before ${problem.min}`;
|
|
184
|
+
case "tooLate": return `not after ${problem.max}`;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** Every field in `value` that breaks its rule, with its path (`hero.title`, `hours[2].day`). */
|
|
188
|
+
function findProblems(fields, value, path = "") {
|
|
189
|
+
if (!isPlain(value)) return [];
|
|
190
|
+
return Object.entries(fields).flatMap(([name, field]) => {
|
|
191
|
+
const at = path ? `${path}.${name}` : name;
|
|
192
|
+
const input = value[name];
|
|
193
|
+
if (!isField(field)) return findProblems(field, input, at);
|
|
194
|
+
if (field.type === "group") return findProblems(field.fields, input, at);
|
|
195
|
+
if (field.type === "list") return Array.isArray(input) ? input.flatMap((item, i) => findProblems(field.item, item, `${at}[${i}]`)) : [];
|
|
196
|
+
const problem = fieldProblem(field, input);
|
|
197
|
+
return problem ? [{
|
|
198
|
+
path: at,
|
|
199
|
+
problem
|
|
200
|
+
}] : [];
|
|
201
|
+
});
|
|
202
|
+
}
|
|
81
203
|
const SAFE_HREF = /^(?:\/(?!\/)|#|\?|https?:\/\/|mailto:|tel:)/i;
|
|
82
204
|
const SAFE_SRC = /^(?:\/(?!\/)|https:\/\/)/i;
|
|
83
205
|
const FILE_ID = /^[A-Za-z0-9_-]{1,100}$/;
|
|
@@ -96,14 +218,28 @@ function toLink(input) {
|
|
|
96
218
|
function toImage(input) {
|
|
97
219
|
if (typeof input === "string") input = { src: input };
|
|
98
220
|
if (!isPlain(input)) return void 0;
|
|
99
|
-
const
|
|
221
|
+
const focus = toFocus(input.focus);
|
|
222
|
+
const extra = {
|
|
223
|
+
...typeof input.alt === "string" && input.alt ? { alt: input.alt } : {},
|
|
224
|
+
...focus ? { focus } : {}
|
|
225
|
+
};
|
|
100
226
|
if (typeof input.id === "string") return FILE_ID.test(input.id) ? {
|
|
101
227
|
id: input.id,
|
|
102
|
-
...
|
|
228
|
+
...extra
|
|
103
229
|
} : void 0;
|
|
104
230
|
if (typeof input.src === "string" && SAFE_SRC.test(input.src)) return {
|
|
105
231
|
src: input.src,
|
|
106
|
-
...
|
|
232
|
+
...extra
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function toFocus(input) {
|
|
236
|
+
if (!isPlain(input)) return void 0;
|
|
237
|
+
const { x, y } = input;
|
|
238
|
+
if (!isPercent(x) || !isPercent(y)) return void 0;
|
|
239
|
+
return {
|
|
240
|
+
x: Math.round(x * 10) / 10,
|
|
241
|
+
y: Math.round(y * 10) / 10
|
|
107
242
|
};
|
|
108
243
|
}
|
|
109
|
-
|
|
244
|
+
const isPercent = (n) => typeof n === "number" && Number.isFinite(n) && n >= 0 && n <= 100;
|
|
245
|
+
export { InvalidValue, fieldProblem, findProblems, isField, isSafeHref, normalizeValue, resolveValue, staticImages, textLength, toFocus, withFocus };
|
package/dist/runtime/pages.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
import { CmsFields, CmsPage } from "./schema.js";
|
|
1
|
+
import { CmsCollection, CmsFields, CmsPage } from "./schema.js";
|
|
2
2
|
/** The pages the app defines in `cms/*.ts`, by key. */
|
|
3
|
-
export declare const pages: Map<string, CmsPage<string, CmsFields>>;
|
|
3
|
+
export declare const pages: Map<string, CmsPage<string, CmsFields>>;
|
|
4
|
+
/** The collections the app defines in `cms/*.ts`, by key. */
|
|
5
|
+
export declare const collections: Map<string, CmsCollection<string, CmsFields>>;
|
|
6
|
+
/** The fields an entry is checked against: its page's, or its collection's for an item. */
|
|
7
|
+
export declare function fieldsOf(key: string): CmsFields | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Whether `key` is under a collection without being one of its items (`posts/a/b`, `posts/Big`):
|
|
10
|
+
* saved, it would never be listed or shown.
|
|
11
|
+
*/
|
|
12
|
+
export declare function strayUnderCollection(key: string): boolean;
|
package/dist/runtime/pages.js
CHANGED
|
@@ -1,14 +1,44 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { itemOf } from "./collections.js";
|
|
2
|
+
import { isCmsCollection, isCmsPage } from "./schema.js";
|
|
2
3
|
import { modules } from "#wervt-cms/pages";
|
|
3
4
|
// src/runtime/pages.ts
|
|
5
|
+
function add(known, value) {
|
|
6
|
+
const before = known.get(value.key);
|
|
7
|
+
if (before && before !== value) console.warn(`[cms] \`${value.key}\` is defined twice in cms/, the last one counts`);
|
|
8
|
+
known.set(value.key, value);
|
|
9
|
+
}
|
|
4
10
|
function collect(found) {
|
|
5
11
|
const pages = /* @__PURE__ */ new Map();
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
const collections = /* @__PURE__ */ new Map();
|
|
13
|
+
for (const module of found) for (const value of Object.values(module)) if (isCmsPage(value)) add(pages, value);
|
|
14
|
+
else if (isCmsCollection(value)) add(collections, value);
|
|
15
|
+
for (const key of pages.keys()) {
|
|
16
|
+
const clash = collections.has(key) ? key : itemOf(collections, key)?.collection.key;
|
|
17
|
+
if (clash) {
|
|
18
|
+
console.warn(`[cms] page \`${key}\` clashes with the collection \`${clash}\` and is left out`);
|
|
19
|
+
pages.delete(key);
|
|
20
|
+
}
|
|
9
21
|
}
|
|
10
|
-
return
|
|
22
|
+
return {
|
|
23
|
+
pages,
|
|
24
|
+
collections
|
|
25
|
+
};
|
|
11
26
|
}
|
|
27
|
+
const found = collect(modules);
|
|
12
28
|
/** The pages the app defines in `cms/*.ts`, by key. */
|
|
13
|
-
const pages =
|
|
14
|
-
|
|
29
|
+
const pages = found.pages;
|
|
30
|
+
/** The collections the app defines in `cms/*.ts`, by key. */
|
|
31
|
+
const collections = found.collections;
|
|
32
|
+
/** The fields an entry is checked against: its page's, or its collection's for an item. */
|
|
33
|
+
function fieldsOf(key) {
|
|
34
|
+
return pages.get(key)?.fields ?? itemOf(collections, key)?.collection.fields;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether `key` is under a collection without being one of its items (`posts/a/b`, `posts/Big`):
|
|
38
|
+
* saved, it would never be listed or shown.
|
|
39
|
+
*/
|
|
40
|
+
function strayUnderCollection(key) {
|
|
41
|
+
const at = key.indexOf("/");
|
|
42
|
+
return at > 0 && collections.has(key.slice(0, at)) && !itemOf(collections, key);
|
|
43
|
+
}
|
|
44
|
+
export { collections, fieldsOf, pages, strayUnderCollection };
|
|
@@ -9,12 +9,15 @@ export interface SiteMessage {
|
|
|
9
9
|
type: 'page';
|
|
10
10
|
path: string;
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* From the editor: show `value` for `key` until the next one; `null` goes back to what's saved,
|
|
14
|
+
* for an item the editor never saved or just deleted.
|
|
15
|
+
*/
|
|
13
16
|
export interface EditorMessage {
|
|
14
17
|
source: typeof source;
|
|
15
18
|
type: 'draft';
|
|
16
19
|
key: string;
|
|
17
|
-
value: Record<string, unknown
|
|
20
|
+
value: Record<string, unknown> | null;
|
|
18
21
|
}
|
|
19
22
|
export declare const siteMessage: (path: string) => SiteMessage;
|
|
20
23
|
/** Values are copied as JSON: `postMessage` refuses Vue's reactive proxies. */
|
package/dist/runtime/preview.js
CHANGED
|
@@ -14,7 +14,7 @@ const editorMessage = (key, value) => ({
|
|
|
14
14
|
source,
|
|
15
15
|
type: "draft",
|
|
16
16
|
key,
|
|
17
|
-
value: JSON.parse(JSON.stringify(value ?? {}))
|
|
17
|
+
value: value === null ? null : JSON.parse(JSON.stringify(value ?? {}))
|
|
18
18
|
});
|
|
19
19
|
/**
|
|
20
20
|
* The message's data if it came from `from` on this origin and has the expected shape. Anything
|
|
@@ -25,7 +25,7 @@ function readMessage(event, from, origin, type) {
|
|
|
25
25
|
const data = event.data;
|
|
26
26
|
if (data?.source !== source || data.type !== type) return;
|
|
27
27
|
if (type === "page" && typeof data.path !== "string") return;
|
|
28
|
-
if (type === "draft" && (typeof data.key !== "string" || typeof data.value !== "object" || data.value
|
|
28
|
+
if (type === "draft" && (typeof data.key !== "string" || typeof data.value !== "object" || Array.isArray(data.value))) return;
|
|
29
29
|
return data;
|
|
30
30
|
}
|
|
31
31
|
/** `/menu/` and `/menu?x#y` are `/menu`. */
|
package/dist/runtime/schema.d.ts
CHANGED
|
@@ -1,33 +1,93 @@
|
|
|
1
1
|
interface FieldBase<T> {
|
|
2
2
|
/** Shown in the editor instead of the field's name. */
|
|
3
3
|
label?: string;
|
|
4
|
+
/** Shown under the field in the editor: what goes in, where it shows. */
|
|
5
|
+
hint?: string;
|
|
4
6
|
default: T;
|
|
5
7
|
}
|
|
6
|
-
|
|
8
|
+
interface TextRules {
|
|
9
|
+
/** Can't be saved empty (or only spaces). */
|
|
10
|
+
required?: boolean;
|
|
11
|
+
/** At most this many characters; the editor counts them as they're typed. */
|
|
12
|
+
maxLength?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface TextField extends FieldBase<string>, TextRules {
|
|
7
15
|
type: 'text';
|
|
8
16
|
/** Several lines in the editor. */
|
|
9
17
|
multiline?: boolean;
|
|
10
18
|
}
|
|
11
|
-
|
|
19
|
+
/** `maxLength` counts the Markdown, formatting included. */
|
|
20
|
+
export interface RichTextField extends FieldBase<string>, TextRules {
|
|
12
21
|
type: 'richText';
|
|
13
22
|
}
|
|
14
23
|
export interface ToggleField extends FieldBase<boolean> {
|
|
15
24
|
type: 'toggle';
|
|
16
25
|
}
|
|
26
|
+
export interface NumberField<Required extends boolean = boolean> extends FieldBase<number | null> {
|
|
27
|
+
type: 'number';
|
|
28
|
+
/** With a default the page always gets a number, and the editor can't empty it. */
|
|
29
|
+
required: Required;
|
|
30
|
+
min?: number;
|
|
31
|
+
max?: number;
|
|
32
|
+
/** How far the editor's buttons move it; any number in range can still be typed. */
|
|
33
|
+
step?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A day (`2026-08-05`) or, with `time`, a day and a time (`2026-08-05T18:30`), as written: no time
|
|
37
|
+
* zone, so the site shows what the editor picked wherever it's rendered.
|
|
38
|
+
*/
|
|
39
|
+
export interface DateField<Required extends boolean = boolean> extends FieldBase<string | null> {
|
|
40
|
+
type: 'date';
|
|
41
|
+
/** With a default the page always gets a date, and the editor can't empty it. */
|
|
42
|
+
required: Required;
|
|
43
|
+
/** A time of day too. */
|
|
44
|
+
time?: boolean;
|
|
45
|
+
/** The earliest it may be, written like the value. */
|
|
46
|
+
min?: string;
|
|
47
|
+
/** The latest it may be, written like the value. */
|
|
48
|
+
max?: string;
|
|
49
|
+
}
|
|
50
|
+
export interface SelectOption<V extends string = string> {
|
|
51
|
+
value: V;
|
|
52
|
+
/** Shown in the editor instead of the value. */
|
|
53
|
+
label?: string;
|
|
54
|
+
}
|
|
55
|
+
export interface SelectField<V extends string = string> extends FieldBase<V> {
|
|
56
|
+
type: 'select';
|
|
57
|
+
options: SelectOption<V>[];
|
|
58
|
+
}
|
|
17
59
|
export interface LinkField extends FieldBase<CmsLink> {
|
|
18
60
|
type: 'link';
|
|
61
|
+
/** Can't be saved without an address. */
|
|
62
|
+
required?: boolean;
|
|
19
63
|
}
|
|
20
64
|
export interface ImageField<Required extends boolean = boolean> extends FieldBase<StoredImage | null> {
|
|
21
65
|
type: 'image';
|
|
22
66
|
/** With a default the page always gets an image, and the editor can only replace it. */
|
|
23
67
|
required: Required;
|
|
68
|
+
/**
|
|
69
|
+
* The editor lets the owner mark what must stay in view when the image is cropped. Only worth
|
|
70
|
+
* it where the page crops (`object-cover`) and sets `object-position` to the image's `position`.
|
|
71
|
+
*/
|
|
72
|
+
focus?: boolean;
|
|
24
73
|
}
|
|
25
74
|
export interface ListField<Item extends CmsFields = CmsFields> extends FieldBase<InputOf<Item>[]> {
|
|
26
75
|
type: 'list';
|
|
27
76
|
item: Item;
|
|
28
77
|
}
|
|
29
|
-
|
|
30
|
-
|
|
78
|
+
/** Fields shown together under a label; the page gets them as one object, like a nested one. */
|
|
79
|
+
export interface GroupField<F extends CmsFields = CmsFields> {
|
|
80
|
+
type: 'group';
|
|
81
|
+
/** Shown in the editor instead of the group's name. */
|
|
82
|
+
label?: string;
|
|
83
|
+
/** Shown under the group's title in the editor. */
|
|
84
|
+
hint?: string;
|
|
85
|
+
fields: F;
|
|
86
|
+
/** Made by `seo()`: the editor labels and explains its fields itself. */
|
|
87
|
+
seo?: true;
|
|
88
|
+
}
|
|
89
|
+
export type CmsField = TextField | RichTextField | ToggleField | NumberField | DateField | SelectField | LinkField | ImageField | ListField | GroupField;
|
|
90
|
+
/** Fields by name; a nested object is a group of fields shown together, `group()` gives it a label. */
|
|
31
91
|
export interface CmsFields {
|
|
32
92
|
[name: string]: CmsField | CmsFields;
|
|
33
93
|
}
|
|
@@ -39,13 +99,19 @@ export interface CmsLink {
|
|
|
39
99
|
* An image as it's stored: a file the editor uploaded to the app's bucket (`id`), or a path or
|
|
40
100
|
* URL the app ships with (`src`, e.g. `/hero.jpg` from `public/`).
|
|
41
101
|
*/
|
|
42
|
-
export type StoredImage = {
|
|
102
|
+
export type StoredImage = ({
|
|
43
103
|
id: string;
|
|
44
|
-
alt?: string;
|
|
45
104
|
} | {
|
|
46
105
|
src: string;
|
|
106
|
+
}) & {
|
|
47
107
|
alt?: string;
|
|
108
|
+
focus?: ImageFocus;
|
|
48
109
|
};
|
|
110
|
+
/** The point that stays in view when the image is cropped, in percent from the top left. */
|
|
111
|
+
export interface ImageFocus {
|
|
112
|
+
x: number;
|
|
113
|
+
y: number;
|
|
114
|
+
}
|
|
49
115
|
/** An image as the page gets it. */
|
|
50
116
|
export interface CmsImage {
|
|
51
117
|
src: string;
|
|
@@ -54,11 +120,15 @@ export interface CmsImage {
|
|
|
54
120
|
alt: string;
|
|
55
121
|
/** The uploaded file, for the editor. */
|
|
56
122
|
id?: string;
|
|
123
|
+
/** Set by the owner on an `image(…, { focus: true })`. */
|
|
124
|
+
focus?: ImageFocus;
|
|
125
|
+
/** `focus` as CSS `object-position`, e.g. `'30% 20%'`; unset when there is none. */
|
|
126
|
+
position?: string;
|
|
57
127
|
}
|
|
58
128
|
/** The value the page gets. */
|
|
59
|
-
export type CmsValue<F extends CmsFields> = { [K in keyof F]: F[K] extends TextField | RichTextField ? string : F[K] extends ToggleField ? boolean : F[K] extends LinkField ? CmsLink : F[K] extends ImageField<true> ? CmsImage : F[K] extends ImageField ? CmsImage | null : F[K] extends ListField<infer Item> ? CmsValue<Item>[] : F[K] extends CmsFields ? CmsValue<F[K]> : never; };
|
|
129
|
+
export type CmsValue<F extends CmsFields> = { [K in keyof F]: F[K] extends TextField | RichTextField ? string : F[K] extends ToggleField ? boolean : F[K] extends NumberField<true> ? number : F[K] extends NumberField ? number | null : F[K] extends DateField<true> ? string : F[K] extends DateField ? string | null : F[K] extends SelectField<infer V> ? V : F[K] extends LinkField ? CmsLink : F[K] extends ImageField<true> ? CmsImage : F[K] extends ImageField ? CmsImage | null : F[K] extends ListField<infer Item> ? CmsValue<Item>[] : F[K] extends GroupField<infer G> ? CmsValue<G> : F[K] extends CmsFields ? CmsValue<F[K]> : never; };
|
|
60
130
|
/** What a default or an edit may contain: a missing field keeps its default. */
|
|
61
|
-
export type InputOf<F extends CmsFields> = { [K in keyof F]?: F[K] extends TextField | RichTextField ? string : F[K] extends ToggleField ? boolean : F[K] extends LinkField ? CmsLink : F[K] extends ImageField ? StoredImage | string | null : F[K] extends ListField<infer Item> ? InputOf<Item>[] : F[K] extends CmsFields ? InputOf<F[K]> : never; };
|
|
131
|
+
export type InputOf<F extends CmsFields> = { [K in keyof F]?: F[K] extends TextField | RichTextField ? string : F[K] extends ToggleField ? boolean : F[K] extends NumberField ? number | null : F[K] extends DateField ? string | null : F[K] extends SelectField<infer V> ? V : F[K] extends LinkField ? CmsLink : F[K] extends ImageField ? StoredImage | string | null : F[K] extends ListField<infer Item> ? InputOf<Item>[] : F[K] extends GroupField<infer G> ? InputOf<G> : F[K] extends CmsFields ? InputOf<F[K]> : never; };
|
|
62
132
|
export interface CmsPage<Key extends string = string, F extends CmsFields = CmsFields> {
|
|
63
133
|
readonly __cmsPage: true;
|
|
64
134
|
key: Key;
|
|
@@ -70,19 +140,68 @@ export interface CmsPage<Key extends string = string, F extends CmsFields = CmsF
|
|
|
70
140
|
}
|
|
71
141
|
type Options = {
|
|
72
142
|
label?: string;
|
|
143
|
+
hint?: string;
|
|
73
144
|
};
|
|
74
|
-
export declare function text(value?: string, options?: Options & {
|
|
145
|
+
export declare function text(value?: string, options?: Options & TextRules & {
|
|
75
146
|
multiline?: boolean;
|
|
76
147
|
}): TextField;
|
|
77
148
|
/** Markdown (Comark). */
|
|
78
|
-
export declare function richText(value?: string, options?: Options): RichTextField;
|
|
149
|
+
export declare function richText(value?: string, options?: Options & TextRules): RichTextField;
|
|
79
150
|
export declare function toggle(value?: boolean, options?: Options): ToggleField;
|
|
80
|
-
|
|
151
|
+
/** `number(4.8)` is always a number; `number()` is `null` until one is typed. */
|
|
152
|
+
export declare function number(value: number, options?: Options & NumberRules): NumberField<true>;
|
|
153
|
+
export declare function number(value?: null, options?: Options & NumberRules): NumberField<false>;
|
|
154
|
+
type NumberRules = Pick<NumberField, 'min' | 'max' | 'step'>;
|
|
155
|
+
/**
|
|
156
|
+
* `date('2026-08-05')` is always a date; `date()` is `null` until one is picked. With
|
|
157
|
+
* `{ time: true }` it's a day and a time: `date('2026-08-05T18:30', { time: true })`.
|
|
158
|
+
*/
|
|
159
|
+
export declare function date(value: string, options?: Options & DateRules): DateField<true>;
|
|
160
|
+
export declare function date(value?: null, options?: Options & DateRules): DateField<false>;
|
|
161
|
+
type DateRules = Pick<DateField, 'time' | 'min' | 'max'>;
|
|
162
|
+
/**
|
|
163
|
+
* Whether `value` is a day that exists (`2026-02-30` isn't) in the field's format, and with `time`
|
|
164
|
+
* a time of day (`24:00` isn't). Written alike, two values compare as strings.
|
|
165
|
+
*/
|
|
166
|
+
export declare function isCmsDate(value: unknown, time?: boolean): value is string;
|
|
167
|
+
/**
|
|
168
|
+
* One of a few values, picked from a menu; the page gets the value:
|
|
169
|
+
* `select(['cut', 'color'], 'cut')` or with labels, `select([{ value: 'cut', label: 'Haircut' }])`.
|
|
170
|
+
* Without a value the first option is the default.
|
|
171
|
+
*/
|
|
172
|
+
export declare function select<const V extends string>(options: readonly (V | SelectOption<V>)[], value?: NoInfer<V>, fieldOptions?: Options): SelectField<V>;
|
|
173
|
+
export declare function link(value?: CmsLink, options?: Options & {
|
|
174
|
+
required?: boolean;
|
|
175
|
+
}): LinkField;
|
|
176
|
+
type ImageOptions = Options & {
|
|
177
|
+
focus?: boolean;
|
|
178
|
+
};
|
|
81
179
|
/** `image('/hero.jpg')` always has an image; `image()` is `null` until one is chosen. */
|
|
82
|
-
export declare function image(value: string | StoredImage, options?:
|
|
83
|
-
export declare function image(value?: null, options?:
|
|
180
|
+
export declare function image(value: string | StoredImage, options?: ImageOptions): ImageField<true>;
|
|
181
|
+
export declare function image(value?: null, options?: ImageOptions): ImageField<false>;
|
|
84
182
|
/** Repeated groups of fields, such as team members or opening hours, in an order of their own. */
|
|
85
183
|
export declare function list<Item extends CmsFields>(item: Item, value?: InputOf<Item>[], options?: Options): ListField<Item>;
|
|
184
|
+
/** Like a nested object, with a label in the editor: `group({ show: toggle(), text: text() }, { label: 'Notice' })`. */
|
|
185
|
+
export declare function group<F extends CmsFields>(fields: F, options?: Options): GroupField<F>;
|
|
186
|
+
/** The fields `seo()` makes. */
|
|
187
|
+
export type SeoFields = {
|
|
188
|
+
title: TextField;
|
|
189
|
+
description: TextField;
|
|
190
|
+
image: ImageField;
|
|
191
|
+
};
|
|
192
|
+
/** What `seo()` starts with; anything left out stays empty until an editor fills it in. */
|
|
193
|
+
export interface SeoDefaults {
|
|
194
|
+
title?: string;
|
|
195
|
+
description?: string;
|
|
196
|
+
/** A path from `public/` or a URL; shown when the page is shared. */
|
|
197
|
+
image?: string | StoredImage | null;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* How the page shows up in search results and when it's shared: its title, description and
|
|
201
|
+
* image, editable like any other field. `useCmsSeo(key)` on the page turns them into its
|
|
202
|
+
* `<title>` and meta tags; an empty one leaves the page's own.
|
|
203
|
+
*/
|
|
204
|
+
export declare function seo(value?: SeoDefaults, options?: Options): GroupField<SeoFields>;
|
|
86
205
|
/**
|
|
87
206
|
* The content of one page, read with `useCms(key)`:
|
|
88
207
|
*
|
|
@@ -98,6 +217,61 @@ export declare function defineCmsPage<const Key extends string, F extends CmsFie
|
|
|
98
217
|
path?: string;
|
|
99
218
|
}): CmsPage<Key, F>;
|
|
100
219
|
export declare function isCmsPage(value: unknown): value is CmsPage;
|
|
220
|
+
/** A field of the collection, or `-` and the field for the other way round (`'-date'`). */
|
|
221
|
+
export type CollectionSort<F extends CmsFields> = Extract<keyof F, string> | `-${Extract<keyof F, string>}`;
|
|
222
|
+
export interface CmsCollection<Key extends string = string, F extends CmsFields = CmsFields> {
|
|
223
|
+
readonly __cmsCollection: true;
|
|
224
|
+
key: Key;
|
|
225
|
+
/** Shown in the editor instead of the key. */
|
|
226
|
+
label?: string;
|
|
227
|
+
/** Where an item is on the site, its slug in place of `[slug]`: `/blog/[slug]`. */
|
|
228
|
+
path?: string;
|
|
229
|
+
fields: F;
|
|
230
|
+
/**
|
|
231
|
+
* The text field that names an item in the editor's list and gives a new one its slug; the
|
|
232
|
+
* first text field when missing.
|
|
233
|
+
*/
|
|
234
|
+
titleField?: Extract<keyof F, string>;
|
|
235
|
+
/** The order items come in, in the editor and from `useCmsCollection`; by title when missing. */
|
|
236
|
+
sort?: CollectionSort<F>;
|
|
237
|
+
}
|
|
238
|
+
/** One item as the page gets it: its fields, its slug, and where it is when the collection has a `path`. */
|
|
239
|
+
export type CmsItem<F extends CmsFields> = CmsValue<F> & {
|
|
240
|
+
slug: string;
|
|
241
|
+
path?: string;
|
|
242
|
+
};
|
|
243
|
+
/**
|
|
244
|
+
* Many items of one kind, such as blog posts or events, each saved on its own and with a page of
|
|
245
|
+
* its own when the collection has a `path`. Read with `useCmsCollection(key)` and
|
|
246
|
+
* `useCmsItem(key, slug)`:
|
|
247
|
+
*
|
|
248
|
+
* ```ts
|
|
249
|
+
* // cms/posts.ts
|
|
250
|
+
* export default defineCmsCollection(
|
|
251
|
+
* 'posts',
|
|
252
|
+
* { title: text('', { required: true }), date: date(), body: richText() },
|
|
253
|
+
* { label: 'Blog', path: '/blog/[slug]', sort: '-date' },
|
|
254
|
+
* )
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
export declare function defineCmsCollection<const Key extends string, F extends CmsFields>(key: Key, fields: F, options?: Options & {
|
|
258
|
+
path?: string;
|
|
259
|
+
titleField?: NoInfer<Extract<keyof F, string>>;
|
|
260
|
+
sort?: NoInfer<CollectionSort<F>>;
|
|
261
|
+
}): CmsCollection<Key, F>;
|
|
262
|
+
export declare function isCmsCollection(value: unknown): value is CmsCollection;
|
|
263
|
+
/**
|
|
264
|
+
* Collections by key, filled in by the module from the app's `cms/*.ts`:
|
|
265
|
+
* `useCmsCollection('posts')` is typed from it.
|
|
266
|
+
*/
|
|
267
|
+
export interface CmsCollections {}
|
|
268
|
+
/** The collections a `cms/*.ts` module exports, by key. Used by the generated types. */
|
|
269
|
+
export type CollectionsOf<M> = { [C in M[keyof M] as C extends {
|
|
270
|
+
__cmsCollection: true;
|
|
271
|
+
key: infer K extends string;
|
|
272
|
+
} ? K : never]: C extends {
|
|
273
|
+
fields: infer F extends CmsFields;
|
|
274
|
+
} ? F : never; };
|
|
101
275
|
/**
|
|
102
276
|
* Pages by key, filled in by the module from the app's `cms/*.ts`:
|
|
103
277
|
* `useCms('home')` is typed from it.
|