@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
57 lines (54 loc) • 1.88 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 const unwrapExpandStep = (nodes, context) => {
const {
schema
} = context;
const outputNodes = [];
const {
expand,
nestedExpand
} = schema.nodes;
nodes.forEach(node => {
const isExpand = node.type.name === expand.name || node.type.name === nestedExpand.name;
if (isExpand) {
var _node$attrs;
const 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) {
const 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(child => {
if (child.type.name === nestedExpand.name) {
const expandNode = convertNestedExpandToExpand(child, schema);
if (expandNode) {
outputNodes.push(expandNode);
} else {
outputNodes.push(child);
}
} else {
outputNodes.push(child);
}
});
} else {
unwrapStep([node], context);
}
});
return outputNodes;
};