@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
46 lines (44 loc) • 1.67 kB
JavaScript
import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
export var isNestedNodeSelected = function isNestedNodeSelected(view) {
var selection = view.state.selection;
return selection instanceof NodeSelection && selection.$from.depth > 0;
};
export var isSelectionInNode = function isSelectionInNode(start, view) {
var node = view.state.doc.nodeAt(start);
if (node === null) {
return false;
}
var endPos = start + node.nodeSize;
var startPos = start;
var _view$state$selection = view.state.selection,
$from = _view$state$selection.$from,
$to = _view$state$selection.$to;
return $from.pos >= startPos && endPos >= $to.pos;
};
// checks if the selection is in a text node
export var isInTextSelection = function isInTextSelection(view) {
var selection = view.state.selection;
if (selection instanceof TextSelection) {
return true;
} else if (selection instanceof NodeSelection) {
// check if this is a node that can appear among text
return selection.node.isInline;
}
};
export var isNonEditableBlock = function isNonEditableBlock(start, view) {
var node = view.state.doc.nodeAt(start);
if (node === null) {
return false;
}
// We want to treat some elements as if they are non-editable blocks for
// the purposes of quick insert.
switch (node.type.name) {
// mediaSingle and mediaGroup always contain a media child node, which is
// both a block and an atom. mediaSingle can also have a caption child node
// which is an editable block.
case 'mediaGroup':
case 'mediaSingle':
return true;
}
return node.isBlock && (node.isAtom || node.isLeaf);
};