UNPKG

@atlaskit/editor-plugin-annotation

Version:

Annotation plugin for @atlaskit/editor-core

43 lines (41 loc) • 1.54 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 var resolveAnnotatableTargetFromLocalId = function resolveAnnotatableTargetFromLocalId(state, localId, opts) { var supportedBlockNodes = opts.supportedBlockNodes; var matches = findNodePosByLocalIds(state, [localId]); if (matches.length === 0) { return undefined; } var annotationMarkType = state.schema.marks.annotation; var nodeAcceptsAnnotation = function nodeAcceptsAnnotation(node) { return node.type.allowsMarkType(annotationMarkType) || isSupportedBlockNode(node, supportedBlockNodes); }; var _matches$ = matches[0], node = _matches$.node, pos = _matches$.pos; if (nodeAcceptsAnnotation(node)) { return { node: node, pos: pos }; } // Ancestors only — never descendants. var $pos = state.doc.resolve(pos); for (var depth = $pos.depth; depth >= 0; depth--) { var ancestor = $pos.node(depth); if (nodeAcceptsAnnotation(ancestor)) { return { node: ancestor, pos: depth === 0 ? 0 : $pos.before(depth) }; } } return undefined; };