@gechiui/block-editor
Version:
66 lines (56 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getRetainedBlockAttributes = exports.getMatchingBlockByName = void 0;
var _blocks = require("@gechiui/blocks");
/**
* GeChiUI dependencies
*/
/**
* Try to find a matching block by a block's name in a provided
* block. We recurse through InnerBlocks and return the reference
* of the matched block (it could be an InnerBlock).
* If no match is found return nothing.
*
* @param {GCBlock} block The block to try to find a match.
* @param {string} selectedBlockName The block's name to use for matching condition.
* @param {Set} consumedBlocks A set holding the previously matched/consumed blocks.
*
* @return {GCBlock?} The matched block if found or nothing(`undefined`).
*/
const getMatchingBlockByName = function (block, selectedBlockName) {
let consumedBlocks = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new Set();
const {
clientId,
name,
innerBlocks = []
} = block; // Check if block has been consumed already.
if (consumedBlocks.has(clientId)) return;
if (name === selectedBlockName) return block; // Try to find a matching block from InnerBlocks recursively.
for (const innerBlock of innerBlocks) {
const match = getMatchingBlockByName(innerBlock, selectedBlockName, consumedBlocks);
if (match) return match;
}
};
/**
* Find and return the block attributes to retain through
* the transformation, based on Block Type's `role:content`
* attributes. If no `role:content` attributes exist,
* return selected block's attributes.
*
* @param {string} name Block type's namespaced name.
* @param {Object} attributes Selected block's attributes.
* @return {Object} The block's attributes to retain.
*/
exports.getMatchingBlockByName = getMatchingBlockByName;
const getRetainedBlockAttributes = (name, attributes) => {
const contentAttributes = (0, _blocks.__experimentalGetBlockAttributesNamesByRole)(name, 'content');
if (!(contentAttributes !== null && contentAttributes !== void 0 && contentAttributes.length)) return attributes;
return contentAttributes.reduce((_accumulator, attribute) => {
if (attributes[attribute]) _accumulator[attribute] = attributes[attribute];
return _accumulator;
}, {});
};
exports.getRetainedBlockAttributes = getRetainedBlockAttributes;
//# sourceMappingURL=utils.js.map