@atlaskit/editor-plugin-tasks-and-decisions
Version:
Tasks and decisions plugin for @atlaskit/editor-core
92 lines (90 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.moveSelectedTaskListItems = moveSelectedTaskListItems;
var _lists = require("@atlaskit/editor-common/lists");
var _utils = require("@atlaskit/editor-common/utils");
var _taskListIndentation = require("../task-list-indentation");
var MAX_TASK_LIST_DEPTH = 6;
function moveSelectedTaskListItems(tr, indentDelta) {
var doc = tr.doc,
selection = tr.selection;
var schema = doc.type.schema;
var taskList = schema.nodes.taskList;
var $from = selection.$from,
$to = selection.$to;
var rootListResult = (0, _utils.findFarthestParentNode)(function (node) {
return node.type === taskList;
})($from);
if (!rootListResult) {
return null;
}
var rootListStart = rootListResult.pos;
var rootListEnd = rootListStart + rootListResult.node.nodeSize;
var flattenResult = (0, _taskListIndentation.flattenTaskList)({
doc: doc,
rootListStart: rootListStart,
rootListEnd: rootListEnd,
selectionFrom: $from.pos,
selectionTo: $to.pos,
indentDelta: indentDelta,
maxDepth: MAX_TASK_LIST_DEPTH
});
if (!flattenResult) {
return null;
}
var flattenedItems = flattenResult.items,
startIndex = flattenResult.startIndex,
endIndex = flattenResult.endIndex;
var _buildReplacementFrag = (0, _lists.buildReplacementFragment)({
items: flattenedItems,
schema: schema,
rebuildFn: _taskListIndentation.rebuildTaskList,
extractContentFn: function extractContentFn(item, s) {
// Extract task item content for breakout.
// taskItem has inline content, so wrap in a paragraph.
// blockTaskItem already has paragraph children.
var blockTaskItem = s.nodes.blockTaskItem;
if (!!blockTaskItem && item.node.type === blockTaskItem) {
// blockTaskItem children are already paragraphs/extensions
var children = [];
item.node.forEach(function (child) {
return children.push(child);
});
return children;
}
// Regular taskItem — wrap inline content in a paragraph
return [s.nodes.paragraph.create(null, item.node.content)];
}
}),
fragment = _buildReplacementFrag.fragment,
contentStartOffsets = _buildReplacementFrag.contentStartOffsets;
if (fragment.size === 0) {
return null;
}
// 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: flattenedItems,
startIndex: startIndex,
endIndex: endIndex,
originalFrom: $from.pos,
originalTo: $to.pos,
contentStartOffsets: narrowed.adjustedContentStartOffsets,
rootListStart: narrowed.start,
docSize: tr.doc.content.size
}),
from = _computeSelectionOffs.from,
to = _computeSelectionOffs.to;
(0, _lists.restoreSelection)({
tr: tr,
originalSelection: selection,
from: from,
to: to
});
tr.scrollIntoView();
return tr;
}