@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
41 lines (40 loc) • 1.83 kB
JavaScript
import { isListWithTextContentOnly } from '../nodeChecks';
import { convertTextNodeToParagraph } from '../utils';
var wrapIntoTaskOrDecisionList = function wrapIntoTaskOrDecisionList(nodes, targetNodeTypeName, schema) {
var itemNodeType = targetNodeTypeName === 'taskList' ? schema.nodes.taskItem : schema.nodes.decisionItem;
var inlineContent = nodes.flatMap(function (node) {
if (node.isTextblock) {
return node.children;
} else if (node.isText) {
return [node];
}
return [];
});
var itemNode = itemNodeType.create({}, inlineContent);
var outputNode = schema.nodes[targetNodeTypeName].createAndFill({}, itemNode);
return outputNode ? [outputNode] : nodes;
};
var wrapIntoBulletOrOrderedList = function wrapIntoBulletOrOrderedList(nodes, targetNodeTypeName, schema) {
var listItemNodes = nodes.map(function (node) {
return schema.nodes.listItem.createAndFill({}, node.isTextblock ? convertTextNodeToParagraph(node, schema) : node);
}).filter(function (node) {
return node !== null;
});
if (listItemNodes.length === 0) {
return nodes;
}
var outputNode = schema.nodes[targetNodeTypeName].createAndFill({}, listItemNodes);
return outputNode ? [outputNode] : nodes;
};
/**
* Wraps nodes into bullet list, numbered list, task list, or decision list.
*
* @param nodes - The nodes to wrap.
* @param context - The transformation context containing schema and target node type.
* @returns The wrapped nodes.
*/
export var wrapIntoListStep = function wrapIntoListStep(nodes, context) {
var schema = context.schema,
targetNodeTypeName = context.targetNodeTypeName;
return isListWithTextContentOnly(targetNodeTypeName, schema) ? wrapIntoTaskOrDecisionList(nodes, targetNodeTypeName, schema) : wrapIntoBulletOrOrderedList(nodes, targetNodeTypeName, schema);
};