UNPKG

@atlaskit/editor-plugin-list

Version:

List plugin for @atlaskit/editor-core

65 lines (63 loc) 2.33 kB
import { computeSelectionOffsets, narrowReplacementRange, restoreSelection } from '@atlaskit/editor-common/lists'; import { MAX_NESTED_LIST_INDENTATION } from '../../types'; import { findFirstParentListNode, findRootParentListNode } from '../utils/find'; import { buildReplacementFragment, flattenList } from '../utils/list-indentation'; /** * Moves the selected list items n levels up (negative delta) or down (positive delta). */ export function moveSelectedListItems(tr, indentDelta) { var originalSelection = tr.selection; var rootListResolved = findRootParentListNode(originalSelection.$from); if (!rootListResolved) { return; } var rootList = findFirstParentListNode(rootListResolved); if (!rootList) { return; } var rootListStart = rootList.pos; var rootListEnd = rootListStart + rootList.node.nodeSize; var result = flattenList({ doc: tr.doc, rootListStart: rootListStart, rootListEnd: rootListEnd, selectionFrom: originalSelection.from, selectionTo: originalSelection.to, indentDelta: indentDelta, maxDepth: MAX_NESTED_LIST_INDENTATION }); if (!result) { return; } var items = result.items, startIndex = result.startIndex, endIndex = result.endIndex; var _buildReplacementFrag = buildReplacementFragment(items, tr.doc.type.schema), fragment = _buildReplacementFrag.fragment, contentStartOffsets = _buildReplacementFrag.contentStartOffsets; if (fragment.size === 0) { return; } // Narrow the replacement to the minimal changed range for collab-friendly // cursor preservation on unaffected list items. var narrowed = narrowReplacementRange(tr.doc, rootListStart, rootListEnd, fragment, contentStartOffsets); tr.replaceWith(narrowed.start, narrowed.end, narrowed.fragment); var _computeSelectionOffs = computeSelectionOffsets({ items: items, startIndex: startIndex, endIndex: endIndex, originalFrom: originalSelection.from, originalTo: originalSelection.to, contentStartOffsets: narrowed.adjustedContentStartOffsets, rootListStart: narrowed.start, docSize: tr.doc.content.size }), from = _computeSelectionOffs.from, to = _computeSelectionOffs.to; restoreSelection({ tr: tr, originalSelection: originalSelection, from: from, to: to }); }