@bright-logic-digital/site-editor 0.0.0-stage → 1.0.0-rc.0
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/CHANGELOG.md +30 -0
- package/LICENSE +4 -0
- package/README.md +318 -2
- package/bin/cli.js +92 -0
- package/bin/copy-editor.js +211 -0
- package/bin/node-version.js +20 -0
- package/bin/site-config.js +46 -0
- package/bin/site-editor.js +16 -0
- package/editor/admin.css +438 -0
- package/editor/api.js +138 -0
- package/editor/app.js +1178 -0
- package/editor/asset-map.js +38 -0
- package/editor/boot.js +72 -0
- package/editor/copy.js +239 -0
- package/editor/drafts.js +67 -0
- package/editor/format.js +80 -0
- package/editor/forms.js +918 -0
- package/editor/images.js +212 -0
- package/editor/index.html +27 -0
- package/editor/model.js +152 -0
- package/editor/pages.js +297 -0
- package/editor/photos.js +232 -0
- package/editor/publish.js +45 -0
- package/editor/shared/rules.js +118 -0
- package/editor/shared/site.js +128 -0
- package/editor/shared/validate.js +303 -0
- package/editor/signin.js +154 -0
- package/editor/status.js +90 -0
- package/editor/uploads.js +91 -0
- package/guide/check.js +233 -0
- package/hosts/next.js +19 -0
- package/hosts/pages.js +20 -0
- package/hosts/shared.js +42 -0
- package/package.json +91 -4
- package/server/blob-sha.js +10 -0
- package/server/changes.js +24 -0
- package/server/check-content.js +197 -0
- package/server/config.js +97 -0
- package/server/dispatch.js +207 -0
- package/server/github.js +149 -0
- package/server/handlers.js +364 -0
- package/server/index.js +153 -0
- package/server/local-repo.js +171 -0
- package/server/mapped-repo.js +48 -0
- package/server/memory-repo.js +104 -0
- package/server/path-map.js +26 -0
- package/server/signin/access.js +329 -0
- package/server/signin/code-store.sql +60 -0
- package/server/signin/code.js +192 -0
- package/server/signin/codes.js +171 -0
- package/server/signin/refusal.js +76 -0
- package/server/signin/session.js +90 -0
- package/tests/fixtures/image-files.json +14 -0
- package/tests/fixtures/slugs.json +202 -0
- package/tests/fixtures/validation.json +12734 -0
- package/types/bin/cli.d.ts +1 -0
- package/types/bin/copy-editor.d.ts +32 -0
- package/types/bin/node-version.d.ts +8 -0
- package/types/bin/site-config.d.ts +17 -0
- package/types/bin/site-editor.d.ts +2 -0
- package/types/editor/api.d.ts +37 -0
- package/types/editor/copy.d.ts +153 -0
- package/types/editor/shared/rules.d.ts +33 -0
- package/types/editor/shared/site.d.ts +35 -0
- package/types/editor/shared/validate.d.ts +19 -0
- package/types/guide/check.d.ts +31 -0
- package/types/hosts/next.d.ts +12 -0
- package/types/hosts/pages.d.ts +9 -0
- package/types/hosts/shared.d.ts +21 -0
- package/types/server/blob-sha.d.ts +1 -0
- package/types/server/changes.d.ts +2 -0
- package/types/server/check-content.d.ts +16 -0
- package/types/server/config.d.ts +46 -0
- package/types/server/dispatch.d.ts +51 -0
- package/types/server/github.d.ts +27 -0
- package/types/server/handlers.d.ts +116 -0
- package/types/server/index.d.ts +75 -0
- package/types/server/local-repo.d.ts +16 -0
- package/types/server/mapped-repo.d.ts +10 -0
- package/types/server/memory-repo.d.ts +28 -0
- package/types/server/path-map.d.ts +3 -0
- package/types/server/signin/access.d.ts +68 -0
- package/types/server/signin/code.d.ts +44 -0
- package/types/server/signin/codes.d.ts +98 -0
- package/types/server/signin/refusal.d.ts +122 -0
- package/types/server/signin/session.d.ts +29 -0
package/editor/app.js
ADDED
|
@@ -0,0 +1,1178 @@
|
|
|
1
|
+
// The editor page: start-up, the two screens (Home, and one page's form),
|
|
2
|
+
// loading and saving, and the publish flow. The form itself is built by
|
|
3
|
+
// forms.js; every rule it follows lives in the modules imported here.
|
|
4
|
+
import { ApiError, SessionExpired, createApi } from "./api.js";
|
|
5
|
+
import { clearDraft, draftPaths, loadDraft, saveDraft } from "./drafts.js";
|
|
6
|
+
import { trackPublish } from "./publish.js";
|
|
7
|
+
import { cleanDoc, isDirty } from "./model.js";
|
|
8
|
+
import { docTypeForPath, validateDoc } from "./shared/validate.js";
|
|
9
|
+
import { DOC_PATH_RE, SHA_RE } from "./shared/rules.js";
|
|
10
|
+
import { SITE } from "./shared/site.js";
|
|
11
|
+
import { el, renderForm } from "./forms.js";
|
|
12
|
+
import { ACTIONS, TEXT, editorVersion, errorView } from "./copy.js";
|
|
13
|
+
import { EMPTY_STATUS, nextStatus, statusView } from "./status.js";
|
|
14
|
+
import { prepareImage } from "./images.js";
|
|
15
|
+
import { createPagePhotos, stillWaiting, withFieldsFrom } from "./photos.js";
|
|
16
|
+
import { UploadFailed, blobToBase64, createUploader } from "./uploads.js";
|
|
17
|
+
import {
|
|
18
|
+
createTitles, customSlug, deleteControl, extraPagesSection, isNewPageDraft, pageTitle,
|
|
19
|
+
} from "./pages.js";
|
|
20
|
+
import { loadAssetMap } from "./asset-map.js";
|
|
21
|
+
// signin.js, the editor's own sign-in screen, is imported where it is shown,
|
|
22
|
+
// so a site signed in to in front of it (signIn "access") never loads it.
|
|
23
|
+
|
|
24
|
+
const SCHEMA_PATH = "content/schema.json";
|
|
25
|
+
const DRAFT_DELAY_MS = 300;
|
|
26
|
+
const WIDE = "(min-width: 48em)";
|
|
27
|
+
// Room left between the end of the page and the top of the publish bar.
|
|
28
|
+
const BAR_GAP_PX = 32;
|
|
29
|
+
|
|
30
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
31
|
+
|
|
32
|
+
/** {name: "home"}, {name: "edit", path}, or null for any other hash. */
|
|
33
|
+
export function parseRoute(hash) {
|
|
34
|
+
if (hash === "" || hash === "#" || hash === "#/") return { name: "home" };
|
|
35
|
+
const m = /^#\/edit\/(.+)$/.exec(hash);
|
|
36
|
+
return m && DOC_PATH_RE.test(m[1]) ? { name: "edit", path: m[1] } : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The public address of the page a document feeds: "/" for Home and for
|
|
40
|
+
* Site details, "/<slug>" otherwise ("/<slug>/" where the site serves that). */
|
|
41
|
+
export function pageUrl(path) {
|
|
42
|
+
const m = typeof path === "string" ? DOC_PATH_RE.exec(path) : null;
|
|
43
|
+
if (!m || m[1] === "site" || m[1] === "pages/index") return "/";
|
|
44
|
+
return `/${m[1].replace(/^(pages|custom)\//, "")}${SITE.trailingSlash ? "/" : ""}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Home's rows: every document whose type the schema lists, in the schema's
|
|
48
|
+
* order with Site details last, as [{path, label, draft}]. Simple pages are
|
|
49
|
+
* listed apart, under "Extra pages" (pages.js). */
|
|
50
|
+
export function homeRows(schema, docs, drafts) {
|
|
51
|
+
const order = Object.keys(schema.documents);
|
|
52
|
+
const rank = (type) => (type === "site" ? order.length : order.indexOf(type));
|
|
53
|
+
return Object.keys(docs)
|
|
54
|
+
.map((path) => ({ path, type: docTypeForPath(path) }))
|
|
55
|
+
.filter(({ type }) => type !== null && type !== "custom" && Object.hasOwn(schema.documents, type))
|
|
56
|
+
.sort((a, b) => rank(a.type) - rank(b.type) || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0))
|
|
57
|
+
.map(({ path, type }) => ({
|
|
58
|
+
path, label: schema.documents[type].label, draft: drafts.has(path),
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The blob sha a publish answer gives for path, or null when it gives
|
|
63
|
+
* none (an older server). */
|
|
64
|
+
export function publishedSha(result, path) {
|
|
65
|
+
const docs = isObject(result) && isObject(result.docs) ? result.docs : null;
|
|
66
|
+
const sha = docs && Object.hasOwn(docs, path) ? docs[path] : null;
|
|
67
|
+
return typeof sha === "string" && SHA_RE.test(sha) ? sha : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** localStorage, or a stand-in that refuses every save when the browser
|
|
71
|
+
* will not give access to it. */
|
|
72
|
+
function openStorage() {
|
|
73
|
+
try {
|
|
74
|
+
if (window.localStorage) return window.localStorage;
|
|
75
|
+
} catch {
|
|
76
|
+
// Blocked storage: fall through to the stand-in.
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
length: 0,
|
|
80
|
+
key: () => null,
|
|
81
|
+
getItem: () => null,
|
|
82
|
+
setItem() { throw new Error("Storage is not available."); },
|
|
83
|
+
removeItem() {},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function start() {
|
|
88
|
+
const main = document.getElementById("main");
|
|
89
|
+
const bar = document.getElementById("bar");
|
|
90
|
+
const statusLine = document.getElementById("status");
|
|
91
|
+
const barActions = document.getElementById("bar-actions");
|
|
92
|
+
const signOutButton = document.getElementById("sign-out");
|
|
93
|
+
document.getElementById("brand").textContent = SITE.title;
|
|
94
|
+
document.title = SITE.title;
|
|
95
|
+
signOutButton.textContent = TEXT.signOut;
|
|
96
|
+
|
|
97
|
+
// A host serving the site without its sign-in in front: only the way to
|
|
98
|
+
// the editor's own address.
|
|
99
|
+
// (The config sets bareHost only together with editorAddress.)
|
|
100
|
+
if (SITE.bareHost !== null && window.location.host === SITE.bareHost) {
|
|
101
|
+
main.replaceChildren(el("p", { class: "lead" }, [
|
|
102
|
+
`${TEXT.wrongHost} `, el("a", { href: SITE.editorAddress, text: SITE.editorAddress }),
|
|
103
|
+
]));
|
|
104
|
+
bar.classList.add("is-empty");
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const api = createApi((...args) => window.fetch(...args), {
|
|
109
|
+
setTimeout: (fn, ms) => window.setTimeout(fn, ms),
|
|
110
|
+
clearTimeout: (id) => window.clearTimeout(id),
|
|
111
|
+
}, { signIn: SITE.signIn });
|
|
112
|
+
const storage = openStorage();
|
|
113
|
+
const wide = window.matchMedia(WIDE);
|
|
114
|
+
const uploader = createUploader(api, { readBase64: (blob) => blobToBase64(blob, window.FileReader) });
|
|
115
|
+
// Preview addresses of photos published in this session, by base.
|
|
116
|
+
const publishedPhotos = new Map();
|
|
117
|
+
// Simple pages' published titles, for Home.
|
|
118
|
+
const titles = createTitles((path) => api.doc(path));
|
|
119
|
+
const readAssetMap = () => loadAssetMap({
|
|
120
|
+
fetch: (...args) => window.fetch(...args),
|
|
121
|
+
setTimeout: (fn, ms) => window.setTimeout(fn, ms),
|
|
122
|
+
clearTimeout: (id) => window.clearTimeout(id),
|
|
123
|
+
});
|
|
124
|
+
const imageEnv = {
|
|
125
|
+
createImageBitmap: typeof window.createImageBitmap === "function"
|
|
126
|
+
? (...args) => window.createImageBitmap(...args) : undefined,
|
|
127
|
+
document, URL: window.URL, crypto: window.crypto,
|
|
128
|
+
setTimeout: (fn, ms) => window.setTimeout(fn, ms),
|
|
129
|
+
clearTimeout: (id) => window.clearTimeout(id),
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// {state, schema, assetMap}; null while signed out. The map (photos
|
|
133
|
+
// "asset-map") is read again per page.
|
|
134
|
+
let site = null;
|
|
135
|
+
let page = null; // the open page; see openPage
|
|
136
|
+
let screen = null; // {alerts, notes, form, discard} elements of the edit screen
|
|
137
|
+
let homeLinks = null; // path -> its link, while Home shows
|
|
138
|
+
let publishing = false;
|
|
139
|
+
let openRun = 0;
|
|
140
|
+
let trackRun = 0;
|
|
141
|
+
let draftTimer = null;
|
|
142
|
+
let storageWarned = false;
|
|
143
|
+
let status = EMPTY_STATUS; // see status.js
|
|
144
|
+
// A publish that failed after its page was left: {path, error}, shown in
|
|
145
|
+
// full when that page is opened again.
|
|
146
|
+
let failed = null;
|
|
147
|
+
let signingIn = false; // the sign-in screen shows
|
|
148
|
+
|
|
149
|
+
// ---- The bottom bar and the status line ----
|
|
150
|
+
|
|
151
|
+
// Where a publish that failed elsewhere is reported, above the status line.
|
|
152
|
+
const barAlert = el("div", { class: "bar-alert" });
|
|
153
|
+
statusLine.before(barAlert);
|
|
154
|
+
let shownFailure = null;
|
|
155
|
+
let failureLink = null; // its "Open the page", absent while that page is open
|
|
156
|
+
|
|
157
|
+
// The page keeps room at its end for the bar, however tall an alert makes it.
|
|
158
|
+
if (typeof window.ResizeObserver === "function") {
|
|
159
|
+
new window.ResizeObserver(() => {
|
|
160
|
+
document.documentElement.style.setProperty("--bar-room", `${bar.offsetHeight + BAR_GAP_PX}px`);
|
|
161
|
+
}).observe(bar);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function updateBar() {
|
|
165
|
+
// Signing out waits for a publish or a delete already sent.
|
|
166
|
+
signOutButton.disabled = publishing;
|
|
167
|
+
const button = barActions.querySelector(".publish");
|
|
168
|
+
if (button) {
|
|
169
|
+
button.disabled = publishing || !page || page.choosing || page.preparing > 0 ||
|
|
170
|
+
!isDirty(page.doc, page.published);
|
|
171
|
+
}
|
|
172
|
+
if (screen && page) {
|
|
173
|
+
// Shown only when there is something to throw away, and not while a
|
|
174
|
+
// keep-or-discard choice waits for the page to come back.
|
|
175
|
+
screen.discard.hidden = page.choosing || !isDirty(page.doc, page.published);
|
|
176
|
+
// Hidden, it goes back to the plain button, so it never reappears
|
|
177
|
+
// already asking.
|
|
178
|
+
if (screen.discard.hidden && screen.discard.querySelector(".confirm")) drawDiscard();
|
|
179
|
+
for (const b of screen.discard.querySelectorAll("button")) {
|
|
180
|
+
if (!b.hasAttribute("data-spent")) b.disabled = publishing;
|
|
181
|
+
}
|
|
182
|
+
// A delete waits, like Publish, for a keep-or-discard choice.
|
|
183
|
+
for (const b of screen.remove?.node.querySelectorAll("button") ?? []) {
|
|
184
|
+
if (!b.hasAttribute("data-spent")) b.disabled = publishing || page.choosing;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
bar.classList.toggle("is-empty",
|
|
188
|
+
!button && statusLine.childNodes.length === 0 && barAlert.childNodes.length === 0);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function renderStatus() {
|
|
192
|
+
const view = statusView(status, page ? page.path : null);
|
|
193
|
+
const parts = [];
|
|
194
|
+
for (const part of view.line) {
|
|
195
|
+
if (parts.length > 0) parts.push(" ");
|
|
196
|
+
parts.push(part.href
|
|
197
|
+
? el("a", { class: "button see-page", href: part.href, target: "_blank", rel: "noopener", text: part.text })
|
|
198
|
+
: part.text);
|
|
199
|
+
}
|
|
200
|
+
statusLine.replaceChildren(...parts);
|
|
201
|
+
// Rebuilt only when it changes, so it is announced once.
|
|
202
|
+
if (view.failure !== shownFailure) {
|
|
203
|
+
shownFailure = view.failure;
|
|
204
|
+
failureLink = null;
|
|
205
|
+
if (view.failure === null) {
|
|
206
|
+
barAlert.replaceChildren();
|
|
207
|
+
} else {
|
|
208
|
+
failureLink = el("a", { class: "button", href: `#/edit/${view.failure.path}`, text: TEXT.openFailed });
|
|
209
|
+
barAlert.replaceChildren(el("div", { class: "alert", role: "alert" }, [
|
|
210
|
+
el("p", { text: view.failure.text }),
|
|
211
|
+
el("div", { class: "actions" }, [
|
|
212
|
+
failureLink,
|
|
213
|
+
el("button", {
|
|
214
|
+
type: "button", class: "button", text: TEXT.dismiss,
|
|
215
|
+
on: { click: () => { failed = null; send({ type: "dismiss" }); } },
|
|
216
|
+
}),
|
|
217
|
+
]),
|
|
218
|
+
]));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (failureLink) {
|
|
222
|
+
// A link to the page on screen would do nothing.
|
|
223
|
+
const here = page !== null && page.path === shownFailure.path;
|
|
224
|
+
if (here && failureLink.isConnected) failureLink.remove();
|
|
225
|
+
else if (!here && !failureLink.isConnected) barAlert.querySelector(".actions").prepend(failureLink);
|
|
226
|
+
}
|
|
227
|
+
updateBar();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function send(event) {
|
|
231
|
+
const next = nextStatus(status, event);
|
|
232
|
+
if (next === status) return;
|
|
233
|
+
status = next;
|
|
234
|
+
renderStatus();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ---- Messages ----
|
|
238
|
+
|
|
239
|
+
/** An alert for a view from errorView, with a control for each action
|
|
240
|
+
* that has a handler (and "Sign in again" as a link). The first press of
|
|
241
|
+
* any of its buttons disables them all, so a choice is made once. */
|
|
242
|
+
function alertBox(view, handlers) {
|
|
243
|
+
const controls = [];
|
|
244
|
+
const box = el("div", { class: "alert", role: "alert", tabindex: "-1" });
|
|
245
|
+
for (const action of view.actions) {
|
|
246
|
+
if (action === "signin" && SITE.signIn === "access") {
|
|
247
|
+
// The editor's own address in a new tab, where the sign-in in front
|
|
248
|
+
// of the site stops; this page keeps its changes for Retry.
|
|
249
|
+
controls.push(el("a", {
|
|
250
|
+
class: "button", href: "/admin/", target: "_blank", rel: "noopener", text: ACTIONS.signin,
|
|
251
|
+
}));
|
|
252
|
+
} else if (action === "signin") {
|
|
253
|
+
controls.push(el("button", {
|
|
254
|
+
type: "button", class: "button", text: ACTIONS.signin,
|
|
255
|
+
on: { click: () => showSignIn(TEXT.signedOut) },
|
|
256
|
+
}));
|
|
257
|
+
} else if (handlers[action]) {
|
|
258
|
+
controls.push(el("button", {
|
|
259
|
+
type: "button", class: "button", text: ACTIONS[action],
|
|
260
|
+
on: {
|
|
261
|
+
click: () => {
|
|
262
|
+
for (const b of box.querySelectorAll("button")) b.disabled = true;
|
|
263
|
+
handlers[action]();
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
}));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
box.append(el("p", { text: view.text }),
|
|
270
|
+
view.detail ? el("p", { class: "detail", text: view.detail }) : "",
|
|
271
|
+
controls.length > 0 ? el("div", { class: "actions" }, controls) : "");
|
|
272
|
+
return box;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The words for a failure while reading, with "Sign in again" when it
|
|
276
|
+
* applies, and always Retry. */
|
|
277
|
+
const retryView = (error) => {
|
|
278
|
+
const view = errorView(error, { loading: true });
|
|
279
|
+
return { text: view.text, actions: [...view.actions.filter((a) => a === "signin"), "retry"] };
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/** A failure while loading, with Retry, and a way back once Home exists. */
|
|
283
|
+
function showLoadFailure(error, retry) {
|
|
284
|
+
homeLinks = null;
|
|
285
|
+
const box = alertBox(retryView(error), { retry });
|
|
286
|
+
main.replaceChildren(site ? el("a", { class: "back", href: "#/", text: TEXT.back }) : "", box);
|
|
287
|
+
box.focus();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** An error on the open page; with retryOnly (a failure while reading),
|
|
291
|
+
* Retry is always offered and nothing else but "Sign in again". */
|
|
292
|
+
function showError(error, handlers, { retryOnly = false } = {}) {
|
|
293
|
+
if (!screen) return null;
|
|
294
|
+
const box = alertBox(retryOnly ? retryView(error) : errorView(error), handlers);
|
|
295
|
+
screen.alerts.replaceChildren(box);
|
|
296
|
+
box.focus();
|
|
297
|
+
return box;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function clearAlert() {
|
|
301
|
+
if (screen) screen.alerts.replaceChildren();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function showLoading() {
|
|
305
|
+
main.replaceChildren(el("p", { class: "loading", text: TEXT.loading }));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** An unexpected script error: said once, below anything already shown. */
|
|
309
|
+
let crashShown = false;
|
|
310
|
+
function showCrash(event) {
|
|
311
|
+
// A ResizeObserver notice and a cross-origin "Script error." carry no
|
|
312
|
+
// error object; they are not crashes.
|
|
313
|
+
if (event && event.type === "error" && (event.error === null || event.error === undefined)) return;
|
|
314
|
+
if (crashShown) return;
|
|
315
|
+
crashShown = true;
|
|
316
|
+
try {
|
|
317
|
+
flushDraft();
|
|
318
|
+
} catch {
|
|
319
|
+
// The report matters more than this last save.
|
|
320
|
+
}
|
|
321
|
+
const box = el("div", { class: "alert", role: "alert" }, [
|
|
322
|
+
el("p", { text: TEXT.crashed }),
|
|
323
|
+
el("div", { class: "actions" }, [el("button", {
|
|
324
|
+
type: "button", class: "button", text: ACTIONS.reload,
|
|
325
|
+
on: { click: () => window.location.reload() },
|
|
326
|
+
})]),
|
|
327
|
+
]);
|
|
328
|
+
if (screen) screen.alerts.append(box);
|
|
329
|
+
else main.prepend(box);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// ---- Drafts ----
|
|
333
|
+
|
|
334
|
+
/** Keep p's typed content in storage, or clear it when nothing differs
|
|
335
|
+
* from what is published. A page clears only a draft it wrote (or took
|
|
336
|
+
* up when it opened), unless `settled`: after a successful publish or a
|
|
337
|
+
* Discard. So a page that was opened clean and never edited never
|
|
338
|
+
* touches a draft another tab may be writing. */
|
|
339
|
+
function keepDraft(p, { settled = false } = {}) {
|
|
340
|
+
// A deleted page keeps nothing.
|
|
341
|
+
if (p.removed) return;
|
|
342
|
+
// New photos' files cannot be stored: the draft keeps the photos the
|
|
343
|
+
// fields had and names the fields to pick again.
|
|
344
|
+
const draft = p.photos.forDraft(p.fields, p.doc);
|
|
345
|
+
// A draft that only asks for photos again is still worth keeping.
|
|
346
|
+
if (!isDirty(p.doc, p.published) && draft.repick.length === 0) {
|
|
347
|
+
if (p.ownsDraft || settled) {
|
|
348
|
+
clearDraft(storage, p.path);
|
|
349
|
+
p.ownsDraft = false;
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
// Only new photos differed: the stored doc is the published page itself.
|
|
354
|
+
const photosOnly = !isDirty(draft.doc, p.published);
|
|
355
|
+
if (saveDraft(storage, p.path, p.sha, draft.doc, Date.now(), draft.repick, photosOnly)) {
|
|
356
|
+
p.ownsDraft = true;
|
|
357
|
+
} else if (!storageWarned) {
|
|
358
|
+
storageWarned = true;
|
|
359
|
+
if (screen && page === p) {
|
|
360
|
+
screen.notes.append(el("p", { class: "notice warn", role: "alert", text: TEXT.storageRefused }));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function flushDraft() {
|
|
366
|
+
window.clearTimeout(draftTimer);
|
|
367
|
+
draftTimer = null;
|
|
368
|
+
if (page) keepDraft(page);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function scheduleDraft() {
|
|
372
|
+
window.clearTimeout(draftTimer);
|
|
373
|
+
draftTimer = window.setTimeout(flushDraft, DRAFT_DELAY_MS);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// ---- Loading and routing ----
|
|
377
|
+
|
|
378
|
+
async function load() {
|
|
379
|
+
showLoading();
|
|
380
|
+
try {
|
|
381
|
+
const [state, schema] = await Promise.all([api.state(), api.doc(SCHEMA_PATH)]);
|
|
382
|
+
if (!isObject(state.docs) || !isObject(schema.doc) || !isObject(schema.doc.documents)) {
|
|
383
|
+
throw new ApiError({ status: 200, code: "upstream", message: "" });
|
|
384
|
+
}
|
|
385
|
+
site = { state, schema: schema.doc, assetMap: {} };
|
|
386
|
+
} catch (err) {
|
|
387
|
+
if (signedOut(err)) return;
|
|
388
|
+
showLoadFailure(err, load);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
// Signing out is the editor's own sign-in's; one in front has none here.
|
|
392
|
+
if (SITE.signIn === "code") signOutButton.hidden = false;
|
|
393
|
+
route(false);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// ---- Signing in and out ----
|
|
397
|
+
|
|
398
|
+
/** The sign-in screen, in place of whatever shows. The open page's
|
|
399
|
+
* draft is kept first; once signed in, the editor loads again and opens
|
|
400
|
+
* the address in location.hash, so the same page comes back with it. */
|
|
401
|
+
function showSignIn(notice = null) {
|
|
402
|
+
if (signingIn) return;
|
|
403
|
+
signingIn = true;
|
|
404
|
+
leavePage();
|
|
405
|
+
site = null;
|
|
406
|
+
signOutButton.hidden = true;
|
|
407
|
+
document.title = SITE.title;
|
|
408
|
+
// "Loading" until the screen's module arrives (the first time only),
|
|
409
|
+
// never the page just left.
|
|
410
|
+
showLoading();
|
|
411
|
+
import("./signin.js").then(({ renderSignIn }) => {
|
|
412
|
+
renderSignIn(main, {
|
|
413
|
+
api, notice,
|
|
414
|
+
onSignedIn: () => {
|
|
415
|
+
signingIn = false;
|
|
416
|
+
load();
|
|
417
|
+
},
|
|
418
|
+
});
|
|
419
|
+
}, () => {
|
|
420
|
+
// Not to be had (a poor connection): a failure to load, with Retry.
|
|
421
|
+
signingIn = false;
|
|
422
|
+
showLoadFailure(new ApiError({ status: 0, code: "offline", message: "" }), () => showSignIn(notice));
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** True when err is a sign-in that has ended and the editor's own sign-in
|
|
427
|
+
* screen takes over. With a sign-in in front of the site (signIn
|
|
428
|
+
* "access"), an ended sign-in is reported like any other failure, its
|
|
429
|
+
* "Sign in again" a link. */
|
|
430
|
+
const endsSignIn = (err) => SITE.signIn === "code" && err instanceof SessionExpired;
|
|
431
|
+
|
|
432
|
+
/** True when err is a sign-in that has ended, after showing the sign-in
|
|
433
|
+
* screen. */
|
|
434
|
+
function signedOut(err) {
|
|
435
|
+
if (!endsSignIn(err)) return false;
|
|
436
|
+
// At the first load nobody was signed out: they had not signed in yet.
|
|
437
|
+
showSignIn(site ? TEXT.signedOut : null);
|
|
438
|
+
return true;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
async function signOut() {
|
|
442
|
+
if (publishing) return;
|
|
443
|
+
signOutButton.disabled = true;
|
|
444
|
+
try {
|
|
445
|
+
await api.session.signOut();
|
|
446
|
+
} catch (err) {
|
|
447
|
+
const box = alertBox({ text: errorView(err).text, actions: [] }, {});
|
|
448
|
+
if (screen) screen.alerts.replaceChildren(box);
|
|
449
|
+
else main.prepend(box);
|
|
450
|
+
box.focus();
|
|
451
|
+
return;
|
|
452
|
+
} finally {
|
|
453
|
+
signOutButton.disabled = publishing;
|
|
454
|
+
}
|
|
455
|
+
showSignIn();
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function leavePage() {
|
|
459
|
+
flushDraft();
|
|
460
|
+
// A publish still sending this page's photos keeps them until it ends.
|
|
461
|
+
if (page && !publishing) page.photos.clear();
|
|
462
|
+
page = null;
|
|
463
|
+
screen = null;
|
|
464
|
+
homeLinks = null;
|
|
465
|
+
openRun++;
|
|
466
|
+
barActions.replaceChildren();
|
|
467
|
+
send({ type: "home" });
|
|
468
|
+
renderStatus();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function route(moveFocus = true) {
|
|
472
|
+
if (!site) return;
|
|
473
|
+
const r = parseRoute(window.location.hash);
|
|
474
|
+
const type = r && r.name === "edit" ? docTypeForPath(r.path) : null;
|
|
475
|
+
// A simple page made in this browser and never published exists only as its draft.
|
|
476
|
+
const exists = type !== null &&
|
|
477
|
+
(Object.hasOwn(site.state.docs, r.path) || isNewPageDraft(storage, r.path));
|
|
478
|
+
if (exists && Object.hasOwn(site.schema.documents, type)) {
|
|
479
|
+
openPage(r.path, type, moveFocus);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (!r || r.name !== "home") window.history.replaceState(null, "", "#/");
|
|
483
|
+
showHome(moveFocus);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function heading(text, moveFocus) {
|
|
487
|
+
const h1 = el("h1", { tabindex: "-1", text });
|
|
488
|
+
if (moveFocus) window.requestAnimationFrame(() => h1.focus());
|
|
489
|
+
return h1;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// ---- Home ----
|
|
493
|
+
|
|
494
|
+
function showHome(moveFocus) {
|
|
495
|
+
leavePage();
|
|
496
|
+
document.title = SITE.title;
|
|
497
|
+
const rows = homeRows(site.schema, site.state.docs, new Set(draftPaths(storage)));
|
|
498
|
+
homeLinks = new Map();
|
|
499
|
+
main.replaceChildren(
|
|
500
|
+
heading(TEXT.homeHeading, moveFocus),
|
|
501
|
+
el("p", { class: "lead", text: TEXT.homeIntro }),
|
|
502
|
+
el("ul", { class: "pages" }, rows.map((row) => {
|
|
503
|
+
const link = el("a", { class: "page-link", href: `#/edit/${row.path}` }, [
|
|
504
|
+
el("span", { class: "page-name", text: row.label }),
|
|
505
|
+
row.draft ? el("span", { class: "badge", text: TEXT.unpublished }) : null,
|
|
506
|
+
]);
|
|
507
|
+
homeLinks.set(row.path, link);
|
|
508
|
+
return el("li", {}, [link]);
|
|
509
|
+
})),
|
|
510
|
+
Object.hasOwn(site.schema.documents, "custom") ? extraPagesSection({
|
|
511
|
+
docs: site.state.docs, storage, titles, schema: site.schema, host: window.location.host,
|
|
512
|
+
links: homeLinks, open: (path) => { window.location.hash = `#/edit/${path}`; },
|
|
513
|
+
}) : "",
|
|
514
|
+
...(SITE.version ? [el("p", { class: "version", text: editorVersion(SITE.version) })] : []),
|
|
515
|
+
);
|
|
516
|
+
window.scrollTo(0, 0);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** Home, in place of a page that is gone. */
|
|
520
|
+
function goHome() {
|
|
521
|
+
window.history.replaceState(null, "", "#/");
|
|
522
|
+
showHome(true);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** Bring Home's "Unpublished changes" badges up to date in place, so
|
|
526
|
+
* focus and scroll stay where they are. Does nothing unless Home shows. */
|
|
527
|
+
function refreshHome() {
|
|
528
|
+
if (!homeLinks) return;
|
|
529
|
+
const drafts = new Set(draftPaths(storage));
|
|
530
|
+
for (const [path, link] of homeLinks) {
|
|
531
|
+
const badge = link.querySelector(".badge");
|
|
532
|
+
if (drafts.has(path) && !badge) link.append(el("span", { class: "badge", text: TEXT.unpublished }));
|
|
533
|
+
if (!drafts.has(path) && badge) badge.remove();
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// ---- A page ----
|
|
538
|
+
|
|
539
|
+
async function openPage(path, type, moveFocus) {
|
|
540
|
+
leavePage();
|
|
541
|
+
const run = openRun;
|
|
542
|
+
showLoading();
|
|
543
|
+
// A simple page never published: nothing on the site yet, only its draft.
|
|
544
|
+
const isNew = !Object.hasOwn(site.state.docs, path);
|
|
545
|
+
let fresh;
|
|
546
|
+
let assetMap = null;
|
|
547
|
+
try {
|
|
548
|
+
// With photos "asset-map", the map is read each time, so a photo
|
|
549
|
+
// published earlier shows once the site has rebuilt; a failed read
|
|
550
|
+
// keeps the last good one.
|
|
551
|
+
[fresh, assetMap] = await Promise.all([
|
|
552
|
+
isNew ? { sha: null, doc: null } : api.doc(path),
|
|
553
|
+
SITE.photos === "asset-map" ? readAssetMap() : null,
|
|
554
|
+
]);
|
|
555
|
+
} catch (err) {
|
|
556
|
+
if (run !== openRun) return;
|
|
557
|
+
if (signedOut(err)) return;
|
|
558
|
+
if (type === "custom" && notFound(err)) deletedElsewhere(path);
|
|
559
|
+
else showLoadFailure(err, () => openPage(path, type, true));
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (run !== openRun) return;
|
|
563
|
+
if (assetMap !== null) site.assetMap = assetMap;
|
|
564
|
+
const draft = loadDraft(storage, path);
|
|
565
|
+
// Deleted meanwhile (in another tab).
|
|
566
|
+
if (isNew && draft === null) {
|
|
567
|
+
route(moveFocus);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
page = {
|
|
571
|
+
path, type,
|
|
572
|
+
label: site.schema.documents[type].label,
|
|
573
|
+
fields: site.schema.documents[type].fields,
|
|
574
|
+
sha: fresh.sha,
|
|
575
|
+
published: fresh.doc,
|
|
576
|
+
doc: fresh.doc,
|
|
577
|
+
// True once this page has written the stored draft, or took it up.
|
|
578
|
+
ownsDraft: false,
|
|
579
|
+
// True while "Keep my changes" or "Discard my changes" is waiting for
|
|
580
|
+
// an answer; publishing waits for it too.
|
|
581
|
+
choosing: false,
|
|
582
|
+
groupState: new Map(),
|
|
583
|
+
form: null,
|
|
584
|
+
// New photos, and how many are being prepared (Publish waits for them).
|
|
585
|
+
photos: createPagePhotos({ revoke: (url) => window.URL.revokeObjectURL(url), shown: publishedPhotos }),
|
|
586
|
+
preparing: 0,
|
|
587
|
+
};
|
|
588
|
+
let notice = null;
|
|
589
|
+
let stale = false;
|
|
590
|
+
// A field waiting for a photo holds an old copy of the site, not an edit
|
|
591
|
+
// of his. Without those fields, is anything of his own left?
|
|
592
|
+
const ownChanges = draft !== null && !draft.photosOnly &&
|
|
593
|
+
isDirty(withFieldsFrom(draft.doc, draft.repick, fresh.doc) ?? draft.doc, fresh.doc);
|
|
594
|
+
let rekeep = false;
|
|
595
|
+
if (ownChanges) {
|
|
596
|
+
page.doc = draft.doc;
|
|
597
|
+
page.ownsDraft = true;
|
|
598
|
+
page.photos.restore(draft.repick);
|
|
599
|
+
if (draft.base === fresh.sha) {
|
|
600
|
+
notice = isNew ? null : TEXT.restored;
|
|
601
|
+
} else {
|
|
602
|
+
// Until the choice is made the draft stays on the version it began from.
|
|
603
|
+
stale = true;
|
|
604
|
+
page.sha = draft.base;
|
|
605
|
+
page.choosing = true;
|
|
606
|
+
}
|
|
607
|
+
} else if (draft) {
|
|
608
|
+
// The live page, never the draft's copy of it (that could put an older
|
|
609
|
+
// photo back), asking again only for photos still as they were. One
|
|
610
|
+
// that changed since is his own publish, landed.
|
|
611
|
+
const waiting = stillWaiting(draft.repick, draft.doc, fresh.doc);
|
|
612
|
+
page.photos.restore(waiting);
|
|
613
|
+
if (waiting.length > 0) {
|
|
614
|
+
page.ownsDraft = true;
|
|
615
|
+
rekeep = true;
|
|
616
|
+
} else {
|
|
617
|
+
clearDraft(storage, path);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
if (type === "custom") {
|
|
621
|
+
page.label = pageTitle(page.doc, customSlug(path));
|
|
622
|
+
if (fresh.doc !== null) titles.set(path, fresh.sha, fresh.doc);
|
|
623
|
+
}
|
|
624
|
+
showEditor(notice, moveFocus);
|
|
625
|
+
// Kept again, on the live version.
|
|
626
|
+
if (rekeep) keepDraft(page);
|
|
627
|
+
send({ type: "open", path });
|
|
628
|
+
const earlier = failed !== null && failed.path === path ? failed.error : null;
|
|
629
|
+
if (earlier) failed = null;
|
|
630
|
+
if (stale) {
|
|
631
|
+
// The page changed since these edits began: ask before showing them.
|
|
632
|
+
screen.form.replaceChildren();
|
|
633
|
+
showError(new ApiError({ status: 409, code: "stale", message: "" }), {
|
|
634
|
+
keep: () => resolveStale(true), discard: () => resolveStale(false),
|
|
635
|
+
});
|
|
636
|
+
} else {
|
|
637
|
+
drawForm();
|
|
638
|
+
// A publish of this page failed after it was left: show that here now.
|
|
639
|
+
if (earlier) showPublishError(page, earlier);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function showEditor(notice, moveFocus) {
|
|
644
|
+
const p = page;
|
|
645
|
+
document.title = `${page.label} - ${SITE.title}`;
|
|
646
|
+
screen = {
|
|
647
|
+
alerts: el("div", { class: "alerts" }),
|
|
648
|
+
notes: el("div", { class: "notes" },
|
|
649
|
+
[notice ? el("p", { class: "notice restored", text: notice }) : null]),
|
|
650
|
+
form: el("div", { class: "form" }),
|
|
651
|
+
discard: el("div", { class: "discard-area" }),
|
|
652
|
+
// A simple page's own end: "Delete this page".
|
|
653
|
+
remove: p.type === "custom" ? deleteControl({
|
|
654
|
+
title: () => pageTitle(p.doc, customSlug(p.path)), onDelete: () => deletePage(p),
|
|
655
|
+
}) : null,
|
|
656
|
+
};
|
|
657
|
+
drawDiscard();
|
|
658
|
+
main.replaceChildren(
|
|
659
|
+
el("a", { class: "back", href: "#/", text: TEXT.back }),
|
|
660
|
+
heading(page.label, moveFocus),
|
|
661
|
+
screen.alerts, screen.notes, screen.form, screen.discard, screen.remove?.node ?? "",
|
|
662
|
+
);
|
|
663
|
+
barActions.replaceChildren(el("button", {
|
|
664
|
+
type: "button", class: "button primary publish", text: TEXT.publish, on: { click: publish },
|
|
665
|
+
}));
|
|
666
|
+
updateBar();
|
|
667
|
+
window.scrollTo(0, 0);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** "Discard my changes" at the end of the form, away from Publish. It
|
|
671
|
+
* asks first, in place; yes reads the published page back. Drawn only for
|
|
672
|
+
* p while it is the page on screen. */
|
|
673
|
+
function drawDiscard(p = page) {
|
|
674
|
+
if (!screen || page !== p) return null;
|
|
675
|
+
const button = el("button", {
|
|
676
|
+
type: "button", class: "button discard", text: ACTIONS.discard,
|
|
677
|
+
on: { click: askDiscard },
|
|
678
|
+
});
|
|
679
|
+
screen.discard.replaceChildren(button);
|
|
680
|
+
return button;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function askDiscard() {
|
|
684
|
+
// A page never published has nothing to go back to: discarding removes it.
|
|
685
|
+
const isNew = neverPublished(page);
|
|
686
|
+
const ask = el("p", {
|
|
687
|
+
class: "confirm-text", id: "discard-ask", tabindex: "-1", text: isNew ? TEXT.discardNewAsk : TEXT.discardAsk,
|
|
688
|
+
});
|
|
689
|
+
const row = el("div", { class: "confirm", role: "group", "aria-labelledby": "discard-ask" }, [
|
|
690
|
+
ask,
|
|
691
|
+
el("div", { class: "actions" }, [
|
|
692
|
+
el("button", {
|
|
693
|
+
type: "button", class: "button danger", text: TEXT.discardYes,
|
|
694
|
+
on: {
|
|
695
|
+
click: () => {
|
|
696
|
+
for (const b of row.querySelectorAll("button")) {
|
|
697
|
+
b.disabled = true;
|
|
698
|
+
b.setAttribute("data-spent", "");
|
|
699
|
+
}
|
|
700
|
+
if (isNew) {
|
|
701
|
+
forget(page);
|
|
702
|
+
goHome();
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
// Publish waits for the published page to come back.
|
|
706
|
+
page.choosing = true;
|
|
707
|
+
updateBar();
|
|
708
|
+
resolveStale(false, true);
|
|
709
|
+
},
|
|
710
|
+
},
|
|
711
|
+
}),
|
|
712
|
+
el("button", {
|
|
713
|
+
type: "button", class: "button", text: TEXT.discardNo,
|
|
714
|
+
on: { click: () => drawDiscard().focus() },
|
|
715
|
+
}),
|
|
716
|
+
]),
|
|
717
|
+
]);
|
|
718
|
+
screen.discard.replaceChildren(row);
|
|
719
|
+
updateBar();
|
|
720
|
+
ask.focus();
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** Prepare a picked photo; Publish waits while any is being prepared. */
|
|
724
|
+
async function preparePhoto(p, file) {
|
|
725
|
+
p.preparing++;
|
|
726
|
+
updateBar();
|
|
727
|
+
try {
|
|
728
|
+
return await prepareImage(file, imageEnv);
|
|
729
|
+
} finally {
|
|
730
|
+
p.preparing--;
|
|
731
|
+
updateBar();
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
function drawForm(problems = []) {
|
|
736
|
+
const p = page;
|
|
737
|
+
// A change that arrives after p was left (a photo finishing) is not p's to make.
|
|
738
|
+
p.form = renderForm(screen.form, p.fields, p.doc, (doc) => { if (page === p) onEdit(doc); }, {
|
|
739
|
+
assetMap: site?.assetMap, problems, wide: wide.matches, groupState: p.groupState,
|
|
740
|
+
// Read when a photo lands: a publish since this draw changes it.
|
|
741
|
+
published: () => p.published,
|
|
742
|
+
photos: {
|
|
743
|
+
...p.photos,
|
|
744
|
+
prepare: (file) => preparePhoto(p, file),
|
|
745
|
+
// The last one let go on a clean page takes the draft (and Home's badge) with it.
|
|
746
|
+
keepCurrent: (name) => {
|
|
747
|
+
p.photos.dropRepick(name);
|
|
748
|
+
keepDraft(p);
|
|
749
|
+
},
|
|
750
|
+
},
|
|
751
|
+
recheck: (doc) => validateDoc(site.schema, p.type, cleanDoc(p.fields, doc)),
|
|
752
|
+
onProblems: (count) => send({ type: "problems", path: p.path, count }),
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
function onEdit(doc) {
|
|
757
|
+
page.doc = doc;
|
|
758
|
+
scheduleDraft();
|
|
759
|
+
send({ type: "edit" });
|
|
760
|
+
updateBar();
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/** "Your unpublished changes from last time are back" no longer holds. */
|
|
764
|
+
function dropRestoredNotice() {
|
|
765
|
+
if (screen) screen.notes.querySelector(".restored")?.remove();
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/** Read the page again, then keep the typed content on the new version
|
|
769
|
+
* or go back to the site's. Used after "this page was changed somewhere
|
|
770
|
+
* else" and by "Discard my changes". When that is a discard (`undo`), a
|
|
771
|
+
* failed read puts the page back as it was before the press. */
|
|
772
|
+
async function resolveStale(keep, undo = false) {
|
|
773
|
+
const p = page;
|
|
774
|
+
let fresh;
|
|
775
|
+
try {
|
|
776
|
+
fresh = await api.doc(p.path);
|
|
777
|
+
} catch (err) {
|
|
778
|
+
if (page === p && signedOut(err)) return;
|
|
779
|
+
if (page === p && p.type === "custom" && notFound(err)) {
|
|
780
|
+
deletedElsewhere(p.path, p);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
if (page === p) {
|
|
784
|
+
if (undo) p.choosing = false;
|
|
785
|
+
drawDiscard();
|
|
786
|
+
updateBar();
|
|
787
|
+
showError(err, {
|
|
788
|
+
retry: () => {
|
|
789
|
+
if (undo && page === p) {
|
|
790
|
+
p.choosing = true;
|
|
791
|
+
updateBar();
|
|
792
|
+
}
|
|
793
|
+
return resolveStale(keep, undo);
|
|
794
|
+
},
|
|
795
|
+
}, { retryOnly: true });
|
|
796
|
+
}
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
if (page !== p) return;
|
|
800
|
+
p.sha = fresh.sha;
|
|
801
|
+
p.published = fresh.doc;
|
|
802
|
+
p.choosing = false;
|
|
803
|
+
// The page is on the site (a new page whose address was taken meanwhile
|
|
804
|
+
// included), so Home lists it with this version.
|
|
805
|
+
site.state.docs[p.path] = fresh.sha;
|
|
806
|
+
if (p.type === "custom") titles.set(p.path, fresh.sha, fresh.doc);
|
|
807
|
+
if (!keep) {
|
|
808
|
+
p.doc = fresh.doc;
|
|
809
|
+
p.photos.clear();
|
|
810
|
+
dropRestoredNotice();
|
|
811
|
+
}
|
|
812
|
+
keepDraft(p, { settled: !keep });
|
|
813
|
+
clearAlert();
|
|
814
|
+
drawForm();
|
|
815
|
+
drawDiscard();
|
|
816
|
+
updateBar();
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
function showProblems(problems) {
|
|
820
|
+
drawForm(problems);
|
|
821
|
+
page.form.focusFirstProblem();
|
|
822
|
+
send({ type: "problems", path: page.path, count: problems.length });
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/** A failed publish of p, shown on p's own screen with its choices. */
|
|
826
|
+
function showPublishError(p, err) {
|
|
827
|
+
if (err instanceof ApiError && err.code === "stale") {
|
|
828
|
+
p.choosing = true;
|
|
829
|
+
updateBar();
|
|
830
|
+
}
|
|
831
|
+
const box = showError(err, {
|
|
832
|
+
retry: async () => {
|
|
833
|
+
await publish();
|
|
834
|
+
// Nothing was sent (nothing left to publish, or problems to fix
|
|
835
|
+
// first), so nothing replaced this alert: it no longer applies.
|
|
836
|
+
if (box && box.isConnected) box.remove();
|
|
837
|
+
},
|
|
838
|
+
keep: () => resolveStale(true), discard: () => resolveStale(false),
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// ---- Publishing ----
|
|
843
|
+
|
|
844
|
+
async function publish() {
|
|
845
|
+
if (!page || publishing || page.choosing) return;
|
|
846
|
+
const p = page;
|
|
847
|
+
const cleaned = cleanDoc(p.fields, p.doc);
|
|
848
|
+
if (isDirty(cleaned, p.doc)) {
|
|
849
|
+
p.doc = cleaned;
|
|
850
|
+
drawForm();
|
|
851
|
+
keepDraft(p);
|
|
852
|
+
}
|
|
853
|
+
if (!isDirty(p.doc, p.published)) {
|
|
854
|
+
updateBar();
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
const problems = validateDoc(site.schema, p.type, p.doc);
|
|
858
|
+
if (problems.length > 0) {
|
|
859
|
+
showProblems(problems);
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
clearAlert();
|
|
863
|
+
publishing = true;
|
|
864
|
+
// From here an older publish's tracker no longer owns the status line.
|
|
865
|
+
const run = ++trackRun;
|
|
866
|
+
const url = pageUrl(p.path);
|
|
867
|
+
send({ type: "publish", path: p.path, url });
|
|
868
|
+
const sent = p.doc;
|
|
869
|
+
// The new photos this version holds; one replaced since is not sent.
|
|
870
|
+
const photos = p.photos.toSend(p.fields, sent);
|
|
871
|
+
// Picked again while they go up, they keep their previews till the end.
|
|
872
|
+
p.photos.sending(photos);
|
|
873
|
+
let result;
|
|
874
|
+
try {
|
|
875
|
+
const body = { docs: [{ path: p.path, sha: p.sha, doc: sent }] };
|
|
876
|
+
if (photos.length > 0) {
|
|
877
|
+
body.images = await uploader.send(photos, (photo, total) =>
|
|
878
|
+
send({ type: "uploading", path: p.path, url, photo, photos: total }));
|
|
879
|
+
send({ type: "phase", path: p.path, url, phase: "saving" });
|
|
880
|
+
}
|
|
881
|
+
result = await api.publish(body);
|
|
882
|
+
} catch (caught) {
|
|
883
|
+
p.photos.notSent(photos);
|
|
884
|
+
publishing = false;
|
|
885
|
+
drawDiscard(p);
|
|
886
|
+
updateBar();
|
|
887
|
+
const here = page === p;
|
|
888
|
+
// The site turned a photo's file away: sending it again will not help.
|
|
889
|
+
const refused = caught instanceof UploadFailed && caught.refused;
|
|
890
|
+
const err = caught instanceof UploadFailed ? caught.cause : caught;
|
|
891
|
+
if (endsSignIn(err)) {
|
|
892
|
+
// Nothing was published; the draft is kept and the page comes back
|
|
893
|
+
// after signing in.
|
|
894
|
+
send({ type: "failed", path: p.path, label: p.label, text: "", here: true });
|
|
895
|
+
signedOut(err);
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
const text = refused ? TEXT.photoNotSaved : errorView(err).text;
|
|
899
|
+
send({ type: "failed", path: p.path, label: p.label, text, here });
|
|
900
|
+
if (!here) {
|
|
901
|
+
// Reported in the bar wherever the owner is now, and in full when
|
|
902
|
+
// this page is opened again (where a refused photo is asked for again).
|
|
903
|
+
if (!refused) failed = { path: p.path, error: err };
|
|
904
|
+
refreshHome();
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
if (refused) {
|
|
908
|
+
// Nothing to mark when that photo has since been replaced or removed.
|
|
909
|
+
const field = p.photos.fieldOf(p.fields, p.doc, caught.photo.base);
|
|
910
|
+
if (field !== null) showProblems([{ field, message: TEXT.photoNotSaved }]);
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
if (err instanceof ApiError && err.code === "invalid" && Array.isArray(err.problems)) {
|
|
914
|
+
const mine = err.problems.filter((x) => isObject(x) && x.path === p.path);
|
|
915
|
+
if (mine.length > 0) {
|
|
916
|
+
showProblems(mine);
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
// An out-of-date simple page may have been deleted on another device.
|
|
921
|
+
if (err instanceof ApiError && err.code === "stale" && p.type === "custom" && p.sha !== null) {
|
|
922
|
+
try {
|
|
923
|
+
await api.doc(p.path);
|
|
924
|
+
} catch (readErr) {
|
|
925
|
+
if (notFound(readErr) && page === p) {
|
|
926
|
+
deletedElsewhere(p.path, p);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
if (page !== p) return;
|
|
931
|
+
}
|
|
932
|
+
showPublishError(p, err);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
publishing = false;
|
|
936
|
+
drawDiscard(p);
|
|
937
|
+
p.published = sent;
|
|
938
|
+
// Its photos are on the site now; their previews stay for the session.
|
|
939
|
+
p.photos.sent(photos, p.fields, sent);
|
|
940
|
+
p.photos.restore([]);
|
|
941
|
+
// The new base, taken from the answer before anything else is awaited,
|
|
942
|
+
// so a second Publish straight away is checked against the right file.
|
|
943
|
+
const sha = publishedSha(result, p.path);
|
|
944
|
+
if (sha !== null) {
|
|
945
|
+
p.sha = sha;
|
|
946
|
+
// Home lists the page as published (a new one included) without a
|
|
947
|
+
// reload. Signed out meanwhile, there is no Home: it is read again.
|
|
948
|
+
if (site) site.state.docs[p.path] = sha;
|
|
949
|
+
if (p.type === "custom") titles.set(p.path, sha, sent);
|
|
950
|
+
} else {
|
|
951
|
+
// An older server: read the page back. If that fails the next publish
|
|
952
|
+
// may report a change elsewhere, which "Keep my changes" resolves.
|
|
953
|
+
try {
|
|
954
|
+
const fresh = await api.doc(p.path);
|
|
955
|
+
p.sha = fresh.sha;
|
|
956
|
+
p.published = fresh.doc;
|
|
957
|
+
} catch {
|
|
958
|
+
// Keep the sha we have.
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
// Clears the draft, or keeps what was typed while the request was out;
|
|
962
|
+
// left alone when the same page has been opened again since, so a late
|
|
963
|
+
// answer cannot touch newer typing.
|
|
964
|
+
if (page === p || page === null || page.path !== p.path) keepDraft(p, { settled: true });
|
|
965
|
+
else catchUp(page, p);
|
|
966
|
+
if (page === p) dropRestoredNotice();
|
|
967
|
+
updateBar();
|
|
968
|
+
refreshHome();
|
|
969
|
+
track(result, p.path, url, run);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/** q, the same page opened again while p's publish went up, comes onto
|
|
973
|
+
* the version just published: fields that waited for a photo take the
|
|
974
|
+
* published one (no longer asked for when it changed), and its next
|
|
975
|
+
* publish starts from the new version. What was typed since is kept.
|
|
976
|
+
* Only when its fields line up with the published page for certain: if
|
|
977
|
+
* a list was changed, a field cannot be found, or a photo is still being
|
|
978
|
+
* prepared, q is left as it is, and its next publish meets the "changed
|
|
979
|
+
* somewhere else" question before anything is overwritten. */
|
|
980
|
+
function catchUp(q, p) {
|
|
981
|
+
if (q.choosing || q.preparing > 0 || q.photos.listsChanged()) return;
|
|
982
|
+
const names = q.photos.repickNames();
|
|
983
|
+
const doc = withFieldsFrom(q.doc, names, p.published);
|
|
984
|
+
// Only when, its waiting photos filled in, it is exactly what was published.
|
|
985
|
+
if (doc === null || isDirty(doc, p.published)) return;
|
|
986
|
+
q.photos.restore(stillWaiting(names, q.doc, p.published));
|
|
987
|
+
q.doc = doc;
|
|
988
|
+
q.sha = p.sha;
|
|
989
|
+
q.published = p.published;
|
|
990
|
+
keepDraft(q, { settled: true });
|
|
991
|
+
if (page === q && screen) {
|
|
992
|
+
drawForm();
|
|
993
|
+
drawDiscard(q);
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
/** A simple page made here that the site does not have yet. (Reopened
|
|
998
|
+
* while its first publish went up, it may have no published version of
|
|
999
|
+
* its own and still be on the site.) */
|
|
1000
|
+
const neverPublished = (p) => p.published === null && !Object.hasOwn(site.state.docs, p.path);
|
|
1001
|
+
|
|
1002
|
+
const notFound = (err) => err instanceof ApiError && err.code === "not_found";
|
|
1003
|
+
|
|
1004
|
+
/** The simple page at path is no longer on the site: deleted on another
|
|
1005
|
+
* device. Home stops listing it, its draft goes, and he is told, with
|
|
1006
|
+
* the way back. p: the open page, when there is one. */
|
|
1007
|
+
function deletedElsewhere(path, p = null) {
|
|
1008
|
+
if (p) forget(p);
|
|
1009
|
+
delete site.state.docs[path];
|
|
1010
|
+
clearDraft(storage, path);
|
|
1011
|
+
leavePage();
|
|
1012
|
+
const box = el("div", { class: "alert", role: "alert", tabindex: "-1" }, [
|
|
1013
|
+
el("p", { text: TEXT.deletedElsewhere }),
|
|
1014
|
+
el("div", { class: "actions" }, [el("a", { class: "button", href: "#/", text: TEXT.back })]),
|
|
1015
|
+
]);
|
|
1016
|
+
main.replaceChildren(box);
|
|
1017
|
+
box.focus();
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/** p is gone: nothing of it is kept, and Home no longer lists it. Its
|
|
1021
|
+
* photos stay on the site. */
|
|
1022
|
+
function forget(p) {
|
|
1023
|
+
p.removed = true;
|
|
1024
|
+
clearDraft(storage, p.path);
|
|
1025
|
+
p.photos.clear();
|
|
1026
|
+
// Signed out meanwhile, nothing is listed: Home is read again after.
|
|
1027
|
+
if (site) delete site.state.docs[p.path];
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/** "Yes, delete it": a page never published is just forgotten; a
|
|
1031
|
+
* published one is removed from the site, then Home shows the status. */
|
|
1032
|
+
async function deletePage(p) {
|
|
1033
|
+
if (publishing) return;
|
|
1034
|
+
if (neverPublished(p)) {
|
|
1035
|
+
forget(p);
|
|
1036
|
+
goHome();
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
clearAlert();
|
|
1040
|
+
publishing = true;
|
|
1041
|
+
updateBar();
|
|
1042
|
+
const run = ++trackRun;
|
|
1043
|
+
send({ type: "publish", path: p.path, url: null });
|
|
1044
|
+
let result;
|
|
1045
|
+
try {
|
|
1046
|
+
result = await api.publish({ deletes: [{ path: p.path, sha: p.sha ?? site.state.docs[p.path] }] });
|
|
1047
|
+
} catch (err) {
|
|
1048
|
+
publishing = false;
|
|
1049
|
+
updateBar();
|
|
1050
|
+
if (endsSignIn(err)) {
|
|
1051
|
+
send({ type: "failed", path: p.path, label: p.label, text: "", here: true, deleting: true });
|
|
1052
|
+
signedOut(err);
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
const here = page === p;
|
|
1056
|
+
const view = errorView(err);
|
|
1057
|
+
send({ type: "failed", path: p.path, label: p.label, text: view.text, here, deleting: true });
|
|
1058
|
+
if (!here) return;
|
|
1059
|
+
if (err instanceof ApiError && err.code === "stale") {
|
|
1060
|
+
await recheckDelete(p);
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
deleteFailed(p, err, () => deletePage(p));
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
publishing = false;
|
|
1067
|
+
updateBar();
|
|
1068
|
+
forget(p);
|
|
1069
|
+
// The same page opened again while the delete went up goes too.
|
|
1070
|
+
if (page !== null && page !== p && page.path === p.path) forget(page);
|
|
1071
|
+
if ((page !== null && page.path === p.path) || homeLinks) goHome();
|
|
1072
|
+
track(result, p.path, null, run);
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/** Why a delete of p, still on screen, did not happen, with Retry when
|
|
1076
|
+
* that can help. */
|
|
1077
|
+
function deleteFailed(p, err, retry) {
|
|
1078
|
+
const view = errorView(err);
|
|
1079
|
+
screen.remove.reset();
|
|
1080
|
+
updateBar();
|
|
1081
|
+
const box = alertBox({ text: view.text, actions: view.actions.filter((a) => a === "retry" || a === "signin") },
|
|
1082
|
+
{ retry });
|
|
1083
|
+
screen.alerts.replaceChildren(box);
|
|
1084
|
+
box.focus();
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
/** A delete refused because the page changed since it was opened: show
|
|
1088
|
+
* the page as it is now and ask again, so the owner deletes what he has
|
|
1089
|
+
* seen. A page already gone is as good as deleted. */
|
|
1090
|
+
async function recheckDelete(p) {
|
|
1091
|
+
// Publish and Delete wait for the page to come back.
|
|
1092
|
+
p.choosing = true;
|
|
1093
|
+
updateBar();
|
|
1094
|
+
let fresh;
|
|
1095
|
+
try {
|
|
1096
|
+
fresh = await api.doc(p.path);
|
|
1097
|
+
} catch (err) {
|
|
1098
|
+
p.choosing = false;
|
|
1099
|
+
if (page === p && signedOut(err)) return;
|
|
1100
|
+
if (notFound(err)) {
|
|
1101
|
+
forget(p);
|
|
1102
|
+
if (page === p || homeLinks) goHome();
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
if (page !== p) {
|
|
1106
|
+
send({ type: "failed", path: p.path, label: p.label, text: errorView(err).text, here: false, deleting: true });
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
deleteFailed(p, err, () => recheckDelete(p));
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
p.choosing = false;
|
|
1113
|
+
if (page !== p) {
|
|
1114
|
+
send({ type: "failed", path: p.path, label: p.label, text: TEXT.stale, here: false, deleting: true });
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
// What he had typed would have gone with the page, and is not kept.
|
|
1118
|
+
const lost = isDirty(p.doc, p.published);
|
|
1119
|
+
p.sha = fresh.sha;
|
|
1120
|
+
p.published = fresh.doc;
|
|
1121
|
+
// What he had typed would have gone with the page; he checks theirs.
|
|
1122
|
+
p.doc = fresh.doc;
|
|
1123
|
+
p.photos.clear();
|
|
1124
|
+
site.state.docs[p.path] = fresh.sha;
|
|
1125
|
+
titles.set(p.path, fresh.sha, fresh.doc);
|
|
1126
|
+
dropRestoredNotice();
|
|
1127
|
+
keepDraft(p, { settled: true });
|
|
1128
|
+
clearAlert();
|
|
1129
|
+
drawForm();
|
|
1130
|
+
drawDiscard();
|
|
1131
|
+
updateBar();
|
|
1132
|
+
screen.remove.ask(lost ? TEXT.deleteChangedLost : TEXT.deleteChanged);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
/** Follow a publish until the site shows it. A newer publish replaces it. */
|
|
1136
|
+
function track(result, path, url, run) {
|
|
1137
|
+
if (run !== trackRun) return;
|
|
1138
|
+
// An unchanged answer is tracked too: its commit may still be building.
|
|
1139
|
+
const phase = (name) => send({ type: "phase", path, url, phase: name });
|
|
1140
|
+
const sleep = (ms) => new Promise((resolve, reject) => {
|
|
1141
|
+
window.setTimeout(() => (run === trackRun ? resolve() : reject(new Error("replaced"))), ms);
|
|
1142
|
+
});
|
|
1143
|
+
trackPublish({
|
|
1144
|
+
commit: result.commit,
|
|
1145
|
+
getDeployed: () => api.deployed(),
|
|
1146
|
+
contains: async (mine, deployed) => (await api.status(mine, deployed)).contains === true,
|
|
1147
|
+
sleep,
|
|
1148
|
+
now: () => Date.now(),
|
|
1149
|
+
onState: (state) => {
|
|
1150
|
+
if (run === trackRun) phase(state);
|
|
1151
|
+
},
|
|
1152
|
+
}).catch(() => {
|
|
1153
|
+
// Replaced by a newer publish; that one owns the status line now.
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
// ---- Leaving ----
|
|
1158
|
+
|
|
1159
|
+
window.addEventListener("beforeunload", (e) => {
|
|
1160
|
+
if (page && isDirty(page.doc, page.published)) {
|
|
1161
|
+
e.preventDefault();
|
|
1162
|
+
e.returnValue = "";
|
|
1163
|
+
}
|
|
1164
|
+
});
|
|
1165
|
+
window.addEventListener("pagehide", flushDraft);
|
|
1166
|
+
document.addEventListener("visibilitychange", () => {
|
|
1167
|
+
if (document.visibilityState === "hidden") flushDraft();
|
|
1168
|
+
});
|
|
1169
|
+
window.addEventListener("hashchange", () => route(true));
|
|
1170
|
+
signOutButton.addEventListener("click", signOut);
|
|
1171
|
+
window.addEventListener("error", showCrash);
|
|
1172
|
+
window.addEventListener("unhandledrejection", showCrash);
|
|
1173
|
+
|
|
1174
|
+
renderStatus();
|
|
1175
|
+
load();
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
if (typeof document !== "undefined") start();
|