UNPKG

@gechiui/block-editor

Version:
96 lines (86 loc) 3.14 kB
import { createElement, Fragment } from "@gechiui/element"; /** * External dependencies */ import classnames from 'classnames'; /** * GeChiUI dependencies */ import { addFilter } from '@gechiui/hooks'; import { TextControl } from '@gechiui/components'; import { __ } from '@gechiui/i18n'; import { hasBlockSupport } from '@gechiui/blocks'; import { createHigherOrderComponent } from '@gechiui/compose'; /** * Internal dependencies */ import { InspectorControls } from '../components'; /** * 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. */ export function addAttribute(settings) { if (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. */ export const withInspectorControl = createHigherOrderComponent(BlockEdit => { return props => { const hasCustomClassName = hasBlockSupport(props.name, 'customClassName', true); if (hasCustomClassName && props.isSelected) { return createElement(Fragment, null, createElement(BlockEdit, props), createElement(InspectorControls, { __experimentalGroup: "advanced" }, createElement(TextControl, { autoComplete: "off", label: __('额外的CSS类'), value: props.attributes.className || '', onChange: nextValue => { props.setAttributes({ className: nextValue !== '' ? nextValue : undefined }); }, help: __('多个类请用空格分开') }))); } return 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. */ export function addSaveProps(extraProps, blockType, attributes) { if (hasBlockSupport(blockType, 'customClassName', true) && attributes.className) { extraProps.className = classnames(extraProps.className, attributes.className); } return extraProps; } addFilter('blocks.registerBlockType', 'core/custom-class-name/attribute', addAttribute); addFilter('editor.BlockEdit', 'core/editor/custom-class-name/with-inspector-control', withInspectorControl); addFilter('blocks.getSaveContent.extraProps', 'core/custom-class-name/save-props', addSaveProps); //# sourceMappingURL=custom-class-name.js.map