@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
51 lines (50 loc) • 2.16 kB
JavaScript
import { NodeSelection, Selection, TextSelection } from '@atlaskit/editor-prosemirror/state';
import { findParentNodeClosestToPos } from '@atlaskit/editor-prosemirror/utils';
import { isListItemNode, isListNode } from '../utils';
export var numberNestedLists = function numberNestedLists(resolvedPos) {
var count = 0;
for (var i = resolvedPos.depth - 1; i > 0; i--) {
var node = resolvedPos.node(i);
if (isListNode(node)) {
count += 1;
}
}
return count;
};
export var getListItemAttributes = function getListItemAttributes($pos) {
// Get level for the correct indent of nesting
var indentLevel = numberNestedLists($pos) - 1;
var itemAtPos = findParentNodeClosestToPos($pos, isListItemNode);
// Get the index of the current item relative to parent (parent is at item depth - 1)
var itemIndex = $pos.index(itemAtPos ? itemAtPos.depth - 1 : undefined);
return {
indentLevel: indentLevel,
itemIndex: itemIndex
};
};
export var normalizeListItemsSelection = function normalizeListItemsSelection(_ref) {
var selection = _ref.selection,
doc = _ref.doc;
if (selection.empty) {
return selection;
}
var $from = selection.$from,
$to = selection.$to;
if (selection instanceof NodeSelection) {
var _head = resolvePositionToStartOfListItem($from);
return new TextSelection(_head, _head);
}
var head = resolvePositionToStartOfListItem($from);
var anchor = resolvePositionToEndOfListItem($to);
return new TextSelection(anchor, head);
};
var resolvePositionToStartOfListItem = function resolvePositionToStartOfListItem($pos) {
var fromRange = $pos.blockRange($pos, isListItemNode);
var fromPosition = fromRange && $pos.textOffset === 0 && fromRange.end - 1 === $pos.pos ? Selection.near($pos.doc.resolve(fromRange.end + 1), 1).$from : $pos;
return fromPosition;
};
var resolvePositionToEndOfListItem = function resolvePositionToEndOfListItem($pos) {
var toRange = $pos.blockRange($pos, isListItemNode);
var toPosition = toRange && $pos.textOffset === 0 && toRange.start + 1 === $pos.pos ? Selection.near($pos.doc.resolve(toRange.start - 1), -1).$to : $pos;
return toPosition;
};