@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
70 lines (68 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.moveSelectedListItems = moveSelectedListItems;
var _lists = require("@atlaskit/editor-common/lists");
var _types = require("../../types");
var _find = require("../utils/find");
var _listIndentation = require("../utils/list-indentation");
/**
* Moves the selected list items n levels up (negative delta) or down (positive delta).
*/
function moveSelectedListItems(tr, indentDelta) {
var originalSelection = tr.selection;
var rootListResolved = (0, _find.findRootParentListNode)(originalSelection.$from);
if (!rootListResolved) {
return;
}
var rootList = (0, _find.findFirstParentListNode)(rootListResolved);
if (!rootList) {
return;
}
var rootListStart = rootList.pos;
var rootListEnd = rootListStart + rootList.node.nodeSize;
var result = (0, _listIndentation.flattenList)({
doc: tr.doc,
rootListStart: rootListStart,
rootListEnd: rootListEnd,
selectionFrom: originalSelection.from,
selectionTo: originalSelection.to,
indentDelta: indentDelta,
maxDepth: _types.MAX_NESTED_LIST_INDENTATION
});
if (!result) {
return;
}
var items = result.items,
startIndex = result.startIndex,
endIndex = result.endIndex;
var _buildReplacementFrag = (0, _listIndentation.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 = (0, _lists.narrowReplacementRange)(tr.doc, rootListStart, rootListEnd, fragment, contentStartOffsets);
tr.replaceWith(narrowed.start, narrowed.end, narrowed.fragment);
var _computeSelectionOffs = (0, _lists.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;
(0, _lists.restoreSelection)({
tr: tr,
originalSelection: originalSelection,
from: from,
to: to
});
}