@pirateandfox/pagestack 0.1.0-beta.7 → 0.1.0-beta.8
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/README.md +39 -12
- package/index.mjs +18 -6
- package/package.json +1 -1
- package/src/client/auth.ts +5 -5
- package/src/client/collection.ts +6 -6
- package/src/client/editor.ts +105 -105
- package/src/client/form.ts +26 -26
- package/src/client/global.ts +4 -4
- package/src/client/inline.ts +4 -4
- package/src/client/inserter.ts +11 -11
- package/src/client/invite.ts +4 -4
- package/src/client/item.ts +202 -54
- package/src/client/live.ts +92 -0
- package/src/client/media.ts +12 -12
- package/src/client/pages.ts +14 -14
- package/src/client/picker.ts +30 -30
- package/src/client/preview.css +58 -58
- package/src/client/rich.ts +14 -14
- package/src/client/shell.css +281 -272
- package/src/client/state.ts +2 -2
- package/src/client/tomarkdown.ts +4 -2
- package/src/client/tree.ts +11 -11
- package/src/client/ui.ts +13 -13
- package/src/client/users.ts +6 -6
- package/src/core/auth.ts +1 -1
- package/src/core/markdown.ts +54 -5
- package/src/core/pages.ts +6 -2
- package/src/core/storage-github.ts +51 -5
- package/src/core/storage.ts +14 -0
- package/src/routes/Nav.astro +5 -3
- package/src/routes/api/collection.ts +30 -8
- package/src/routes/api/live.ts +27 -0
- package/src/routes/edit-collection.astro +14 -14
- package/src/routes/edit-global.astro +8 -8
- package/src/routes/edit-index.astro +18 -18
- package/src/routes/edit-item.astro +25 -23
- package/src/routes/edit-media.astro +9 -9
- package/src/routes/edit-page.astro +32 -30
- package/src/routes/edit-users.astro +9 -9
- package/src/routes/enroll.astro +6 -6
- package/src/routes/login.astro +6 -6
- package/src/routes/preview-item.astro +1 -1
- package/src/routes/preview.astro +1 -1
- package/src/runtime.ts +3 -3
- package/src/server/context.ts +3 -3
- package/src/server/middleware.ts +19 -4
package/src/client/editor.ts
CHANGED
|
@@ -28,10 +28,10 @@ interface Boot {
|
|
|
28
28
|
reserved: string[];
|
|
29
29
|
options: { storage: string; auth: string; siteName: string; user: string | null; pullRequest: boolean; viewUrl: string; interactiveSelector: string; redirects: boolean };
|
|
30
30
|
}
|
|
31
|
-
const boot = (window as any).
|
|
31
|
+
const boot = (window as any).__PS__ as Boot;
|
|
32
32
|
const { registry } = boot;
|
|
33
33
|
const $ = <T extends HTMLElement = HTMLElement>(sel: string) => document.querySelector<T>(sel)!;
|
|
34
|
-
const iframe = $<HTMLIFrameElement>('#
|
|
34
|
+
const iframe = $<HTMLIFrameElement>('#ps-preview');
|
|
35
35
|
const state = new EditorState(boot.slug, boot.page, boot.hash);
|
|
36
36
|
const isHome = boot.slug === 'index';
|
|
37
37
|
let selected: string | null = null;
|
|
@@ -57,8 +57,8 @@ const pageLinks = boot.pages.map((p) => ({ url: p.url, title: p.title.split(/\s[
|
|
|
57
57
|
const idoc = () => iframe.contentDocument!;
|
|
58
58
|
const iwin = () => iframe.contentWindow!;
|
|
59
59
|
const isTop = (path: string) => /^sections\.\d+$/.test(path);
|
|
60
|
-
const topSections = (doc: Document) => Array.from(doc.querySelectorAll<HTMLElement>('[data-
|
|
61
|
-
const nodeEl = (path: string) => idoc().querySelector<HTMLElement>(`[data-
|
|
60
|
+
const topSections = (doc: Document) => Array.from(doc.querySelectorAll<HTMLElement>('[data-ps-node]')).filter((el) => isTop(el.dataset.psNode!));
|
|
61
|
+
const nodeEl = (path: string) => idoc().querySelector<HTMLElement>(`[data-ps-node="${path}"]`);
|
|
62
62
|
|
|
63
63
|
/** Where sections live in the page, remembered so an emptied page can still be filled. */
|
|
64
64
|
let sectionHost: HTMLElement | null = null;
|
|
@@ -77,7 +77,7 @@ function onPreviewLoad() {
|
|
|
77
77
|
iwin().addEventListener('scroll', placeOverlays, { passive: true });
|
|
78
78
|
iwin().addEventListener('resize', placeOverlays, { passive: true });
|
|
79
79
|
sectionHost = topSections(d)[0]?.parentElement ?? d.querySelector('main') ?? d.body;
|
|
80
|
-
d.documentElement.style.setProperty('--
|
|
80
|
+
d.documentElement.style.setProperty('--ps-ui', String(uiScale));
|
|
81
81
|
previewReady = true;
|
|
82
82
|
if (state.dirty) render(); else { decorate(); seedHtml(); }
|
|
83
83
|
}
|
|
@@ -89,40 +89,40 @@ async function seedHtml() {
|
|
|
89
89
|
|
|
90
90
|
function onPreviewHover(e: MouseEvent) {
|
|
91
91
|
const t = e.target as HTMLElement;
|
|
92
|
-
if (t.closest('[data-
|
|
93
|
-
hover(t.closest<HTMLElement>('[data-
|
|
92
|
+
if (t.closest('[data-ps-ui]')) return;
|
|
93
|
+
hover(t.closest<HTMLElement>('[data-ps-node]'));
|
|
94
94
|
}
|
|
95
95
|
function hover(el: HTMLElement | null) {
|
|
96
|
-
idoc().querySelectorAll('[data-
|
|
97
|
-
if (el) el.setAttribute('data-
|
|
96
|
+
idoc().querySelectorAll('[data-ps-hover]').forEach((x) => x.removeAttribute('data-ps-hover'));
|
|
97
|
+
if (el) el.setAttribute('data-ps-hover', '');
|
|
98
98
|
}
|
|
99
99
|
function onPreviewClick(e: MouseEvent) {
|
|
100
100
|
const t = e.target as HTMLElement;
|
|
101
|
-
if (t.closest('[data-
|
|
102
|
-
const field = t.closest<HTMLElement>('[data-
|
|
103
|
-
const node = t.closest<HTMLElement>('[data-
|
|
101
|
+
if (t.closest('[data-ps-ui], [data-ps-toolbar], .ps-popmenu')) return; // handled by their own listeners
|
|
102
|
+
const field = t.closest<HTMLElement>('[data-ps-field]');
|
|
103
|
+
const node = t.closest<HTMLElement>('[data-ps-node]');
|
|
104
104
|
// Let the page's own interactive bits work (accordion summaries, tab titles…); block navigation.
|
|
105
105
|
const interactive = t.closest(boot.options.interactiveSelector || 'summary');
|
|
106
106
|
if (!interactive) e.preventDefault();
|
|
107
107
|
if (field) {
|
|
108
108
|
e.stopPropagation();
|
|
109
|
-
select(nodePathOf(field.dataset.
|
|
110
|
-
const type = field.dataset.
|
|
111
|
-
if (type === 'image') pickImage(getAt(state.doc, field.dataset.
|
|
112
|
-
else if (inline.activePath !== field.dataset.
|
|
109
|
+
select(nodePathOf(field.dataset.psField!), { fromPreview: true });
|
|
110
|
+
const type = field.dataset.psFieldType;
|
|
111
|
+
if (type === 'image') pickImage(getAt(state.doc, field.dataset.psField!), (img) => setField(field.dataset.psField!, img));
|
|
112
|
+
else if (inline.activePath !== field.dataset.psField) inline.start(field);
|
|
113
113
|
return;
|
|
114
114
|
}
|
|
115
115
|
if (inline.isActive) inline.stop(true);
|
|
116
|
-
if (node) select(node.dataset.
|
|
116
|
+
if (node) select(node.dataset.psNode!, { fromPreview: true }); else select(null);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
const inline = new InlineEditor({
|
|
120
120
|
onSlash: (fieldPath) => slash(fieldPath),
|
|
121
121
|
pages: pageLinks,
|
|
122
|
-
onStart: () => { state.snapshot(); idoc().body.setAttribute('data-
|
|
122
|
+
onStart: () => { state.snapshot(); idoc().body.setAttribute('data-ps-typing', ''); },
|
|
123
123
|
onInput: (path, value) => { state.apply(setAt(state.doc, path, value), 'inline', false); refreshButtons(); },
|
|
124
124
|
onCommit: (path, value, changed) => {
|
|
125
|
-
idoc().body.removeAttribute('data-
|
|
125
|
+
idoc().body.removeAttribute('data-ps-typing');
|
|
126
126
|
if (changed) state.apply(setAt(state.doc, path, value), 'inline', false); else state.dropSnapshot();
|
|
127
127
|
refreshChrome();
|
|
128
128
|
if (changed || renderQueued) scheduleRender(0);
|
|
@@ -179,10 +179,10 @@ function swap(html: string) {
|
|
|
179
179
|
/** Re-apply selection highlight + overlays after the DOM changed. */
|
|
180
180
|
function decorate() {
|
|
181
181
|
const d = idoc();
|
|
182
|
-
d.querySelectorAll('[data-
|
|
183
|
-
d.querySelector('.
|
|
182
|
+
d.querySelectorAll('[data-ps-selected]').forEach((x) => x.removeAttribute('data-ps-selected'));
|
|
183
|
+
d.querySelector('.ps-toolbar')?.remove();
|
|
184
184
|
const el = selected ? nodeEl(selected) : null;
|
|
185
|
-
if (el) { el.setAttribute('data-
|
|
185
|
+
if (el) { el.setAttribute('data-ps-selected', ''); mountToolbar(el); }
|
|
186
186
|
decorateEmpty();
|
|
187
187
|
placeOverlays();
|
|
188
188
|
}
|
|
@@ -191,28 +191,28 @@ function decorate() {
|
|
|
191
191
|
function decorateEmpty() {
|
|
192
192
|
const d = idoc();
|
|
193
193
|
const empty = !state.doc.sections.length;
|
|
194
|
-
const box = d.querySelector('.
|
|
194
|
+
const box = d.querySelector('.ps-empty');
|
|
195
195
|
if (!empty) { box?.remove(); return; }
|
|
196
196
|
if (box) return;
|
|
197
197
|
const div = d.createElement('div');
|
|
198
|
-
div.className = '
|
|
199
|
-
div.innerHTML = `<p class="
|
|
198
|
+
div.className = 'ps-empty'; div.setAttribute('data-ps-ui', '');
|
|
199
|
+
div.innerHTML = `<p class="ps-empty__title">This page is empty</p><p>Add your first block to get started.</p><button type="button">${icons.plus} Add block</button>`;
|
|
200
200
|
div.querySelector('button')!.addEventListener('click', () => openInserter({ listPath: 'sections', index: 0 }));
|
|
201
201
|
sectionHost?.append(div);
|
|
202
202
|
}
|
|
203
203
|
|
|
204
204
|
function mountToolbar(el: HTMLElement) {
|
|
205
205
|
const d = idoc();
|
|
206
|
-
const path = el.dataset.
|
|
206
|
+
const path = el.dataset.psNode!;
|
|
207
207
|
const node = nodeAt(state.doc, path)!;
|
|
208
208
|
const canAdd = !!allowedChildTypes(node.type, registry)?.length;
|
|
209
209
|
const tb = d.createElement('div');
|
|
210
|
-
tb.className = '
|
|
211
|
-
tb.setAttribute('data-
|
|
210
|
+
tb.className = 'ps-toolbar';
|
|
211
|
+
tb.setAttribute('data-ps-ui', '');
|
|
212
212
|
tb.setAttribute('role', 'toolbar');
|
|
213
213
|
tb.setAttribute('aria-label', 'Block tools');
|
|
214
|
-
tb.innerHTML = `${isTop(path) ? '' : `<button type="button" data-act="parent" title="Select parent block" aria-label="Select parent block">${icons.parent}</button>`}<span class="
|
|
215
|
-
tb.querySelector('.
|
|
214
|
+
tb.innerHTML = `${isTop(path) ? '' : `<button type="button" data-act="parent" title="Select parent block" aria-label="Select parent block">${icons.parent}</button>`}<span class="ps-grip" title="Drag to move">${icons.grip}</span><span class="ps-toolbar__label"></span><button type="button" data-act="up" title="Move up" aria-label="Move up">${icons.up}</button><button type="button" data-act="down" title="Move down" aria-label="Move down">${icons.down}</button>${canAdd ? `<button type="button" data-act="add" title="Add a block inside" aria-label="Add a block inside">${icons.plus}</button>` : ''}<button type="button" data-act="more" title="Options" aria-label="Options">${icons.more}</button>`;
|
|
215
|
+
tb.querySelector('.ps-toolbar__label')!.textContent = registry[node.type]?.label ?? node.type;
|
|
216
216
|
tb.addEventListener('click', (e) => {
|
|
217
217
|
const b = (e.target as HTMLElement).closest('button');
|
|
218
218
|
if (!b) return;
|
|
@@ -221,14 +221,14 @@ function mountToolbar(el: HTMLElement) {
|
|
|
221
221
|
});
|
|
222
222
|
tb.addEventListener('mousedown', (e) => e.stopPropagation());
|
|
223
223
|
d.body.append(tb);
|
|
224
|
-
tb.querySelector('.
|
|
224
|
+
tb.querySelector('.ps-grip')!.addEventListener('pointerdown', (e) => startDrag(e as PointerEvent, el));
|
|
225
225
|
}
|
|
226
226
|
|
|
227
227
|
/** Keep the floating pieces next to what they belong to; the page can scroll under them. */
|
|
228
228
|
function placeOverlays() {
|
|
229
229
|
if (!previewReady) return;
|
|
230
230
|
const d = idoc();
|
|
231
|
-
const tb = d.querySelector<HTMLElement>('.
|
|
231
|
+
const tb = d.querySelector<HTMLElement>('.ps-toolbar');
|
|
232
232
|
const el = selected ? nodeEl(selected) : null;
|
|
233
233
|
if (tb && el) {
|
|
234
234
|
const r = el.getBoundingClientRect();
|
|
@@ -239,7 +239,7 @@ function placeOverlays() {
|
|
|
239
239
|
tb.style.top = `${Math.max(4, r.top - size.height - 6)}px`;
|
|
240
240
|
tb.style.left = `${Math.max(4, Math.min(r.left, vw - size.width - 4))}px`;
|
|
241
241
|
}
|
|
242
|
-
const line = d.querySelector<HTMLElement>('.
|
|
242
|
+
const line = d.querySelector<HTMLElement>('.ps-insertline');
|
|
243
243
|
if (line) {
|
|
244
244
|
const y = target ? insertionY(target) : null;
|
|
245
245
|
line.style.display = y === null ? 'none' : '';
|
|
@@ -259,21 +259,21 @@ function insertionY(t: Target): number | null {
|
|
|
259
259
|
|
|
260
260
|
function siblings(listPath: string): HTMLElement[] {
|
|
261
261
|
const re = new RegExp(`^${listPath.replace(/\./g, '\\.')}\\.(\\d+)$`);
|
|
262
|
-
return Array.from(idoc().querySelectorAll<HTMLElement>('[data-
|
|
262
|
+
return Array.from(idoc().querySelectorAll<HTMLElement>('[data-ps-node]')).filter((e) => re.test(e.dataset.psNode!)).sort((a, b) => Number(a.dataset.psNode!.match(re)![1]) - Number(b.dataset.psNode!.match(re)![1]));
|
|
263
263
|
}
|
|
264
264
|
|
|
265
265
|
/** Drag a block to reorder it among its siblings, in whichever direction they are laid out. */
|
|
266
266
|
function startDrag(e: PointerEvent, el: HTMLElement) {
|
|
267
267
|
e.preventDefault();
|
|
268
268
|
const d = idoc();
|
|
269
|
-
const path = el.dataset.
|
|
269
|
+
const path = el.dataset.psNode!;
|
|
270
270
|
const { listPath, index: from } = splitNodePath(path);
|
|
271
271
|
const kids = siblings(listPath);
|
|
272
272
|
if (kids.length < 2) return;
|
|
273
273
|
const [a, b] = [kids[0].getBoundingClientRect(), kids[1].getBoundingClientRect()];
|
|
274
274
|
const horizontal = Math.abs(a.top - b.top) < a.height / 2;
|
|
275
|
-
const line = d.createElement('div'); line.className = `
|
|
276
|
-
el.classList.add('
|
|
275
|
+
const line = d.createElement('div'); line.className = `ps-dropline${horizontal ? ' ps-dropline--v' : ''}`; line.setAttribute('data-ps-ui', ''); d.body.append(line);
|
|
276
|
+
el.classList.add('ps-dragging');
|
|
277
277
|
let to = from;
|
|
278
278
|
const move = (ev: PointerEvent) => {
|
|
279
279
|
let best = 0, dist = Infinity;
|
|
@@ -288,7 +288,7 @@ function startDrag(e: PointerEvent, el: HTMLElement) {
|
|
|
288
288
|
};
|
|
289
289
|
const up = () => {
|
|
290
290
|
d.removeEventListener('pointermove', move); d.removeEventListener('pointerup', up);
|
|
291
|
-
line.remove(); el.classList.remove('
|
|
291
|
+
line.remove(); el.classList.remove('ps-dragging');
|
|
292
292
|
const dest = to > from ? to - 1 : to;
|
|
293
293
|
if (dest !== from) { selected = `${listPath}.${dest}`; state.apply(moveNode(state.doc, path, listPath, dest)); scheduleRender(0); } else refreshChrome();
|
|
294
294
|
};
|
|
@@ -302,7 +302,7 @@ function onPreviewMove(e: MouseEvent) {
|
|
|
302
302
|
const { clientY, target: t } = e;
|
|
303
303
|
plusRaf = requestAnimationFrame(() => {
|
|
304
304
|
plusRaf = 0;
|
|
305
|
-
if (inline.isActive || (t as HTMLElement).closest?.('.
|
|
305
|
+
if (inline.isActive || (t as HTMLElement).closest?.('.ps-toolbar, [data-ps-toolbar]')) { showPlus(null); return; }
|
|
306
306
|
const secs = topSections(idoc());
|
|
307
307
|
let index: number | null = null, y = 0;
|
|
308
308
|
secs.forEach((s, i) => {
|
|
@@ -316,11 +316,11 @@ function onPreviewMove(e: MouseEvent) {
|
|
|
316
316
|
function showPlus(at: { index: number; y: number } | null) {
|
|
317
317
|
if (!previewReady) return;
|
|
318
318
|
const d = idoc();
|
|
319
|
-
let plus = d.querySelector<HTMLElement>('.
|
|
319
|
+
let plus = d.querySelector<HTMLElement>('.ps-plus');
|
|
320
320
|
if (!at) { if (plus) plus.hidden = true; return; }
|
|
321
321
|
if (!plus) {
|
|
322
322
|
plus = d.createElement('button');
|
|
323
|
-
plus.type = 'button'; plus.className = '
|
|
323
|
+
plus.type = 'button'; plus.className = 'ps-plus'; plus.setAttribute('data-ps-ui', ''); plus.title = 'Add block'; plus.setAttribute('aria-label', 'Add block'); plus.innerHTML = icons.plus;
|
|
324
324
|
plus.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); openInserter({ listPath: 'sections', index: Number(plus!.dataset.index) }); plus!.hidden = true; });
|
|
325
325
|
d.body.append(plus);
|
|
326
326
|
}
|
|
@@ -339,26 +339,26 @@ function select(path: string | null, opts: { fromPreview?: boolean; scroll?: boo
|
|
|
339
339
|
function refreshButtons() {
|
|
340
340
|
const dirty = state.dirty;
|
|
341
341
|
const draft = state.doc.status === 'draft';
|
|
342
|
-
$<HTMLButtonElement>('#
|
|
343
|
-
$<HTMLButtonElement>('#
|
|
344
|
-
const save = $<HTMLButtonElement>('#
|
|
342
|
+
$<HTMLButtonElement>('#ps-undo').disabled = !state.canUndo;
|
|
343
|
+
$<HTMLButtonElement>('#ps-redo').disabled = !state.canRedo;
|
|
344
|
+
const save = $<HTMLButtonElement>('#ps-save');
|
|
345
345
|
save.disabled = !dirty || saving;
|
|
346
346
|
save.textContent = draft ? 'Save draft' : 'Save';
|
|
347
|
-
save.classList.toggle('
|
|
348
|
-
save.classList.toggle('
|
|
349
|
-
const publish = $<HTMLButtonElement>('#
|
|
347
|
+
save.classList.toggle('ps-btn--primary', !draft);
|
|
348
|
+
save.classList.toggle('ps-btn--ghost', draft);
|
|
349
|
+
const publish = $<HTMLButtonElement>('#ps-publish');
|
|
350
350
|
publish.hidden = !draft;
|
|
351
351
|
publish.disabled = saving;
|
|
352
|
-
$('#
|
|
353
|
-
$('#
|
|
354
|
-
$('#
|
|
355
|
-
$('#
|
|
352
|
+
$('#ps-discard').hidden = !dirty;
|
|
353
|
+
$('#ps-view').hidden = draft || boot.page.status === 'draft';
|
|
354
|
+
$('#ps-draft-pill').hidden = !draft;
|
|
355
|
+
$('#ps-title').textContent = state.doc.title;
|
|
356
356
|
document.title = `${dirty ? '• ' : ''}Edit: ${state.doc.title}`;
|
|
357
357
|
}
|
|
358
358
|
|
|
359
359
|
function refreshChrome(opts: { inspector?: boolean } = {}) {
|
|
360
360
|
if (previewReady) decorate();
|
|
361
|
-
renderTree($('#
|
|
361
|
+
renderTree($('#ps-tree'), state.doc, registry, selected, {
|
|
362
362
|
onSelect: (p) => select(p),
|
|
363
363
|
onMove: (fromPath, toList, toIndex) => {
|
|
364
364
|
const id = nodeAt(state.doc, fromPath)!.id;
|
|
@@ -382,10 +382,10 @@ const parentTypeOf = (listPath: string): string | null => (listPath === 'section
|
|
|
382
382
|
|
|
383
383
|
// ---------- sidebar ----------
|
|
384
384
|
function renderInspector() {
|
|
385
|
-
const root = $('#
|
|
385
|
+
const root = $('#ps-inspector');
|
|
386
386
|
root.innerHTML = '';
|
|
387
387
|
const tabs = document.createElement('div');
|
|
388
|
-
tabs.className = '
|
|
388
|
+
tabs.className = 'ps-tabs'; tabs.setAttribute('role', 'tablist');
|
|
389
389
|
tabs.innerHTML = `<button type="button" role="tab" data-tab="page" aria-selected="${tab === 'page'}">Page</button><button type="button" role="tab" data-tab="block" aria-selected="${tab === 'block'}">Block</button>`;
|
|
390
390
|
tabs.addEventListener('click', (e) => { const b = (e.target as HTMLElement).closest<HTMLElement>('[data-tab]'); if (b) { tab = b.dataset.tab as typeof tab; renderInspector(); } });
|
|
391
391
|
root.append(tabs);
|
|
@@ -414,15 +414,15 @@ function applyField(path: string, value: unknown, live: boolean | undefined, fie
|
|
|
414
414
|
function renderBlockTab(root: HTMLElement, node: Node, path: string) {
|
|
415
415
|
const schema = registry[node.type];
|
|
416
416
|
const head = document.createElement('div');
|
|
417
|
-
head.className = '
|
|
417
|
+
head.className = 'ps-blockhead';
|
|
418
418
|
const name = document.createElement('strong'); name.textContent = schema?.label ?? node.type;
|
|
419
419
|
head.append(name);
|
|
420
|
-
if (schema?.description) { const d = document.createElement('p'); d.className = '
|
|
420
|
+
if (schema?.description) { const d = document.createElement('p'); d.className = 'ps-muted'; d.textContent = schema.description; head.append(d); }
|
|
421
421
|
root.append(breadcrumbs(path), head);
|
|
422
422
|
const actions = document.createElement('div');
|
|
423
|
-
actions.className = '
|
|
423
|
+
actions.className = 'ps-actions';
|
|
424
424
|
const allowed = allowedChildTypes(node.type, registry);
|
|
425
|
-
actions.innerHTML = `${allowed?.length ? `<button type="button" data-act="add" class="
|
|
425
|
+
actions.innerHTML = `${allowed?.length ? `<button type="button" data-act="add" class="ps-btn ps-btn--small">${icons.plus} Add block</button>` : ''}<button type="button" data-act="dup" class="ps-btn ps-btn--small">Duplicate</button><button type="button" data-act="del" class="ps-btn ps-btn--small ps-btn--danger">Delete</button>`;
|
|
426
426
|
actions.addEventListener('click', (e) => { const b = (e.target as HTMLElement).closest('button'); if (b) act(b.dataset.act!); });
|
|
427
427
|
root.append(actions);
|
|
428
428
|
const form = document.createElement('div');
|
|
@@ -436,7 +436,7 @@ function renderBlockTab(root: HTMLElement, node: Node, path: string) {
|
|
|
436
436
|
/** Page › Hero › Button; each step selects that block. */
|
|
437
437
|
function breadcrumbs(path: string): HTMLElement {
|
|
438
438
|
const nav = document.createElement('nav');
|
|
439
|
-
nav.className = '
|
|
439
|
+
nav.className = 'ps-crumbs'; nav.setAttribute('aria-label', 'Block position');
|
|
440
440
|
const add = (label: string, to: string | null, current = false) => {
|
|
441
441
|
if (nav.childElementCount) nav.append(' › ');
|
|
442
442
|
const b = document.createElement('button');
|
|
@@ -458,17 +458,17 @@ function renderPageTab(root: HTMLElement) {
|
|
|
458
458
|
const doc = state.doc;
|
|
459
459
|
const draft = doc.status === 'draft';
|
|
460
460
|
const summary = document.createElement('div');
|
|
461
|
-
summary.className = '
|
|
461
|
+
summary.className = 'ps-summary';
|
|
462
462
|
const row = (label: string, value: string, button?: { text: string; id: string; run: () => void; disabled?: boolean }) => {
|
|
463
|
-
const r = document.createElement('div'); r.className = '
|
|
464
|
-
const l = document.createElement('span'); l.className = '
|
|
465
|
-
const v = document.createElement('span'); v.textContent = value; v.className = '
|
|
463
|
+
const r = document.createElement('div'); r.className = 'ps-summary__row';
|
|
464
|
+
const l = document.createElement('span'); l.className = 'ps-muted'; l.textContent = label;
|
|
465
|
+
const v = document.createElement('span'); v.textContent = value; v.className = 'ps-ellipsis';
|
|
466
466
|
r.append(l, v);
|
|
467
|
-
if (button) { const b = document.createElement('button'); b.type = 'button'; b.id = button.id; b.className = '
|
|
467
|
+
if (button) { const b = document.createElement('button'); b.type = 'button'; b.id = button.id; b.className = 'ps-linkbtn'; b.textContent = button.text; b.disabled = !!button.disabled; b.addEventListener('click', button.run); r.append(b); }
|
|
468
468
|
summary.append(r);
|
|
469
469
|
};
|
|
470
|
-
row('Status', draft ? 'Draft' : 'Published', draft ? undefined : { text: 'Switch to draft', id: '
|
|
471
|
-
row('URL', boot.options.viewUrl, isHome ? undefined : { text: 'Change', id: '
|
|
470
|
+
row('Status', draft ? 'Draft' : 'Published', draft ? undefined : { text: 'Switch to draft', id: 'ps-unpublish', run: () => publish(false), disabled: isHome });
|
|
471
|
+
row('URL', boot.options.viewUrl, isHome ? undefined : { text: 'Change', id: 'ps-change-url', run: changeUrl });
|
|
472
472
|
root.append(summary);
|
|
473
473
|
|
|
474
474
|
const form = document.createElement('div');
|
|
@@ -493,13 +493,13 @@ function renderPageTab(root: HTMLElement) {
|
|
|
493
493
|
}
|
|
494
494
|
|
|
495
495
|
const tools = document.createElement('div');
|
|
496
|
-
tools.className = '
|
|
497
|
-
tools.innerHTML = `<button type="button" id="
|
|
498
|
-
tools.querySelector('#
|
|
499
|
-
tools.querySelector('#
|
|
496
|
+
tools.className = 'ps-actions';
|
|
497
|
+
tools.innerHTML = `<button type="button" id="ps-revisions" class="ps-btn ps-btn--small">Revisions</button>${isHome ? '' : '<button type="button" id="ps-delete-page" class="ps-btn ps-btn--small ps-btn--danger">Delete page</button>'}`;
|
|
498
|
+
tools.querySelector('#ps-revisions')!.addEventListener('click', showRevisions);
|
|
499
|
+
tools.querySelector('#ps-delete-page')?.addEventListener('click', deletePage);
|
|
500
500
|
root.append(tools, hint('Click anything in the page to edit it. Press ? for keyboard shortcuts.'));
|
|
501
501
|
}
|
|
502
|
-
const hint = (text: string) => { const p = document.createElement('p'); p.className = '
|
|
502
|
+
const hint = (text: string) => { const p = document.createElement('p'); p.className = 'ps-muted ps-hint'; p.textContent = text; return p; };
|
|
503
503
|
|
|
504
504
|
// ---------- block actions ----------
|
|
505
505
|
function blockMenu(path: string): MenuItem[] {
|
|
@@ -600,7 +600,7 @@ function setField(fieldPath: string, value: unknown) {
|
|
|
600
600
|
}
|
|
601
601
|
|
|
602
602
|
// ---------- clipboard: works across pages of the site ----------
|
|
603
|
-
const CLIP = '
|
|
603
|
+
const CLIP = 'ps:clipboard';
|
|
604
604
|
function writeClipboard(n: Node) { try { localStorage.setItem(CLIP, JSON.stringify(n)); } catch { /* private mode */ } }
|
|
605
605
|
function readClipboard(): Node | null { try { return JSON.parse(localStorage.getItem(CLIP) ?? 'null'); } catch { return null; } }
|
|
606
606
|
|
|
@@ -659,9 +659,9 @@ let leftView: 'list' | 'add' | 'off' = 'list';
|
|
|
659
659
|
function setLeft(view: typeof leftView) {
|
|
660
660
|
leftView = view;
|
|
661
661
|
document.body.dataset.left = view;
|
|
662
|
-
$('#
|
|
663
|
-
$('#
|
|
664
|
-
if (view !== 'add') { target = null; slashBlock = null; placeOverlays(); idoc()?.querySelector('.
|
|
662
|
+
$('#ps-toggle-add').classList.toggle('is-active', view === 'add');
|
|
663
|
+
$('#ps-toggle-list').classList.toggle('is-active', view === 'list');
|
|
664
|
+
if (view !== 'add') { target = null; slashBlock = null; placeOverlays(); idoc()?.querySelector('.ps-insertline')?.remove(); }
|
|
665
665
|
fitPreview();
|
|
666
666
|
}
|
|
667
667
|
|
|
@@ -679,14 +679,14 @@ function openInserter(at: Target = defaultTarget()) {
|
|
|
679
679
|
setLeft('add');
|
|
680
680
|
renderInserterPanel();
|
|
681
681
|
const d = idoc();
|
|
682
|
-
if (!d.querySelector('.
|
|
682
|
+
if (!d.querySelector('.ps-insertline')) { const line = d.createElement('div'); line.className = 'ps-insertline'; line.setAttribute('data-ps-ui', ''); d.body.append(line); }
|
|
683
683
|
placeOverlays();
|
|
684
684
|
}
|
|
685
685
|
|
|
686
686
|
function renderInserterPanel() {
|
|
687
687
|
if (!target) return;
|
|
688
688
|
const parent = parentTypeOf(target.listPath);
|
|
689
|
-
renderInserter($('#
|
|
689
|
+
renderInserter($('#ps-inserter'), {
|
|
690
690
|
items: () => insertItems(parent, registry, boot.presets, patterns),
|
|
691
691
|
where: () => (parent ? `Adding to ${registry[parent]?.label ?? parent}` : 'Adding to the page'),
|
|
692
692
|
onPick: (item) => pick(item),
|
|
@@ -738,13 +738,13 @@ let uiScale = 1;
|
|
|
738
738
|
/** Tools drawn inside the preview scale up by the inverse, so they stay a usable size. */
|
|
739
739
|
function applyUiScale(scale: number) {
|
|
740
740
|
uiScale = 1 / scale;
|
|
741
|
-
idoc()?.documentElement?.style.setProperty('--
|
|
741
|
+
idoc()?.documentElement?.style.setProperty('--ps-ui', String(uiScale));
|
|
742
742
|
placeOverlays();
|
|
743
743
|
}
|
|
744
744
|
|
|
745
745
|
/** The site should show its real layout at every width, so a device wider than the stage scales down. */
|
|
746
746
|
function fitPreview() {
|
|
747
|
-
const stage = $('.
|
|
747
|
+
const stage = $('.ps-stage');
|
|
748
748
|
const width = WIDTHS[stage.dataset.device!];
|
|
749
749
|
const room = stage.clientWidth;
|
|
750
750
|
const big = stage.dataset.device === 'desktop' && room >= width;
|
|
@@ -754,33 +754,33 @@ function fitPreview() {
|
|
|
754
754
|
iframe.style.transform = scale < 1 ? `scale(${scale})` : '';
|
|
755
755
|
stage.classList.toggle('is-scaled', scale < 1);
|
|
756
756
|
applyUiScale(scale);
|
|
757
|
-
$('#
|
|
757
|
+
$('#ps-zoom').textContent = scale < 1 ? `${Math.round(scale * 100)}%` : '';
|
|
758
758
|
}
|
|
759
|
-
new ResizeObserver(fitPreview).observe($('.
|
|
759
|
+
new ResizeObserver(fitPreview).observe($('.ps-stage'));
|
|
760
760
|
|
|
761
761
|
// ---------- top bar ----------
|
|
762
|
-
$('#
|
|
763
|
-
$('#
|
|
764
|
-
$('#
|
|
765
|
-
$('#
|
|
766
|
-
const toggleSettings = () => { const off = document.body.dataset.right !== 'off'; document.body.dataset.right = off ? 'off' : 'on'; $('#
|
|
767
|
-
$('#
|
|
768
|
-
$('#
|
|
762
|
+
$('#ps-undo').addEventListener('click', () => { inline.stop(true); state.undo(); });
|
|
763
|
+
$('#ps-redo').addEventListener('click', () => { inline.stop(true); state.redo(); });
|
|
764
|
+
$('#ps-toggle-add').addEventListener('click', () => { if (leftView === 'add') setLeft('list'); else openInserter(); });
|
|
765
|
+
$('#ps-toggle-list').addEventListener('click', () => setLeft(leftView === 'list' ? 'off' : 'list'));
|
|
766
|
+
const toggleSettings = () => { const off = document.body.dataset.right !== 'off'; document.body.dataset.right = off ? 'off' : 'on'; $('#ps-toggle-settings').classList.toggle('is-active', !off); fitPreview(); };
|
|
767
|
+
$('#ps-toggle-settings').addEventListener('click', toggleSettings);
|
|
768
|
+
$('#ps-discard').addEventListener('click', async () => {
|
|
769
769
|
if (!await confirmDialog('Discard changes', 'Discard all unsaved changes to this page?', 'Discard', 'danger')) return;
|
|
770
770
|
const r = await (await fetch(`/api/editor/page/${boot.slug}`)).json();
|
|
771
771
|
renderFull = true;
|
|
772
772
|
state.reset(r.page, r.hash);
|
|
773
773
|
});
|
|
774
|
-
$('#
|
|
775
|
-
$('#
|
|
776
|
-
$('#
|
|
777
|
-
document.querySelectorAll<HTMLButtonElement>('.
|
|
778
|
-
document.querySelectorAll('.
|
|
774
|
+
$('#ps-save').addEventListener('click', () => save());
|
|
775
|
+
$('#ps-publish').addEventListener('click', () => publish(true));
|
|
776
|
+
$('#ps-preview-tab').addEventListener('click', previewInTab);
|
|
777
|
+
document.querySelectorAll<HTMLButtonElement>('.ps-top__devices button').forEach((b) => b.addEventListener('click', () => {
|
|
778
|
+
document.querySelectorAll('.ps-top__devices button').forEach((x) => x.classList.remove('is-active'));
|
|
779
779
|
b.classList.add('is-active');
|
|
780
|
-
$('.
|
|
780
|
+
$('.ps-stage').dataset.device = b.dataset.device;
|
|
781
781
|
fitPreview();
|
|
782
782
|
}));
|
|
783
|
-
$('#
|
|
783
|
+
$('#ps-more').addEventListener('click', (e) => {
|
|
784
784
|
const items: MenuItem[] = [
|
|
785
785
|
{ label: 'Keyboard shortcuts', hint: '?', run: showShortcuts },
|
|
786
786
|
{ label: 'Revisions', run: showRevisions },
|
|
@@ -813,7 +813,7 @@ async function save(opts: { slug?: string } = {}): Promise<{ slug?: string; resu
|
|
|
813
813
|
state.markSaved(data.hash);
|
|
814
814
|
boot.page = state.doc;
|
|
815
815
|
const r = data.result;
|
|
816
|
-
if (r.kind === 'github') toast(r.pullRequest ? `Committed and opened a pull request: ${r.pullRequest}` : `Committed ${String(r.commit).slice(0, 7)} to ${r.branch}. The host will rebuild the site.`, 'ok', { href: r.pullRequest || r.url });
|
|
816
|
+
if (r.kind === 'github') toast(r.pullRequest ? `Committed and opened a pull request: ${r.pullRequest}` : document.getElementById('ps-live') ? 'Saved. Use Go live to put it on the site.' : `Committed ${String(r.commit).slice(0, 7)} to ${r.branch}. The host will rebuild the site.`, 'ok', { href: r.pullRequest || r.url });
|
|
817
817
|
else toast(`Saved to the working tree (${r.files.join(', ')}). Commit it with git when you're happy.`, 'ok');
|
|
818
818
|
setSaveStatus('Saved');
|
|
819
819
|
return data;
|
|
@@ -858,20 +858,20 @@ async function deletePage() {
|
|
|
858
858
|
|
|
859
859
|
async function showRevisions() {
|
|
860
860
|
const box = document.createElement('div');
|
|
861
|
-
box.innerHTML = '<p class="
|
|
861
|
+
box.innerHTML = '<p class="ps-muted">Loading…</p>';
|
|
862
862
|
const d = dialog('Revisions', box, [{ label: 'Close' }], { wide: true });
|
|
863
863
|
const data = await (await fetch(`/api/editor/page/${boot.slug}?revisions=1`)).json();
|
|
864
864
|
const revs = (data.revisions ?? []) as Revision[];
|
|
865
865
|
box.innerHTML = '';
|
|
866
|
-
if (!revs.length) { box.innerHTML = '<p class="
|
|
867
|
-
const ul = document.createElement('ul'); ul.className = '
|
|
866
|
+
if (!revs.length) { box.innerHTML = '<p class="ps-muted">No revisions yet. Each committed save appears here.</p>'; return; }
|
|
867
|
+
const ul = document.createElement('ul'); ul.className = 'ps-revisions';
|
|
868
868
|
for (const r of revs) {
|
|
869
869
|
const li = document.createElement('li');
|
|
870
870
|
const info = document.createElement('div');
|
|
871
871
|
const when = document.createElement('strong'); when.textContent = new Date(r.date).toLocaleString();
|
|
872
|
-
const what = document.createElement('span'); what.className = '
|
|
872
|
+
const what = document.createElement('span'); what.className = 'ps-muted ps-ellipsis'; what.textContent = `${r.author} · ${r.message}`;
|
|
873
873
|
info.append(when, what);
|
|
874
|
-
const b = document.createElement('button'); b.type = 'button'; b.className = '
|
|
874
|
+
const b = document.createElement('button'); b.type = 'button'; b.className = 'ps-btn ps-btn--small'; b.textContent = 'Restore';
|
|
875
875
|
b.addEventListener('click', async () => {
|
|
876
876
|
const res = await (await fetch(`/api/editor/page/${boot.slug}?rev=${r.sha}`)).json();
|
|
877
877
|
if (!res.page) { toast('That revision could not be loaded.', 'error'); return; }
|
|
@@ -896,7 +896,7 @@ function showShortcuts() {
|
|
|
896
896
|
['Text', [['⌘B', 'Bold'], ['⌘I', 'Italic'], ['⌘U', 'Underline'], ['⌘K', 'Link'], ['Esc', 'Cancel the edit']]],
|
|
897
897
|
];
|
|
898
898
|
const box = document.createElement('div');
|
|
899
|
-
box.className = '
|
|
899
|
+
box.className = 'ps-keys';
|
|
900
900
|
for (const [title, rows] of groups) {
|
|
901
901
|
const h = document.createElement('h3'); h.textContent = title;
|
|
902
902
|
const dl = document.createElement('dl');
|
|
@@ -908,7 +908,7 @@ function showShortcuts() {
|
|
|
908
908
|
|
|
909
909
|
/** The block before or after the selected one, in page order. */
|
|
910
910
|
function neighbour(step: 1 | -1): string | null {
|
|
911
|
-
const all = Array.from(idoc().querySelectorAll<HTMLElement>('[data-
|
|
911
|
+
const all = Array.from(idoc().querySelectorAll<HTMLElement>('[data-ps-node]')).map((e) => e.dataset.psNode!);
|
|
912
912
|
if (!selected) return all[0] ?? null;
|
|
913
913
|
return all[all.indexOf(selected) + step] ?? null;
|
|
914
914
|
}
|
|
@@ -922,7 +922,7 @@ function onKeydown(e: KeyboardEvent) {
|
|
|
922
922
|
if (mod && e.shiftKey && (key === ',' || key === '<')) return run(toggleSettings);
|
|
923
923
|
if (e.ctrlKey && e.altKey && (key === 'o' || e.code === 'KeyO')) return run(() => setLeft(leftView === 'list' ? 'off' : 'list'));
|
|
924
924
|
if (typing) return;
|
|
925
|
-
if (document.querySelector('.
|
|
925
|
+
if (document.querySelector('.ps-modal')) return;
|
|
926
926
|
if (mod && key === 'z') return run(() => (e.shiftKey ? state.redo() : state.undo()));
|
|
927
927
|
if (mod && key === 'c' && selected && !idoc().getSelection()?.toString() && !getSelection()?.toString()) return run(() => act('copy'));
|
|
928
928
|
if (mod && key === 'x' && selected) return run(() => act('cut'));
|
|
@@ -954,7 +954,7 @@ state.addEventListener('change', (e) => {
|
|
|
954
954
|
refreshChrome();
|
|
955
955
|
});
|
|
956
956
|
|
|
957
|
-
function setSaveStatus(t: string) { $('#
|
|
957
|
+
function setSaveStatus(t: string) { $('#ps-status').textContent = t; }
|
|
958
958
|
|
|
959
959
|
// Top-bar buttons get their icons here, so the Astro shell stays plain markup.
|
|
960
960
|
document.querySelectorAll<HTMLElement>('[data-icon]').forEach((el) => { el.insertAdjacentHTML('afterbegin', icons[el.dataset.icon as keyof typeof icons]); });
|