UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

86 lines (83 loc) 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flattenListStep = void 0; var _model = require("@atlaskit/editor-prosemirror/model"); var _nodeChecks = require("../nodeChecks"); var extractNestedLists = function extractNestedLists(node, schema) { var items = []; var paragraph = schema.nodes.paragraph; var itemTypes = [schema.nodes.listItem, schema.nodes.taskItem, schema.nodes.decisionItem]; var _extract = function extract(currentNode) { currentNode.forEach(function (child) { if (itemTypes.some(function (type) { return child.type === type; })) { var contentWithoutNestedLists = []; var nestedLists = []; // Check if this item type expects inline content (taskItem, decisionItem) // vs block content (listItem) based on the schema definition var isInlineItem = child.type.inlineContent; child.forEach(function (grandChild) { if ((0, _nodeChecks.isListWithIndentation)(grandChild.type.name, schema)) { nestedLists.push(grandChild); } else if (grandChild.isInline) { // For taskItem/decisionItem, keep text as-is (they support inline content) // For listItem, wrap text in paragraph (they require block content) if (isInlineItem) { contentWithoutNestedLists.push(grandChild); } else { contentWithoutNestedLists.push(paragraph.createAndFill({}, grandChild)); } } else { contentWithoutNestedLists.push(grandChild); } }); items.push(child.copy(_model.Fragment.from(contentWithoutNestedLists))); nestedLists.forEach(function (nestedList) { _extract(nestedList); }); } else if ((0, _nodeChecks.isListWithIndentation)(child.type.name, schema)) { _extract(child); } }); }; _extract(node); return items; }; /** * Given an array of nodes, returns an array with the flattened children of any list node * to it's first ancestor list, maintaining document order. * * @example * Input (deeply nested): * - bulletList * - listItem "1" * - bulletList * - listItem "1.1" * - bulletList * - listItem "1.1.1" * - listItem "1.2" * - listItem "2" * * Output: * - bulletList * - listItem "1" * - listItem "1.1" * - listItem "1.1.1" * - listItem "1.2" * - listItem "2" * * @param nodes * @param context * @returns */ var flattenListStep = exports.flattenListStep = function flattenListStep(nodes, context) { return nodes.map(function (node) { if ((0, _nodeChecks.isListWithIndentation)(node.type.name, context.schema)) { return node.copy(_model.Fragment.from(extractNestedLists(node, context.schema))); } return node; }); };