verso-cms 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +288 -2
- package/bin/verso.js +16 -0
- package/editor/door.js +139 -0
- package/editor/editor.js +1880 -0
- package/editor/elevate.css +2158 -0
- package/editor/ext-attrs.js +87 -0
- package/editor/ext-blocks.js +422 -0
- package/editor/ext-builder.js +819 -0
- package/editor/ext-channels.js +574 -0
- package/editor/ext-components.js +160 -0
- package/editor/ext-history.js +242 -0
- package/editor/ext-palette.js +307 -0
- package/editor/ext-people.js +345 -0
- package/editor/ext-repeaters.js +143 -0
- package/editor/ext-screens.js +94 -0
- package/editor/ext-site-settings.js +318 -0
- package/editor/ext-site-view.js +632 -0
- package/editor/ext-tokens.js +1166 -0
- package/editor/new-page.js +252 -0
- package/editor/rich.js +23 -0
- package/package.json +69 -4
- package/src/blocks.js +269 -0
- package/src/box.js +339 -0
- package/src/build.js +210 -0
- package/src/cli.js +161 -0
- package/src/color.js +137 -0
- package/src/components.js +223 -0
- package/src/elevate.js +74 -0
- package/src/errors.js +46 -0
- package/src/extensions/README.md +70 -0
- package/src/extensions/blocks.js +294 -0
- package/src/extensions/builder.js +734 -0
- package/src/extensions/channels.js +341 -0
- package/src/extensions/components.js +141 -0
- package/src/extensions/history.js +169 -0
- package/src/extensions/palette.js +31 -0
- package/src/extensions/people.js +113 -0
- package/src/extensions/site-settings.js +251 -0
- package/src/extensions/site-view.js +381 -0
- package/src/extensions/tags.js +15 -0
- package/src/extensions/tokens.js +587 -0
- package/src/fonts.js +139 -0
- package/src/git.js +419 -0
- package/src/html-edit.js +942 -0
- package/src/images.js +116 -0
- package/src/people.js +286 -0
- package/src/rich.js +124 -0
- package/src/scaffold.js +185 -0
- package/src/screens.js +65 -0
- package/src/server.js +693 -0
- package/src/site.js +618 -0
- package/src/template.js +915 -0
- package/src/video.js +29 -0
- package/starter/Dockerfile +20 -0
- package/starter/README.md +49 -0
- package/starter/blocks/call-to-action.html +6 -0
- package/starter/blocks/features.html +15 -0
- package/starter/blocks/hero.html +8 -0
- package/starter/blocks/image-and-text.html +8 -0
- package/starter/blocks/latest-posts.html +20 -0
- package/starter/blocks/post-index.html +30 -0
- package/starter/blocks/quote.html +9 -0
- package/starter/blocks/site-footer.html +8 -0
- package/starter/blocks/site-nav.html +8 -0
- package/starter/blocks/tag-list.html +10 -0
- package/starter/blocks/tagged-posts.html +19 -0
- package/starter/blocks/team.html +15 -0
- package/starter/blocks/text.html +4 -0
- package/starter/content/404/page.json +21 -0
- package/starter/content/about/page.json +65 -0
- package/starter/content/journal/designing-for-the-morning-shift/body.html +22 -0
- package/starter/content/journal/designing-for-the-morning-shift/checklist.jpg +0 -0
- package/starter/content/journal/designing-for-the-morning-shift/cover.jpg +0 -0
- package/starter/content/journal/designing-for-the-morning-shift/page.json +17 -0
- package/starter/content/journal/page.json +19 -0
- package/starter/content/journal/the-first-week/body.html +17 -0
- package/starter/content/journal/the-first-week/cover.jpg +0 -0
- package/starter/content/journal/the-first-week/page.json +17 -0
- package/starter/content/journal/the-first-week/wall-of-cards.jpg +0 -0
- package/starter/content/page.json +94 -0
- package/starter/content/site.json +88 -0
- package/starter/content/studio-table.jpg +0 -0
- package/starter/content/topics/page.json +27 -0
- package/starter/dockerignore +5 -0
- package/starter/elevate/config/design.ts +30 -0
- package/starter/elevate/config/elevate.ts +16 -0
- package/starter/elevate/config/syntax.ts +6 -0
- package/starter/elevate/design/site.ts +9 -0
- package/starter/elevate/design/tokens.json +20 -0
- package/starter/elevate/extend/fonts.css +1 -0
- package/starter/gitignore +6 -0
- package/starter/netlify.toml +5 -0
- package/starter/parts/.gitkeep +0 -0
- package/starter/public/favicon.svg +1 -0
- package/starter/templates/page.html +20 -0
- package/starter/templates/page.layout.json +23 -0
- package/starter/templates/post.html +65 -0
- package/starter/verso.defaults.json +33 -0
- package/starter-blank/blocks/site-footer.html +3 -0
- package/starter-blank/blocks/site-nav.html +8 -0
- package/starter-blank/blocks/text.html +4 -0
- package/starter-blank/content/404/page.json +18 -0
- package/starter-blank/content/page.json +18 -0
- package/starter-blank/content/site.json +36 -0
- package/starter-blank/elevate/design/tokens.json +10 -0
- package/starter-blank/templates/page.html +17 -0
- package/starter-blank/verso.defaults.json +26 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Components in the editor: naming a block as a component, locking fields so
|
|
2
|
+
// they read the same on every page, and marking locked fields on the page.
|
|
3
|
+
// The block handle (ext-blocks.js) opens these through verso.components.
|
|
4
|
+
|
|
5
|
+
const SVG = {
|
|
6
|
+
lock: '<svg width="12" height="12" viewBox="0 0 24 24" aria-hidden="true"><rect x="5" y="11" width="14" height="10" rx="2" fill="currentColor"/><path d="M8 11V8a4 4 0 0 1 8 0v3" fill="none" stroke="currentColor" stroke-width="2.4"/></svg>',
|
|
7
|
+
close: '<svg width="18" height="18" viewBox="0 0 24 24" aria-hidden="true"><path d="m7 7 10 10M17 7 7 17" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function init(verso) {
|
|
11
|
+
const { root, data, ui, esc, toast } = verso;
|
|
12
|
+
const fields = (data.fields || []).filter((f) => f.component);
|
|
13
|
+
|
|
14
|
+
const layer = document.createElement('div');
|
|
15
|
+
layer.className = ui("font:ui:small text:ink");
|
|
16
|
+
layer.innerHTML = `
|
|
17
|
+
<div id="cmp-marks" class="${ui("fixed top:0 left:0 w:full h:full events:none z:2")}"></div>
|
|
18
|
+
<div id="cmp-dialog" class="${ui("fixed top:0 left:0 w:full h:full row:x-center:y-center z:5")}" style="display:none;background:rgba(21,21,24,.45)"></div>`;
|
|
19
|
+
root.appendChild(layer);
|
|
20
|
+
const marks = root.getElementById('cmp-marks');
|
|
21
|
+
const dialog = root.getElementById('cmp-dialog');
|
|
22
|
+
|
|
23
|
+
const post = async (action, body, note) => {
|
|
24
|
+
await verso.flush();
|
|
25
|
+
const res = await fetch(`/__verso/components/${action}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: data.url, ...body }) });
|
|
26
|
+
const out = await res.json().catch(() => ({}));
|
|
27
|
+
if (!out.ok) {
|
|
28
|
+
toast(out.message || 'That didn\'t work. Reload the page and try again.', 'error');
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (note) {
|
|
32
|
+
try { sessionStorage.setItem('verso-components-note', note(out)); } catch {}
|
|
33
|
+
}
|
|
34
|
+
verso.reload();
|
|
35
|
+
return out;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// A note saved before a reload is shown once the page is back.
|
|
39
|
+
try {
|
|
40
|
+
const note = sessionStorage.getItem('verso-components-note');
|
|
41
|
+
if (note) {
|
|
42
|
+
sessionStorage.removeItem('verso-components-note');
|
|
43
|
+
setTimeout(() => toast(note, 'ok'), 300);
|
|
44
|
+
}
|
|
45
|
+
} catch {}
|
|
46
|
+
|
|
47
|
+
const close = () => { dialog.style.display = 'none'; dialog.innerHTML = ''; };
|
|
48
|
+
dialog.addEventListener('mousedown', (e) => { if (e.target === dialog) close(); });
|
|
49
|
+
dialog.addEventListener('keydown', (e) => { if (e.key === 'Escape') close(); });
|
|
50
|
+
|
|
51
|
+
const card = (title, sub, body) => `
|
|
52
|
+
<div role="dialog" aria-modal="true" aria-label="${esc(title)}" class="${ui("col gap:d4 w:card pd:d6 bg-color:paper bd:round-d5 shadow:float")}">
|
|
53
|
+
<header class="${ui("row:y-start:x-between gap:d3")}">
|
|
54
|
+
<div class="${ui("col gap:d1")}">
|
|
55
|
+
<h2 class="${ui("font:h6:semibold text:ink")}">${esc(title)}</h2>
|
|
56
|
+
<p class="${ui("font:small text:mist")}">${sub}</p>
|
|
57
|
+
</div>
|
|
58
|
+
<button data-close class="${ui("bg-color:transparent bd:width-0 cursor:pointer row:x-center:y-center w:d9 h:d9 bd:round-full text:ink @hover:[bg-color:paper-2] transition:fast")}" aria-label="Close">${SVG.close}</button>
|
|
59
|
+
</header>
|
|
60
|
+
${body}
|
|
61
|
+
</div>`;
|
|
62
|
+
|
|
63
|
+
// ════ Save as component ════
|
|
64
|
+
function saveAs(blockId, blockLabel) {
|
|
65
|
+
dialog.innerHTML = card('Save as a component', `The ${esc(blockLabel)} block and everything inside it become a component you can use on any page. Its layout is shared; each page keeps its own words.`, `
|
|
66
|
+
<label class="${ui("col gap:d2")}">
|
|
67
|
+
<span class="${ui("font:small:medium text:ink")}">Name</span>
|
|
68
|
+
<input data-name maxlength="80" placeholder="Name it, like Studio intro" class="${ui("w:full h:d11 pd-x:d3 bd:round-d3 bd:width-px:solid:line bg-color:paper font:ui:small text:ink @focus:[bd:proof_shadow:proof-focus_outline:none] transition:fast")}">
|
|
69
|
+
</label>
|
|
70
|
+
<div class="${ui("row:y-center:x-end gap:d2")}">
|
|
71
|
+
<button data-close class="${ui("bg-color:transparent bd:width-0 cursor:pointer h:d10 pd-x:d4 bd:round-pill font:small:semibold text:ink @hover:[bg-color:paper-2] transition:fast")}">Cancel</button>
|
|
72
|
+
<button data-save class="${ui("bd:width-0 cursor:pointer h:d10 pd-x:d5 bd:round-pill bg-color:proof text:paper font:small:semibold @hover:[bg-color:proof-2] transition:fast")}">Save component</button>
|
|
73
|
+
</div>`);
|
|
74
|
+
dialog.style.display = 'flex';
|
|
75
|
+
const input = dialog.querySelector('[data-name]');
|
|
76
|
+
input.value = `${blockLabel} (component)`;
|
|
77
|
+
input.select();
|
|
78
|
+
input.focus();
|
|
79
|
+
const go = () => {
|
|
80
|
+
const label = input.value.trim();
|
|
81
|
+
if (!label) return input.focus();
|
|
82
|
+
close();
|
|
83
|
+
post('save', { id: blockId, label }, (out) => `Saved "${out.label}" as a component. Find it in the Blocks library to use it on other pages.`);
|
|
84
|
+
};
|
|
85
|
+
dialog.querySelector('[data-save]').addEventListener('click', go);
|
|
86
|
+
input.addEventListener('keydown', (e) => { if (e.key === 'Enter') go(); });
|
|
87
|
+
dialog.querySelectorAll('[data-close]').forEach((b) => b.addEventListener('click', close));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// What a field says now, so two "Title" fields can be told apart.
|
|
91
|
+
const preview = (f) => {
|
|
92
|
+
const el = document.querySelector(`[data-verso-field="${CSS.escape(f.name)}"], [data-verso-rich="${CSS.escape(f.name)}"]`);
|
|
93
|
+
const text = (el && !el.hasAttribute('data-verso-empty') ? el.textContent : '').replace(/\s+/g, ' ').trim();
|
|
94
|
+
return text ? ` · “${esc(text.length > 28 ? `${text.slice(0, 27)}…` : text)}”` : ' · empty';
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// ════ Lock fields ════
|
|
98
|
+
function lockPanel(instanceId) {
|
|
99
|
+
const own = fields.filter((f) => f.instance === instanceId);
|
|
100
|
+
if (!own.length) return toast('This component has no fields to lock.', 'error');
|
|
101
|
+
const label = own[0].componentLabel;
|
|
102
|
+
dialog.innerHTML = card(`Lock fields in ${label}`, 'A locked field reads the same on every page that uses this component. Unlocked fields are written page by page.', `
|
|
103
|
+
<ul class="${ui("col gap:d1 max-h:half-screen overflow-y:auto list-style-type:none")}">
|
|
104
|
+
${own.map((f) => `
|
|
105
|
+
<li>
|
|
106
|
+
<label class="${ui("row:y-center:x-between gap:d3 pd:d3 bd:round-d3 cursor:pointer @hover:[bg-color:paper-2] transition:fast")}">
|
|
107
|
+
<span class="${ui("col gap:d0")}"><span class="${ui("font:small:medium text:ink")}">${esc(f.label)}</span><span class="${ui("font:tiny text:mist")}">${esc(f.block)} block${f.type === 'image' ? ' · images stay page by page' : preview(f)}</span></span>
|
|
108
|
+
<input type="checkbox" data-field="${esc(f.name)}" ${f.locked ? 'checked' : ''} ${f.type === 'image' ? 'disabled' : ''} class="${ui("w:d5 h:d5 cursor:pointer")}">
|
|
109
|
+
</label>
|
|
110
|
+
</li>`).join('')}
|
|
111
|
+
</ul>
|
|
112
|
+
<div class="${ui("row:y-center:x-end gap:d2")}">
|
|
113
|
+
<button data-close class="${ui("bd:width-0 cursor:pointer h:d10 pd-x:d5 bd:round-pill bg-color:ink text:paper font:small:semibold @hover:[bg-color:ink-2] transition:fast")}">Done</button>
|
|
114
|
+
</div>`);
|
|
115
|
+
dialog.style.display = 'flex';
|
|
116
|
+
dialog.querySelectorAll('[data-close]').forEach((b) => b.addEventListener('click', close));
|
|
117
|
+
dialog.querySelectorAll('[data-field]').forEach((box) => box.addEventListener('change', async () => {
|
|
118
|
+
const f = own.find((x) => x.name === box.dataset.field);
|
|
119
|
+
box.disabled = true;
|
|
120
|
+
const res = await fetch('/__verso/components/lock', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: data.url, field: f.name, locked: box.checked }) });
|
|
121
|
+
const out = await res.json().catch(() => ({}));
|
|
122
|
+
box.disabled = false;
|
|
123
|
+
if (!out.ok) {
|
|
124
|
+
box.checked = !box.checked;
|
|
125
|
+
return toast(out.message || 'That field couldn\'t be changed.', 'error');
|
|
126
|
+
}
|
|
127
|
+
f.locked = box.checked;
|
|
128
|
+
toast(box.checked ? `"${out.field}" now reads the same on all ${out.pages} page${out.pages === 1 ? '' : 's'} that use ${out.label}.` : `"${out.field}" can now be written page by page.`, 'ok');
|
|
129
|
+
draw();
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ════ Locked fields on the page ════
|
|
134
|
+
const lockedEls = () => [...document.querySelectorAll('[data-verso-locked]')];
|
|
135
|
+
function draw() {
|
|
136
|
+
marks.innerHTML = lockedEls().map((el) => {
|
|
137
|
+
const r = el.getBoundingClientRect();
|
|
138
|
+
if (r.bottom < 0 || r.top > innerHeight || !r.width) return '';
|
|
139
|
+
return `<span class="${ui("absolute row:y-center gap:d0 pd:d0:d1 bd:round-d1 bg-color:ink text:paper font:tiny:semibold")}" style="left:${r.left - 4}px;top:${r.top - 4}px;transform:translate(-100%,0)" title="Locked: the same on every page">${SVG.lock}</span>`;
|
|
140
|
+
}).join('');
|
|
141
|
+
}
|
|
142
|
+
if (lockedEls().length) {
|
|
143
|
+
addEventListener('scroll', draw, { passive: true, capture: true });
|
|
144
|
+
addEventListener('resize', draw);
|
|
145
|
+
addEventListener('load', draw);
|
|
146
|
+
setTimeout(draw, 300);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Editing a locked field changes every page: say so the first time.
|
|
150
|
+
let warned = false;
|
|
151
|
+
document.addEventListener('focusin', (e) => {
|
|
152
|
+
const el = e.target.closest?.('[data-verso-locked]');
|
|
153
|
+
if (!el || warned) return;
|
|
154
|
+
warned = true;
|
|
155
|
+
const f = fields.find((x) => x.name === (el.dataset.versoField || el.dataset.versoRich));
|
|
156
|
+
toast(`This is locked in ${f ? f.componentLabel : 'its component'}. Changes here change every page that uses it.`, 'ok');
|
|
157
|
+
}, true);
|
|
158
|
+
|
|
159
|
+
verso.components = { saveAs, lockPanel, detach: (id) => post('detach', { id }, (out) => `Detached ${out.label}. It's plain blocks on this page now, and won't change with the component.`) };
|
|
160
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// History and clashes, in the editor.
|
|
2
|
+
//
|
|
3
|
+
// History: a clock button in the bar (and "History" in the page menu) opens a
|
|
4
|
+
// timeline of publishes. Each can be previewed, and the page restored to it.
|
|
5
|
+
//
|
|
6
|
+
// Clashes: Publish answers with { conflict } when someone else published the
|
|
7
|
+
// same page first. This file watches the editor's own requests to
|
|
8
|
+
// /__verso/publish (it wraps window.fetch and reads a copy of the answer), so
|
|
9
|
+
// the editor needs no changes: when a clash comes back, or one is still
|
|
10
|
+
// waiting when a page opens, both versions are shown side by side.
|
|
11
|
+
|
|
12
|
+
const CLOCK = '<svg width="18" height="18" viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="8.5" fill="none" stroke="currentColor" stroke-width="2"/><path d="M12 7.5V12l3 2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
|
|
13
|
+
|
|
14
|
+
export function init(verso) {
|
|
15
|
+
const { root, data, ui, toast, icon, esc, show, hide } = verso;
|
|
16
|
+
|
|
17
|
+
const layer = document.createElement('div');
|
|
18
|
+
layer.className = ui("font:ui:small text:ink");
|
|
19
|
+
root.appendChild(layer);
|
|
20
|
+
layer.innerHTML = `
|
|
21
|
+
<aside id="history" class="${ui("fixed top:d3 right:d3 bottom:d3 w:sheet col bg-color:paper bd:round-d5 shadow:float bd:width-px:solid:line z:4 overflow-y:auto")}" style="display:none" aria-label="History"></aside>
|
|
22
|
+
<div id="past" class="${ui("fixed top:0 left:0 w:full h:full z:6")}" style="display:none"></div>
|
|
23
|
+
<div id="clash-note" class="${ui("fixed top:d4 left:half translate:(-50%_0) w:toast row:y-center gap:d3 pd:d3:d4 bg-color:ink text:paper bd:round-d4 shadow:float z:5")}" style="display:none"></div>`;
|
|
24
|
+
const $ = (id) => root.getElementById(id);
|
|
25
|
+
|
|
26
|
+
const ago = (iso) => {
|
|
27
|
+
const seconds = (new Date(iso).getTime() - Date.now()) / 1000;
|
|
28
|
+
const units = [['year', 31536000], ['month', 2592000], ['week', 604800], ['day', 86400], ['hour', 3600], ['minute', 60]];
|
|
29
|
+
const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' });
|
|
30
|
+
for (const [unit, size] of units) if (Math.abs(seconds) >= size) return rtf.format(Math.round(seconds / size), unit);
|
|
31
|
+
return 'just now';
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// ════ Timeline ════
|
|
35
|
+
|
|
36
|
+
let scope = 'page';
|
|
37
|
+
|
|
38
|
+
async function openHistory() {
|
|
39
|
+
const panel = $('history');
|
|
40
|
+
root.getElementById('settings')?.style && (root.getElementById('settings').style.display = 'none');
|
|
41
|
+
show(panel, 'flex');
|
|
42
|
+
panel.innerHTML = `
|
|
43
|
+
<header class="${ui("col gap:d4 pd:d5:d5:d3 sticky top:0 bg-color:paper z:1")}">
|
|
44
|
+
<div class="${ui("row:y-center:x-between")}">
|
|
45
|
+
<div class="${ui("col gap:d0")}">
|
|
46
|
+
<h2 class="${ui("font:h6:semibold text:ink")}">History</h2>
|
|
47
|
+
<p class="${ui("font:tiny text:mist")}">Every publish is a restore point.</p>
|
|
48
|
+
</div>
|
|
49
|
+
<button data-close class="${ui("bg-color:transparent bd:width-0 cursor:pointer row:x-center:y-center w:d9 h:d9 bd:round-full text:ink @hover:[bg-color:paper-2] @focus-visible:[outline:d0:solid:proof] transition:fast")}" aria-label="Close history">${icon('close')}</button>
|
|
50
|
+
</div>
|
|
51
|
+
<div class="${ui("row gap:d0 pd:d0 bg-color:paper-2 bd:round-pill")}" role="tablist">
|
|
52
|
+
${['page', 'site'].map((s) => `<button data-scope="${s}" role="tab" aria-selected="${scope === s}" class="${scope === s ? ui("bd:width-0 cursor:pointer child:grow h:d9 bd:round-pill bg-color:paper text:ink font:small:semibold shadow:float") : ui("bg-color:transparent bd:width-0 cursor:pointer child:grow h:d9 bd:round-pill text:ink-3 font:small:medium @hover:[text:ink] transition:fast")}">${s === 'page' ? 'This page' : 'Whole site'}</button>`).join('')}
|
|
53
|
+
</div>
|
|
54
|
+
</header>
|
|
55
|
+
<div id="history-list" class="${ui("pd-x:d5 pd-t:d4 pd-b:c3")}"><p class="${ui("pd-y:d6 text:mist")}">Loading…</p></div>`;
|
|
56
|
+
panel.querySelector('[data-close]').addEventListener('click', () => hide(panel));
|
|
57
|
+
panel.querySelectorAll('[data-scope]').forEach((b) => b.addEventListener('click', () => { scope = b.dataset.scope; openHistory(); }));
|
|
58
|
+
|
|
59
|
+
let entries = [];
|
|
60
|
+
try {
|
|
61
|
+
const q = scope === 'page' ? `?url=${encodeURIComponent(data.url)}` : '';
|
|
62
|
+
entries = (await (await fetch(`/__verso/history${q}`)).json()).entries || [];
|
|
63
|
+
} catch {
|
|
64
|
+
$('history-list').innerHTML = `<p class="${ui("pd-y:d6 text:proof-2")}">Verso couldn't read the history. Is it still running?</p>`;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (!entries.length) {
|
|
68
|
+
$('history-list').innerHTML = `<p class="${ui("pd-y:d6 text:mist")}">${scope === 'page' ? 'This page hasn\'t been published yet.' : 'Nothing has been published yet.'}</p>`;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
$('history-list').innerHTML = `<ol class="${ui("col")}">${entries.map((e, i) => `
|
|
72
|
+
<li class="${ui("row gap:d4")}">
|
|
73
|
+
<span class="${ui("col:x-center w:d4 child:no-shrink")}">
|
|
74
|
+
<span class="${i === 0 ? ui("w:d3 h:d3 mg-t:d1 bd:round-full bg-color:proof child:no-shrink") : ui("w:d3 h:d3 mg-t:d1 bd:round-full bd:width-d0:solid:line bg-color:paper child:no-shrink")}"></span>
|
|
75
|
+
${i < entries.length - 1 ? `<span class="${ui("w:px child:grow bg-color:line")}"></span>` : ''}
|
|
76
|
+
</span>
|
|
77
|
+
<div class="${ui("col gap:d2 pd-b:d6 min-w:0 child:grow")}">
|
|
78
|
+
<p class="${ui("font:small:medium text:ink:snug")}">${esc(e.message)}</p>
|
|
79
|
+
<p class="${ui("font:tiny text:mist")}" title="${esc(e.when)}">${esc(e.author)}, ${esc(ago(e.date))}${e.published === false ? `. <span class="${ui("text:proof-2 font:semibold")}">Not published yet</span>` : ''}</p>
|
|
80
|
+
${scope === 'site' && e.pages.length ? `<p class="${ui("font:tiny text:ink-3")}">${e.pages.map((pg) => `<a href="${esc(pg.url)}" class="${ui("text:ink-3:underline @hover:[text:proof-2]")}">${esc(pg.label)}</a>`).join(', ')}</p>` : ''}
|
|
81
|
+
${scope === 'page' ? `<div class="${ui("row gap:d2 pd-t:d1")}">
|
|
82
|
+
<button data-preview="${esc(e.sha)}" class="${ui("bg-color:transparent cursor:pointer h:d8 pd-x:d3 bd:round-pill bd:width-px:solid:line text:ink font:tiny:semibold @hover:[bg-color:paper-2] @focus-visible:[outline:d0:solid:proof] transition:fast")}">Preview</button>
|
|
83
|
+
<button data-restore="${esc(e.sha)}" class="${ui("bd:width-0 cursor:pointer h:d8 pd-x:d3 bd:round-pill bg-color:ink text:paper font:tiny:semibold @hover:[bg-color:ink-2] @focus-visible:[outline:d0:solid:proof] transition:fast")}">Restore</button>
|
|
84
|
+
</div>` : ''}
|
|
85
|
+
</div>
|
|
86
|
+
</li>`).join('')}</ol>`;
|
|
87
|
+
const find = (sha) => entries.find((e) => e.sha === sha);
|
|
88
|
+
$('history-list').querySelectorAll('[data-preview]').forEach((b) => b.addEventListener('click', () => preview(find(b.dataset.preview))));
|
|
89
|
+
$('history-list').querySelectorAll('[data-restore]').forEach((b) => b.addEventListener('click', () => restore(find(b.dataset.restore))));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ════ A page as it was ════
|
|
93
|
+
|
|
94
|
+
const frame = (src, label) => `<iframe src="${esc(src)}" title="${esc(label)}" class="${ui("w:full h:full bd:width-0 bg-color:paper")}" loading="lazy"></iframe>`;
|
|
95
|
+
|
|
96
|
+
function overlay(inner) {
|
|
97
|
+
const el = $('past');
|
|
98
|
+
el.innerHTML = `
|
|
99
|
+
<div data-backdrop class="${ui("absolute top:0 left:0 w:full h:full bg-color:ink opacity:0.55")}"></div>
|
|
100
|
+
<section role="dialog" aria-modal="true" class="${ui("absolute top:d4 left:d4 right:d4 bottom:d4 col bg-color:paper bd:round-d5 shadow:float overflow:hidden /md/ top:d6 left:d6 right:d6 bottom:d6")}">${inner}</section>`;
|
|
101
|
+
show(el);
|
|
102
|
+
el.querySelector('[data-backdrop]').addEventListener('click', () => hide(el));
|
|
103
|
+
el.querySelectorAll('[data-close]').forEach((b) => b.addEventListener('click', () => hide(el)));
|
|
104
|
+
return el;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function preview(entry) {
|
|
108
|
+
const src = `/__verso/history/preview?url=${encodeURIComponent(data.url)}&sha=${entry.sha}`;
|
|
109
|
+
const el = overlay(`
|
|
110
|
+
<header class="${ui("row:y-center:x-between gap:d4 pd:d3:d4 bd-b:width-px:solid:line")}">
|
|
111
|
+
<div class="${ui("col gap:d0 min-w:0")}">
|
|
112
|
+
<p class="${ui("font:small:semibold text:ink overflow:hidden text-overflow:ellipsis format:no-wrap")}">${esc(data.label)}, as it was ${esc(ago(entry.date))}</p>
|
|
113
|
+
<p class="${ui("font:tiny text:mist overflow:hidden text-overflow:ellipsis format:no-wrap")}">${esc(entry.message)}. ${esc(entry.author)}, ${esc(entry.when)}</p>
|
|
114
|
+
</div>
|
|
115
|
+
<div class="${ui("row:y-center gap:d2 child:no-shrink")}">
|
|
116
|
+
<button data-restore class="${ui("bd:width-0 cursor:pointer h:d9 pd-x:d4 bd:round-pill bg-color:proof text:paper font:small:semibold @hover:[bg-color:proof-2] @focus-visible:[outline:d0:solid:ink] transition:fast")}">Restore this version</button>
|
|
117
|
+
<button data-close class="${ui("bg-color:transparent bd:width-0 cursor:pointer row:x-center:y-center w:d9 h:d9 bd:round-full text:ink @hover:[bg-color:paper-2] transition:fast")}" aria-label="Close">${icon('close')}</button>
|
|
118
|
+
</div>
|
|
119
|
+
</header>
|
|
120
|
+
<div class="${ui("child:grow relative bg-color:paper-2")}">${frame(src, 'Earlier version')}</div>`);
|
|
121
|
+
el.querySelector('[data-restore]').addEventListener('click', () => restore(entry));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function restore(entry) {
|
|
125
|
+
await verso.flush();
|
|
126
|
+
try {
|
|
127
|
+
const res = await fetch('/__verso/history/restore', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: data.url, sha: entry.sha }) });
|
|
128
|
+
const out = await res.json();
|
|
129
|
+
if (!out.ok) return toast(out.message || 'That version couldn\'t be restored.', 'error');
|
|
130
|
+
try { sessionStorage.setItem('verso-restored', JSON.stringify({ message: `${data.label} restored to ${entry.when}. Publish to make it live.`, undo: out.undo })); } catch {}
|
|
131
|
+
location.reload();
|
|
132
|
+
} catch {
|
|
133
|
+
toast('Verso isn\'t running, so nothing was restored.', 'error');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
const note = sessionStorage.getItem('verso-restored');
|
|
139
|
+
if (note) {
|
|
140
|
+
sessionStorage.removeItem('verso-restored');
|
|
141
|
+
let saved = { message: note };
|
|
142
|
+
try { saved = JSON.parse(note); } catch { /* a plain message */ }
|
|
143
|
+
const undo = saved.undo && {
|
|
144
|
+
label: 'Undo',
|
|
145
|
+
run: async () => {
|
|
146
|
+
try {
|
|
147
|
+
const res = await fetch('/__verso/history/undo-restore', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token: saved.undo }) });
|
|
148
|
+
const out = await res.json();
|
|
149
|
+
if (!out.ok) return toast(out.message, 'error');
|
|
150
|
+
try { sessionStorage.setItem('verso-restored', JSON.stringify({ message: `${data.label} is back as it was before the restore.` })); } catch {}
|
|
151
|
+
location.reload();
|
|
152
|
+
} catch {
|
|
153
|
+
toast('Verso isn\'t running, so the restore wasn\'t undone.', 'error');
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
setTimeout(() => toast(saved.message, 'ok', undo || null), 600);
|
|
158
|
+
}
|
|
159
|
+
} catch {}
|
|
160
|
+
|
|
161
|
+
// ════ Clashes ════
|
|
162
|
+
|
|
163
|
+
async function openClash() {
|
|
164
|
+
let conflict = null;
|
|
165
|
+
try {
|
|
166
|
+
conflict = (await (await fetch('/__verso/conflict')).json()).conflict;
|
|
167
|
+
} catch {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (!conflict) { hide($('clash-note')); return; }
|
|
171
|
+
hide($('clash-note'));
|
|
172
|
+
const page = conflict.pages.find((p) => !p.choice) || conflict.pages[0];
|
|
173
|
+
const src = (sha) => `/__verso/history/preview?url=${encodeURIComponent(page.url)}&sha=${sha}`;
|
|
174
|
+
const side = (who, sha, title, detail, primary) => `
|
|
175
|
+
<div class="${ui("col min-h:0 bd-b:width-px:solid:line /md/ bd-b:width-0")}">
|
|
176
|
+
<div class="${ui("row:y-center:x-between gap:d3 pd:d3:d4")}">
|
|
177
|
+
<div class="${ui("col gap:d0 min-w:0")}">
|
|
178
|
+
<p class="${ui("font:small:semibold text:ink")}">${esc(title)}</p>
|
|
179
|
+
<p class="${ui("font:tiny text:mist overflow:hidden text-overflow:ellipsis format:no-wrap")}">${esc(detail)}</p>
|
|
180
|
+
</div>
|
|
181
|
+
<button data-keep="${who}" class="${primary ? ui("bd:width-0 cursor:pointer h:d9 pd-x:d4 bd:round-pill bg-color:proof text:paper font:small:semibold child:no-shrink @hover:[bg-color:proof-2] @focus-visible:[outline:d0:solid:ink] transition:fast") : ui("bd:width-0 cursor:pointer h:d9 pd-x:d4 bd:round-pill bg-color:ink text:paper font:small:semibold child:no-shrink @hover:[bg-color:ink-2] @focus-visible:[outline:d0:solid:proof] transition:fast")}">Keep this one</button>
|
|
182
|
+
</div>
|
|
183
|
+
<div class="${ui("child:grow relative bg-color:paper-2 mg:d0:d3:d3 bd:round-d3 overflow:hidden bd:width-px:solid:line")}">${frame(src(sha), title)}</div>
|
|
184
|
+
</div>`;
|
|
185
|
+
const el = overlay(`
|
|
186
|
+
<header class="${ui("row:y-center:x-between gap:d4 pd:d4 bd-b:width-px:solid:line")}">
|
|
187
|
+
<div class="${ui("col gap:d1 min-w:0")}">
|
|
188
|
+
<p class="${ui("row:y-center gap:d2 font:small:semibold text:proof-2")}">${icon('alert')}<span>Two versions of ${esc(page.label)}</span></p>
|
|
189
|
+
<p class="${ui("font:small:normal text:ink-3")}">${esc(conflict.theirs.author)} published changes to this page while you were editing. Choose the version to keep. The other one stays in the history.</p>
|
|
190
|
+
</div>
|
|
191
|
+
<button data-close class="${ui("bg-color:transparent bd:width-0 cursor:pointer row:x-center:y-center w:d9 h:d9 bd:round-full text:ink child:no-shrink @hover:[bg-color:paper-2] transition:fast")}" aria-label="Decide later">${icon('close')}</button>
|
|
192
|
+
</header>
|
|
193
|
+
<div class="${ui("child:grow grid:cols-1-fr:rows-2-fr min-h:0 /md/ grid:cols-2-fr:rows-1-fr")}">
|
|
194
|
+
${side('mine', conflict.mine.sha, 'Your version', 'Saved on this computer, not published', true)}
|
|
195
|
+
${side('theirs', conflict.theirs.sha, `Their version`, `Published by ${conflict.theirs.author}, ${ago(conflict.theirs.date)}`, false)}
|
|
196
|
+
</div>`);
|
|
197
|
+
el.querySelectorAll('[data-keep]').forEach((b) => b.addEventListener('click', async () => {
|
|
198
|
+
el.querySelectorAll('[data-keep]').forEach((x) => { x.disabled = true; });
|
|
199
|
+
b.textContent = 'Publishing…';
|
|
200
|
+
try {
|
|
201
|
+
const res = await fetch('/__verso/conflict/resolve', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: page.url, keep: b.dataset.keep }) });
|
|
202
|
+
const out = await res.json();
|
|
203
|
+
if (out.ok && out.pending?.length) { toast(out.message, 'ok'); openClash(); return; }
|
|
204
|
+
hide(el);
|
|
205
|
+
verso.refreshStatus();
|
|
206
|
+
if (!out.ok) return toast(out.message, 'error');
|
|
207
|
+
try { sessionStorage.setItem('verso-restored', JSON.stringify({ message: out.message })); } catch {}
|
|
208
|
+
location.reload();
|
|
209
|
+
} catch {
|
|
210
|
+
hide(el);
|
|
211
|
+
toast('Verso isn\'t running, so nothing was published.', 'error');
|
|
212
|
+
}
|
|
213
|
+
}));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function clashNote() {
|
|
217
|
+
const note = $('clash-note');
|
|
218
|
+
note.innerHTML = `<span class="${ui("text:proof child:no-shrink")}">${icon('alert')}</span><p class="${ui("child:grow font:small:normal text:paper")}">Your last publish clashed with someone else's changes.</p><button data-open class="${ui("bd:width-0 cursor:pointer h:d8 pd-x:d3 bd:round-pill bg-color:proof text:paper font:tiny:semibold child:no-shrink @hover:[bg-color:proof-2] transition:fast")}">Compare</button>`;
|
|
219
|
+
note.querySelector('[data-open]').addEventListener('click', openClash);
|
|
220
|
+
show(note, 'flex');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Watch the editor's publish requests for a clash.
|
|
224
|
+
const fetch0 = window.fetch.bind(window);
|
|
225
|
+
window.fetch = async (input, init) => {
|
|
226
|
+
const res = await fetch0(input, init);
|
|
227
|
+
const target = typeof input === 'string' ? input : input?.url || '';
|
|
228
|
+
if (/\/__verso\/publish$/.test(target)) {
|
|
229
|
+
res.clone().json().then((out) => { if (out?.conflict) openClash(); }).catch(() => {});
|
|
230
|
+
}
|
|
231
|
+
return res;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
if (data.conflictPending) clashNote();
|
|
235
|
+
|
|
236
|
+
verso.addBarButton({ id: 'open-history', title: 'History', svg: CLOCK, onClick: () => (isShown($('history')) ? hide($('history')) : openHistory()) });
|
|
237
|
+
verso.menuItems.push({ label: 'History', icon: CLOCK, run: openHistory });
|
|
238
|
+
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') { hide($('past')); } });
|
|
239
|
+
root.addEventListener('keydown', (e) => { if (e.key === 'Escape') { hide($('past')); hide($('history')); } });
|
|
240
|
+
|
|
241
|
+
function isShown(el) { return el.style.display !== 'none'; }
|
|
242
|
+
}
|