@prunellejs/cms 0.0.0-stage → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/llms.txt +74 -0
- package/package.json +24 -4
- package/src/assets-js.ts +101 -0
- package/src/assets.ts +576 -0
- package/src/form-data.ts +469 -0
- package/src/guard.ts +23 -0
- package/src/handlers.ts +356 -0
- package/src/index.ts +101 -0
- package/src/inline-css.ts +621 -0
- package/src/inline-dom-js.ts +166 -0
- package/src/inline-form-js.ts +501 -0
- package/src/inline-handlers.ts +248 -0
- package/src/inline-js.ts +547 -0
- package/src/render.ts +583 -0
- package/src/sanitize.ts +16 -0
- package/src/schema.ts +487 -0
- package/src/shared.ts +108 -0
- package/src/write-file.ts +58 -0
- package/src/yaml-serialize.ts +235 -0
- package/README.md +0 -3
package/src/inline-js.ts
ADDED
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
// Self-contained Shadow DOM in-page CMS overlay script for @prunellejs/cms (dev-only).
|
|
2
|
+
// Supports floating toolbar, Edit-on-Page highlights, direct inline text editing,
|
|
3
|
+
// live-linked Markdown body preview via HTML comment anchors, and docked schema drawer.
|
|
4
|
+
|
|
5
|
+
import { INLINE_DOM_JS } from './inline-dom-js'
|
|
6
|
+
import { INLINE_FORM_JS } from './inline-form-js'
|
|
7
|
+
|
|
8
|
+
export const INLINE_CMS_JS = `(() => {
|
|
9
|
+
const meta = document.querySelector('meta[name="pru-cms-config"]');
|
|
10
|
+
if (!meta) return;
|
|
11
|
+
let config;
|
|
12
|
+
try {
|
|
13
|
+
config = JSON.parse(meta.getAttribute('content') || '');
|
|
14
|
+
} catch (_err) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (!config || !config.basePath || !Array.isArray(config.entries) || config.entries.length === 0) return;
|
|
18
|
+
|
|
19
|
+
const BASE = config.basePath;
|
|
20
|
+
const ENTRIES = config.entries;
|
|
21
|
+
const STORAGE_KEY = 'pru-cms-inline-state';
|
|
22
|
+
const SKIP_TAGS = new Set(['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE', 'TEXTAREA', 'INPUT', 'SELECT', 'OPTION', 'PRU-CMS-OVERLAY']);
|
|
23
|
+
|
|
24
|
+
${INLINE_FORM_JS}
|
|
25
|
+
${INLINE_DOM_JS}
|
|
26
|
+
|
|
27
|
+
function loadSessionState() {
|
|
28
|
+
try {
|
|
29
|
+
const raw = sessionStorage.getItem(STORAGE_KEY);
|
|
30
|
+
if (!raw) return null;
|
|
31
|
+
const parsed = JSON.parse(raw);
|
|
32
|
+
if (!parsed || parsed.path !== location.pathname) return null;
|
|
33
|
+
return parsed;
|
|
34
|
+
} catch (_err) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function saveSessionState(extra) {
|
|
40
|
+
try {
|
|
41
|
+
sessionStorage.setItem(STORAGE_KEY, JSON.stringify({
|
|
42
|
+
path: location.pathname,
|
|
43
|
+
open: state.drawerOpen,
|
|
44
|
+
editMode: state.editMode,
|
|
45
|
+
tab: state.tab,
|
|
46
|
+
entryKey: entryId(state.activeRef),
|
|
47
|
+
...(extra || {}),
|
|
48
|
+
}));
|
|
49
|
+
} catch (_err) {
|
|
50
|
+
// Ignore storage restrictions
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Mount Shadow DOM host
|
|
55
|
+
const host = document.createElement('pru-cms-overlay');
|
|
56
|
+
host.setAttribute('data-pru-cms-inline', 'true');
|
|
57
|
+
const shadow = host.attachShadow({ mode: 'open' });
|
|
58
|
+
shadow.appendChild(el('link', { rel: 'stylesheet', href: BASE + '/_assets/inline.css' }));
|
|
59
|
+
|
|
60
|
+
const hlLayer = el('div', { class: 'pru-hl-layer', hidden: true });
|
|
61
|
+
const inlineBar = el('div', { class: 'pru-inline-bar', hidden: true, role: 'dialog', 'aria-label': 'Inline field editor' });
|
|
62
|
+
const bar = el('div', { class: 'pru-bar', role: 'region', 'aria-label': 'Prunelle CMS toolbar' });
|
|
63
|
+
const drawer = el('aside', { class: 'pru-drawer', hidden: true, role: 'dialog', 'aria-label': 'In-page CMS editor' });
|
|
64
|
+
const toast = el('div', { class: 'pru-toast', hidden: true, role: 'status', 'aria-live': 'polite' });
|
|
65
|
+
shadow.append(hlLayer, inlineBar, bar, drawer, toast);
|
|
66
|
+
document.body.appendChild(host);
|
|
67
|
+
|
|
68
|
+
const restored = loadSessionState();
|
|
69
|
+
const initialRef = (restored && ENTRIES.find(e => entryId(e) === restored.entryKey)) || ENTRIES[0];
|
|
70
|
+
|
|
71
|
+
const state = {
|
|
72
|
+
activeRef: initialRef,
|
|
73
|
+
entry: null,
|
|
74
|
+
draftData: {},
|
|
75
|
+
draftYaml: '',
|
|
76
|
+
draftBody: '',
|
|
77
|
+
dirty: false,
|
|
78
|
+
saving: false,
|
|
79
|
+
errors: {},
|
|
80
|
+
editMode: Boolean(restored && restored.editMode),
|
|
81
|
+
drawerOpen: Boolean(restored && restored.open),
|
|
82
|
+
tab: restored && restored.tab === 'markdown' ? 'markdown' : 'fields',
|
|
83
|
+
bodyAnchors: findBodyAnchors(),
|
|
84
|
+
originalBodyHtml: new Map(),
|
|
85
|
+
fieldTargets: [],
|
|
86
|
+
activeInline: null,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
for (const a of state.bodyAnchors) {
|
|
90
|
+
state.originalBodyHtml.set(a, captureAnchorHtml(a));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const prevDocPaddingRight = document.documentElement.style.paddingRight;
|
|
94
|
+
function syncPageDockOffset() {
|
|
95
|
+
const docked = state.drawerOpen && window.innerWidth >= 900;
|
|
96
|
+
document.documentElement.style.paddingRight = docked ? '440px' : prevDocPaddingRight;
|
|
97
|
+
toast.setAttribute('data-drawer-open', state.drawerOpen ? 'true' : 'false');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let toastTimer = null;
|
|
101
|
+
function showToast(msg, tone) {
|
|
102
|
+
toast.textContent = msg;
|
|
103
|
+
toast.setAttribute('data-drawer-open', state.drawerOpen ? 'true' : 'false');
|
|
104
|
+
if (tone === 'error') toast.setAttribute('data-tone', 'error');
|
|
105
|
+
else toast.removeAttribute('data-tone');
|
|
106
|
+
toast.hidden = false;
|
|
107
|
+
if (toastTimer !== null) clearTimeout(toastTimer);
|
|
108
|
+
toastTimer = setTimeout(() => { toast.hidden = true; }, 3200);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function matchingBodyAnchors() {
|
|
112
|
+
const r = state.activeRef;
|
|
113
|
+
return state.bodyAnchors.filter(a =>
|
|
114
|
+
a.collection === r.collection && a.slug === r.slug && (a.locale || '') === (r.locale || ''),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let previewTimer = null;
|
|
119
|
+
let previewSeq = 0;
|
|
120
|
+
async function syncLiveBodyPreview(markdown) {
|
|
121
|
+
const anchors = matchingBodyAnchors();
|
|
122
|
+
if (anchors.length === 0) return;
|
|
123
|
+
const ticket = ++previewSeq;
|
|
124
|
+
try {
|
|
125
|
+
const res = await fetch(BASE + '/_preview', {
|
|
126
|
+
method: 'POST',
|
|
127
|
+
headers: { 'content-type': 'application/json', 'accept': 'text/html' },
|
|
128
|
+
body: JSON.stringify({ markdown }),
|
|
129
|
+
});
|
|
130
|
+
if (!res.ok || ticket !== previewSeq) return;
|
|
131
|
+
const html = await res.text();
|
|
132
|
+
for (const a of anchors) replaceAnchorHtml(a, html);
|
|
133
|
+
if (state.editMode) scheduleHighlights();
|
|
134
|
+
} catch (_err) {
|
|
135
|
+
// Ignore transient preview failures
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function scheduleBodyPreview(markdown) {
|
|
140
|
+
if (previewTimer !== null) clearTimeout(previewTimer);
|
|
141
|
+
previewTimer = setTimeout(() => void syncLiveBodyPreview(markdown), 160);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function setDirty(next) {
|
|
145
|
+
state.dirty = next;
|
|
146
|
+
renderToolbar();
|
|
147
|
+
renderDrawerFooter();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function fetchActiveEntry() {
|
|
151
|
+
const r = state.activeRef;
|
|
152
|
+
const qs = new URLSearchParams({ collection: r.collection, slug: r.slug });
|
|
153
|
+
if (r.locale) qs.set('locale', r.locale);
|
|
154
|
+
const res = await fetch(BASE + '/_entry?' + qs.toString(), { headers: { accept: 'application/json' } });
|
|
155
|
+
if (!res.ok) {
|
|
156
|
+
showToast('Failed to load entry (' + res.status + ')', 'error');
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const payload = await res.json();
|
|
160
|
+
state.entry = payload;
|
|
161
|
+
state.draftData = cloneVal(payload.data || {});
|
|
162
|
+
state.draftYaml = payload.rawFrontmatterYaml || '';
|
|
163
|
+
state.draftBody = payload.body || '';
|
|
164
|
+
state.dirty = false;
|
|
165
|
+
state.errors = {};
|
|
166
|
+
state.fieldTargets = findMatchingFieldTargets(payload, state.draftData, state.bodyAnchors);
|
|
167
|
+
renderToolbar();
|
|
168
|
+
renderDrawer();
|
|
169
|
+
renderHighlights();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function persistInline(bodyPayload, options) {
|
|
173
|
+
if (state.saving) return false;
|
|
174
|
+
state.saving = true;
|
|
175
|
+
renderDrawerFooter();
|
|
176
|
+
try {
|
|
177
|
+
const res = await fetch(BASE + '/_inline', {
|
|
178
|
+
method: 'PATCH',
|
|
179
|
+
headers: { 'content-type': 'application/json', 'accept': 'application/json' },
|
|
180
|
+
body: JSON.stringify(bodyPayload),
|
|
181
|
+
});
|
|
182
|
+
const json = await res.json().catch(() => ({ ok: false, error: 'Invalid server response' }));
|
|
183
|
+
if (!res.ok) {
|
|
184
|
+
state.errors = json.errors || { '': json.error || 'Save failed (' + res.status + ')' };
|
|
185
|
+
if (options && options.onError) options.onError(state.errors);
|
|
186
|
+
else renderDrawer();
|
|
187
|
+
showToast('Fix validation errors before saving.', 'error');
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
state.errors = {};
|
|
191
|
+
if (state.entry) {
|
|
192
|
+
state.entry.data = cloneVal(json.data || state.draftData);
|
|
193
|
+
state.entry.body = typeof json.body === 'string' ? json.body : state.draftBody;
|
|
194
|
+
if (typeof json.rawFrontmatterYaml === 'string') {
|
|
195
|
+
state.entry.rawFrontmatterYaml = json.rawFrontmatterYaml;
|
|
196
|
+
state.draftYaml = json.rawFrontmatterYaml;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
state.draftData = cloneVal(json.data || state.draftData);
|
|
200
|
+
if (typeof json.body === 'string') state.draftBody = json.body;
|
|
201
|
+
for (const t of state.fieldTargets) {
|
|
202
|
+
const latest = getNestedValue(state.draftData, t.path);
|
|
203
|
+
if (typeof latest === 'string') t.originalText = latest;
|
|
204
|
+
}
|
|
205
|
+
if (typeof json.previewHtml === 'string') {
|
|
206
|
+
for (const a of matchingBodyAnchors()) {
|
|
207
|
+
replaceAnchorHtml(a, json.previewHtml);
|
|
208
|
+
state.originalBodyHtml.set(a, json.previewHtml);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
state.dirty = false;
|
|
212
|
+
saveSessionState({ justSaved: Date.now() });
|
|
213
|
+
renderToolbar();
|
|
214
|
+
renderDrawer();
|
|
215
|
+
renderHighlights();
|
|
216
|
+
showToast('Saved to working tree.', 'success');
|
|
217
|
+
return true;
|
|
218
|
+
} catch (_err) {
|
|
219
|
+
showToast('Network error while saving.', 'error');
|
|
220
|
+
return false;
|
|
221
|
+
} finally {
|
|
222
|
+
state.saving = false;
|
|
223
|
+
renderDrawerFooter();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function closeInlineEditor(revert) {
|
|
228
|
+
const active = state.activeInline;
|
|
229
|
+
if (!active) return;
|
|
230
|
+
active.el.contentEditable = active.prevEditable;
|
|
231
|
+
active.el.style.outline = active.prevOutline;
|
|
232
|
+
active.el.style.outlineOffset = active.prevOutlineOffset;
|
|
233
|
+
active.el.style.borderRadius = active.prevRadius;
|
|
234
|
+
active.el.removeAttribute('data-pru-cms-editing');
|
|
235
|
+
active.el.removeEventListener('keydown', active.onKeyDown);
|
|
236
|
+
active.el.removeEventListener('input', active.onInput);
|
|
237
|
+
if (revert) {
|
|
238
|
+
active.el.textContent = active.beforeText;
|
|
239
|
+
setNestedValue(state.draftData, active.target.path, active.beforeText);
|
|
240
|
+
}
|
|
241
|
+
state.activeInline = null;
|
|
242
|
+
inlineBar.hidden = true;
|
|
243
|
+
inlineBar.textContent = '';
|
|
244
|
+
if (state.editMode) renderHighlights();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function positionInlineBar(targetEl) {
|
|
248
|
+
const rect = targetEl.getBoundingClientRect();
|
|
249
|
+
const top = rect.bottom + 10 + 72 < window.innerHeight ? rect.bottom + 10 : Math.max(8, rect.top - 78);
|
|
250
|
+
const left = Math.min(Math.max(8, rect.left), Math.max(8, window.innerWidth - 360));
|
|
251
|
+
inlineBar.style.top = top + 'px';
|
|
252
|
+
inlineBar.style.left = left + 'px';
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function openInlineFieldEditor(target) {
|
|
256
|
+
if (state.activeInline) closeInlineEditor(true);
|
|
257
|
+
const targetEl = target.el;
|
|
258
|
+
const beforeText = targetEl.textContent || '';
|
|
259
|
+
const prevEditable = targetEl.contentEditable;
|
|
260
|
+
const prevOutline = targetEl.style.outline;
|
|
261
|
+
const prevOutlineOffset = targetEl.style.outlineOffset;
|
|
262
|
+
const prevRadius = targetEl.style.borderRadius;
|
|
263
|
+
try {
|
|
264
|
+
targetEl.contentEditable = 'plaintext-only';
|
|
265
|
+
} catch (_err) {
|
|
266
|
+
targetEl.contentEditable = 'true';
|
|
267
|
+
}
|
|
268
|
+
if (targetEl.contentEditable !== 'plaintext-only' && targetEl.contentEditable !== 'true') {
|
|
269
|
+
targetEl.contentEditable = 'true';
|
|
270
|
+
}
|
|
271
|
+
const accent = getComputedStyle(host).getPropertyValue('--cms-accent').trim() || '#7a3667';
|
|
272
|
+
targetEl.style.outline = '2px solid ' + accent;
|
|
273
|
+
targetEl.style.outlineOffset = '4px';
|
|
274
|
+
targetEl.style.borderRadius = '2px';
|
|
275
|
+
targetEl.setAttribute('data-pru-cms-editing', 'true');
|
|
276
|
+
targetEl.focus();
|
|
277
|
+
|
|
278
|
+
const errSlot = el('p', { class: 'pru-err', role: 'alert', hidden: true });
|
|
279
|
+
const saveField = async () => {
|
|
280
|
+
const nextText = (targetEl.textContent || '').trim();
|
|
281
|
+
const ok = await persistInline({
|
|
282
|
+
collection: state.activeRef.collection,
|
|
283
|
+
slug: state.activeRef.slug,
|
|
284
|
+
locale: state.activeRef.locale,
|
|
285
|
+
patch: { [target.path]: nextText },
|
|
286
|
+
}, {
|
|
287
|
+
onError(errs) {
|
|
288
|
+
const msg = errs[target.path] || Object.values(errs)[0] || 'Validation failed.';
|
|
289
|
+
errSlot.textContent = msg;
|
|
290
|
+
errSlot.hidden = false;
|
|
291
|
+
targetEl.focus();
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
if (ok) {
|
|
295
|
+
target.originalText = nextText;
|
|
296
|
+
closeInlineEditor(false);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const onInput = () => {
|
|
301
|
+
errSlot.hidden = true;
|
|
302
|
+
setNestedValue(state.draftData, target.path, targetEl.textContent || '');
|
|
303
|
+
positionInlineBar(targetEl);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const onKeyDown = (ev) => {
|
|
307
|
+
if (ev.key === 'Escape') {
|
|
308
|
+
ev.preventDefault();
|
|
309
|
+
closeInlineEditor(true);
|
|
310
|
+
} else if ((ev.key === 'Enter' && !target.multiline) || ((ev.metaKey || ev.ctrlKey) && ev.key.toLowerCase() === 's')) {
|
|
311
|
+
ev.preventDefault();
|
|
312
|
+
void saveField();
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
targetEl.addEventListener('input', onInput);
|
|
317
|
+
targetEl.addEventListener('keydown', onKeyDown);
|
|
318
|
+
state.activeInline = { target, el: targetEl, beforeText, prevEditable, prevOutline, prevOutlineOffset, prevRadius, onKeyDown, onInput };
|
|
319
|
+
|
|
320
|
+
inlineBar.textContent = '';
|
|
321
|
+
const row = el('div', { class: 'pru-inline-bar-row' });
|
|
322
|
+
row.appendChild(el('span', { class: 'pru-inline-label' }, 'Editing ' + target.label));
|
|
323
|
+
const actions = el('div', { class: 'pru-inline-actions' });
|
|
324
|
+
const cancelBtn = el('button', { type: 'button', class: 'pru-btn pru-btn-sm' }, 'Cancel');
|
|
325
|
+
cancelBtn.addEventListener('click', () => closeInlineEditor(true));
|
|
326
|
+
const saveBtn = el('button', { type: 'button', class: 'pru-btn pru-btn-sm pru-btn-primary' }, 'Save');
|
|
327
|
+
saveBtn.addEventListener('click', () => void saveField());
|
|
328
|
+
actions.append(cancelBtn, saveBtn);
|
|
329
|
+
row.appendChild(actions);
|
|
330
|
+
inlineBar.append(row, errSlot);
|
|
331
|
+
inlineBar.hidden = false;
|
|
332
|
+
positionInlineBar(targetEl);
|
|
333
|
+
hlLayer.hidden = true;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
let hlFrame = 0;
|
|
337
|
+
function scheduleHighlights() {
|
|
338
|
+
if (hlFrame !== 0) return;
|
|
339
|
+
hlFrame = requestAnimationFrame(() => {
|
|
340
|
+
hlFrame = 0;
|
|
341
|
+
renderHighlights();
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function renderHighlights() {
|
|
346
|
+
hlLayer.textContent = '';
|
|
347
|
+
if (!state.editMode || state.activeInline || !state.entry) {
|
|
348
|
+
hlLayer.hidden = true;
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
hlLayer.hidden = false;
|
|
352
|
+
|
|
353
|
+
for (const target of state.fieldTargets) {
|
|
354
|
+
const r = target.el.getBoundingClientRect();
|
|
355
|
+
if (r.width === 0 && r.height === 0) continue;
|
|
356
|
+
if (r.bottom < 0 || r.top > window.innerHeight) continue;
|
|
357
|
+
const box = el('button', {
|
|
358
|
+
type: 'button',
|
|
359
|
+
class: 'pru-hl-box',
|
|
360
|
+
'aria-label': 'Edit ' + target.label + ' inline',
|
|
361
|
+
});
|
|
362
|
+
box.style.top = (r.top - 4) + 'px';
|
|
363
|
+
box.style.left = (r.left - 4) + 'px';
|
|
364
|
+
box.style.width = (r.width + 8) + 'px';
|
|
365
|
+
box.style.height = (r.height + 8) + 'px';
|
|
366
|
+
box.appendChild(el('span', { class: 'pru-hl-tag' }, target.label));
|
|
367
|
+
box.addEventListener('click', (ev) => {
|
|
368
|
+
ev.preventDefault();
|
|
369
|
+
ev.stopPropagation();
|
|
370
|
+
openInlineFieldEditor(target);
|
|
371
|
+
});
|
|
372
|
+
hlLayer.appendChild(box);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
for (const anchor of matchingBodyAnchors()) {
|
|
376
|
+
const r = measureAnchorRect(anchor);
|
|
377
|
+
if (!r || r.top + r.height < 0 || r.top > window.innerHeight) continue;
|
|
378
|
+
const box = el('button', {
|
|
379
|
+
type: 'button',
|
|
380
|
+
class: 'pru-hl-box',
|
|
381
|
+
'aria-label': 'Edit Markdown body in drawer',
|
|
382
|
+
});
|
|
383
|
+
box.style.top = (r.top - 4) + 'px';
|
|
384
|
+
box.style.left = (r.left - 4) + 'px';
|
|
385
|
+
box.style.width = (r.width + 8) + 'px';
|
|
386
|
+
box.style.height = (r.height + 8) + 'px';
|
|
387
|
+
box.appendChild(el('span', { class: 'pru-hl-tag' }, 'Markdown body'));
|
|
388
|
+
box.addEventListener('click', (ev) => {
|
|
389
|
+
ev.preventDefault();
|
|
390
|
+
ev.stopPropagation();
|
|
391
|
+
state.drawerOpen = true;
|
|
392
|
+
state.tab = 'markdown';
|
|
393
|
+
saveSessionState();
|
|
394
|
+
renderToolbar();
|
|
395
|
+
renderDrawer();
|
|
396
|
+
const ta = drawer.querySelector('textarea.pru-md-textarea');
|
|
397
|
+
if (ta) ta.focus();
|
|
398
|
+
});
|
|
399
|
+
hlLayer.appendChild(box);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function renderToolbar() {
|
|
404
|
+
bar.textContent = '';
|
|
405
|
+
bar.setAttribute('data-drawer-open', state.drawerOpen ? 'true' : 'false');
|
|
406
|
+
|
|
407
|
+
const brand = el('span', { class: 'pru-brand' });
|
|
408
|
+
const eye = el('span', { class: 'pru-eye', 'aria-hidden': 'true' });
|
|
409
|
+
eye.appendChild(el('i'));
|
|
410
|
+
brand.append(eye, el('span', null, 'CMS'));
|
|
411
|
+
if (state.dirty) {
|
|
412
|
+
brand.appendChild(el('span', { class: 'pru-dot', 'data-dirty': 'true', 'aria-label': 'Unsaved changes' }));
|
|
413
|
+
}
|
|
414
|
+
bar.appendChild(brand);
|
|
415
|
+
|
|
416
|
+
if (ENTRIES.length > 1) {
|
|
417
|
+
const sel = el('select', { class: 'pru-entry-select', 'aria-label': 'Active content entry' });
|
|
418
|
+
const activeKey = entryId(state.activeRef);
|
|
419
|
+
for (const ref of ENTRIES) {
|
|
420
|
+
const key = entryId(ref);
|
|
421
|
+
const label = ref.collection + '/' + ref.slug + (ref.locale ? ' (' + ref.locale + ')' : '');
|
|
422
|
+
const opt = el('option', { value: key }, label);
|
|
423
|
+
opt.selected = key === activeKey;
|
|
424
|
+
sel.appendChild(opt);
|
|
425
|
+
}
|
|
426
|
+
sel.addEventListener('change', () => {
|
|
427
|
+
const next = ENTRIES.find(e => entryId(e) === sel.value);
|
|
428
|
+
if (!next) return;
|
|
429
|
+
closeInlineEditor(true);
|
|
430
|
+
state.activeRef = next;
|
|
431
|
+
saveSessionState();
|
|
432
|
+
void fetchActiveEntry();
|
|
433
|
+
});
|
|
434
|
+
bar.appendChild(sel);
|
|
435
|
+
} else {
|
|
436
|
+
const r = state.activeRef;
|
|
437
|
+
const badgeLabel = r.collection + '/' + r.slug + (r.locale ? ' (' + r.locale + ')' : '');
|
|
438
|
+
bar.appendChild(el('span', { class: 'pru-entry-badge' }, badgeLabel));
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const editBtn = el('button', {
|
|
442
|
+
type: 'button',
|
|
443
|
+
class: 'pru-btn',
|
|
444
|
+
'aria-pressed': state.editMode ? 'true' : 'false',
|
|
445
|
+
}, state.editMode ? 'Editing on page' : 'Edit on page');
|
|
446
|
+
editBtn.addEventListener('click', () => {
|
|
447
|
+
if (state.activeInline) closeInlineEditor(true);
|
|
448
|
+
state.editMode = !state.editMode;
|
|
449
|
+
saveSessionState();
|
|
450
|
+
renderToolbar();
|
|
451
|
+
renderHighlights();
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const drawerBtn = el('button', {
|
|
455
|
+
type: 'button',
|
|
456
|
+
class: 'pru-btn',
|
|
457
|
+
'aria-pressed': state.drawerOpen ? 'true' : 'false',
|
|
458
|
+
}, state.drawerOpen ? 'Hide panel' : 'Fields & Markdown');
|
|
459
|
+
drawerBtn.addEventListener('click', () => {
|
|
460
|
+
state.drawerOpen = !state.drawerOpen;
|
|
461
|
+
saveSessionState();
|
|
462
|
+
renderToolbar();
|
|
463
|
+
renderDrawer();
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
bar.append(editBtn, drawerBtn);
|
|
467
|
+
|
|
468
|
+
if (state.entry && state.entry.editUrl) {
|
|
469
|
+
bar.appendChild(el('a', {
|
|
470
|
+
class: 'pru-btn pru-btn-ghost',
|
|
471
|
+
href: state.entry.editUrl,
|
|
472
|
+
title: 'Open full-page CMS editor',
|
|
473
|
+
}, 'Full CMS'));
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const drawerCtrl = createDrawerController({
|
|
478
|
+
drawer,
|
|
479
|
+
state,
|
|
480
|
+
saveSessionState,
|
|
481
|
+
renderToolbar,
|
|
482
|
+
renderHighlights,
|
|
483
|
+
scheduleHighlights,
|
|
484
|
+
matchingBodyAnchors,
|
|
485
|
+
replaceAnchorHtml,
|
|
486
|
+
scheduleBodyPreview,
|
|
487
|
+
setDirty,
|
|
488
|
+
persistInline,
|
|
489
|
+
});
|
|
490
|
+
const renderDrawer = () => {
|
|
491
|
+
syncPageDockOffset();
|
|
492
|
+
drawerCtrl.render();
|
|
493
|
+
};
|
|
494
|
+
const renderDrawerFooter = () => drawerCtrl.renderFooter();
|
|
495
|
+
const saveDrawer = () => drawerCtrl.save();
|
|
496
|
+
|
|
497
|
+
window.addEventListener('scroll', () => {
|
|
498
|
+
if (state.editMode) scheduleHighlights();
|
|
499
|
+
if (state.activeInline) positionInlineBar(state.activeInline.el);
|
|
500
|
+
}, { passive: true });
|
|
501
|
+
window.addEventListener('resize', () => {
|
|
502
|
+
syncPageDockOffset();
|
|
503
|
+
if (state.editMode) scheduleHighlights();
|
|
504
|
+
if (state.activeInline) positionInlineBar(state.activeInline.el);
|
|
505
|
+
}, { passive: true });
|
|
506
|
+
|
|
507
|
+
document.addEventListener('keydown', (ev) => {
|
|
508
|
+
if (ev.altKey && ev.shiftKey && ev.key.toLowerCase() === 'e') {
|
|
509
|
+
ev.preventDefault();
|
|
510
|
+
if (state.activeInline) closeInlineEditor(true);
|
|
511
|
+
state.editMode = !state.editMode;
|
|
512
|
+
saveSessionState();
|
|
513
|
+
renderToolbar();
|
|
514
|
+
renderHighlights();
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if ((ev.metaKey || ev.ctrlKey) && ev.key.toLowerCase() === 's' && (state.drawerOpen || state.dirty)) {
|
|
518
|
+
ev.preventDefault();
|
|
519
|
+
if (state.dirty) void saveDrawer();
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (ev.key === 'Escape') {
|
|
523
|
+
if (state.activeInline) {
|
|
524
|
+
closeInlineEditor(true);
|
|
525
|
+
} else if (state.drawerOpen) {
|
|
526
|
+
state.drawerOpen = false;
|
|
527
|
+
saveSessionState();
|
|
528
|
+
renderToolbar();
|
|
529
|
+
renderDrawer();
|
|
530
|
+
} else if (state.editMode) {
|
|
531
|
+
state.editMode = false;
|
|
532
|
+
saveSessionState();
|
|
533
|
+
renderToolbar();
|
|
534
|
+
renderHighlights();
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
renderToolbar();
|
|
540
|
+
void fetchActiveEntry().then(() => {
|
|
541
|
+
if (restored && typeof restored.justSaved === 'number' && Date.now() - restored.justSaved < 8000) {
|
|
542
|
+
showToast('Saved to working tree.', 'success');
|
|
543
|
+
saveSessionState({ justSaved: undefined });
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
})();
|
|
547
|
+
`
|