// notices.mjs — what the editor tells the person, around the page: the // lead slot's save status (estilo's savestatus), and a bar above the // page for the answers that need a choice (§6.5): a page deleted while // you wrote, a base past the history window, another tab's newer text, // and leaving with something unsettled. Each bar is built from the // existing routes' forms and links, styled as the draft bar is. const barAttr = 'mapa-editor-notice' function element (tag, attrs = {}, ...children) { const node = document.createElement(tag) for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) for (const child of children) node.append(child) return node } // words are the states estilo's savestatus has no text for: draft is a // page deleted under the editor, whose text is stored as the draft only // (review H1). const words = { draft: 'Saved as a draft', orphan: 'Not saved' } // status shows state (idle, saving, saved, retrying, failed, draft) in // the lead slot, in the words the server wrote on the element. export function status (state) { const lead = document.querySelector('[estilo-save-status]') if (!lead || lead.getAttribute('data-state') === state) return lead.setAttribute('data-state', state) lead.textContent = lead.getAttribute(`data-text-${state}`) ?? words[state] ?? '' } function form (action, label, fields = {}, primary = false) { const node = element('form', { method: 'post', action }) for (const [name, value] of Object.entries(fields)) { node.append(element('input', { type: 'hidden', name, value })) } node.append(element('button', { 'rst-btn': primary ? 'primary' : '', type: 'submit' }, label)) return node } function button (label, run, primary = false) { const node = element('button', { 'rst-btn': primary ? 'primary' : '', type: 'button' }, label) node.addEventListener('click', run) return node } // show puts one bar above the article, replacing any earlier one. The // bar opens a section of the page's stack (level-up build note 89), as // the server's does: it follows the island's scripts, not the head, so // without it the stack would give it the step between ordinary blocks. export function show (article, text, ...controls) { clear(article) const bar = element('div', { 'mapa-unsaved': '', 'rst-section': '', [barAttr]: '', role: 'status' }, element('p', {}, text), ...controls) article.before(bar) return bar } export function clear (article) { article.parentNode?.querySelectorAll(`[${barAttr}]`).forEach(bar => bar.remove()) } // notice draws one of the saver's answers. export function notice (article, kind, info) { switch (kind) { case 'gone': return show(article, 'This page was deleted while you were writing. What you type is kept as a draft, never saved to the page, until you choose.', form(`/p/${info.page}/draft/scratch`, 'Save as a scratch page', {}, true), form(`/p/${info.page}/draft/discard`, 'Discard', { version: info.version })) case 'orphan': return show(article, orphanText, scratchForm()) case 'noBase': return show(article, 'Your edit started from a version older than the 90-day history, so it cannot be merged block by block.', element('a', { 'rst-btn': 'primary', href: `/p/${info.page}/conflict` }, 'Choose what to keep')) case 'committed': return show(article, 'This was saved in another tab. What you type here is kept as a new draft.', button('Reload', () => location.reload(), true)) case 'newer': return show(article, 'Another tab had newer text, so this tab now shows it.') } } const orphanText = 'This page was deleted before what you typed could be saved. Save it as a scratch page to keep it.' // scratchForm posts the text as a new scratch page (POST /scratch); the // page's script fills in the body as the person sends it (review round // 4, M-B). function scratchForm () { return form('/scratch', 'Save as a scratch page', { body: '' }, true) } // leaving is the choice when leaving cannot commit (§6.5): a conflict // to decide, or text the server never got. export function leaving (article, kind, go) { if (kind === 'orphan') { return show(article, orphanText + ' If you leave now, it is lost.', scratchForm(), button('Leave anyway', go)) } if (kind === 'uploading') { return show(article, 'An image is still uploading. If you leave now, it is not added.', button('Leave anyway', go)) } if (kind === 'conflict') { return show(article, 'Someone saved this page while you were writing. Choose below what to keep, or leave and decide on your next visit.', button('Leave and decide later', go)) } return show(article, 'Not saved: what you typed since the last save would be lost if you leave now.', button('Leave anyway', go)) }