UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

81 lines (76 loc) 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapBlockquoteToDecisionListStep = void 0; var _utils = require("../utils"); /** * Creates a decisionItem with the given inline content. */ var createDecisionItem = function createDecisionItem(inlineContent, schema) { var decisionItemType = schema.nodes.decisionItem; if (!decisionItemType) { return null; } var canContentBeWrappedInDecisionItem = decisionItemType.validContent(inlineContent); // Check if the content is valid for decisionItem if (!canContentBeWrappedInDecisionItem) { return null; } return decisionItemType.createAndFill({}, inlineContent); }; /** * Creates a decisionList containing the given decisionItems. */ var createDecisionListWithItems = function createDecisionListWithItems(decisionItems, schema) { var decisionListType = schema.nodes.decisionList; if (!decisionListType || decisionItems.length === 0) { return null; } return decisionListType.createAndFill({}, decisionItems); }; /** * Wraps blockquote content into decisionList: * - Text nodes (paragraph, heading) have their inline content extracted and wrapped in decisionItems * - Consecutive text nodes are grouped into a single decisionList with multiple decisionItems * - All other nodes break out (lists, code blocks, media, tables, macros, containers, etc.) * * The logic follows the transform rules: * - Only flatten text nodes into decisionItem (since that's the intent - converting text to decisions) * - Structures that can't be represented in a decisionItem should break out unchanged * - When a break-out node is encountered, flush accumulated decisionItems into a decisionList * * Example: blockquote(p('a'), p('b'), ul(...), p('c')) → [decisionList(decisionItem('a'), decisionItem('b')), ul(...), decisionList(decisionItem('c'))] */ var wrapBlockquoteToDecisionListStep = exports.wrapBlockquoteToDecisionListStep = function wrapBlockquoteToDecisionListStep(nodes, context) { var schema = context.schema; var decisionItemType = schema.nodes.decisionItem; if (!decisionItemType) { return nodes; } var result = []; var currentDecisionItems = []; var flushCurrentDecisionList = function flushCurrentDecisionList() { if (currentDecisionItems.length > 0) { var decisionList = createDecisionListWithItems(currentDecisionItems, schema); if (decisionList) { result.push(decisionList); } currentDecisionItems = []; } }; nodes.forEach(function (node) { var decisionItem = (0, _utils.isTextNode)(node) ? createDecisionItem(node.content, schema) : null; if (decisionItem) { // Accumulate consecutive decisionItems currentDecisionItems.push(decisionItem); } else { // Content can't be wrapped in decisionItem - break out the node flushCurrentDecisionList(); result.push(node); } }); // Flush any remaining decisionItems flushCurrentDecisionList(); return result.length > 0 ? result : nodes; };