oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1// anchors.mjs — a block's ^id (spec §6.3, §6.12): a ¶ button at the
2// block's start, shown on hover and on focus, "Link to this block",
3// which copies the block's link ([[Title#^id]]) to paste anywhere in
4// Mapa. It sits outside the text's flow, so drawing it moves nothing.
5// Markers are minted only by File → Link to a block, on a team page.
6
7import { Plugin } from 'prosemirror-state'
8import { Decoration, DecorationSet } from 'prosemirror-view'
9
10function handle (marker, title) {
11 const button = document.createElement('button')
12 button.type = 'button'
13 button.setAttribute('mapa-anchor', '')
14 button.setAttribute('contenteditable', 'false')
15 button.setAttribute('aria-label', 'Link to this block')
16 button.title = 'Copy link to this block'
17 button.textContent = '¶'
18 button.addEventListener('mousedown', event => event.preventDefault())
19 button.addEventListener('click', async () => {
20 try {
21 await navigator.clipboard.writeText(`[[${title()}#^${marker}]]`)
22 button.setAttribute('data-copied', '')
23 setTimeout(() => button.removeAttribute('data-copied'), 1500)
24 } catch {}
25 })
26 return button
27}
28
29export function anchorsPlugin ({ title }) {
30 const draw = doc => {
31 const decorations = []
32 doc.descendants((node, pos) => {
33 const marker = node.attrs?.marker
34 if (marker && node.type.name !== 'list_item' && node.type.name !== 'blockquote') {
35 decorations.push(Decoration.widget(pos + 1, () => handle(marker, title),
36 { side: -1, key: `anchor-${marker}`, ignoreSelection: true, stopEvent: () => true }))
37 }
38 if (marker && (node.type.name === 'list_item' || node.type.name === 'blockquote')) {
39 decorations.push(Decoration.widget(pos + 2, () => handle(marker, title),
40 { side: -1, key: `anchor-${marker}`, ignoreSelection: true, stopEvent: () => true }))
41 }
42 })
43 return DecorationSet.create(doc, decorations)
44 }
45 return new Plugin({
46 state: {
47 init: (config, state) => draw(state.doc),
48 apply: (tr, set) => tr.docChanged ? draw(tr.doc) : set
49 },
50 props: { decorations (state) { return this.getState(state) } }
51 })
52}
53