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