// chipview.mjs — a link chip in the editor (spec §4.2, §6.6). The chip // is drawn by the schema's own toDOM, so it is the server's markup; its // state (team, alias, pending, a date) comes from resolutions.mjs, never // from the node. When an answer arrives the chip is drawn again in // place, without a transaction: nothing in the document changed. import { DOMSerializer } from 'prosemirror-model' import { linkKey } from './resolutions.mjs' function keyOf (node) { return node.type.name === 'datelink' ? linkKey(node.attrs.date, node.attrs.anchor) : linkKey(node.attrs.target, node.attrs.anchor) } function draw (node) { return DOMSerializer.renderSpec(document, node.type.spec.toDOM(node)).dom } export class ChipView { constructor (node, resolutions) { this.node = node this.dom = draw(node) this.unwatch = resolutions.watch(keys => { if (keys.has(keyOf(this.node))) this.redraw() }) } // redraw takes the new drawing's attributes and children into the // element the view already holds. redraw () { const fresh = draw(this.node) if (fresh.nodeName !== this.dom.nodeName) return for (const { name } of [...this.dom.attributes]) { if (name !== 'contenteditable' && !fresh.hasAttribute(name)) this.dom.removeAttribute(name) } for (const { name, value } of fresh.attributes) this.dom.setAttribute(name, value) this.dom.replaceChildren(...fresh.childNodes) } update (node) { if (node.type !== this.node.type || keyOf(node) !== keyOf(this.node) || node.attrs.shown !== this.node.attrs.shown) return false this.node = node return true } // The chip is one atom: what happens inside it is not an edit. ignoreMutation () { return true } destroy () { this.unwatch() } } // chipViews is the view's nodeViews for every chip kind. export function chipViews (resolutions) { const make = node => new ChipView(node, resolutions) return { wikilink: make, datelink: make } } // chipHref is where a chip leads, read from its drawing. export function chipHref (view, pos) { const dom = view.nodeDOM(pos) return dom?.getAttribute?.('href') ?? null }