UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

50 lines (47 loc) • 1.46 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. */ const findActive = (node, pos, registry) => { var _root$isHidden; // By convention an element toolbar root uses its node type name as its key. const key = node.type.name; const root = registry.getComponent({ type: 'toolbar', key }); if (root && !((_root$isHidden = root.isHidden) !== null && _root$isHidden !== void 0 && _root$isHidden.call(root))) { return { components: registry.getComponents(key), key, node, 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 const getActiveElementToolbar = (state, registry) => { const { selection } = state; if (selection instanceof NodeSelection) { const active = findActive(selection.node, selection.from, registry); if (active) { return active; } } const { $from } = selection; for (let depth = $from.depth; depth > 0; depth--) { const active = findActive($from.node(depth), $from.before(depth), registry); if (active) { return active; } } };