@bettercms-ai/sveltekit 0.2.0 → 0.4.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/README.md +81 -9
- package/dist/index.d.ts +40 -8
- package/dist/index.js +22 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @bettercms-ai/sveltekit
|
|
2
2
|
|
|
3
|
-
BetterCMS for SvelteKit.
|
|
3
|
+
BetterCMS for SvelteKit. Your own components stay your own: they spread a few attributes onto their elements, and every element becomes editable in the BetterCMS visual editor. Pages stay current when content is published.
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
npm i @bettercms-ai/sveltekit
|
|
@@ -12,19 +12,91 @@ import { env as pub } from "$env/dynamic/public";
|
|
|
12
12
|
import { env as priv } from "$env/dynamic/private";
|
|
13
13
|
export const bcms = createBcms({ env: { ...pub, ...priv } });
|
|
14
14
|
|
|
15
|
+
// src/hooks.server.ts: drafts, <html data-bcms-draft>, <meta name="generator">
|
|
16
|
+
export const handle = bcmsHandle(bcms); // or sequence(bcmsHandle(bcms), yourHandle)
|
|
17
|
+
|
|
18
|
+
// src/routes/api/draft/+server.ts: the path the editor's "Preview on site" opens
|
|
19
|
+
export const GET = ({ request }) => bcms.draftEnable(request);
|
|
20
|
+
|
|
21
|
+
// src/routes/+page.server.ts: drafts while the editor previews
|
|
22
|
+
export const load = async ({ request }) => ({ post: await (await bcms.client(request)).getEntry("hello-world") });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```svelte
|
|
26
|
+
<!-- src/routes/+layout.svelte: a publish, and the editor's re-render request, re-run `load` -->
|
|
27
|
+
<script>
|
|
28
|
+
import { onMount } from "svelte";
|
|
29
|
+
import { invalidateAll } from "$app/navigation";
|
|
30
|
+
import { env } from "$env/dynamic/public";
|
|
31
|
+
import { startBcmsLive } from "@bettercms-ai/sveltekit";
|
|
32
|
+
onMount(() => startBcmsLive({ refresh: invalidateAll, src: env.PUBLIC_BCMS_LIVE_URL }));
|
|
33
|
+
</script>
|
|
34
|
+
<slot />
|
|
35
|
+
|
|
36
|
+
<!-- your own markup, bound to its content -->
|
|
37
|
+
<article {...entryAttrs(data.post)}>
|
|
38
|
+
<h1 {...entryField(data.post, "title")}>{data.post.data.title}</h1>
|
|
39
|
+
<img {...imageAttrs(data.post.data.cover, { sizes: "100vw" })} {...entryField(data.post, "cover")} class="cover" />
|
|
40
|
+
<div {...entryField(data.post, "body")}>{@html rich(data.post.data.body)}</div>
|
|
41
|
+
</article>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
| Helper | What it binds |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `fieldAttrs(path, page.fieldStyles)` | A page field. It also applies the field's stored style, which turns on the editor's Align, Size and Weight controls. |
|
|
47
|
+
| `entryAttrs(entry)` | A collection entry's card. |
|
|
48
|
+
| `entryField(entry, path)` | A field of that entry. |
|
|
49
|
+
| `sectionRootAttrs(block)` | A section root (its spacing and design). |
|
|
50
|
+
| `sectionColumnAttrs(block)` | The section's content column. |
|
|
51
|
+
| `imageAttrs(value, { path, sizes })` | An optimized `<img>` with a srcset, alt text, the focal point and the field binding. |
|
|
52
|
+
| `rich(value)`, `lines(value)`, `plain(value)` | Rich text as HTML, a one-line value, or plain text for attributes. |
|
|
53
|
+
|
|
54
|
+
## Sections
|
|
55
|
+
|
|
56
|
+
A page built from sections in BetterCMS renders as the hosted renderer draws it, so forms, sliders and tabs work exactly as they do on a BetterCMS-hosted site. Pass the request and the editor's session gets the draft, with every section and value clickable:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
15
59
|
// src/routes/+page.server.ts
|
|
16
|
-
export const load = async ({ fetch }) => ({ sections: await bcms.sections("home", fetch) });
|
|
60
|
+
export const load = async ({ fetch, request }) => ({ sections: await bcms.sections("home", fetch, request) });
|
|
61
|
+
```
|
|
17
62
|
|
|
18
|
-
|
|
63
|
+
```svelte
|
|
19
64
|
<div use:bcmsSections={data.sections}>{@html sectionsMarkup(data.sections)}</div>
|
|
65
|
+
```
|
|
20
66
|
|
|
21
|
-
|
|
22
|
-
|
|
67
|
+
### Your own sections in the CMS order
|
|
68
|
+
|
|
69
|
+
A page that draws its own sections can let BetterCMS order them, and draw sections added in the editor between them: name the page's sections (`slots`, in source order) and loop over `sectionItems`. `npx @bettercms-ai/convert --sections` writes this for you.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
// src/routes/+page.server.ts
|
|
73
|
+
const bcmsSlots = ["hero", "code:features"];
|
|
74
|
+
export const load = async ({ fetch, request }) => ({
|
|
75
|
+
bcmsSections: sectionItems(await bcms.sections("home", fetch, request, { slots: bcmsSlots }).catch(() => null), bcmsSlots, "home"),
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```svelte
|
|
80
|
+
{#each data.bcmsSections as item (item.key)}
|
|
81
|
+
{#if item.slot === "hero"}<section class="hero" {...item.root}>…</section>
|
|
82
|
+
{:else if item.slot === "code:features"}<div {...item.box}><Features /></div>
|
|
83
|
+
{:else if item.slot === null}<div data-bcms-rendered="platform" use:bcmsSections={item.html}>{@html item.html}</div>
|
|
84
|
+
{/if}
|
|
85
|
+
{/each}
|
|
23
86
|
```
|
|
24
87
|
|
|
25
|
-
|
|
88
|
+
`root` goes on a section's own root element (its placement's `data-bcms-block` and style); `box` on a wrapper when the section is a component. With no answer, every slot renders in source order.
|
|
89
|
+
|
|
90
|
+
## Settings
|
|
91
|
+
|
|
92
|
+
Options are read first, then `PUBLIC_BCMS_*`, then `BCMS_*`, the same precedence as every adapter:
|
|
26
93
|
|
|
27
|
-
|
|
94
|
+
| Variable | Purpose |
|
|
95
|
+
|---|---|
|
|
96
|
+
| `PUBLIC_BCMS_API_URL` | The API to read from. |
|
|
97
|
+
| `PUBLIC_BCMS_WORKSPACE` | Your workspace. |
|
|
98
|
+
| `PUBLIC_BCMS_PROJECT_ID` | Your project. |
|
|
99
|
+
| `PUBLIC_BCMS_LIVE_URL` | The live stream. Optional. |
|
|
100
|
+
| `BCMS_DRAFT_SECRET` | Signs the draft cookie. |
|
|
28
101
|
|
|
29
|
-
|
|
30
|
-
- Sections are the PUBLISHED page. Draft mode switches the read client (`await bcms.client(request)`) to drafts; the sections fragment shows drafts in a later release.
|
|
102
|
+
The sections run small inline scripts (forms, sliders and tabs), so a Content-Security-Policy must allow inline scripts.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,40 @@
|
|
|
1
1
|
import { BcmsSite } from '@bettercms-ai/sdk/sections';
|
|
2
|
-
export { BcmsSite, SectionsFragment, sectionsMarkup } from '@bettercms-ai/sdk/sections';
|
|
2
|
+
export { BcmsSite, SectionItem, SectionsFragment, SectionsFragmentItem, sectionItems, sectionsMarkup } from '@bettercms-ai/sdk/sections';
|
|
3
3
|
import { BcmsSiteOptions } from '@bettercms-ai/sdk/config';
|
|
4
|
+
export * from '@bettercms-ai/sdk/bindings';
|
|
5
|
+
export { BcmsChangeEvent, StartBcmsLiveOptions, inEditor, liveSrc, onContentApplied, startBcmsLive } from '@bettercms-ai/sdk/live';
|
|
6
|
+
export { lines, plain, rich } from '@bettercms-ai/sdk/read';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
|
-
* @bettercms-ai/sveltekit — BetterCMS for SvelteKit
|
|
9
|
+
* @bettercms-ai/sveltekit — BetterCMS for SvelteKit: the site's own markup bound to its content,
|
|
10
|
+
* so every element is editable in the BetterCMS visual editor, and the site stays live on publish.
|
|
7
11
|
*
|
|
8
12
|
* // src/lib/bcms.ts
|
|
9
13
|
* import { env as pub } from "$env/dynamic/public";
|
|
10
14
|
* import { env as priv } from "$env/dynamic/private";
|
|
11
15
|
* export const bcms = createBcms({ env: { ...pub, ...priv } });
|
|
12
16
|
*
|
|
13
|
-
* // src/
|
|
14
|
-
* export const
|
|
17
|
+
* // src/hooks.server.ts — drafts, `<html data-bcms-draft>`, `<meta name="generator">`
|
|
18
|
+
* export const handle = bcmsHandle(bcms);
|
|
15
19
|
*
|
|
16
|
-
* // src/routes/+
|
|
20
|
+
* // src/routes/+layout.svelte — a publish, and the editor's re-render request, re-run `load`
|
|
21
|
+
* onMount(() => startBcmsLive({ refresh: invalidateAll, src: env.PUBLIC_BCMS_LIVE_URL }));
|
|
22
|
+
*
|
|
23
|
+
* // src/routes/+page.server.ts — drafts while the editor previews
|
|
24
|
+
* export const load = async ({ request }) => ({ page: await (await bcms.client(request)).getPage("home") });
|
|
25
|
+
*
|
|
26
|
+
* // src/routes/+page.svelte — the site's OWN markup, bound to its fields
|
|
27
|
+
* <h1 {...fieldAttrs("hero-title", data.page.fieldStyles)}>{data.page.data["hero-title"]}</h1>
|
|
28
|
+
* <img {...imageAttrs(data.page.data.cover, { path: "cover", sizes: "100vw" })} class="cover" />
|
|
29
|
+
* <div {...fieldAttrs("body")}>{@html rich(data.page.data.body)}</div>
|
|
30
|
+
*
|
|
31
|
+
* // or a page's sections as the hosted renderer draws them
|
|
17
32
|
* <div use:bcmsSections={data.sections}>{@html sectionsMarkup(data.sections)}</div>
|
|
18
33
|
*
|
|
19
34
|
* // src/routes/api/draft/+server.ts — the path the editor's "Preview on site" opens
|
|
20
35
|
* export const GET = ({ request }) => bcms.draftEnable(request);
|
|
21
36
|
*
|
|
22
37
|
* Settings: options, then `PUBLIC_BCMS_*`, then `BCMS_*` (the same precedence as every adapter).
|
|
23
|
-
* The sections are the hosted renderer's own markup, so forms, sliders and tabs work as on a
|
|
24
|
-
* BetterCMS-hosted site; `bcmsSections` runs their scripts after a client-side navigation.
|
|
25
38
|
*/
|
|
26
39
|
|
|
27
40
|
/** Build the site's BetterCMS access from its settings (pass `{ ...publicEnv, ...privateEnv }`). */
|
|
@@ -29,6 +42,25 @@ declare function createBcms(options?: BcmsSiteOptions & {
|
|
|
29
42
|
env?: Record<string, string | undefined>;
|
|
30
43
|
draftSecret?: string;
|
|
31
44
|
}): BcmsSite;
|
|
45
|
+
/** The least of SvelteKit's `handle` this needs, so the package carries no @sveltejs/kit types. */
|
|
46
|
+
interface HandleInput {
|
|
47
|
+
event: {
|
|
48
|
+
request: Request;
|
|
49
|
+
locals: object;
|
|
50
|
+
};
|
|
51
|
+
resolve(event: HandleInput["event"], opts?: {
|
|
52
|
+
transformPageChunk?: (input: {
|
|
53
|
+
html: string;
|
|
54
|
+
done: boolean;
|
|
55
|
+
}) => string | undefined;
|
|
56
|
+
}): Response | Promise<Response>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The server hook: `locals.bcmsDraft` says whether this request previews drafts; every page names
|
|
60
|
+
* BetterCMS in its `<head>`, and in drafts carries `<html data-bcms-draft="1">`, the canvas's proof
|
|
61
|
+
* that the frame renders drafts. Compose it with the site's own hooks through `sequence()`.
|
|
62
|
+
*/
|
|
63
|
+
declare function bcmsHandle(bcms: BcmsSite): ({ event, resolve }: HandleInput) => Promise<Response>;
|
|
32
64
|
/**
|
|
33
65
|
* Svelte action for the element holding `{@html sectionsMarkup(…)}`: its scripts run on every page.
|
|
34
66
|
* Pass the sections as its parameter (`use:bcmsSections={data.sections}`): Svelte calls `update`
|
|
@@ -38,4 +70,4 @@ declare function bcmsSections(node: Element, _sections?: unknown): {
|
|
|
38
70
|
update(): void;
|
|
39
71
|
};
|
|
40
72
|
|
|
41
|
-
export { bcmsSections, createBcms };
|
|
73
|
+
export { bcmsHandle, bcmsSections, createBcms };
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { createBcmsSite, runSectionScripts } from "@bettercms-ai/sdk/sections";
|
|
3
|
-
import {
|
|
3
|
+
import { markDraft, withGenerator } from "@bettercms-ai/sdk/live";
|
|
4
|
+
import { sectionsMarkup, sectionItems } from "@bettercms-ai/sdk/sections";
|
|
5
|
+
export * from "@bettercms-ai/sdk/bindings";
|
|
6
|
+
import { startBcmsLive, liveSrc, inEditor, onContentApplied } from "@bettercms-ai/sdk/live";
|
|
7
|
+
import { plain, rich, lines } from "@bettercms-ai/sdk/read";
|
|
4
8
|
function createBcms(options = {}) {
|
|
5
9
|
return createBcmsSite({ ...options, prefixes: ["PUBLIC_"], adapter: "@bettercms-ai/sveltekit" });
|
|
6
10
|
}
|
|
11
|
+
function bcmsHandle(bcms) {
|
|
12
|
+
return async ({ event, resolve }) => {
|
|
13
|
+
const draft = await bcms.isDraft(event.request);
|
|
14
|
+
event.locals.bcmsDraft = draft;
|
|
15
|
+
return resolve(event, { transformPageChunk: ({ html }) => draft ? markDraft(withGenerator(html)) : withGenerator(html) });
|
|
16
|
+
};
|
|
17
|
+
}
|
|
7
18
|
function bcmsSections(node, _sections) {
|
|
8
19
|
runSectionScripts(node);
|
|
9
20
|
return { update: () => queueMicrotask(() => runSectionScripts(node)) };
|
|
10
21
|
}
|
|
11
22
|
export {
|
|
23
|
+
bcmsHandle,
|
|
12
24
|
bcmsSections,
|
|
13
25
|
createBcms,
|
|
14
|
-
|
|
26
|
+
inEditor,
|
|
27
|
+
lines,
|
|
28
|
+
liveSrc,
|
|
29
|
+
onContentApplied,
|
|
30
|
+
plain,
|
|
31
|
+
rich,
|
|
32
|
+
sectionItems,
|
|
33
|
+
sectionsMarkup,
|
|
34
|
+
startBcmsLive
|
|
15
35
|
};
|
|
16
36
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/// <reference lib=\"dom\" />\n/**\n * @bettercms-ai/sveltekit — BetterCMS for SvelteKit.\n *\n * // src/lib/bcms.ts\n * import { env as pub } from \"$env/dynamic/public\";\n * import { env as priv } from \"$env/dynamic/private\";\n * export const bcms = createBcms({ env: { ...pub, ...priv } });\n *\n * // src/routes/+page.server.ts\n * export const load = async ({
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/// <reference lib=\"dom\" />\n/**\n * @bettercms-ai/sveltekit — BetterCMS for SvelteKit: the site's own markup bound to its content,\n * so every element is editable in the BetterCMS visual editor, and the site stays live on publish.\n *\n * // src/lib/bcms.ts\n * import { env as pub } from \"$env/dynamic/public\";\n * import { env as priv } from \"$env/dynamic/private\";\n * export const bcms = createBcms({ env: { ...pub, ...priv } });\n *\n * // src/hooks.server.ts — drafts, `<html data-bcms-draft>`, `<meta name=\"generator\">`\n * export const handle = bcmsHandle(bcms);\n *\n * // src/routes/+layout.svelte — a publish, and the editor's re-render request, re-run `load`\n * onMount(() => startBcmsLive({ refresh: invalidateAll, src: env.PUBLIC_BCMS_LIVE_URL }));\n *\n * // src/routes/+page.server.ts — drafts while the editor previews\n * export const load = async ({ request }) => ({ page: await (await bcms.client(request)).getPage(\"home\") });\n *\n * // src/routes/+page.svelte — the site's OWN markup, bound to its fields\n * <h1 {...fieldAttrs(\"hero-title\", data.page.fieldStyles)}>{data.page.data[\"hero-title\"]}</h1>\n * <img {...imageAttrs(data.page.data.cover, { path: \"cover\", sizes: \"100vw\" })} class=\"cover\" />\n * <div {...fieldAttrs(\"body\")}>{@html rich(data.page.data.body)}</div>\n *\n * // or a page's sections as the hosted renderer draws them\n * <div use:bcmsSections={data.sections}>{@html sectionsMarkup(data.sections)}</div>\n *\n * // src/routes/api/draft/+server.ts — the path the editor's \"Preview on site\" opens\n * export const GET = ({ request }) => bcms.draftEnable(request);\n *\n * Settings: options, then `PUBLIC_BCMS_*`, then `BCMS_*` (the same precedence as every adapter).\n */\nimport { createBcmsSite, runSectionScripts, type BcmsSite } from \"@bettercms-ai/sdk/sections\";\nimport type { BcmsSiteOptions } from \"@bettercms-ai/sdk/config\";\nimport { markDraft, withGenerator } from \"@bettercms-ai/sdk/live\";\n\nexport { sectionsMarkup, sectionItems, type SectionsFragment, type SectionsFragmentItem, type SectionItem, type BcmsSite } from \"@bettercms-ai/sdk/sections\";\nexport * from \"@bettercms-ai/sdk/bindings\";\nexport { startBcmsLive, liveSrc, inEditor, onContentApplied, type BcmsChangeEvent, type StartBcmsLiveOptions } from \"@bettercms-ai/sdk/live\";\nexport { plain, rich, lines } from \"@bettercms-ai/sdk/read\";\n\n/** Build the site's BetterCMS access from its settings (pass `{ ...publicEnv, ...privateEnv }`). */\nexport function createBcms(options: BcmsSiteOptions & { env?: Record<string, string | undefined>; draftSecret?: string } = {}): BcmsSite {\n return createBcmsSite({ ...options, prefixes: [\"PUBLIC_\"], adapter: \"@bettercms-ai/sveltekit\" });\n}\n\n/** The least of SvelteKit's `handle` this needs, so the package carries no @sveltejs/kit types. */\ninterface HandleInput {\n event: { request: Request; locals: object };\n resolve(\n event: HandleInput[\"event\"],\n opts?: { transformPageChunk?: (input: { html: string; done: boolean }) => string | undefined },\n ): Response | Promise<Response>;\n}\n\n/**\n * The server hook: `locals.bcmsDraft` says whether this request previews drafts; every page names\n * BetterCMS in its `<head>`, and in drafts carries `<html data-bcms-draft=\"1\">`, the canvas's proof\n * that the frame renders drafts. Compose it with the site's own hooks through `sequence()`.\n */\nexport function bcmsHandle(bcms: BcmsSite) {\n return async ({ event, resolve }: HandleInput): Promise<Response> => {\n const draft = await bcms.isDraft(event.request);\n (event.locals as { bcmsDraft?: boolean }).bcmsDraft = draft;\n // Each streamed chunk is stamped where it holds the tag; both stamps are no-ops anywhere else.\n return resolve(event, { transformPageChunk: ({ html }) => (draft ? markDraft(withGenerator(html)) : withGenerator(html)) });\n };\n}\n\n/**\n * Svelte action for the element holding `{@html sectionsMarkup(…)}`: its scripts run on every page.\n * Pass the sections as its parameter (`use:bcmsSections={data.sections}`): Svelte calls `update`\n * only when the parameter changes, which is how a move between two pages of one route reruns them.\n */\nexport function bcmsSections(node: Element, _sections?: unknown): { update(): void } {\n runSectionScripts(node);\n // After Svelte has written the new markup.\n return { update: () => queueMicrotask(() => runSectionScripts(node)) };\n}\n"],"mappings":";AAgCA,SAAS,gBAAgB,yBAAwC;AAEjE,SAAS,WAAW,qBAAqB;AAEzC,SAAS,gBAAgB,oBAAuG;AAChI,cAAc;AACd,SAAS,eAAe,SAAS,UAAU,wBAAyE;AACpH,SAAS,OAAO,MAAM,aAAa;AAG5B,SAAS,WAAW,UAAgG,CAAC,GAAa;AACvI,SAAO,eAAe,EAAE,GAAG,SAAS,UAAU,CAAC,SAAS,GAAG,SAAS,0BAA0B,CAAC;AACjG;AAgBO,SAAS,WAAW,MAAgB;AACzC,SAAO,OAAO,EAAE,OAAO,QAAQ,MAAsC;AACnE,UAAM,QAAQ,MAAM,KAAK,QAAQ,MAAM,OAAO;AAC9C,IAAC,MAAM,OAAmC,YAAY;AAEtD,WAAO,QAAQ,OAAO,EAAE,oBAAoB,CAAC,EAAE,KAAK,MAAO,QAAQ,UAAU,cAAc,IAAI,CAAC,IAAI,cAAc,IAAI,EAAG,CAAC;AAAA,EAC5H;AACF;AAOO,SAAS,aAAa,MAAe,WAAyC;AACnF,oBAAkB,IAAI;AAEtB,SAAO,EAAE,QAAQ,MAAM,eAAe,MAAM,kBAAkB,IAAI,CAAC,EAAE;AACvE;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bettercms-ai/sveltekit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "BetterCMS for SvelteKit: settings from PUBLIC_BCMS_*, the read client per request with draft mode, the editor's draft routes, and the page sections server-rendered by BetterCMS.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@bettercms-ai/sdk": "^1.
|
|
22
|
+
"@bettercms-ai/sdk": "^1.41.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"svelte": ">=4"
|