wordpress - 古腾堡 WordPress : Extending Core Block

标签 wordpress wordpress-gutenberg gutenberg-blocks

我正在尝试向新 Gutenberg Wordpress 编辑器的所有核心 block 添加填充检查器控件。我已经在编辑器上创建了控件,现在我尝试将此样式应用于 block 元素本身。

但我不断收到此 block 包含意外或无效内容。 block 上的错误。谁能帮我解决我到底没有做什么?

  var paddingEditor = wp.compose.createHigherOrderComponent(function(
    BlockEdit
  ) {
    return function(props) {
      var padding = props.attributes.padding || 0;
      handleChange = name => newValue => {
        if (props) {
          props.setAttributes({ [name]: newValue });
        }
      };
      return el(
        Fragment,
        {},
        el(BlockEdit, props),
        el(
          editor.InspectorControls,
          {},
          el(
            components.PanelBody,
            {
              title: "Padding",
              className: "",
              initialOpen: false
            },
            el("p", {}, "Padding"),
            el(components.TextControl, {
              value: padding,
              onChange: this.handleChange("padding")
            })
          )
        )
      );
    };
  },
  "paddingStyle");

  wp.hooks.addFilter(
    "editor.BlockEdit",
    "my-plugin/padding-style",
    paddingStyle
  );

  function AddPaddingAttribute(element, blockType, attributes) {
    Object.assign(blockType.attributes, {
      padding: {
        type: "string"
      }
    });

    return element;
  }

  wp.hooks.addFilter(
    "blocks.getSaveElement",
    "my-plugin/add-padding-attr",
    AddPaddingAttribute
  );

  function AddPaddingStyle(props, blockType, attributes) {
    if (attributes.padding) {
      props.style = lodash.assign(props.style, { padding: attributes.padding });
    }
    return props;
  }

  wp.hooks.addFilter(
    "blocks.getSaveContent.extraProps",
    "my-plugin/add-background-color-style",
    AddPaddingStyle
  );

PHP

function register_block_extensions(){

    wp_register_script(
        'extend-blocks', // Handle.
        get_template_directory_uri() . '/js/extend-blocks.js',
        array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor' )
    );
    wp_enqueue_script( 'extend-blocks' );

  }
  add_action('enqueue_block_editor_assets', 'register_block_extensions');

最佳答案

你的 editor.BlockEdit 看起来是正确的,但我很难解析旧的语法。假设它是正确的,您需要将blocks.getSaveElement替换为:

function AddPaddingAttribute(props) {
  if (props.attributes) { // Some modules don't have attributes
    props.attributes = Object.assign(
      props.attributes,
      {
        padding: {}
      }
    );
  }
  return props;
}
wp.hooks.addFilter(
  'blocks.registerBlockType',
  'my-plugin/add-padding-attr',
  AddPaddingAttribute
);

并将blocks.getSaveContent.extraProps修改为:

function AddPaddingStyle(props, blockType, attributes) {
  return Object.assign(
    props,
    {
      style: {
        padding: attributes.padding
      }
    }
  );
}
wp.hooks.addFilter(
  "blocks.getSaveContent.extraProps",
  "my-plugin/add-background-color-style",
  AddPaddingStyle
);

关于wordpress - 古腾堡 WordPress : Extending Core Block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679678/

相关文章:

wordpress-rest-api - 如何将 "getEntityRecords"用于特定的分类术语

php - 在 WordPress 上更新 mysql 不起作用

mysql - SQL 查询使用 SQL 查询从 WooCommerce 选择所有产品

WordPress 网站提示 "strict standards"

wordpress-gutenberg - 附加 CSS 类不适用于自定义古腾堡 block

带有 and 或 条件的 WordPress 分类查询

javascript - WP Block Styles - 选择 block 样式时触发JS

wordpress - 如何禁用核心 block 的工具栏?这可能吗?

wordpress - 有没有办法通过 theme.json 注册核心/按钮样式