@wervt/cms 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -13
- package/dist/editor/assets/Button-Dai6Ph2c.js +6 -0
- package/dist/editor/assets/RichTextInput-CdL9ZFoP.js +271 -0
- package/dist/editor/assets/Toast-BaUcosq6.js +13 -0
- package/dist/editor/assets/index-CP6dGIyl.css +2 -0
- package/dist/editor/assets/index-Du-lvx2V.js +16 -0
- package/dist/editor/assets/poppins-latin-400-normal-BOb3E3N0.woff +0 -0
- package/dist/editor/assets/poppins-latin-400-normal-cpxAROuN.woff2 +0 -0
- package/dist/editor/assets/poppins-latin-500-normal-C8OXljZJ.woff2 +0 -0
- package/dist/editor/assets/poppins-latin-500-normal-DGXqpDMm.woff +0 -0
- package/dist/editor/assets/poppins-latin-600-normal-BJdTmd5m.woff +0 -0
- package/dist/editor/assets/poppins-latin-600-normal-zEkxB9Mr.woff2 +0 -0
- package/dist/editor/index.html +15 -0
- package/dist/editor-theme.d.ts +28 -0
- package/dist/editor-theme.js +62 -0
- package/dist/module.d.ts +6 -1
- package/dist/module.js +101 -11
- package/dist/runtime/app/CmsRichText.d.ts +15 -0
- package/dist/runtime/app/CmsRichText.js +48 -0
- package/dist/runtime/app/drafts.d.ts +5 -0
- package/dist/runtime/app/drafts.js +8 -0
- package/dist/runtime/app/preview.client.d.ts +2 -0
- package/dist/runtime/app/preview.client.js +28 -0
- package/dist/runtime/app/useCms.d.ts +9 -2
- package/dist/runtime/app/useCms.js +12 -5
- package/dist/runtime/fields.d.ts +24 -0
- package/dist/runtime/fields.js +109 -0
- package/dist/runtime/pages.d.ts +3 -0
- package/dist/runtime/pages.js +14 -0
- package/dist/runtime/preview.d.ts +34 -0
- package/dist/runtime/preview.js +35 -0
- package/dist/runtime/schema.d.ts +108 -0
- package/dist/runtime/schema.js +74 -0
- package/dist/runtime/server/dev-images.d.ts +2 -0
- package/dist/runtime/server/dev-images.js +12 -0
- package/dist/runtime/server/editor.get.d.ts +2 -0
- package/dist/runtime/server/editor.get.js +9 -0
- package/dist/runtime/server/entry.get.d.ts +7 -2
- package/dist/runtime/server/entry.get.js +11 -1
- package/dist/runtime/server/entry.put.js +14 -1
- package/dist/runtime/server/images.d.ts +4 -0
- package/dist/runtime/server/images.js +30 -0
- package/dist/runtime/server/schema.get.d.ts +10 -0
- package/dist/runtime/server/schema.get.js +10 -0
- package/dist/runtime/server/sniff.d.ts +11 -0
- package/dist/runtime/server/sniff.js +53 -0
- package/dist/runtime/server/upload.post.d.ts +13 -0
- package/dist/runtime/server/upload.post.js +44 -0
- package/package.json +29 -5
- package/dist/runtime/app/editor.vue +0 -42
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/runtime/preview.ts
|
|
2
|
+
/**
|
|
3
|
+
* The editor at `/_cms` shows the site in an iframe and sends it the values nobody saved yet. Both
|
|
4
|
+
* sides share one origin; these are the messages between them.
|
|
5
|
+
*/
|
|
6
|
+
const source = "wervt-cms";
|
|
7
|
+
const siteMessage = (path) => ({
|
|
8
|
+
source,
|
|
9
|
+
type: "page",
|
|
10
|
+
path
|
|
11
|
+
});
|
|
12
|
+
/** Values are copied as JSON: `postMessage` refuses Vue's reactive proxies. */
|
|
13
|
+
const editorMessage = (key, value) => ({
|
|
14
|
+
source,
|
|
15
|
+
type: "draft",
|
|
16
|
+
key,
|
|
17
|
+
value: JSON.parse(JSON.stringify(value ?? {}))
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* The message's data if it came from `from` on this origin and has the expected shape. Anything
|
|
21
|
+
* else (other frames, browser extensions, other origins) is ignored.
|
|
22
|
+
*/
|
|
23
|
+
function readMessage(event, from, origin, type) {
|
|
24
|
+
if (event.origin !== origin || !from || event.source !== from) return;
|
|
25
|
+
const data = event.data;
|
|
26
|
+
if (data?.source !== source || data.type !== type) return;
|
|
27
|
+
if (type === "page" && typeof data.path !== "string") return;
|
|
28
|
+
if (type === "draft" && (typeof data.key !== "string" || typeof data.value !== "object" || data.value === null)) return;
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
/** `/menu/` and `/menu?x#y` are `/menu`. */
|
|
32
|
+
const normalizePath = (path) => path.replace(/[?#].*$/, "").replace(/(.)\/+$/, "$1") || "/";
|
|
33
|
+
/** The page whose `path` is where the site is. */
|
|
34
|
+
const pageAt = (pages, path) => pages.find((page) => page.path !== void 0 && normalizePath(page.path) === normalizePath(path));
|
|
35
|
+
export { editorMessage, normalizePath, pageAt, readMessage, siteMessage };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
interface FieldBase<T> {
|
|
2
|
+
/** Shown in the editor instead of the field's name. */
|
|
3
|
+
label?: string;
|
|
4
|
+
default: T;
|
|
5
|
+
}
|
|
6
|
+
export interface TextField extends FieldBase<string> {
|
|
7
|
+
type: 'text';
|
|
8
|
+
/** Several lines in the editor. */
|
|
9
|
+
multiline?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface RichTextField extends FieldBase<string> {
|
|
12
|
+
type: 'richText';
|
|
13
|
+
}
|
|
14
|
+
export interface ToggleField extends FieldBase<boolean> {
|
|
15
|
+
type: 'toggle';
|
|
16
|
+
}
|
|
17
|
+
export interface LinkField extends FieldBase<CmsLink> {
|
|
18
|
+
type: 'link';
|
|
19
|
+
}
|
|
20
|
+
export interface ImageField<Required extends boolean = boolean> extends FieldBase<StoredImage | null> {
|
|
21
|
+
type: 'image';
|
|
22
|
+
/** With a default the page always gets an image, and the editor can only replace it. */
|
|
23
|
+
required: Required;
|
|
24
|
+
}
|
|
25
|
+
export interface ListField<Item extends CmsFields = CmsFields> extends FieldBase<InputOf<Item>[]> {
|
|
26
|
+
type: 'list';
|
|
27
|
+
item: Item;
|
|
28
|
+
}
|
|
29
|
+
export type CmsField = TextField | RichTextField | ToggleField | LinkField | ImageField | ListField;
|
|
30
|
+
/** Fields by name; a nested object is a group of fields shown together. */
|
|
31
|
+
export interface CmsFields {
|
|
32
|
+
[name: string]: CmsField | CmsFields;
|
|
33
|
+
}
|
|
34
|
+
export interface CmsLink {
|
|
35
|
+
label: string;
|
|
36
|
+
href: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* An image as it's stored: a file the editor uploaded to the app's bucket (`id`), or a path or
|
|
40
|
+
* URL the app ships with (`src`, e.g. `/hero.jpg` from `public/`).
|
|
41
|
+
*/
|
|
42
|
+
export type StoredImage = {
|
|
43
|
+
id: string;
|
|
44
|
+
alt?: string;
|
|
45
|
+
} | {
|
|
46
|
+
src: string;
|
|
47
|
+
alt?: string;
|
|
48
|
+
};
|
|
49
|
+
/** An image as the page gets it. */
|
|
50
|
+
export interface CmsImage {
|
|
51
|
+
src: string;
|
|
52
|
+
/** For uploaded images: the same picture in several widths. */
|
|
53
|
+
srcset?: string;
|
|
54
|
+
alt: string;
|
|
55
|
+
/** The uploaded file, for the editor. */
|
|
56
|
+
id?: string;
|
|
57
|
+
}
|
|
58
|
+
/** 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; };
|
|
60
|
+
/** 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; };
|
|
62
|
+
export interface CmsPage<Key extends string = string, F extends CmsFields = CmsFields> {
|
|
63
|
+
readonly __cmsPage: true;
|
|
64
|
+
key: Key;
|
|
65
|
+
/** Shown in the editor instead of the key. */
|
|
66
|
+
label?: string;
|
|
67
|
+
/** Where the page is on the site, for the editor's link to it (`/`, `/menu`). */
|
|
68
|
+
path?: string;
|
|
69
|
+
fields: F;
|
|
70
|
+
}
|
|
71
|
+
type Options = {
|
|
72
|
+
label?: string;
|
|
73
|
+
};
|
|
74
|
+
export declare function text(value?: string, options?: Options & {
|
|
75
|
+
multiline?: boolean;
|
|
76
|
+
}): TextField;
|
|
77
|
+
/** Markdown (Comark). */
|
|
78
|
+
export declare function richText(value?: string, options?: Options): RichTextField;
|
|
79
|
+
export declare function toggle(value?: boolean, options?: Options): ToggleField;
|
|
80
|
+
export declare function link(value?: CmsLink, options?: Options): LinkField;
|
|
81
|
+
/** `image('/hero.jpg')` always has an image; `image()` is `null` until one is chosen. */
|
|
82
|
+
export declare function image(value: string | StoredImage, options?: Options): ImageField<true>;
|
|
83
|
+
export declare function image(value?: null, options?: Options): ImageField<false>;
|
|
84
|
+
/** Repeated groups of fields, such as team members or opening hours, in an order of their own. */
|
|
85
|
+
export declare function list<Item extends CmsFields>(item: Item, value?: InputOf<Item>[], options?: Options): ListField<Item>;
|
|
86
|
+
/**
|
|
87
|
+
* The content of one page, read with `useCms(key)`:
|
|
88
|
+
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* // cms/home.ts
|
|
91
|
+
* export default defineCmsPage('home', {
|
|
92
|
+
* hero: { title: text('Welcome'), image: image('/hero.jpg') },
|
|
93
|
+
* hours: list({ day: text(), time: text() }, [{ day: 'Mon–Fri', time: '8–18' }]),
|
|
94
|
+
* })
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export declare function defineCmsPage<const Key extends string, F extends CmsFields>(key: Key, fields: F, options?: Options & {
|
|
98
|
+
path?: string;
|
|
99
|
+
}): CmsPage<Key, F>;
|
|
100
|
+
export declare function isCmsPage(value: unknown): value is CmsPage;
|
|
101
|
+
/**
|
|
102
|
+
* Pages by key, filled in by the module from the app's `cms/*.ts`:
|
|
103
|
+
* `useCms('home')` is typed from it.
|
|
104
|
+
*/
|
|
105
|
+
export interface CmsPages {}
|
|
106
|
+
/** The pages a `cms/*.ts` module exports, by key. Used by the generated types. */
|
|
107
|
+
export type PagesOf<M> = { [P in M[keyof M] as P extends CmsPage<infer K> ? K : never]: P extends CmsPage<string, infer F> ? F : never; };
|
|
108
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/runtime/schema.ts
|
|
2
|
+
function text(value = "", options = {}) {
|
|
3
|
+
return {
|
|
4
|
+
type: "text",
|
|
5
|
+
default: value,
|
|
6
|
+
...options
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** Markdown (Comark). */
|
|
10
|
+
function richText(value = "", options = {}) {
|
|
11
|
+
return {
|
|
12
|
+
type: "richText",
|
|
13
|
+
default: value,
|
|
14
|
+
...options
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function toggle(value = false, options = {}) {
|
|
18
|
+
return {
|
|
19
|
+
type: "toggle",
|
|
20
|
+
default: value,
|
|
21
|
+
...options
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function link(value = {
|
|
25
|
+
label: "",
|
|
26
|
+
href: ""
|
|
27
|
+
}, options = {}) {
|
|
28
|
+
return {
|
|
29
|
+
type: "link",
|
|
30
|
+
default: value,
|
|
31
|
+
...options
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function image(value, options = {}) {
|
|
35
|
+
const stored = typeof value === "string" ? { src: value } : value ?? null;
|
|
36
|
+
return {
|
|
37
|
+
type: "image",
|
|
38
|
+
default: stored,
|
|
39
|
+
required: stored !== null,
|
|
40
|
+
...options
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** Repeated groups of fields, such as team members or opening hours, in an order of their own. */
|
|
44
|
+
function list(item, value = [], options = {}) {
|
|
45
|
+
return {
|
|
46
|
+
type: "list",
|
|
47
|
+
item,
|
|
48
|
+
default: value,
|
|
49
|
+
...options
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The content of one page, read with `useCms(key)`:
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* // cms/home.ts
|
|
57
|
+
* export default defineCmsPage('home', {
|
|
58
|
+
* hero: { title: text('Welcome'), image: image('/hero.jpg') },
|
|
59
|
+
* hours: list({ day: text(), time: text() }, [{ day: 'Mon–Fri', time: '8–18' }]),
|
|
60
|
+
* })
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
function defineCmsPage(key, fields, options = {}) {
|
|
64
|
+
return {
|
|
65
|
+
__cmsPage: true,
|
|
66
|
+
key,
|
|
67
|
+
fields,
|
|
68
|
+
...options
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function isCmsPage(value) {
|
|
72
|
+
return typeof value === "object" && value !== null && "__cmsPage" in value;
|
|
73
|
+
}
|
|
74
|
+
export { defineCmsPage, image, isCmsPage, link, list, richText, text, toggle };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
4
|
+
// src/runtime/server/dev-images.ts
|
|
5
|
+
/** Writes the image and returns the path the app serves it at. */
|
|
6
|
+
async function saveDevImage(publicDir, data, extension) {
|
|
7
|
+
const name = `${randomUUID()}.${extension}`;
|
|
8
|
+
await mkdir(join(publicDir, "cms"), { recursive: true });
|
|
9
|
+
await writeFile(join(publicDir, "cms", name), data);
|
|
10
|
+
return `/cms/${name}`;
|
|
11
|
+
}
|
|
12
|
+
export { saveDevImage };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineEventHandler } from "nuxt/server";
|
|
2
|
+
import { html } from "#wervt-cms/editor";
|
|
3
|
+
// src/runtime/server/editor.get.ts
|
|
4
|
+
var editor_get_default = defineEventHandler(() => new Response(html, { headers: {
|
|
5
|
+
"content-type": "text/html; charset=utf-8",
|
|
6
|
+
"cache-control": "no-cache",
|
|
7
|
+
"x-robots-tag": "noindex"
|
|
8
|
+
} }));
|
|
9
|
+
export { editor_get_default as default };
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
declare const _default: import("nuxt/server").EventHandler<Promise<{
|
|
2
|
+
key: string;
|
|
3
|
+
value: {
|
|
4
|
+
[x: string]: unknown;
|
|
5
|
+
};
|
|
6
|
+
updatedAt: string | null;
|
|
7
|
+
} | null>>;
|
|
3
8
|
export { _default as default };
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { isEntryKey } from "./entries.js";
|
|
2
|
+
import { resolveValue } from "../fields.js";
|
|
3
|
+
import { pages } from "../pages.js";
|
|
4
|
+
import { serverImages } from "./images.js";
|
|
2
5
|
import { readEntry } from "./store.js";
|
|
3
6
|
import { createError, defineEventHandler, getRouterParam } from "nuxt/server";
|
|
4
7
|
// src/runtime/server/entry.get.ts
|
|
@@ -8,6 +11,13 @@ var entry_get_default = defineEventHandler(async (event) => {
|
|
|
8
11
|
status: 400,
|
|
9
12
|
message: "Invalid key"
|
|
10
13
|
});
|
|
11
|
-
|
|
14
|
+
const entry = await readEntry(key);
|
|
15
|
+
const page = pages.get(key);
|
|
16
|
+
if (!page) return entry ?? null;
|
|
17
|
+
return {
|
|
18
|
+
key,
|
|
19
|
+
value: resolveValue(page.fields, entry?.value, serverImages),
|
|
20
|
+
updatedAt: entry?.updatedAt ?? null
|
|
21
|
+
};
|
|
12
22
|
});
|
|
13
23
|
export { entry_get_default as default };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { isEntryKey } from "./entries.js";
|
|
2
|
+
import { InvalidValue, normalizeValue } from "../fields.js";
|
|
3
|
+
import { pages } from "../pages.js";
|
|
2
4
|
import { requireEditor } from "./access.js";
|
|
3
5
|
import { writeEntry } from "./store.js";
|
|
4
6
|
import { createError, defineEventHandler, getRouterParam, readBody } from "nuxt/server";
|
|
@@ -15,7 +17,18 @@ var entry_put_default = defineEventHandler(async (event) => {
|
|
|
15
17
|
status: 400,
|
|
16
18
|
message: "Expected an object"
|
|
17
19
|
});
|
|
18
|
-
|
|
20
|
+
const page = pages.get(key);
|
|
21
|
+
let value = body;
|
|
22
|
+
try {
|
|
23
|
+
if (page) value = normalizeValue(page.fields, body);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error instanceof InvalidValue) throw createError({
|
|
26
|
+
status: 400,
|
|
27
|
+
message: error.message
|
|
28
|
+
});
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
await writeEntry(key, value);
|
|
19
32
|
return { ok: true };
|
|
20
33
|
});
|
|
21
34
|
export { entry_put_default as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { staticImages } from "../fields.js";
|
|
2
|
+
import { imageUrl } from "@wervt/nuxt/server";
|
|
3
|
+
// src/runtime/server/images.ts
|
|
4
|
+
/** The bucket group uploads go to. */
|
|
5
|
+
const IMAGE_GROUP = "cms";
|
|
6
|
+
const WIDTHS = [
|
|
7
|
+
640,
|
|
8
|
+
1024,
|
|
9
|
+
1600,
|
|
10
|
+
2400
|
|
11
|
+
];
|
|
12
|
+
const serverImages = (image) => {
|
|
13
|
+
if ("src" in image) return staticImages(image);
|
|
14
|
+
const file = {
|
|
15
|
+
group: "cms",
|
|
16
|
+
id: image.id
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
return {
|
|
20
|
+
id: image.id,
|
|
21
|
+
alt: image.alt ?? "",
|
|
22
|
+
src: imageUrl(file, { width: 1600 }),
|
|
23
|
+
srcset: WIDTHS.map((width) => `${imageUrl(file, { width })} ${width}w`).join(", ")
|
|
24
|
+
};
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error(`[cms] can't show image ${image.id}:`, error);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export { IMAGE_GROUP, serverImages };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { pages } from "../pages.js";
|
|
2
|
+
import { defineEventHandler } from "nuxt/server";
|
|
3
|
+
// src/runtime/server/schema.get.ts
|
|
4
|
+
var schema_get_default = defineEventHandler(() => ({ pages: [...pages.values()].map(({ key, label, path, fields }) => ({
|
|
5
|
+
key,
|
|
6
|
+
label,
|
|
7
|
+
path,
|
|
8
|
+
fields
|
|
9
|
+
})) }));
|
|
10
|
+
export { schema_get_default as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface SniffedImage {
|
|
2
|
+
type: string;
|
|
3
|
+
extension: string;
|
|
4
|
+
/** Shown by every browser as a plain file, without the image service. */
|
|
5
|
+
browserSafe: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The image type the bytes start with. What the browser claims counts for nothing: the file is
|
|
9
|
+
* served from the app's own origin, so an SVG (or HTML calling itself an image) could run script.
|
|
10
|
+
*/
|
|
11
|
+
export declare function sniffImage(data: Uint8Array): SniffedImage | undefined;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/runtime/server/sniff.ts
|
|
2
|
+
const ascii = (data, start, end) => String.fromCharCode(...data.subarray(start, end));
|
|
3
|
+
const HEIF_BRANDS = /* @__PURE__ */ new Set([
|
|
4
|
+
"heic",
|
|
5
|
+
"heix",
|
|
6
|
+
"heim",
|
|
7
|
+
"heis",
|
|
8
|
+
"hevc",
|
|
9
|
+
"hevx",
|
|
10
|
+
"mif1",
|
|
11
|
+
"msf1"
|
|
12
|
+
]);
|
|
13
|
+
/**
|
|
14
|
+
* The image type the bytes start with. What the browser claims counts for nothing: the file is
|
|
15
|
+
* served from the app's own origin, so an SVG (or HTML calling itself an image) could run script.
|
|
16
|
+
*/
|
|
17
|
+
function sniffImage(data) {
|
|
18
|
+
if (data.length < 12) return void 0;
|
|
19
|
+
if (data[0] === 255 && data[1] === 216 && data[2] === 255) return {
|
|
20
|
+
type: "image/jpeg",
|
|
21
|
+
extension: "jpg",
|
|
22
|
+
browserSafe: true
|
|
23
|
+
};
|
|
24
|
+
if (ascii(data, 0, 8) === "PNG\r\n\n") return {
|
|
25
|
+
type: "image/png",
|
|
26
|
+
extension: "png",
|
|
27
|
+
browserSafe: true
|
|
28
|
+
};
|
|
29
|
+
if (ascii(data, 0, 6) === "GIF87a" || ascii(data, 0, 6) === "GIF89a") return {
|
|
30
|
+
type: "image/gif",
|
|
31
|
+
extension: "gif",
|
|
32
|
+
browserSafe: true
|
|
33
|
+
};
|
|
34
|
+
if (ascii(data, 0, 4) === "RIFF" && ascii(data, 8, 12) === "WEBP") return {
|
|
35
|
+
type: "image/webp",
|
|
36
|
+
extension: "webp",
|
|
37
|
+
browserSafe: true
|
|
38
|
+
};
|
|
39
|
+
if (ascii(data, 4, 8) === "ftyp") {
|
|
40
|
+
const brand = ascii(data, 8, 12);
|
|
41
|
+
if (brand === "avif" || brand === "avis") return {
|
|
42
|
+
type: "image/avif",
|
|
43
|
+
extension: "avif",
|
|
44
|
+
browserSafe: true
|
|
45
|
+
};
|
|
46
|
+
if (HEIF_BRANDS.has(brand)) return {
|
|
47
|
+
type: "image/heic",
|
|
48
|
+
extension: "heic",
|
|
49
|
+
browserSafe: false
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export { sniffImage };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CmsImage } from "../schema.js";
|
|
2
|
+
declare const _default: import("nuxt/server").EventHandler<Promise<{
|
|
3
|
+
value: {
|
|
4
|
+
src: string;
|
|
5
|
+
};
|
|
6
|
+
image: CmsImage | undefined;
|
|
7
|
+
} | {
|
|
8
|
+
value: {
|
|
9
|
+
id: string;
|
|
10
|
+
};
|
|
11
|
+
image: CmsImage | null;
|
|
12
|
+
}>>;
|
|
13
|
+
export { _default as default };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { staticImages } from "../fields.js";
|
|
2
|
+
import { requireEditor } from "./access.js";
|
|
3
|
+
import { serverImages } from "./images.js";
|
|
4
|
+
import { sniffImage } from "./sniff.js";
|
|
5
|
+
import { createError, defineEventHandler } from "nuxt/server";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { publicDir } from "#wervt-cms/entries";
|
|
8
|
+
import { readUploadedFile, useFileStorage } from "#imports";
|
|
9
|
+
// src/runtime/server/upload.post.ts
|
|
10
|
+
var upload_post_default = defineEventHandler(async (event) => {
|
|
11
|
+
requireEditor(event);
|
|
12
|
+
const devDir = import.meta.dev ? publicDir : void 0;
|
|
13
|
+
const upload = await readUploadedFile(event, {
|
|
14
|
+
types: ["image"],
|
|
15
|
+
maxSize: devDir ? "5MB" : "25MB"
|
|
16
|
+
});
|
|
17
|
+
const sniffed = sniffImage(upload.data);
|
|
18
|
+
if (!sniffed) throw createError({
|
|
19
|
+
status: 415,
|
|
20
|
+
message: "Use a JPEG, PNG, WebP, GIF, AVIF or HEIC image"
|
|
21
|
+
});
|
|
22
|
+
if (devDir) {
|
|
23
|
+
if (!sniffed.browserSafe) throw createError({
|
|
24
|
+
status: 415,
|
|
25
|
+
message: "Use a JPEG, PNG, WebP, GIF or AVIF image"
|
|
26
|
+
});
|
|
27
|
+
const { saveDevImage } = await import("./dev-images.js");
|
|
28
|
+
const value = { src: await saveDevImage(devDir, upload.data, sniffed.extension) };
|
|
29
|
+
return {
|
|
30
|
+
value,
|
|
31
|
+
image: staticImages(value)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const { id } = await useFileStorage().put("cms", upload.data, {
|
|
35
|
+
contentType: sniffed.type,
|
|
36
|
+
name: `${randomUUID()}.${sniffed.extension}`
|
|
37
|
+
});
|
|
38
|
+
const value = { id };
|
|
39
|
+
return {
|
|
40
|
+
value,
|
|
41
|
+
image: serverImages(value) ?? null
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
export { upload_post_default as default };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wervt/cms",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Editable texts for Nuxt sites on wervt: written to
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Editable texts, images and lists for Nuxt sites on wervt: written to files in dev, to the app's database and bucket once deployed.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cms",
|
|
7
7
|
"nuxt",
|
|
@@ -25,21 +25,45 @@
|
|
|
25
25
|
".": {
|
|
26
26
|
"types": "./dist/module.d.ts",
|
|
27
27
|
"default": "./dist/module.js"
|
|
28
|
+
},
|
|
29
|
+
"./schema": {
|
|
30
|
+
"types": "./dist/runtime/schema.d.ts",
|
|
31
|
+
"default": "./dist/runtime/schema.js"
|
|
28
32
|
}
|
|
29
33
|
},
|
|
30
34
|
"dependencies": {
|
|
31
|
-
"@
|
|
35
|
+
"@comark/vue": "^0.8.1",
|
|
36
|
+
"@wervt/nuxt": "^0.3.24",
|
|
37
|
+
"ablage": "^0.1.1",
|
|
38
|
+
"comark": "^0.8.1"
|
|
32
39
|
},
|
|
33
40
|
"devDependencies": {
|
|
41
|
+
"@fontsource/poppins": "^5.3.0",
|
|
42
|
+
"@golar/vue": "^0.1.11",
|
|
43
|
+
"@iconify-json/lucide": "^1.2.157",
|
|
44
|
+
"@iconify/vue": "^5.0.3",
|
|
45
|
+
"@nuxt/ui": "^4.12.0",
|
|
46
|
+
"@tiptap/core": "3.31.4",
|
|
47
|
+
"@tiptap/extension-image": "3.31.4",
|
|
48
|
+
"@tiptap/extension-list": "3.31.4",
|
|
49
|
+
"@tiptap/extension-table": "3.31.4",
|
|
50
|
+
"@tiptap/pm": "3.31.4",
|
|
51
|
+
"@tiptap/starter-kit": "3.31.4",
|
|
52
|
+
"@tiptap/vue-3": "3.31.4",
|
|
53
|
+
"@vitejs/plugin-vue": "^6.0.11",
|
|
54
|
+
"comark-ui": "^0.8.1",
|
|
34
55
|
"drizzle-orm": "1.0.0-rc.5",
|
|
56
|
+
"golar": "^0.1.11",
|
|
35
57
|
"nuxt": "npm:nuxt-nightly@5x",
|
|
58
|
+
"tailwindcss": "^4.3.3",
|
|
36
59
|
"vue": "^3.5.45"
|
|
37
60
|
},
|
|
38
61
|
"peerDependencies": {
|
|
39
62
|
"drizzle-orm": ">=1.0.0-rc.5 <2",
|
|
40
|
-
"nuxt": ">=4.
|
|
63
|
+
"nuxt": ">=4.6"
|
|
41
64
|
},
|
|
42
65
|
"scripts": {
|
|
43
|
-
"build": "vp pack"
|
|
66
|
+
"build": "vp pack && vp build editor",
|
|
67
|
+
"typecheck": "vp build editor && cd editor && golar typecheck"
|
|
44
68
|
}
|
|
45
69
|
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
<script setup lang="ts">
|
|
2
|
-
import { ref, watch } from 'vue'
|
|
3
|
-
import { useFetch, useRoute } from '#app'
|
|
4
|
-
|
|
5
|
-
const route = useRoute()
|
|
6
|
-
const key = String(route.query.key ?? 'home')
|
|
7
|
-
const { data } = await useFetch<{ value: Record<string, unknown> } | null>(
|
|
8
|
-
`/api/_cms/entries/${key}`,
|
|
9
|
-
)
|
|
10
|
-
const text = ref('{}')
|
|
11
|
-
watch(data, (entry) => (text.value = JSON.stringify(entry?.value ?? {}, null, 2)), {
|
|
12
|
-
immediate: true,
|
|
13
|
-
})
|
|
14
|
-
const status = ref('')
|
|
15
|
-
const signedOut = ref(false)
|
|
16
|
-
// A plain link, not a router navigation: on wervt the runtime answers this path, not the app.
|
|
17
|
-
const loginUrl = `/_wervt/login?redirect=${encodeURIComponent(route.fullPath)}`
|
|
18
|
-
|
|
19
|
-
async function save() {
|
|
20
|
-
signedOut.value = false
|
|
21
|
-
try {
|
|
22
|
-
await $fetch(`/api/_cms/entries/${key}`, { method: 'PUT', body: JSON.parse(text.value) })
|
|
23
|
-
status.value = 'Saved'
|
|
24
|
-
} catch (e) {
|
|
25
|
-
const error = e as { statusCode?: number; data?: { message?: string } }
|
|
26
|
-
signedOut.value = error.statusCode === 401
|
|
27
|
-
status.value = signedOut.value ? '' : (error.data?.message ?? String(e))
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
</script>
|
|
31
|
-
|
|
32
|
-
<template>
|
|
33
|
-
<main style="max-width: 40rem; margin: 2rem auto; font-family: system-ui">
|
|
34
|
-
<h1>Content: {{ key }}</h1>
|
|
35
|
-
<textarea v-model="text" rows="12" style="width: 100%; font-family: monospace" />
|
|
36
|
-
<button type="button" @click="save">Save</button>
|
|
37
|
-
<p data-testid="status">
|
|
38
|
-
{{ status }}
|
|
39
|
-
<a v-if="signedOut" :href="loginUrl" data-testid="sign-in">Sign in to edit</a>
|
|
40
|
-
</p>
|
|
41
|
-
</main>
|
|
42
|
-
</template>
|