@wordpress/block-editor
Version:
66 lines (62 loc) • 2.03 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addAttribute = addAttribute;
exports.addSaveProps = addSaveProps;
exports.default = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _hooks = require("@wordpress/hooks");
var _blocks = require("@wordpress/blocks");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Filters registered block settings, extending attributes to include `className`.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
function addAttribute(settings) {
if ((0, _blocks.hasBlockSupport)(settings, 'customClassName', true)) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
className: {
type: 'string'
}
};
}
return settings;
}
/**
* Override props assigned to save component to inject the className, if block
* supports customClassName. This is only applied if the block's save result is an
* element and not a markup string.
*
* @param {Object} extraProps Additional props applied to save element.
* @param {Object} blockType Block type.
* @param {Object} attributes Current block attributes.
*
* @return {Object} Filtered props applied to save element.
*/
function addSaveProps(extraProps, blockType, attributes) {
if ((0, _blocks.hasBlockSupport)(blockType, 'customClassName', true) && attributes.className) {
extraProps.className = (0, _clsx.default)(extraProps.className, attributes.className);
}
return extraProps;
}
(0, _hooks.addFilter)('blocks.registerBlockType', 'core/custom-class-name/attribute', addAttribute);
var _default = exports.default = {
addSaveProps,
attributeKeys: ['className'],
hasSupport(name) {
return (0, _blocks.hasBlockSupport)(name, 'customClassName', true);
}
};
//# sourceMappingURL=custom-class-name.native.js.map
;