@atlaskit/editor-plugin-tasks-and-decisions
Version:
Tasks and decisions plugin for @atlaskit/editor-core
86 lines (85 loc) • 3.15 kB
JavaScript
import { buildReplacementFragment, computeSelectionOffsets, narrowReplacementRange, restoreSelection } from '@atlaskit/editor-common/lists';
import { findFarthestParentNode } from '@atlaskit/editor-common/utils';
import { flattenTaskList, rebuildTaskList } from '../task-list-indentation';
var MAX_TASK_LIST_DEPTH = 6;
export 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 = findFarthestParentNode(function (node) {
return node.type === taskList;
})($from);
if (!rootListResult) {
return null;
}
var rootListStart = rootListResult.pos;
var rootListEnd = rootListStart + rootListResult.node.nodeSize;
var flattenResult = 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 = buildReplacementFragment({
items: flattenedItems,
schema: schema,
rebuildFn: 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 = narrowReplacementRange(tr.doc, rootListStart, rootListEnd, fragment, contentStartOffsets);
tr.replaceWith(narrowed.start, narrowed.end, narrowed.fragment);
var _computeSelectionOffs = 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;
restoreSelection({
tr: tr,
originalSelection: selection,
from: from,
to: to
});
tr.scrollIntoView();
return tr;
}