@bettercms-ai/nuxt 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 CHANGED
@@ -65,10 +65,29 @@ interface NitroLike {
65
65
  * frame renders drafts (and what `startBcmsLive` reads to load the click-to-edit overlay).
66
66
  */
67
67
  declare function bcmsNitroPlugin(bcms: BcmsSite): (nitroApp: NitroLike) => void;
68
+ /**
69
+ * `v-bcms-editable` on the element around the page (`<div v-bcms-editable="page.id">` in the app's
70
+ * layout): while the page previews drafts (the `<html data-bcms-draft>` bcmsNitroPlugin stamps),
71
+ * every CMS string under it that carries a stega frame (`stega: 2`, the read client's default for
72
+ * drafts) becomes a click-to-edit stamp, again whenever its content changes and after each edit the
73
+ * editor applies. The value is the page's id, so its own fields stamp as `data-bcms-field`; a
74
+ * client-side navigation keeps the layout's element and changes the id, so `updated` watches again
75
+ * for the new page. A no-op outside drafts: the stamping module is imported only behind that check.
76
+ * @see watchEditable
77
+ */
78
+ declare const vBcmsEditable: {
79
+ mounted(el: Element, binding: {
80
+ value?: string;
81
+ }): void;
82
+ updated(el: Element, binding: {
83
+ value?: string;
84
+ }): void;
85
+ unmounted(el: Element): void;
86
+ };
68
87
  /** Vue directive for the element holding `v-html="sectionsMarkup(…)"`: its scripts run on every page. */
69
88
  declare const vBcmsSections: {
70
89
  mounted: (el: Element) => void;
71
90
  updated: (el: Element) => void;
72
91
  };
73
92
 
74
- export { bcmsNitroPlugin, createBcms, vBcmsSections };
93
+ export { bcmsNitroPlugin, createBcms, vBcmsEditable, vBcmsSections };
package/dist/index.js CHANGED
@@ -18,6 +18,34 @@ function bcmsNitroPlugin(bcms) {
18
18
  });
19
19
  };
20
20
  }
21
+ var editing = /* @__PURE__ */ new WeakMap();
22
+ var watchFor = (el, run) => void import("@bettercms-ai/sdk/editable").then(({ watchEditable }) => {
23
+ if (run.gone) return;
24
+ run.stop?.();
25
+ run.stop = watchEditable(el, run.pageId ? { pageId: run.pageId } : {});
26
+ });
27
+ var vBcmsEditable = {
28
+ mounted(el, binding) {
29
+ if (!document.documentElement.hasAttribute("data-bcms-draft")) return;
30
+ const run = { gone: false, pageId: binding.value };
31
+ editing.set(el, run);
32
+ el.setAttribute("data-bcms-scope", "");
33
+ watchFor(el, run);
34
+ },
35
+ updated(el, binding) {
36
+ const run = editing.get(el);
37
+ if (!run || run.pageId === binding.value) return;
38
+ run.pageId = binding.value;
39
+ watchFor(el, run);
40
+ },
41
+ unmounted(el) {
42
+ const run = editing.get(el);
43
+ if (!run) return;
44
+ run.gone = true;
45
+ run.stop?.();
46
+ editing.delete(el);
47
+ }
48
+ };
21
49
  var vBcmsSections = {
22
50
  mounted: (el) => runSectionScripts(el),
23
51
  updated: (el) => runSectionScripts(el)
@@ -34,6 +62,7 @@ export {
34
62
  sectionItems,
35
63
  sectionsMarkup,
36
64
  startBcmsLive,
65
+ vBcmsEditable,
37
66
  vBcmsSections
38
67
  };
39
68
  //# 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/nuxt — BetterCMS for Nuxt: the site's own markup bound to its content, so every\n * element is editable in the BetterCMS visual editor, and the site stays live on publish.\n *\n * // server/utils/bcms.ts\n * export const bcms = createBcms({ env: process.env });\n *\n * // server/plugins/bcms.ts — `<html data-bcms-draft>` in drafts, `<meta name=\"generator\">`\n * export default defineNitroPlugin(bcmsNitroPlugin(bcms));\n *\n * // plugins/bcms.client.ts — a publish, and the editor's re-render request, refetch the data\n * export default defineNuxtPlugin(() => { startBcmsLive({ refresh: () => refreshNuxtData() }) });\n *\n * // server/api/page/[slug].get.ts — drafts while the editor previews\n * export default defineEventHandler(async (e) => (await bcms.client(toWebRequest(e))).getPage(getRouterParam(e, \"slug\")!));\n *\n * // pages/index.vue — the site's OWN markup, bound to its fields\n * const { data: page } = await useFetch(\"/api/page/home\");\n * <h1 v-bind=\"fieldAttrs('hero-title', page.fieldStyles)\">{{ page.data[\"hero-title\"] }}</h1>\n * <img v-bind=\"imageAttrs(page.data.cover, { path: 'cover', sizes: '100vw' })\" class=\"cover\" />\n * <div v-bind=\"fieldAttrs('body')\" v-html=\"rich(page.data.body)\" />\n *\n * // or a page's sections as the hosted renderer draws them\n * <div v-bcms-sections v-html=\"sectionsMarkup(sections)\" />\n *\n * // server/routes/api/draft.get.ts — the path the editor's \"Preview on site\" opens\n * export default defineEventHandler((e) => bcms.draftEnable(toWebRequest(e)));\n *\n * Settings: options, then `NUXT_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 { GENERATOR_META } 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 `process.env` on the server). */\nexport function createBcms(options: BcmsSiteOptions & { env?: Record<string, string | undefined>; draftSecret?: string } = {}): BcmsSite {\n return createBcmsSite({ ...options, prefixes: [\"NUXT_PUBLIC_\"], adapter: \"@bettercms-ai/nuxt\" });\n}\n\n/** The least of Nitro this needs, so the package carries no nitropack types. */\ninterface NitroLike {\n hooks: {\n hook(\n name: \"render:html\",\n fn: (html: { htmlAttrs: string[]; head: string[] }, ctx: { event: { headers?: Headers; node?: { req?: { headers?: Record<string, string | string[] | undefined> } } } }) => unknown,\n ): unknown;\n };\n}\n\n/**\n * The Nitro plugin: every server-rendered page names BetterCMS in its `<head>` and, while the\n * request previews drafts, carries `<html data-bcms-draft=\"1\">`, the canvas's proof that the\n * frame renders drafts (and what `startBcmsLive` reads to load the click-to-edit overlay).\n */\nexport function bcmsNitroPlugin(bcms: BcmsSite) {\n return (nitroApp: NitroLike): void => {\n nitroApp.hooks.hook(\"render:html\", async (html, { event }) => {\n html.head.push(GENERATOR_META);\n const raw = event.headers?.get(\"cookie\") ?? event.node?.req?.headers?.cookie;\n const cookie = Array.isArray(raw) ? raw.join(\"; \") : raw;\n if (cookie && (await bcms.isDraft(new Request(\"http://bcms.local/\", { headers: { cookie } })))) html.htmlAttrs.push('data-bcms-draft=\"1\"');\n });\n };\n}\n\n/** Vue directive for the element holding `v-html=\"sectionsMarkup(…)\"`: its scripts run on every page. */\nexport const vBcmsSections = {\n mounted: (el: Element) => runSectionScripts(el),\n updated: (el: Element) => runSectionScripts(el),\n};\n"],"mappings":";AA+BA,SAAS,gBAAgB,yBAAwC;AAEjE,SAAS,sBAAsB;AAE/B,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,cAAc,GAAG,SAAS,qBAAqB,CAAC;AACjG;AAiBO,SAAS,gBAAgB,MAAgB;AAC9C,SAAO,CAAC,aAA8B;AACpC,aAAS,MAAM,KAAK,eAAe,OAAO,MAAM,EAAE,MAAM,MAAM;AAC5D,WAAK,KAAK,KAAK,cAAc;AAC7B,YAAM,MAAM,MAAM,SAAS,IAAI,QAAQ,KAAK,MAAM,MAAM,KAAK,SAAS;AACtE,YAAM,SAAS,MAAM,QAAQ,GAAG,IAAI,IAAI,KAAK,IAAI,IAAI;AACrD,UAAI,UAAW,MAAM,KAAK,QAAQ,IAAI,QAAQ,sBAAsB,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC,EAAI,MAAK,UAAU,KAAK,qBAAqB;AAAA,IAC3I,CAAC;AAAA,EACH;AACF;AAGO,IAAM,gBAAgB;AAAA,EAC3B,SAAS,CAAC,OAAgB,kBAAkB,EAAE;AAAA,EAC9C,SAAS,CAAC,OAAgB,kBAAkB,EAAE;AAChD;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/// <reference lib=\"dom\" />\n/**\n * @bettercms-ai/nuxt — BetterCMS for Nuxt: the site's own markup bound to its content, so every\n * element is editable in the BetterCMS visual editor, and the site stays live on publish.\n *\n * // server/utils/bcms.ts\n * export const bcms = createBcms({ env: process.env });\n *\n * // server/plugins/bcms.ts — `<html data-bcms-draft>` in drafts, `<meta name=\"generator\">`\n * export default defineNitroPlugin(bcmsNitroPlugin(bcms));\n *\n * // plugins/bcms.client.ts — a publish, and the editor's re-render request, refetch the data\n * export default defineNuxtPlugin(() => { startBcmsLive({ refresh: () => refreshNuxtData() }) });\n *\n * // server/api/page/[slug].get.ts — drafts while the editor previews\n * export default defineEventHandler(async (e) => (await bcms.client(toWebRequest(e))).getPage(getRouterParam(e, \"slug\")!));\n *\n * // pages/index.vue — the site's OWN markup, bound to its fields\n * const { data: page } = await useFetch(\"/api/page/home\");\n * <h1 v-bind=\"fieldAttrs('hero-title', page.fieldStyles)\">{{ page.data[\"hero-title\"] }}</h1>\n * <img v-bind=\"imageAttrs(page.data.cover, { path: 'cover', sizes: '100vw' })\" class=\"cover\" />\n * <div v-bind=\"fieldAttrs('body')\" v-html=\"rich(page.data.body)\" />\n *\n * // or a page's sections as the hosted renderer draws them\n * <div v-bcms-sections v-html=\"sectionsMarkup(sections)\" />\n *\n * // server/routes/api/draft.get.ts — the path the editor's \"Preview on site\" opens\n * export default defineEventHandler((e) => bcms.draftEnable(toWebRequest(e)));\n *\n * Settings: options, then `NUXT_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 { GENERATOR_META } 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 `process.env` on the server). */\nexport function createBcms(options: BcmsSiteOptions & { env?: Record<string, string | undefined>; draftSecret?: string } = {}): BcmsSite {\n return createBcmsSite({ ...options, prefixes: [\"NUXT_PUBLIC_\"], adapter: \"@bettercms-ai/nuxt\" });\n}\n\n/** The least of Nitro this needs, so the package carries no nitropack types. */\ninterface NitroLike {\n hooks: {\n hook(\n name: \"render:html\",\n fn: (html: { htmlAttrs: string[]; head: string[] }, ctx: { event: { headers?: Headers; node?: { req?: { headers?: Record<string, string | string[] | undefined> } } } }) => unknown,\n ): unknown;\n };\n}\n\n/**\n * The Nitro plugin: every server-rendered page names BetterCMS in its `<head>` and, while the\n * request previews drafts, carries `<html data-bcms-draft=\"1\">`, the canvas's proof that the\n * frame renders drafts (and what `startBcmsLive` reads to load the click-to-edit overlay).\n */\nexport function bcmsNitroPlugin(bcms: BcmsSite) {\n return (nitroApp: NitroLike): void => {\n nitroApp.hooks.hook(\"render:html\", async (html, { event }) => {\n html.head.push(GENERATOR_META);\n const raw = event.headers?.get(\"cookie\") ?? event.node?.req?.headers?.cookie;\n const cookie = Array.isArray(raw) ? raw.join(\"; \") : raw;\n if (cookie && (await bcms.isDraft(new Request(\"http://bcms.local/\", { headers: { cookie } })))) html.htmlAttrs.push('data-bcms-draft=\"1\"');\n });\n };\n}\n\n/** Each element's watch: the page it stamps for, and how to stop it. */\ntype EditRun = { gone: boolean; pageId?: string; stop?: () => void };\nconst editing = new WeakMap<Element, EditRun>();\n\n/** (Re)start `el`'s watch for `run.pageId`, once the stamping module is loaded. */\nconst watchFor = (el: Element, run: EditRun) =>\n void import(\"@bettercms-ai/sdk/editable\").then(({ watchEditable }) => {\n if (run.gone) return;\n run.stop?.();\n run.stop = watchEditable(el, run.pageId ? { pageId: run.pageId } : {});\n });\n\n/**\n * `v-bcms-editable` on the element around the page (`<div v-bcms-editable=\"page.id\">` in the app's\n * layout): while the page previews drafts (the `<html data-bcms-draft>` bcmsNitroPlugin stamps),\n * every CMS string under it that carries a stega frame (`stega: 2`, the read client's default for\n * drafts) becomes a click-to-edit stamp, again whenever its content changes and after each edit the\n * editor applies. The value is the page's id, so its own fields stamp as `data-bcms-field`; a\n * client-side navigation keeps the layout's element and changes the id, so `updated` 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 const vBcmsEditable = {\n mounted(el: Element, binding: { value?: string }) {\n if (!document.documentElement.hasAttribute(\"data-bcms-draft\")) return;\n const run: EditRun = { gone: false, pageId: binding.value };\n editing.set(el, run);\n el.setAttribute(\"data-bcms-scope\", \"\");\n watchFor(el, run);\n },\n updated(el: Element, binding: { value?: string }) {\n const run = editing.get(el);\n if (!run || run.pageId === binding.value) return;\n run.pageId = binding.value;\n watchFor(el, run);\n },\n unmounted(el: Element) {\n const run = editing.get(el);\n if (!run) return;\n run.gone = true;\n run.stop?.();\n editing.delete(el);\n },\n};\n\n/** Vue directive for the element holding `v-html=\"sectionsMarkup(…)\"`: its scripts run on every page. */\nexport const vBcmsSections = {\n mounted: (el: Element) => runSectionScripts(el),\n updated: (el: Element) => runSectionScripts(el),\n};\n"],"mappings":";AA+BA,SAAS,gBAAgB,yBAAwC;AAEjE,SAAS,sBAAsB;AAE/B,SAAS,gBAAgB,oBAAuG;AAChI,cAAc;AACd,SAAS,eAAe,SAAS,UAAU,wBAAyE;AACpH,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AAI,WAAW,QAAQ,MAAO;AAC1C,QAAI,SAAS,QAAQ;AACrB,aAAS,IAAI,GAAG;AAAA,EAClB;AAAA,EACA,UAAU,IAAa;AACrB,UAAM,MAAM,QAAQ,IAAI,EAAE;AAC1B,QAAI,CAAC,IAAK;AACV,QAAI,OAAO;AACX,QAAI,OAAO;AACX,YAAQ,OAAO,EAAE;AAAA,EACnB;AACF;AAGO,IAAM,gBAAgB;AAAA,EAC3B,SAAS,CAAC,OAAgB,kBAAkB,EAAE;AAAA,EAC9C,SAAS,CAAC,OAAgB,kBAAkB,EAAE;AAChD;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettercms-ai/nuxt",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "BetterCMS for Nuxt: settings from NUXT_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.40.0"
22
+ "@bettercms-ai/sdk": "^1.47.0"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "vue": ">=3"