@kidecms/core 0.27.0 → 0.27.1
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/admin/components/BlockEditor.tsx +3 -1
- package/admin/components/ContentEditor.tsx +5 -3
- package/admin/components/RichTextEditor.tsx +5 -3
- package/admin/components/UnsavedGuard.tsx +14 -12
- package/admin/lib/preview-channel.ts +13 -0
- package/admin/views/EditView.astro +7 -6
- package/admin/views/edit/EditScripts.astro +3 -1
- package/client/preview.ts +5 -2
- package/package.json +1 -1
- package/platform/cloudflare/cf-env.ts +1 -4
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { useState, useCallback, useEffect, useRef } from "react";
|
|
4
4
|
import { ArrowUpRight, GripVertical, ChevronRight, Link2, Plus, Save, Trash2, Unlink } from "lucide-react";
|
|
5
5
|
import { cn } from "../lib/utils";
|
|
6
|
+
import { openPreviewChannel } from "../lib/preview-channel";
|
|
6
7
|
import {
|
|
7
8
|
DndContext,
|
|
8
9
|
closestCenter,
|
|
@@ -300,8 +301,9 @@ export default function BlockEditor({
|
|
|
300
301
|
// Live preview: broadcast block data for server-side rendering. Also replay the
|
|
301
302
|
// current value when a preview tab opened after edits announces itself.
|
|
302
303
|
useEffect(() => {
|
|
303
|
-
if (!previewChannelRef.current) previewChannelRef.current =
|
|
304
|
+
if (!previewChannelRef.current) previewChannelRef.current = openPreviewChannel();
|
|
304
305
|
const channel = previewChannelRef.current;
|
|
306
|
+
if (!channel) return;
|
|
305
307
|
const broadcast = () => channel.postMessage({ field: name, value: serializeBlocks(blocks), render: "blocks" });
|
|
306
308
|
broadcast();
|
|
307
309
|
channel.onmessage = (e: MessageEvent) => {
|
|
@@ -27,6 +27,7 @@ import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, D
|
|
|
27
27
|
import { Input } from "./ui/input";
|
|
28
28
|
import { cn } from "../lib/utils";
|
|
29
29
|
import { toggleHeading } from "../lib/editor-commands";
|
|
30
|
+
import { openPreviewChannel } from "../lib/preview-channel";
|
|
30
31
|
import {
|
|
31
32
|
LinkDialog,
|
|
32
33
|
fetchLinkGroups,
|
|
@@ -457,8 +458,9 @@ export default function ContentEditor({
|
|
|
457
458
|
|
|
458
459
|
// Replay the current value when a preview tab opened after edits announces itself
|
|
459
460
|
useEffect(() => {
|
|
460
|
-
if (!previewChannelRef.current) previewChannelRef.current =
|
|
461
|
+
if (!previewChannelRef.current) previewChannelRef.current = openPreviewChannel();
|
|
461
462
|
const channel = previewChannelRef.current;
|
|
463
|
+
if (!channel) return;
|
|
462
464
|
channel.onmessage = (e: MessageEvent) => {
|
|
463
465
|
if (e.data?.type === "preview-ready") {
|
|
464
466
|
channel.postMessage({ field: name, value: cmsJson, render: "content" });
|
|
@@ -495,8 +497,8 @@ export default function ContentEditor({
|
|
|
495
497
|
const json = JSON.stringify(cmsDoc);
|
|
496
498
|
setCmsJson(json);
|
|
497
499
|
// Live preview: broadcast for server-side rendering
|
|
498
|
-
if (!previewChannelRef.current) previewChannelRef.current =
|
|
499
|
-
previewChannelRef.current
|
|
500
|
+
if (!previewChannelRef.current) previewChannelRef.current = openPreviewChannel();
|
|
501
|
+
previewChannelRef.current?.postMessage({ field: name, value: json, render: "content" });
|
|
500
502
|
},
|
|
501
503
|
onSelectionUpdate: forceTick,
|
|
502
504
|
onTransaction: forceTick,
|
|
@@ -27,6 +27,7 @@ import { Label } from "./ui/label";
|
|
|
27
27
|
import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
|
|
28
28
|
import { cn } from "../lib/utils";
|
|
29
29
|
import { toggleHeading } from "../lib/editor-commands";
|
|
30
|
+
import { openPreviewChannel } from "../lib/preview-channel";
|
|
30
31
|
import ImageBrowseDialog from "./ImageBrowseDialog";
|
|
31
32
|
|
|
32
33
|
// -----------------------------------------------
|
|
@@ -441,8 +442,9 @@ export default function RichTextEditor({ name, initialValue, rows = 10, onChange
|
|
|
441
442
|
|
|
442
443
|
// Replay the current value when a preview tab opened after edits announces itself
|
|
443
444
|
useEffect(() => {
|
|
444
|
-
if (!previewChannelRef.current) previewChannelRef.current =
|
|
445
|
+
if (!previewChannelRef.current) previewChannelRef.current = openPreviewChannel();
|
|
445
446
|
const channel = previewChannelRef.current;
|
|
447
|
+
if (!channel) return;
|
|
446
448
|
channel.onmessage = (e: MessageEvent) => {
|
|
447
449
|
if (e.data?.type === "preview-ready") {
|
|
448
450
|
channel.postMessage({ field: name, value: cmsJson, render: "richText" });
|
|
@@ -475,8 +477,8 @@ export default function RichTextEditor({ name, initialValue, rows = 10, onChange
|
|
|
475
477
|
setCmsJson(json);
|
|
476
478
|
onChange?.(json);
|
|
477
479
|
// Live preview: broadcast for server-side rendering
|
|
478
|
-
if (!previewChannelRef.current) previewChannelRef.current =
|
|
479
|
-
previewChannelRef.current
|
|
480
|
+
if (!previewChannelRef.current) previewChannelRef.current = openPreviewChannel();
|
|
481
|
+
previewChannelRef.current?.postMessage({ field: name, value: json, render: "richText" });
|
|
480
482
|
},
|
|
481
483
|
onSelectionUpdate: forceTick,
|
|
482
484
|
onTransaction: forceTick,
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
AlertDialogHeader,
|
|
12
12
|
AlertDialogTitle,
|
|
13
13
|
} from "./ui/alert-dialog";
|
|
14
|
+
import { openPreviewChannel } from "../lib/preview-channel";
|
|
14
15
|
import { Button } from "./ui/button";
|
|
15
16
|
|
|
16
17
|
/**
|
|
@@ -68,26 +69,27 @@ export default function UnsavedGuard({
|
|
|
68
69
|
};
|
|
69
70
|
|
|
70
71
|
// Live preview: broadcast field changes to any open preview tab
|
|
71
|
-
const previewChannel =
|
|
72
|
+
const previewChannel = openPreviewChannel();
|
|
72
73
|
const broadcastField = (e: Event) => {
|
|
73
74
|
const target = e.target as HTMLInputElement;
|
|
74
75
|
if (!target.name || target.name.startsWith("_") || target.name === "redirectTo") return;
|
|
75
76
|
// Skip hidden inputs — complex fields (rich text, blocks) handle their own broadcasting
|
|
76
77
|
if (target.type === "hidden") return;
|
|
77
|
-
previewChannel
|
|
78
|
+
previewChannel?.postMessage({ field: target.name, value: target.value });
|
|
78
79
|
};
|
|
79
80
|
|
|
80
81
|
// A preview tab opened after edits were made announces itself; replay the
|
|
81
82
|
// current value of every simple field so it doesn't miss unsaved changes.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
if (previewChannel)
|
|
84
|
+
previewChannel.onmessage = (e: MessageEvent) => {
|
|
85
|
+
if (e.data?.type !== "preview-ready") return;
|
|
86
|
+
for (const el of Array.from(form.elements)) {
|
|
87
|
+
const input = el as HTMLInputElement;
|
|
88
|
+
if (!input.name || input.name.startsWith("_") || input.name === "redirectTo") continue;
|
|
89
|
+
if (input.type === "hidden" || input.type === "submit" || input.type === "button") continue;
|
|
90
|
+
previewChannel?.postMessage({ field: input.name, value: input.value });
|
|
91
|
+
}
|
|
92
|
+
};
|
|
91
93
|
|
|
92
94
|
form.addEventListener("input", checkDirty);
|
|
93
95
|
form.addEventListener("input", broadcastField);
|
|
@@ -101,7 +103,7 @@ export default function UnsavedGuard({
|
|
|
101
103
|
form.removeEventListener("change", checkDirty);
|
|
102
104
|
form.removeEventListener("change", broadcastField);
|
|
103
105
|
form.removeEventListener("submit", handleSubmit);
|
|
104
|
-
previewChannel
|
|
106
|
+
previewChannel?.close();
|
|
105
107
|
};
|
|
106
108
|
}, [formId, isNew, isDraft]);
|
|
107
109
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Live preview is scoped per document: the edit view stamps
|
|
2
|
+
// `data-preview-channel="<collection>:<id>"` and links the preview tab with the same
|
|
3
|
+
// key in `?preview=`, so an editor only drives the preview of its own document
|
|
4
|
+
// (a single shared channel let any open edit tab overwrite every preview tab).
|
|
5
|
+
|
|
6
|
+
export const previewChannelName = (key: string) => `cms-preview:${key}`;
|
|
7
|
+
|
|
8
|
+
/** Opens this edit view's preview channel, or null when the document has no preview. */
|
|
9
|
+
export function openPreviewChannel(): BroadcastChannel | null {
|
|
10
|
+
if (typeof document === "undefined" || typeof BroadcastChannel === "undefined") return null;
|
|
11
|
+
const key = document.querySelector<HTMLElement>("[data-preview-channel]")?.dataset.previewChannel;
|
|
12
|
+
return key ? new BroadcastChannel(previewChannelName(key)) : null;
|
|
13
|
+
}
|
|
@@ -186,11 +186,12 @@ const hasSidebarMeta = reverseRefs.length > 0 || hasDocMeta;
|
|
|
186
186
|
const hasSidebarColumn = !isSubmissionView && (hasSidebarFields || hasSidebarMeta);
|
|
187
187
|
|
|
188
188
|
const hasPreview = !!(mode === "edit" && (collection.preview || (collection.pathPrefix && doc?.slug)));
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
189
|
+
// Live-preview channel key: the preview tab only listens to this document's editor.
|
|
190
|
+
const previewKey = hasPreview && documentId ? `${collection.slug}:${documentId}` : null;
|
|
191
|
+
const previewUrl =
|
|
192
|
+
hasPreview && previewKey
|
|
193
|
+
? `${typeof collection.preview === "string" ? collection.preview : cms.meta.getRouteForDocument(collection.slug, doc)}?preview=${encodeURIComponent(previewKey)}`
|
|
194
|
+
: null;
|
|
194
195
|
|
|
195
196
|
const getCurrentValue = (fieldName: string) => (doc ? doc[fieldName] : undefined);
|
|
196
197
|
const getSharedValue = (fieldName: string) => (baseDoc ? baseDoc[fieldName] : undefined);
|
|
@@ -437,7 +438,7 @@ const publishBlocked =
|
|
|
437
438
|
(doc?._status === "draft" || doc?._status === "scheduled" || (doc && getVisualStatus(doc) === "changed")),
|
|
438
439
|
)}
|
|
439
440
|
/>
|
|
440
|
-
<div id="document-edit-area">
|
|
441
|
+
<div id="document-edit-area" data-preview-channel={!translationMode ? previewKey : undefined}>
|
|
441
442
|
{collection.slug === "users" && mode === "edit" && Astro.url.searchParams.get("inviteToken") && (
|
|
442
443
|
<AdminCard title={Astro.url.searchParams.get("emailSent") === "true" ? "Invitation sent" : "Invite link"}>
|
|
443
444
|
{Astro.url.searchParams.get("emailSent") === "true" ? (
|
|
@@ -200,7 +200,9 @@
|
|
|
200
200
|
}
|
|
201
201
|
if (u.searchParams.get("_toast") === "success") {
|
|
202
202
|
try {
|
|
203
|
-
|
|
203
|
+
var previewEl = document.querySelector("[data-preview-channel]");
|
|
204
|
+
var previewKey = previewEl && previewEl.getAttribute("data-preview-channel");
|
|
205
|
+
if (previewKey) new BroadcastChannel("cms-preview:" + previewKey).postMessage({ type: "reload" });
|
|
204
206
|
} catch (e) {}
|
|
205
207
|
}
|
|
206
208
|
if (window.parent !== window && u.searchParams.get("_toast") === "success") {
|
package/client/preview.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// `?preview=<collection>:<id>` (as linked from the edit view) scopes live updates to
|
|
2
|
+
// that document's editor. A bare `?preview` / `?preview=true` only shows drafts.
|
|
3
|
+
const previewKey = new URLSearchParams(location.search).get("preview");
|
|
4
|
+
if (previewKey && previewKey !== "true") {
|
|
5
|
+
const ch = new BroadcastChannel(`cms-preview:${previewKey}`);
|
|
3
6
|
let pending = 0;
|
|
4
7
|
|
|
5
8
|
ch.onmessage = (e: MessageEvent) => {
|
package/package.json
CHANGED
|
@@ -17,10 +17,7 @@
|
|
|
17
17
|
// [[d1_databases]] and [[r2_buckets]]. Catches a binding-name typo at typecheck time.
|
|
18
18
|
// IMAGES isn't in wrangler.toml: @astrojs/cloudflare adds it to every build (its
|
|
19
19
|
// `imagesBindingName` option), so it's optional here and images.ts copes without it.
|
|
20
|
-
export type CfEnv = { CMS_DB?: D1Database; CMS_ASSETS?: R2Bucket; IMAGES?: ImagesBinding } & Record<
|
|
21
|
-
string,
|
|
22
|
-
unknown
|
|
23
|
-
>;
|
|
20
|
+
export type CfEnv = { CMS_DB?: D1Database; CMS_ASSETS?: R2Bucket; IMAGES?: ImagesBinding } & Record<string, unknown>;
|
|
24
21
|
|
|
25
22
|
type CfProxy = { env: CfEnv; dispose: () => Promise<void> };
|
|
26
23
|
|