UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

80 lines (78 loc) 2.66 kB
import { Fragment } from '@atlaskit/editor-prosemirror/model'; import { isListWithIndentation } from '../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 (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(Fragment.from(contentWithoutNestedLists))); nestedLists.forEach(function (nestedList) { _extract(nestedList); }); } else if (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 */ export var flattenListStep = function flattenListStep(nodes, context) { return nodes.map(function (node) { if (isListWithIndentation(node.type.name, context.schema)) { return node.copy(Fragment.from(extractNestedLists(node, context.schema))); } return node; }); };