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

main
Last change on this file since a7e8e19 was a7e8e19, checked in by roboburr <roboburr@…>, 3 months ago

fix(editor): mobile — formatting buttons no longer steal focus (toggle + no jump)

Toolbar buttons called editor.focus() on click after the tap had already
moved focus/selection out of the contenteditable field → bold/italic
could not be toggled off and the page jumped to the caret. Now
mousedown-preventDefault on the toolbar (focus stays in the editor) +
editor.focus({preventScroll:true}) + touch-action:manipulation for snappy
taps.

Co-Authored-By: Claude <noreply@…>

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