@bettercms-ai/sveltekit 0.3.0 → 0.5.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/dist/index.d.ts +15 -1
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -61,6 +61,20 @@ interface HandleInput {
|
|
|
61
61
|
* that the frame renders drafts. Compose it with the site's own hooks through `sequence()`.
|
|
62
62
|
*/
|
|
63
63
|
declare function bcmsHandle(bcms: BcmsSite): ({ event, resolve }: HandleInput) => Promise<Response>;
|
|
64
|
+
/**
|
|
65
|
+
* Svelte action for the element around the page (`<div use:bcmsEditable>` in the root layout): while
|
|
66
|
+
* the page previews drafts (the `<html data-bcms-draft>` bcmsHandle stamps), every CMS string under it
|
|
67
|
+
* that carries a stega frame (`stega: 2`, the read client's default for drafts) becomes a
|
|
68
|
+
* click-to-edit stamp, again whenever its content changes and after each edit the editor applies.
|
|
69
|
+
* Pass the page's id (`use:bcmsEditable={data.page.id}`) so its own fields stamp as `data-bcms-field`;
|
|
70
|
+
* a client-side navigation keeps the layout's element and changes the id, so `update` watches again
|
|
71
|
+
* for the new page. A no-op outside drafts: the stamping module is imported only behind that check.
|
|
72
|
+
* @see watchEditable
|
|
73
|
+
*/
|
|
74
|
+
declare function bcmsEditable(node: HTMLElement, pageId?: string): {
|
|
75
|
+
update(pageId?: string): void;
|
|
76
|
+
destroy(): void;
|
|
77
|
+
};
|
|
64
78
|
/**
|
|
65
79
|
* Svelte action for the element holding `{@html sectionsMarkup(…)}`: its scripts run on every page.
|
|
66
80
|
* Pass the sections as its parameter (`use:bcmsSections={data.sections}`): Svelte calls `update`
|
|
@@ -70,4 +84,4 @@ declare function bcmsSections(node: Element, _sections?: unknown): {
|
|
|
70
84
|
update(): void;
|
|
71
85
|
};
|
|
72
86
|
|
|
73
|
-
export { bcmsHandle, bcmsSections, createBcms };
|
|
87
|
+
export { bcmsEditable, bcmsHandle, bcmsSections, createBcms };
|
package/dist/index.js
CHANGED
|
@@ -15,11 +15,38 @@ function bcmsHandle(bcms) {
|
|
|
15
15
|
return resolve(event, { transformPageChunk: ({ html }) => draft ? markDraft(withGenerator(html)) : withGenerator(html) });
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
+
function bcmsEditable(node, pageId) {
|
|
19
|
+
let stop;
|
|
20
|
+
let gone = false;
|
|
21
|
+
let current = pageId;
|
|
22
|
+
const draft = document.documentElement.hasAttribute("data-bcms-draft");
|
|
23
|
+
const watch = () => void import("@bettercms-ai/sdk/editable").then(({ watchEditable }) => {
|
|
24
|
+
if (gone) return;
|
|
25
|
+
stop?.();
|
|
26
|
+
stop = watchEditable(node, current ? { pageId: current } : {});
|
|
27
|
+
});
|
|
28
|
+
if (draft) {
|
|
29
|
+
node.setAttribute("data-bcms-scope", "");
|
|
30
|
+
watch();
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
update: (next) => {
|
|
34
|
+
if (!draft || next === current) return;
|
|
35
|
+
current = next;
|
|
36
|
+
watch();
|
|
37
|
+
},
|
|
38
|
+
destroy: () => {
|
|
39
|
+
gone = true;
|
|
40
|
+
stop?.();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
18
44
|
function bcmsSections(node, _sections) {
|
|
19
45
|
runSectionScripts(node);
|
|
20
46
|
return { update: () => queueMicrotask(() => runSectionScripts(node)) };
|
|
21
47
|
}
|
|
22
48
|
export {
|
|
49
|
+
bcmsEditable,
|
|
23
50
|
bcmsHandle,
|
|
24
51
|
bcmsSections,
|
|
25
52
|
createBcms,
|
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: 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":[]}
|
|
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 around the page (`<div use:bcmsEditable>` in the root layout): while\n * the page previews drafts (the `<html data-bcms-draft>` bcmsHandle stamps), every CMS string under it\n * that carries a stega frame (`stega: 2`, the read client's default for drafts) becomes a\n * click-to-edit stamp, again whenever its content changes and after each edit the editor applies.\n * Pass the page's id (`use:bcmsEditable={data.page.id}`) so its own fields stamp as `data-bcms-field`;\n * a client-side navigation keeps the layout's element and changes the id, so `update` watches again\n * for the new page. A no-op outside drafts: the stamping module is imported only behind that check.\n * @see watchEditable\n */\nexport function bcmsEditable(node: HTMLElement, pageId?: string): { update(pageId?: string): void; destroy(): void } {\n let stop: (() => void) | undefined;\n let gone = false;\n let current = pageId;\n const draft = document.documentElement.hasAttribute(\"data-bcms-draft\");\n const watch = () =>\n void import(\"@bettercms-ai/sdk/editable\").then(({ watchEditable }) => {\n if (gone) return;\n stop?.();\n stop = watchEditable(node, current ? { pageId: current } : {});\n });\n if (draft) {\n node.setAttribute(\"data-bcms-scope\", \"\");\n watch();\n }\n return {\n update: (next) => {\n if (!draft || next === current) return;\n current = next;\n watch();\n },\n destroy: () => {\n gone = true;\n stop?.();\n },\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"],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B,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.5.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.47.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"svelte": ">=4"
|