@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
84 lines (82 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.decisionListToListStep = void 0;
var _model = require("@atlaskit/editor-prosemirror/model");
var targets = function targets(targetNodeTypeName, schema) {
var targetListType;
var targetItemType;
switch (targetNodeTypeName) {
case 'bulletList':
targetListType = schema.nodes.bulletList;
targetItemType = schema.nodes.listItem;
break;
case 'orderedList':
targetListType = schema.nodes.orderedList;
targetItemType = schema.nodes.listItem;
break;
case 'taskList':
targetListType = schema.nodes.taskList;
targetItemType = schema.nodes.taskItem;
break;
default:
}
return {
targetListType: targetListType,
targetItemType: targetItemType
};
};
/**
* Transforms a decisionList into a bulletList, orderedList, or taskList.
* Decision items are converted to the appropriate list item type.
*
* @example
* Input (decisionList):
* - decisionList
* - decisionItem "Task 1"
* - decisionItem "Task 2"
*
* Output (bulletList):
* - bulletList
* - listItem
* - paragraph "Task 1"
* - listItem
* - paragraph "Task 2"
*
* Output (taskList):
* - taskList
* - taskItem
* - paragraph "Task 1"
* - taskItem
* - paragraph "Task 2"
*
* @param nodes - Array of nodes to transform
* @param context - Transform context with schema and target node type
* @returns array of transformed nodes
*/
var decisionListToListStep = exports.decisionListToListStep = function decisionListToListStep(nodes, context) {
var schema = context.schema,
targetNodeTypeName = context.targetNodeTypeName;
var paragraphType = schema.nodes.paragraph;
return nodes.map(function (node) {
if (node.type !== schema.nodes.decisionList) {
return node;
}
var _targets = targets(targetNodeTypeName, schema),
targetListType = _targets.targetListType,
targetItemType = _targets.targetItemType;
if (!targetListType || !targetItemType) {
return node;
}
var newItems = [];
node.forEach(function (decisionItem) {
var newItem = targetItemType.inlineContent ? targetItemType.create({}, decisionItem.children) : targetItemType.create({}, paragraphType.create({}, decisionItem.children));
if (newItem) {
newItems.push(newItem);
}
});
var newList = targetListType.create({}, _model.Fragment.from(newItems));
return newList || node;
});
};