@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
54 lines (51 loc) • 1.96 kB
JavaScript
import { convertNestedExpandToExpand } from '../utils';
import { unwrapStep } from './unwrapStep';
/**
* Unwraps an expand/nestedExpand node, converting its title attribute to a paragraph
* and prepending it to the children.
*
* Any nestedExpand children are converted to regular expands since nestedExpand
* can only exist inside an expand.
*
* Example: expand({ title: 'title' })(p('b')) → [p('title'), p('b')]
* Example: expand({ title: 'outer' })(nestedExpand({ title: 'inner' })(p('x')))
* → [p('outer'), expand({ title: 'inner' })(p('x'))]
*/
export var unwrapExpandStep = function unwrapExpandStep(nodes, context) {
var schema = context.schema;
var outputNodes = [];
var _schema$nodes = schema.nodes,
expand = _schema$nodes.expand,
nestedExpand = _schema$nodes.nestedExpand;
nodes.forEach(function (node) {
var isExpand = node.type.name === expand.name || node.type.name === nestedExpand.name;
if (isExpand) {
var _node$attrs;
var title = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.title;
// Create a paragraph from the title if it exists
if (title) {
var titleParagraph = schema.nodes.paragraph.createAndFill({}, schema.text(title));
if (titleParagraph) {
outputNodes.push(titleParagraph);
}
}
// Add the children, converting any nestedExpands to regular expands
// since nestedExpand can only exist inside an expand
node.children.forEach(function (child) {
if (child.type.name === nestedExpand.name) {
var expandNode = convertNestedExpandToExpand(child, schema);
if (expandNode) {
outputNodes.push(expandNode);
} else {
outputNodes.push(child);
}
} else {
outputNodes.push(child);
}
});
} else {
unwrapStep([node], context);
}
});
return outputNodes;
};