@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
39 lines (38 loc) • 1.68 kB
JavaScript
import { isListWithTextContentOnly } from '../nodeChecks';
import { convertTextNodeToParagraph } from '../utils';
const wrapIntoTaskOrDecisionList = (nodes, targetNodeTypeName, schema) => {
const itemNodeType = targetNodeTypeName === 'taskList' ? schema.nodes.taskItem : schema.nodes.decisionItem;
const inlineContent = nodes.flatMap(node => {
if (node.isTextblock) {
return node.children;
} else if (node.isText) {
return [node];
}
return [];
});
const itemNode = itemNodeType.create({}, inlineContent);
const outputNode = schema.nodes[targetNodeTypeName].createAndFill({}, itemNode);
return outputNode ? [outputNode] : nodes;
};
const wrapIntoBulletOrOrderedList = (nodes, targetNodeTypeName, schema) => {
const listItemNodes = nodes.map(node => schema.nodes.listItem.createAndFill({}, node.isTextblock ? convertTextNodeToParagraph(node, schema) : node)).filter(node => node !== null);
if (listItemNodes.length === 0) {
return nodes;
}
const 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 const wrapIntoListStep = (nodes, context) => {
const {
schema,
targetNodeTypeName
} = context;
return isListWithTextContentOnly(targetNodeTypeName, schema) ? wrapIntoTaskOrDecisionList(nodes, targetNodeTypeName, schema) : wrapIntoBulletOrOrderedList(nodes, targetNodeTypeName, schema);
};