source: Klonkt/src/views/pages/post-edit.ejs@ 7bc636b

main
Last change on this file since 7bc636b was 7bc636b, checked in by Robin <robin@…>, 4 months ago

Initial commit — PrutFolio v1 source (pulled from Hetzner /srv/prutfolio)

  • Property mode set to 100644
File size: 52.2 KB
Line 
1<%
2// Post editor — split into card sections for visual hierarchy:
3// - Identity : title, slug, excerpt
4// - Cover : thumbnail + URL + custom upload button
5// - Content : markdown textarea with attached toolbar
6// - Meta : status, type, pin, noindex, tags
7//
8// All behavior (IDs, names, upload endpoints) is preserved — only layout
9// and styling changed.
10const _hasCover = !!post.cover_image_url;
11%>
12<div class="container post-edit-page">
13 <h1><%= isNew ? 'Nieuwe post' : 'Post bewerken' %></h1>
14
15 <form method="post" action="<%= isNew ? '/posts/create' : '/posts/' + post.slug + '/save' %>" class="post-edit-form">
16
17 <%# ── IDENTITY ─────────────────────────────────────────────── %>
18 <section class="pe-card">
19 <label class="pe-field">
20 <span>Titel</span>
21 <input type="text" name="title" value="<%= post.title %>" required autofocus>
22 </label>
23 <div class="pe-row pe-row-2">
24 <label class="pe-field">
25 <span>Slug (URL)</span>
26 <input type="text" name="slug" value="<%= post.slug %>" placeholder="auto van titel als leeg">
27 </label>
28 <label class="pe-field">
29 <span>Tags <small>(komma-gescheiden)</small></span>
30 <input type="text" name="tags" value="<%= Array.isArray(post.tags) ? post.tags.join(', ') : '' %>">
31 </label>
32 </div>
33 <label class="pe-field">
34 <span>Excerpt <small>(preview tekst)</small></span>
35 <textarea name="excerpt" rows="2"><%= post.excerpt %></textarea>
36 </label>
37 </section>
38
39 <%# ── COVER ────────────────────────────────────────────────── %>
40 <section class="pe-card">
41 <div class="pe-section-title">Cover</div>
42 <div class="pe-cover-row">
43 <div class="pe-cover-thumb" id="cover-preview-wrap" <%= _hasCover ? '' : 'data-empty' %>>
44 <img src="<%= _hasCover ? post.cover_image_url : '' %>" alt="" id="cover-preview-img" <%= _hasCover ? '' : 'hidden' %>>
45 <% if (!_hasCover) { %><span class="pe-cover-empty">🖼</span><% } %>
46 </div>
47 <div class="pe-cover-actions">
48 <label class="pe-field">
49 <span>Cover URL</span>
50 <input type="url" name="cover_image_url" id="cover-url-field"
51 value="<%= post.cover_image_url || '' %>"
52 placeholder="https://… of upload met de knop">
53 </label>
54 <div class="pe-cover-upload">
55 <button type="button" class="pe-btn pe-btn-secondary" id="cover-upload-trigger">
56 📷 Upload nieuwe cover
57 </button>
58 <input type="file" id="cover-upload-field" accept="image/jpeg,image/png,image/webp,image/gif" hidden>
59 <small id="cover-upload-status" class="pe-status"></small>
60 </div>
61 </div>
62 </div>
63 </section>
64
65 <%# ── CONTENT ──────────────────────────────────────────────── %>
66 <section class="pe-card pe-card-content">
67 <div class="pe-section-title">
68 Content
69 <small>Sleep een afbeelding om in te voegen · selecteer tekst om op te maken</small>
70 </div>
71 <div class="pe-editor-frame">
72 <div class="pe-toolbar" id="pe-toolbar" role="toolbar" aria-label="Format">
73 <button type="button" data-cmd="bold" title="Vet (Ctrl+B)" aria-label="Vet">
74 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 4h8a4 4 0 0 1 0 8H6z"/><path d="M6 12h9a4 4 0 0 1 0 8H6z"/></svg>
75 </button>
76 <button type="button" data-cmd="italic" title="Cursief (Ctrl+I)" aria-label="Cursief">
77 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="19" y1="4" x2="10" y2="4"/><line x1="14" y1="20" x2="5" y2="20"/><line x1="15" y1="4" x2="9" y2="20"/></svg>
78 </button>
79 <button type="button" data-cmd="underline" title="Onderstreept" aria-label="Onderstreept">
80 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 4v6a6 6 0 0 0 12 0V4"/><line x1="4" y1="20" x2="20" y2="20"/></svg>
81 </button>
82 <span class="pe-toolbar-sep" aria-hidden="true"></span>
83 <button type="button" class="pe-tb-text" data-cmd="formatBlock" data-arg="h2" title="Kop">H2</button>
84 <button type="button" class="pe-tb-text" data-cmd="formatBlock" data-arg="h3" title="Subkop">H3</button>
85 <button type="button" class="pe-tb-text" data-cmd="formatBlock" data-arg="p" title="Paragraaf">¶</button>
86 <span class="pe-toolbar-sep" aria-hidden="true"></span>
87 <button type="button" data-cmd="insertUnorderedList" title="Lijst" aria-label="Lijst">
88 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="9" y1="6" x2="20" y2="6"/><line x1="9" y1="12" x2="20" y2="12"/><line x1="9" y1="18" x2="20" y2="18"/><circle cx="4.5" cy="6" r="1.2"/><circle cx="4.5" cy="12" r="1.2"/><circle cx="4.5" cy="18" r="1.2"/></svg>
89 </button>
90 <button type="button" data-cmd="insertOrderedList" title="Genummerde lijst" aria-label="Genummerde lijst">
91 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="10" y1="6" x2="21" y2="6"/><line x1="10" y1="12" x2="21" y2="12"/><line x1="10" y1="18" x2="21" y2="18"/><path d="M4 6h1v4"/><path d="M4 10h2"/><path d="M6 18H4c0-1 2-2 2-3s-1-1.5-2-1"/></svg>
92 </button>
93 <button type="button" data-cmd="formatBlock" data-arg="blockquote" title="Quote" aria-label="Quote">
94 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 21c3 0 7-1 7-8V5c0-1.25-.75-2-2-2H4c-1.25 0-2 .75-2 2v6c0 1.25.75 2 2 2h2.5C6 17.5 4 19 3 19v2z"/><path d="M14 21c3 0 7-1 7-8V5c0-1.25-.75-2-2-2h-4c-1.25 0-2 .75-2 2v6c0 1.25.75 2 2 2h2.5c-.5 4.5-2.5 6-3.5 6v2z"/></svg>
95 </button>
96 <button type="button" data-cmd="link-prompt" title="Link (Ctrl+K)" aria-label="Link">
97 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>
98 </button>
99 <button type="button" data-cmd="code-wrap" title="Code (inline)" aria-label="Code">
100 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/></svg>
101 </button>
102 <span class="pe-toolbar-sep" aria-hidden="true"></span>
103 <button type="button" id="insert-image-btn" title="Afbeelding invoegen" aria-label="Afbeelding">
104 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>
105 </button>
106 <button type="button" id="insert-track-btn" title="Track invoegen" aria-label="Track">
107 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 18V5l12-2v13"/><circle cx="6" cy="18" r="3"/><circle cx="18" cy="16" r="3"/></svg>
108 </button>
109 <button type="button" id="insert-playlist-btn" title="Playlist invoegen" aria-label="Playlist">
110 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="15" y2="18"/><polygon points="3 5 3 13 9 9"/></svg>
111 </button>
112 <span class="pe-toolbar-spacer"></span>
113 <button type="button" data-cmd="removeFormat" title="Wis opmaak" aria-label="Wis opmaak">
114 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M5 5l14 14" stroke-linecap="round"/></svg>
115 </button>
116 </div>
117
118 <div id="content-editor"
119 class="pe-editor"
120 contenteditable="true"
121 role="textbox"
122 aria-multiline="true"
123 aria-label="Content"
124 data-placeholder="Begin met schrijven…"></div>
125
126 <%# Hidden field is what actually submits to the server. The visible
127 contenteditable's serialized HTML is copied here on submit. %>
128 <input type="hidden" name="content" id="content-hidden" value="">
129
130 <%# Initial content is injected as a JSON string in a script tag so we
131 can set innerHTML safely from JS. EJS-escaping HTML directly into
132 the editor would render entity-escaped tags as text. %>
133 <script id="initial-content" type="application/json"><%- JSON.stringify(post.content || '').replace(/</g, '\\u003c') %></script>
134
135 <div class="pe-editor-status">
136 <small id="content-upload-status" class="pe-status"></small>
137 <span class="pe-char-count"><span id="char-count">0</span> tekens</span>
138 </div>
139
140 <input type="file" id="content-upload-field" accept="image/jpeg,image/png,image/webp,image/gif" hidden>
141 </div>
142 </section>
143
144 <%# ── META ─────────────────────────────────────────────────── %>
145 <section class="pe-card">
146 <div class="pe-section-title">Publicatie</div>
147 <div class="pe-row pe-row-2">
148 <label class="pe-field">
149 <span>Status</span>
150 <select name="status">
151 <option value="draft" <%= post.status === 'draft' ? 'selected' : '' %>>Draft</option>
152 <option value="published" <%= post.status === 'published' ? 'selected' : '' %>>Published</option>
153 <option value="archived" <%= post.status === 'archived' ? 'selected' : '' %>>Archived</option>
154 </select>
155 </label>
156 <label class="pe-field">
157 <span>Type</span>
158 <select name="type">
159 <% var ptype = post.type || 'post'; %>
160 <option value="post" <%= ptype === 'post' ? 'selected' : '' %>>Post</option>
161 <option value="foto" <%= ptype === 'foto' ? 'selected' : '' %>>Foto</option>
162 <option value="video" <%= ptype === 'video' ? 'selected' : '' %>>Video</option>
163 <option value="audio" <%= ptype === 'audio' ? 'selected' : '' %>>Audio</option>
164 </select>
165 </label>
166 </div>
167 <div class="pe-checkboxes">
168 <label class="pe-pinned-rank">
169 <span>📌 Pinned rank</span>
170 <input type="number" name="pinned" min="0" step="1"
171 inputmode="numeric" pattern="[0-9]*"
172 value="<%= post.pinned || 0 %>"
173 title="0 = niet pinned. 1 = bovenaan, 2 = daaronder, etc.">
174 <small>0 = niet pinned · 1 = bovenaan · 2 daaronder · …</small>
175 </label>
176 <label class="pe-checkbox">
177 <input type="checkbox" name="noindex" value="1" <%= post.noindex ? 'checked' : '' %>>
178 <span>🚫 noindex (verberg voor zoekmachines)</span>
179 </label>
180 </div>
181 </section>
182
183 <%# ── ACTIONS (sticky at bottom) ──────────────────────────── %>
184 <div class="pe-actions">
185 <a href="<%= isNew ? '/' : '/' + post.slug %>" class="pe-btn">Annuleren</a>
186 <div class="pe-actions-spacer"></div>
187 <% if (!isNew && post.status !== 'published') { %>
188 <button type="submit" name="action" value="publish" class="pe-btn pe-btn-success">📤 Publish</button>
189 <% } %>
190 <button type="submit" class="pe-btn pe-btn-primary">💾 Opslaan</button>
191 </div>
192 </form>
193</div>
194
195<style>
196/* ─── Page wrapper ──────────────────────────────────────────────── */
197.post-edit-page {
198 max-width: 880px;
199 margin: 1.5rem auto 6rem; /* extra bottom for sticky-actions clearance */
200 padding: 0 1rem;
201}
202.post-edit-page h1 {
203 font-family: var(--font-display, serif);
204 margin: 0 0 1.25rem;
205 font-size: 1.75rem;
206}
207
208.post-edit-form {
209 display: flex; flex-direction: column;
210 gap: 1rem;
211}
212
213/* ─── Card sections ─────────────────────────────────────────────── */
214.pe-card {
215 background: var(--paper);
216 border: 1px solid var(--rule);
217 border-radius: 12px;
218 padding: 1.25rem;
219 display: flex; flex-direction: column;
220 gap: 0.85rem;
221}
222.pe-card-content { padding-bottom: 0; overflow: hidden; } /* editor flush to bottom */
223.pe-section-title {
224 font-size: 0.8rem;
225 font-weight: 600;
226 text-transform: uppercase;
227 letter-spacing: 0.05em;
228 color: var(--ink-soft);
229 display: flex; align-items: baseline; gap: 0.5rem;
230 flex-wrap: wrap;
231}
232.pe-section-title small {
233 font-size: 0.75rem; font-weight: 400;
234 letter-spacing: 0; text-transform: none;
235 color: var(--ink-muted, var(--ink-soft));
236}
237
238/* ─── Field primitives ──────────────────────────────────────────── */
239.pe-field {
240 display: flex; flex-direction: column;
241 gap: 0.35rem;
242 min-width: 0; /* allow grid items to shrink */
243}
244.pe-field > span {
245 font-size: 0.8rem;
246 font-weight: 600;
247 color: var(--ink-soft);
248 display: flex; align-items: baseline; gap: 0.4rem;
249}
250.pe-field > span small {
251 font-weight: 400; color: var(--ink-muted);
252}
253.pe-field input,
254.pe-field textarea,
255.pe-field select {
256 width: 100%;
257 box-sizing: border-box;
258 display: block;
259 padding: 0.6rem 0.75rem;
260 border: 1px solid var(--rule);
261 border-radius: 6px;
262 background: var(--paper-2);
263 color: var(--ink);
264 font-family: var(--font-ui, system-ui), sans-serif;
265 font-size: 0.95rem;
266 -webkit-appearance: none;
267 appearance: none;
268 transition: border-color 120ms;
269}
270.pe-field input:focus,
271.pe-field textarea:focus,
272.pe-field select:focus {
273 outline: 2px solid var(--accent);
274 outline-offset: -1px;
275 border-color: var(--accent);
276}
277.pe-field textarea {
278 font-family: var(--font-mono, monospace);
279 resize: vertical;
280}
281.pe-field select {
282 /* Restore the dropdown arrow we removed with appearance:none */
283 background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23999' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
284 background-repeat: no-repeat;
285 background-position: right 0.75rem center;
286 padding-right: 2rem;
287}
288
289.pe-row {
290 display: grid;
291 gap: 0.75rem;
292}
293.pe-row-2 { grid-template-columns: 1fr 1fr; }
294@media (max-width: 600px) {
295 .pe-row-2 { grid-template-columns: 1fr; }
296}
297
298/* ─── Cover field ───────────────────────────────────────────────── */
299.pe-cover-row {
300 display: grid;
301 grid-template-columns: 120px 1fr;
302 gap: 1rem;
303 align-items: start;
304}
305.pe-cover-thumb {
306 width: 120px; height: 120px;
307 border-radius: 10px;
308 overflow: hidden;
309 background: var(--paper-2);
310 border: 1px solid var(--rule);
311 display: flex; align-items: center; justify-content: center;
312 position: relative;
313}
314.pe-cover-thumb[data-empty] {
315 border-style: dashed;
316}
317.pe-cover-thumb img {
318 width: 100%; height: 100%;
319 object-fit: cover; display: block;
320}
321/* Honor HTML hidden — our display:block above otherwise renders an empty
322 broken-image icon when no cover is set. */
323.pe-cover-thumb img[hidden] { display: none; }
324.pe-cover-empty {
325 font-size: 2rem;
326 color: var(--ink-muted, var(--ink-soft));
327 opacity: 0.5;
328}
329.pe-cover-actions {
330 display: flex; flex-direction: column;
331 gap: 0.6rem;
332 min-width: 0;
333}
334.pe-cover-upload {
335 display: flex; align-items: center; gap: 0.6rem;
336 flex-wrap: wrap;
337}
338@media (max-width: 600px) {
339 .pe-cover-row { grid-template-columns: 88px 1fr; }
340 .pe-cover-thumb { width: 88px; height: 88px; }
341}
342
343/* ─── Editor frame (WYSIWYG: top toolbar, contenteditable body, status bar) ─── */
344.pe-editor-frame {
345 margin: 0 -1.25rem -1.25rem; /* break out of card padding for flush edges */
346 border-top: 1px solid var(--rule);
347 background: var(--paper);
348 display: flex; flex-direction: column;
349}
350
351/* Top toolbar — sticks to the top of the viewport while editing */
352.pe-toolbar {
353 display: flex; align-items: center; gap: .15rem;
354 flex-wrap: wrap;
355 padding: .4rem .75rem;
356 background: color-mix(in srgb, var(--paper) 92%, transparent);
357 backdrop-filter: blur(10px);
358 -webkit-backdrop-filter: blur(10px);
359 border-bottom: 1px solid var(--rule);
360 position: sticky;
361 top: 0;
362 z-index: 10;
363}
364.pe-toolbar button {
365 display: inline-flex; align-items: center; justify-content: center;
366 width: 32px; height: 32px;
367 padding: 0;
368 background: transparent;
369 border: 1px solid transparent;
370 border-radius: 6px;
371 color: var(--ink);
372 cursor: pointer;
373 font-family: var(--font-ui, system-ui), sans-serif;
374 font-weight: 600; font-size: .85rem;
375 transition: background var(--transition), color var(--transition), border-color var(--transition);
376 -webkit-tap-highlight-color: transparent;
377}
378.pe-toolbar button:hover {
379 background: var(--paper-2);
380 color: var(--accent);
381}
382.pe-toolbar button:active { transform: scale(.94); }
383.pe-toolbar button.is-active {
384 background: color-mix(in srgb, var(--accent) 14%, var(--paper-2));
385 color: var(--accent);
386 border-color: color-mix(in srgb, var(--accent) 30%, transparent);
387}
388.pe-toolbar button svg { width: 16px; height: 16px; }
389.pe-toolbar button.pe-tb-text { font-family: var(--font-display, serif); font-weight: 700; }
390.pe-toolbar-sep {
391 width: 1px; height: 18px;
392 background: var(--rule);
393 margin: 0 .35rem;
394 flex-shrink: 0;
395}
396.pe-toolbar-spacer { flex: 1; }
397
398/* Editor body — looks like prose, behaves like a textarea */
399.pe-editor {
400 width: 100%;
401 box-sizing: border-box;
402 min-height: 420px;
403 padding: 1.25rem 1.5rem;
404 border: 0;
405 background: transparent;
406 color: var(--ink);
407 font-family: var(--font-body, system-ui), serif;
408 font-size: 1.0625rem;
409 line-height: 1.65;
410 outline: none;
411 overflow-y: auto;
412}
413.pe-editor:focus { outline: none; }
414.pe-editor.is-dragover {
415 outline: 2px dashed var(--accent);
416 outline-offset: -10px;
417}
418.pe-editor[data-placeholder]:empty::before,
419.pe-editor[data-placeholder]:has(br:only-child)::before {
420 content: attr(data-placeholder);
421 color: var(--ink-muted, #999);
422 pointer-events: none;
423 opacity: .55;
424}
425/* Inline prose styles inside the editor — match the public post styling so
426 what you see is roughly what you'll get. Margins are tighter than on the
427 live site since this is a confined writing space. */
428.pe-editor h1, .pe-editor h2, .pe-editor h3, .pe-editor h4 {
429 font-family: var(--font-display, serif);
430 line-height: 1.2;
431 margin: 1.1rem 0 .35rem;
432}
433.pe-editor h1 { font-size: 1.85rem; }
434.pe-editor h2 { font-size: 1.45rem; }
435.pe-editor h3 { font-size: 1.2rem; }
436.pe-editor p { margin: 0 0 .85em; }
437.pe-editor ul, .pe-editor ol { margin: 0 0 .85em 1.4em; padding: 0; }
438.pe-editor li { margin: .15em 0; }
439.pe-editor blockquote {
440 margin: 1em 0;
441 padding: .15em 0 .15em 1em;
442 border-left: 3px solid var(--accent);
443 color: var(--ink-soft, var(--ink));
444 font-style: italic;
445}
446.pe-editor code {
447 background: var(--paper-2);
448 border: 1px solid var(--rule);
449 border-radius: 4px;
450 padding: .1em .35em;
451 font-family: var(--font-mono, ui-monospace, monospace);
452 font-size: .92em;
453}
454.pe-editor a { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
455.pe-editor img {
456 max-width: 100%; height: auto;
457 border-radius: 6px;
458 display: block;
459 margin: 1em auto;
460}
461
462/* Shortcode chips — visible inline placeholder in the editor for
463 [[track:UUID]] / [[album:Name]] / [[playlist:slug]]. They serialize back
464 to plain shortcode text on submit. */
465.sc-chip {
466 display: inline-flex; align-items: center; gap: .3rem;
467 padding: .15em .5em;
468 margin: 0 .15em;
469 background: color-mix(in srgb, var(--accent) 10%, var(--paper-2));
470 border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--rule));
471 border-radius: 999px;
472 color: var(--accent);
473 font-family: var(--font-ui, system-ui), sans-serif;
474 font-size: .85em;
475 font-weight: 600;
476 font-style: normal;
477 white-space: nowrap;
478 user-select: none;
479 cursor: default;
480 vertical-align: baseline;
481}
482.sc-chip-icon { display: inline-flex; opacity: .8; }
483.sc-chip-icon svg { width: 12px; height: 12px; }
484
485/* Status bar below the editor */
486.pe-editor-status {
487 display: flex; align-items: center; justify-content: space-between;
488 gap: .75rem;
489 padding: .45rem 1rem;
490 border-top: 1px solid var(--rule);
491 background: var(--paper-2);
492 color: var(--ink-muted, var(--ink-soft));
493 font-size: .78rem;
494}
495.pe-char-count { font-variant-numeric: tabular-nums; }
496
497/* ─── Track picker modal (P59) ─────────────────────────────────
498 Mobile-first: full-screen bottom-sheet on phones, centered card
499 on desktop. Lock body scroll while open via .tp-locked on body. */
500.tp-modal {
501 position: fixed; inset: 0;
502 z-index: 1000;
503 display: flex; align-items: stretch; justify-content: center;
504 /* Avoid the iOS home-indicator + the keyboard when search is focused */
505 padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
506}
507.tp-modal[hidden] { display: none; }
508.tp-backdrop {
509 position: absolute; inset: 0;
510 background: color-mix(in srgb, var(--ink) 55%, transparent);
511 backdrop-filter: blur(4px);
512 -webkit-backdrop-filter: blur(4px);
513 animation: tp-bd-in .18s ease-out;
514}
515@keyframes tp-bd-in { from { opacity: 0 } to { opacity: 1 } }
516
517/* Mobile: sheet docks to the bottom and fills viewport almost completely */
518.tp-sheet {
519 position: relative;
520 margin-top: auto;
521 width: 100%;
522 max-height: calc(100dvh - env(safe-area-inset-top) - 1rem);
523 background: var(--paper);
524 border-top: 1px solid var(--rule);
525 border-radius: 16px 16px 0 0;
526 box-shadow:
527 0 -2px 8px color-mix(in srgb, var(--ink) 8%, transparent),
528 0 -16px 48px color-mix(in srgb, var(--ink) 18%, transparent);
529 display: flex; flex-direction: column;
530 animation: tp-sheet-up .22s cubic-bezier(.2, .8, .25, 1);
531 overflow: hidden;
532}
533@keyframes tp-sheet-up {
534 from { transform: translateY(100%); opacity: .8; }
535 to { transform: translateY(0); opacity: 1; }
536}
537
538/* Header — title + close, with a small grab-handle bar on mobile */
539.tp-header {
540 display: flex; align-items: center; justify-content: space-between;
541 gap: .75rem;
542 padding: .85rem 1rem .65rem;
543 border-bottom: 1px solid var(--rule);
544 position: relative;
545}
546.tp-header::before {
547 /* Grab-handle: visible on mobile only */
548 content: '';
549 position: absolute;
550 top: .35rem; left: 50%;
551 transform: translateX(-50%);
552 width: 38px; height: 4px;
553 background: var(--rule-2, var(--rule));
554 border-radius: 2px;
555}
556.tp-h2 {
557 margin: .35rem 0 0;
558 font-family: var(--font-display, serif);
559 font-size: 1.15rem; font-weight: 600;
560 color: var(--ink);
561 line-height: 1.1;
562}
563.tp-close {
564 width: 32px; height: 32px; border-radius: 8px;
565 display: inline-flex; align-items: center; justify-content: center;
566 background: transparent; border: 1px solid transparent;
567 color: var(--ink-muted, var(--ink-soft));
568 cursor: pointer;
569 transition: background var(--transition), color var(--transition), border-color var(--transition);
570 -webkit-tap-highlight-color: transparent;
571 margin-top: .35rem;
572}
573.tp-close:hover, .tp-close:focus-visible {
574 background: var(--paper-2);
575 color: var(--ink);
576 outline: none;
577}
578.tp-close svg { width: 18px; height: 18px; }
579
580/* Search row — sticky just below header so list scrolls underneath */
581.tp-search-row {
582 position: relative;
583 padding: .65rem 1rem;
584 border-bottom: 1px solid var(--rule);
585 background: var(--paper);
586}
587.tp-search-icon {
588 position: absolute;
589 top: 50%; left: 1.65rem;
590 transform: translateY(-50%);
591 color: var(--ink-muted, var(--ink-soft));
592 pointer-events: none;
593 display: inline-flex;
594}
595.tp-search-icon svg { width: 16px; height: 16px; }
596.tp-search {
597 width: 100%; box-sizing: border-box;
598 padding: .65rem .85rem .65rem 2.4rem;
599 font-family: var(--font-ui, system-ui), sans-serif;
600 font-size: 1rem;
601 background: var(--paper-2);
602 color: var(--ink);
603 border: 1px solid var(--rule);
604 border-radius: 9px;
605 -webkit-appearance: none;
606 appearance: none;
607}
608.tp-search:focus {
609 outline: none;
610 border-color: var(--accent);
611 box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
612}
613
614/* List — vertically scrollable, takes remaining sheet height */
615.tp-list {
616 flex: 1 1 auto;
617 overflow-y: auto;
618 -webkit-overflow-scrolling: touch;
619 padding: .35rem .35rem 1rem;
620}
621.tp-empty {
622 padding: 1.5rem 1rem;
623 text-align: center;
624 color: var(--ink-muted, var(--ink-soft));
625 font-size: .9rem;
626}
627
628/* Track row — tap target ≥ 56px for mobile */
629.tp-row {
630 display: grid;
631 grid-template-columns: 44px 1fr auto;
632 align-items: center;
633 gap: .75rem;
634 width: 100%;
635 padding: .55rem .65rem;
636 border: 0; background: transparent;
637 border-radius: 10px;
638 text-align: left;
639 cursor: pointer;
640 color: inherit;
641 font: inherit;
642 transition: background var(--transition);
643 -webkit-tap-highlight-color: transparent;
644}
645.tp-row:hover, .tp-row:focus-visible {
646 background: color-mix(in srgb, var(--accent) 8%, var(--paper-2));
647 outline: none;
648}
649.tp-row:active { transform: scale(.98); }
650.tp-row[aria-disabled="true"] { opacity: .55; cursor: not-allowed; }
651
652.tp-cover {
653 width: 44px; height: 44px;
654 border-radius: 6px;
655 background-size: cover; background-position: center;
656 background-color: var(--rule);
657 display: inline-flex; align-items: center; justify-content: center;
658 flex-shrink: 0;
659}
660.tp-cover-empty {
661 background: linear-gradient(135deg,
662 color-mix(in srgb, var(--accent) 30%, var(--paper-2)),
663 color-mix(in srgb, var(--accent) 8%, var(--paper-2)));
664 color: var(--accent);
665}
666.tp-cover-empty svg { width: 18px; height: 18px; opacity: .8; }
667
668.tp-meta {
669 display: flex; flex-direction: column;
670 min-width: 0;
671 line-height: 1.25;
672}
673.tp-row-title {
674 font-weight: 600; font-size: .94rem;
675 color: var(--ink);
676 white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
677}
678.tp-row-artist {
679 font-size: .8rem;
680 color: var(--ink-muted, var(--ink-soft));
681 white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
682 margin-top: .1rem;
683}
684.tp-duration {
685 font-variant-numeric: tabular-nums;
686 font-size: .8rem;
687 color: var(--ink-muted, var(--ink-soft));
688 flex-shrink: 0;
689}
690
691/* Body lock when modal open */
692body.tp-locked { overflow: hidden; touch-action: none; }
693
694/* Desktop: centered card, max-width ~520px */
695@media (min-width: 640px) {
696 .tp-modal {
697 align-items: center;
698 padding: 1.5rem;
699 }
700 .tp-sheet {
701 margin-top: 0;
702 width: 100%;
703 max-width: 520px;
704 max-height: 80dvh;
705 border-radius: 14px;
706 border: 1px solid var(--rule);
707 animation: tp-sheet-pop .2s cubic-bezier(.2, .8, .25, 1);
708 }
709 .tp-header::before { display: none; } /* hide grab-handle on desktop */
710 .tp-header { padding: 1rem 1.25rem .75rem; }
711 .tp-h2 { margin-top: 0; font-size: 1.25rem; }
712 .tp-close { margin-top: 0; }
713 @keyframes tp-sheet-pop {
714 from { transform: translateY(-8px) scale(.97); opacity: 0; }
715 to { transform: translateY(0) scale(1); opacity: 1; }
716 }
717}
718
719/* ─── Buttons ───────────────────────────────────────────────────── */
720.pe-btn {
721 display: inline-flex; align-items: center; gap: 0.4rem;
722 padding: 0.55rem 1rem;
723 border: 1px solid var(--rule);
724 background: var(--paper-2);
725 color: var(--ink);
726 font-family: var(--font-ui, system-ui), sans-serif;
727 font-size: 0.9rem; font-weight: 500;
728 text-decoration: none;
729 border-radius: 6px;
730 cursor: pointer;
731 transition: background 120ms, border-color 120ms;
732 min-height: 40px;
733 -webkit-tap-highlight-color: transparent;
734}
735.pe-btn:hover { border-color: var(--accent); }
736.pe-btn:active { transform: scale(0.97); }
737.pe-btn-tiny {
738 padding: 0.35rem 0.7rem;
739 font-size: 0.85rem;
740 min-height: 32px;
741}
742.pe-btn-secondary { background: var(--paper-2); }
743.pe-btn-primary {
744 background: var(--accent); color: white;
745 border-color: var(--accent);
746}
747.pe-btn-primary:hover { opacity: 0.92; border-color: var(--accent); }
748.pe-btn-success {
749 background: #16a34a; color: white;
750 border-color: #16a34a;
751}
752.pe-btn-success:hover { opacity: 0.92; border-color: #16a34a; }
753
754.pe-status {
755 color: var(--ink-muted, var(--ink-soft));
756 font-size: 0.8rem;
757}
758.pe-status.is-error { color: #dc2626; }
759
760/* ─── Checkboxes (clean inline row) ─────────────────────────────── */
761.pe-checkboxes {
762 display: flex; flex-direction: column;
763 gap: 0.5rem;
764 padding-top: 0.25rem;
765}
766.pe-checkbox {
767 display: inline-flex; align-items: center; gap: 0.5rem;
768 cursor: pointer;
769 font-size: 0.95rem;
770 color: var(--ink);
771 user-select: none;
772}
773.pe-checkbox input[type="checkbox"] {
774 width: 18px; height: 18px;
775 margin: 0;
776 cursor: pointer;
777 accent-color: var(--accent);
778}
779
780/* Pinned rank: replaces the old boolean checkbox with a number input.
781 Stack the label / input / hint vertically — small footprint. */
782.pe-pinned-rank {
783 display: flex; flex-direction: column; gap: 0.25rem;
784 font-size: 0.95rem;
785 color: var(--ink);
786}
787.pe-pinned-rank input[type="number"] {
788 width: 80px;
789 padding: 0.4rem 0.6rem; min-height: 36px;
790 border: 1px solid var(--rule); border-radius: 6px;
791 background: var(--paper-2); color: var(--ink);
792 font-family: var(--font-ui, system-ui), sans-serif;
793 font-size: 0.95rem;
794 font-variant-numeric: tabular-nums;
795 -webkit-appearance: none; appearance: textfield;
796}
797.pe-pinned-rank input[type="number"]:focus {
798 outline: 2px solid var(--accent); outline-offset: -1px;
799 border-color: var(--accent);
800}
801.pe-pinned-rank small {
802 font-size: 0.78rem;
803 color: var(--ink-muted, var(--ink-soft));
804}
805
806/* ─── Sticky action footer ──────────────────────────────────────── */
807.pe-actions {
808 position: sticky;
809 bottom: 0;
810 display: flex; align-items: center;
811 gap: 0.5rem;
812 padding: 0.85rem 1rem;
813 margin: 0.5rem -1rem 0; /* extend to viewport edges on tight container */
814 background: color-mix(in srgb, var(--paper) 94%, transparent);
815 -webkit-backdrop-filter: blur(8px);
816 backdrop-filter: blur(8px);
817 border-top: 1px solid var(--rule);
818 z-index: 10;
819}
820.pe-actions-spacer { flex: 1; }
821
822/* On a body that already has a fixed bottom-tab nav (mobile), nudge the
823 sticky footer up so it doesn't sit under it. The bottom-tab is ~64px tall. */
824body.has-bottom-tab .pe-actions {
825 bottom: 64px;
826}
827</style>
828
829<script>
830(function() {
831
832 // ── Cover upload ────────────────────────────────────────────────
833 const coverField = document.getElementById('cover-upload-field');
834 const coverTrigger = document.getElementById('cover-upload-trigger');
835 const coverUrl = document.getElementById('cover-url-field');
836 const coverStatus = document.getElementById('cover-upload-status');
837 const coverWrap = document.getElementById('cover-preview-wrap');
838 const coverImg = document.getElementById('cover-preview-img');
839
840 async function uploadImage(file) {
841 const fd = new FormData();
842 fd.append('image', file);
843 const res = await fetch('/posts/upload-image', { method: 'POST', body: fd });
844 if (!res.ok) {
845 const j = await res.json().catch(() => ({}));
846 throw new Error(j.error || ('Upload failed (' + res.status + ')'));
847 }
848 return await res.json(); // {url, size, mime}
849 }
850
851 function showCoverPreview(url) {
852 if (!coverWrap || !coverImg) return;
853 if (url) {
854 coverImg.src = url;
855 coverImg.hidden = false;
856 coverWrap.removeAttribute('data-empty');
857 const emptyIcon = coverWrap.querySelector('.pe-cover-empty');
858 if (emptyIcon) emptyIcon.remove();
859 } else {
860 coverImg.hidden = true;
861 coverImg.src = '';
862 coverWrap.setAttribute('data-empty', '');
863 if (!coverWrap.querySelector('.pe-cover-empty')) {
864 const span = document.createElement('span');
865 span.className = 'pe-cover-empty';
866 span.textContent = '🖼';
867 coverWrap.appendChild(span);
868 }
869 }
870 }
871
872 if (coverTrigger && coverField) {
873 coverTrigger.addEventListener('click', () => coverField.click());
874 }
875 if (coverField) {
876 coverField.addEventListener('change', async () => {
877 if (!coverField.files[0]) return;
878 coverStatus.classList.remove('is-error');
879 coverStatus.textContent = 'Uploaden…';
880 try {
881 const j = await uploadImage(coverField.files[0]);
882 coverUrl.value = j.url;
883 showCoverPreview(j.url);
884 coverStatus.textContent = 'Geüpload ✓';
885 setTimeout(() => { coverStatus.textContent = ''; }, 2000);
886 } catch (e) {
887 coverStatus.classList.add('is-error');
888 coverStatus.textContent = 'Mislukt: ' + e.message;
889 }
890 });
891 }
892 // Live-update preview when user pastes a URL manually
893 if (coverUrl) {
894 coverUrl.addEventListener('input', () => {
895 const v = coverUrl.value.trim();
896 if (v) showCoverPreview(v); else showCoverPreview('');
897 });
898 }
899
900 // ── WYSIWYG editor (P58) ────────────────────────────────────────
901 // Architecture:
902 // - Visible <div contenteditable> (`#content-editor`) is what the user
903 // types in; it shows real HTML (formatted, not raw markup).
904 // - Hidden <input name="content"> (`#content-hidden`) is what submits.
905 // On submit we serialize the editor's HTML into it, with shortcode
906 // chips reduced back to their [[track:UUID]]/[[album:Name]]/[[playlist:slug]] text.
907 // - Initial content comes from a <script type="application/json"> tag
908 // to avoid HTML-escape-into-DOM issues; we set innerHTML once on load
909 // and walk text nodes to render shortcode tokens as chips.
910 const contentField = document.getElementById('content-upload-field');
911 const contentBtn = document.getElementById('insert-image-btn');
912 const contentStatus = document.getElementById('content-upload-status');
913 const editor = document.getElementById('content-editor');
914 const hiddenField = document.getElementById('content-hidden');
915 const charCountEl = document.getElementById('char-count');
916 const initialEl = document.getElementById('initial-content');
917 const toolbar = document.getElementById('pe-toolbar');
918 const form = editor && editor.closest('form');
919
920 if (!editor) return;
921
922 // ── Shortcode chip rendering / serialization ────────────────────
923 // Pattern matches [[track:UUID]] / [[album:any text]] / [[playlist:slug]]
924 // — but we DON'T want to chipify text the user is mid-typing inside an
925 // HTML attribute; since chipify only walks text nodes (never attribute
926 // values) that's already safe.
927 const SC_RE = /\[\[(track|album|playlist):([^\]]+)\]\]/g;
928
929 const SC_ICONS = {
930 track: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="6 4 20 12 6 20 6 4"/></svg>',
931 album: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><circle cx="12" cy="12" r="3"/></svg>',
932 playlist: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="15" y2="18"/><polygon points="3 5 3 13 9 9"/></svg>',
933 };
934
935 function chipLabel(kind, value) {
936 if (kind === 'track') {
937 // UUIDs are noisy — show a 6-char prefix for visual hint
938 const v = String(value || '');
939 return 'Track ' + (v.length > 8 ? v.slice(0, 6) + '…' : v);
940 }
941 if (kind === 'album') return 'Album: ' + value;
942 if (kind === 'playlist') return 'Playlist: ' + value;
943 return value;
944 }
945
946 function makeChip(kind, value) {
947 const span = document.createElement('span');
948 span.className = 'sc-chip';
949 span.contentEditable = 'false';
950 span.setAttribute('data-sc', kind + ':' + value);
951 span.innerHTML =
952 '<span class="sc-chip-icon" aria-hidden="true">' + (SC_ICONS[kind] || '') + '</span>' +
953 '<span class="sc-chip-label"></span>';
954 span.querySelector('.sc-chip-label').textContent = chipLabel(kind, value);
955 return span;
956 }
957
958 // Walk text nodes inside `root` and replace [[type:value]] tokens with chips.
959 function chipifyShortcodes(root) {
960 const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null);
961 const targets = [];
962 while (walker.nextNode()) {
963 const n = walker.currentNode;
964 // Skip text inside existing chips (their .sc-chip-label is set via .textContent so the [[...]] text never appears)
965 if (n.parentElement && n.parentElement.closest('.sc-chip')) continue;
966 if (SC_RE.test(n.nodeValue)) targets.push(n);
967 SC_RE.lastIndex = 0;
968 }
969 for (const node of targets) {
970 const txt = node.nodeValue;
971 const frag = document.createDocumentFragment();
972 let last = 0;
973 let m;
974 SC_RE.lastIndex = 0;
975 while ((m = SC_RE.exec(txt)) !== null) {
976 if (m.index > last) frag.appendChild(document.createTextNode(txt.slice(last, m.index)));
977 frag.appendChild(makeChip(m[1], m[2].trim()));
978 last = m.index + m[0].length;
979 }
980 if (last < txt.length) frag.appendChild(document.createTextNode(txt.slice(last)));
981 node.parentNode.replaceChild(frag, node);
982 }
983 }
984
985 // Inverse of chipify: clone the editor, replace every chip with its text.
986 function serializeChips(rootClone) {
987 const chips = rootClone.querySelectorAll('.sc-chip[data-sc]');
988 for (const c of chips) {
989 const txt = '[[' + c.getAttribute('data-sc') + ']]';
990 c.replaceWith(document.createTextNode(txt));
991 }
992 }
993
994 // ── Boot: load initial content as HTML, then render shortcodes as chips
995 try {
996 const initial = JSON.parse(initialEl.textContent || '""');
997 editor.innerHTML = initial || '';
998 chipifyShortcodes(editor);
999 } catch (e) {
1000 console.error('[editor] could not parse initial content', e);
1001 editor.innerHTML = '';
1002 }
1003
1004 // ── Char counter
1005 function updateCharCount() {
1006 const text = (editor.innerText || '').replace(/\s+/g, ' ').trim();
1007 if (charCountEl) charCountEl.textContent = String(text.length);
1008 }
1009 updateCharCount();
1010 editor.addEventListener('input', updateCharCount);
1011
1012 // ── Toolbar wiring
1013 function execCmd(cmd, arg) {
1014 editor.focus();
1015 document.execCommand(cmd, false, arg);
1016 updateToolbarState();
1017 updateCharCount();
1018 }
1019 function wrapCode() {
1020 const sel = window.getSelection();
1021 if (!sel || sel.rangeCount === 0 || sel.isCollapsed) return;
1022 const range = sel.getRangeAt(0);
1023 const code = document.createElement('code');
1024 code.textContent = sel.toString();
1025 range.deleteContents();
1026 range.insertNode(code);
1027 // Move caret after the new node
1028 range.setStartAfter(code);
1029 range.collapse(true);
1030 sel.removeAllRanges();
1031 sel.addRange(range);
1032 editor.focus();
1033 }
1034 function linkPrompt() {
1035 const url = window.prompt('Link URL (https://… of /pad)');
1036 if (!url) return;
1037 execCmd('createLink', url);
1038 }
1039
1040 if (toolbar) {
1041 toolbar.addEventListener('click', (e) => {
1042 const btn = e.target.closest('button[data-cmd]');
1043 if (!btn) return;
1044 e.preventDefault();
1045 const cmd = btn.dataset.cmd;
1046 const arg = btn.dataset.arg || null;
1047 if (cmd === 'link-prompt') linkPrompt();
1048 else if (cmd === 'code-wrap') wrapCode();
1049 else execCmd(cmd, arg);
1050 });
1051 }
1052
1053 // Reflect bold/italic/list state on the toolbar buttons
1054 function updateToolbarState() {
1055 if (!toolbar) return;
1056 const cmds = ['bold', 'italic', 'underline', 'insertUnorderedList', 'insertOrderedList'];
1057 for (const cmd of cmds) {
1058 const btn = toolbar.querySelector('button[data-cmd="' + cmd + '"]');
1059 if (!btn) continue;
1060 try { btn.classList.toggle('is-active', document.queryCommandState(cmd)); } catch(_) {}
1061 }
1062 }
1063 document.addEventListener('selectionchange', () => {
1064 if (document.activeElement === editor) updateToolbarState();
1065 });
1066
1067 // Keyboard shortcuts: Ctrl/Cmd + B/I/U/K
1068 editor.addEventListener('keydown', (e) => {
1069 const mod = e.ctrlKey || e.metaKey;
1070 if (!mod) return;
1071 const k = e.key.toLowerCase();
1072 if (k === 'b') { e.preventDefault(); execCmd('bold'); }
1073 else if (k === 'i') { e.preventDefault(); execCmd('italic'); }
1074 else if (k === 'u') { e.preventDefault(); execCmd('underline'); }
1075 else if (k === 'k') { e.preventDefault(); linkPrompt(); }
1076 });
1077
1078 // Paste: keep it simple — strip formatting unless user wants it. Default
1079 // execCommand 'paste' includes Word/Google-Docs garbage. We accept inline
1080 // styles from clipboard only when shift is held — otherwise plain text.
1081 editor.addEventListener('paste', (e) => {
1082 if (e.shiftKey) return; // user wants formatted paste
1083 const text = (e.clipboardData || window.clipboardData).getData('text/plain');
1084 if (text == null) return;
1085 e.preventDefault();
1086 document.execCommand('insertText', false, text);
1087 });
1088
1089 // ── Image upload (button + drag-drop into the editor)
1090 async function uploadAndInsertImage(file) {
1091 contentStatus.classList.remove('is-error');
1092 contentStatus.textContent = 'Uploaden…';
1093 try {
1094 const j = await uploadImage(file);
1095 const img = '<img src="' + j.url + '" alt="">';
1096 editor.focus();
1097 document.execCommand('insertHTML', false, img);
1098 contentStatus.textContent = 'Ingevoegd ✓';
1099 setTimeout(() => { contentStatus.textContent = ''; }, 2000);
1100 updateCharCount();
1101 } catch (e) {
1102 contentStatus.classList.add('is-error');
1103 contentStatus.textContent = 'Mislukt: ' + e.message;
1104 }
1105 }
1106
1107 if (contentBtn && contentField) {
1108 contentBtn.addEventListener('click', () => contentField.click());
1109 contentField.addEventListener('change', () => {
1110 if (contentField.files[0]) uploadAndInsertImage(contentField.files[0]);
1111 contentField.value = '';
1112 });
1113
1114 editor.addEventListener('dragover', (e) => {
1115 if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
1116 e.preventDefault();
1117 editor.classList.add('is-dragover');
1118 }
1119 });
1120 editor.addEventListener('dragleave', () => editor.classList.remove('is-dragover'));
1121 editor.addEventListener('drop', async (e) => {
1122 editor.classList.remove('is-dragover');
1123 const files = e.dataTransfer && e.dataTransfer.files;
1124 if (!files || !files.length) return;
1125 e.preventDefault();
1126 for (const f of files) {
1127 if (f.type.startsWith('image/')) await uploadAndInsertImage(f);
1128 }
1129 });
1130 }
1131
1132 // ── Insert chip helpers (track / playlist)
1133 function insertChip(kind, value) {
1134 editor.focus();
1135 const chip = makeChip(kind, value);
1136 // Insert at caret using the Selection API (execCommand insertNode)
1137 const sel = window.getSelection();
1138 if (sel && sel.rangeCount > 0) {
1139 const range = sel.getRangeAt(0);
1140 range.deleteContents();
1141 range.insertNode(chip);
1142 // Insert a trailing space so the user can keep typing after the chip
1143 const space = document.createTextNode('\u00A0');
1144 chip.after(space);
1145 range.setStartAfter(space);
1146 range.collapse(true);
1147 sel.removeAllRanges();
1148 sel.addRange(range);
1149 } else {
1150 editor.appendChild(chip);
1151 editor.appendChild(document.createTextNode('\u00A0'));
1152 }
1153 updateCharCount();
1154 }
1155
1156 // ── Track insert: opens the track-picker modal (P59)
1157 const trackBtn = document.getElementById('insert-track-btn');
1158 const trackPicker = document.getElementById('track-picker');
1159 if (trackBtn && trackPicker) {
1160 const tpList = document.getElementById('tp-list');
1161 const tpEmpty = document.getElementById('tp-empty');
1162 const tpSearch = document.getElementById('tp-search');
1163 let tpCache = null; // cached tracks list (fetched once per page load)
1164 let tpLastFocus = null; // element to restore focus to on close
1165
1166 const SVG_NOTE = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 17V5l12-2v12"/><circle cx="6" cy="17" r="3"/><circle cx="18" cy="15" r="3"/></svg>';
1167
1168 function fmtDur(sec) {
1169 sec = Math.max(0, Math.floor(sec || 0));
1170 const m = Math.floor(sec / 60), s = sec % 60;
1171 return m + ':' + String(s).padStart(2, '0');
1172 }
1173 function escAttr(s) {
1174 return String(s == null ? '' : s).replace(/[&<>"']/g, c => ({
1175 '&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'
1176 }[c]));
1177 }
1178
1179 function renderList(filter) {
1180 if (!Array.isArray(tpCache)) return;
1181 const q = (filter || '').trim().toLowerCase();
1182 const filtered = q
1183 ? tpCache.filter(t =>
1184 (t.title || '').toLowerCase().includes(q) ||
1185 (t.artist || '').toLowerCase().includes(q))
1186 : tpCache;
1187
1188 if (!filtered.length) {
1189 tpList.innerHTML = '';
1190 tpEmpty.textContent = q ? 'Geen tracks gevonden voor "' + q + '"' : 'Nog geen tracks. Upload via Beheer → Audio.';
1191 tpList.appendChild(tpEmpty);
1192 return;
1193 }
1194
1195 tpList.innerHTML = filtered.map(t => {
1196 const cov = t.cover
1197 ? '<span class="tp-cover" style="background-image:url(\'' + escAttr(t.cover) + '\')"></span>'
1198 : '<span class="tp-cover tp-cover-empty">' + SVG_NOTE + '</span>';
1199 const dis = t.playable ? '' : ' aria-disabled="true"';
1200 const sub = t.artist ? '<span class="tp-row-artist">' + escAttr(t.artist) + '</span>' : '';
1201 return (
1202 '<button type="button" class="tp-row" role="option" data-track-id="' + escAttr(t.id) + '"' + dis + '>' +
1203 cov +
1204 '<span class="tp-meta">' +
1205 '<span class="tp-row-title">' + escAttr(t.title) + '</span>' +
1206 sub +
1207 '</span>' +
1208 '<span class="tp-duration">' + fmtDur(t.duration) + '</span>' +
1209 '</button>'
1210 );
1211 }).join('');
1212 }
1213
1214 async function loadTracks() {
1215 if (Array.isArray(tpCache)) return tpCache;
1216 tpEmpty.textContent = 'Tracks laden…';
1217 try {
1218 const r = await fetch('/admin/playlists/api/tracks', { credentials: 'same-origin' });
1219 const j = await r.json();
1220 tpCache = (j && j.ok && Array.isArray(j.tracks)) ? j.tracks : [];
1221 } catch (e) {
1222 tpCache = [];
1223 tpEmpty.textContent = 'Kon tracks niet laden: ' + e.message;
1224 }
1225 return tpCache;
1226 }
1227
1228 function openPicker() {
1229 tpLastFocus = document.activeElement;
1230 trackPicker.hidden = false;
1231 trackPicker.setAttribute('aria-hidden', 'false');
1232 document.body.classList.add('tp-locked');
1233 tpSearch.value = '';
1234 renderList('');
1235 // Defer focus so the open animation doesn't get jumped
1236 setTimeout(() => tpSearch.focus(), 30);
1237 }
1238 function closePicker() {
1239 trackPicker.hidden = true;
1240 trackPicker.setAttribute('aria-hidden', 'true');
1241 document.body.classList.remove('tp-locked');
1242 if (tpLastFocus && typeof tpLastFocus.focus === 'function') {
1243 try { tpLastFocus.focus(); } catch(_) {}
1244 }
1245 }
1246
1247 trackBtn.addEventListener('click', async () => {
1248 openPicker();
1249 await loadTracks();
1250 renderList(tpSearch.value);
1251 });
1252
1253 // Close: backdrop click, [data-tp-close], or Escape
1254 trackPicker.addEventListener('click', (e) => {
1255 if (e.target.closest('[data-tp-close]')) {
1256 closePicker();
1257 return;
1258 }
1259 const row = e.target.closest('.tp-row[data-track-id]');
1260 if (row) {
1261 if (row.getAttribute('aria-disabled') === 'true') return;
1262 const id = row.dataset.trackId;
1263 if (id) {
1264 insertChip('track', id);
1265 closePicker();
1266 }
1267 }
1268 });
1269 document.addEventListener('keydown', (e) => {
1270 if (!trackPicker.hidden && e.key === 'Escape') {
1271 e.preventDefault();
1272 closePicker();
1273 }
1274 });
1275
1276 // Live filter
1277 tpSearch.addEventListener('input', () => renderList(tpSearch.value));
1278 }
1279
1280 // ── Playlist insert (open existing or create new via modal)
1281 const playlistBtn = document.getElementById('insert-playlist-btn');
1282 if (playlistBtn) {
1283 playlistBtn.addEventListener('click', async () => {
1284 if (typeof window.openPlaylistEditor !== 'function') {
1285 alert('Playlist editor niet geladen');
1286 return;
1287 }
1288 try {
1289 const r = await fetch('/admin/playlists/api/list', { credentials: 'same-origin' });
1290 const j = await r.json();
1291 if (j.ok && Array.isArray(j.playlists) && j.playlists.length > 0) {
1292 const choice = prompt(
1293 'Bestaande playlists:\n\n' +
1294 j.playlists.map((p, i) => `${i + 1}. ${p.title} (${p.track_count} tracks)`).join('\n') +
1295 '\n\nKies een nummer om in te voegen, leeg = nieuwe maken:'
1296 );
1297 if (choice && /^\d+$/.test(choice.trim())) {
1298 const idx = parseInt(choice.trim(), 10) - 1;
1299 if (idx >= 0 && idx < j.playlists.length) {
1300 insertChip('playlist', j.playlists[idx].id);
1301 return;
1302 }
1303 }
1304 if (choice === null) return;
1305 }
1306 } catch (_) { /* fall through to create */ }
1307
1308 window.openPlaylistEditor({
1309 mode: 'create',
1310 onSaved: ({ id }) => insertChip('playlist', id),
1311 });
1312 });
1313 }
1314
1315 // ── Submit: serialize editor contents into the hidden field
1316 if (form && hiddenField) {
1317 form.addEventListener('submit', () => {
1318 const clone = editor.cloneNode(true);
1319 serializeChips(clone);
1320 hiddenField.value = clone.innerHTML;
1321 });
1322 }
1323})();
1324</script>
1325
1326<%# ── Track picker modal (P59). Mobile-first: full-screen sheet on small
1327 viewports, centered modal on ≥640px. Populated lazily on first open. %>
1328<% if (typeof user !== 'undefined' && user) { %>
1329<div id="track-picker" class="tp-modal" hidden aria-hidden="true">
1330 <div class="tp-backdrop" data-tp-close></div>
1331 <div class="tp-sheet" role="dialog" aria-modal="true" aria-labelledby="tp-title">
1332 <header class="tp-header">
1333 <h2 id="tp-title" class="tp-h2">Track invoegen</h2>
1334 <button type="button" class="tp-close" data-tp-close aria-label="Sluiten">
1335 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
1336 </button>
1337 </header>
1338 <div class="tp-search-row">
1339 <span class="tp-search-icon" aria-hidden="true">
1340 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="7"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
1341 </span>
1342 <input type="search" class="tp-search" id="tp-search" placeholder="Zoek op titel of artiest…" autocomplete="off" inputmode="search">
1343 </div>
1344 <div class="tp-list" id="tp-list" role="listbox" aria-label="Tracks">
1345 <div class="tp-empty" id="tp-empty">Tracks laden…</div>
1346 </div>
1347 </div>
1348</div>
1349<% } %>
1350
1351<%# Playlist editor modal — included so its global window.openPlaylistEditor
1352 is available when the toolbar button fires. Only renders if user can post. %>
1353<% if (typeof user !== 'undefined' && user) { %>
1354 <%- include('../partials/playlist-editor', { csrfToken: (typeof csrfToken !== 'undefined' ? csrfToken : '') }) %>
1355<% } %>
Note: See TracBrowser for help on using the repository browser.