@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
96 lines • 3.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var commands = require("../../commands");
var baseListCommand = require("../../prosemirror/prosemirror-schema-list");
var lists_1 = require("../../commands/lists");
exports.enterKeyCommand = function (state, dispatch) {
var selection = state.selection;
if (selection.empty) {
var $from = selection.$from;
var listItem = state.schema.nodes.listItem;
var node = $from.node($from.depth);
var wrapper = $from.node($from.depth - 1);
if (wrapper.type === listItem) {
if (node.textContent.length === 0) {
return commands.outdentList()(state, dispatch);
}
else {
return baseListCommand.splitListItem(listItem)(state, dispatch);
}
}
}
return false;
};
exports.toggleList = function (state, dispatch, view, listType) {
var selection = state.selection;
var _a = state.schema.nodes, bulletList = _a.bulletList, orderedList = _a.orderedList, listItem = _a.listItem;
var fromNode = selection.$from.node(selection.$from.depth - 2);
var endNode = selection.$to.node(selection.$to.depth - 2);
if ((!fromNode || fromNode.type.name !== listType) ||
(!endNode || endNode.type.name !== listType)) {
return commands.toggleList(listType)(state, dispatch, view);
}
else {
var rootListDepth = void 0;
for (var i = selection.$to.depth - 1; i > 0; i--) {
var node = selection.$to.node(i);
if (node.type === bulletList || node.type === orderedList) {
rootListDepth = i;
}
if (node.type !== bulletList && node.type !== orderedList && node.type !== listItem) {
break;
}
}
var tr = lists_1.liftFollowingList(state, selection.$to.pos, selection.$to.end(rootListDepth), rootListDepth, state.tr);
tr = liftSelectionList(state, tr);
dispatch(tr);
return true;
}
};
/**
* The function will list paragraphs in selection out to level 1 below root list.
*/
function liftSelectionList(state, tr) {
var _a = state.selection, from = _a.from, to = _a.to;
var paragraph = state.schema.nodes.paragraph;
var listCol = [];
tr.doc.nodesBetween(from, to, function (node, pos) {
if (node.type === paragraph) {
listCol.push({ node: node, pos: pos });
}
});
for (var i = listCol.length - 1; i >= 0; i--) {
var paragraph_1 = listCol[i];
var start = tr.doc.resolve(tr.mapping.map(paragraph_1.pos));
if (start.depth > 0) {
var end = void 0;
if (paragraph_1.node.textContent && paragraph_1.node.textContent.length > 0) {
end = tr.doc.resolve(tr.mapping.map(paragraph_1.pos + paragraph_1.node.textContent.length));
}
else {
end = tr.doc.resolve(tr.mapping.map(paragraph_1.pos + 1));
}
var range = start.blockRange(end);
tr.lift(range, listLiftTarget(state.schema, start));
}
}
return tr;
}
/**
* This will return (depth - 1) for root list parent of a list.
*/
function listLiftTarget(schema, resPos) {
var target = resPos.depth;
var _a = schema.nodes, bulletList = _a.bulletList, orderedList = _a.orderedList, listItem = _a.listItem;
for (var i = resPos.depth; i > 0; i--) {
var node = resPos.node(i);
if (node.type === bulletList || node.type === orderedList) {
target = i;
}
if (node.type !== bulletList && node.type !== orderedList && node.type !== listItem) {
break;
}
}
return target - 1;
}
//# sourceMappingURL=commands.js.map