简体   繁体   English

Wordpress 将类添加到核心块工作但给出验证错误

[英]Wordpress add classes to core block working but gives validation error

Following this guide from Jeffrey Carandang works with when a single class is added, no block validation errors.遵循Jeffrey Carandang 的本指南,在添加单个 class 时使用,没有块验证错误。

But when I modify it to add more attributes and classes I get a block validation error.但是当我修改它以添加更多属性和类时,我得到一个块验证错误。 在此处输入图像描述

The classes I want to add are added as expected on the front end.我要添加的类按预期添加到前端。

I see that a validation error is expected when using blocks.getSaveContent.extraProps on existing content but the error appears when there is no content at all.我看到在现有内容上使用blocks.getSaveContent.extraProps时会出现验证错误,但是当根本没有内容时会出现错误。

#imports

const allowedBlocks = ['core/button']

function addAttributes(settings) {
   if (allowedBlocks.includes(settings.name)) {
      settings.attributes = Object.assign(settings.attributes, {
         buttonColor: {
            type: 'string',
            default: 'color-blue',
         },
         buttonWidth: {
            type: 'string',
            default: 'width-default',
         },
         buttonStyle: {
            type: 'string',
            default: 'style-default',
         },
      })
   }

   return settings
}

const withAdvancedControls = createHigherOrderComponent((BlockEdit) => {
   return (props) => {
      const { name, attributes, setAttributes, isSelected } = props

      const { buttonColor, buttonWidth, buttonStyle } = attributes

      function onChangeButtonColor(newValue) {
         setAttributes({ buttonColor: newValue })
      }
      function onChangeButtonWidth(newValue) {
         setAttributes({ buttonWidth: newValue })
      }
      function onChangeButtonStyle(newValue) {
         setAttributes({ buttonStyle: newValue })
      }

      return (
         <Fragment>
            <BlockEdit {...props} />
            {isSelected && allowedBlocks.includes(name) && (
               <InspectorControls>
                  <PanelBody title="Button Settings" initialOpen={true}>
                     <PanelRow>
                        <RadioControl
                           label="Color"
                           selected={buttonColor}
                           options={[
                              { label: 'Blue', value: 'color-blue' },
                              {
                                 label: 'Light Gray',
                                 value: 'color-light-gray',
                              },
                              { label: 'Dark Gray', value: 'color-dark-gray' },
                           ]}
                           onChange={onChangeButtonColor}
                        />
                     </PanelRow>
                     <PanelRow>
                        <RadioControl
                           label="Width"
                           selected={buttonWidth}
                           options={[
                              { label: 'Default', value: 'width-default' },
                              { label: 'Full Width', value: 'width-full' },
                           ]}
                           onChange={onChangeButtonWidth}
                        />
                     </PanelRow>
                     <PanelRow>
                        <RadioControl
                           label="Style"
                           selected={buttonStyle}
                           options={[
                              { label: 'Default', value: 'style-default' },
                              { label: 'Outline', value: 'style-outline' },
                           ]}
                           onChange={onChangeButtonStyle}
                        />
                     </PanelRow>
                  </PanelBody>
               </InspectorControls>
            )}
         </Fragment>
      )
   }
}, 'withAdvancedControls')

function applyExtraClass(extraProps, blockType, attributes) {
   const { buttonColor, buttonWidth, buttonStyle } = attributes

   if (allowedBlocks.includes(blockType.name)) {
      const addedClasses = `${buttonColor} ${buttonWidth} ${buttonStyle}`
      extraProps.className = classnames(extraProps.className, addedClasses)
   }

   return extraProps
}

addFilter(
   'blocks.registerBlockType',
   'editorskit/custom-attributes',
   addAttributes
)

addFilter(
   'editor.BlockEdit',
   'editorskit/custom-advanced-control',
   withAdvancedControls
)

addFilter(
   'blocks.getSaveContent.extraProps',
   'editorskit/applyExtraClass',
   applyExtraClass
)

Any help would be appreciated.任何帮助,将不胜感激。

I believe you may be over-thinking things in your use of the classname NPM package.我相信您在使用名 NPM package 时可能会想太多。 The whole point of using it is that it strings the classes together for you.使用它的全部意义在于它为您将类串在一起。 I suggest you replace classnames(extraProps.className, addedClasses) with classnames(extraProps.className, buttonColor, buttonWidth, buttonStyle) .我建议您将classnames(extraProps.className, addedClasses)替换为classnames(extraProps.className, buttonColor, buttonWidth, buttonStyle)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM