@cmssy/cli 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1758 @@
1
+ import { execSync } from "child_process";
2
+ import fs from "fs-extra";
3
+ import path from "path";
4
+ const DEV_DIR = ".cmssy/dev";
5
+ /**
6
+ * Generate the .cmssy/dev/ Next.js app structure for cmssy dev.
7
+ * This creates a minimal Next.js app that imports blocks directly,
8
+ * enabling "use client" boundaries, next/image, and SSR in dev preview.
9
+ */
10
+ export function generateDevApp(projectRoot, resources) {
11
+ const devRoot = path.join(projectRoot, DEV_DIR);
12
+ // Clean and recreate
13
+ fs.removeSync(devRoot);
14
+ fs.mkdirSync(path.join(devRoot, "app/preview"), { recursive: true });
15
+ fs.mkdirSync(path.join(devRoot, "app/api/blocks"), { recursive: true });
16
+ fs.mkdirSync(path.join(devRoot, "app/api/preview"), { recursive: true });
17
+ fs.mkdirSync(path.join(devRoot, "app/api/workspaces"), { recursive: true });
18
+ // Generate all files
19
+ generateNextConfig(devRoot, projectRoot);
20
+ generateTsConfig(devRoot, projectRoot);
21
+ generateRootLayout(devRoot);
22
+ generateGlobalsCss(devRoot, projectRoot);
23
+ generateHomePage(devRoot);
24
+ generateBlocksApiRoute(devRoot);
25
+ generateBlockConfigApiRoute(devRoot);
26
+ generatePreviewApiRoute(devRoot);
27
+ generateWorkspacesApiRoute(devRoot);
28
+ generatePreviewPages(devRoot, projectRoot, resources);
29
+ return devRoot;
30
+ }
31
+ /**
32
+ * Regenerate only the preview pages (called when new blocks are detected).
33
+ */
34
+ export function regeneratePreviewPages(projectRoot, resources) {
35
+ const devRoot = path.join(projectRoot, DEV_DIR);
36
+ const previewDir = path.join(devRoot, "app/preview");
37
+ // Remove old preview pages
38
+ if (fs.existsSync(previewDir)) {
39
+ fs.removeSync(previewDir);
40
+ }
41
+ fs.mkdirSync(previewDir, { recursive: true });
42
+ generatePreviewPages(devRoot, projectRoot, resources);
43
+ }
44
+ // ============================================================================
45
+ // File Generators
46
+ // ============================================================================
47
+ function generateNextConfig(devRoot, projectRoot) {
48
+ const rel = path.relative(devRoot, projectRoot);
49
+ const content = `import { resolve, dirname } from "path";
50
+ import { fileURLToPath } from "url";
51
+
52
+ const __dirname = dirname(fileURLToPath(import.meta.url));
53
+
54
+ /** @type {import('next').NextConfig} */
55
+ const nextConfig = {
56
+ turbopack: {
57
+ root: resolve(__dirname, "${rel}"),
58
+ },
59
+
60
+ allowedDevOrigins: ['*'],
61
+
62
+ images: {
63
+ remotePatterns: [{ protocol: 'https', hostname: '**' }],
64
+ },
65
+ };
66
+
67
+ export default nextConfig;
68
+ `;
69
+ fs.writeFileSync(path.join(devRoot, "next.config.mjs"), content);
70
+ }
71
+ function generateTsConfig(devRoot, projectRoot) {
72
+ // Paths must be relative to tsconfig location (.cmssy/dev/)
73
+ const rel = path.relative(devRoot, projectRoot);
74
+ // Read project tsconfig to forward user-defined path aliases and includes
75
+ const projectTsConfigPath = path.join(projectRoot, "tsconfig.json");
76
+ let userPaths = {};
77
+ let userIncludes = [];
78
+ if (fs.existsSync(projectTsConfigPath)) {
79
+ try {
80
+ const projectTsConfig = JSON.parse(fs.readFileSync(projectTsConfigPath, "utf-8"));
81
+ const rawPaths = projectTsConfig.compilerOptions?.paths || {};
82
+ // Re-map user paths relative to .cmssy/dev/ (project tsconfig uses baseUrl: ".")
83
+ for (const [alias, targets] of Object.entries(rawPaths)) {
84
+ // Skip cmssy-cli/config — we handle it ourselves
85
+ if (alias === "cmssy-cli/config")
86
+ continue;
87
+ userPaths[alias] = targets.map((t) => `${rel}/${t}`);
88
+ }
89
+ // Re-map user includes relative to .cmssy/dev/
90
+ const rawIncludes = projectTsConfig.include || [];
91
+ userIncludes = rawIncludes.map((inc) => `${rel}/${inc}`);
92
+ }
93
+ catch {
94
+ // Ignore parse errors — fall back to defaults
95
+ }
96
+ }
97
+ const tsConfig = {
98
+ compilerOptions: {
99
+ target: "ES2020",
100
+ lib: ["dom", "dom.iterable", "esnext"],
101
+ allowJs: true,
102
+ skipLibCheck: true,
103
+ strict: true,
104
+ noEmit: true,
105
+ esModuleInterop: true,
106
+ module: "esnext",
107
+ moduleResolution: "bundler",
108
+ resolveJsonModule: true,
109
+ isolatedModules: true,
110
+ jsx: "preserve",
111
+ incremental: true,
112
+ plugins: [{ name: "next" }],
113
+ paths: {
114
+ // User-defined aliases from project tsconfig (e.g. @/* for shadcn)
115
+ ...userPaths,
116
+ // Cmssy built-in aliases (override user if conflicting)
117
+ "@blocks/*": [`${rel}/blocks/*`],
118
+ "@templates/*": [`${rel}/templates/*`],
119
+ "@styles/*": [`${rel}/styles/*`],
120
+ "@lib/*": [`${rel}/lib/*`],
121
+ "cmssy-cli/config": [`${rel}/node_modules/cmssy-cli/config`],
122
+ },
123
+ },
124
+ include: [
125
+ "next-env.d.ts",
126
+ "**/*.ts",
127
+ "**/*.tsx",
128
+ ".next/types/**/*.ts",
129
+ `${rel}/blocks/**/*.ts`,
130
+ `${rel}/blocks/**/*.tsx`,
131
+ `${rel}/templates/**/*.ts`,
132
+ `${rel}/templates/**/*.tsx`,
133
+ // User-defined includes from project tsconfig (e.g. components/**/*, lib/**/*)
134
+ ...userIncludes,
135
+ ],
136
+ exclude: ["node_modules"],
137
+ };
138
+ fs.writeFileSync(path.join(devRoot, "tsconfig.json"), JSON.stringify(tsConfig, null, 2) + "\n");
139
+ }
140
+ function generateRootLayout(devRoot) {
141
+ const content = `import type { Metadata } from "next";
142
+ import "./globals.css";
143
+
144
+ export const metadata: Metadata = {
145
+ title: "Cmssy Dev Server",
146
+ icons: {
147
+ icon: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' rx='20' fill='%23667eea'/%3E%3Ctext x='50' y='70' font-size='60' font-weight='bold' text-anchor='middle' fill='white' font-family='system-ui'%3EC%3C/text%3E%3C/svg%3E",
148
+ },
149
+ };
150
+
151
+ export default function RootLayout({
152
+ children,
153
+ }: {
154
+ children: React.ReactNode;
155
+ }) {
156
+ return (
157
+ <html lang="en">
158
+ <body>{children}</body>
159
+ </html>
160
+ );
161
+ }
162
+ `;
163
+ fs.writeFileSync(path.join(devRoot, "app/layout.tsx"), content);
164
+ }
165
+ function generateGlobalsCss(devRoot, projectRoot) {
166
+ const rel = path.relative(path.join(devRoot, "app"), projectRoot);
167
+ // Check for project CSS files that contain Tailwind / theme
168
+ const cssFiles = ["styles/main.css", "styles/globals.css", "app/globals.css"];
169
+ const projectCssFile = cssFiles.find((f) => fs.existsSync(path.join(projectRoot, f)));
170
+ // Import the project's main CSS (Tailwind + theme) if it exists
171
+ const projectCssImport = projectCssFile
172
+ ? `@import "${rel}/${projectCssFile}";\n\n`
173
+ : "";
174
+ const content = `${projectCssImport}*,
175
+ *::before,
176
+ *::after {
177
+ box-sizing: border-box;
178
+ }
179
+
180
+ html,
181
+ body {
182
+ height: 100%;
183
+ margin: 0;
184
+ padding: 0;
185
+ }
186
+
187
+ body {
188
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
189
+ background: #f5f5f5;
190
+ line-height: 1.5;
191
+ -webkit-font-smoothing: antialiased;
192
+ }
193
+ `;
194
+ fs.writeFileSync(path.join(devRoot, "app/globals.css"), content);
195
+ }
196
+ function generateHomePage(devRoot) {
197
+ // The home page embeds the dev UI as a client component
198
+ // It uses an iframe-based architecture: left sidebar (block list),
199
+ // center (preview iframe), right sidebar (editor)
200
+ const content = `"use client";
201
+
202
+ import { useState, useEffect, useCallback, useRef } from "react";
203
+
204
+ interface Block {
205
+ type: "block" | "template";
206
+ name: string;
207
+ displayName: string;
208
+ description?: string;
209
+ category?: string;
210
+ tags?: string[];
211
+ version: string;
212
+ hasConfig?: boolean;
213
+ schema?: Record<string, any>;
214
+ pages?: Array<{ name: string; slug: string; blocksCount: number }>;
215
+ layoutPositions?: Array<{ position: string; type: string }>;
216
+ }
217
+
218
+ export default function DevHome() {
219
+ const [blocks, setBlocks] = useState<Block[]>([]);
220
+ const [selected, setSelected] = useState<Block | null>(null);
221
+ const [previewData, setPreviewData] = useState<Record<string, unknown>>({});
222
+ const [configLoading, setConfigLoading] = useState(false);
223
+ const configDataRef = useRef<Record<string, unknown>>({});
224
+ const iframeRef = useRef<HTMLIFrameElement>(null);
225
+ const iframeLoadedRef = useRef(false);
226
+ const [isDirty, setIsDirty] = useState(false);
227
+ const [showBlockList, setShowBlockList] = useState(true);
228
+ const [showEditor, setShowEditor] = useState(true);
229
+
230
+ // Load blocks list
231
+ useEffect(() => {
232
+ fetch("/api/blocks")
233
+ .then((r) => r.json())
234
+ .then(setBlocks)
235
+ .catch(console.error);
236
+ }, []);
237
+
238
+ // Load config when block selected
239
+ useEffect(() => {
240
+ if (!selected || selected.type === "template") return;
241
+ setConfigLoading(true);
242
+ fetch(\`/api/blocks/\${selected.name}/config\`)
243
+ .then((r) => r.json())
244
+ .then((config) => {
245
+ setSelected((prev) =>
246
+ prev ? { ...prev, schema: config.schema } : null
247
+ );
248
+ const data = config.previewData || {};
249
+ configDataRef.current = data;
250
+ setPreviewData(data);
251
+ setConfigLoading(false);
252
+ })
253
+ .catch(() => setConfigLoading(false));
254
+ }, [selected?.name]);
255
+
256
+ // Select block — templates redirect to full-page preview
257
+ const handleSelect = useCallback((block: Block) => {
258
+ if (block.type === "template") {
259
+ window.location.href = \`/preview/\${block.name}\`;
260
+ return;
261
+ }
262
+ setSelected(block);
263
+ setPreviewData({});
264
+ configDataRef.current = {};
265
+ setIsDirty(false);
266
+ iframeLoadedRef.current = false;
267
+ }, []);
268
+
269
+ // Send props to iframe
270
+ useEffect(() => {
271
+ if (!iframeRef.current?.contentWindow || !iframeLoadedRef.current) return;
272
+ if (!previewData || Object.keys(previewData).length === 0) return;
273
+ iframeRef.current.contentWindow.postMessage(
274
+ { type: "UPDATE_PROPS", props: previewData },
275
+ "*"
276
+ );
277
+ }, [previewData]);
278
+
279
+ // Auto-save preview data
280
+ useEffect(() => {
281
+ if (!isDirty || !selected || Object.keys(configDataRef.current).length === 0) return;
282
+ const t = setTimeout(async () => {
283
+ const dataToSave = { ...configDataRef.current, ...previewData };
284
+ await fetch(\`/api/preview/\${selected.name}\`, {
285
+ method: "POST",
286
+ headers: { "Content-Type": "application/json" },
287
+ body: JSON.stringify(dataToSave),
288
+ });
289
+ setIsDirty(false);
290
+ }, 500);
291
+ return () => clearTimeout(t);
292
+ }, [previewData, selected, isDirty]);
293
+
294
+ // URL state
295
+ useEffect(() => {
296
+ if (blocks.length === 0) return;
297
+ const params = new URLSearchParams(window.location.search);
298
+ const name = params.get("block") || params.get("template");
299
+ if (name) {
300
+ const b = blocks.find((x) => x.name === name);
301
+ if (b) handleSelect(b);
302
+ }
303
+ }, [blocks, handleSelect]);
304
+
305
+ const previewUrl = selected ? \`/preview/\${selected.name}\` : null;
306
+
307
+ function renderField(field: any, value: any, onChange: (val: any) => void) {
308
+ if (field.type === "multiLine" || field.type === "richText") {
309
+ return (
310
+ <textarea
311
+ value={(value as string) || ""}
312
+ onChange={(e) => onChange(e.target.value)}
313
+ placeholder={field.placeholder}
314
+ style={{ width: "100%", padding: "8px", border: "1px solid #ddd", borderRadius: "4px", fontSize: "13px", minHeight: "60px", resize: "vertical", fontFamily: "inherit" }}
315
+ />
316
+ );
317
+ }
318
+ if (field.type === "boolean") {
319
+ return (
320
+ <label style={{ display: "flex", alignItems: "center", gap: "8px", cursor: "pointer" }}>
321
+ <input
322
+ type="checkbox"
323
+ checked={!!value}
324
+ onChange={(e) => onChange(e.target.checked)}
325
+ />
326
+ {field.label}
327
+ </label>
328
+ );
329
+ }
330
+ if (field.type === "select") {
331
+ return (
332
+ <select
333
+ value={(value as string) || ""}
334
+ onChange={(e) => onChange(e.target.value)}
335
+ style={{ width: "100%", padding: "8px", border: "1px solid #ddd", borderRadius: "4px", fontSize: "13px" }}
336
+ >
337
+ <option value="">Select...</option>
338
+ {field.options?.map((opt: any) => (
339
+ <option key={opt.value} value={opt.value}>{opt.label}</option>
340
+ ))}
341
+ </select>
342
+ );
343
+ }
344
+ if (field.type === "multiselect") {
345
+ const selected = Array.isArray(value) ? value : [];
346
+ return (
347
+ <div style={{ border: "1px solid #ddd", borderRadius: "4px", padding: "8px", display: "flex", flexWrap: "wrap", gap: "6px" }}>
348
+ {field.options?.map((opt: any) => (
349
+ <label key={opt.value} style={{ display: "flex", alignItems: "center", gap: "4px", fontSize: "13px", cursor: "pointer" }}>
350
+ <input
351
+ type="checkbox"
352
+ checked={selected.includes(opt.value)}
353
+ onChange={(e) => {
354
+ const next = e.target.checked
355
+ ? [...selected, opt.value]
356
+ : selected.filter((v: string) => v !== opt.value);
357
+ onChange(next);
358
+ }}
359
+ />
360
+ {opt.label}
361
+ </label>
362
+ ))}
363
+ {!field.options?.length && <span style={{ color: "#999", fontSize: "12px" }}>No options defined</span>}
364
+ </div>
365
+ );
366
+ }
367
+ if (field.type === "date") {
368
+ return (
369
+ <input
370
+ type="date"
371
+ value={(value as string) || ""}
372
+ onChange={(e) => onChange(e.target.value)}
373
+ style={{ width: "100%", padding: "8px", border: "1px solid #ddd", borderRadius: "4px", fontSize: "13px" }}
374
+ />
375
+ );
376
+ }
377
+ if (field.type === "slider") {
378
+ const min = field.min ?? 0;
379
+ const max = field.max ?? 100;
380
+ const step = field.step ?? 1;
381
+ return (
382
+ <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
383
+ <input
384
+ type="range"
385
+ min={min}
386
+ max={max}
387
+ step={step}
388
+ value={value ?? min}
389
+ onChange={(e) => onChange(Number(e.target.value))}
390
+ style={{ flex: 1 }}
391
+ />
392
+ <span style={{ fontSize: "13px", fontWeight: 500, minWidth: "32px", textAlign: "right" }}>{value ?? min}</span>
393
+ </div>
394
+ );
395
+ }
396
+ if (field.type === "media") {
397
+ return (
398
+ <div>
399
+ {value && (
400
+ <div style={{ marginBottom: "6px", borderRadius: "4px", overflow: "hidden", border: "1px solid #ddd" }}>
401
+ <img src={value as string} alt="" style={{ maxWidth: "100%", maxHeight: "120px", objectFit: "contain", display: "block" }} />
402
+ </div>
403
+ )}
404
+ <input
405
+ type="text"
406
+ value={(value as string) || ""}
407
+ onChange={(e) => onChange(e.target.value)}
408
+ placeholder="Image URL"
409
+ style={{ width: "100%", padding: "8px", border: "1px solid #ddd", borderRadius: "4px", fontSize: "13px" }}
410
+ />
411
+ </div>
412
+ );
413
+ }
414
+ if (field.type === "repeater" && field.schema) {
415
+ const items = (Array.isArray(value) ? value : []) as any[];
416
+ return (
417
+ <div style={{ border: "1px solid #ddd", borderRadius: "6px", overflow: "hidden" }}>
418
+ {items.map((item: any, idx: number) => (
419
+ <div key={idx} style={{ padding: "12px", borderBottom: "1px solid #eee", background: idx % 2 === 0 ? "#fafafa" : "#fff" }}>
420
+ <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: "8px" }}>
421
+ <span style={{ fontSize: "12px", fontWeight: 600, color: "#888" }}>#{idx + 1}</span>
422
+ <button
423
+ type="button"
424
+ onClick={() => {
425
+ const newItems = [...items];
426
+ newItems.splice(idx, 1);
427
+ onChange(newItems);
428
+ }}
429
+ style={{ fontSize: "11px", color: "#e53935", background: "none", border: "none", cursor: "pointer" }}
430
+ >&times; Remove</button>
431
+ </div>
432
+ {Object.entries(field.schema).map(([subKey, subField]: [string, any]) => (
433
+ <div key={subKey} style={{ marginBottom: "8px" }}>
434
+ <label style={{ display: "block", fontSize: "11px", fontWeight: 500, marginBottom: "4px", color: "#666" }}>
435
+ {subField.label || subKey}
436
+ </label>
437
+ {renderField(subField, item[subKey], (subVal) => {
438
+ const newItems = [...items];
439
+ newItems[idx] = { ...newItems[idx], [subKey]: subVal };
440
+ onChange(newItems);
441
+ })}
442
+ </div>
443
+ ))}
444
+ </div>
445
+ ))}
446
+ <button
447
+ type="button"
448
+ onClick={() => {
449
+ const newItem: any = {};
450
+ Object.entries(field.schema).forEach(([k, f]: [string, any]) => {
451
+ newItem[k] = (f as any).type === "repeater" ? [] : "";
452
+ });
453
+ onChange([...items, newItem]);
454
+ }}
455
+ style={{ width: "100%", padding: "10px", fontSize: "13px", color: "#667eea", background: "none", border: "none", cursor: "pointer", fontWeight: 500 }}
456
+ >+ Add item</button>
457
+ </div>
458
+ );
459
+ }
460
+ // singleLine, link, numeric, color, form, emailTemplate, emailConfiguration, pageSelector
461
+ return (
462
+ <input
463
+ type={field.type === "numeric" ? "number" : field.type === "color" ? "color" : field.type === "link" ? "url" : "text"}
464
+ value={(value as string) || ""}
465
+ onChange={(e) => onChange(field.type === "numeric" ? Number(e.target.value) : e.target.value)}
466
+ placeholder={field.placeholder || (field.type === "link" ? "https://..." : field.type === "form" || field.type === "emailTemplate" || field.type === "emailConfiguration" || field.type === "pageSelector" ? "Enter ID..." : "")}
467
+ style={{ width: "100%", padding: "8px", border: "1px solid #ddd", borderRadius: "4px", fontSize: "13px" }}
468
+ />
469
+ );
470
+ }
471
+
472
+ return (
473
+ <div style={{ display: "grid", gridTemplateColumns: \`\${showBlockList ? "280px" : "0px"} 1fr \${showEditor ? "400px" : "0px"}\`, height: "100vh", transition: "grid-template-columns 0.2s ease" }}>
474
+ {/* Block List */}
475
+ <div style={{ background: "#fff", borderRight: showBlockList ? "1px solid #e0e0e0" : "none", overflow: showBlockList ? "auto" : "hidden", width: showBlockList ? "auto" : 0 }}>
476
+ <div style={{ padding: "16px", borderBottom: "1px solid #e0e0e0", background: "#fafafa" }}>
477
+ <h1 style={{ fontSize: "18px", fontWeight: 600, margin: 0 }}>Cmssy Dev</h1>
478
+ <p style={{ fontSize: "13px", color: "#666", margin: "4px 0 0" }}>
479
+ {blocks.length} blocks
480
+ </p>
481
+ </div>
482
+ <div style={{ padding: "12px" }}>
483
+ {blocks.map((b) => (
484
+ <div
485
+ key={b.name}
486
+ onClick={() => {
487
+ handleSelect(b);
488
+ const url = new URL(window.location.href);
489
+ url.searchParams.set(b.type === "template" ? "template" : "block", b.name);
490
+ window.history.replaceState({}, "", url.toString());
491
+ }}
492
+ style={{
493
+ padding: "12px 16px",
494
+ marginBottom: "4px",
495
+ borderRadius: "8px",
496
+ cursor: "pointer",
497
+ background: selected?.name === b.name ? "#667eea" : "transparent",
498
+ color: selected?.name === b.name ? "white" : "inherit",
499
+ }}
500
+ >
501
+ <div style={{ fontSize: "14px", fontWeight: 500 }}>{b.displayName}</div>
502
+ <div style={{ fontSize: "12px", opacity: 0.7 }}>
503
+ {b.type} &middot; v{b.version}
504
+ </div>
505
+ </div>
506
+ ))}
507
+ </div>
508
+ </div>
509
+
510
+ {/* Preview */}
511
+ <div style={{ background: "#fafafa", display: "flex", flexDirection: "column" }}>
512
+ <div style={{ padding: "10px 16px", background: "white", borderBottom: "1px solid #e0e0e0", display: "flex", alignItems: "center", justifyContent: "space-between", gap: "12px" }}>
513
+ <button
514
+ type="button"
515
+ onClick={() => setShowBlockList(!showBlockList)}
516
+ title={showBlockList ? "Hide block list" : "Show block list"}
517
+ style={{ background: showBlockList ? "#f0f0f0" : "#667eea", color: showBlockList ? "#333" : "#fff", border: "1px solid #ddd", borderRadius: "6px", padding: "6px 10px", cursor: "pointer", fontSize: "13px", fontWeight: 500, whiteSpace: "nowrap" }}
518
+ >{showBlockList ? "\u2190 Blocks" : "\u2192 Blocks"}</button>
519
+ <div style={{ flex: 1, fontSize: "16px", fontWeight: 600, textAlign: "center", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
520
+ {selected?.displayName || "Preview"}
521
+ </div>
522
+ <button
523
+ type="button"
524
+ onClick={() => setShowEditor(!showEditor)}
525
+ title={showEditor ? "Hide editor" : "Show editor"}
526
+ style={{ background: showEditor ? "#f0f0f0" : "#667eea", color: showEditor ? "#333" : "#fff", border: "1px solid #ddd", borderRadius: "6px", padding: "6px 10px", cursor: "pointer", fontSize: "13px", fontWeight: 500, whiteSpace: "nowrap" }}
527
+ >{showEditor ? "Editor \u2192" : "Editor \u2190"}</button>
528
+ </div>
529
+ <div style={{ flex: 1, padding: "24px", display: "flex", alignItems: "center", justifyContent: "center" }}>
530
+ {previewUrl ? (
531
+ <div style={{ width: "100%", height: "100%", background: "white", borderRadius: "12px", boxShadow: "0 2px 8px rgba(0,0,0,0.1)", overflow: "hidden" }}>
532
+ <iframe
533
+ ref={iframeRef}
534
+ src={previewUrl}
535
+ key={previewUrl}
536
+ onLoad={() => { iframeLoadedRef.current = true; }}
537
+ style={{ width: "100%", height: "100%", border: "none" }}
538
+ />
539
+ </div>
540
+ ) : (
541
+ <div style={{ textAlign: "center", color: "#999" }}>
542
+ <p>Select a block to preview</p>
543
+ </div>
544
+ )}
545
+ </div>
546
+ </div>
547
+
548
+ {/* Editor */}
549
+ <div style={{ background: "#fff", borderLeft: showEditor ? "1px solid #e0e0e0" : "none", overflow: showEditor ? "auto" : "hidden", width: showEditor ? "auto" : 0 }}>
550
+ <div style={{ padding: "16px", borderBottom: "1px solid #e0e0e0", background: "#fafafa" }}>
551
+ <h2 style={{ fontSize: "16px", fontWeight: 600, margin: 0 }}>Editor</h2>
552
+ </div>
553
+ <div style={{ padding: "20px" }}>
554
+ {!selected && <p style={{ color: "#999" }}>Select a block to edit</p>}
555
+ {selected && configLoading && <p style={{ color: "#999" }}>Loading...</p>}
556
+ {selected && !configLoading && selected.schema && (
557
+ <div>
558
+ {Object.entries(selected.schema).map(([key, field]: [string, any]) => (
559
+ <div key={key} style={{ marginBottom: "20px" }}>
560
+ <label style={{ display: "block", fontSize: "13px", fontWeight: 500, marginBottom: "6px" }}>
561
+ {field.label || key}
562
+ {field.required && <span style={{ color: "#e53935" }}> *</span>}
563
+ </label>
564
+ {renderField(field, previewData[key], (val) => {
565
+ setPreviewData({ ...previewData, [key]: val });
566
+ setIsDirty(true);
567
+ })}
568
+ {field.helpText && (
569
+ <div style={{ fontSize: "12px", color: "#666", marginTop: "4px" }}>{field.helpText}</div>
570
+ )}
571
+ </div>
572
+ ))}
573
+ </div>
574
+ )}
575
+ </div>
576
+ </div>
577
+ </div>
578
+ );
579
+ }
580
+ `;
581
+ fs.writeFileSync(path.join(devRoot, "app/page.tsx"), content);
582
+ }
583
+ function generateBlocksApiRoute(devRoot) {
584
+ const content = `import { NextResponse } from "next/server";
585
+ import fs from "fs";
586
+ import path from "path";
587
+
588
+ // Project root is passed via env var set by cmssy dev
589
+ const projectRoot = process.env.CMSSY_PROJECT_ROOT || process.cwd();
590
+
591
+ export async function GET() {
592
+ const blocks: any[] = [];
593
+
594
+ // Scan blocks/
595
+ const blocksDir = path.join(projectRoot, "blocks");
596
+ if (fs.existsSync(blocksDir)) {
597
+ const dirs = fs.readdirSync(blocksDir, { withFileTypes: true })
598
+ .filter((d) => d.isDirectory());
599
+
600
+ for (const dir of dirs) {
601
+ const pkgPath = path.join(blocksDir, dir.name, "package.json");
602
+ const pkg = fs.existsSync(pkgPath) ? JSON.parse(fs.readFileSync(pkgPath, "utf-8")) : {};
603
+ blocks.push({
604
+ type: "block",
605
+ name: dir.name,
606
+ displayName: pkg.cmssy?.displayName || dir.name,
607
+ version: pkg.version || "1.0.0",
608
+ category: pkg.cmssy?.category || "other",
609
+ tags: pkg.cmssy?.tags || [],
610
+ description: pkg.description || "",
611
+ hasConfig: fs.existsSync(path.join(blocksDir, dir.name, "block.config.ts")),
612
+ });
613
+ }
614
+ }
615
+
616
+ // Scan templates/
617
+ const templatesDir = path.join(projectRoot, "templates");
618
+ if (fs.existsSync(templatesDir)) {
619
+ const dirs = fs.readdirSync(templatesDir, { withFileTypes: true })
620
+ .filter((d) => d.isDirectory());
621
+
622
+ for (const dir of dirs) {
623
+ const pkgPath = path.join(templatesDir, dir.name, "package.json");
624
+ const pkg = fs.existsSync(pkgPath) ? JSON.parse(fs.readFileSync(pkgPath, "utf-8")) : {};
625
+ blocks.push({
626
+ type: "template",
627
+ name: dir.name,
628
+ displayName: pkg.cmssy?.displayName || dir.name,
629
+ version: pkg.version || "1.0.0",
630
+ category: pkg.cmssy?.category || "pages",
631
+ tags: pkg.cmssy?.tags || [],
632
+ description: pkg.description || "",
633
+ hasConfig: fs.existsSync(path.join(templatesDir, dir.name, "block.config.ts")),
634
+ });
635
+ }
636
+ }
637
+
638
+ return NextResponse.json(blocks);
639
+ }
640
+ `;
641
+ fs.mkdirSync(path.join(devRoot, "app/api/blocks"), { recursive: true });
642
+ fs.writeFileSync(path.join(devRoot, "app/api/blocks/route.ts"), content);
643
+ }
644
+ function generateBlockConfigApiRoute(devRoot) {
645
+ const dir = path.join(devRoot, "app/api/blocks/[name]/config");
646
+ fs.mkdirSync(dir, { recursive: true });
647
+ const content = `import { NextResponse } from "next/server";
648
+ import fs from "fs";
649
+ import path from "path";
650
+ import { execSync } from "child_process";
651
+
652
+ const projectRoot = process.env.CMSSY_PROJECT_ROOT || process.cwd();
653
+
654
+ function loadBlockConfig(blockPath: string): Record<string, unknown> | null {
655
+ const configPath = path.join(blockPath, "block.config.ts");
656
+ if (!fs.existsSync(configPath)) return null;
657
+
658
+ try {
659
+ // Find tsx binary
660
+ const tsxPaths = [
661
+ path.join(projectRoot, "node_modules", ".bin", "tsx"),
662
+ path.join(projectRoot, "node_modules", "cmssy-cli", "node_modules", ".bin", "tsx"),
663
+ ];
664
+ const tsxBinary = tsxPaths.find((p) => fs.existsSync(p)) || "npx -y tsx";
665
+
666
+ // Create mock cmssy-cli/config module
667
+ const cacheDir = path.join(projectRoot, ".cmssy", "cache");
668
+ fs.mkdirSync(cacheDir, { recursive: true });
669
+
670
+ const mockConfigPath = path.join(cacheDir, "cmssy-cli-config.mjs");
671
+ fs.writeFileSync(mockConfigPath,
672
+ "export const defineBlock = (config) => config;\\nexport const defineTemplate = (config) => config;"
673
+ );
674
+
675
+ // Replace import path in config
676
+ const configContent = fs.readFileSync(configPath, "utf-8");
677
+ const modified = configContent.replace(
678
+ /from\\s+['"](?:@cmssy\\/cli\\/config|cmssy-cli\\/config)['"]/g,
679
+ \`from '\${mockConfigPath.replace(/\\\\\\\\/g, "/")}'\`
680
+ );
681
+
682
+ const tempPath = path.join(cacheDir, "temp-block-config.ts");
683
+ fs.writeFileSync(tempPath, modified);
684
+
685
+ const evalCode = \`import cfg from '\${tempPath.replace(/\\\\\\\\/g, "/")}'; console.log(JSON.stringify(cfg.default || cfg));\`;
686
+ const cmd = tsxBinary.includes("npx")
687
+ ? \`\${tsxBinary} --eval "\${evalCode}"\`
688
+ : \`"\${tsxBinary}" --eval "\${evalCode}"\`;
689
+
690
+ const output = execSync(cmd, {
691
+ encoding: "utf-8",
692
+ cwd: projectRoot,
693
+ stdio: ["pipe", "pipe", "pipe"],
694
+ });
695
+
696
+ // Clean up
697
+ try { fs.unlinkSync(tempPath); } catch {}
698
+ try { fs.unlinkSync(mockConfigPath); } catch {}
699
+
700
+ const lines = output.trim().split("\\n");
701
+ return JSON.parse(lines[lines.length - 1]);
702
+ } catch {
703
+ return null;
704
+ }
705
+ }
706
+
707
+ export async function GET(
708
+ _request: Request,
709
+ { params }: { params: Promise<{ name: string }> }
710
+ ) {
711
+ const { name } = await params;
712
+
713
+ // Look in blocks/ first, then templates/
714
+ let blockPath = path.join(projectRoot, "blocks", name);
715
+ if (!fs.existsSync(blockPath)) {
716
+ blockPath = path.join(projectRoot, "templates", name);
717
+ }
718
+
719
+ if (!fs.existsSync(blockPath)) {
720
+ return NextResponse.json({ error: "Block not found" }, { status: 404 });
721
+ }
722
+
723
+ // Load preview data
724
+ const previewPath = path.join(blockPath, "preview.json");
725
+ const previewData = fs.existsSync(previewPath)
726
+ ? JSON.parse(fs.readFileSync(previewPath, "utf-8"))
727
+ : {};
728
+
729
+ // Load block.config.ts schema
730
+ const config = loadBlockConfig(blockPath);
731
+
732
+ // Check for pages.json (templates), fall back to block.config.ts
733
+ const pagesJsonPath = path.join(blockPath, "pages.json");
734
+ let pagesData = fs.existsSync(pagesJsonPath)
735
+ ? JSON.parse(fs.readFileSync(pagesJsonPath, "utf-8"))
736
+ : null;
737
+
738
+ // If no pages.json but config has pages (template), convert on the fly
739
+ if (!pagesData && config && (config.pages || config.layoutPositions)) {
740
+ const layoutPositions: Record<string, any> = {};
741
+ if (Array.isArray(config.layoutPositions)) {
742
+ for (const lp of config.layoutPositions as any[]) {
743
+ layoutPositions[lp.position] = { type: lp.type, content: lp.content || {} };
744
+ }
745
+ } else if (config.layoutPositions && typeof config.layoutPositions === "object") {
746
+ Object.assign(layoutPositions, config.layoutPositions);
747
+ }
748
+
749
+ const pages = ((config.pages || []) as any[]).map((page: any, index: number) => ({
750
+ name: page.name,
751
+ slug: page.slug === "home" || page.slug === "/" || index === 0
752
+ ? "/"
753
+ : page.slug.startsWith("/") ? page.slug : "/" + page.slug,
754
+ blocks: page.blocks || [],
755
+ }));
756
+
757
+ pagesData = { layoutPositions, pages };
758
+ }
759
+
760
+ return NextResponse.json({
761
+ name,
762
+ schema: config?.schema || {},
763
+ previewData,
764
+ pages: config?.pages,
765
+ layoutPositions: config?.layoutPositions,
766
+ pagesData,
767
+ });
768
+ }
769
+ `;
770
+ fs.writeFileSync(path.join(dir, "route.ts"), content);
771
+ }
772
+ function generatePreviewApiRoute(devRoot) {
773
+ const dir = path.join(devRoot, "app/api/preview/[blockName]");
774
+ fs.mkdirSync(dir, { recursive: true });
775
+ const content = `import { NextResponse } from "next/server";
776
+ import fs from "fs";
777
+ import path from "path";
778
+
779
+ const projectRoot = process.env.CMSSY_PROJECT_ROOT || process.cwd();
780
+
781
+ export async function GET(
782
+ _request: Request,
783
+ { params }: { params: Promise<{ blockName: string }> }
784
+ ) {
785
+ const { blockName } = await params;
786
+ let blockPath = path.join(projectRoot, "blocks", blockName);
787
+ if (!fs.existsSync(blockPath)) {
788
+ blockPath = path.join(projectRoot, "templates", blockName);
789
+ }
790
+
791
+ const previewPath = path.join(blockPath, "preview.json");
792
+ const data = fs.existsSync(previewPath)
793
+ ? JSON.parse(fs.readFileSync(previewPath, "utf-8"))
794
+ : {};
795
+
796
+ return NextResponse.json(data);
797
+ }
798
+
799
+ export async function POST(
800
+ request: Request,
801
+ { params }: { params: Promise<{ blockName: string }> }
802
+ ) {
803
+ const { blockName } = await params;
804
+ const body = await request.json();
805
+
806
+ let blockPath = path.join(projectRoot, "blocks", blockName);
807
+ if (!fs.existsSync(blockPath)) {
808
+ blockPath = path.join(projectRoot, "templates", blockName);
809
+ }
810
+
811
+ const previewPath = path.join(blockPath, "preview.json");
812
+ fs.writeFileSync(previewPath, JSON.stringify(body, null, 2));
813
+
814
+ return NextResponse.json({ success: true });
815
+ }
816
+ `;
817
+ fs.writeFileSync(path.join(dir, "route.ts"), content);
818
+ }
819
+ function generateWorkspacesApiRoute(devRoot) {
820
+ const content = `import { NextResponse } from "next/server";
821
+
822
+ export async function GET() {
823
+ // Workspace listing requires API token - return empty for now
824
+ // The full implementation uses GraphQL client with cmssy configure credentials
825
+ return NextResponse.json([]);
826
+ }
827
+ `;
828
+ fs.writeFileSync(path.join(devRoot, "app/api/workspaces/route.ts"), content);
829
+ }
830
+ /**
831
+ * Convert full block type name to simple name.
832
+ * "@cmssy-marketing/blocks.hero" -> "hero"
833
+ * "@vendor/blocks.pricing-table" -> "pricing-table"
834
+ * "hero" -> "hero" (already simple)
835
+ */
836
+ function convertBlockTypeToSimple(blockType) {
837
+ let simple = blockType;
838
+ if (simple.includes("/")) {
839
+ simple = simple.split("/").pop();
840
+ }
841
+ if (simple.startsWith("blocks.")) {
842
+ simple = simple.substring(7);
843
+ }
844
+ else if (simple.startsWith("templates.")) {
845
+ simple = simple.substring(10);
846
+ }
847
+ return simple;
848
+ }
849
+ /**
850
+ * Load block.config.ts synchronously using tsx/esbuild.
851
+ * Used to generate template preview pages when pages.json is missing.
852
+ */
853
+ function loadTemplateConfigSync(templateDir, projectRoot) {
854
+ const configPath = path.join(templateDir, "block.config.ts");
855
+ if (!fs.existsSync(configPath))
856
+ return null;
857
+ try {
858
+ const cliPath = path.dirname(path.dirname(new URL(import.meta.url).pathname));
859
+ const possibleTsxPaths = [
860
+ path.join(cliPath, "..", "node_modules", ".bin", "tsx"),
861
+ path.join(cliPath, "..", "..", "node_modules", ".bin", "tsx"),
862
+ path.join(projectRoot, "node_modules", ".bin", "tsx"),
863
+ ];
864
+ let tsxBinary = possibleTsxPaths.find((p) => fs.existsSync(p));
865
+ if (!tsxBinary)
866
+ tsxBinary = "npx -y tsx";
867
+ const cacheDir = path.join(projectRoot, ".cmssy", "cache");
868
+ fs.ensureDirSync(cacheDir);
869
+ const mockConfigPath = path.join(cacheDir, "cmssy-cli-config.mjs");
870
+ fs.writeFileSync(mockConfigPath, "export const defineBlock = (config) => config;\nexport const defineTemplate = (config) => config;\n");
871
+ const configContent = fs.readFileSync(configPath, "utf-8");
872
+ const modified = configContent.replace(/from\s+['"](?:@?cmssy-?(?:\/cli)?\/config|cmssy-cli\/config)['"]/g, `from '${mockConfigPath.replace(/\\/g, "/")}'`);
873
+ const tempPath = path.join(cacheDir, `temp-template-config-${Date.now()}.ts`);
874
+ fs.writeFileSync(tempPath, modified);
875
+ const evalCode = `import cfg from '${tempPath.replace(/\\/g, "/")}'; console.log(JSON.stringify(cfg.default || cfg));`;
876
+ const cmd = tsxBinary.includes("npx")
877
+ ? `${tsxBinary} --eval "${evalCode}"`
878
+ : `"${tsxBinary}" --eval "${evalCode}"`;
879
+ const output = execSync(cmd, {
880
+ encoding: "utf-8",
881
+ cwd: projectRoot,
882
+ stdio: ["pipe", "pipe", "pipe"],
883
+ });
884
+ try {
885
+ fs.removeSync(tempPath);
886
+ }
887
+ catch { }
888
+ try {
889
+ fs.removeSync(mockConfigPath);
890
+ }
891
+ catch { }
892
+ const lines = output.trim().split("\n");
893
+ return JSON.parse(lines[lines.length - 1]);
894
+ }
895
+ catch {
896
+ return null;
897
+ }
898
+ }
899
+ /**
900
+ * Convert template config (from block.config.ts defineTemplate) to pages.json format.
901
+ * - layoutPositions: array → object keyed by position
902
+ * - page slugs: "home" → "/", others → "/{slug}"
903
+ */
904
+ function convertConfigToPagesData(config) {
905
+ // Convert layoutPositions from array to object
906
+ const layoutPositions = {};
907
+ if (Array.isArray(config.layoutPositions)) {
908
+ for (const lp of config.layoutPositions) {
909
+ layoutPositions[lp.position] = {
910
+ type: lp.type,
911
+ content: lp.content || {},
912
+ };
913
+ }
914
+ }
915
+ else if (config.layoutPositions && typeof config.layoutPositions === "object") {
916
+ // Already in object format
917
+ Object.assign(layoutPositions, config.layoutPositions);
918
+ }
919
+ // Convert page slugs
920
+ const pages = (config.pages || []).map((page, index) => ({
921
+ name: page.name,
922
+ slug: page.slug === "home" || page.slug === "/" || index === 0
923
+ ? "/"
924
+ : page.slug.startsWith("/") ? page.slug : `/${page.slug}`,
925
+ blocks: page.blocks || [],
926
+ }));
927
+ return { layoutPositions, pages };
928
+ }
929
+ function generatePreviewPages(devRoot, projectRoot, resources) {
930
+ for (const resource of resources) {
931
+ if (resource.type === "template") {
932
+ generateTemplatePreviewPage(devRoot, projectRoot, resource);
933
+ }
934
+ else {
935
+ generateBlockPreviewPage(devRoot, projectRoot, resource);
936
+ }
937
+ }
938
+ }
939
+ function generateBlockPreviewPage(devRoot, projectRoot, resource) {
940
+ const pageDir = path.join(devRoot, "app/preview", resource.name);
941
+ fs.mkdirSync(pageDir, { recursive: true });
942
+ const blockSrcDir = path.join(projectRoot, "blocks", resource.name, "src");
943
+ const hasIndex = fs.existsSync(path.join(blockSrcDir, "index.tsx")) ||
944
+ fs.existsSync(path.join(blockSrcDir, "index.ts"));
945
+ const hasCss = fs.existsSync(path.join(blockSrcDir, "index.css"));
946
+ if (!hasIndex)
947
+ return;
948
+ const finalBlockImport = `@blocks/${resource.name}/src/index`;
949
+ const finalCssImport = `@blocks/${resource.name}/src/index.css`;
950
+ const pageContent = `"use client";
951
+
952
+ import { useState, useEffect } from "react";
953
+ import BlockComponent from "${finalBlockImport}";
954
+ ${hasCss ? `import "${finalCssImport}";` : ""}
955
+
956
+ export default function ${toPascalCase(resource.name)}Preview() {
957
+ const [data, setData] = useState<Record<string, unknown>>({});
958
+
959
+ useEffect(() => {
960
+ fetch("/api/preview/${resource.name}")
961
+ .then((r) => r.json())
962
+ .then(setData)
963
+ .catch(console.error);
964
+ }, []);
965
+
966
+ useEffect(() => {
967
+ function handleMessage(e: MessageEvent) {
968
+ if (e.data?.type === "UPDATE_PROPS") {
969
+ setData(e.data.props);
970
+ }
971
+ }
972
+ window.addEventListener("message", handleMessage);
973
+ return () => window.removeEventListener("message", handleMessage);
974
+ }, []);
975
+
976
+ return (
977
+ <div>
978
+ <div style={{
979
+ position: "fixed", top: 0, left: 0, right: 0,
980
+ background: "white", borderBottom: "1px solid #e0e0e0",
981
+ padding: "1rem 2rem", zIndex: 1000,
982
+ display: "flex", justifyContent: "space-between", alignItems: "center",
983
+ }}>
984
+ <div style={{ fontSize: "1.25rem", fontWeight: 600 }}>${resource.displayName || resource.name}</div>
985
+ <a href="/" style={{ color: "#667eea", textDecoration: "none", fontWeight: 500 }} target="_parent">
986
+ &larr; Back to Home
987
+ </a>
988
+ </div>
989
+ <div style={{ marginTop: "60px", minHeight: "calc(100vh - 60px)" }}>
990
+ <BlockComponent content={data} />
991
+ </div>
992
+ </div>
993
+ );
994
+ }
995
+ `;
996
+ fs.writeFileSync(path.join(pageDir, "page.tsx"), pageContent);
997
+ }
998
+ function generateTemplatePreviewPage(devRoot, projectRoot, resource) {
999
+ // Read pages data from pages.json or fall back to block.config.ts
1000
+ const templateDir = path.join(projectRoot, "templates", resource.name);
1001
+ const pagesJsonPath = path.join(templateDir, "pages.json");
1002
+ let pagesData;
1003
+ if (fs.existsSync(pagesJsonPath)) {
1004
+ pagesData = JSON.parse(fs.readFileSync(pagesJsonPath, "utf-8"));
1005
+ }
1006
+ else {
1007
+ // No pages.json — load from block.config.ts and convert format
1008
+ const config = loadTemplateConfigSync(templateDir, projectRoot);
1009
+ if (!config || (!config.pages && !config.layoutPositions))
1010
+ return;
1011
+ pagesData = convertConfigToPagesData(config);
1012
+ }
1013
+ const pages = pagesData.pages || [];
1014
+ const layoutPositions = pagesData.layoutPositions || {};
1015
+ // Collect all unique block types across all pages + layoutPositions
1016
+ const blockTypesSet = new Set();
1017
+ for (const page of pages) {
1018
+ for (const block of page.blocks || []) {
1019
+ blockTypesSet.add(convertBlockTypeToSimple(block.type));
1020
+ }
1021
+ }
1022
+ for (const data of Object.values(layoutPositions)) {
1023
+ blockTypesSet.add(convertBlockTypeToSimple(data.type));
1024
+ }
1025
+ // Check which blocks exist in blocks/ dir with src/index.tsx
1026
+ const blockImports = [];
1027
+ const cssImports = [];
1028
+ const componentMapEntries = [];
1029
+ for (const blockName of Array.from(blockTypesSet)) {
1030
+ const blockSrcDir = path.join(projectRoot, "blocks", blockName, "src");
1031
+ const hasIndex = fs.existsSync(path.join(blockSrcDir, "index.tsx")) ||
1032
+ fs.existsSync(path.join(blockSrcDir, "index.ts"));
1033
+ const hasCss = fs.existsSync(path.join(blockSrcDir, "index.css"));
1034
+ if (!hasIndex)
1035
+ continue;
1036
+ const pascalName = toPascalCase(blockName);
1037
+ blockImports.push(`import ${pascalName} from "@blocks/${blockName}/src/index";`);
1038
+ if (hasCss) {
1039
+ cssImports.push(`import "@blocks/${blockName}/src/index.css";`);
1040
+ }
1041
+ componentMapEntries.push(` "${blockName}": ${pascalName},`);
1042
+ }
1043
+ // Generate [[...slug]] catch-all route
1044
+ const pageDir = path.join(devRoot, "app/preview", resource.name, "[[...slug]]");
1045
+ fs.mkdirSync(pageDir, { recursive: true });
1046
+ const pageContent = `"use client";
1047
+
1048
+ import { useState, useEffect, useRef } from "react";
1049
+ import { useParams, useRouter } from "next/navigation";
1050
+ ${blockImports.join("\n")}
1051
+ ${cssImports.join("\n")}
1052
+
1053
+ const BLOCK_COMPONENTS: Record<string, React.ComponentType<any>> = {
1054
+ ${componentMapEntries.join("\n")}
1055
+ };
1056
+
1057
+ interface BlockData {
1058
+ type: string;
1059
+ content: Record<string, any>;
1060
+ }
1061
+
1062
+ interface PageData {
1063
+ name: string;
1064
+ slug: string;
1065
+ blocks: BlockData[];
1066
+ }
1067
+
1068
+ interface PagesJson {
1069
+ layoutPositions?: Record<string, BlockData>;
1070
+ pages: PageData[];
1071
+ }
1072
+
1073
+ function convertBlockType(type: string): string {
1074
+ let simple = type;
1075
+ if (simple.includes("/")) simple = simple.split("/").pop()!;
1076
+ if (simple.startsWith("blocks.")) simple = simple.substring(7);
1077
+ else if (simple.startsWith("templates.")) simple = simple.substring(10);
1078
+ return simple;
1079
+ }
1080
+
1081
+ export default function ${toPascalCase(resource.name)}TemplatePreview() {
1082
+ const params = useParams();
1083
+ const router = useRouter();
1084
+ const slugParts = (params.slug as string[]) || [];
1085
+ const currentSlug = slugParts.length > 0 ? "/" + slugParts.join("/") : "/";
1086
+ const tabsRef = useRef<HTMLDivElement>(null);
1087
+ const activeTabRef = useRef<HTMLButtonElement>(null);
1088
+
1089
+ const [pagesData, setPagesData] = useState<PagesJson | null>(null);
1090
+ const [contentOverrides, setContentOverrides] = useState<Record<string, BlockData[]>>({});
1091
+ const [navVisible, setNavVisible] = useState(true);
1092
+ const [paletteOpen, setPaletteOpen] = useState(false);
1093
+ const [paletteQuery, setPaletteQuery] = useState("");
1094
+ const [navCollapsed, setNavCollapsed] = useState(false);
1095
+ const paletteInputRef = useRef<HTMLInputElement>(null);
1096
+
1097
+ // Fetch pages data from config API
1098
+ useEffect(() => {
1099
+ fetch("/api/blocks/${resource.name}/config")
1100
+ .then((r) => r.json())
1101
+ .then((config) => {
1102
+ if (config.pagesData) {
1103
+ setPagesData(config.pagesData);
1104
+ }
1105
+ })
1106
+ .catch(console.error);
1107
+ }, []);
1108
+
1109
+ // Listen for live content updates from editor
1110
+ useEffect(() => {
1111
+ function handleMessage(e: MessageEvent) {
1112
+ if (e.data?.type === "UPDATE_TEMPLATE_CONTENT") {
1113
+ setContentOverrides((prev) => ({
1114
+ ...prev,
1115
+ [e.data.pageSlug]: e.data.blocks,
1116
+ }));
1117
+ }
1118
+ }
1119
+ window.addEventListener("message", handleMessage);
1120
+ return () => window.removeEventListener("message", handleMessage);
1121
+ }, []);
1122
+
1123
+ // Scroll active tab into view
1124
+ useEffect(() => {
1125
+ if (activeTabRef.current && tabsRef.current) {
1126
+ activeTabRef.current.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "center" });
1127
+ }
1128
+ }, [currentSlug, pagesData]);
1129
+
1130
+ // Ctrl+K / Cmd+K to open palette
1131
+ useEffect(() => {
1132
+ function onKeyDown(e: KeyboardEvent) {
1133
+ if ((e.metaKey || e.ctrlKey) && e.key === "k") {
1134
+ e.preventDefault();
1135
+ setPaletteOpen((v) => !v);
1136
+ setPaletteQuery("");
1137
+ }
1138
+ if (e.key === "Escape") {
1139
+ setPaletteOpen(false);
1140
+ }
1141
+ }
1142
+ window.addEventListener("keydown", onKeyDown);
1143
+ return () => window.removeEventListener("keydown", onKeyDown);
1144
+ }, []);
1145
+
1146
+ // Focus input when palette opens
1147
+ useEffect(() => {
1148
+ if (paletteOpen && paletteInputRef.current) {
1149
+ paletteInputRef.current.focus();
1150
+ }
1151
+ }, [paletteOpen]);
1152
+
1153
+ // Auto-hide nav on scroll down, show on scroll up
1154
+ useEffect(() => {
1155
+ let lastY = 0;
1156
+ function onScroll() {
1157
+ const y = window.scrollY;
1158
+ if (y > 80 && y > lastY) setNavVisible(false);
1159
+ else setNavVisible(true);
1160
+ lastY = y;
1161
+ }
1162
+ window.addEventListener("scroll", onScroll, { passive: true });
1163
+ return () => window.removeEventListener("scroll", onScroll);
1164
+ }, []);
1165
+
1166
+ if (!pagesData) {
1167
+ return (
1168
+ <div style={{
1169
+ height: "100vh",
1170
+ display: "flex",
1171
+ alignItems: "center",
1172
+ justifyContent: "center",
1173
+ background: "#0a0a0a",
1174
+ }}>
1175
+ <div style={{ textAlign: "center" }}>
1176
+ <div style={{
1177
+ width: "24px",
1178
+ height: "24px",
1179
+ border: "2px solid rgba(255,255,255,0.1)",
1180
+ borderTopColor: "#667eea",
1181
+ borderRadius: "50%",
1182
+ animation: "spin 0.8s linear infinite",
1183
+ margin: "0 auto 16px",
1184
+ }} />
1185
+ <div style={{ color: "rgba(255,255,255,0.4)", fontSize: "13px", letterSpacing: "0.05em" }}>
1186
+ Loading template...
1187
+ </div>
1188
+ </div>
1189
+ <style>{\`@keyframes spin { to { transform: rotate(360deg) } }\`}</style>
1190
+ </div>
1191
+ );
1192
+ }
1193
+
1194
+ // Find current page
1195
+ const currentPage = pagesData.pages.find((p) => p.slug === currentSlug);
1196
+ if (!currentPage) {
1197
+ return (
1198
+ <div style={{
1199
+ height: "100vh",
1200
+ display: "flex",
1201
+ alignItems: "center",
1202
+ justifyContent: "center",
1203
+ background: "#fafafa",
1204
+ }}>
1205
+ <div style={{ textAlign: "center" }}>
1206
+ <div style={{ fontSize: "48px", marginBottom: "12px", opacity: 0.3 }}>404</div>
1207
+ <div style={{ color: "#666", fontSize: "14px" }}>Page not found: {currentSlug}</div>
1208
+ <button
1209
+ onClick={() => router.push("/preview/${resource.name}")}
1210
+ style={{
1211
+ marginTop: "20px",
1212
+ padding: "8px 20px",
1213
+ background: "#667eea",
1214
+ color: "#fff",
1215
+ border: "none",
1216
+ borderRadius: "6px",
1217
+ fontSize: "13px",
1218
+ cursor: "pointer",
1219
+ }}
1220
+ >Go to homepage</button>
1221
+ </div>
1222
+ </div>
1223
+ );
1224
+ }
1225
+
1226
+ // Use overrides if available, otherwise use pages.json content
1227
+ const pageBlocks = contentOverrides[currentSlug] || currentPage.blocks || [];
1228
+
1229
+ // Render header layout position
1230
+ const headerData = pagesData.layoutPositions?.header;
1231
+ const headerType = headerData ? convertBlockType(headerData.type) : null;
1232
+ const HeaderComponent = headerType ? BLOCK_COMPONENTS[headerType] : null;
1233
+
1234
+ // Render footer layout position
1235
+ const footerData = pagesData.layoutPositions?.footer;
1236
+ const footerType = footerData ? convertBlockType(footerData.type) : null;
1237
+ const FooterComponent = footerType ? BLOCK_COMPONENTS[footerType] : null;
1238
+
1239
+ // Render sidebar_left layout position
1240
+ const sidebarLeftData = pagesData.layoutPositions?.sidebar_left;
1241
+ const sidebarLeftType = sidebarLeftData ? convertBlockType(sidebarLeftData.type) : null;
1242
+ const SidebarLeftComponent = sidebarLeftType ? BLOCK_COMPONENTS[sidebarLeftType] : null;
1243
+
1244
+ const navigateTo = (slug: string) => {
1245
+ const path = slug === "/" ? "/preview/${resource.name}" : \`/preview/${resource.name}\${slug}\`;
1246
+ router.push(path);
1247
+ };
1248
+
1249
+ return (
1250
+ <>
1251
+ <style>{\`
1252
+ @import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600&display=swap');
1253
+
1254
+ .cmssy-nav {
1255
+ position: fixed;
1256
+ top: 0;
1257
+ left: 0;
1258
+ right: 0;
1259
+ z-index: 9999;
1260
+ transform: translateY(\${navVisible ? "0" : "-100%"});
1261
+ transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
1262
+ }
1263
+ .cmssy-nav-inner {
1264
+ margin: 10px;
1265
+ background: rgba(10, 10, 10, 0.92);
1266
+ backdrop-filter: blur(20px) saturate(180%);
1267
+ -webkit-backdrop-filter: blur(20px) saturate(180%);
1268
+ border-radius: 14px;
1269
+ border: 1px solid rgba(255, 255, 255, 0.08);
1270
+ box-shadow:
1271
+ 0 8px 32px rgba(0, 0, 0, 0.4),
1272
+ 0 0 0 1px rgba(255, 255, 255, 0.05) inset;
1273
+ padding: 8px 12px;
1274
+ display: flex;
1275
+ align-items: center;
1276
+ gap: 8px;
1277
+ font-family: 'DM Sans', -apple-system, sans-serif;
1278
+ }
1279
+ .cmssy-back-btn {
1280
+ display: flex;
1281
+ align-items: center;
1282
+ gap: 6px;
1283
+ padding: 6px 12px;
1284
+ background: rgba(255, 255, 255, 0.06);
1285
+ border: 1px solid rgba(255, 255, 255, 0.08);
1286
+ border-radius: 8px;
1287
+ color: rgba(255, 255, 255, 0.7);
1288
+ font-size: 12px;
1289
+ font-weight: 500;
1290
+ cursor: pointer;
1291
+ text-decoration: none;
1292
+ white-space: nowrap;
1293
+ transition: all 0.15s ease;
1294
+ font-family: inherit;
1295
+ letter-spacing: 0.01em;
1296
+ }
1297
+ .cmssy-back-btn:hover {
1298
+ background: rgba(255, 255, 255, 0.12);
1299
+ color: #fff;
1300
+ }
1301
+ .cmssy-divider {
1302
+ width: 1px;
1303
+ height: 24px;
1304
+ background: rgba(255, 255, 255, 0.1);
1305
+ flex-shrink: 0;
1306
+ }
1307
+ .cmssy-template-name {
1308
+ font-size: 13px;
1309
+ font-weight: 600;
1310
+ color: #fff;
1311
+ white-space: nowrap;
1312
+ letter-spacing: -0.01em;
1313
+ flex-shrink: 0;
1314
+ }
1315
+ .cmssy-tabs-wrapper {
1316
+ flex: 1;
1317
+ overflow-x: auto;
1318
+ overflow-y: hidden;
1319
+ scrollbar-width: none;
1320
+ -ms-overflow-style: none;
1321
+ mask-image: linear-gradient(to right, transparent, black 20px, black calc(100% - 20px), transparent);
1322
+ -webkit-mask-image: linear-gradient(to right, transparent, black 20px, black calc(100% - 20px), transparent);
1323
+ }
1324
+ .cmssy-tabs-wrapper::-webkit-scrollbar {
1325
+ display: none;
1326
+ }
1327
+ .cmssy-tabs {
1328
+ display: flex;
1329
+ gap: 2px;
1330
+ padding: 0 8px;
1331
+ }
1332
+ .cmssy-tab {
1333
+ padding: 5px 12px;
1334
+ font-size: 12px;
1335
+ font-weight: 500;
1336
+ border: none;
1337
+ border-radius: 7px;
1338
+ cursor: pointer;
1339
+ white-space: nowrap;
1340
+ transition: all 0.15s ease;
1341
+ font-family: inherit;
1342
+ letter-spacing: 0.01em;
1343
+ }
1344
+ .cmssy-tab-inactive {
1345
+ background: transparent;
1346
+ color: rgba(255, 255, 255, 0.45);
1347
+ }
1348
+ .cmssy-tab-inactive:hover {
1349
+ background: rgba(255, 255, 255, 0.08);
1350
+ color: rgba(255, 255, 255, 0.8);
1351
+ }
1352
+ .cmssy-tab-active {
1353
+ background: #667eea;
1354
+ color: #fff;
1355
+ box-shadow: 0 2px 8px rgba(102, 126, 234, 0.35);
1356
+ }
1357
+ .cmssy-page-count {
1358
+ font-size: 11px;
1359
+ font-weight: 500;
1360
+ color: rgba(255, 255, 255, 0.3);
1361
+ white-space: nowrap;
1362
+ flex-shrink: 0;
1363
+ padding-right: 4px;
1364
+ }
1365
+ .cmssy-collapse-btn {
1366
+ display: flex;
1367
+ align-items: center;
1368
+ justify-content: center;
1369
+ width: 28px;
1370
+ height: 28px;
1371
+ background: rgba(255, 255, 255, 0.06);
1372
+ border: 1px solid rgba(255, 255, 255, 0.08);
1373
+ border-radius: 7px;
1374
+ color: rgba(255, 255, 255, 0.5);
1375
+ cursor: pointer;
1376
+ flex-shrink: 0;
1377
+ transition: all 0.15s ease;
1378
+ font-family: inherit;
1379
+ }
1380
+ .cmssy-collapse-btn:hover {
1381
+ background: rgba(255, 255, 255, 0.12);
1382
+ color: #fff;
1383
+ }
1384
+ .cmssy-nav-collapsed .cmssy-nav-inner {
1385
+ padding: 6px 8px;
1386
+ width: fit-content;
1387
+ }
1388
+ .cmssy-nav-pill {
1389
+ display: flex;
1390
+ align-items: center;
1391
+ gap: 8px;
1392
+ font-family: 'DM Sans', -apple-system, sans-serif;
1393
+ }
1394
+ .cmssy-nav-pill-page {
1395
+ font-size: 12px;
1396
+ font-weight: 500;
1397
+ color: rgba(255, 255, 255, 0.7);
1398
+ white-space: nowrap;
1399
+ }
1400
+ .cmssy-search-btn {
1401
+ display: flex;
1402
+ align-items: center;
1403
+ gap: 6px;
1404
+ padding: 5px 10px;
1405
+ background: rgba(255, 255, 255, 0.06);
1406
+ border: 1px solid rgba(255, 255, 255, 0.08);
1407
+ border-radius: 7px;
1408
+ color: rgba(255, 255, 255, 0.5);
1409
+ font-size: 11px;
1410
+ font-family: inherit;
1411
+ cursor: pointer;
1412
+ white-space: nowrap;
1413
+ transition: all 0.15s ease;
1414
+ flex-shrink: 0;
1415
+ }
1416
+ .cmssy-search-btn:hover {
1417
+ background: rgba(255, 255, 255, 0.1);
1418
+ color: rgba(255, 255, 255, 0.8);
1419
+ }
1420
+ .cmssy-search-kbd {
1421
+ font-size: 10px;
1422
+ padding: 1px 5px;
1423
+ border-radius: 4px;
1424
+ background: rgba(255, 255, 255, 0.08);
1425
+ color: rgba(255, 255, 255, 0.35);
1426
+ font-family: inherit;
1427
+ }
1428
+ .cmssy-palette-overlay {
1429
+ position: fixed;
1430
+ inset: 0;
1431
+ z-index: 10000;
1432
+ background: rgba(0, 0, 0, 0.5);
1433
+ backdrop-filter: blur(4px);
1434
+ display: flex;
1435
+ align-items: flex-start;
1436
+ justify-content: center;
1437
+ padding-top: 20vh;
1438
+ animation: cmssyFadeIn 0.15s ease;
1439
+ }
1440
+ .cmssy-palette {
1441
+ width: 480px;
1442
+ max-width: calc(100vw - 32px);
1443
+ background: #1a1a1a;
1444
+ border: 1px solid rgba(255, 255, 255, 0.1);
1445
+ border-radius: 14px;
1446
+ box-shadow: 0 24px 64px rgba(0, 0, 0, 0.6);
1447
+ overflow: hidden;
1448
+ animation: cmssySlideUp 0.15s ease;
1449
+ font-family: 'DM Sans', -apple-system, sans-serif;
1450
+ }
1451
+ .cmssy-palette-input-wrapper {
1452
+ display: flex;
1453
+ align-items: center;
1454
+ padding: 14px 16px;
1455
+ border-bottom: 1px solid rgba(255, 255, 255, 0.08);
1456
+ gap: 10px;
1457
+ }
1458
+ .cmssy-palette-input {
1459
+ flex: 1;
1460
+ background: none;
1461
+ border: none;
1462
+ outline: none;
1463
+ color: #fff;
1464
+ font-size: 15px;
1465
+ font-family: inherit;
1466
+ letter-spacing: -0.01em;
1467
+ }
1468
+ .cmssy-palette-input::placeholder {
1469
+ color: rgba(255, 255, 255, 0.3);
1470
+ }
1471
+ .cmssy-palette-list {
1472
+ max-height: 340px;
1473
+ overflow-y: auto;
1474
+ padding: 6px;
1475
+ scrollbar-width: thin;
1476
+ scrollbar-color: rgba(255,255,255,0.1) transparent;
1477
+ }
1478
+ .cmssy-palette-item {
1479
+ display: flex;
1480
+ align-items: center;
1481
+ justify-content: space-between;
1482
+ padding: 10px 12px;
1483
+ border-radius: 8px;
1484
+ cursor: pointer;
1485
+ transition: background 0.1s ease;
1486
+ border: none;
1487
+ background: none;
1488
+ width: 100%;
1489
+ text-align: left;
1490
+ font-family: inherit;
1491
+ }
1492
+ .cmssy-palette-item:hover {
1493
+ background: rgba(255, 255, 255, 0.06);
1494
+ }
1495
+ .cmssy-palette-item-active {
1496
+ background: rgba(102, 126, 234, 0.15);
1497
+ }
1498
+ .cmssy-palette-item-active:hover {
1499
+ background: rgba(102, 126, 234, 0.2);
1500
+ }
1501
+ .cmssy-palette-item-name {
1502
+ font-size: 14px;
1503
+ font-weight: 500;
1504
+ color: #fff;
1505
+ }
1506
+ .cmssy-palette-item-slug {
1507
+ font-size: 12px;
1508
+ color: rgba(255, 255, 255, 0.3);
1509
+ font-family: monospace;
1510
+ }
1511
+ .cmssy-palette-item-current {
1512
+ font-size: 10px;
1513
+ padding: 2px 8px;
1514
+ border-radius: 4px;
1515
+ background: #667eea;
1516
+ color: #fff;
1517
+ font-weight: 600;
1518
+ letter-spacing: 0.03em;
1519
+ }
1520
+ .cmssy-palette-empty {
1521
+ padding: 24px 16px;
1522
+ text-align: center;
1523
+ color: rgba(255, 255, 255, 0.3);
1524
+ font-size: 13px;
1525
+ }
1526
+ @keyframes cmssyFadeIn {
1527
+ from { opacity: 0; }
1528
+ to { opacity: 1; }
1529
+ }
1530
+ @keyframes cmssySlideUp {
1531
+ from { opacity: 0; transform: translateY(8px) scale(0.98); }
1532
+ to { opacity: 1; transform: translateY(0) scale(1); }
1533
+ }
1534
+ \`}</style>
1535
+
1536
+ {/* Floating Navigation Bar */}
1537
+ <div className={\`cmssy-nav \${navCollapsed ? "cmssy-nav-collapsed" : ""}\`}>
1538
+ <div className="cmssy-nav-inner">
1539
+ {navCollapsed ? (
1540
+ <div className="cmssy-nav-pill">
1541
+ <a href="/" className="cmssy-back-btn">
1542
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
1543
+ <polyline points="15 18 9 12 15 6" />
1544
+ </svg>
1545
+ Dev
1546
+ </a>
1547
+ <div className="cmssy-divider" />
1548
+ <div className="cmssy-nav-pill-page">{currentPage?.name || "—"}</div>
1549
+ <button
1550
+ className="cmssy-search-btn"
1551
+ onClick={() => { setPaletteOpen(true); setPaletteQuery(""); }}
1552
+ title="Search pages (Ctrl+K)"
1553
+ >
1554
+ <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
1555
+ <circle cx="11" cy="11" r="8" />
1556
+ <line x1="21" y1="21" x2="16.65" y2="16.65" />
1557
+ </svg>
1558
+ <span className="cmssy-search-kbd">\u2318K</span>
1559
+ </button>
1560
+ <button
1561
+ className="cmssy-collapse-btn"
1562
+ onClick={() => setNavCollapsed(false)}
1563
+ title="Expand navigation"
1564
+ >
1565
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
1566
+ <polyline points="6 9 12 15 18 9" />
1567
+ </svg>
1568
+ </button>
1569
+ </div>
1570
+ ) : (
1571
+ <>
1572
+ <a href="/" className="cmssy-back-btn">
1573
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
1574
+ <polyline points="15 18 9 12 15 6" />
1575
+ </svg>
1576
+ Dev
1577
+ </a>
1578
+
1579
+ <div className="cmssy-divider" />
1580
+
1581
+ <div className="cmssy-template-name">${resource.displayName || resource.name}</div>
1582
+
1583
+ <div className="cmssy-divider" />
1584
+
1585
+ <div className="cmssy-tabs-wrapper" ref={tabsRef}>
1586
+ <div className="cmssy-tabs">
1587
+ {pagesData.pages.map((page) => (
1588
+ <button
1589
+ key={page.slug}
1590
+ ref={page.slug === currentSlug ? activeTabRef : undefined}
1591
+ onClick={() => navigateTo(page.slug)}
1592
+ className={\`cmssy-tab \${page.slug === currentSlug ? "cmssy-tab-active" : "cmssy-tab-inactive"}\`}
1593
+ >
1594
+ {page.name}
1595
+ </button>
1596
+ ))}
1597
+ </div>
1598
+ </div>
1599
+
1600
+ <button
1601
+ className="cmssy-search-btn"
1602
+ onClick={() => { setPaletteOpen(true); setPaletteQuery(""); }}
1603
+ title="Search pages (Ctrl+K)"
1604
+ >
1605
+ <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
1606
+ <circle cx="11" cy="11" r="8" />
1607
+ <line x1="21" y1="21" x2="16.65" y2="16.65" />
1608
+ </svg>
1609
+ <span className="cmssy-search-kbd">\u2318K</span>
1610
+ </button>
1611
+
1612
+ <button
1613
+ className="cmssy-collapse-btn"
1614
+ onClick={() => setNavCollapsed(true)}
1615
+ title="Collapse navigation"
1616
+ >
1617
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
1618
+ <polyline points="18 15 12 9 6 15" />
1619
+ </svg>
1620
+ </button>
1621
+ </>
1622
+ )}
1623
+ </div>
1624
+ </div>
1625
+
1626
+ {/* Command Palette */}
1627
+ {paletteOpen && (
1628
+ <div className="cmssy-palette-overlay" onClick={() => setPaletteOpen(false)}>
1629
+ <div className="cmssy-palette" onClick={(e) => e.stopPropagation()}>
1630
+ <div className="cmssy-palette-input-wrapper">
1631
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="rgba(255,255,255,0.4)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
1632
+ <circle cx="11" cy="11" r="8" />
1633
+ <line x1="21" y1="21" x2="16.65" y2="16.65" />
1634
+ </svg>
1635
+ <input
1636
+ ref={paletteInputRef}
1637
+ type="text"
1638
+ className="cmssy-palette-input"
1639
+ placeholder="Search pages..."
1640
+ value={paletteQuery}
1641
+ onChange={(e) => setPaletteQuery(e.target.value)}
1642
+ onKeyDown={(e) => {
1643
+ if (e.key === "Enter") {
1644
+ const q = paletteQuery.toLowerCase();
1645
+ const match = pagesData.pages.find(
1646
+ (p) => p.name.toLowerCase().includes(q) || p.slug.toLowerCase().includes(q)
1647
+ );
1648
+ if (match) {
1649
+ navigateTo(match.slug);
1650
+ setPaletteOpen(false);
1651
+ }
1652
+ }
1653
+ }}
1654
+ />
1655
+ </div>
1656
+ <div className="cmssy-palette-list">
1657
+ {(() => {
1658
+ const q = paletteQuery.toLowerCase();
1659
+ const filtered = pagesData.pages.filter(
1660
+ (p) => p.name.toLowerCase().includes(q) || p.slug.toLowerCase().includes(q)
1661
+ );
1662
+ if (filtered.length === 0) {
1663
+ return <div className="cmssy-palette-empty">No pages match &ldquo;{paletteQuery}&rdquo;</div>;
1664
+ }
1665
+ return filtered.map((page) => (
1666
+ <button
1667
+ key={page.slug}
1668
+ className={\`cmssy-palette-item \${page.slug === currentSlug ? "cmssy-palette-item-active" : ""}\`}
1669
+ onClick={() => {
1670
+ navigateTo(page.slug);
1671
+ setPaletteOpen(false);
1672
+ }}
1673
+ >
1674
+ <div>
1675
+ <div className="cmssy-palette-item-name">{page.name}</div>
1676
+ <div className="cmssy-palette-item-slug">{page.slug}</div>
1677
+ </div>
1678
+ {page.slug === currentSlug && (
1679
+ <span className="cmssy-palette-item-current">Current</span>
1680
+ )}
1681
+ </button>
1682
+ ));
1683
+ })()}
1684
+ </div>
1685
+ </div>
1686
+ </div>
1687
+ )}
1688
+
1689
+ {/* Page Content */}
1690
+ <div>
1691
+ {HeaderComponent && headerData && (
1692
+ <HeaderComponent content={headerData.content || {}} />
1693
+ )}
1694
+ {SidebarLeftComponent && sidebarLeftData ? (
1695
+ <div style={{ display: "grid", gridTemplateColumns: "280px 1fr", minHeight: "100vh" }}>
1696
+ <SidebarLeftComponent content={sidebarLeftData.content || {}} />
1697
+ <main>
1698
+ {pageBlocks.map((block, idx) => {
1699
+ const blockType = convertBlockType(block.type);
1700
+ const Component = BLOCK_COMPONENTS[blockType];
1701
+ if (!Component) {
1702
+ return (
1703
+ <div key={idx} style={{
1704
+ padding: "2rem",
1705
+ background: "#fff3cd",
1706
+ textAlign: "center",
1707
+ margin: "1rem",
1708
+ borderRadius: "8px",
1709
+ fontSize: "14px",
1710
+ color: "#856404",
1711
+ }}>
1712
+ Missing block component: {blockType}
1713
+ </div>
1714
+ );
1715
+ }
1716
+ return <Component key={idx} content={block.content || {}} />;
1717
+ })}
1718
+ </main>
1719
+ </div>
1720
+ ) : (
1721
+ pageBlocks.map((block, idx) => {
1722
+ const blockType = convertBlockType(block.type);
1723
+ const Component = BLOCK_COMPONENTS[blockType];
1724
+ if (!Component) {
1725
+ return (
1726
+ <div key={idx} style={{
1727
+ padding: "2rem",
1728
+ background: "#fff3cd",
1729
+ textAlign: "center",
1730
+ margin: "1rem",
1731
+ borderRadius: "8px",
1732
+ fontSize: "14px",
1733
+ color: "#856404",
1734
+ }}>
1735
+ Missing block component: {blockType}
1736
+ </div>
1737
+ );
1738
+ }
1739
+ return <Component key={idx} content={block.content || {}} />;
1740
+ })
1741
+ )}
1742
+ {FooterComponent && footerData && (
1743
+ <FooterComponent content={footerData.content || {}} />
1744
+ )}
1745
+ </div>
1746
+ </>
1747
+ );
1748
+ }
1749
+ `;
1750
+ fs.writeFileSync(path.join(pageDir, "page.tsx"), pageContent);
1751
+ }
1752
+ function toPascalCase(str) {
1753
+ return str
1754
+ .split(/[-_\s]+/)
1755
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
1756
+ .join("");
1757
+ }
1758
+ //# sourceMappingURL=dev-generator.js.map