UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

46 lines (43 loc) • 1.51 kB
import { NodeSelection } from '@atlaskit/editor-prosemirror/state'; /** * The `uiControlRegistry` lookups the host needs. `getComponent` is an indexed lookup of one * component, and `getComponents` returns the cached components of one surface. */ var findActive = function findActive(node, pos, registry) { var _root$isHidden; // By convention an element toolbar root uses its node type name as its key. var key = node.type.name; var root = registry.getComponent({ type: 'toolbar', key: key }); if (root && !((_root$isHidden = root.isHidden) !== null && _root$isHidden !== void 0 && _root$isHidden.call(root))) { return { components: registry.getComponents(key), key: key, node: node, pos: pos }; } }; /** * Finds the element toolbar for the current selection: the selected node first, then the deepest * ancestor of the selection whose root is not hidden. Cost is O(selection depth) indexed root * lookups, then one surface lookup. */ export var getActiveElementToolbar = function getActiveElementToolbar(state, registry) { var selection = state.selection; if (selection instanceof NodeSelection) { var active = findActive(selection.node, selection.from, registry); if (active) { return active; } } var $from = selection.$from; for (var depth = $from.depth; depth > 0; depth--) { var _active = findActive($from.node(depth), $from.before(depth), registry); if (_active) { return _active; } } };