| 1 | // notices.mjs — what the editor tells the person, around the page: the |
| 2 | // lead slot's save status (estilo's savestatus), and a bar above the |
| 3 | // page for the answers that need a choice (§6.5): a page deleted while |
| 4 | // you wrote, a base past the history window, another tab's newer text, |
| 5 | // and leaving with something unsettled. Each bar is built from the |
| 6 | // existing routes' forms and links, styled as the draft bar is. |
| 7 | |
| 8 | const barAttr = 'mapa-editor-notice' |
| 9 | |
| 10 | function element (tag, attrs = {}, ...children) { |
| 11 | const node = document.createElement(tag) |
| 12 | for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) |
| 13 | for (const child of children) node.append(child) |
| 14 | return node |
| 15 | } |
| 16 | |
| 17 | // words are the states estilo's savestatus has no text for: draft is a |
| 18 | // page deleted under the editor, whose text is stored as the draft only |
| 19 | // (review H1). |
| 20 | const words = { draft: 'Saved as a draft', orphan: 'Not saved' } |
| 21 | |
| 22 | // status shows state (idle, saving, saved, retrying, failed, draft) in |
| 23 | // the lead slot, in the words the server wrote on the element. |
| 24 | export function status (state) { |
| 25 | const lead = document.querySelector('[estilo-save-status]') |
| 26 | if (!lead || lead.getAttribute('data-state') === state) return |
| 27 | lead.setAttribute('data-state', state) |
| 28 | lead.textContent = lead.getAttribute(`data-text-${state}`) ?? words[state] ?? '' |
| 29 | } |
| 30 | |
| 31 | function form (action, label, fields = {}, primary = false) { |
| 32 | const node = element('form', { method: 'post', action }) |
| 33 | for (const [name, value] of Object.entries(fields)) { |
| 34 | node.append(element('input', { type: 'hidden', name, value })) |
| 35 | } |
| 36 | node.append(element('button', { 'rst-btn': primary ? 'primary' : '', type: 'submit' }, label)) |
| 37 | return node |
| 38 | } |
| 39 | |
| 40 | function button (label, run, primary = false) { |
| 41 | const node = element('button', { 'rst-btn': primary ? 'primary' : '', type: 'button' }, label) |
| 42 | node.addEventListener('click', run) |
| 43 | return node |
| 44 | } |
| 45 | |
| 46 | // show puts one bar above the article, replacing any earlier one. The |
| 47 | // bar opens a section of the page's stack (level-up build note 89), as |
| 48 | // the server's does: it follows the island's scripts, not the head, so |
| 49 | // without it the stack would give it the step between ordinary blocks. |
| 50 | export function show (article, text, ...controls) { |
| 51 | clear(article) |
| 52 | const bar = element('div', { 'mapa-unsaved': '', 'rst-section': '', [barAttr]: '', role: 'status' }, |
| 53 | element('p', {}, text), ...controls) |
| 54 | article.before(bar) |
| 55 | return bar |
| 56 | } |
| 57 | |
| 58 | export function clear (article) { |
| 59 | article.parentNode?.querySelectorAll(`[${barAttr}]`).forEach(bar => bar.remove()) |
| 60 | } |
| 61 | |
| 62 | // notice draws one of the saver's answers. |
| 63 | export function notice (article, kind, info) { |
| 64 | switch (kind) { |
| 65 | case 'gone': |
| 66 | 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.', |
| 67 | form(`/p/${info.page}/draft/scratch`, 'Save as a scratch page', {}, true), |
| 68 | form(`/p/${info.page}/draft/discard`, 'Discard', { version: info.version })) |
| 69 | case 'orphan': |
| 70 | return show(article, orphanText, scratchForm()) |
| 71 | case 'noBase': |
| 72 | return show(article, 'Your edit started from a version older than the 90-day history, so it cannot be merged block by block.', |
| 73 | element('a', { 'rst-btn': 'primary', href: `/p/${info.page}/conflict` }, 'Choose what to keep')) |
| 74 | case 'committed': |
| 75 | return show(article, 'This was saved in another tab. What you type here is kept as a new draft.', |
| 76 | button('Reload', () => location.reload(), true)) |
| 77 | case 'newer': |
| 78 | return show(article, 'Another tab had newer text, so this tab now shows it.') |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const orphanText = 'This page was deleted before what you typed could be saved. Save it as a scratch page to keep it.' |
| 83 | |
| 84 | // scratchForm posts the text as a new scratch page (POST /scratch); the |
| 85 | // page's script fills in the body as the person sends it (review round |
| 86 | // 4, M-B). |
| 87 | function scratchForm () { |
| 88 | return form('/scratch', 'Save as a scratch page', { body: '' }, true) |
| 89 | } |
| 90 | |
| 91 | // leaving is the choice when leaving cannot commit (§6.5): a conflict |
| 92 | // to decide, or text the server never got. |
| 93 | export function leaving (article, kind, go) { |
| 94 | if (kind === 'orphan') { |
| 95 | return show(article, orphanText + ' If you leave now, it is lost.', |
| 96 | scratchForm(), button('Leave anyway', go)) |
| 97 | } |
| 98 | if (kind === 'uploading') { |
| 99 | return show(article, 'An image is still uploading. If you leave now, it is not added.', |
| 100 | button('Leave anyway', go)) |
| 101 | } |
| 102 | if (kind === 'conflict') { |
| 103 | return show(article, 'Someone saved this page while you were writing. Choose below what to keep, or leave and decide on your next visit.', |
| 104 | button('Leave and decide later', go)) |
| 105 | } |
| 106 | return show(article, 'Not saved: what you typed since the last save would be lost if you leave now.', |
| 107 | button('Leave anyway', go)) |
| 108 | } |
| 109 | |