@kidecms/core 0.17.0 → 0.17.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 +8 -2
- package/admin/components/ContentEditor.tsx +64 -32
- package/admin/components/RichTextEditor.tsx +11 -0
- package/admin/components/UnsavedGuard.tsx +12 -0
- package/admin/components/ui/command.tsx +1 -1
- package/client/preview.ts +5 -0
- package/internals/mcp-worker.ts +8 -1
- package/package.json +1 -1
|
@@ -302,10 +302,16 @@ export default function BlockEditor({
|
|
|
302
302
|
}
|
|
303
303
|
}, []);
|
|
304
304
|
|
|
305
|
-
// Live preview: broadcast block data for server-side rendering
|
|
305
|
+
// Live preview: broadcast block data for server-side rendering. Also replay the
|
|
306
|
+
// current value when a preview tab opened after edits announces itself.
|
|
306
307
|
useEffect(() => {
|
|
307
308
|
if (!previewChannelRef.current) previewChannelRef.current = new BroadcastChannel("cms-preview");
|
|
308
|
-
previewChannelRef.current
|
|
309
|
+
const channel = previewChannelRef.current;
|
|
310
|
+
const broadcast = () => channel.postMessage({ field: name, value: serializeBlocks(blocks), render: "blocks" });
|
|
311
|
+
broadcast();
|
|
312
|
+
channel.onmessage = (e: MessageEvent) => {
|
|
313
|
+
if (e.data?.type === "preview-ready") broadcast();
|
|
314
|
+
};
|
|
309
315
|
}, [blocks, name]);
|
|
310
316
|
|
|
311
317
|
const updateBlocks = useCallback(
|
|
@@ -66,16 +66,22 @@ const contentToTiptap = (doc: ContentDocument | null | undefined): any => {
|
|
|
66
66
|
return { type: "doc", content: content.length > 0 ? content : [{ type: "paragraph" }] };
|
|
67
67
|
};
|
|
68
68
|
|
|
69
|
-
const
|
|
70
|
-
type
|
|
71
|
-
|
|
69
|
+
const isEmptyParagraph = (node: ContentNode | undefined) =>
|
|
70
|
+
node?.type === "paragraph" && !(node as { children?: unknown[] }).children?.length;
|
|
71
|
+
|
|
72
|
+
const tiptapToContent = (json: any): ContentDocument => {
|
|
73
|
+
const children: ContentNode[] = (json.content ?? [])
|
|
72
74
|
.map((node: any): ContentNode | null =>
|
|
73
75
|
node.type === BLOCK_NODE_NAME
|
|
74
76
|
? { type: "block", blockType: node.attrs?.blockType ?? "", fields: node.attrs?.fields ?? {} }
|
|
75
77
|
: (tiptapNodeToCms(node) as ContentNode | null),
|
|
76
78
|
)
|
|
77
|
-
.filter(Boolean)
|
|
78
|
-
|
|
79
|
+
.filter(Boolean);
|
|
80
|
+
// The editor keeps a paragraph after a trailing block so the cursor can get
|
|
81
|
+
// there — don't store it; it would render as visible empty space.
|
|
82
|
+
while (isEmptyParagraph(children[children.length - 1])) children.pop();
|
|
83
|
+
return { type: "root", children };
|
|
84
|
+
};
|
|
79
85
|
|
|
80
86
|
// -----------------------------------------------
|
|
81
87
|
// Inline block node view
|
|
@@ -87,7 +93,7 @@ function isBlankFieldValue(value: unknown): boolean {
|
|
|
87
93
|
}
|
|
88
94
|
|
|
89
95
|
function BlockNodeView(props: NodeViewProps) {
|
|
90
|
-
const { node, updateAttributes, deleteNode, extension
|
|
96
|
+
const { node, updateAttributes, deleteNode, extension } = props;
|
|
91
97
|
const options = extension.options as BlockNodeOptions;
|
|
92
98
|
const blockType = String(node.attrs.blockType ?? "");
|
|
93
99
|
const fields = (node.attrs.fields ?? {}) as Record<string, unknown>;
|
|
@@ -172,40 +178,48 @@ function BlockNodeView(props: NodeViewProps) {
|
|
|
172
178
|
})();
|
|
173
179
|
|
|
174
180
|
return (
|
|
175
|
-
<NodeViewWrapper
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
181
|
+
<NodeViewWrapper className="bg-card my-3 overflow-hidden rounded-lg border">
|
|
182
|
+
{/* Header — entire row is clickable to expand/collapse, same as BlockEditor */}
|
|
183
|
+
<div
|
|
184
|
+
className="group/row bg-muted-strong/50 hover:bg-muted-strong/70 flex items-center gap-2 px-3 py-2 transition-colors select-none"
|
|
185
|
+
contentEditable={false}
|
|
186
|
+
onClick={() => setExpanded((v) => !v)}
|
|
187
|
+
>
|
|
179
188
|
<span
|
|
180
189
|
data-drag-handle
|
|
181
190
|
draggable="true"
|
|
182
191
|
className="text-muted-foreground/50 hover:text-muted-foreground -ml-1 cursor-grab rounded p-1 transition-colors active:cursor-grabbing"
|
|
183
192
|
title="Drag to reorder"
|
|
193
|
+
onClick={(e) => e.stopPropagation()}
|
|
184
194
|
>
|
|
185
195
|
<GripVertical className="size-4" />
|
|
186
196
|
</span>
|
|
187
197
|
|
|
188
|
-
<
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
/>
|
|
195
|
-
<span
|
|
196
|
-
className={cn(
|
|
197
|
-
"rounded px-2 py-0.5 text-xs font-medium",
|
|
198
|
-
isShared
|
|
199
|
-
? "bg-primary/10 text-primary border-primary/20 border"
|
|
200
|
-
: "bg-secondary text-secondary-foreground",
|
|
201
|
-
)}
|
|
202
|
-
>
|
|
203
|
-
{isShared ? "Shared" : humanize(blockType)}
|
|
204
|
-
</span>
|
|
205
|
-
{isShared && sharedType && <span className="text-foreground/70 text-xs">{humanize(sharedType)}</span>}
|
|
206
|
-
</button>
|
|
198
|
+
<ChevronRight
|
|
199
|
+
className={cn(
|
|
200
|
+
"text-muted-foreground group-hover/row:text-foreground/70 size-4 shrink-0 transition-[color,transform]",
|
|
201
|
+
expanded && "rotate-90",
|
|
202
|
+
)}
|
|
203
|
+
/>
|
|
207
204
|
|
|
208
|
-
|
|
205
|
+
<span
|
|
206
|
+
className={cn(
|
|
207
|
+
"rounded px-2 py-0.5 text-xs font-medium",
|
|
208
|
+
isShared
|
|
209
|
+
? "bg-primary/10 text-primary border-primary/20 border"
|
|
210
|
+
: "bg-card text-secondary-foreground border",
|
|
211
|
+
)}
|
|
212
|
+
>
|
|
213
|
+
{isShared ? "Shared" : humanize(blockType)}
|
|
214
|
+
</span>
|
|
215
|
+
|
|
216
|
+
{isShared && sharedType && <span className="text-foreground/70 text-xs">{humanize(sharedType)}</span>}
|
|
217
|
+
|
|
218
|
+
{!expanded && preview && (
|
|
219
|
+
<span className="text-foreground/70 group-hover/row:text-foreground/85 min-w-0 truncate text-sm transition-colors">
|
|
220
|
+
{preview}
|
|
221
|
+
</span>
|
|
222
|
+
)}
|
|
209
223
|
|
|
210
224
|
<div className="ml-auto flex shrink-0 items-center">
|
|
211
225
|
<Button
|
|
@@ -214,7 +228,11 @@ function BlockNodeView(props: NodeViewProps) {
|
|
|
214
228
|
size="icon"
|
|
215
229
|
title={isShared ? "Detach shared section" : "Save as shared section"}
|
|
216
230
|
className="text-muted-foreground hover:text-foreground size-7"
|
|
217
|
-
onClick={() =>
|
|
231
|
+
onClick={(e) => {
|
|
232
|
+
e.stopPropagation();
|
|
233
|
+
if (isShared) detach();
|
|
234
|
+
else openSaveDialog();
|
|
235
|
+
}}
|
|
218
236
|
>
|
|
219
237
|
{isShared ? <Unlink className="size-3.5" /> : <Save className="size-3.5" />}
|
|
220
238
|
</Button>
|
|
@@ -224,7 +242,10 @@ function BlockNodeView(props: NodeViewProps) {
|
|
|
224
242
|
size="icon"
|
|
225
243
|
title="Remove block"
|
|
226
244
|
className="text-muted-foreground hover:text-destructive size-7"
|
|
227
|
-
onClick={() =>
|
|
245
|
+
onClick={(e) => {
|
|
246
|
+
e.stopPropagation();
|
|
247
|
+
deleteNode();
|
|
248
|
+
}}
|
|
228
249
|
>
|
|
229
250
|
<Trash2 className="size-3.5" />
|
|
230
251
|
</Button>
|
|
@@ -422,6 +443,17 @@ export default function ContentEditor({
|
|
|
422
443
|
}
|
|
423
444
|
}, [cmsJson]);
|
|
424
445
|
|
|
446
|
+
// Replay the current value when a preview tab opened after edits announces itself
|
|
447
|
+
useEffect(() => {
|
|
448
|
+
if (!previewChannelRef.current) previewChannelRef.current = new BroadcastChannel("cms-preview");
|
|
449
|
+
const channel = previewChannelRef.current;
|
|
450
|
+
channel.onmessage = (e: MessageEvent) => {
|
|
451
|
+
if (e.data?.type === "preview-ready") {
|
|
452
|
+
channel.postMessage({ field: name, value: cmsJson, render: "content" });
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
}, [cmsJson, name]);
|
|
456
|
+
|
|
425
457
|
const editor = useEditor({
|
|
426
458
|
immediatelyRender: false,
|
|
427
459
|
extensions: [
|
|
@@ -432,6 +432,17 @@ export default function RichTextEditor({ name, initialValue, rows = 10, onChange
|
|
|
432
432
|
}
|
|
433
433
|
}, [cmsJson]);
|
|
434
434
|
|
|
435
|
+
// Replay the current value when a preview tab opened after edits announces itself
|
|
436
|
+
useEffect(() => {
|
|
437
|
+
if (!previewChannelRef.current) previewChannelRef.current = new BroadcastChannel("cms-preview");
|
|
438
|
+
const channel = previewChannelRef.current;
|
|
439
|
+
channel.onmessage = (e: MessageEvent) => {
|
|
440
|
+
if (e.data?.type === "preview-ready") {
|
|
441
|
+
channel.postMessage({ field: name, value: cmsJson, render: "richText" });
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
}, [cmsJson, name]);
|
|
445
|
+
|
|
435
446
|
const editor = useEditor({
|
|
436
447
|
immediatelyRender: false,
|
|
437
448
|
extensions: [
|
|
@@ -77,6 +77,18 @@ export default function UnsavedGuard({
|
|
|
77
77
|
previewChannel.postMessage({ field: target.name, value: target.value });
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
+
// A preview tab opened after edits were made announces itself; replay the
|
|
81
|
+
// current value of every simple field so it doesn't miss unsaved changes.
|
|
82
|
+
previewChannel.onmessage = (e: MessageEvent) => {
|
|
83
|
+
if (e.data?.type !== "preview-ready") return;
|
|
84
|
+
for (const el of Array.from(form.elements)) {
|
|
85
|
+
const input = el as HTMLInputElement;
|
|
86
|
+
if (!input.name || input.name.startsWith("_") || input.name === "redirectTo") continue;
|
|
87
|
+
if (input.type === "hidden" || input.type === "submit" || input.type === "button") continue;
|
|
88
|
+
previewChannel.postMessage({ field: input.name, value: input.value });
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
80
92
|
form.addEventListener("input", checkDirty);
|
|
81
93
|
form.addEventListener("input", broadcastField);
|
|
82
94
|
form.addEventListener("change", checkDirty);
|
|
@@ -77,7 +77,7 @@ function CommandItem({ className, ...props }: React.ComponentProps<typeof Comman
|
|
|
77
77
|
<CommandPrimitive.Item
|
|
78
78
|
data-slot="command-item"
|
|
79
79
|
className={cn(
|
|
80
|
-
"data-[selected=true]:bg-
|
|
80
|
+
"data-[selected=true]:bg-muted-strong/70 data-[selected=true]:text-foreground relative flex cursor-default items-center gap-2 rounded-sm py-1.5 text-sm outline-hidden select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
|
|
81
81
|
className,
|
|
82
82
|
)}
|
|
83
83
|
{...props}
|
package/client/preview.ts
CHANGED
|
@@ -9,6 +9,7 @@ if (new URLSearchParams(location.search).has("preview")) {
|
|
|
9
9
|
location.reload();
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
12
|
+
if (!d.field) return;
|
|
12
13
|
|
|
13
14
|
const els = document.querySelectorAll<HTMLElement>(`[data-cms="${d.field}"]`);
|
|
14
15
|
if (!els.length) return;
|
|
@@ -32,6 +33,10 @@ if (new URLSearchParams(location.search).has("preview")) {
|
|
|
32
33
|
els.forEach((el) => (el.textContent = d.value));
|
|
33
34
|
}
|
|
34
35
|
};
|
|
36
|
+
|
|
37
|
+
// Ask any open admin tab to replay its current (possibly unsaved) field values,
|
|
38
|
+
// so a preview opened after edits were made still shows them.
|
|
39
|
+
ch.postMessage({ type: "preview-ready" });
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
// --- Edit bar: "Edit this page" chip on public pages for logged-in editors ---
|
package/internals/mcp-worker.ts
CHANGED
|
@@ -174,6 +174,13 @@ const ops: Record<string, (args: OpArgs) => unknown> = {
|
|
|
174
174
|
},
|
|
175
175
|
};
|
|
176
176
|
|
|
177
|
+
// A missing cms_* table means the config declares a collection the database
|
|
178
|
+
// doesn't have yet — the one sync step MCP can't do for the agent. Say so.
|
|
179
|
+
const withSchemaHint = (message: string) =>
|
|
180
|
+
/no such table: cms_/i.test(message)
|
|
181
|
+
? `${message}. The database schema is out of sync with the CMS config — run "pnpm cms:push" (or start the dev server, which pushes on boot), then retry.`
|
|
182
|
+
: message;
|
|
183
|
+
|
|
177
184
|
process.on("message", (message: { id: number; op: string; args?: OpArgs }) => {
|
|
178
185
|
void (async () => {
|
|
179
186
|
try {
|
|
@@ -185,7 +192,7 @@ process.on("message", (message: { id: number; op: string; args?: OpArgs }) => {
|
|
|
185
192
|
sendToParent({
|
|
186
193
|
id: message.id,
|
|
187
194
|
ok: false,
|
|
188
|
-
error: error instanceof Error ? error.message : String(error),
|
|
195
|
+
error: withSchemaHint(error instanceof Error ? error.message : String(error)),
|
|
189
196
|
});
|
|
190
197
|
}
|
|
191
198
|
})();
|