@wervt/cms 0.0.3 → 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.
@@ -5,9 +5,9 @@
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
6
  <meta name="robots" content="noindex" />
7
7
  <title>Edit content</title>
8
- <script type="module" crossorigin src="/_cms/assets/index-JJD_O40W.js"></script>
9
- <link rel="modulepreload" crossorigin href="/_cms/assets/Button-BHCKwuIs.js">
10
- <link rel="stylesheet" crossorigin href="/_cms/assets/index-kXxO7vu1.css">
8
+ <script type="module" crossorigin src="/_cms/assets/index-Du-lvx2V.js"></script>
9
+ <link rel="modulepreload" crossorigin href="/_cms/assets/Button-Dai6Ph2c.js">
10
+ <link rel="stylesheet" crossorigin href="/_cms/assets/index-CP6dGIyl.css">
11
11
  </head>
12
12
  <body>
13
13
  <div id="app" class="isolate"></div>
package/dist/module.js CHANGED
@@ -2,7 +2,7 @@ import { editorThemeHead } from "./editor-theme.js";
2
2
  import { stampEntries } from "./runtime/server/entries.js";
3
3
  import { existsSync, mkdirSync, readFileSync } from "node:fs";
4
4
  import { join, relative, resolve } from "node:path";
5
- import { addComponent, addImports, addServerHandler, addServerTemplate, addTemplate, addTypeTemplate, createResolver, defineNuxtModule, resolveFiles } from "nuxt/kit";
5
+ import { addComponent, addImports, addPlugin, addServerHandler, addServerTemplate, addTemplate, addTypeTemplate, createResolver, defineNuxtModule, resolveFiles } from "nuxt/kit";
6
6
  // src/module.ts
7
7
  var module_default = defineNuxtModule({
8
8
  meta: {
@@ -107,6 +107,10 @@ var module_default = defineNuxtModule({
107
107
  name: "CmsRichText",
108
108
  filePath: resolver.resolve("./runtime/app/CmsRichText")
109
109
  });
110
+ addPlugin({
111
+ src: resolver.resolve("./runtime/app/preview.client"),
112
+ mode: "client"
113
+ });
110
114
  setupEditor(resolver.resolve("../dist/editor"), editorThemeHead(options.editor?.theme), nuxt);
111
115
  }
112
116
  });
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Values the editor shows in its preview before they are saved, by page key. Only written in the
3
+ * browser inside the editor's iframe, so the server's copy, shared by all requests, stays empty.
4
+ */
5
+ export declare const drafts: import("vue").ShallowRef<Record<string, Record<string, unknown>>, Record<string, Record<string, unknown>>>;
@@ -0,0 +1,8 @@
1
+ import { shallowRef } from "vue";
2
+ // src/runtime/app/drafts.ts
3
+ /**
4
+ * Values the editor shows in its preview before they are saved, by page key. Only written in the
5
+ * browser inside the editor's iframe, so the server's copy, shared by all requests, stays empty.
6
+ */
7
+ const drafts = shallowRef({});
8
+ export { drafts };
@@ -0,0 +1,2 @@
1
+ declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
2
+ export { _default as default };
@@ -0,0 +1,28 @@
1
+ import { readMessage, siteMessage } from "../preview.js";
2
+ import { drafts } from "./drafts.js";
3
+ import { defineNuxtPlugin, useRouter } from "#app";
4
+ // src/runtime/app/preview.client.ts
5
+ var preview_client_default = defineNuxtPlugin((nuxtApp) => {
6
+ const parent = window.parent;
7
+ if (parent === window) return;
8
+ try {
9
+ if (parent.location.origin !== location.origin || !parent.location.pathname.startsWith("/_cms")) return;
10
+ } catch {
11
+ return;
12
+ }
13
+ const router = useRouter();
14
+ const report = (path) => parent.postMessage(siteMessage(path), location.origin);
15
+ addEventListener("message", (event) => {
16
+ const message = readMessage(event, parent, location.origin, "draft");
17
+ if (message) drafts.value = {
18
+ ...drafts.value,
19
+ [message.key]: message.value
20
+ };
21
+ });
22
+ nuxtApp.hook("app:mounted", () => report(router.currentRoute.value.fullPath));
23
+ router.afterEach((to) => {
24
+ if (nuxtApp.isHydrating) return;
25
+ report(to.fullPath);
26
+ });
27
+ });
28
+ export { preview_client_default as default };
@@ -1,5 +1,6 @@
1
1
  import { resolveValue, staticImages } from "../fields.js";
2
2
  import { pages } from "../pages.js";
3
+ import { drafts } from "./drafts.js";
3
4
  import { computed } from "vue";
4
5
  import { useFetch } from "#app";
5
6
  // src/runtime/app/useCms.ts
@@ -7,7 +8,7 @@ function useCms(key, defaults) {
7
8
  const { data } = useFetch(`/api/_cms/entries/${key}`, { key: `wervt-cms:${key}` });
8
9
  const page = pages.get(key);
9
10
  return computed(() => {
10
- const stored = data.value?.value;
11
+ const stored = drafts.value[key] ?? data.value?.value;
11
12
  if (defaults) return {
12
13
  ...defaults,
13
14
  ...stored
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The editor at `/_cms` shows the site in an iframe and sends it the values nobody saved yet. Both
3
+ * sides share one origin; these are the messages between them.
4
+ */
5
+ declare const source = "wervt-cms";
6
+ /** From the site in the iframe: it loaded or moved to `path`. */
7
+ export interface SiteMessage {
8
+ source: typeof source;
9
+ type: 'page';
10
+ path: string;
11
+ }
12
+ /** From the editor: show `value` for `key` until the next one. */
13
+ export interface EditorMessage {
14
+ source: typeof source;
15
+ type: 'draft';
16
+ key: string;
17
+ value: Record<string, unknown>;
18
+ }
19
+ export declare const siteMessage: (path: string) => SiteMessage;
20
+ /** Values are copied as JSON: `postMessage` refuses Vue's reactive proxies. */
21
+ export declare const editorMessage: (key: string, value: unknown) => EditorMessage;
22
+ /**
23
+ * The message's data if it came from `from` on this origin and has the expected shape. Anything
24
+ * else (other frames, browser extensions, other origins) is ignored.
25
+ */
26
+ export declare function readMessage<T extends SiteMessage | EditorMessage>(event: Pick<MessageEvent, 'data' | 'origin' | 'source'>, from: unknown, origin: string, type: T['type']): T | undefined;
27
+ /** `/menu/` and `/menu?x#y` are `/menu`. */
28
+ export declare const normalizePath: (path: string) => string;
29
+ /** The page whose `path` is where the site is. */
30
+ export declare const pageAt: <P extends {
31
+ key: string;
32
+ path?: string;
33
+ }>(pages: P[], path: string) => P | undefined;
34
+ export {};
@@ -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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wervt/cms",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
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",