oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1// pagelink.mjs — Link to a page over a selection (level-up spec §6.6,
2// build note 81): ⌘L, the bubble's Page link, the Insert menu's row
3// and the phone's [[ button. The selected words become one chip whose
4// shown text is those words, [[Target|words]], or [[Target]] when the
5// words are the title. With nothing selected the same command types
6// [[, so the list opens as it does for typing.
7
8import { TextSelection, NodeSelection } from 'prosemirror-state'
9import { spans, WikiLink, goTrimSpace, isDate } from './markup.mjs'
10import { inline } from './serialize.mjs'
11import { inlineNodes } from './fromMarkdown.mjs'
12
13// refusals are what Link to a page says when the selection cannot be
14// one chip. A chip is one inline atom, so it holds words from one
15// block, and Mapa's Markdown has no link inside a link.
16export const refusals = {
17 blocks: 'A page link holds words from one paragraph, heading or item. Select words in just one.',
18 atoms: 'A page link can’t hold another link or a line break. Select plain words.',
19 syntax: 'A page link can’t show [ or ]]. Select words without them.',
20 blank: 'Select some words to link them to a page.',
21 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.',
22 moved: 'The selection changed. Select the words again.',
23 // A date chip (build note 83): when even a space before it would not
24 // let the block read back with it.
25 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.'
26}
27
28// pageLinkSource is the Markdown for a link to target showing shown
29// ('' for the title), or null when Mapa's parser would read those bytes
30// as anything else: a bracket in the words, a # or | in the target.
31export function pageLinkSource (target, shown = '') {
32 const source = `[[${target}${shown ? '|' + shown : ''}]]`
33 const found = spans(source)
34 const span = found[0]
35 const exact = found.length === 1 && span.kind === WikiLink &&
36 span.source === source && span.target === target &&
37 span.text === shown && !span.anchor
38 return exact ? source : null
39}
40
41// linkable is the selection as Link to a page reads it: the words and
42// their range, edge spaces left outside, or a refusal. Null when there
43// is no selection to link (an empty one, or one in a code block). A
44// selection of all (⌘A) or of a block is refused, so ⌘L says why
45// instead of doing nothing (review low 1).
46export function linkable (state) {
47 const { selection } = state
48 if (selection.empty) return null
49 const { $from, $to, from, to } = selection
50 if ($from.parent.type.spec.code || $to.parent.type.spec.code) return null
51 if (selection instanceof NodeSelection) return { refuse: refusals.atoms }
52 if (!(selection instanceof TextSelection) || $from.parent !== $to.parent) {
53 return { refuse: refusals.blocks }
54 }
55 let plain = true
56 state.doc.nodesBetween(from, to, node => {
57 if (node.isInline && !node.isText) plain = false
58 })
59 // Web-linked words would lose their address (review M4).
60 if (!plain || state.doc.rangeHasMark(from, to, state.schema.marks.link)) {
61 return { refuse: refusals.atoms }
62 }
63 const text = state.doc.textBetween(from, to)
64 const words = goTrimSpace(text)
65 if (words === '') return { refuse: refusals.blank }
66 if (pageLinkSource('Page', words) === null) return { refuse: refusals.syntax }
67 const lead = text.indexOf(words)
68 const found = { words, from: from + lead, to: from + lead + words.length }
69 if (made(state, found, 'Page').refuse) return { refuse: refusals.context }
70 return found
71}
72
73// chipFor is the chip linking words to target: a date link for a day.
74function chipFor (schema, target, words) {
75 const shown = words === target ? '' : words
76 return isDate(target)
77 ? schema.nodes.datelink.create({ date: target, shown })
78 : schema.nodes.wikilink.create({ target, shown })
79}
80
81// counts is what a block's inline content holds: its text, and how many
82// links and embeds.
83function counts (nodes) {
84 let text = ''
85 let links = 0
86 let embeds = 0
87 for (const node of nodes) {
88 if (node.isText) text += node.text
89 else if (node.type.name === 'embed') embeds++
90 else if (node.type.name !== 'hard_break') links++
91 }
92 return { text, links, embeds }
93}
94
95// made is the transaction putting the chip in, or a refusal when the
96// block's Markdown would not read back with it as one link: the words
97// right after a ! make an embed, a backtick before and after the chip
98// pair into code (review M3). The block is written as the serialiser
99// would and parsed as the server will.
100function made (state, found, target) {
101 const tr = state.tr.replaceWith(found.from, found.to,
102 chipFor(state.schema, target, found.words))
103 if (!readsBack(tr.doc.resolve(found.from).parent)) return { refuse: refusals.context }
104 return { tr }
105}
106
107// readsBack is whether a textblock's Markdown, written as the
108// serialiser would and parsed as the server will, holds the same text,
109// links and embeds the block does. Link to a page and the date chip
110// (commands.mjs insertDate, build note 83) check a block with this
111// before they change it.
112export function readsBack (block) {
113 const children = []
114 block.forEach(child => children.push(child))
115 const now = counts(children)
116 const back = counts(inlineNodes(block.type.schema, inline(block)))
117 return now.text === back.text && now.links === back.links && now.embeds === back.embeds
118}
119
120// linkCheck is the transaction turning the selected words into a chip
121// to target, the caret after it, or a refusal: the selection is no
122// longer the words the field was opened over, or the link would not
123// read back as itself.
124export function linkCheck (state, target, words) {
125 const found = linkable(state)
126 if (!found) return { refuse: refusals.moved }
127 if (found.refuse) return found
128 if (found.words !== words) return { refuse: refusals.moved }
129 const shown = words === target ? '' : words
130 if (pageLinkSource(target, shown) === null) return { refuse: refusals.syntax }
131 const result = made(state, found, target)
132 if (result.refuse) return result
133 const after = result.tr.mapping.map(found.to)
134 return { tr: result.tr.setSelection(TextSelection.create(result.tr.doc, after)).scrollIntoView() }
135}
136
137// linkSelection is linkCheck as a command: false when it refuses.
138export function linkSelection (target, words) {
139 return (state, dispatch) => {
140 const { tr } = linkCheck(state, target, words)
141 if (!tr) return false
142 if (dispatch) dispatch(tr)
143 return true
144 }
145}
146
147// linkPage is the command ⌘L, Insert → Link to a page and the phone's
148// [[ run. Over a selection it asks open(found) to offer pages for it
149// (the bubble's field), or to say why it cannot; with none it types [[.
150// In a code block it does nothing, and still takes the key, so ⌘L never
151// reaches the browser's address bar from the page's text.
152export function linkPage (open) {
153 return (state, dispatch, view) => {
154 const found = linkable(state)
155 if (found) {
156 if (dispatch && view) open(view, found)
157 return true
158 }
159 const { $from, empty } = state.selection
160 if (!empty || $from.parent.type.spec.code || !$from.parent.inlineContent) return true
161 if (dispatch) dispatch(state.tr.insertText('[[').scrollIntoView())
162 return true
163 }
164}
165