UNPKG

@atlaskit/editor-plugin-annotation

Version:

Annotation plugin for @atlaskit/editor-core

48 lines (46 loc) • 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveAnnotatableTargetFromLocalId = void 0; var _nodesByLocalIds = require("@atlaskit/editor-common/utils/nodes-by-localIds"); var _utils = require("./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. */ var resolveAnnotatableTargetFromLocalId = exports.resolveAnnotatableTargetFromLocalId = function resolveAnnotatableTargetFromLocalId(state, localId, opts) { var supportedBlockNodes = opts.supportedBlockNodes; var matches = (0, _nodesByLocalIds.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) || (0, _utils.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; };