// maketeam.mjs — Make team page from a selection (spec §6.7). The // selection is widened to every block it touches, and those blocks are // tinted before anything is confirmed. The popover asks for a title // (prefilled from the first heading, or the first words) and says what // moves. On confirm the draft is committed as an ordinary commit (a // day's first commit makes it), the block numbers are read from the // body just committed, parsed by markup.mjs, never from the editor's // tree, and Make team page is posted with them and the revision they // index, which the server checks. A taken or date-shaped title asks for // another. The note then holds a link where the blocks were. import { Plugin, PluginKey } from 'prosemirror-state' import { Decoration, DecorationSet } from 'prosemirror-view' import { parse, cleanBody, Grammar } from './markup.mjs' import { serialiseDetailed } from './serialize.mjs' import { held } from './busy.mjs' export const movingKey = new PluginKey('mapa-moving') // widen is the emitted blocks the selection touches: their numbers in // the body the document writes, and their places. export function widen (state, fidelity) { const { from, to } = state.selection const { emitted, body } = serialiseDetailed(state.doc, fidelity) const chosen = [] emitted.forEach((block, index) => { const end = block.pos + block.node.nodeSize if (block.pos < Math.max(to, from + 1) && end > from) chosen.push({ index, block }) }) return { chosen, body } } // suggestTitle is the first heading among the blocks, or the first words. export function suggestTitle (blocks) { const heading = blocks.find(({ block }) => block.node.type.name === 'heading') const text = (heading ?? blocks[0])?.block.node.textContent ?? '' const words = text.trim().split(/\s+/u).slice(0, 6).join(' ') return words.replace(/[[\]|#^]/gu, '').slice(0, 80) } // numbersIn maps chosen blocks to the committed body's block numbers: // the same blocks, in the same order, saying the same thing. export function numbersIn (body, chosen, emitted) { const blocks = parse(cleanBody(body)) if (blocks.length !== emitted) return null const numbers = [] for (const { index, block } of chosen) { if (!blocks[index] || blocks[index].source !== block.text) return null numbers.push(index) } return numbers } export function movingPlugin () { return new Plugin({ key: movingKey, state: { init: () => null, apply (tr, ranges) { const meta = tr.getMeta(movingKey) if (meta !== undefined) return meta return ranges && ranges.map(range => ({ from: tr.mapping.map(range.from), to: tr.mapping.map(range.to) })) } }, props: { decorations (state) { const ranges = movingKey.getState(state) if (!ranges) return null return DecorationSet.create(state.doc, ranges.map(range => Decoration.node(range.from, range.to, { 'mapa-moving': '' }))) } } }) } function element (tag, attrs = {}, text = '') { const node = document.createElement(tag) for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) if (text) node.textContent = text return node } // dialog is the confirm popover, built once. function dialog () { const box = element('dialog', { 'mapa-make-team': '', 'aria-labelledby': 'mapa-make-team-title' }) const form = element('form', { method: 'dialog' }) const heading = element('h2', { id: 'mapa-make-team-title' }, 'Make team page') const label = element('label', { for: 'mapa-make-team-name' }, 'Title of the new page') const input = element('input', { id: 'mapa-make-team-name', name: 'title', required: '', autocomplete: 'off', 'rst-input': '' }) const error = element('p', { 'mapa-make-team-error': '', role: 'alert' }) const said = element('p', {}, 'These blocks move to a new team page everyone can open. Your note keeps a link in their place. Nothing else in your note is shared.') const confirm = element('button', { type: 'submit', 'rst-btn': 'primary' }, 'Make team page') const cancel = element('button', { type: 'button', 'rst-btn': '' }, 'Cancel') const foot = element('div', { 'mapa-make-team-foot': '' }) foot.append(cancel, confirm) form.append(heading, label, input, error, said, foot) box.append(form) document.body.append(box) return { box, form, input, error, confirm, cancel } } // makeTeamPage is the command: from the bubble, the accessory row. export function makeTeamPage ({ saver, fidelity, post, notify }) { let parts = null return view => { const { chosen } = widen(view.state, fidelity) if (chosen.length === 0) return false const ranges = chosen.map(({ block }) => ({ from: block.pos, to: block.pos + block.node.nodeSize })) view.dispatch(view.state.tr.setMeta(movingKey, ranges)) parts = parts ?? dialog() const { box, form, input, error, confirm, cancel } = parts input.value = suggestTitle(chosen) error.textContent = '' const done = () => { view.dispatch(view.state.tr.setMeta(movingKey, null)) form.onsubmit = null cancel.onclick = null box.onclose = null if (box.open) box.close() view.focus() } cancel.onclick = done box.onclose = done form.onsubmit = async event => { event.preventDefault() const title = input.value.trim() const answer = await held(confirm, () => move(view, chosen, title)) if (answer.error) { error.textContent = answer.error input.focus() return } done() notify(title, answer.href) } box.showModal() input.select() return true } // move commits, numbers the blocks from the commit, and posts them. async function move (view, chosen, title) { const outcome = await saver.commit({ explicit: true }) if (!['none', 'saved', 'merged'].includes(outcome) || !saver.page) { return { error: 'Your note could not be saved just now, so nothing was moved. Try again.' } } const { emitted } = serialiseDetailed(view.state.doc, fidelity) const fresh = widenAgain(view, chosen) const numbers = fresh && numbersIn(saver.saved, fresh, emitted.length) if (!numbers) return { error: 'Your note changed while it was saved. Choose the blocks again.' } let reply try { reply = await post(`/p/${saver.page}/share`, { title, revision: saver.base, block: numbers.map(String), grammar: String(Grammar) }) } catch { return { error: 'Nothing was moved: the server could not be reached.' } } if (reply.status !== 200) return { error: reply.json?.error ?? 'Nothing was moved.' } saver.took(reply.json.body, reply.json.revision) return { href: reply.json.href } } // widenAgain finds the chosen blocks in the document as it is after // the commit (a merge may have moved them), by the text they wrote. function widenAgain (view, chosen) { const { emitted } = serialiseDetailed(view.state.doc, fidelity) const out = [] let from = 0 for (const { block } of chosen) { const at = emitted.findIndex((other, index) => index >= from && other.text === block.text) if (at < 0) return null out.push({ index: at, block: emitted[at] }) from = at + 1 } return out } }