@conloca/cms-spa 0.1.0 → 0.1.2

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/dist/index.mjs ADDED
@@ -0,0 +1,1329 @@
1
+ import { n as setDataSchemas } from "./data-schemas-DdWjbi4X.mjs";
2
+ import { BlockTypeSelect, BoldItalicUnderlineToggles, CreateLink, DiffSourceToggleWrapper, InsertCodeBlock, InsertImage, InsertTable, InsertThematicBreak, ListsToggle, MDXEditor, Separator, UndoRedo, closeImageDialog$, codeBlockPlugin, codeMirrorPlugin, diffSourcePlugin, frontmatterPlugin, headingsPlugin, imageDialogState$, imagePlugin, insertImage$, jsxPlugin, linkDialogPlugin, linkPlugin, listsPlugin, markdownShortcutPlugin, quotePlugin, tablePlugin, thematicBreakPlugin, toolbarPlugin, useCellValue, usePublisher } from "@mdxeditor/editor";
3
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
4
+ import "@mdxeditor/editor/style.css";
5
+ import { Folder, FolderPlus, Search, Upload, X } from "lucide-react";
6
+ import { useAssetFolders, useCreateFolder, useFolderTree, useImportAssetUrl, useUploadAsset } from "@conloca/content-api-client";
7
+ import { clsx } from "clsx";
8
+ import { twMerge } from "tailwind-merge";
9
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
10
+ import { useMDXEvaluation } from "@conloca/mdx";
11
+
12
+ //#region src/ui-config.ts
13
+ const defaultConfig = {
14
+ basename: "/__cms",
15
+ apiBaseUrl: "/__cms/api",
16
+ siteBaseUrl: "",
17
+ enableDevtools: false,
18
+ queryClientOptions: { defaultOptions: { queries: {
19
+ staleTime: 1e3 * 60 * 5,
20
+ retry: 1
21
+ } } }
22
+ };
23
+ let config = { ...defaultConfig };
24
+ function configureUI(options) {
25
+ config = {
26
+ ...defaultConfig,
27
+ ...options
28
+ };
29
+ if (typeof window !== "undefined") window.__UI_CONFIG__ = config;
30
+ }
31
+ if (typeof window !== "undefined") window.configureUI = configureUI;
32
+
33
+ //#endregion
34
+ //#region src/hooks/useUpload.ts
35
+ /** Read image dimensions from a File using the browser Image API */
36
+ function getImageDimensions(file) {
37
+ return new Promise((resolve, reject) => {
38
+ if (!file.type.startsWith("image/")) {
39
+ reject(/* @__PURE__ */ new Error("File is not an image"));
40
+ return;
41
+ }
42
+ const url = URL.createObjectURL(file);
43
+ const img = new Image();
44
+ img.onload = () => {
45
+ resolve({
46
+ width: img.naturalWidth,
47
+ height: img.naturalHeight
48
+ });
49
+ URL.revokeObjectURL(url);
50
+ };
51
+ img.onerror = () => {
52
+ URL.revokeObjectURL(url);
53
+ reject(/* @__PURE__ */ new Error("Failed to load image for dimension reading"));
54
+ };
55
+ img.src = url;
56
+ });
57
+ }
58
+ /** Build FormData for asset upload, reading dimensions client-side */
59
+ async function buildUploadFormData(file, alt, folder) {
60
+ const formData = new FormData();
61
+ formData.append("file", file);
62
+ if (alt) formData.append("alt", alt);
63
+ if (folder && folder !== "/") formData.append("folder", folder);
64
+ try {
65
+ const { width, height } = await getImageDimensions(file);
66
+ formData.append("width", String(width));
67
+ formData.append("height", String(height));
68
+ } catch {}
69
+ return formData;
70
+ }
71
+
72
+ //#endregion
73
+ //#region src/utils/cn.ts
74
+ function cn(...inputs) {
75
+ return twMerge(clsx(inputs));
76
+ }
77
+
78
+ //#endregion
79
+ //#region src/components/dialogs/CreateFolderDialog.tsx
80
+ function CreateFolderDialog({ open, isPending, onClose, onCreate }) {
81
+ const [folderName, setFolderName] = useState("");
82
+ const inputRef = useRef(null);
83
+ useEffect(() => {
84
+ if (open) {
85
+ setFolderName("");
86
+ setTimeout(() => inputRef.current?.focus(), 50);
87
+ }
88
+ }, [open]);
89
+ const handleCreate = () => {
90
+ const name = folderName.trim();
91
+ if (!name) return;
92
+ onCreate(name);
93
+ };
94
+ const handleKeyDown = (e) => {
95
+ if (e.key === "Escape") onClose();
96
+ else if (e.key === "Enter" && folderName.trim()) handleCreate();
97
+ };
98
+ if (!open) return null;
99
+ const isValid = folderName.trim().length > 0;
100
+ return /* @__PURE__ */ jsx("div", {
101
+ className: "fixed inset-0 bg-black/50 flex items-center justify-center z-50",
102
+ role: "dialog",
103
+ onKeyDown: handleKeyDown,
104
+ children: /* @__PURE__ */ jsxs("div", {
105
+ className: "bg-white rounded-lg p-6 w-full max-w-md",
106
+ "data-testid": "create-folder-dialog",
107
+ children: [
108
+ /* @__PURE__ */ jsx("h2", {
109
+ className: "text-xl font-semibold text-grey-01 mb-4",
110
+ children: "Create New Folder"
111
+ }),
112
+ /* @__PURE__ */ jsxs("div", {
113
+ className: "mb-4",
114
+ children: [/* @__PURE__ */ jsx("label", {
115
+ htmlFor: "folder-name",
116
+ className: "block text-sm font-medium text-grey-01 mb-2",
117
+ children: "Folder Name"
118
+ }), /* @__PURE__ */ jsx("input", {
119
+ ref: inputRef,
120
+ id: "folder-name",
121
+ type: "text",
122
+ value: folderName,
123
+ onChange: (e) => setFolderName(e.target.value),
124
+ placeholder: "Enter folder name",
125
+ className: "w-full px-3 py-2 border border-grey-09 rounded focus:outline-none focus:ring-2 focus:ring-azure-04",
126
+ "data-testid": "folder-name-input"
127
+ })]
128
+ }),
129
+ /* @__PURE__ */ jsxs("div", {
130
+ className: "flex justify-end gap-3",
131
+ children: [/* @__PURE__ */ jsx("button", {
132
+ type: "button",
133
+ onClick: onClose,
134
+ className: "px-4 py-2 border border-grey-09 rounded hover:bg-grey-11 transition-colors",
135
+ children: "Cancel"
136
+ }), /* @__PURE__ */ jsx("button", {
137
+ type: "button",
138
+ onClick: handleCreate,
139
+ disabled: !isValid || isPending,
140
+ className: "px-4 py-2 bg-azure-04 text-white rounded hover:bg-azure-03 transition-colors disabled:opacity-50 disabled:cursor-not-allowed",
141
+ "data-testid": "create-folder-submit",
142
+ children: isPending ? "Creating..." : "Create"
143
+ })]
144
+ })
145
+ ]
146
+ })
147
+ });
148
+ }
149
+
150
+ //#endregion
151
+ //#region src/components/media/FolderTreeSidebar.tsx
152
+ function FolderNode({ node, depth, currentFolder, dropTargetFolder, onFolderSelect }) {
153
+ const isActive = node.path === currentFolder;
154
+ const isDropTarget = node.path === dropTargetFolder;
155
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("button", {
156
+ type: "button",
157
+ onClick: () => onFolderSelect(node.path),
158
+ className: cn("w-full flex items-center gap-2 py-1.5 px-3 rounded text-sm transition-colors text-left", {
159
+ "bg-azure-11 text-azure-01": isActive,
160
+ "hover:bg-grey-11": !isActive,
161
+ "ring-2 ring-azure-04": isDropTarget
162
+ }),
163
+ style: { paddingLeft: `${depth * 16 + 12}px` },
164
+ children: [
165
+ /* @__PURE__ */ jsx(Folder, { className: "w-4 h-4 flex-shrink-0" }),
166
+ /* @__PURE__ */ jsx("span", {
167
+ className: "truncate flex-1",
168
+ children: node.name
169
+ }),
170
+ /* @__PURE__ */ jsxs("span", {
171
+ className: "text-grey-05 text-xs",
172
+ children: [
173
+ "(",
174
+ node.assetCount,
175
+ ")"
176
+ ]
177
+ })
178
+ ]
179
+ }), node.children.map((child) => /* @__PURE__ */ jsx(FolderNode, {
180
+ node: child,
181
+ depth: depth + 1,
182
+ currentFolder,
183
+ dropTargetFolder,
184
+ onFolderSelect
185
+ }, child.path))] });
186
+ }
187
+ function FolderTreeSidebar({ currentFolder, onFolderSelect, dropTargetFolder }) {
188
+ const [showCreateFolderDialog, setShowCreateFolderDialog] = useState(false);
189
+ const { data, isLoading } = useFolderTree();
190
+ const createFolder = useCreateFolder();
191
+ const rawTree = data?.tree ?? [];
192
+ const tree = rawTree.length === 1 && rawTree[0].path === "/" ? rawTree[0].children : rawTree;
193
+ const handleCreateFolder = (name) => {
194
+ const folderPath = `/${name}`;
195
+ createFolder.mutate(folderPath, { onSuccess: () => {
196
+ setShowCreateFolderDialog(false);
197
+ } });
198
+ };
199
+ const rootAssetCount = rawTree.length === 1 && rawTree[0].path === "/" ? rawTree[0].assetCount : tree.reduce((sum, node) => sum + node.assetCount, 0);
200
+ return /* @__PURE__ */ jsxs("div", {
201
+ className: "w-56 flex-shrink-0 border-r border-grey-09 bg-white",
202
+ children: [
203
+ /* @__PURE__ */ jsxs("div", {
204
+ className: "flex items-center justify-between px-4 py-3 border-b border-grey-09",
205
+ children: [/* @__PURE__ */ jsx("h3", {
206
+ className: "font-semibold text-grey-01",
207
+ children: "Folders"
208
+ }), /* @__PURE__ */ jsx("button", {
209
+ type: "button",
210
+ onClick: () => setShowCreateFolderDialog(true),
211
+ className: "p-1.5 rounded hover:bg-grey-11 transition-colors",
212
+ title: "New Folder",
213
+ children: /* @__PURE__ */ jsx(FolderPlus, { className: "w-4 h-4 text-grey-04" })
214
+ })]
215
+ }),
216
+ /* @__PURE__ */ jsx("div", {
217
+ className: "p-2 overflow-y-auto",
218
+ style: { maxHeight: "calc(100vh - 200px)" },
219
+ children: isLoading ? /* @__PURE__ */ jsx("div", {
220
+ className: "text-sm text-grey-05 px-3 py-2",
221
+ children: "Loading..."
222
+ }) : /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("button", {
223
+ type: "button",
224
+ onClick: () => onFolderSelect("/"),
225
+ className: cn("w-full flex items-center gap-2 py-1.5 px-3 rounded text-sm transition-colors text-left", {
226
+ "bg-azure-11 text-azure-01": currentFolder === "/",
227
+ "hover:bg-grey-11": currentFolder !== "/",
228
+ "ring-2 ring-azure-04": dropTargetFolder === "/"
229
+ }),
230
+ children: [
231
+ /* @__PURE__ */ jsx(Folder, { className: "w-4 h-4 flex-shrink-0" }),
232
+ /* @__PURE__ */ jsx("span", {
233
+ className: "truncate flex-1",
234
+ children: "Root"
235
+ }),
236
+ /* @__PURE__ */ jsxs("span", {
237
+ className: "text-grey-05 text-xs",
238
+ children: [
239
+ "(",
240
+ rootAssetCount,
241
+ ")"
242
+ ]
243
+ })
244
+ ]
245
+ }), tree.length > 0 ? tree.map((node) => /* @__PURE__ */ jsx(FolderNode, {
246
+ node,
247
+ depth: 1,
248
+ currentFolder,
249
+ dropTargetFolder,
250
+ onFolderSelect
251
+ }, node.path)) : /* @__PURE__ */ jsx("div", {
252
+ className: "text-sm text-grey-05 px-3 py-2",
253
+ children: "No folders yet"
254
+ })] })
255
+ }),
256
+ /* @__PURE__ */ jsx(CreateFolderDialog, {
257
+ open: showCreateFolderDialog,
258
+ isPending: createFolder.isPending,
259
+ onClose: () => setShowCreateFolderDialog(false),
260
+ onCreate: handleCreateFolder
261
+ })
262
+ ]
263
+ });
264
+ }
265
+
266
+ //#endregion
267
+ //#region src/components/media/AssetCard.tsx
268
+ function AssetCard({ asset, isSelected, onClick, onDoubleClick, assetsBasePath = "/__cms/api/assets/serve" }) {
269
+ const handleClick = () => {
270
+ onClick?.();
271
+ };
272
+ const handleDoubleClick = () => {
273
+ onDoubleClick?.();
274
+ };
275
+ return /* @__PURE__ */ jsxs("div", {
276
+ onClick: handleClick,
277
+ onDoubleClick: handleDoubleClick,
278
+ onKeyDown: (e) => {
279
+ if (e.key === "Enter" || e.key === " ") {
280
+ e.preventDefault();
281
+ handleClick();
282
+ }
283
+ },
284
+ role: "button",
285
+ tabIndex: 0,
286
+ className: cn("group relative rounded overflow-hidden cursor-pointer transition-colors", "bg-white hover:border-azure-04", isSelected ? "border-2 border-azure-04" : "border border-grey-09"),
287
+ children: [/* @__PURE__ */ jsx("div", {
288
+ className: "aspect-square bg-grey-11 flex items-center justify-center overflow-hidden",
289
+ children: /* @__PURE__ */ jsx("img", {
290
+ src: `${assetsBasePath}/${asset.folder && asset.folder !== "/" ? `${asset.folder.replace(/^\//, "")}/${asset.filename}` : asset.filename}`,
291
+ alt: asset.alt || asset.originalName,
292
+ className: "w-full h-full object-contain",
293
+ loading: "lazy"
294
+ })
295
+ }), /* @__PURE__ */ jsx("div", {
296
+ className: "px-2 py-1.5",
297
+ children: /* @__PURE__ */ jsx("p", {
298
+ className: "text-xs text-grey-01 truncate",
299
+ title: asset.originalName,
300
+ children: asset.originalName
301
+ })
302
+ })]
303
+ });
304
+ }
305
+
306
+ //#endregion
307
+ //#region src/components/media/MediaToolbar.tsx
308
+ function MediaToolbar({ search, onSearchChange, fileType, onFileTypeChange, sort, onSortChange, isSelectMode, onEnterSelectMode, onExitSelectMode, onUploadClick }) {
309
+ return /* @__PURE__ */ jsxs("div", {
310
+ className: "flex items-center gap-3 flex-wrap",
311
+ children: [
312
+ /* @__PURE__ */ jsxs("div", {
313
+ className: "relative flex-1 min-w-[200px]",
314
+ children: [/* @__PURE__ */ jsx(Search, { className: "absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-grey-04" }), /* @__PURE__ */ jsx("input", {
315
+ type: "text",
316
+ value: search,
317
+ onChange: (e) => onSearchChange(e.target.value),
318
+ placeholder: "Search files...",
319
+ className: "w-full pl-9 pr-3 py-2 border border-grey-09 rounded text-sm focus:outline-none focus:ring-2 focus:ring-azure-04 focus:border-azure-04"
320
+ })]
321
+ }),
322
+ /* @__PURE__ */ jsxs("select", {
323
+ value: fileType,
324
+ onChange: (e) => onFileTypeChange(e.target.value),
325
+ className: "px-3 py-2 border border-grey-09 rounded text-sm bg-white hover:bg-grey-11 transition-colors",
326
+ children: [
327
+ /* @__PURE__ */ jsx("option", {
328
+ value: "all",
329
+ children: "All types"
330
+ }),
331
+ /* @__PURE__ */ jsx("option", {
332
+ value: "images",
333
+ children: "Images"
334
+ }),
335
+ /* @__PURE__ */ jsx("option", {
336
+ value: "svg",
337
+ children: "SVG"
338
+ })
339
+ ]
340
+ }),
341
+ /* @__PURE__ */ jsxs("select", {
342
+ value: sort,
343
+ onChange: (e) => onSortChange(e.target.value),
344
+ className: "px-3 py-2 border border-grey-09 rounded text-sm bg-white hover:bg-grey-11 transition-colors",
345
+ children: [
346
+ /* @__PURE__ */ jsx("option", {
347
+ value: "date-newest",
348
+ children: "Date (newest)"
349
+ }),
350
+ /* @__PURE__ */ jsx("option", {
351
+ value: "date-oldest",
352
+ children: "Date (oldest)"
353
+ }),
354
+ /* @__PURE__ */ jsx("option", {
355
+ value: "name-asc",
356
+ children: "Name (A-Z)"
357
+ }),
358
+ /* @__PURE__ */ jsx("option", {
359
+ value: "name-desc",
360
+ children: "Name (Z-A)"
361
+ }),
362
+ /* @__PURE__ */ jsx("option", {
363
+ value: "size-largest",
364
+ children: "Size (largest)"
365
+ }),
366
+ /* @__PURE__ */ jsx("option", {
367
+ value: "size-smallest",
368
+ children: "Size (smallest)"
369
+ })
370
+ ]
371
+ }),
372
+ isSelectMode ? /* @__PURE__ */ jsx("button", {
373
+ type: "button",
374
+ onClick: onExitSelectMode,
375
+ className: "px-3 py-2 bg-azure-04 text-white rounded text-sm hover:bg-azure-03 transition-colors",
376
+ children: "Done"
377
+ }) : onEnterSelectMode && /* @__PURE__ */ jsx("button", {
378
+ type: "button",
379
+ onClick: onEnterSelectMode,
380
+ className: "px-3 py-2 border border-grey-09 rounded text-sm bg-white hover:bg-grey-11 transition-colors",
381
+ children: "Select"
382
+ }),
383
+ onUploadClick && /* @__PURE__ */ jsxs("button", {
384
+ type: "button",
385
+ onClick: onUploadClick,
386
+ className: "flex items-center gap-2 px-3 py-2 bg-azure-04 text-white rounded text-sm hover:bg-azure-03 transition-colors",
387
+ children: [/* @__PURE__ */ jsx(Upload, { className: "w-4 h-4" }), "Upload"]
388
+ })
389
+ ]
390
+ });
391
+ }
392
+
393
+ //#endregion
394
+ //#region src/components/media/MediaLibrary.tsx
395
+ function MediaLibrary({ folder: initialFolder = "/", onFolderChange, showToolbar = true, mode = "page", assetsBasePath, selectedAssets, onToggleSelect, onAssetClick, onAssetDoubleClick, onSelect, selectedAsset, search: externalSearch, fileType: externalFileType, sort: externalSort }) {
396
+ const [internalSearch, setInternalSearch] = useState("");
397
+ const [internalFileType, setInternalFileType] = useState("all");
398
+ const [internalSort, setInternalSort] = useState("date-newest");
399
+ const search = externalSearch ?? internalSearch;
400
+ const fileType = externalFileType ?? internalFileType;
401
+ const sort = externalSort ?? internalSort;
402
+ const [showCreateFolderDialog, setShowCreateFolderDialog] = useState(false);
403
+ const { data, isLoading } = useAssetFolders(initialFolder);
404
+ const createFolder = useCreateFolder();
405
+ const assets = data?.assets ?? [];
406
+ const filteredAssets = useMemo(() => {
407
+ let result = assets;
408
+ if (search.trim()) {
409
+ const q = search.toLowerCase();
410
+ result = result.filter((a) => a.originalName.toLowerCase().includes(q) || a.filename.toLowerCase().includes(q) || a.alt?.toLowerCase().includes(q));
411
+ }
412
+ if (fileType !== "all") result = result.filter((a) => {
413
+ if (fileType === "images") return a.mimeType.startsWith("image/") && a.mimeType !== "image/svg+xml";
414
+ if (fileType === "svg") return a.mimeType === "image/svg+xml";
415
+ return true;
416
+ });
417
+ return result;
418
+ }, [
419
+ assets,
420
+ search,
421
+ fileType
422
+ ]);
423
+ const sortedAssets = useMemo(() => {
424
+ const sorted = [...filteredAssets];
425
+ switch (sort) {
426
+ case "date-newest":
427
+ sorted.sort((a, b) => new Date(b.uploadedAt).getTime() - new Date(a.uploadedAt).getTime());
428
+ break;
429
+ case "date-oldest":
430
+ sorted.sort((a, b) => new Date(a.uploadedAt).getTime() - new Date(b.uploadedAt).getTime());
431
+ break;
432
+ case "name-asc":
433
+ sorted.sort((a, b) => a.originalName.localeCompare(b.originalName));
434
+ break;
435
+ case "name-desc":
436
+ sorted.sort((a, b) => b.originalName.localeCompare(a.originalName));
437
+ break;
438
+ case "size-largest":
439
+ sorted.sort((a, b) => b.size - a.size);
440
+ break;
441
+ case "size-smallest":
442
+ sorted.sort((a, b) => a.size - b.size);
443
+ break;
444
+ }
445
+ return sorted;
446
+ }, [filteredAssets, sort]);
447
+ const handleAssetClick = (asset) => {
448
+ if (mode === "picker") onSelect?.(asset);
449
+ else onAssetClick?.(asset);
450
+ };
451
+ const handleAssetDoubleClick = (asset) => {
452
+ onAssetDoubleClick?.(asset);
453
+ };
454
+ const handleCreateFolder = (name) => {
455
+ const folderPath = initialFolder === "/" ? `/${name}` : `${initialFolder}/${name}`;
456
+ createFolder.mutate(folderPath, { onSuccess: () => {
457
+ setShowCreateFolderDialog(false);
458
+ onFolderChange?.(folderPath);
459
+ } });
460
+ };
461
+ return /* @__PURE__ */ jsxs("div", {
462
+ className: "flex flex-col gap-4",
463
+ children: [
464
+ showToolbar && externalSearch === void 0 && /* @__PURE__ */ jsx(MediaToolbar, {
465
+ search: internalSearch,
466
+ onSearchChange: setInternalSearch,
467
+ fileType: internalFileType,
468
+ onFileTypeChange: setInternalFileType,
469
+ sort: internalSort,
470
+ onSortChange: setInternalSort
471
+ }),
472
+ isLoading ? /* @__PURE__ */ jsx("div", {
473
+ className: "text-center py-12 text-grey-04",
474
+ children: "Loading assets..."
475
+ }) : /* @__PURE__ */ jsx(Fragment, { children: sortedAssets.length === 0 ? /* @__PURE__ */ jsx("div", {
476
+ className: "text-center py-12 text-grey-04",
477
+ children: assets.length === 0 ? "No assets yet. Click Upload to add your first image." : "No assets match your filter."
478
+ }) : /* @__PURE__ */ jsx("div", {
479
+ className: "grid gap-4",
480
+ style: { gridTemplateColumns: "repeat(auto-fit, minmax(120px, 150px))" },
481
+ children: sortedAssets.map((asset) => /* @__PURE__ */ jsx(AssetCard, {
482
+ asset,
483
+ isSelected: selectedAssets?.has(asset.filename) ?? selectedAsset?.filename === asset.filename,
484
+ onClick: () => handleAssetClick(asset),
485
+ onDoubleClick: () => handleAssetDoubleClick(asset),
486
+ assetsBasePath
487
+ }, asset.filename))
488
+ }) }),
489
+ mode === "picker" && /* @__PURE__ */ jsx(CreateFolderDialog, {
490
+ open: showCreateFolderDialog,
491
+ isPending: createFolder.isPending,
492
+ onClose: () => setShowCreateFolderDialog(false),
493
+ onCreate: handleCreateFolder
494
+ })
495
+ ]
496
+ });
497
+ }
498
+
499
+ //#endregion
500
+ //#region src/components/media/UploadModal.tsx
501
+ const ACCEPTED_TYPES = "image/jpeg,image/png,image/gif,image/webp,image/svg+xml,image/avif";
502
+ function UploadModal({ isOpen, onClose, folder, onUploadComplete }) {
503
+ const [activeTab, setActiveTab] = useState("file");
504
+ const [isDragOver, setIsDragOver] = useState(false);
505
+ const [pendingFile, setPendingFile] = useState(null);
506
+ const [altText, setAltText] = useState("");
507
+ const [customName, setCustomName] = useState("");
508
+ const [importUrl, setImportUrl] = useState("");
509
+ const [uploadProgress, setUploadProgress] = useState(null);
510
+ const fileInputRef = useRef(null);
511
+ const uploadMutation = useUploadAsset();
512
+ const importMutation = useImportAssetUrl();
513
+ const isUploading = uploadMutation.isPending || importMutation.isPending || (uploadProgress?.inProgress ?? false);
514
+ const error = uploadMutation.error || importMutation.error;
515
+ const resetState = useCallback(() => {
516
+ setPendingFile(null);
517
+ setAltText("");
518
+ setCustomName("");
519
+ setImportUrl("");
520
+ setUploadProgress(null);
521
+ setActiveTab("file");
522
+ setIsDragOver(false);
523
+ if (fileInputRef.current) fileInputRef.current.value = "";
524
+ }, []);
525
+ useEffect(() => {
526
+ if (!isOpen) return;
527
+ const handleKeyDown = (e) => {
528
+ if (e.key === "Escape") onClose();
529
+ };
530
+ document.addEventListener("keydown", handleKeyDown);
531
+ return () => document.removeEventListener("keydown", handleKeyDown);
532
+ }, [isOpen, onClose]);
533
+ useEffect(() => {
534
+ if (!isOpen) resetState();
535
+ }, [isOpen, resetState]);
536
+ const handleSingleFile = useCallback((file) => {
537
+ setPendingFile(file);
538
+ setAltText("");
539
+ setCustomName("");
540
+ }, []);
541
+ const handleMultipleFiles = useCallback(async (files) => {
542
+ if (files.length === 0) return;
543
+ if (files.length === 1) {
544
+ handleSingleFile(files[0]);
545
+ return;
546
+ }
547
+ setUploadProgress({
548
+ total: files.length,
549
+ completed: 0,
550
+ failed: 0,
551
+ inProgress: true
552
+ });
553
+ const uploadPromises = files.map(async (file) => {
554
+ const formData = await buildUploadFormData(file, void 0, folder);
555
+ return uploadMutation.mutateAsync(formData);
556
+ });
557
+ const results = await Promise.allSettled(uploadPromises);
558
+ const completed = results.filter((r) => r.status === "fulfilled").length;
559
+ const failed = results.filter((r) => r.status === "rejected").length;
560
+ setUploadProgress({
561
+ total: files.length,
562
+ completed,
563
+ failed,
564
+ inProgress: false
565
+ });
566
+ setTimeout(() => {
567
+ if (completed > 0) onUploadComplete?.();
568
+ onClose();
569
+ }, 2e3);
570
+ }, [
571
+ folder,
572
+ uploadMutation,
573
+ handleSingleFile,
574
+ onUploadComplete,
575
+ onClose
576
+ ]);
577
+ const handleFiles = useCallback((fileList) => {
578
+ if (!fileList || fileList.length === 0) return;
579
+ handleMultipleFiles(Array.from(fileList));
580
+ }, [handleMultipleFiles]);
581
+ const handleDragOver = useCallback((e) => {
582
+ e.preventDefault();
583
+ setIsDragOver(true);
584
+ }, []);
585
+ const handleDragLeave = useCallback((e) => {
586
+ e.preventDefault();
587
+ setIsDragOver(false);
588
+ }, []);
589
+ const handleDrop = useCallback((e) => {
590
+ e.preventDefault();
591
+ setIsDragOver(false);
592
+ handleFiles(e.dataTransfer.files);
593
+ }, [handleFiles]);
594
+ const handleUpload = async () => {
595
+ if (!pendingFile) return;
596
+ const formData = await buildUploadFormData(pendingFile, altText || void 0, folder);
597
+ uploadMutation.mutate(formData, { onSuccess: () => {
598
+ onUploadComplete?.();
599
+ onClose();
600
+ } });
601
+ };
602
+ const handleImportUrl = () => {
603
+ if (!importUrl.trim()) return;
604
+ importMutation.mutate({
605
+ url: importUrl.trim(),
606
+ alt: altText || void 0,
607
+ folder
608
+ }, { onSuccess: () => {
609
+ onUploadComplete?.();
610
+ onClose();
611
+ } });
612
+ };
613
+ const handleCancel = () => {
614
+ setPendingFile(null);
615
+ setAltText("");
616
+ setCustomName("");
617
+ if (fileInputRef.current) fileInputRef.current.value = "";
618
+ };
619
+ if (!isOpen) return null;
620
+ return /* @__PURE__ */ jsx("div", {
621
+ className: "fixed inset-0 bg-black/50 flex items-center justify-center z-50",
622
+ onClick: (e) => {
623
+ if (e.target === e.currentTarget) onClose();
624
+ },
625
+ children: /* @__PURE__ */ jsxs("div", {
626
+ className: "bg-white rounded-lg w-full max-w-md mx-4 shadow-xl",
627
+ children: [
628
+ /* @__PURE__ */ jsxs("div", {
629
+ className: "flex items-center justify-between px-6 py-4 border-b border-grey-09",
630
+ children: [/* @__PURE__ */ jsx("h2", {
631
+ className: "text-lg font-semibold text-grey-01",
632
+ children: "Upload Assets"
633
+ }), /* @__PURE__ */ jsx("button", {
634
+ type: "button",
635
+ onClick: onClose,
636
+ className: "p-1 text-grey-04 hover:text-grey-01 transition-colors rounded hover:bg-grey-11",
637
+ children: /* @__PURE__ */ jsx(X, { className: "w-5 h-5" })
638
+ })]
639
+ }),
640
+ /* @__PURE__ */ jsxs("div", {
641
+ className: "p-6",
642
+ children: [
643
+ /* @__PURE__ */ jsxs("div", {
644
+ className: "flex gap-2 mb-4",
645
+ children: [/* @__PURE__ */ jsx("button", {
646
+ type: "button",
647
+ onClick: () => setActiveTab("file"),
648
+ className: cn("px-3 py-1.5 text-sm rounded transition-colors", activeTab === "file" ? "bg-azure-04 text-white" : "bg-white border border-grey-09 text-grey-04 hover:bg-grey-11"),
649
+ children: "Upload File"
650
+ }), /* @__PURE__ */ jsx("button", {
651
+ type: "button",
652
+ onClick: () => setActiveTab("url"),
653
+ className: cn("px-3 py-1.5 text-sm rounded transition-colors", activeTab === "url" ? "bg-azure-04 text-white" : "bg-white border border-grey-09 text-grey-04 hover:bg-grey-11"),
654
+ children: "Import URL"
655
+ })]
656
+ }),
657
+ activeTab === "file" && /* @__PURE__ */ jsxs(Fragment, { children: [uploadProgress && /* @__PURE__ */ jsxs("div", {
658
+ className: "mb-4 p-3 bg-grey-11 rounded border border-grey-09",
659
+ children: [
660
+ /* @__PURE__ */ jsx("p", {
661
+ className: "text-sm font-medium text-grey-01",
662
+ children: uploadProgress.inProgress ? `Uploading ${uploadProgress.total} files...` : `Upload complete: ${uploadProgress.completed}/${uploadProgress.total} succeeded`
663
+ }),
664
+ uploadProgress.failed > 0 && /* @__PURE__ */ jsxs("p", {
665
+ className: "text-sm text-red-04 mt-1",
666
+ children: [uploadProgress.failed, " file(s) failed to upload"]
667
+ }),
668
+ /* @__PURE__ */ jsx("div", {
669
+ className: "mt-2 h-2 bg-grey-09 rounded overflow-hidden",
670
+ children: /* @__PURE__ */ jsx("div", {
671
+ className: cn("h-full transition-all", uploadProgress.failed > 0 ? "bg-yellow-06" : "bg-azure-04"),
672
+ style: { width: `${(uploadProgress.completed + uploadProgress.failed) / uploadProgress.total * 100}%` }
673
+ })
674
+ })
675
+ ]
676
+ }), pendingFile ? /* @__PURE__ */ jsxs("div", {
677
+ className: "space-y-4",
678
+ children: [
679
+ /* @__PURE__ */ jsxs("div", {
680
+ className: "flex items-center gap-3",
681
+ children: [/* @__PURE__ */ jsx("div", {
682
+ className: "w-16 h-16 bg-grey-09 rounded overflow-hidden flex-shrink-0",
683
+ children: /* @__PURE__ */ jsx("img", {
684
+ src: URL.createObjectURL(pendingFile),
685
+ alt: "Preview",
686
+ className: "w-full h-full object-contain"
687
+ })
688
+ }), /* @__PURE__ */ jsxs("div", {
689
+ className: "flex-1 min-w-0",
690
+ children: [/* @__PURE__ */ jsx("p", {
691
+ className: "text-sm font-medium text-grey-01 truncate",
692
+ children: pendingFile.name
693
+ }), /* @__PURE__ */ jsxs("p", {
694
+ className: "text-xs text-grey-04",
695
+ children: [(pendingFile.size / 1024).toFixed(1), " KB"]
696
+ })]
697
+ })]
698
+ }),
699
+ /* @__PURE__ */ jsxs("div", { children: [
700
+ /* @__PURE__ */ jsx("label", {
701
+ htmlFor: "custom-name",
702
+ className: "block text-xs text-grey-04 uppercase tracking-wide mb-1",
703
+ children: "Asset Name (optional)"
704
+ }),
705
+ /* @__PURE__ */ jsx("input", {
706
+ id: "custom-name",
707
+ type: "text",
708
+ value: customName,
709
+ onChange: (e) => setCustomName(e.target.value),
710
+ placeholder: pendingFile.name,
711
+ className: "w-full px-3 py-2 border border-grey-09 rounded text-sm focus:outline-none focus:ring-2 focus:ring-azure-04 focus:border-azure-04"
712
+ }),
713
+ /* @__PURE__ */ jsx("p", {
714
+ className: "mt-1 text-xs text-grey-07",
715
+ children: "Leave blank to use original filename"
716
+ })
717
+ ] }),
718
+ /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("label", {
719
+ htmlFor: "alt-text",
720
+ className: "block text-xs text-grey-04 uppercase tracking-wide mb-1",
721
+ children: "Alt Text"
722
+ }), /* @__PURE__ */ jsx("input", {
723
+ id: "alt-text",
724
+ type: "text",
725
+ value: altText,
726
+ onChange: (e) => setAltText(e.target.value),
727
+ placeholder: "Describe the image for accessibility",
728
+ className: "w-full px-3 py-2 border border-grey-09 rounded text-sm focus:outline-none focus:ring-2 focus:ring-azure-04 focus:border-azure-04"
729
+ })] })
730
+ ]
731
+ }) : /* @__PURE__ */ jsxs("div", {
732
+ onDragOver: handleDragOver,
733
+ onDragLeave: handleDragLeave,
734
+ onDrop: handleDrop,
735
+ className: cn("border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors bg-grey-11", isDragOver ? "border-azure-04 bg-azure-11" : "border-grey-09 hover:border-grey-07"),
736
+ onClick: () => fileInputRef.current?.click(),
737
+ onKeyDown: (e) => {
738
+ if (e.key === "Enter" || e.key === " ") {
739
+ e.preventDefault();
740
+ fileInputRef.current?.click();
741
+ }
742
+ },
743
+ role: "button",
744
+ tabIndex: 0,
745
+ children: [
746
+ /* @__PURE__ */ jsx(Upload, { className: "mx-auto h-10 w-10 text-grey-04" }),
747
+ /* @__PURE__ */ jsx("p", {
748
+ className: "mt-2 text-sm text-grey-04",
749
+ children: "Drag and drop images here, or click to browse"
750
+ }),
751
+ /* @__PURE__ */ jsx("p", {
752
+ className: "mt-1 text-xs text-grey-07",
753
+ children: "JPEG, PNG, GIF, WebP, SVG, AVIF - Multiple files supported"
754
+ }),
755
+ /* @__PURE__ */ jsx("input", {
756
+ ref: fileInputRef,
757
+ type: "file",
758
+ accept: ACCEPTED_TYPES,
759
+ multiple: true,
760
+ onChange: (e) => handleFiles(e.target.files),
761
+ className: "hidden"
762
+ })
763
+ ]
764
+ })] }),
765
+ activeTab === "url" && /* @__PURE__ */ jsxs("div", {
766
+ className: "space-y-4",
767
+ children: [/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("label", {
768
+ htmlFor: "import-url",
769
+ className: "block text-xs text-grey-04 uppercase tracking-wide mb-1",
770
+ children: "Image URL"
771
+ }), /* @__PURE__ */ jsx("input", {
772
+ id: "import-url",
773
+ type: "url",
774
+ value: importUrl,
775
+ onChange: (e) => setImportUrl(e.target.value),
776
+ placeholder: "https://example.com/image.jpg",
777
+ className: "w-full px-3 py-2 border border-grey-09 rounded text-sm focus:outline-none focus:ring-2 focus:ring-azure-04 focus:border-azure-04"
778
+ })] }), /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("label", {
779
+ htmlFor: "url-alt-text",
780
+ className: "block text-xs text-grey-04 uppercase tracking-wide mb-1",
781
+ children: "Alt Text"
782
+ }), /* @__PURE__ */ jsx("input", {
783
+ id: "url-alt-text",
784
+ type: "text",
785
+ value: altText,
786
+ onChange: (e) => setAltText(e.target.value),
787
+ placeholder: "Describe the image for accessibility",
788
+ className: "w-full px-3 py-2 border border-grey-09 rounded text-sm focus:outline-none focus:ring-2 focus:ring-azure-04 focus:border-azure-04"
789
+ })] })]
790
+ }),
791
+ error && /* @__PURE__ */ jsx("p", {
792
+ className: "mt-4 text-sm text-red-04",
793
+ children: error instanceof Error ? error.message : "Upload failed"
794
+ })
795
+ ]
796
+ }),
797
+ /* @__PURE__ */ jsxs("div", {
798
+ className: "flex items-center justify-end gap-3 px-6 py-4 border-t border-grey-09 bg-grey-11 rounded-b-lg",
799
+ children: [/* @__PURE__ */ jsx("button", {
800
+ type: "button",
801
+ onClick: pendingFile ? handleCancel : onClose,
802
+ disabled: isUploading,
803
+ className: "px-4 py-2 bg-white border border-grey-09 text-grey-04 text-sm rounded hover:bg-grey-11 disabled:opacity-50 transition-colors",
804
+ children: pendingFile ? "Clear" : "Cancel"
805
+ }), activeTab === "file" ? /* @__PURE__ */ jsxs("button", {
806
+ type: "button",
807
+ onClick: handleUpload,
808
+ disabled: isUploading || !pendingFile,
809
+ className: "flex items-center gap-2 px-4 py-2 bg-azure-04 text-white text-sm rounded hover:bg-azure-03 disabled:opacity-50 transition-colors",
810
+ children: [/* @__PURE__ */ jsx(Upload, { className: "w-4 h-4" }), uploadMutation.isPending ? "Uploading..." : "Upload"]
811
+ }) : /* @__PURE__ */ jsxs("button", {
812
+ type: "button",
813
+ onClick: handleImportUrl,
814
+ disabled: isUploading || !importUrl.trim(),
815
+ className: "flex items-center gap-2 px-4 py-2 bg-azure-04 text-white text-sm rounded hover:bg-azure-03 disabled:opacity-50 transition-colors",
816
+ children: [/* @__PURE__ */ jsx(Upload, { className: "w-4 h-4" }), importMutation.isPending ? "Importing..." : "Import"]
817
+ })]
818
+ })
819
+ ]
820
+ })
821
+ });
822
+ }
823
+
824
+ //#endregion
825
+ //#region src/components/media/ImagePicker.tsx
826
+ /**
827
+ * Generic image picker modal with folder tree, media grid, and upload.
828
+ * Renders a 3-panel modal: folder tree sidebar, image grid, and header with Upload button.
829
+ * Callers are responsible for path construction from the selected AssetEntry.
830
+ */
831
+ function ImagePicker({ isOpen, onSelect, onClose }) {
832
+ const [currentFolder, setCurrentFolder] = useState("/");
833
+ const [showUpload, setShowUpload] = useState(false);
834
+ useEffect(() => {
835
+ if (!isOpen) return;
836
+ const handleKeyDown = (e) => {
837
+ if (e.key === "Escape") onClose();
838
+ };
839
+ document.addEventListener("keydown", handleKeyDown);
840
+ return () => document.removeEventListener("keydown", handleKeyDown);
841
+ }, [isOpen, onClose]);
842
+ if (!isOpen) return null;
843
+ const handleAssetSelect = (asset) => {
844
+ onSelect(asset);
845
+ };
846
+ const handleBackdropClick = (e) => {
847
+ if (e.target === e.currentTarget) onClose();
848
+ };
849
+ return /* @__PURE__ */ jsxs("div", {
850
+ className: "fixed inset-0 z-50 flex items-center justify-center bg-black/50",
851
+ onClick: handleBackdropClick,
852
+ children: [/* @__PURE__ */ jsxs("div", {
853
+ className: "bg-white rounded-lg shadow-xl w-full max-w-5xl max-h-[85vh] mx-4 flex flex-col overflow-hidden",
854
+ children: [/* @__PURE__ */ jsxs("div", {
855
+ className: "flex items-center justify-between px-6 py-4 border-b border-grey-09 flex-shrink-0",
856
+ children: [/* @__PURE__ */ jsx("h2", {
857
+ className: "text-lg font-semibold text-grey-01",
858
+ children: "Select Image"
859
+ }), /* @__PURE__ */ jsxs("div", {
860
+ className: "flex items-center gap-2",
861
+ children: [/* @__PURE__ */ jsxs("button", {
862
+ type: "button",
863
+ onClick: () => setShowUpload(true),
864
+ className: "flex items-center gap-2 px-3 py-1.5 text-sm bg-azure-04 text-white rounded hover:bg-azure-03 transition-colors",
865
+ children: [/* @__PURE__ */ jsx(Upload, { className: "w-4 h-4" }), "Upload"]
866
+ }), /* @__PURE__ */ jsx("button", {
867
+ type: "button",
868
+ onClick: onClose,
869
+ className: "p-1.5 text-grey-04 hover:text-grey-01 transition-colors rounded hover:bg-grey-11",
870
+ "aria-label": "Close",
871
+ children: /* @__PURE__ */ jsx(X, { className: "w-5 h-5" })
872
+ })]
873
+ })]
874
+ }), /* @__PURE__ */ jsxs("div", {
875
+ className: "flex flex-1 min-h-0",
876
+ children: [/* @__PURE__ */ jsx(FolderTreeSidebar, {
877
+ currentFolder,
878
+ onFolderSelect: setCurrentFolder
879
+ }), /* @__PURE__ */ jsx("div", {
880
+ className: "flex-1 p-4 overflow-y-auto",
881
+ children: /* @__PURE__ */ jsx(MediaLibrary, {
882
+ folder: currentFolder,
883
+ onFolderChange: setCurrentFolder,
884
+ mode: "picker",
885
+ showToolbar: true,
886
+ onSelect: handleAssetSelect
887
+ })
888
+ })]
889
+ })]
890
+ }), /* @__PURE__ */ jsx(UploadModal, {
891
+ isOpen: showUpload,
892
+ folder: currentFolder,
893
+ onClose: () => setShowUpload(false),
894
+ onUploadComplete: () => {
895
+ setShowUpload(false);
896
+ }
897
+ })]
898
+ });
899
+ }
900
+
901
+ //#endregion
902
+ //#region src/components/editor/ImagePickerDialog.tsx
903
+ /**
904
+ * MDX-specific thin wrapper around ImagePicker.
905
+ * Bridges MDXEditor gurx signals to the generic ImagePicker component.
906
+ */
907
+ function ImagePickerDialog() {
908
+ const state = useCellValue(imageDialogState$);
909
+ const insertImage = usePublisher(insertImage$);
910
+ const closeDialog = usePublisher(closeImageDialog$);
911
+ const handleSelect = (asset) => {
912
+ insertImage({ src: `/assets${asset.folder && asset.folder !== "/" ? asset.folder : ""}/${asset.filename}` });
913
+ closeDialog();
914
+ };
915
+ return /* @__PURE__ */ jsx(ImagePicker, {
916
+ isOpen: state.type !== "inactive",
917
+ onSelect: handleSelect,
918
+ onClose: () => closeDialog()
919
+ });
920
+ }
921
+
922
+ //#endregion
923
+ //#region src/components/editor/CMSMDXEditor.tsx
924
+ /**
925
+ * Error Boundary for catching React errors in MDXEditor.
926
+ * Prevents editor crashes from breaking the entire CMS.
927
+ */
928
+ var MDXEditorErrorBoundary = class extends React.Component {
929
+ constructor(props) {
930
+ super(props);
931
+ this.state = { hasError: false };
932
+ }
933
+ static getDerivedStateFromError(error) {
934
+ return {
935
+ hasError: true,
936
+ error
937
+ };
938
+ }
939
+ componentDidCatch(error, errorInfo) {
940
+ console.error("[CMSMDXEditor] Error boundary caught:", error, errorInfo);
941
+ }
942
+ render() {
943
+ if (this.state.hasError) return /* @__PURE__ */ jsxs("div", {
944
+ className: "p-4 bg-red-50 border border-red-200 rounded text-red-600",
945
+ children: [
946
+ /* @__PURE__ */ jsx("p", {
947
+ className: "font-semibold mb-2",
948
+ children: "MDXEditor Error"
949
+ }),
950
+ /* @__PURE__ */ jsx("p", {
951
+ className: "text-sm mb-2",
952
+ children: this.state.error?.message || "Unknown error occurred"
953
+ }),
954
+ /* @__PURE__ */ jsxs("details", {
955
+ className: "text-xs",
956
+ children: [/* @__PURE__ */ jsx("summary", {
957
+ className: "cursor-pointer font-medium",
958
+ children: "Stack Trace"
959
+ }), /* @__PURE__ */ jsx("pre", {
960
+ className: "mt-2 p-2 bg-red-100 rounded overflow-auto whitespace-pre-wrap",
961
+ children: this.state.error?.stack
962
+ })]
963
+ })
964
+ ]
965
+ });
966
+ return this.props.children;
967
+ }
968
+ };
969
+ /**
970
+ * CMS-integrated MDX Editor with Media Library image picker.
971
+ * This component wraps MDXEditor with ImagePickerDialog and keyboard shortcuts.
972
+ */
973
+ const CMSMDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false }, ref) => {
974
+ useEffect(() => {
975
+ const handleKeyDown = (e) => {
976
+ if ((e.ctrlKey || e.metaKey) && e.key === "s") {
977
+ e.preventDefault();
978
+ if (onSave) onSave(value);
979
+ }
980
+ if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key.toLowerCase() === "i") {
981
+ e.preventDefault();
982
+ document.querySelector("[aria-label=\"Insert image\"]")?.click();
983
+ }
984
+ };
985
+ document.addEventListener("keydown", handleKeyDown);
986
+ return () => document.removeEventListener("keydown", handleKeyDown);
987
+ }, [value, onSave]);
988
+ return /* @__PURE__ */ jsx(MDXEditor, {
989
+ ref,
990
+ markdown: value,
991
+ onChange,
992
+ readOnly,
993
+ contentEditableClassName: "prose max-w-none",
994
+ plugins: [
995
+ headingsPlugin(),
996
+ listsPlugin(),
997
+ quotePlugin(),
998
+ linkPlugin(),
999
+ linkDialogPlugin(),
1000
+ tablePlugin(),
1001
+ thematicBreakPlugin(),
1002
+ frontmatterPlugin(),
1003
+ codeBlockPlugin({ defaultCodeBlockLanguage: "ts" }),
1004
+ codeMirrorPlugin({ codeBlockLanguages: {
1005
+ text: "Plain Text",
1006
+ js: "JavaScript",
1007
+ jsx: "JavaScript (React)",
1008
+ ts: "TypeScript",
1009
+ tsx: "TypeScript (React)",
1010
+ css: "CSS",
1011
+ html: "HTML",
1012
+ json: "JSON",
1013
+ md: "Markdown",
1014
+ mdx: "MDX",
1015
+ bash: "Bash",
1016
+ python: "Python"
1017
+ } }),
1018
+ diffSourcePlugin({ viewMode: "rich-text" }),
1019
+ markdownShortcutPlugin(),
1020
+ jsxPlugin(),
1021
+ imagePlugin({
1022
+ ImageDialog: ImagePickerDialog,
1023
+ disableImageSettingsButton: true
1024
+ }),
1025
+ toolbarPlugin({ toolbarContents: () => /* @__PURE__ */ jsxs(DiffSourceToggleWrapper, { children: [
1026
+ /* @__PURE__ */ jsx(UndoRedo, {}),
1027
+ /* @__PURE__ */ jsx(Separator, {}),
1028
+ /* @__PURE__ */ jsx(BoldItalicUnderlineToggles, {}),
1029
+ /* @__PURE__ */ jsx(Separator, {}),
1030
+ /* @__PURE__ */ jsx(ListsToggle, {}),
1031
+ /* @__PURE__ */ jsx(Separator, {}),
1032
+ /* @__PURE__ */ jsx(BlockTypeSelect, {}),
1033
+ /* @__PURE__ */ jsx(Separator, {}),
1034
+ /* @__PURE__ */ jsx(CreateLink, {}),
1035
+ /* @__PURE__ */ jsx(InsertImage, {}),
1036
+ /* @__PURE__ */ jsx(InsertTable, {}),
1037
+ /* @__PURE__ */ jsx(InsertThematicBreak, {}),
1038
+ /* @__PURE__ */ jsx(Separator, {}),
1039
+ /* @__PURE__ */ jsx(InsertCodeBlock, {})
1040
+ ] }) })
1041
+ ]
1042
+ });
1043
+ });
1044
+ CMSMDXEditor.displayName = "CMSMDXEditor";
1045
+ /**
1046
+ * DeferredCMSMDXEditor - A wrapper component that defers CMSMDXEditor initialization
1047
+ * to avoid React 19 render-phase state updates.
1048
+ *
1049
+ * See the original DeferredMDXEditor in @conloca/mdx for detailed documentation.
1050
+ */
1051
+ const DeferredCMSMDXEditor = ({ value, onChange, onSave, readOnly }) => {
1052
+ const editorRef = React.useRef(null);
1053
+ const [isReady, setIsReady] = useState(false);
1054
+ useEffect(() => {
1055
+ const timer = setTimeout(() => {
1056
+ setIsReady(true);
1057
+ }, 10);
1058
+ return () => clearTimeout(timer);
1059
+ }, []);
1060
+ if (!isReady) return /* @__PURE__ */ jsx("div", {
1061
+ className: "flex items-center justify-center h-full text-gray-500",
1062
+ "data-testid": "deferred-editor-loading",
1063
+ children: "Initializing editor..."
1064
+ });
1065
+ return /* @__PURE__ */ jsx("div", {
1066
+ "data-testid": "deferred-editor-ready",
1067
+ children: /* @__PURE__ */ jsx(CMSMDXEditor, {
1068
+ ref: editorRef,
1069
+ value,
1070
+ onChange,
1071
+ onSave,
1072
+ readOnly
1073
+ })
1074
+ });
1075
+ };
1076
+ const Dialog = ({ open, onOpenChange, children }) => {
1077
+ if (!open) return null;
1078
+ return /* @__PURE__ */ jsxs("div", {
1079
+ className: "fixed inset-0 z-50 flex items-center justify-center",
1080
+ children: [/* @__PURE__ */ jsx("div", {
1081
+ className: "fixed inset-0 bg-black/50",
1082
+ onClick: () => onOpenChange(false)
1083
+ }), /* @__PURE__ */ jsx("div", {
1084
+ className: "relative z-50 bg-white rounded-lg shadow-lg max-w-[90vw] max-h-[90vh] overflow-hidden",
1085
+ children
1086
+ })]
1087
+ });
1088
+ };
1089
+ const DialogContent = ({ children, className = "" }) => {
1090
+ return /* @__PURE__ */ jsx("div", {
1091
+ className: `p-6 ${className}`,
1092
+ children
1093
+ });
1094
+ };
1095
+ const DialogHeader = ({ children }) => {
1096
+ return /* @__PURE__ */ jsx("div", {
1097
+ className: "mb-4",
1098
+ children
1099
+ });
1100
+ };
1101
+ const DialogTitle = ({ children }) => {
1102
+ return /* @__PURE__ */ jsx("h2", {
1103
+ className: "text-xl font-semibold",
1104
+ children
1105
+ });
1106
+ };
1107
+ /**
1108
+ * Modal wrapper for CMSMDXEditor with integrated Media Library image picker.
1109
+ * Use this instead of MDXEditorModal from @conloca/mdx when you need the image picker.
1110
+ */
1111
+ const CMSMDXEditorModal = ({ isOpen, onClose, filePath, initialContent, onSave, headerExtra, onBeforeClose }) => {
1112
+ const [content, setContent] = useState(initialContent);
1113
+ const [isEditorReady, setIsEditorReady] = useState(false);
1114
+ const [isSaving, setIsSaving] = useState(false);
1115
+ useEffect(() => {
1116
+ setContent(initialContent);
1117
+ }, [initialContent]);
1118
+ const handleClose = () => {
1119
+ if (onBeforeClose && !onBeforeClose()) return;
1120
+ onClose();
1121
+ };
1122
+ useEffect(() => {
1123
+ if (isOpen) {
1124
+ const timer = setTimeout(() => {
1125
+ setIsEditorReady(true);
1126
+ }, 0);
1127
+ return () => clearTimeout(timer);
1128
+ }
1129
+ setIsEditorReady(false);
1130
+ }, [isOpen]);
1131
+ const handleSave = async () => {
1132
+ setIsSaving(true);
1133
+ try {
1134
+ await onSave(content);
1135
+ handleClose();
1136
+ } catch (error) {
1137
+ console.error("Save failed:", error);
1138
+ } finally {
1139
+ setIsSaving(false);
1140
+ }
1141
+ };
1142
+ if (!isOpen) return null;
1143
+ return /* @__PURE__ */ jsx(Dialog, {
1144
+ open: isOpen,
1145
+ onOpenChange: handleClose,
1146
+ children: /* @__PURE__ */ jsxs(DialogContent, {
1147
+ className: "w-[90vw] h-[90vh] flex flex-col",
1148
+ "data-testid": "mdx-editor-modal",
1149
+ children: [
1150
+ /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsxs("div", {
1151
+ className: "flex items-center justify-between gap-4",
1152
+ children: [
1153
+ /* @__PURE__ */ jsx(DialogTitle, { children: filePath ? `Edit: ${filePath}` : "New MDX File" }),
1154
+ headerExtra && /* @__PURE__ */ jsx("div", {
1155
+ className: "flex items-center gap-2",
1156
+ children: headerExtra
1157
+ }),
1158
+ /* @__PURE__ */ jsx("button", {
1159
+ onClick: handleClose,
1160
+ className: "p-2 hover:bg-gray-100 rounded",
1161
+ "aria-label": "Close",
1162
+ children: "✕"
1163
+ })
1164
+ ]
1165
+ }) }),
1166
+ /* @__PURE__ */ jsx("div", {
1167
+ className: "flex-1 overflow-hidden",
1168
+ children: isEditorReady ? /* @__PURE__ */ jsx(MDXEditorErrorBoundary, { children: /* @__PURE__ */ jsx(DeferredCMSMDXEditor, {
1169
+ value: content,
1170
+ onChange: setContent,
1171
+ onSave: handleSave
1172
+ }) }) : /* @__PURE__ */ jsx("div", {
1173
+ className: "flex items-center justify-center h-full",
1174
+ children: /* @__PURE__ */ jsx("div", {
1175
+ className: "text-gray-500",
1176
+ "data-testid": "modal-editor-loading",
1177
+ children: "Loading editor..."
1178
+ })
1179
+ })
1180
+ }),
1181
+ /* @__PURE__ */ jsxs("div", {
1182
+ className: "flex justify-end gap-2 mt-4",
1183
+ children: [/* @__PURE__ */ jsx("button", {
1184
+ onClick: handleClose,
1185
+ className: "px-4 py-2 border border-gray-300 rounded hover:bg-gray-50",
1186
+ disabled: isSaving,
1187
+ children: "Cancel"
1188
+ }), /* @__PURE__ */ jsx("button", {
1189
+ onClick: handleSave,
1190
+ className: "px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed",
1191
+ disabled: isSaving,
1192
+ children: isSaving ? "Saving..." : "Save"
1193
+ })]
1194
+ })
1195
+ ]
1196
+ })
1197
+ });
1198
+ };
1199
+
1200
+ //#endregion
1201
+ //#region src/components/puck/MDXContent.tsx
1202
+ /**
1203
+ * Error Boundary for catching React errors in MDXContent rendering.
1204
+ */
1205
+ var MDXContentErrorBoundary = class extends React.Component {
1206
+ constructor(props) {
1207
+ super(props);
1208
+ this.state = { hasError: false };
1209
+ }
1210
+ static getDerivedStateFromError(error) {
1211
+ return {
1212
+ hasError: true,
1213
+ error
1214
+ };
1215
+ }
1216
+ componentDidCatch(error, errorInfo) {
1217
+ console.error("[MDXContent] Error boundary caught:", error, errorInfo);
1218
+ }
1219
+ render() {
1220
+ if (this.state.hasError) return /* @__PURE__ */ jsxs("div", {
1221
+ className: "p-4 bg-red-50 border border-red-200 rounded text-red-600",
1222
+ children: [
1223
+ /* @__PURE__ */ jsx("p", {
1224
+ className: "font-semibold mb-2",
1225
+ children: "MDXContent Rendering Error"
1226
+ }),
1227
+ /* @__PURE__ */ jsx("p", {
1228
+ className: "text-sm mb-2",
1229
+ children: this.state.error?.message || "Unknown error occurred"
1230
+ }),
1231
+ /* @__PURE__ */ jsxs("p", {
1232
+ className: "text-xs text-red-500 mb-2",
1233
+ children: ["Block ID: ", this.props.contentId]
1234
+ }),
1235
+ /* @__PURE__ */ jsxs("details", {
1236
+ className: "text-xs",
1237
+ children: [/* @__PURE__ */ jsx("summary", {
1238
+ className: "cursor-pointer font-medium",
1239
+ children: "Stack Trace"
1240
+ }), /* @__PURE__ */ jsx("pre", {
1241
+ className: "mt-2 p-2 bg-red-100 rounded overflow-auto whitespace-pre-wrap",
1242
+ children: this.state.error?.stack
1243
+ })]
1244
+ })
1245
+ ]
1246
+ });
1247
+ return this.props.children;
1248
+ }
1249
+ };
1250
+ /**
1251
+ * Component for rendering MDX content blocks in the CMS editor.
1252
+ *
1253
+ * Receives a LocalizedEntry and renders its MDX content using the useMDXEvaluation hook.
1254
+ * Displays appropriate loading, error, and empty states.
1255
+ *
1256
+ * @param props - Component props
1257
+ * @param props.entry - LocalizedEntry containing the content and ETag
1258
+ */
1259
+ function MDXContentInner({ entry }) {
1260
+ const actualMdxContent = useMemo(() => {
1261
+ if (!entry?.localized?.content) return null;
1262
+ return entry.localized.content.mdx || null;
1263
+ }, [entry]);
1264
+ const { Component, error: compileError, isLoading: isCompiling, retry } = useMDXEvaluation({
1265
+ mdxContent: actualMdxContent,
1266
+ cacheKey: entry.localized.etag
1267
+ });
1268
+ if (isCompiling) return /* @__PURE__ */ jsxs("div", {
1269
+ className: "flex items-center justify-center p-8 text-grey-04",
1270
+ children: [/* @__PURE__ */ jsx("div", { className: "animate-spin rounded-full h-6 w-6 border-b-2 border-azure-04 mr-3" }), "Loading content..."]
1271
+ });
1272
+ if (compileError) return /* @__PURE__ */ jsxs("div", {
1273
+ className: "p-4 bg-red-50 border border-red-200 rounded text-red-600",
1274
+ children: [
1275
+ /* @__PURE__ */ jsx("p", {
1276
+ className: "font-semibold mb-2",
1277
+ children: "Cannot Render Block Content"
1278
+ }),
1279
+ /* @__PURE__ */ jsx("p", {
1280
+ className: "text-sm mb-2",
1281
+ children: compileError.message
1282
+ }),
1283
+ /* @__PURE__ */ jsx("p", {
1284
+ className: "text-xs text-red-500 mb-3",
1285
+ children: "The block content contains invalid MDX syntax. Please edit the block to fix any syntax errors."
1286
+ }),
1287
+ /* @__PURE__ */ jsx("button", {
1288
+ onClick: retry,
1289
+ className: "px-3 py-1 text-sm bg-red-600 text-white rounded hover:bg-red-700 transition-colors",
1290
+ children: "Retry"
1291
+ })
1292
+ ]
1293
+ });
1294
+ if (!actualMdxContent) return /* @__PURE__ */ jsxs("div", {
1295
+ className: "p-4 bg-yellow-50 border border-yellow-200 rounded text-yellow-700",
1296
+ children: [/* @__PURE__ */ jsx("p", {
1297
+ className: "font-semibold mb-1",
1298
+ children: "Empty Block Content"
1299
+ }), /* @__PURE__ */ jsxs("p", {
1300
+ className: "text-sm",
1301
+ children: [
1302
+ "This block (",
1303
+ entry.id,
1304
+ ") doesn't have any content yet. Click to edit and add some MDX content."
1305
+ ]
1306
+ })]
1307
+ });
1308
+ if (!Component) return /* @__PURE__ */ jsxs("div", {
1309
+ className: "flex items-center justify-center p-8 text-grey-04",
1310
+ children: [/* @__PURE__ */ jsx("div", { className: "animate-spin rounded-full h-6 w-6 border-b-2 border-azure-04 mr-3" }), "Loading MDX..."]
1311
+ });
1312
+ return /* @__PURE__ */ jsx("div", {
1313
+ className: "mdx-content prose prose-sm max-w-none",
1314
+ children: /* @__PURE__ */ jsx(Component, {})
1315
+ });
1316
+ }
1317
+ /**
1318
+ * MDXContent component wrapped with error boundary.
1319
+ */
1320
+ function MDXContent(props) {
1321
+ return /* @__PURE__ */ jsx(MDXContentErrorBoundary, {
1322
+ contentId: props.entry.id,
1323
+ children: /* @__PURE__ */ jsx(MDXContentInner, { ...props })
1324
+ });
1325
+ }
1326
+
1327
+ //#endregion
1328
+ export { CMSMDXEditorModal, FolderTreeSidebar, MDXContent, MediaLibrary, UploadModal, configureUI, setDataSchemas };
1329
+ //# sourceMappingURL=index.mjs.map