@kidecms/core 0.1.11 → 0.14.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.
- package/LICENSE +21 -0
- package/admin/components/AssetsGrid.tsx +366 -173
- package/admin/components/BlockEditor.tsx +260 -679
- package/admin/components/CheckboxField.tsx +2 -2
- package/admin/components/CollaborationReviewBar.tsx +384 -0
- package/admin/components/ColorField.tsx +87 -0
- package/admin/components/CommandPalette.tsx +169 -0
- package/admin/components/ContentEditor.tsx +687 -0
- package/admin/components/DocumentActions.tsx +4 -2
- package/admin/components/DocumentsDataTable.tsx +91 -10
- package/admin/components/FieldControl.astro +151 -26
- package/admin/components/FocalPointSelector.tsx +27 -1
- package/admin/components/InternalLinkPicker.tsx +4 -3
- package/admin/components/LinkField.tsx +126 -0
- package/admin/components/RelationField.tsx +105 -26
- package/admin/components/RepeaterInput.tsx +46 -0
- package/admin/components/RichTextEditor.tsx +44 -9
- package/admin/components/SharedSectionBlockField.tsx +93 -0
- package/admin/components/SidebarUserMenu.tsx +1 -1
- package/admin/components/Toast.astro +1 -1
- package/admin/components/TreeItemsEditor.tsx +20 -19
- package/admin/components/TreeSelect.tsx +2 -2
- package/admin/components/YoutubeField.tsx +48 -0
- package/admin/components/block-fields.tsx +777 -0
- package/admin/components/content-block-spec.ts +114 -0
- package/admin/components/slash-command.tsx +242 -0
- package/admin/components/tree-utils.ts +14 -2
- package/admin/components/ui/badge.tsx +2 -3
- package/admin/components/ui/button.tsx +5 -4
- package/admin/components/ui/checkbox.tsx +1 -1
- package/admin/components/ui/input.tsx +1 -1
- package/admin/components/ui/select.tsx +1 -1
- package/admin/components/ui/table.tsx +1 -1
- package/admin/components/ui/textarea.tsx +1 -1
- package/admin/layouts/AdminLayout.astro +323 -123
- package/admin/lib/access.ts +54 -0
- package/admin/lib/collaboration.ts +178 -0
- package/admin/lib/edit-data.ts +448 -0
- package/admin/views/AccessDeniedView.astro +8 -0
- package/admin/views/EditView.astro +706 -0
- package/admin/views/ListView.astro +202 -0
- package/admin/views/RecentView.astro +76 -0
- package/admin/views/SinglesView.astro +65 -0
- package/admin/views/edit/EditScripts.astro +145 -0
- package/admin/views/edit/ReverseReferencesList.astro +68 -0
- package/core/admin.ts +193 -0
- package/core/ai.ts +87 -0
- package/core/api.ts +1392 -0
- package/core/assets.ts +336 -0
- package/core/audit.ts +86 -0
- package/core/auth-config.ts +44 -0
- package/core/auth.ts +303 -0
- package/core/collaboration.ts +276 -0
- package/core/content.ts +27 -0
- package/core/create-admin.ts +28 -0
- package/core/define.ts +623 -0
- package/core/field-model.ts +172 -0
- package/core/generator.ts +519 -0
- package/core/http.ts +42 -0
- package/core/image.ts +219 -0
- package/core/index.ts +211 -0
- package/core/locks.ts +52 -0
- package/core/log.ts +37 -0
- package/core/migrate.ts +278 -0
- package/core/rate-limit.ts +110 -0
- package/core/request-scope.ts +33 -0
- package/core/richtext.ts +1 -0
- package/core/runtime.ts +63 -0
- package/core/schema.ts +32 -0
- package/core/search.ts +489 -0
- package/core/seed.ts +101 -0
- package/core/shared-sections.ts +96 -0
- package/core/tasks.ts +176 -0
- package/core/values.ts +433 -0
- package/core/webhooks.ts +85 -0
- package/core/youtube.ts +39 -0
- package/internals/cli.mjs +34 -0
- package/internals/context.ts +67 -0
- package/internals/create-admin.ts +40 -0
- package/internals/describe.ts +25 -0
- package/internals/eject.ts +144 -0
- package/internals/generator.ts +11 -0
- package/internals/integration.ts +481 -0
- package/internals/mcp.ts +397 -0
- package/internals/project.ts +31 -0
- package/internals/push.ts +160 -0
- package/internals/reindex.ts +11 -0
- package/internals/restore.ts +257 -0
- package/internals/seed.ts +15 -0
- package/internals/upgrade.ts +785 -0
- package/{virtual.d.ts → internals/virtual.d.ts} +39 -5
- package/middleware/auth.ts +130 -17
- package/middleware/push.ts +121 -0
- package/package.json +55 -67
- package/platform/cloudflare/cf-env.ts +60 -0
- package/platform/cloudflare/database.ts +28 -0
- package/platform/cloudflare/globals.d.ts +5 -0
- package/platform/cloudflare/storage.ts +33 -0
- package/platform/cloudflare/uploads-route.ts +37 -0
- package/platform/node/database.ts +39 -0
- package/platform/node/storage.ts +52 -0
- package/routes/admin/[...path].astro +71 -0
- package/routes/{pages/admin → admin}/assets/[id].astro +25 -19
- package/routes/admin/assets/index.astro +90 -0
- package/routes/{pages/admin/login.astro → admin/forgot-password.astro} +21 -19
- package/routes/{pages/admin → admin}/invite.astro +13 -6
- package/routes/admin/login.astro +103 -0
- package/routes/admin/reset-password.astro +73 -0
- package/routes/{pages/admin → admin}/setup.astro +32 -8
- package/routes/api/{cms/[collection] → [collection]}/[...path].ts +67 -15
- package/routes/api/admin/search.ts +89 -0
- package/routes/api/{cms/assets → assets}/[id].ts +24 -14
- package/routes/api/{cms/assets → assets}/index.ts +4 -2
- package/routes/api/assets/upload.ts +147 -0
- package/routes/api/auth/forgot-password.ts +77 -0
- package/routes/api/{cms/auth → auth}/invite.ts +41 -9
- package/routes/api/auth/login.ts +167 -0
- package/routes/api/{cms/auth → auth}/logout.ts +11 -2
- package/routes/api/auth/reset-password.ts +85 -0
- package/routes/api/auth/setup.ts +110 -0
- package/routes/api/auth/sso/[provider]/start.ts +34 -0
- package/routes/api/collaboration/index.ts +106 -0
- package/routes/api/cron/_authorize.ts +25 -0
- package/routes/api/{cms/cron → cron}/publish.ts +3 -9
- package/routes/api/cron/tasks.ts +34 -0
- package/routes/api/forms/submit/[slug].ts +143 -0
- package/routes/api/img/[...path].ts +65 -0
- package/routes/api/{cms/preview → preview}/render.ts +8 -3
- package/routes/api/{cms/references → references}/[collection]/[id].ts +31 -1
- package/routes/api/webhooks/[provider].ts +54 -0
- package/README.md +0 -28
- package/dist/admin.d.ts +0 -33
- package/dist/admin.js +0 -92
- package/dist/ai.d.ts +0 -15
- package/dist/ai.js +0 -70
- package/dist/api.d.ts +0 -14
- package/dist/api.js +0 -833
- package/dist/assets.d.ts +0 -51
- package/dist/assets.js +0 -163
- package/dist/auth.d.ts +0 -30
- package/dist/auth.js +0 -132
- package/dist/content.d.ts +0 -3
- package/dist/content.js +0 -29
- package/dist/create-admin.d.ts +0 -5
- package/dist/create-admin.js +0 -23
- package/dist/define.d.ts +0 -254
- package/dist/define.js +0 -36
- package/dist/generator.d.ts +0 -9
- package/dist/generator.js +0 -370
- package/dist/image.d.ts +0 -9
- package/dist/image.js +0 -69
- package/dist/index.d.ts +0 -28
- package/dist/index.js +0 -16
- package/dist/integration.d.ts +0 -14
- package/dist/integration.js +0 -327
- package/dist/locks.d.ts +0 -7
- package/dist/locks.js +0 -37
- package/dist/richtext.d.ts +0 -1
- package/dist/richtext.js +0 -1
- package/dist/runtime.d.ts +0 -24
- package/dist/runtime.js +0 -26
- package/dist/schema.d.ts +0 -17
- package/dist/schema.js +0 -13
- package/dist/seed.d.ts +0 -2
- package/dist/seed.js +0 -84
- package/dist/values.d.ts +0 -8
- package/dist/values.js +0 -102
- package/dist/webhooks.d.ts +0 -2
- package/dist/webhooks.js +0 -73
- package/routes/api/cms/assets/upload.ts +0 -112
- package/routes/api/cms/auth/login.ts +0 -124
- package/routes/api/cms/auth/setup.ts +0 -77
- package/routes/api/cms/img/[...path].ts +0 -24
- package/routes/pages/admin/[...path].astro +0 -1107
- package/routes/pages/admin/assets/index.astro +0 -63
- /package/routes/api/{cms/ai → ai}/alt-text.ts +0 -0
- /package/routes/api/{cms/ai → ai}/seo.ts +0 -0
- /package/routes/api/{cms/ai → ai}/translate.ts +0 -0
- /package/routes/api/{cms/assets → assets}/folders.ts +0 -0
- /package/routes/api/{cms/locks → locks}/[...path].ts +0 -0
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
import { useEditor, EditorContent, NodeViewWrapper, ReactNodeViewRenderer, type NodeViewProps } from "@tiptap/react";
|
|
2
|
+
import { BubbleMenu } from "@tiptap/react/menus";
|
|
3
|
+
import { Node } from "@tiptap/core";
|
|
4
|
+
import StarterKit from "@tiptap/starter-kit";
|
|
5
|
+
import Image from "@tiptap/extension-image";
|
|
6
|
+
import Link from "@tiptap/extension-link";
|
|
7
|
+
import Placeholder from "@tiptap/extension-placeholder";
|
|
8
|
+
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
|
9
|
+
import {
|
|
10
|
+
ArrowUpRight,
|
|
11
|
+
Bold,
|
|
12
|
+
ChevronRight,
|
|
13
|
+
GripVertical,
|
|
14
|
+
Italic,
|
|
15
|
+
Heading2,
|
|
16
|
+
Heading3,
|
|
17
|
+
Link as LinkIcon,
|
|
18
|
+
Maximize2,
|
|
19
|
+
Minimize2,
|
|
20
|
+
Save,
|
|
21
|
+
Trash2,
|
|
22
|
+
Unlink,
|
|
23
|
+
} from "lucide-react";
|
|
24
|
+
import { Button, buttonVariants } from "./ui/button";
|
|
25
|
+
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "./ui/dialog";
|
|
26
|
+
import { Input } from "./ui/input";
|
|
27
|
+
import { cn } from "../lib/utils";
|
|
28
|
+
import {
|
|
29
|
+
LinkDialog,
|
|
30
|
+
fetchLinkGroups,
|
|
31
|
+
cmsNodeToTiptap,
|
|
32
|
+
tiptapNodeToCms,
|
|
33
|
+
type CmsNode,
|
|
34
|
+
type LinkGroup,
|
|
35
|
+
} from "./RichTextEditor";
|
|
36
|
+
import { SubField, humanize, type BlockTypesMeta, type RelationOption, type SubFieldMeta } from "./block-fields";
|
|
37
|
+
import type { LinkOptionGroup } from "./InternalLinkPicker";
|
|
38
|
+
import { blockNodeSpec, BLOCK_NODE_NAME, SHARED_BLOCK_TYPE, type BlockNodeOptions } from "./content-block-spec";
|
|
39
|
+
import { SlashCommand, type SharedSectionOption } from "./slash-command";
|
|
40
|
+
|
|
41
|
+
// -----------------------------------------------
|
|
42
|
+
// Content AST ↔ Tiptap JSON conversion
|
|
43
|
+
//
|
|
44
|
+
// A `content` document is a rich-text document whose children may also be inline
|
|
45
|
+
// component blocks: { type: "block", blockType, fields }. Prose nodes reuse the
|
|
46
|
+
// RichTextEditor converters; block nodes map to a custom Tiptap node.
|
|
47
|
+
// -----------------------------------------------
|
|
48
|
+
|
|
49
|
+
type ContentBlock = { type: "block"; blockType: string; fields: Record<string, unknown>; _key?: string };
|
|
50
|
+
type ContentNode = CmsNode | ContentBlock;
|
|
51
|
+
type ContentDocument = { type: "root"; children: ContentNode[] };
|
|
52
|
+
|
|
53
|
+
const isBlock = (node: ContentNode): node is ContentBlock => node.type === "block";
|
|
54
|
+
|
|
55
|
+
const contentToTiptap = (doc: ContentDocument | null | undefined): any => {
|
|
56
|
+
if (!doc || doc.type !== "root") return { type: "doc", content: [{ type: "paragraph" }] };
|
|
57
|
+
// CMS stores inline blocks as `{ type: "block", ... }`; the Tiptap node is named
|
|
58
|
+
// BLOCK_NODE_NAME (NOT "block" — that collides with ProseMirror's content group).
|
|
59
|
+
const content = doc.children
|
|
60
|
+
.map((node) =>
|
|
61
|
+
isBlock(node)
|
|
62
|
+
? { type: BLOCK_NODE_NAME, attrs: { blockType: node.blockType, fields: node.fields ?? {} } }
|
|
63
|
+
: cmsNodeToTiptap(node as CmsNode),
|
|
64
|
+
)
|
|
65
|
+
.filter(Boolean);
|
|
66
|
+
return { type: "doc", content: content.length > 0 ? content : [{ type: "paragraph" }] };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const tiptapToContent = (json: any): ContentDocument => ({
|
|
70
|
+
type: "root",
|
|
71
|
+
children: (json.content ?? [])
|
|
72
|
+
.map((node: any): ContentNode | null =>
|
|
73
|
+
node.type === BLOCK_NODE_NAME
|
|
74
|
+
? { type: "block", blockType: node.attrs?.blockType ?? "", fields: node.attrs?.fields ?? {} }
|
|
75
|
+
: (tiptapNodeToCms(node) as ContentNode | null),
|
|
76
|
+
)
|
|
77
|
+
.filter(Boolean),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// -----------------------------------------------
|
|
81
|
+
// Inline block node view
|
|
82
|
+
// -----------------------------------------------
|
|
83
|
+
|
|
84
|
+
/** A block field counts as blank when it's falsy or an empty array/object. */
|
|
85
|
+
function isBlankFieldValue(value: unknown): boolean {
|
|
86
|
+
return !value || (typeof value === "object" && Object.keys(value as object).length === 0);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function BlockNodeView(props: NodeViewProps) {
|
|
90
|
+
const { node, updateAttributes, deleteNode, extension, selected } = props;
|
|
91
|
+
const options = extension.options as BlockNodeOptions;
|
|
92
|
+
const blockType = String(node.attrs.blockType ?? "");
|
|
93
|
+
const fields = (node.attrs.fields ?? {}) as Record<string, unknown>;
|
|
94
|
+
const fieldsMeta: Record<string, SubFieldMeta> = options.types[blockType] ?? {};
|
|
95
|
+
// A freshly-inserted block (every field still blank) starts expanded so it can be
|
|
96
|
+
// filled in; a block loaded with content starts collapsed. Treat empty arrays/objects
|
|
97
|
+
// as blank too — e.g. an image block defaults `images` to `[]`, which is truthy and
|
|
98
|
+
// would otherwise wrongly start the block collapsed.
|
|
99
|
+
const [expanded, setExpanded] = useState(() => Object.values(fields).every(isBlankFieldValue));
|
|
100
|
+
const [saveDialogOpen, setSaveDialogOpen] = useState(false);
|
|
101
|
+
const [sharedName, setSharedName] = useState("");
|
|
102
|
+
// Stable per-node id so sub-field DOM ids/names stay unique across repeated blocks of
|
|
103
|
+
// the same type (two FAQ blocks must not share input ids/labels).
|
|
104
|
+
const fieldIdPrefix = useId();
|
|
105
|
+
|
|
106
|
+
const updateField = (fieldName: string, value: unknown) => {
|
|
107
|
+
updateAttributes({ fields: { ...fields, [fieldName]: value } });
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const getRelationOptions = (fieldName: string): RelationOption[] =>
|
|
111
|
+
options.blockRelationOptions[`block:${options.fieldName}:${blockType}:${fieldName}`] ?? [];
|
|
112
|
+
|
|
113
|
+
const isShared = blockType === SHARED_BLOCK_TYPE;
|
|
114
|
+
const sharedTitle = String(fields.title ?? "Shared section");
|
|
115
|
+
const sharedRef = String(fields.ref ?? "");
|
|
116
|
+
const sharedType = String(fields.blockType ?? "");
|
|
117
|
+
|
|
118
|
+
// Save this block as a reusable shared section, then turn it into a reference.
|
|
119
|
+
const openSaveDialog = () => {
|
|
120
|
+
setSharedName(preview || humanize(blockType));
|
|
121
|
+
setSaveDialogOpen(true);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const saveAsShared = async () => {
|
|
125
|
+
const title = sharedName.trim();
|
|
126
|
+
if (!title) return;
|
|
127
|
+
setSaveDialogOpen(false);
|
|
128
|
+
const res = await fetch("/api/cms/shared-sections", {
|
|
129
|
+
method: "POST",
|
|
130
|
+
credentials: "same-origin",
|
|
131
|
+
headers: { "Content-Type": "application/json" },
|
|
132
|
+
body: JSON.stringify({ title, blockType, block: { type: blockType, ...fields }, _status: "published" }),
|
|
133
|
+
});
|
|
134
|
+
if (!res.ok) {
|
|
135
|
+
window.alert("Could not create the shared section.");
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const created = (await res.json()) as Record<string, unknown>;
|
|
139
|
+
updateAttributes({
|
|
140
|
+
blockType: SHARED_BLOCK_TYPE,
|
|
141
|
+
fields: { ref: String(created._id), title: String(created.title ?? title), blockType },
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// Replace a shared reference with a local, editable copy of the source block.
|
|
146
|
+
const detach = async () => {
|
|
147
|
+
if (!sharedRef) return;
|
|
148
|
+
const res = await fetch(`/api/cms/shared-sections/${sharedRef}?status=any`, { credentials: "same-origin" });
|
|
149
|
+
if (!res.ok) {
|
|
150
|
+
window.alert("Could not load the shared section to detach.");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const shared = (await res.json()) as { block?: Record<string, unknown> };
|
|
154
|
+
if (!shared.block || typeof shared.block !== "object") {
|
|
155
|
+
window.alert("The shared section has no block content to detach.");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const { type, ...rest } = shared.block;
|
|
159
|
+
updateAttributes({ blockType: String(type ?? ""), fields: rest });
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const preview = isShared
|
|
163
|
+
? sharedTitle
|
|
164
|
+
: (() => {
|
|
165
|
+
for (const [key, meta] of Object.entries(fieldsMeta)) {
|
|
166
|
+
if (meta.type === "text" && fields[key]) {
|
|
167
|
+
const text = String(fields[key]);
|
|
168
|
+
return text.length > 60 ? text.slice(0, 60) + "..." : text;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return "";
|
|
172
|
+
})();
|
|
173
|
+
|
|
174
|
+
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}>
|
|
179
|
+
<span
|
|
180
|
+
data-drag-handle
|
|
181
|
+
draggable="true"
|
|
182
|
+
className="text-muted-foreground/50 hover:text-muted-foreground -ml-1 cursor-grab rounded p-1 transition-colors active:cursor-grabbing"
|
|
183
|
+
title="Drag to reorder"
|
|
184
|
+
>
|
|
185
|
+
<GripVertical className="size-4" />
|
|
186
|
+
</span>
|
|
187
|
+
|
|
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>
|
|
207
|
+
|
|
208
|
+
{!expanded && preview && <span className="text-foreground/70 min-w-0 truncate text-sm">{preview}</span>}
|
|
209
|
+
|
|
210
|
+
<div className="ml-auto flex shrink-0 items-center">
|
|
211
|
+
<Button
|
|
212
|
+
type="button"
|
|
213
|
+
variant="ghost"
|
|
214
|
+
size="icon"
|
|
215
|
+
title={isShared ? "Detach shared section" : "Save as shared section"}
|
|
216
|
+
className="text-muted-foreground hover:text-foreground size-7"
|
|
217
|
+
onClick={() => (isShared ? detach() : openSaveDialog())}
|
|
218
|
+
>
|
|
219
|
+
{isShared ? <Unlink className="size-3.5" /> : <Save className="size-3.5" />}
|
|
220
|
+
</Button>
|
|
221
|
+
<Button
|
|
222
|
+
type="button"
|
|
223
|
+
variant="ghost"
|
|
224
|
+
size="icon"
|
|
225
|
+
title="Remove block"
|
|
226
|
+
className="text-muted-foreground hover:text-destructive size-7"
|
|
227
|
+
onClick={() => deleteNode()}
|
|
228
|
+
>
|
|
229
|
+
<Trash2 className="size-3.5" />
|
|
230
|
+
</Button>
|
|
231
|
+
</div>
|
|
232
|
+
</div>
|
|
233
|
+
|
|
234
|
+
{expanded && (
|
|
235
|
+
<div className="space-y-4 border-t px-4 py-4" contentEditable={false}>
|
|
236
|
+
{isShared ? (
|
|
237
|
+
<div className="space-y-3">
|
|
238
|
+
<div>
|
|
239
|
+
<p className="font-medium">{sharedTitle}</p>
|
|
240
|
+
<p className="text-muted-foreground text-sm">
|
|
241
|
+
Editing the source updates every place that uses this shared section.
|
|
242
|
+
</p>
|
|
243
|
+
</div>
|
|
244
|
+
{sharedRef && (
|
|
245
|
+
<a
|
|
246
|
+
href={`/admin/shared-sections/${sharedRef}`}
|
|
247
|
+
target="_blank"
|
|
248
|
+
className={buttonVariants({ variant: "outline", size: "sm" })}
|
|
249
|
+
>
|
|
250
|
+
<ArrowUpRight className="size-3.5" />
|
|
251
|
+
Edit source
|
|
252
|
+
</a>
|
|
253
|
+
)}
|
|
254
|
+
</div>
|
|
255
|
+
) : (
|
|
256
|
+
<>
|
|
257
|
+
{Object.keys(fieldsMeta).length === 0 && (
|
|
258
|
+
<p className="text-muted-foreground text-sm">Unknown block type "{blockType}".</p>
|
|
259
|
+
)}
|
|
260
|
+
{Object.entries(fieldsMeta).map(([fieldName, meta]) => (
|
|
261
|
+
<SubField
|
|
262
|
+
key={fieldName}
|
|
263
|
+
blockKey={fieldIdPrefix}
|
|
264
|
+
fieldName={fieldName}
|
|
265
|
+
meta={meta}
|
|
266
|
+
value={fields[fieldName]}
|
|
267
|
+
onChange={(v) => updateField(fieldName, v)}
|
|
268
|
+
relationOptions={meta.type === "relation" ? getRelationOptions(fieldName) : []}
|
|
269
|
+
linkOptions={options.linkOptions ?? []}
|
|
270
|
+
/>
|
|
271
|
+
))}
|
|
272
|
+
</>
|
|
273
|
+
)}
|
|
274
|
+
</div>
|
|
275
|
+
)}
|
|
276
|
+
|
|
277
|
+
<Dialog open={saveDialogOpen} onOpenChange={setSaveDialogOpen}>
|
|
278
|
+
<DialogContent className="max-w-sm">
|
|
279
|
+
<DialogHeader>
|
|
280
|
+
<DialogTitle>Save as shared section</DialogTitle>
|
|
281
|
+
<DialogDescription>Give this section a name so it can be reused on other pages.</DialogDescription>
|
|
282
|
+
</DialogHeader>
|
|
283
|
+
<form
|
|
284
|
+
onSubmit={(e) => {
|
|
285
|
+
e.preventDefault();
|
|
286
|
+
saveAsShared();
|
|
287
|
+
}}
|
|
288
|
+
>
|
|
289
|
+
<Input
|
|
290
|
+
value={sharedName}
|
|
291
|
+
onChange={(e) => setSharedName(e.target.value)}
|
|
292
|
+
placeholder="Section name"
|
|
293
|
+
autoFocus
|
|
294
|
+
/>
|
|
295
|
+
<DialogFooter className="mt-4">
|
|
296
|
+
<Button type="button" variant="outline" onClick={() => setSaveDialogOpen(false)}>
|
|
297
|
+
Cancel
|
|
298
|
+
</Button>
|
|
299
|
+
<Button type="submit" disabled={!sharedName.trim()}>
|
|
300
|
+
Save
|
|
301
|
+
</Button>
|
|
302
|
+
</DialogFooter>
|
|
303
|
+
</form>
|
|
304
|
+
</DialogContent>
|
|
305
|
+
</Dialog>
|
|
306
|
+
</NodeViewWrapper>
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const BlockNode = Node.create<BlockNodeOptions>({
|
|
311
|
+
...blockNodeSpec,
|
|
312
|
+
addNodeView() {
|
|
313
|
+
return ReactNodeViewRenderer(BlockNodeView);
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// -----------------------------------------------
|
|
318
|
+
// Toolbar button
|
|
319
|
+
// -----------------------------------------------
|
|
320
|
+
|
|
321
|
+
const ToolbarButton = ({
|
|
322
|
+
onClick,
|
|
323
|
+
active,
|
|
324
|
+
disabled,
|
|
325
|
+
children,
|
|
326
|
+
title,
|
|
327
|
+
}: {
|
|
328
|
+
onClick: () => void;
|
|
329
|
+
active?: boolean;
|
|
330
|
+
disabled?: boolean;
|
|
331
|
+
children: React.ReactNode;
|
|
332
|
+
title: string;
|
|
333
|
+
}) => (
|
|
334
|
+
<button
|
|
335
|
+
type="button"
|
|
336
|
+
data-slot="button"
|
|
337
|
+
onClick={onClick}
|
|
338
|
+
disabled={disabled}
|
|
339
|
+
title={title}
|
|
340
|
+
className={cn(
|
|
341
|
+
"disabled:hover:text-muted-foreground inline-flex size-8 items-center justify-center rounded-md transition-colors outline-none disabled:opacity-50 disabled:hover:bg-transparent",
|
|
342
|
+
active ? "bg-accent text-accent-foreground" : "text-muted-foreground hover:bg-accent/60 hover:text-foreground",
|
|
343
|
+
)}
|
|
344
|
+
>
|
|
345
|
+
{children}
|
|
346
|
+
</button>
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
// -----------------------------------------------
|
|
350
|
+
// Editor component
|
|
351
|
+
// -----------------------------------------------
|
|
352
|
+
|
|
353
|
+
type Props = {
|
|
354
|
+
name: string;
|
|
355
|
+
label?: string;
|
|
356
|
+
initialValue?: string;
|
|
357
|
+
rows?: number;
|
|
358
|
+
types: BlockTypesMeta;
|
|
359
|
+
blockRelationOptions?: Record<string, RelationOption[]>;
|
|
360
|
+
linkOptions?: LinkOptionGroup[];
|
|
361
|
+
/** Shared sections offered by the `/` menu. */
|
|
362
|
+
sharedSections?: SharedSectionOption[];
|
|
363
|
+
/** When true, show a button that expands the editor into a fullscreen overlay. */
|
|
364
|
+
fullscreen?: boolean;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
export default function ContentEditor({
|
|
368
|
+
name,
|
|
369
|
+
label,
|
|
370
|
+
initialValue,
|
|
371
|
+
rows = 14,
|
|
372
|
+
types,
|
|
373
|
+
blockRelationOptions = {},
|
|
374
|
+
linkOptions = [],
|
|
375
|
+
sharedSections = [],
|
|
376
|
+
fullscreen = false,
|
|
377
|
+
}: Props) {
|
|
378
|
+
const hiddenRef = useRef<HTMLInputElement>(null);
|
|
379
|
+
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
380
|
+
// Mirror of the real save button (rendered by EditView) so the fullscreen header
|
|
381
|
+
// can offer Save without duplicating any draft/publish logic.
|
|
382
|
+
const [saveLabel, setSaveLabel] = useState<string | null>(null);
|
|
383
|
+
const [saveDisabled, setSaveDisabled] = useState(false);
|
|
384
|
+
// Per-document, per-field key used to re-enter fullscreen after a save reload.
|
|
385
|
+
const restoreKey = useCallback(
|
|
386
|
+
() => `cms-content-fullscreen:${typeof window === "undefined" ? "" : window.location.pathname}:${name}`,
|
|
387
|
+
[name],
|
|
388
|
+
);
|
|
389
|
+
const previewChannelRef = useRef<BroadcastChannel | null>(null);
|
|
390
|
+
const [linkDialogOpen, setLinkDialogOpen] = useState(false);
|
|
391
|
+
const [linkType, setLinkType] = useState<"internal" | "external">("internal");
|
|
392
|
+
const [linkUrl, setLinkUrl] = useState("");
|
|
393
|
+
const [linkGroups, setLinkGroups] = useState<LinkGroup[]>([]);
|
|
394
|
+
|
|
395
|
+
const [cmsJson, setCmsJson] = useState<string>(() => {
|
|
396
|
+
if (!initialValue) return JSON.stringify({ type: "root", children: [] });
|
|
397
|
+
try {
|
|
398
|
+
const parsed = JSON.parse(initialValue);
|
|
399
|
+
if (parsed?.type === "root") return initialValue;
|
|
400
|
+
} catch {}
|
|
401
|
+
return JSON.stringify({ type: "root", children: [] });
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// Counter to force re-renders on selection/transaction changes
|
|
405
|
+
const [, setTick] = useState(0);
|
|
406
|
+
const forceTick = useCallback(() => setTick((t) => t + 1), []);
|
|
407
|
+
|
|
408
|
+
const parsedInitial = (() => {
|
|
409
|
+
try {
|
|
410
|
+
return JSON.parse(cmsJson);
|
|
411
|
+
} catch {
|
|
412
|
+
return { type: "root", children: [] };
|
|
413
|
+
}
|
|
414
|
+
})();
|
|
415
|
+
|
|
416
|
+
// Notify the form of value changes so UnsavedGuard detects them
|
|
417
|
+
const prevCmsJsonRef = useRef(cmsJson);
|
|
418
|
+
useEffect(() => {
|
|
419
|
+
if (prevCmsJsonRef.current !== cmsJson) {
|
|
420
|
+
prevCmsJsonRef.current = cmsJson;
|
|
421
|
+
hiddenRef.current?.dispatchEvent(new Event("change", { bubbles: true }));
|
|
422
|
+
}
|
|
423
|
+
}, [cmsJson]);
|
|
424
|
+
|
|
425
|
+
const editor = useEditor({
|
|
426
|
+
immediatelyRender: false,
|
|
427
|
+
extensions: [
|
|
428
|
+
// StarterKit bundles its own Link; disabled so the configured one below is the only registration.
|
|
429
|
+
StarterKit.configure({ link: false }),
|
|
430
|
+
Image,
|
|
431
|
+
Link.configure({
|
|
432
|
+
openOnClick: false,
|
|
433
|
+
autolink: false,
|
|
434
|
+
linkOnPaste: false,
|
|
435
|
+
HTMLAttributes: { class: "text-primary underline cursor-text pointer-events-none" },
|
|
436
|
+
}),
|
|
437
|
+
// Command hint, shown on the empty field and on the empty paragraph the cursor is on.
|
|
438
|
+
Placeholder.configure({
|
|
439
|
+
showOnlyCurrent: true,
|
|
440
|
+
placeholder: ({ node }) => (node.type.name === "paragraph" ? "Type / for commands…" : ""),
|
|
441
|
+
}),
|
|
442
|
+
BlockNode.configure({ types, blockRelationOptions, linkOptions, fieldName: name }),
|
|
443
|
+
SlashCommand.configure({ types, sharedSections }),
|
|
444
|
+
],
|
|
445
|
+
content: contentToTiptap(parsedInitial),
|
|
446
|
+
onUpdate: ({ editor }) => {
|
|
447
|
+
const cmsDoc = tiptapToContent(editor.getJSON());
|
|
448
|
+
const json = JSON.stringify(cmsDoc);
|
|
449
|
+
setCmsJson(json);
|
|
450
|
+
// Live preview: broadcast for server-side rendering
|
|
451
|
+
if (!previewChannelRef.current) previewChannelRef.current = new BroadcastChannel("cms-preview");
|
|
452
|
+
previewChannelRef.current.postMessage({ field: name, value: json, render: "content" });
|
|
453
|
+
},
|
|
454
|
+
onSelectionUpdate: forceTick,
|
|
455
|
+
onTransaction: forceTick,
|
|
456
|
+
editorProps: {
|
|
457
|
+
attributes: {
|
|
458
|
+
class: "prose prose-sm max-w-none text-base focus:outline-none py-3 px-5",
|
|
459
|
+
style: `min-height: ${rows * 1.5}rem;`,
|
|
460
|
+
},
|
|
461
|
+
},
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
// Listen for external content updates (e.g. AI translate)
|
|
465
|
+
useEffect(() => {
|
|
466
|
+
const hidden = hiddenRef.current;
|
|
467
|
+
if (!hidden) return;
|
|
468
|
+
const handler = (e: Event) => {
|
|
469
|
+
const detail = (e as CustomEvent).detail;
|
|
470
|
+
if (typeof detail !== "string") return;
|
|
471
|
+
try {
|
|
472
|
+
const parsed = JSON.parse(detail);
|
|
473
|
+
if (parsed?.type === "root") {
|
|
474
|
+
setCmsJson(detail);
|
|
475
|
+
editor?.commands.setContent(contentToTiptap(parsed));
|
|
476
|
+
}
|
|
477
|
+
} catch {}
|
|
478
|
+
};
|
|
479
|
+
hidden.addEventListener("cms:set-value", handler);
|
|
480
|
+
return () => hidden.removeEventListener("cms:set-value", handler);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
// Find the document form's real save button and mirror its label + disabled state.
|
|
484
|
+
const findSaveButton = useCallback(() => {
|
|
485
|
+
const form = hiddenRef.current?.form;
|
|
486
|
+
if (!form?.id) return null;
|
|
487
|
+
return form.ownerDocument.querySelector<HTMLButtonElement>(
|
|
488
|
+
`button[type="submit"][form="${form.id}"][value="save"]`,
|
|
489
|
+
);
|
|
490
|
+
}, []);
|
|
491
|
+
|
|
492
|
+
useEffect(() => {
|
|
493
|
+
const btn = findSaveButton();
|
|
494
|
+
if (!btn) return;
|
|
495
|
+
// btn is a real DOM node from a sibling component tree; its state can't be read at render time.
|
|
496
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
497
|
+
setSaveLabel((btn.textContent || "Save").trim());
|
|
498
|
+
setSaveDisabled(btn.disabled);
|
|
499
|
+
const observer = new MutationObserver(() => setSaveDisabled(btn.disabled));
|
|
500
|
+
observer.observe(btn, { attributes: true, attributeFilter: ["disabled"] });
|
|
501
|
+
return () => observer.disconnect();
|
|
502
|
+
}, [findSaveButton]);
|
|
503
|
+
|
|
504
|
+
const triggerSave = () => {
|
|
505
|
+
const btn = findSaveButton();
|
|
506
|
+
if (btn && !btn.disabled) btn.click();
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// Re-enter fullscreen after a save reload (flag set by the submit handler below).
|
|
510
|
+
useEffect(() => {
|
|
511
|
+
if (!fullscreen) return;
|
|
512
|
+
try {
|
|
513
|
+
if (sessionStorage.getItem(restoreKey()) === "1") {
|
|
514
|
+
sessionStorage.removeItem(restoreKey());
|
|
515
|
+
// Deliberately post-mount: computing this during render would make the client's first
|
|
516
|
+
// render diverge from SSR's (sessionStorage doesn't exist server-side) — a hydration mismatch.
|
|
517
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
518
|
+
setIsFullscreen(true);
|
|
519
|
+
}
|
|
520
|
+
} catch {}
|
|
521
|
+
}, [fullscreen, restoreKey]);
|
|
522
|
+
|
|
523
|
+
// Fullscreen overlay: lock background scroll, exit on Escape, and survive the
|
|
524
|
+
// save reload — Cmd/Ctrl+S submits the form and navigates, so we flag the field
|
|
525
|
+
// to re-enter fullscreen on the next load instead of dropping out of the view.
|
|
526
|
+
useEffect(() => {
|
|
527
|
+
if (!isFullscreen) return;
|
|
528
|
+
// The overlay now covers the viewport — drop the pre-paint no-flash cover.
|
|
529
|
+
document.documentElement.classList.remove("cms-fullscreen-restoring");
|
|
530
|
+
const prevOverflow = document.body.style.overflow;
|
|
531
|
+
document.body.style.overflow = "hidden";
|
|
532
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
533
|
+
if (e.key === "Escape") setIsFullscreen(false);
|
|
534
|
+
};
|
|
535
|
+
window.addEventListener("keydown", onKeyDown);
|
|
536
|
+
const form = hiddenRef.current?.form;
|
|
537
|
+
const onSubmit = () => {
|
|
538
|
+
try {
|
|
539
|
+
sessionStorage.setItem(restoreKey(), "1");
|
|
540
|
+
} catch {}
|
|
541
|
+
};
|
|
542
|
+
form?.addEventListener("submit", onSubmit);
|
|
543
|
+
return () => {
|
|
544
|
+
document.body.style.overflow = prevOverflow;
|
|
545
|
+
window.removeEventListener("keydown", onKeyDown);
|
|
546
|
+
form?.removeEventListener("submit", onSubmit);
|
|
547
|
+
};
|
|
548
|
+
}, [isFullscreen, restoreKey]);
|
|
549
|
+
|
|
550
|
+
const openLinkDialog = () => {
|
|
551
|
+
if (!editor) return;
|
|
552
|
+
const href = editor.getAttributes("link").href ?? "";
|
|
553
|
+
setLinkUrl(href);
|
|
554
|
+
const isExternal = href.startsWith("http://") || href.startsWith("https://");
|
|
555
|
+
setLinkType(href && !isExternal ? "internal" : href ? "external" : "internal");
|
|
556
|
+
if (linkGroups.length === 0) fetchLinkGroups().then(setLinkGroups);
|
|
557
|
+
setLinkDialogOpen(true);
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
const minHeight = `${rows * 1.5}rem`;
|
|
561
|
+
|
|
562
|
+
return (
|
|
563
|
+
<div
|
|
564
|
+
data-slot="editor"
|
|
565
|
+
className={cn(
|
|
566
|
+
isFullscreen
|
|
567
|
+
? "bg-background fixed inset-0 z-50 flex flex-col"
|
|
568
|
+
: "border-input hover:border-foreground/20 bg-muted/30 dark:bg-input/30 relative overflow-hidden rounded-lg border transition-colors",
|
|
569
|
+
)}
|
|
570
|
+
>
|
|
571
|
+
<input ref={hiddenRef} type="hidden" name={name} value={cmsJson} />
|
|
572
|
+
|
|
573
|
+
{/* Fullscreen header — keeps the field title and an exit affordance in view */}
|
|
574
|
+
{isFullscreen && (
|
|
575
|
+
<div className="bg-background flex items-center justify-between gap-3 border-b px-4 py-2.5">
|
|
576
|
+
<span className="text-foreground text-sm font-medium">{label ?? humanize(name)}</span>
|
|
577
|
+
<div className="flex items-center gap-2">
|
|
578
|
+
{saveLabel && (
|
|
579
|
+
<Button type="button" variant="outline" size="sm" disabled={saveDisabled} onClick={triggerSave}>
|
|
580
|
+
{saveLabel}
|
|
581
|
+
</Button>
|
|
582
|
+
)}
|
|
583
|
+
<Button type="button" variant="ghost" size="sm" className="gap-1.5" onClick={() => setIsFullscreen(false)}>
|
|
584
|
+
<Minimize2 className="size-4" />
|
|
585
|
+
Exit
|
|
586
|
+
</Button>
|
|
587
|
+
</div>
|
|
588
|
+
</div>
|
|
589
|
+
)}
|
|
590
|
+
|
|
591
|
+
{/* Enter-fullscreen affordance (inline mode only) */}
|
|
592
|
+
{fullscreen && !isFullscreen && (
|
|
593
|
+
<button
|
|
594
|
+
type="button"
|
|
595
|
+
title="Fullscreen"
|
|
596
|
+
onClick={() => setIsFullscreen(true)}
|
|
597
|
+
className="text-muted-foreground hover:text-foreground bg-background/70 hover:bg-background absolute top-2 right-2 z-10 rounded-md border p-1.5 backdrop-blur transition-colors"
|
|
598
|
+
>
|
|
599
|
+
<Maximize2 className="size-4" />
|
|
600
|
+
</button>
|
|
601
|
+
)}
|
|
602
|
+
|
|
603
|
+
{/* Editor area */}
|
|
604
|
+
{editor ? (
|
|
605
|
+
<div className={cn("min-w-0", isFullscreen && "flex-1 overflow-y-auto")}>
|
|
606
|
+
<div
|
|
607
|
+
className={cn(
|
|
608
|
+
isFullscreen && "mx-auto w-full max-w-3xl px-2 py-6",
|
|
609
|
+
fullscreen && !isFullscreen && "[&_.ProseMirror]:pr-8",
|
|
610
|
+
)}
|
|
611
|
+
>
|
|
612
|
+
{/* Selection toolbar — appears when text is highlighted */}
|
|
613
|
+
<BubbleMenu
|
|
614
|
+
editor={editor}
|
|
615
|
+
options={{ placement: "top" }}
|
|
616
|
+
shouldShow={({ editor: ed, from, to }) => from !== to && !ed.isActive(BLOCK_NODE_NAME)}
|
|
617
|
+
className="bg-popover flex items-center gap-0.5 rounded-md border p-1 shadow-md"
|
|
618
|
+
>
|
|
619
|
+
<ToolbarButton
|
|
620
|
+
onClick={() => editor.chain().focus().toggleBold().run()}
|
|
621
|
+
active={editor.isActive("bold")}
|
|
622
|
+
title="Bold"
|
|
623
|
+
>
|
|
624
|
+
<Bold className="size-4" />
|
|
625
|
+
</ToolbarButton>
|
|
626
|
+
<ToolbarButton
|
|
627
|
+
onClick={() => editor.chain().focus().toggleItalic().run()}
|
|
628
|
+
active={editor.isActive("italic")}
|
|
629
|
+
title="Italic"
|
|
630
|
+
>
|
|
631
|
+
<Italic className="size-4" />
|
|
632
|
+
</ToolbarButton>
|
|
633
|
+
<div className="bg-border mx-0.5 h-5 w-px" />
|
|
634
|
+
<ToolbarButton
|
|
635
|
+
onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
|
|
636
|
+
active={editor.isActive("heading", { level: 2 })}
|
|
637
|
+
title="Heading 2"
|
|
638
|
+
>
|
|
639
|
+
<Heading2 className="size-4" />
|
|
640
|
+
</ToolbarButton>
|
|
641
|
+
<ToolbarButton
|
|
642
|
+
onClick={() => editor.chain().focus().toggleHeading({ level: 3 }).run()}
|
|
643
|
+
active={editor.isActive("heading", { level: 3 })}
|
|
644
|
+
title="Heading 3"
|
|
645
|
+
>
|
|
646
|
+
<Heading3 className="size-4" />
|
|
647
|
+
</ToolbarButton>
|
|
648
|
+
<div className="bg-border mx-0.5 h-5 w-px" />
|
|
649
|
+
<ToolbarButton onClick={openLinkDialog} active={editor.isActive("link")} title="Link">
|
|
650
|
+
<LinkIcon className="size-4" />
|
|
651
|
+
</ToolbarButton>
|
|
652
|
+
</BubbleMenu>
|
|
653
|
+
<EditorContent editor={editor} />
|
|
654
|
+
</div>
|
|
655
|
+
</div>
|
|
656
|
+
) : (
|
|
657
|
+
<div className="prose prose-sm max-w-none" style={{ minHeight, padding: "0.625rem 0.75rem" }} />
|
|
658
|
+
)}
|
|
659
|
+
|
|
660
|
+
<LinkDialog
|
|
661
|
+
open={linkDialogOpen}
|
|
662
|
+
onOpenChange={setLinkDialogOpen}
|
|
663
|
+
linkType={linkType}
|
|
664
|
+
onLinkTypeChange={setLinkType}
|
|
665
|
+
linkUrl={linkUrl}
|
|
666
|
+
onLinkUrlChange={setLinkUrl}
|
|
667
|
+
linkGroups={linkGroups}
|
|
668
|
+
isEditing={!!editor?.isActive("link")}
|
|
669
|
+
onApply={() => {
|
|
670
|
+
if (linkUrl) {
|
|
671
|
+
const isExternal = linkUrl.startsWith("http://") || linkUrl.startsWith("https://");
|
|
672
|
+
editor
|
|
673
|
+
?.chain()
|
|
674
|
+
.focus()
|
|
675
|
+
.setLink({ href: linkUrl, target: isExternal ? "_blank" : null })
|
|
676
|
+
.run();
|
|
677
|
+
}
|
|
678
|
+
setLinkDialogOpen(false);
|
|
679
|
+
}}
|
|
680
|
+
onRemove={() => {
|
|
681
|
+
editor?.chain().focus().unsetLink().run();
|
|
682
|
+
setLinkDialogOpen(false);
|
|
683
|
+
}}
|
|
684
|
+
/>
|
|
685
|
+
</div>
|
|
686
|
+
);
|
|
687
|
+
}
|