@gechiui/block-editor
Version:
118 lines (95 loc) • 3.67 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addAttribute = addAttribute;
exports.addSaveProps = addSaveProps;
exports.withInspectorControl = void 0;
var _element = require("@gechiui/element");
var _classnames = _interopRequireDefault(require("classnames"));
var _hooks = require("@gechiui/hooks");
var _components = require("@gechiui/components");
var _i18n = require("@gechiui/i18n");
var _blocks = require("@gechiui/blocks");
var _compose = require("@gechiui/compose");
var _components2 = require("../components");
/**
* External dependencies
*/
/**
* GeChiUI dependencies
*/
/**
* Internal dependencies
*/
/**
* Filters registered block settings, extending attributes with anchor using ID
* of the first node.
*
* @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 the default edit UI to include a new block inspector control for
* assigning the custom class name, if block supports custom class name.
*
* @param {GCComponent} BlockEdit Original component.
*
* @return {GCComponent} Wrapped component.
*/
const withInspectorControl = (0, _compose.createHigherOrderComponent)(BlockEdit => {
return props => {
const hasCustomClassName = (0, _blocks.hasBlockSupport)(props.name, 'customClassName', true);
if (hasCustomClassName && props.isSelected) {
return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(BlockEdit, props), (0, _element.createElement)(_components2.InspectorControls, {
__experimentalGroup: "advanced"
}, (0, _element.createElement)(_components.TextControl, {
autoComplete: "off",
label: (0, _i18n.__)('额外的CSS类'),
value: props.attributes.className || '',
onChange: nextValue => {
props.setAttributes({
className: nextValue !== '' ? nextValue : undefined
});
},
help: (0, _i18n.__)('多个类请用空格分开')
})));
}
return (0, _element.createElement)(BlockEdit, props);
};
}, 'withInspectorControl');
/**
* Override props assigned to save component to inject anchor ID, if block
* supports anchor. 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.
*/
exports.withInspectorControl = withInspectorControl;
function addSaveProps(extraProps, blockType, attributes) {
if ((0, _blocks.hasBlockSupport)(blockType, 'customClassName', true) && attributes.className) {
extraProps.className = (0, _classnames.default)(extraProps.className, attributes.className);
}
return extraProps;
}
(0, _hooks.addFilter)('blocks.registerBlockType', 'core/custom-class-name/attribute', addAttribute);
(0, _hooks.addFilter)('editor.BlockEdit', 'core/editor/custom-class-name/with-inspector-control', withInspectorControl);
(0, _hooks.addFilter)('blocks.getSaveContent.extraProps', 'core/custom-class-name/save-props', addSaveProps);
//# sourceMappingURL=custom-class-name.js.map