@kidecms/core 0.1.9 → 0.1.10

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.
Files changed (2) hide show
  1. package/client/preview.ts +35 -0
  2. package/package.json +2 -1
@@ -0,0 +1,35 @@
1
+ if (new URLSearchParams(location.search).has("preview")) {
2
+ const ch = new BroadcastChannel("cms-preview");
3
+ let pending = 0;
4
+
5
+ ch.onmessage = (e: MessageEvent) => {
6
+ const d = e.data;
7
+
8
+ if (d.type === "reload") {
9
+ location.reload();
10
+ return;
11
+ }
12
+
13
+ const els = document.querySelectorAll<HTMLElement>(`[data-cms="${d.field}"]`);
14
+ if (!els.length) return;
15
+
16
+ if (d.render) {
17
+ const id = ++pending;
18
+ fetch("/api/cms/preview/render", {
19
+ method: "POST",
20
+ headers: { "Content-Type": "application/json" },
21
+ body: JSON.stringify({ type: d.render, data: d.value }),
22
+ })
23
+ .then((r) => (r.ok ? r.text() : null))
24
+ .then((html) => {
25
+ // Endpoint may not exist in production builds (only dev) — silently skip
26
+ if (html != null && id === pending) els.forEach((el) => (el.innerHTML = html));
27
+ })
28
+ .catch(() => {});
29
+ } else if (d.html != null) {
30
+ els.forEach((el) => (el.innerHTML = d.html));
31
+ } else {
32
+ els.forEach((el) => (el.textContent = d.value));
33
+ }
34
+ };
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kidecms/core",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Code-first CMS framework built for Astro.",
5
5
  "author": "Matti Hernesniemi",
6
6
  "license": "MIT",
@@ -22,6 +22,7 @@
22
22
  "files": [
23
23
  "dist",
24
24
  "admin",
25
+ "client",
25
26
  "routes",
26
27
  "middleware",
27
28
  "virtual.d.ts"