oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/status.mjs — the editor's save line and its load-error line,
2// factored out of editor-entry.js so the rules are testable under plain
3// Node (status.test.mjs).
4//
5// The save line is estilo's savestatus (the template renders it with
6// every state's words from the catalog; its module, served at
7// static/estilo/save-status.<hash>.mjs, moves it between them). Its
8// four states: `saved`, `saving`, `retrying` (a save is being retried —
9// the network, or a 5xx) and `failed` (a batch was DROPPED: the server
10// refused it with a 4xx, and the person's Y.Doc now holds changes the
11// server never will). The last two are different words on purpose:
12// "Offline, retrying" on a batch nobody is retrying tells a person to
13// wait for something that will not happen.
14//
15// A slide that would not load is not a save state — savestatus has no
16// word for it — so it is its own line ([slides-load-error]), said until
17// a slide does load, and independent of whatever the save line says.
18
19/** The save queue's status word (save-queue.mjs) as a savestatus state. */
20export function queueStatusState (state) {
21 if (state === 'ok') return 'saved'
22 if (state === 'saving') return 'saving'
23 if (state === 'failed') return 'failed'
24 return 'retrying'
25}
26
27/**
28 * The state to show. A dropped batch outranks everything — it is the
29 * one fact the person has to act on — and stays for the session; then
30 * whatever the queue says.
31 */
32export function statusState ({ queueState = 'saved', sessionFailed = false } = {}) {
33 if (sessionFailed) return 'failed'
34 return queueState
35}
36
37/**
38 * The editor's status: setSave is savestatus's setter, errorEl the
39 * load-error line, errorText its words. Returns {save, drop,
40 * loadFailed}: save(state) records the queue's state (or re-shows the
41 * current one with no argument), drop() marks the session failed,
42 * loadFailed(bool) says or clears the load error.
43 */
44export function createEditorStatus ({ setSave, errorEl, errorText }) {
45 let queueState = 'saved'
46 let sessionFailed = false
47 function save (state) {
48 if (state) queueState = state
49 setSave(statusState({ queueState, sessionFailed }))
50 }
51 return {
52 save,
53 drop () { sessionFailed = true; save() },
54 loadFailed (failed) { if (errorEl) errorEl.textContent = failed ? (errorText || '') : '' }
55 }
56}
57
58/**
59 * Loads estilo's savestatus module (its fingerprinted URL is on the
60 * root as data-save-status-module) and mounts it on the line. A module
61 * that will not load leaves the load-error line saying so and rejects:
62 * an editor whose save line cannot move is one that would say "Saved"
63 * over changes it never saved.
64 */
65export async function mountSaveStatus (root, load = src => import(src)) {
66 const errorEl = root.querySelector('[slides-load-error]')
67 const errorText = root.dataset.textError
68 try {
69 const { mount } = await load(root.dataset.saveStatusModule)
70 return createEditorStatus({ setSave: mount(root.querySelector('[estilo-save-status]')), errorEl, errorText })
71 } catch (err) {
72 if (errorEl) errorEl.textContent = errorText || ''
73 throw err
74 }
75}
76