UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

41 lines 1.78 kB
import { Fragment, liftTarget, TextSelection, ReplaceAroundStep, NodeRange, Slice } from '../prosemirror'; /** * Function will lift list item following selection to level-1. */ export function liftFollowingList(state, from, to, rootListDepth, tr) { var listItem = state.schema.nodes.listItem; var lifted = false; tr.doc.nodesBetween(from, to, function (node, pos) { if (!lifted && node.type === listItem && pos > from) { lifted = true; var listDepth = rootListDepth + 3; while (listDepth > rootListDepth + 2) { var start = tr.doc.resolve(tr.mapping.map(pos)); listDepth = start.depth; var end = tr.doc.resolve(tr.mapping.map(pos + node.textContent.length)); var sel = new TextSelection(start, end); tr = liftListItem(state, sel, tr); } } }); return tr; } /** * Lift list item. */ function liftListItem(state, selection, tr) { var $from = selection.$from, $to = selection.$to; var nodeType = state.schema.nodes.listItem; var range = $from.blockRange($to, function (node) { return node.childCount && node.firstChild.type === nodeType; }); if (!range || range.depth < 2 || $from.node(range.depth - 1).type !== nodeType) { return tr; } var end = range.end; var endOfList = $to.end(range.depth); if (end < endOfList) { tr.step(new ReplaceAroundStep(end - 1, endOfList, end, endOfList, new Slice(Fragment.from(nodeType.create(null, range.parent.copy())), 1, 0), 1, true)); range = new NodeRange(tr.doc.resolve($from.pos), tr.doc.resolve(endOfList), range.depth); } return tr.lift(range, liftTarget(range)).scrollIntoView(); } //# sourceMappingURL=lists.js.map