// anchors.mjs — a block's ^id (spec §6.3, §6.12): a ¶ button at the // block's start, shown on hover and on focus, "Link to this block", // which copies the block's link ([[Title#^id]]) to paste anywhere in // Mapa. It sits outside the text's flow, so drawing it moves nothing. // Markers are minted only by File → Link to a block, on a team page. import { Plugin } from 'prosemirror-state' import { Decoration, DecorationSet } from 'prosemirror-view' function handle (marker, title) { const button = document.createElement('button') button.type = 'button' button.setAttribute('mapa-anchor', '') button.setAttribute('contenteditable', 'false') button.setAttribute('aria-label', 'Link to this block') button.title = 'Copy link to this block' button.textContent = '¶' button.addEventListener('mousedown', event => event.preventDefault()) button.addEventListener('click', async () => { try { await navigator.clipboard.writeText(`[[${title()}#^${marker}]]`) button.setAttribute('data-copied', '') setTimeout(() => button.removeAttribute('data-copied'), 1500) } catch {} }) return button } export function anchorsPlugin ({ title }) { const draw = doc => { const decorations = [] doc.descendants((node, pos) => { const marker = node.attrs?.marker if (marker && node.type.name !== 'list_item' && node.type.name !== 'blockquote') { decorations.push(Decoration.widget(pos + 1, () => handle(marker, title), { side: -1, key: `anchor-${marker}`, ignoreSelection: true, stopEvent: () => true })) } if (marker && (node.type.name === 'list_item' || node.type.name === 'blockquote')) { decorations.push(Decoration.widget(pos + 2, () => handle(marker, title), { side: -1, key: `anchor-${marker}`, ignoreSelection: true, stopEvent: () => true })) } }) return DecorationSet.create(doc, decorations) } return new Plugin({ state: { init: (config, state) => draw(state.doc), apply: (tr, set) => tr.docChanged ? draw(tr.doc) : set }, props: { decorations (state) { return this.getState(state) } } }) }