@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
47 lines (42 loc) • 2.14 kB
JavaScript
import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
import { ReplaceAroundStep } from '@atlaskit/editor-prosemirror/transform';
// adapted from https://github.com/ProseMirror/prosemirror-schema-list/blob/master/src/schema-list.js#L206:L231
export var indentList = function indentList(tr) {
var _tr$selection = tr.selection,
$from = _tr$selection.$from,
$to = _tr$selection.$to;
var listItem = tr.doc.type.schema.nodes.listItem;
var range = $from.blockRange($to, function (node) {
return !!node.childCount && !!node.firstChild && node.firstChild.type === listItem;
});
if (!range) {
return false;
}
// get the index of the selected list item in the list it is part of
var startIndex = range.startIndex;
if (startIndex === 0) {
return false;
}
// get the parent list of the list item(s) in the selected range
var parent = range.parent;
// get the list immediately before the selection start
var previousListItem = parent.child(startIndex - 1);
if (previousListItem.type !== listItem) {
return false;
}
// if that list was nested, join the selected list items into the same
// nested list; if not, create a new child list of the same type and
// nest it under the current level
var isPreviousListNested = previousListItem.lastChild && ['bulletList', 'orderedList'].includes(previousListItem.lastChild.type.name);
var inner = Fragment.from(isPreviousListNested ? listItem.create() : undefined);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
var nextListNodeType = isPreviousListNested ? previousListItem.lastChild.type : parent.type;
var nextListNodeContent = Fragment.from(nextListNodeType.create(null, inner));
var slice = new Slice(Fragment.from(listItem.create(null, nextListNodeContent)), isPreviousListNested ? 3 : 1, 0);
var before = range.start;
var after = range.end;
tr.step(new ReplaceAroundStep(before - (isPreviousListNested ? 3 : 1), after, before, after, slice, 1, true));
};