UNPKG

@atlaskit/editor-plugin-annotation

Version:

Annotation plugin for @atlaskit/editor-core

44 lines (42 loc) • 1.41 kB
import { findNodePosByLocalIds } from '@atlaskit/editor-common/utils/nodes-by-localIds'; import { isSupportedBlockNode } from './utils'; /** * Resolves the node to annotate for a `localId`: the node itself when it accepts an annotation * (allows the mark, or is a supported block node), otherwise its closest accepting ancestor. * Returns undefined when nothing in the ancestor chain accepts one. * * Kept in sync with the renderer implementation so both sides resolve the same node. */ export const resolveAnnotatableTargetFromLocalId = (state, localId, opts) => { const { supportedBlockNodes } = opts; const matches = findNodePosByLocalIds(state, [localId]); if (matches.length === 0) { return undefined; } const annotationMarkType = state.schema.marks.annotation; const nodeAcceptsAnnotation = node => node.type.allowsMarkType(annotationMarkType) || isSupportedBlockNode(node, supportedBlockNodes); const { node, pos } = matches[0]; if (nodeAcceptsAnnotation(node)) { return { node, pos }; } // Ancestors only — never descendants. const $pos = state.doc.resolve(pos); for (let depth = $pos.depth; depth >= 0; depth--) { const ancestor = $pos.node(depth); if (nodeAcceptsAnnotation(ancestor)) { return { node: ancestor, pos: depth === 0 ? 0 : $pos.before(depth) }; } } return undefined; };