@kidecms/core 0.17.0 → 0.18.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.
@@ -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.postMessage({ field: name, value: serializeBlocks(blocks), render: "blocks" });
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 tiptapToContent = (json: any): ContentDocument => ({
70
- type: "root",
71
- children: (json.content ?? [])
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, selected } = props;
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
- className={cn("bg-muted/20 my-3 overflow-hidden rounded-lg border", selected && "border-ring bg-accent/40")}
177
- >
178
- <div className="bg-muted/40 flex items-center gap-2 px-3 py-2 select-none" contentEditable={false}>
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
- <button type="button" onClick={() => setExpanded((v) => !v)} className="group/row flex items-center gap-2">
189
- <ChevronRight
190
- className={cn(
191
- "text-muted-foreground group-hover/row:text-foreground/70 size-4 shrink-0 transition-[color,transform]",
192
- expanded && "rotate-90",
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
- {!expanded && preview && <span className="text-foreground/70 min-w-0 truncate text-sm">{preview}</span>}
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={() => (isShared ? detach() : openSaveDialog())}
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={() => deleteNode()}
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: [
@@ -34,15 +34,14 @@ export default function InternalLinkPicker({
34
34
  }, [editHref, linkOptions]);
35
35
 
36
36
  return (
37
- <div className="min-w-0 flex-3">
37
+ <div className="min-w-0 flex-1">
38
38
  <Popover open={open} onOpenChange={setOpen}>
39
39
  <PopoverTrigger asChild>
40
40
  <Button
41
41
  variant="input"
42
42
  role="combobox"
43
43
  aria-expanded={open}
44
- size="sm"
45
- className="bg-background w-full justify-between text-sm font-normal"
44
+ className="h-9 w-full min-w-0 justify-between rounded-lg px-3 text-sm font-normal"
46
45
  >
47
46
  <span className={cn("truncate", !selectedLabel && "text-muted-foreground")}>
48
47
  {selectedLabel || "Search documents..."}
@@ -8,10 +8,12 @@ import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectVa
8
8
  import InternalLinkPicker, { type LinkOptionGroup } from "./InternalLinkPicker";
9
9
 
10
10
  // A structured link control: URL + label + open-in-new-tab, stored as
11
- // { type, url, label, newTab }. A leading "/" is treated as an internal link.
12
- // When linkOptions are provided, internal links are chosen with a document
11
+ // { type, url, label, title, newTab }. A leading "/" is treated as an internal
12
+ // link. When linkOptions are provided, internal links are chosen with a document
13
13
  // picker instead of a hand-typed path (which silently breaks on slug edits).
14
- type LinkValue = { type?: string; url?: string; label?: string; newTab?: boolean };
14
+ // `title` is the picked document's title — renderers use it as the link text
15
+ // when `label` is left empty.
16
+ type LinkValue = { type?: string; url?: string; label?: string; title?: string; newTab?: boolean };
15
17
 
16
18
  type Props = {
17
19
  name?: string;
@@ -64,53 +66,59 @@ export default function LinkField({ name, value: initial, onChange, linkOptions
64
66
  return (
65
67
  <div className="space-y-2 rounded-md border p-3">
66
68
  {name && <input type="hidden" name={name} value={value.url ? JSON.stringify(value) : ""} ref={hiddenRef} />}
67
- <div className="grid gap-1">
68
- <Label className="text-xs">{hasPicker ? "Link" : "URL"}</Label>
69
- {hasPicker ? (
70
- <div className="flex items-center gap-2">
71
- <Select
72
- items={[
73
- { value: "internal", label: "Internal" },
74
- { value: "external", label: "External" },
75
- ]}
76
- value={linkType}
77
- onValueChange={(v) => setLinkType((v as "internal" | "external") ?? "internal")}
78
- >
79
- <SelectTrigger className="w-28 shrink-0 text-sm">
80
- <SelectValue />
81
- </SelectTrigger>
82
- <SelectContent>
83
- <SelectGroup>
84
- <SelectItem value="internal">Internal</SelectItem>
85
- <SelectItem value="external">External</SelectItem>
86
- </SelectGroup>
87
- </SelectContent>
88
- </Select>
89
- {linkType === "internal" ? (
90
- <InternalLinkPicker
91
- editHref={value.url ?? ""}
92
- linkOptions={linkOptions}
93
- onSelect={(item) => set({ url: item.href, ...(value.label ? {} : { label: item.label }) })}
94
- />
95
- ) : (
96
- <Input
97
- value={value.url ?? ""}
98
- placeholder="https://example.com"
99
- onChange={(e) => set({ url: e.target.value })}
100
- />
101
- )}
102
- </div>
103
- ) : (
69
+ <div className="grid gap-2 sm:grid-cols-[minmax(0,3fr)_minmax(0,2fr)]">
70
+ <div className="grid gap-1">
71
+ <Label className="text-xs">{hasPicker ? "Link" : "URL"}</Label>
72
+ {hasPicker ? (
73
+ <div className="flex min-w-0 items-center gap-2">
74
+ <Select
75
+ items={[
76
+ { value: "internal", label: "Internal" },
77
+ { value: "external", label: "External" },
78
+ ]}
79
+ value={linkType}
80
+ onValueChange={(v) => setLinkType((v as "internal" | "external") ?? "internal")}
81
+ >
82
+ <SelectTrigger className="w-28 shrink-0 text-sm">
83
+ <SelectValue />
84
+ </SelectTrigger>
85
+ <SelectContent>
86
+ <SelectGroup>
87
+ <SelectItem value="internal">Internal</SelectItem>
88
+ <SelectItem value="external">External</SelectItem>
89
+ </SelectGroup>
90
+ </SelectContent>
91
+ </Select>
92
+ {linkType === "internal" ? (
93
+ <InternalLinkPicker
94
+ editHref={value.url ?? ""}
95
+ linkOptions={linkOptions}
96
+ onSelect={(item) => set({ url: item.href, title: item.label })}
97
+ />
98
+ ) : (
99
+ <Input
100
+ value={value.url ?? ""}
101
+ placeholder="https://example.com"
102
+ onChange={(e) => set({ url: e.target.value, title: undefined })}
103
+ />
104
+ )}
105
+ </div>
106
+ ) : (
107
+ <Input
108
+ value={value.url ?? ""}
109
+ placeholder="https://example.com or /about"
110
+ onChange={(e) => set({ url: e.target.value, title: undefined })}
111
+ />
112
+ )}
113
+ </div>
114
+ <div className="grid gap-1">
115
+ <Label className="text-xs">Label</Label>
104
116
  <Input
105
- value={value.url ?? ""}
106
- placeholder="https://example.com or /about"
107
- onChange={(e) => set({ url: e.target.value })}
117
+ value={value.label ?? ""}
118
+ placeholder={value.title || "Link text"}
119
+ onChange={(e) => set({ label: e.target.value })}
108
120
  />
109
- )}
110
- </div>
111
- <div className="grid gap-1">
112
- <Label className="text-xs">Label</Label>
113
- <Input value={value.label ?? ""} placeholder="Link text" onChange={(e) => set({ label: e.target.value })} />
121
+ </div>
114
122
  </div>
115
123
  <label className="text-muted-foreground group inline-flex cursor-pointer items-center gap-2 text-sm">
116
124
  <Checkbox
@@ -119,7 +127,6 @@ export default function LinkField({ name, value: initial, onChange, linkOptions
119
127
  onCheckedChange={(checked) => set({ newTab: Boolean(checked) })}
120
128
  />
121
129
  Open in new tab
122
- {value.url && <span className="ml-auto text-xs">({value.type})</span>}
123
130
  </label>
124
131
  </div>
125
132
  );
@@ -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-accent data-[selected=true]:text-accent-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",
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}
@@ -1,5 +1,5 @@
1
1
  ---
2
- import { ArrowLeft, ArrowUpRight, Dot, Lock } from "lucide-react";
2
+ import { Archive, ArrowLeft, ArrowUpRight, Dot, Lock } from "lucide-react";
3
3
 
4
4
  import AdminCard from "../components/AdminCard.astro";
5
5
  import AiGenerateButton from "../components/AiGenerateButton";
@@ -122,6 +122,12 @@ const formTab = "edit" as "edit" | "submissions";
122
122
 
123
123
  // --- Form-submissions-specific: custom read-only view ---
124
124
  const isSubmissionView = collection.slug === "form-submissions" && mode === "edit";
125
+ // Opening a new submission marks it read, like email. System write: the
126
+ // read-only `data` field's access rule must not block the status flip.
127
+ if (isSubmissionView && doc && documentId && doc.status === "new") {
128
+ await collectionApi.update(documentId, { status: "read" }, { _system: true });
129
+ doc.status = "read";
130
+ }
125
131
  const parentFormTitle =
126
132
  isSubmissionView && doc?.form ? await loadParentFormTitle(String(doc.form), cmsRuntime, runtimeContext) : null;
127
133
  const submissionData = (isSubmissionView && doc?.data ? doc.data : {}) as Record<string, unknown>;
@@ -284,6 +290,16 @@ const publishBlocked =
284
290
  >
285
291
  Save translation
286
292
  </button>
293
+ ) : isSubmissionView && documentId ? (
294
+ <form method="post" action={getFormAction(collection.slug, documentId)}>
295
+ <input type="hidden" name="_action" value="update" />
296
+ <input type="hidden" name="status" value={doc?.status === "archived" ? "read" : "archived"} />
297
+ <input type="hidden" name="redirectTo" value={`/admin/${collection.slug}`} />
298
+ <button class={buttonVariants({ variant: "outline" })} type="submit">
299
+ <Archive className="size-3.5" />
300
+ {doc?.status === "archived" ? "Unarchive" : "Archive"}
301
+ </button>
302
+ </form>
287
303
  ) : (
288
304
  <>
289
305
  <button
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 ---
@@ -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
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kidecms/core",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Kide CMS runtime — core library, admin UI, routes, and middleware for Astro. Shipped as TypeScript source.",
5
5
  "keywords": [
6
6
  "cms",