// pagelink.mjs — Link to a page over a selection (level-up spec §6.6, // build note 81): ⌘L, the bubble's Page link, the Insert menu's row // and the phone's [[ button. The selected words become one chip whose // shown text is those words, [[Target|words]], or [[Target]] when the // words are the title. With nothing selected the same command types // [[, so the list opens as it does for typing. import { TextSelection, NodeSelection } from 'prosemirror-state' import { spans, WikiLink, goTrimSpace, isDate } from './markup.mjs' import { inline } from './serialize.mjs' import { inlineNodes } from './fromMarkdown.mjs' // refusals are what Link to a page says when the selection cannot be // one chip. A chip is one inline atom, so it holds words from one // block, and Mapa's Markdown has no link inside a link. export const refusals = { blocks: 'A page link holds words from one paragraph, heading or item. Select words in just one.', atoms: 'A page link can’t hold another link or a line break. Select plain words.', syntax: 'A page link can’t show [ or ]]. Select words without them.', blank: 'Select some words to link them to a page.', context: 'Linked here, the Markdown around these words would read differently: a ! before them makes an embed, and a stray ` or * pairs up. Select other words.', moved: 'The selection changed. Select the words again.', // A date chip (build note 83): when even a space before it would not // let the block read back with it. date: 'A date can’t go here: the Markdown around it would read differently, such as a stray ` or * pairing up. Move the caret and try again.' } // pageLinkSource is the Markdown for a link to target showing shown // ('' for the title), or null when Mapa's parser would read those bytes // as anything else: a bracket in the words, a # or | in the target. export function pageLinkSource (target, shown = '') { const source = `[[${target}${shown ? '|' + shown : ''}]]` const found = spans(source) const span = found[0] const exact = found.length === 1 && span.kind === WikiLink && span.source === source && span.target === target && span.text === shown && !span.anchor return exact ? source : null } // linkable is the selection as Link to a page reads it: the words and // their range, edge spaces left outside, or a refusal. Null when there // is no selection to link (an empty one, or one in a code block). A // selection of all (⌘A) or of a block is refused, so ⌘L says why // instead of doing nothing (review low 1). export function linkable (state) { const { selection } = state if (selection.empty) return null const { $from, $to, from, to } = selection if ($from.parent.type.spec.code || $to.parent.type.spec.code) return null if (selection instanceof NodeSelection) return { refuse: refusals.atoms } if (!(selection instanceof TextSelection) || $from.parent !== $to.parent) { return { refuse: refusals.blocks } } let plain = true state.doc.nodesBetween(from, to, node => { if (node.isInline && !node.isText) plain = false }) // Web-linked words would lose their address (review M4). if (!plain || state.doc.rangeHasMark(from, to, state.schema.marks.link)) { return { refuse: refusals.atoms } } const text = state.doc.textBetween(from, to) const words = goTrimSpace(text) if (words === '') return { refuse: refusals.blank } if (pageLinkSource('Page', words) === null) return { refuse: refusals.syntax } const lead = text.indexOf(words) const found = { words, from: from + lead, to: from + lead + words.length } if (made(state, found, 'Page').refuse) return { refuse: refusals.context } return found } // chipFor is the chip linking words to target: a date link for a day. function chipFor (schema, target, words) { const shown = words === target ? '' : words return isDate(target) ? schema.nodes.datelink.create({ date: target, shown }) : schema.nodes.wikilink.create({ target, shown }) } // counts is what a block's inline content holds: its text, and how many // links and embeds. function counts (nodes) { let text = '' let links = 0 let embeds = 0 for (const node of nodes) { if (node.isText) text += node.text else if (node.type.name === 'embed') embeds++ else if (node.type.name !== 'hard_break') links++ } return { text, links, embeds } } // made is the transaction putting the chip in, or a refusal when the // block's Markdown would not read back with it as one link: the words // right after a ! make an embed, a backtick before and after the chip // pair into code (review M3). The block is written as the serialiser // would and parsed as the server will. function made (state, found, target) { const tr = state.tr.replaceWith(found.from, found.to, chipFor(state.schema, target, found.words)) if (!readsBack(tr.doc.resolve(found.from).parent)) return { refuse: refusals.context } return { tr } } // readsBack is whether a textblock's Markdown, written as the // serialiser would and parsed as the server will, holds the same text, // links and embeds the block does. Link to a page and the date chip // (commands.mjs insertDate, build note 83) check a block with this // before they change it. export function readsBack (block) { const children = [] block.forEach(child => children.push(child)) const now = counts(children) const back = counts(inlineNodes(block.type.schema, inline(block))) return now.text === back.text && now.links === back.links && now.embeds === back.embeds } // linkCheck is the transaction turning the selected words into a chip // to target, the caret after it, or a refusal: the selection is no // longer the words the field was opened over, or the link would not // read back as itself. export function linkCheck (state, target, words) { const found = linkable(state) if (!found) return { refuse: refusals.moved } if (found.refuse) return found if (found.words !== words) return { refuse: refusals.moved } const shown = words === target ? '' : words if (pageLinkSource(target, shown) === null) return { refuse: refusals.syntax } const result = made(state, found, target) if (result.refuse) return result const after = result.tr.mapping.map(found.to) return { tr: result.tr.setSelection(TextSelection.create(result.tr.doc, after)).scrollIntoView() } } // linkSelection is linkCheck as a command: false when it refuses. export function linkSelection (target, words) { return (state, dispatch) => { const { tr } = linkCheck(state, target, words) if (!tr) return false if (dispatch) dispatch(tr) return true } } // linkPage is the command ⌘L, Insert → Link to a page and the phone's // [[ run. Over a selection it asks open(found) to offer pages for it // (the bubble's field), or to say why it cannot; with none it types [[. // In a code block it does nothing, and still takes the key, so ⌘L never // reaches the browser's address bar from the page's text. export function linkPage (open) { return (state, dispatch, view) => { const found = linkable(state) if (found) { if (dispatch && view) open(view, found) return true } const { $from, empty } = state.selection if (!empty || $from.parent.type.spec.code || !$from.parent.inlineContent) return true if (dispatch) dispatch(state.tr.insertText('[[').scrollIntoView()) return true } }