UNPKG

@wordpress/block-editor

Version:
138 lines (131 loc) 4.64 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.addAttribute = addAttribute; exports.addSaveProps = addSaveProps; exports.addTransforms = addTransforms; exports.default = void 0; var _clsx = _interopRequireDefault(require("clsx")); var _hooks = require("@wordpress/hooks"); var _components = require("@wordpress/components"); var _i18n = require("@wordpress/i18n"); var _blocks = require("@wordpress/blocks"); var _components2 = require("../components"); var _blockEditingMode = require("../components/block-editing-mode"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal 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; } function CustomClassNameControlsPure({ className, setAttributes }) { const blockEditingMode = (0, _blockEditingMode.useBlockEditingMode)(); if (blockEditingMode !== 'default') { return null; } return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components2.InspectorControls, { group: "advanced", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, { __nextHasNoMarginBottom: true, __next40pxDefaultSize: true, autoComplete: "off", label: (0, _i18n.__)('Additional CSS class(es)'), value: className || '', onChange: nextValue => { setAttributes({ className: nextValue !== '' ? nextValue : undefined }); }, help: (0, _i18n.__)('Separate multiple classes with spaces.') }) }); } var _default = exports.default = { edit: CustomClassNameControlsPure, addSaveProps, attributeKeys: ['className'], hasSupport(name) { return (0, _blocks.hasBlockSupport)(name, 'customClassName', true); } }; /** * 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; } function addTransforms(result, source, index, results) { if (!(0, _blocks.hasBlockSupport)(result.name, 'customClassName', true)) { return result; } // If the condition verifies we are probably in the presence of a wrapping transform // e.g: nesting paragraphs in a group or columns and in that case the class should not be kept. if (results.length === 1 && result.innerBlocks.length === source.length) { return result; } // If we are transforming one block to multiple blocks or multiple blocks to one block, // we ignore the class during the transform. if (results.length === 1 && source.length > 1 || results.length > 1 && source.length === 1) { return result; } // If we are in presence of transform between one or more block in the source // that have one or more blocks in the result // we apply the class on source N to the result N, // if source N does not exists we do nothing. if (source[index]) { const originClassName = source[index]?.attributes.className; // Avoid overriding classes if the transformed block already includes them. if (originClassName && result.attributes.className === undefined) { return { ...result, attributes: { ...result.attributes, className: originClassName } }; } } return result; } (0, _hooks.addFilter)('blocks.registerBlockType', 'core/editor/custom-class-name/attribute', addAttribute); (0, _hooks.addFilter)('blocks.switchToBlockType.transformedBlock', 'core/color/addTransforms', addTransforms); //# sourceMappingURL=custom-class-name.js.map