{"version":3,"file":"mention-node-view.cjs","sources":["../../../components/editor/mention-node-view.ts"],"sourcesContent":["import type { Node as PMNode } from 'prosemirror-model';\nimport type { NodeView } from 'prosemirror-view';\nimport styles from './editor.module.css';\nimport type { MentionAttrs } from './mention';\nimport { mentionType } from './schema';\n\n/**\n * One live chip. The node view owns the element and writes the label into it\n * synchronously — there is never an empty chip frame — while React portals the\n * consumer's `icon` and `trailing` nodes into the two slots, so those render in\n * the host tree and see Theme and any other provider context.\n */\nexport interface MentionPortal {\n  /** Stable React key for the lifetime of this node view. */\n  id: number;\n  attrs: MentionAttrs;\n  iconTarget: HTMLElement;\n  trailingTarget: HTMLElement;\n}\n\nexport interface MentionPortalRegistry {\n  add: (portal: MentionPortal) => void;\n  update: (id: number, attrs: MentionAttrs) => void;\n  remove: (id: number) => void;\n}\n\nlet nextPortalId = 0;\n\nexport class MentionNodeView implements NodeView {\n  readonly dom: HTMLElement;\n\n  private readonly id = (nextPortalId += 1);\n  private readonly label: HTMLElement;\n  private readonly iconTarget: HTMLElement;\n  private readonly trailingTarget: HTMLElement;\n  private readonly registry: MentionPortalRegistry;\n\n  constructor(node: PMNode, registry: MentionPortalRegistry) {\n    this.registry = registry;\n\n    const root = document.createElement('span');\n    root.className = styles.mention;\n    root.contentEditable = 'false';\n    root.draggable = false;\n    root.setAttribute('data-mention', '');\n\n    this.iconTarget = document.createElement('span');\n    this.iconTarget.className = styles.mentionIcon;\n    this.iconTarget.setAttribute('aria-hidden', 'true');\n\n    this.label = document.createElement('span');\n    this.label.className = styles.mentionLabel;\n\n    this.trailingTarget = document.createElement('span');\n    this.trailingTarget.className = styles.mentionTrailing;\n\n    root.append(this.iconTarget, this.label, this.trailingTarget);\n    this.dom = root;\n\n    this.write(node.attrs as MentionAttrs);\n    registry.add({\n      id: this.id,\n      attrs: node.attrs as MentionAttrs,\n      iconTarget: this.iconTarget,\n      trailingTarget: this.trailingTarget\n    });\n  }\n\n  private write(attrs: MentionAttrs) {\n    // The chip reads as the entity, not as the syntax that picked it: the\n    // trigger stays in the serialized text and markup, where the mention\n    // boundary has to survive, but the pill itself carries the label alone.\n    this.label.textContent = attrs.label;\n    this.dom.setAttribute('data-mention-id', attrs.id);\n    this.dom.setAttribute('data-mention-type', attrs.type);\n    this.dom.setAttribute('data-mention-trigger', attrs.trigger);\n    this.dom.setAttribute('aria-label', `mention: ${attrs.label}`);\n    this.dom.title = attrs.label;\n  }\n\n  update(node: PMNode) {\n    if (node.type !== mentionType) return false;\n    this.write(node.attrs as MentionAttrs);\n    this.registry.update(this.id, node.attrs as MentionAttrs);\n    return true;\n  }\n\n  selectNode() {\n    this.dom.setAttribute('data-selected', '');\n  }\n\n  deselectNode() {\n    this.dom.removeAttribute('data-selected');\n  }\n\n  /**\n   * Swallowing `dragstart` is what keeps a chip from being dropped into the\n   * middle of a word; a text selection that happens to contain one still\n   * drags, which is the platform behavior.\n   */\n  stopEvent(event: Event) {\n    return event.type === 'dragstart';\n  }\n\n  /** React writes into the slots; those mutations are never document edits. */\n  ignoreMutation() {\n    return true;\n  }\n\n  destroy() {\n    this.registry.remove(this.id);\n  }\n}\n"],"names":["styles","mentionType"],"mappings":";;;;;AA0BA,IAAI,YAAY,GAAG,CAAC,CAAC;MAER,eAAe,CAAA;AACjB,IAAA,GAAG,CAAc;AAET,IAAA,EAAE,IAAI,YAAY,IAAI,CAAC,EAAE;AACzB,IAAA,KAAK,CAAc;AACnB,IAAA,UAAU,CAAc;AACxB,IAAA,cAAc,CAAc;AAC5B,IAAA,QAAQ,CAAwB;IAEjD,WAAY,CAAA,IAAY,EAAE,QAA+B,EAAA;AACvD,QAAA,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAEzB,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;AAC5C,QAAA,IAAI,CAAC,SAAS,GAAGA,qBAAM,CAAC,OAAO,CAAC;AAChC,QAAA,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC;AAC/B,QA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