@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
81 lines (80 loc) • 2.84 kB
JavaScript
import { GapCursorSelection } from '@atlaskit/editor-common/selection';
import { isListItemNode, isListNode, isParagraphNode } from '@atlaskit/editor-common/utils';
import { findWrapping } from '@atlaskit/editor-prosemirror/transform';
import { findParentNodeOfType, hasParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
export var isPosInsideParagraph = function isPosInsideParagraph($pos) {
return $pos.parent.type.name === 'paragraph';
};
export var isPosInsideList = function isPosInsideList($pos) {
var posGrandParent = $pos.node(-1);
return isListItemNode($pos.parent) || isListNode($pos.parent) || isListItemNode(posGrandParent);
};
export var isWrappingPossible = function isWrappingPossible(nodeType, selection) {
var $from = selection.$from,
$to = selection.$to;
var range;
if (selection instanceof GapCursorSelection && $from.nodeAfter) {
var nodeSize = $from.nodeAfter.nodeSize || 1;
range = $from.blockRange($from.doc.resolve($from.pos + nodeSize));
} else {
range = $from.blockRange($to);
}
if (!range) {
return false;
}
var wrap = findWrapping(range, nodeType);
if (!wrap) {
return false;
}
return true;
};
// canOutdent
export var isInsideListItem = function isInsideListItem(tr) {
var parent = tr.selection.$from.parent;
var listItem = tr.doc.type.schema.nodes.listItem;
if (tr.selection instanceof GapCursorSelection) {
return isListItemNode(parent);
}
return hasParentNodeOfType(listItem)(tr.selection) && isParagraphNode(parent);
};
export var isInsideTableCell = function isInsideTableCell(tr) {
var _tr$doc$type$schema$n = tr.doc.type.schema.nodes,
tableCell = _tr$doc$type$schema$n.tableCell,
tableHeader = _tr$doc$type$schema$n.tableHeader;
return !!findParentNodeOfType([tableCell, tableHeader])(tr.selection);
};
export var canJoinToPreviousListItem = function canJoinToPreviousListItem(tr) {
var $from = tr.selection.$from;
var $before = tr.doc.resolve($from.pos - 1);
var nodeBefore = $before ? $before.nodeBefore : null;
if (tr.selection instanceof GapCursorSelection) {
nodeBefore = $from.nodeBefore;
}
return isListNode(nodeBefore);
};
export var selectionContainsList = function selectionContainsList(tr) {
var _tr$selection = tr.selection,
from = _tr$selection.from,
to = _tr$selection.to;
var foundListNode = null;
tr.doc.nodesBetween(from, to, function (node) {
if (isListNode(node)) {
foundListNode = node;
}
if (foundListNode) {
return false;
}
return true;
});
return foundListNode;
};
export var createListNodeRange = function createListNodeRange(_ref) {
var selection = _ref.selection;
var $from = selection.$from,
$to = selection.$to;
var range = $from.blockRange($to, isListNode);
if (!range) {
return null;
}
return range;
};