deneljs 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -2
- package/drizzle/0000_lonely_reptil.sql +27 -0
- package/drizzle/0001_misty_wendigo.sql +74 -0
- package/drizzle/meta/0000_snapshot.json +179 -0
- package/drizzle/meta/0001_snapshot.json +492 -0
- package/drizzle/meta/_journal.json +20 -0
- package/package.json +55 -4
- package/src/admin/auth.ts +851 -0
- package/src/admin/layout.ts +419 -0
- package/src/admin/pages/categories.ts +223 -0
- package/src/admin/pages/dashboard.ts +192 -0
- package/src/admin/pages/media.ts +270 -0
- package/src/admin/pages/posts.ts +804 -0
- package/src/admin/pages/settings.ts +169 -0
- package/src/admin/pages/themes.ts +461 -0
- package/src/admin/pages/users.ts +501 -0
- package/src/admin/routes.ts +455 -0
- package/src/admin/tiptap-bundle.ts +48 -0
- package/src/admin/tiptap-entry.ts +226 -0
- package/src/ai/generator.ts +296 -0
- package/src/ai/index.ts +22 -0
- package/src/ai/schema.ts +13 -0
- package/src/ai/spec.ts +193 -0
- package/src/cli.ts +151 -0
- package/src/db/index.ts +278 -0
- package/src/db/schema.ts +115 -0
- package/src/engine/index.ts +52 -0
- package/src/engine/renderer.ts +533 -0
- package/src/engine/sandbox.ts +108 -0
- package/src/engine/schema.ts +71 -0
- package/src/engine/validator.ts +404 -0
- package/src/routes/admin-api.ts +1035 -0
- package/src/routes/posts.ts +294 -0
- package/src/routes/uploads.ts +119 -0
- package/src/server.ts +672 -0
- package/src/utils/sanitize-html.ts +145 -0
- package/themes/.gitkeep +1 -0
- package/themes/default/assets/style.css +799 -0
- package/themes/default/layouts/base.liquid +31 -0
- package/themes/default/layouts/home.liquid +180 -0
- package/themes/default/layouts/page.liquid +21 -0
- package/themes/default/layouts/post.liquid +55 -0
- package/themes/default/partials/footer.liquid +33 -0
- package/themes/default/partials/navbar.liquid +49 -0
- package/themes/default/theme.json +59 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import type { CurrentUser } from "./auth";
|
|
2
|
+
|
|
3
|
+
export type AdminNavKey =
|
|
4
|
+
| "dashboard"
|
|
5
|
+
| "posts"
|
|
6
|
+
| "categories"
|
|
7
|
+
| "media"
|
|
8
|
+
| "themes"
|
|
9
|
+
| "users"
|
|
10
|
+
| "settings";
|
|
11
|
+
|
|
12
|
+
export interface AdminLayoutOptions {
|
|
13
|
+
title: string;
|
|
14
|
+
activeNav: AdminNavKey;
|
|
15
|
+
currentUser: CurrentUser;
|
|
16
|
+
siteTitle: string;
|
|
17
|
+
content: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function escapeHtml(value: string): string {
|
|
21
|
+
return value
|
|
22
|
+
.replace(/&/g, "&")
|
|
23
|
+
.replace(/</g, "<")
|
|
24
|
+
.replace(/>/g, ">")
|
|
25
|
+
.replace(/"/g, """)
|
|
26
|
+
.replace(/'/g, "'");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function serializeSafeJson(value: unknown): string {
|
|
30
|
+
return JSON.stringify(value)
|
|
31
|
+
.replace(/</g, "\\u003c")
|
|
32
|
+
.replace(/>/g, "\\u003e")
|
|
33
|
+
.replace(/&/g, "\\u0026")
|
|
34
|
+
.replace(/\u2028/g, "\\u2028")
|
|
35
|
+
.replace(/\u2029/g, "\\u2029");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface NavItem {
|
|
39
|
+
key: AdminNavKey;
|
|
40
|
+
label: string;
|
|
41
|
+
href: string;
|
|
42
|
+
icon: string;
|
|
43
|
+
adminOnly?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const NAV_ITEMS: NavItem[] = [
|
|
47
|
+
{ key: "dashboard", label: "Dashboard", href: "/admin", icon: "layout-dashboard" },
|
|
48
|
+
{ key: "posts", label: "Posts", href: "/admin/posts", icon: "file-text" },
|
|
49
|
+
{ key: "categories", label: "Categories", href: "/admin/categories", icon: "folder-tree" },
|
|
50
|
+
{ key: "media", label: "Media Library", href: "/admin/media", icon: "image" },
|
|
51
|
+
{ key: "themes", label: "Theme Manager", href: "/admin/themes", icon: "palette", adminOnly: true },
|
|
52
|
+
{ key: "users", label: "Users", href: "/admin/users", icon: "users", adminOnly: true },
|
|
53
|
+
{ key: "settings", label: "Settings", href: "/admin/settings", icon: "settings", adminOnly: true },
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Render the persistent Admin Shell (sidebar, top bar, toast system, and reusable Media Picker modal).
|
|
58
|
+
*/
|
|
59
|
+
export function renderAdminLayout(options: AdminLayoutOptions): string {
|
|
60
|
+
const { title, activeNav, currentUser, siteTitle, content } = options;
|
|
61
|
+
const safeTitle = escapeHtml(title);
|
|
62
|
+
const safeSiteTitle = escapeHtml(siteTitle);
|
|
63
|
+
const safeUserName = escapeHtml(currentUser.name);
|
|
64
|
+
const safeUserRole = escapeHtml(currentUser.role);
|
|
65
|
+
|
|
66
|
+
const navLinksHtml = NAV_ITEMS.filter(
|
|
67
|
+
(item) => !item.adminOnly || currentUser.role === "admin",
|
|
68
|
+
)
|
|
69
|
+
.map((item) => {
|
|
70
|
+
const active = item.key === activeNav;
|
|
71
|
+
const cls = active
|
|
72
|
+
? "bg-slate-800 text-white shadow-sm"
|
|
73
|
+
: "text-slate-300 hover:bg-slate-800/60 hover:text-white";
|
|
74
|
+
return `<a href="${item.href}" class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium transition ${cls}">
|
|
75
|
+
<i data-lucide="${item.icon}" class="w-4 h-4 shrink-0"></i>
|
|
76
|
+
<span>${escapeHtml(item.label)}</span>
|
|
77
|
+
</a>`;
|
|
78
|
+
})
|
|
79
|
+
.join("\n");
|
|
80
|
+
|
|
81
|
+
return `<!DOCTYPE html>
|
|
82
|
+
<html lang="en">
|
|
83
|
+
<head>
|
|
84
|
+
<meta charset="UTF-8">
|
|
85
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
86
|
+
<title>${safeTitle} — ${safeSiteTitle} Admin</title>
|
|
87
|
+
<script src="https://cdn.tailwindcss.com"></script>
|
|
88
|
+
<script src="https://unpkg.com/lucide@latest"></script>
|
|
89
|
+
<style>
|
|
90
|
+
[x-cloak] { display: none !important; }
|
|
91
|
+
</style>
|
|
92
|
+
<script>
|
|
93
|
+
window.cmsToast = function (type, message) {
|
|
94
|
+
window.dispatchEvent(new CustomEvent("cms-toast", { detail: { type, message } }));
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
window.openMediaPicker = function (onSelect) {
|
|
98
|
+
window.dispatchEvent(new CustomEvent("cms-open-media-picker", { detail: { onSelect } }));
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
document.addEventListener("alpine:init", () => {
|
|
102
|
+
Alpine.data("adminShell", () => ({
|
|
103
|
+
sidebarOpen: false,
|
|
104
|
+
toast: null,
|
|
105
|
+
toastTimer: null,
|
|
106
|
+
loggingOut: false,
|
|
107
|
+
|
|
108
|
+
init() {
|
|
109
|
+
window.addEventListener("cms-toast", (e) => {
|
|
110
|
+
const detail = e.detail || {};
|
|
111
|
+
this.showToast(detail.type || "success", detail.message || "");
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
showToast(type, message) {
|
|
116
|
+
this.toast = { type, message };
|
|
117
|
+
if (this.toastTimer) clearTimeout(this.toastTimer);
|
|
118
|
+
this.toastTimer = setTimeout(() => {
|
|
119
|
+
this.toast = null;
|
|
120
|
+
}, 3500);
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
async logout() {
|
|
124
|
+
this.loggingOut = true;
|
|
125
|
+
try {
|
|
126
|
+
await fetch("/api/auth/logout", { method: "POST" });
|
|
127
|
+
} finally {
|
|
128
|
+
window.location.href = "/admin/login";
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
}));
|
|
132
|
+
|
|
133
|
+
Alpine.data("mediaPickerModal", () => ({
|
|
134
|
+
open: false,
|
|
135
|
+
loading: false,
|
|
136
|
+
uploading: false,
|
|
137
|
+
search: "",
|
|
138
|
+
files: [],
|
|
139
|
+
error: "",
|
|
140
|
+
onSelectCallback: null,
|
|
141
|
+
|
|
142
|
+
init() {
|
|
143
|
+
window.addEventListener("cms-open-media-picker", (e) => {
|
|
144
|
+
this.onSelectCallback = e.detail?.onSelect || null;
|
|
145
|
+
this.search = "";
|
|
146
|
+
this.open = true;
|
|
147
|
+
this.loadFiles();
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
get filteredFiles() {
|
|
152
|
+
const q = this.search.trim().toLowerCase();
|
|
153
|
+
if (!q) return this.files;
|
|
154
|
+
return this.files.filter((f) =>
|
|
155
|
+
String(f.original_name || "").toLowerCase().includes(q) ||
|
|
156
|
+
String(f.alt_text || "").toLowerCase().includes(q)
|
|
157
|
+
);
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
async loadFiles() {
|
|
161
|
+
this.loading = true;
|
|
162
|
+
this.error = "";
|
|
163
|
+
try {
|
|
164
|
+
const res = await fetch("/api/admin/media?t=" + Date.now(), { cache: "no-store" });
|
|
165
|
+
const data = await res.json();
|
|
166
|
+
this.files = Array.isArray(data.files) ? data.files : [];
|
|
167
|
+
} catch {
|
|
168
|
+
this.error = "Failed to load media library.";
|
|
169
|
+
} finally {
|
|
170
|
+
this.loading = false;
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
async uploadInline(event) {
|
|
175
|
+
const input = event.target;
|
|
176
|
+
const fileList = input?.files;
|
|
177
|
+
if (!fileList || fileList.length === 0) return;
|
|
178
|
+
|
|
179
|
+
this.uploading = true;
|
|
180
|
+
this.error = "";
|
|
181
|
+
const fd = new FormData();
|
|
182
|
+
for (const f of fileList) {
|
|
183
|
+
fd.append("files", f);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
const res = await fetch("/api/admin/media/upload", { method: "POST", body: fd });
|
|
188
|
+
const data = await res.json();
|
|
189
|
+
if (!res.ok || !data.success) {
|
|
190
|
+
this.error = (data.errors && data.errors[0]) || "Upload failed.";
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (Array.isArray(data.files) && data.files.length > 0) {
|
|
194
|
+
const existingIds = new Set(this.files.map((f) => f.id));
|
|
195
|
+
const added = data.files.filter((f) => !existingIds.has(f.id));
|
|
196
|
+
this.files = [...added, ...this.files];
|
|
197
|
+
window.dispatchEvent(new CustomEvent("cms-media-uploaded", { detail: { files: data.files } }));
|
|
198
|
+
}
|
|
199
|
+
window.cmsToast("success", "Uploaded media file.");
|
|
200
|
+
} catch {
|
|
201
|
+
this.error = "Network error during upload.";
|
|
202
|
+
} finally {
|
|
203
|
+
this.uploading = false;
|
|
204
|
+
if (input) input.value = "";
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
choose(file) {
|
|
209
|
+
if (typeof this.onSelectCallback === "function") {
|
|
210
|
+
this.onSelectCallback(file.url, file);
|
|
211
|
+
}
|
|
212
|
+
this.open = false;
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
isImage(file) {
|
|
216
|
+
return String(file?.mime_type || "").startsWith("image/") || /\\.(webp|png|jpe?g|gif)$/i.test(file?.filename || "");
|
|
217
|
+
},
|
|
218
|
+
}));
|
|
219
|
+
});
|
|
220
|
+
</script>
|
|
221
|
+
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js"></script>
|
|
222
|
+
</head>
|
|
223
|
+
<body class="min-h-screen bg-slate-100 text-slate-900 antialiased" x-data="adminShell">
|
|
224
|
+
<div class="min-h-screen flex">
|
|
225
|
+
<!-- Mobile Sidebar Backdrop -->
|
|
226
|
+
<div
|
|
227
|
+
x-show="sidebarOpen"
|
|
228
|
+
x-cloak
|
|
229
|
+
@click="sidebarOpen = false"
|
|
230
|
+
class="fixed inset-0 z-30 bg-slate-950/60 lg:hidden"
|
|
231
|
+
></div>
|
|
232
|
+
|
|
233
|
+
<!-- Persistent Left Sidebar -->
|
|
234
|
+
<aside
|
|
235
|
+
:class="sidebarOpen ? 'translate-x-0' : '-translate-x-full lg:translate-x-0'"
|
|
236
|
+
class="fixed inset-y-0 left-0 z-40 w-64 bg-slate-900 text-slate-100 flex flex-col transition-transform duration-200 lg:static lg:shrink-0"
|
|
237
|
+
>
|
|
238
|
+
<div class="h-16 px-4 border-b border-slate-800 flex items-center justify-between">
|
|
239
|
+
<a href="/admin" class="flex items-center gap-2.5 min-w-0">
|
|
240
|
+
<span class="w-8 h-8 rounded-lg bg-blue-600 text-white flex items-center justify-center font-bold text-sm shrink-0">A</span>
|
|
241
|
+
<span class="font-semibold text-sm text-white truncate">${safeSiteTitle}</span>
|
|
242
|
+
</a>
|
|
243
|
+
<button
|
|
244
|
+
type="button"
|
|
245
|
+
@click="sidebarOpen = false"
|
|
246
|
+
class="lg:hidden p-1.5 rounded-md text-slate-400 hover:text-white hover:bg-slate-800"
|
|
247
|
+
aria-label="Close sidebar"
|
|
248
|
+
>
|
|
249
|
+
<i data-lucide="x" class="w-4 h-4"></i>
|
|
250
|
+
</button>
|
|
251
|
+
</div>
|
|
252
|
+
|
|
253
|
+
<nav class="flex-1 px-3 py-4 space-y-1 overflow-y-auto" aria-label="Admin navigation">
|
|
254
|
+
${navLinksHtml}
|
|
255
|
+
</nav>
|
|
256
|
+
|
|
257
|
+
<div class="p-3 border-t border-slate-800 text-xs text-slate-400 flex items-center justify-between">
|
|
258
|
+
<span>Bun + Elysia CMS</span>
|
|
259
|
+
<span class="px-2 py-0.5 rounded bg-slate-800 text-slate-300 font-mono">SSR</span>
|
|
260
|
+
</div>
|
|
261
|
+
</aside>
|
|
262
|
+
|
|
263
|
+
<!-- Main Column -->
|
|
264
|
+
<div class="flex-1 min-w-0 flex flex-col">
|
|
265
|
+
<!-- Top Bar -->
|
|
266
|
+
<header class="h-16 bg-white border-b border-slate-200 px-4 sm:px-6 flex items-center justify-between gap-4 sticky top-0 z-20">
|
|
267
|
+
<div class="flex items-center gap-3 min-w-0">
|
|
268
|
+
<button
|
|
269
|
+
type="button"
|
|
270
|
+
@click="sidebarOpen = true"
|
|
271
|
+
class="lg:hidden p-2 rounded-lg text-slate-600 hover:bg-slate-100"
|
|
272
|
+
aria-label="Open sidebar"
|
|
273
|
+
>
|
|
274
|
+
<i data-lucide="menu" class="w-5 h-5"></i>
|
|
275
|
+
</button>
|
|
276
|
+
<h1 class="text-base sm:text-lg font-semibold text-slate-900 truncate">${safeTitle}</h1>
|
|
277
|
+
</div>
|
|
278
|
+
|
|
279
|
+
<div class="flex items-center gap-3 shrink-0">
|
|
280
|
+
<a
|
|
281
|
+
href="/"
|
|
282
|
+
target="_blank"
|
|
283
|
+
rel="noopener"
|
|
284
|
+
class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-slate-200 bg-white text-xs sm:text-sm font-medium text-slate-700 hover:bg-slate-50 transition"
|
|
285
|
+
>
|
|
286
|
+
<i data-lucide="external-link" class="w-3.5 h-3.5"></i>
|
|
287
|
+
<span class="hidden sm:inline">View Live Site</span>
|
|
288
|
+
</a>
|
|
289
|
+
|
|
290
|
+
<a
|
|
291
|
+
href="/admin/profile"
|
|
292
|
+
title="View your account details"
|
|
293
|
+
class="hidden sm:flex items-center gap-2 px-2.5 py-1 rounded-full bg-slate-100 border border-slate-200 text-xs hover:bg-slate-200/70 transition"
|
|
294
|
+
>
|
|
295
|
+
<span class="font-medium text-slate-800">${safeUserName}</span>
|
|
296
|
+
<span class="px-1.5 py-0.5 rounded bg-slate-900 text-white uppercase text-[10px] font-semibold">${safeUserRole}</span>
|
|
297
|
+
</a>
|
|
298
|
+
|
|
299
|
+
<button
|
|
300
|
+
type="button"
|
|
301
|
+
@click="logout()"
|
|
302
|
+
:disabled="loggingOut"
|
|
303
|
+
class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs sm:text-sm font-medium text-red-600 hover:bg-red-50 transition"
|
|
304
|
+
>
|
|
305
|
+
<i data-lucide="log-out" class="w-3.5 h-3.5"></i>
|
|
306
|
+
<span class="hidden sm:inline">Logout</span>
|
|
307
|
+
</button>
|
|
308
|
+
</div>
|
|
309
|
+
</header>
|
|
310
|
+
|
|
311
|
+
<!-- Main View Content -->
|
|
312
|
+
<main class="flex-1 p-4 sm:p-6 lg:p-8 max-w-7xl w-full mx-auto">
|
|
313
|
+
${content}
|
|
314
|
+
</main>
|
|
315
|
+
</div>
|
|
316
|
+
</div>
|
|
317
|
+
|
|
318
|
+
<!-- Global Toast Notification -->
|
|
319
|
+
<div
|
|
320
|
+
x-show="toast"
|
|
321
|
+
x-cloak
|
|
322
|
+
x-transition
|
|
323
|
+
class="fixed bottom-5 right-5 z-50 max-w-sm rounded-xl px-4 py-3 text-sm font-medium shadow-lg border flex items-center gap-2"
|
|
324
|
+
:class="toast?.type === 'error' ? 'bg-red-50 text-red-800 border-red-200' : 'bg-emerald-50 text-emerald-800 border-emerald-200'"
|
|
325
|
+
>
|
|
326
|
+
<span x-text="toast?.message"></span>
|
|
327
|
+
</div>
|
|
328
|
+
|
|
329
|
+
<!-- Reusable Media Picker Modal -->
|
|
330
|
+
<div
|
|
331
|
+
x-data="mediaPickerModal"
|
|
332
|
+
x-show="open"
|
|
333
|
+
x-cloak
|
|
334
|
+
class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-950/60"
|
|
335
|
+
>
|
|
336
|
+
<div @click.outside="open = false" class="bg-white rounded-2xl shadow-2xl border border-slate-200 w-full max-w-3xl max-h-[85vh] flex flex-col overflow-hidden">
|
|
337
|
+
<div class="px-5 py-4 border-b border-slate-200 flex flex-wrap items-center justify-between gap-3">
|
|
338
|
+
<div>
|
|
339
|
+
<h3 class="text-base font-semibold text-slate-900">Select Media</h3>
|
|
340
|
+
<p class="text-xs text-slate-500">Pick an existing upload or upload a new file inline.</p>
|
|
341
|
+
</div>
|
|
342
|
+
<div class="flex items-center gap-2">
|
|
343
|
+
<input
|
|
344
|
+
type="search"
|
|
345
|
+
x-model="search"
|
|
346
|
+
placeholder="Filter media..."
|
|
347
|
+
class="rounded-lg border border-slate-300 px-2.5 py-1.5 text-xs focus:border-blue-500 focus:outline-none"
|
|
348
|
+
>
|
|
349
|
+
<label class="cursor-pointer inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-slate-900 text-white text-xs font-medium hover:bg-slate-800">
|
|
350
|
+
<span x-text="uploading ? 'Uploading...' : '+ Upload File'"></span>
|
|
351
|
+
<input type="file" multiple accept=".webp,.png,.jpg,.jpeg,.gif,.pdf" class="hidden" @change="uploadInline($event)">
|
|
352
|
+
</label>
|
|
353
|
+
<button type="button" @click="open = false" class="p-1.5 rounded-lg text-slate-500 hover:bg-slate-100">
|
|
354
|
+
<i data-lucide="x" class="w-4 h-4"></i>
|
|
355
|
+
</button>
|
|
356
|
+
</div>
|
|
357
|
+
</div>
|
|
358
|
+
|
|
359
|
+
<div x-show="error" class="mx-5 mt-4 rounded-lg bg-red-50 border border-red-200 px-3 py-2 text-xs text-red-700" x-text="error"></div>
|
|
360
|
+
|
|
361
|
+
<div class="flex-1 overflow-y-auto p-5">
|
|
362
|
+
<div x-show="loading" class="py-12 text-center text-sm text-slate-500">Loading media...</div>
|
|
363
|
+
<div x-show="!loading && filteredFiles.length === 0" class="py-12 text-center text-sm text-slate-500">
|
|
364
|
+
No media files found.
|
|
365
|
+
</div>
|
|
366
|
+
|
|
367
|
+
<div x-show="!loading && filteredFiles.length > 0" class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-3">
|
|
368
|
+
<template x-for="file in filteredFiles" :key="file.id">
|
|
369
|
+
<button
|
|
370
|
+
type="button"
|
|
371
|
+
@click="choose(file)"
|
|
372
|
+
class="group text-left rounded-xl border border-slate-200 overflow-hidden hover:border-blue-500 hover:ring-2 hover:ring-blue-500/20 transition bg-slate-50"
|
|
373
|
+
>
|
|
374
|
+
<div class="aspect-square bg-slate-100 flex items-center justify-center overflow-hidden">
|
|
375
|
+
<img x-show="isImage(file)" :src="file.url" :alt="file.alt_text || file.original_name" class="w-full h-full object-cover group-hover:scale-105 transition">
|
|
376
|
+
<span x-show="!isImage(file)" class="px-2 py-1 rounded bg-slate-200 text-slate-700 text-xs font-mono uppercase">PDF</span>
|
|
377
|
+
</div>
|
|
378
|
+
<div class="p-2 bg-white border-t border-slate-200">
|
|
379
|
+
<p class="text-xs font-medium text-slate-800 truncate" x-text="file.original_name"></p>
|
|
380
|
+
</div>
|
|
381
|
+
</button>
|
|
382
|
+
</template>
|
|
383
|
+
</div>
|
|
384
|
+
</div>
|
|
385
|
+
</div>
|
|
386
|
+
</div>
|
|
387
|
+
|
|
388
|
+
<script>
|
|
389
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
390
|
+
window.lucide?.createIcons();
|
|
391
|
+
});
|
|
392
|
+
</script>
|
|
393
|
+
</body>
|
|
394
|
+
</html>`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Render a clean 403 Forbidden page inside the persistent admin shell.
|
|
399
|
+
*/
|
|
400
|
+
export function renderForbiddenPage(currentUser: CurrentUser, siteTitle: string): string {
|
|
401
|
+
return renderAdminLayout({
|
|
402
|
+
title: "Access Denied",
|
|
403
|
+
activeNav: "dashboard",
|
|
404
|
+
currentUser,
|
|
405
|
+
siteTitle,
|
|
406
|
+
content: `
|
|
407
|
+
<div class="max-w-lg mx-auto mt-12 bg-white rounded-2xl border border-red-200 p-8 text-center space-y-3 shadow-sm">
|
|
408
|
+
<div class="w-12 h-12 rounded-full bg-red-50 text-red-600 mx-auto flex items-center justify-center">
|
|
409
|
+
<i data-lucide="lock" class="w-6 h-6"></i>
|
|
410
|
+
</div>
|
|
411
|
+
<h2 class="text-lg font-semibold text-slate-900">Admin Role Required</h2>
|
|
412
|
+
<p class="text-sm text-slate-600">Your current role (<span class="font-mono">${escapeHtml(currentUser.role)}</span>) does not have permission to access this section.</p>
|
|
413
|
+
<div class="pt-2">
|
|
414
|
+
<a href="/admin" class="inline-flex items-center px-4 py-2 rounded-lg bg-slate-900 text-white text-sm font-medium hover:bg-slate-800">Return to Dashboard</a>
|
|
415
|
+
</div>
|
|
416
|
+
</div>
|
|
417
|
+
`,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { serializeSafeJson } from "../layout";
|
|
2
|
+
|
|
3
|
+
export interface CategoryListItem {
|
|
4
|
+
id: number;
|
|
5
|
+
name: string;
|
|
6
|
+
slug: string;
|
|
7
|
+
description: string;
|
|
8
|
+
post_count: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Render the WordPress-style two-column Categories Manager (/admin/categories).
|
|
13
|
+
*/
|
|
14
|
+
export function renderCategoriesPage(initialCategories: CategoryListItem[]): string {
|
|
15
|
+
const seed = serializeSafeJson({ categories: initialCategories });
|
|
16
|
+
|
|
17
|
+
return `
|
|
18
|
+
<script id="categories-seed" type="application/json">${seed}</script>
|
|
19
|
+
<script>
|
|
20
|
+
document.addEventListener("alpine:init", () => {
|
|
21
|
+
Alpine.data("categoriesManager", () => ({
|
|
22
|
+
categories: [],
|
|
23
|
+
editingId: null,
|
|
24
|
+
slugEdited: false,
|
|
25
|
+
form: { name: "", slug: "", description: "" },
|
|
26
|
+
errors: [],
|
|
27
|
+
saving: false,
|
|
28
|
+
confirmDeleteId: null,
|
|
29
|
+
|
|
30
|
+
init() {
|
|
31
|
+
const el = document.getElementById("categories-seed");
|
|
32
|
+
const seed = el ? JSON.parse(el.textContent || "{}") : {};
|
|
33
|
+
this.categories = Array.isArray(seed.categories) ? seed.categories : [];
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
slugify(v) {
|
|
37
|
+
return String(v || "").toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
onNameInput() {
|
|
41
|
+
if (!this.editingId && !this.slugEdited) {
|
|
42
|
+
this.form.slug = this.slugify(this.form.name);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
startEdit(cat) {
|
|
47
|
+
this.editingId = cat.id;
|
|
48
|
+
this.slugEdited = true;
|
|
49
|
+
this.errors = [];
|
|
50
|
+
this.form = { name: cat.name, slug: cat.slug, description: cat.description || "" };
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
resetForm() {
|
|
54
|
+
this.editingId = null;
|
|
55
|
+
this.slugEdited = false;
|
|
56
|
+
this.errors = [];
|
|
57
|
+
this.form = { name: "", slug: "", description: "" };
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
async reload() {
|
|
61
|
+
const res = await fetch("/api/admin/categories?t=" + Date.now(), { cache: "no-store" });
|
|
62
|
+
if (res.ok) {
|
|
63
|
+
const data = await res.json();
|
|
64
|
+
this.categories = Array.isArray(data.categories) ? data.categories : [];
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
async save() {
|
|
69
|
+
this.saving = true;
|
|
70
|
+
this.errors = [];
|
|
71
|
+
const url = this.editingId ? "/api/admin/categories/" + this.editingId : "/api/admin/categories";
|
|
72
|
+
const method = this.editingId ? "PUT" : "POST";
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
const res = await fetch(url, {
|
|
76
|
+
method,
|
|
77
|
+
headers: { "content-type": "application/json" },
|
|
78
|
+
body: JSON.stringify(this.form)
|
|
79
|
+
});
|
|
80
|
+
const data = await res.json();
|
|
81
|
+
if (!res.ok || !data.success) {
|
|
82
|
+
this.errors = Array.isArray(data.errors) ? data.errors : ["Failed to save category."];
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
await this.reload();
|
|
86
|
+
this.resetForm();
|
|
87
|
+
window.cmsToast("success", method === "POST" ? "Category created." : "Category updated.");
|
|
88
|
+
} catch {
|
|
89
|
+
this.errors = ["Network error while saving category."];
|
|
90
|
+
} finally {
|
|
91
|
+
this.saving = false;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
async remove(id) {
|
|
96
|
+
try {
|
|
97
|
+
const res = await fetch("/api/admin/categories/" + id, { method: "DELETE" });
|
|
98
|
+
const data = await res.json();
|
|
99
|
+
if (!res.ok || !data.success) {
|
|
100
|
+
window.cmsToast("error", (data.errors && data.errors[0]) || "Failed to delete category.");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (this.editingId === id) this.resetForm();
|
|
104
|
+
this.confirmDeleteId = null;
|
|
105
|
+
await this.reload();
|
|
106
|
+
window.cmsToast("success", "Category deleted.");
|
|
107
|
+
} catch {
|
|
108
|
+
window.cmsToast("error", "Network error while deleting category.");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}));
|
|
112
|
+
});
|
|
113
|
+
</script>
|
|
114
|
+
|
|
115
|
+
<div x-data="categoriesManager" class="grid grid-cols-1 lg:grid-cols-12 gap-6 items-start">
|
|
116
|
+
<!-- Left Form -->
|
|
117
|
+
<div class="lg:col-span-4 bg-white rounded-xl border border-slate-200 shadow-sm p-5">
|
|
118
|
+
<h2 class="text-base font-semibold text-slate-900 mb-4" x-text="editingId ? 'Edit Category' : 'Add New Category'"></h2>
|
|
119
|
+
|
|
120
|
+
<div x-show="errors.length > 0" x-cloak class="mb-4 rounded-lg bg-red-50 border border-red-200 p-3 text-xs text-red-700 space-y-1">
|
|
121
|
+
<template x-for="(err, idx) in errors" :key="idx">
|
|
122
|
+
<p x-text="err"></p>
|
|
123
|
+
</template>
|
|
124
|
+
</div>
|
|
125
|
+
|
|
126
|
+
<form @submit.prevent="save()" class="space-y-4">
|
|
127
|
+
<div>
|
|
128
|
+
<label for="cat-name" class="block text-xs font-medium text-slate-700 mb-1">Name</label>
|
|
129
|
+
<input
|
|
130
|
+
id="cat-name"
|
|
131
|
+
type="text"
|
|
132
|
+
required
|
|
133
|
+
x-model="form.name"
|
|
134
|
+
@input="onNameInput()"
|
|
135
|
+
placeholder="e.g. Tutorials"
|
|
136
|
+
class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
|
|
137
|
+
>
|
|
138
|
+
</div>
|
|
139
|
+
|
|
140
|
+
<div>
|
|
141
|
+
<label for="cat-slug" class="block text-xs font-medium text-slate-700 mb-1">Slug</label>
|
|
142
|
+
<input
|
|
143
|
+
id="cat-slug"
|
|
144
|
+
type="text"
|
|
145
|
+
required
|
|
146
|
+
x-model="form.slug"
|
|
147
|
+
@input="slugEdited = true"
|
|
148
|
+
placeholder="tutorials"
|
|
149
|
+
class="w-full rounded-lg border border-slate-300 px-3 py-2 font-mono text-sm focus:border-blue-500 focus:outline-none"
|
|
150
|
+
>
|
|
151
|
+
</div>
|
|
152
|
+
|
|
153
|
+
<div>
|
|
154
|
+
<label for="cat-desc" class="block text-xs font-medium text-slate-700 mb-1">Description</label>
|
|
155
|
+
<textarea
|
|
156
|
+
id="cat-desc"
|
|
157
|
+
rows="3"
|
|
158
|
+
x-model="form.description"
|
|
159
|
+
placeholder="Optional description..."
|
|
160
|
+
class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
|
|
161
|
+
></textarea>
|
|
162
|
+
</div>
|
|
163
|
+
|
|
164
|
+
<div class="flex items-center gap-2">
|
|
165
|
+
<button
|
|
166
|
+
type="submit"
|
|
167
|
+
:disabled="saving"
|
|
168
|
+
class="flex-1 px-4 py-2 rounded-lg bg-slate-900 text-white text-sm font-medium hover:bg-slate-800 disabled:opacity-50"
|
|
169
|
+
x-text="saving ? 'Saving...' : (editingId ? 'Update Category' : 'Add Category')"
|
|
170
|
+
></button>
|
|
171
|
+
<button x-show="editingId" type="button" @click="resetForm()" class="px-3.5 py-2 rounded-lg border border-slate-300 text-sm text-slate-700 hover:bg-slate-50">
|
|
172
|
+
Cancel
|
|
173
|
+
</button>
|
|
174
|
+
</div>
|
|
175
|
+
</form>
|
|
176
|
+
</div>
|
|
177
|
+
|
|
178
|
+
<!-- Right Table -->
|
|
179
|
+
<div class="lg:col-span-8 bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden">
|
|
180
|
+
<div class="px-5 py-3.5 border-b border-slate-200 flex items-center justify-between">
|
|
181
|
+
<span class="text-sm font-semibold text-slate-800">Categories (<span x-text="categories.length"></span>)</span>
|
|
182
|
+
</div>
|
|
183
|
+
|
|
184
|
+
<div class="overflow-x-auto">
|
|
185
|
+
<table class="w-full text-left border-collapse">
|
|
186
|
+
<thead>
|
|
187
|
+
<tr class="bg-slate-50 border-b border-slate-200 text-[11px] font-semibold uppercase tracking-wider text-slate-500">
|
|
188
|
+
<th class="px-5 py-3">Name</th>
|
|
189
|
+
<th class="px-5 py-3">Slug</th>
|
|
190
|
+
<th class="px-5 py-3">Description</th>
|
|
191
|
+
<th class="px-5 py-3 text-center">Posts</th>
|
|
192
|
+
<th class="px-5 py-3 text-right">Actions</th>
|
|
193
|
+
</tr>
|
|
194
|
+
</thead>
|
|
195
|
+
<tbody class="divide-y divide-slate-200">
|
|
196
|
+
<tr x-show="categories.length === 0">
|
|
197
|
+
<td colspan="5" class="px-5 py-8 text-center text-sm text-slate-500">No categories yet.</td>
|
|
198
|
+
</tr>
|
|
199
|
+
<template x-for="cat in categories" :key="cat.id">
|
|
200
|
+
<tr class="hover:bg-slate-50/70">
|
|
201
|
+
<td class="px-5 py-3.5 text-sm font-semibold text-slate-900" x-text="cat.name"></td>
|
|
202
|
+
<td class="px-5 py-3.5 text-xs font-mono text-slate-500" x-text="cat.slug"></td>
|
|
203
|
+
<td class="px-5 py-3.5 text-xs text-slate-600 max-w-xs truncate" x-text="cat.description || '—'"></td>
|
|
204
|
+
<td class="px-5 py-3.5 text-center">
|
|
205
|
+
<span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium bg-slate-100 text-slate-700" x-text="cat.post_count"></span>
|
|
206
|
+
</td>
|
|
207
|
+
<td class="px-5 py-3.5 text-right whitespace-nowrap space-x-2">
|
|
208
|
+
<button type="button" @click="startEdit(cat)" class="text-xs font-medium text-blue-600 hover:underline">Edit</button>
|
|
209
|
+
<button x-show="confirmDeleteId !== cat.id" type="button" @click="confirmDeleteId = cat.id" class="text-xs font-medium text-red-600 hover:underline">Delete</button>
|
|
210
|
+
<span x-show="confirmDeleteId === cat.id" class="inline-flex items-center gap-1">
|
|
211
|
+
<button type="button" @click="remove(cat.id)" class="px-2 py-0.5 rounded bg-red-600 text-white text-xs font-medium">Confirm</button>
|
|
212
|
+
<button type="button" @click="confirmDeleteId = null" class="px-2 py-0.5 rounded bg-slate-100 text-slate-600 text-xs font-medium">Cancel</button>
|
|
213
|
+
</span>
|
|
214
|
+
</td>
|
|
215
|
+
</tr>
|
|
216
|
+
</template>
|
|
217
|
+
</tbody>
|
|
218
|
+
</table>
|
|
219
|
+
</div>
|
|
220
|
+
</div>
|
|
221
|
+
</div>
|
|
222
|
+
`;
|
|
223
|
+
}
|