最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Replication of core column block produces different markup

programmeradmin2浏览0评论

I've scaffolded a custom block via the official create-block package, I then went and pulled the code from the core block column from Github. And it seems to be working with some minor issues. For some reason my custom block gets a slightly different element structure where the block parent element gets nested within itself:

Whereas the core column block does not:



Any idea why this is happening? Relevant code below.

index.js

/**
 * WordPress dependencies
 */
import { registerBlockType } from '@wordpress/blocks';
import { __, _x } from '@wordpress/i18n';
import { column as icon } from '@wordpress/icons';

/**
 * Internal dependencies
 */
// import deprecated from './deprecated';
import edit from './edit';
import metadata from '../block.json';
import save from './save';

const { name } = metadata;

export { metadata, name };

export const settings = {
  title: _x( '12 Column', 'block title' ),
  icon,
  description: __( 'A single column within a columns block.' ),
  edit,
  save,
  // deprecated,
};

registerBlockType( 'wav/twelve-column', settings );

edit.js

/**
 * External dependencies
 */
import classnames from 'classnames';

/**
 * WordPress dependencies
 */
import {
  InnerBlocks,
  BlockControls,
  BlockVerticalAlignmentToolbar,
  InspectorControls,
  useBlockProps,
  __experimentalUseInnerBlocksProps as useInnerBlocksProps,
  store as blockEditorStore,
} from '@wordpress/block-editor';
import {
  PanelBody,
  __experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
 * Internal dependencies
 */
import { CSS_UNITS } from './utils';

function ColumnEdit( {
  attributes: { verticalAlignment, width, templateLock = false },
  setAttributes,
  clientId,
} ) {
  const classes = classnames( 'block-core-columns', {
    [ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
  } );

  const { hasChildBlocks, rootClientId } = useSelect(
    ( select ) => {
      const { getBlockOrder, getBlockRootClientId } = select(
        blockEditorStore
      );

      return {
        hasChildBlocks: getBlockOrder( clientId ).length > 0,
        rootClientId: getBlockRootClientId( clientId ),
      };
    },
    [ clientId ]
  );

  const { updateBlockAttributes } = useDispatch( blockEditorStore );

  const updateAlignment = ( value ) => {
    // Update own alignment.
    setAttributes( { verticalAlignment: value } );
    // Reset parent Columns block.
    updateBlockAttributes( rootClientId, {
      verticalAlignment: null,
    } );
  };

  const widthWithUnit = Number.isFinite( width ) ? width + '%' : width;
  const blockProps = useBlockProps( {
    className: classes,
    style: widthWithUnit ? { flexBasis: widthWithUnit } : undefined,
  } );
  const innerBlocksProps = useInnerBlocksProps( blockProps, {
    templateLock,
    renderAppender: hasChildBlocks
      ? undefined
      : InnerBlocks.ButtonBlockAppender,
  } );

  return (
    <>
      <BlockControls>
        <BlockVerticalAlignmentToolbar
          onChange={ updateAlignment }
          value={ verticalAlignment }
        />
      </BlockControls>
      <InspectorControls>
        <PanelBody title={ __( 'Column settings' ) }>
          <UnitControl
            label={ __( 'Width' ) }
            labelPosition="edge"
            __unstableInputWidth="80px"
            value={ width || '' }
            onChange={ ( nextWidth ) => {
              nextWidth =
                0 > parseFloat( nextWidth ) ? '0' : nextWidth;
              setAttributes( { width: nextWidth } );
            } }
            units={ CSS_UNITS }
          />
        </PanelBody>
      </InspectorControls>
      <div { ...innerBlocksProps } />
    </>
  );
}

export default ColumnEdit;

save.js

/**
 * External dependencies
 */
import classnames from 'classnames';

/**
 * WordPress dependencies
 */
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

export default function save( { attributes } ) {
  const { verticalAlignment, width } = attributes;

  const wrapperClasses = classnames( {
    [ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
  } );

  let style;

  if ( width ) {
    // Numbers are handled for backward compatibility as they can be still provided with templates.
    style = { flexBasis: Number.isFinite( width ) ? width + '%' : width };
  }

  return (
    <div
      { ...useBlockProps.save( {
        className: wrapperClasses,
        style,
      } ) }
    >
      <InnerBlocks.Content />
    </div>
  );
}

I've scaffolded a custom block via the official create-block package, I then went and pulled the code from the core block column from Github. And it seems to be working with some minor issues. For some reason my custom block gets a slightly different element structure where the block parent element gets nested within itself:

Whereas the core column block does not:



Any idea why this is happening? Relevant code below.

index.js

/**
 * WordPress dependencies
 */
import { registerBlockType } from '@wordpress/blocks';
import { __, _x } from '@wordpress/i18n';
import { column as icon } from '@wordpress/icons';

/**
 * Internal dependencies
 */
// import deprecated from './deprecated';
import edit from './edit';
import metadata from '../block.json';
import save from './save';

const { name } = metadata;

export { metadata, name };

export const settings = {
  title: _x( '12 Column', 'block title' ),
  icon,
  description: __( 'A single column within a columns block.' ),
  edit,
  save,
  // deprecated,
};

registerBlockType( 'wav/twelve-column', settings );

edit.js

/**
 * External dependencies
 */
import classnames from 'classnames';

/**
 * WordPress dependencies
 */
import {
  InnerBlocks,
  BlockControls,
  BlockVerticalAlignmentToolbar,
  InspectorControls,
  useBlockProps,
  __experimentalUseInnerBlocksProps as useInnerBlocksProps,
  store as blockEditorStore,
} from '@wordpress/block-editor';
import {
  PanelBody,
  __experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
 * Internal dependencies
 */
import { CSS_UNITS } from './utils';

function ColumnEdit( {
  attributes: { verticalAlignment, width, templateLock = false },
  setAttributes,
  clientId,
} ) {
  const classes = classnames( 'block-core-columns', {
    [ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
  } );

  const { hasChildBlocks, rootClientId } = useSelect(
    ( select ) => {
      const { getBlockOrder, getBlockRootClientId } = select(
        blockEditorStore
      );

      return {
        hasChildBlocks: getBlockOrder( clientId ).length > 0,
        rootClientId: getBlockRootClientId( clientId ),
      };
    },
    [ clientId ]
  );

  const { updateBlockAttributes } = useDispatch( blockEditorStore );

  const updateAlignment = ( value ) => {
    // Update own alignment.
    setAttributes( { verticalAlignment: value } );
    // Reset parent Columns block.
    updateBlockAttributes( rootClientId, {
      verticalAlignment: null,
    } );
  };

  const widthWithUnit = Number.isFinite( width ) ? width + '%' : width;
  const blockProps = useBlockProps( {
    className: classes,
    style: widthWithUnit ? { flexBasis: widthWithUnit } : undefined,
  } );
  const innerBlocksProps = useInnerBlocksProps( blockProps, {
    templateLock,
    renderAppender: hasChildBlocks
      ? undefined
      : InnerBlocks.ButtonBlockAppender,
  } );

  return (
    <>
      <BlockControls>
        <BlockVerticalAlignmentToolbar
          onChange={ updateAlignment }
          value={ verticalAlignment }
        />
      </BlockControls>
      <InspectorControls>
        <PanelBody title={ __( 'Column settings' ) }>
          <UnitControl
            label={ __( 'Width' ) }
            labelPosition="edge"
            __unstableInputWidth="80px"
            value={ width || '' }
            onChange={ ( nextWidth ) => {
              nextWidth =
                0 > parseFloat( nextWidth ) ? '0' : nextWidth;
              setAttributes( { width: nextWidth } );
            } }
            units={ CSS_UNITS }
          />
        </PanelBody>
      </InspectorControls>
      <div { ...innerBlocksProps } />
    </>
  );
}

export default ColumnEdit;

save.js

/**
 * External dependencies
 */
import classnames from 'classnames';

/**
 * WordPress dependencies
 */
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

export default function save( { attributes } ) {
  const { verticalAlignment, width } = attributes;

  const wrapperClasses = classnames( {
    [ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
  } );

  let style;

  if ( width ) {
    // Numbers are handled for backward compatibility as they can be still provided with templates.
    style = { flexBasis: Number.isFinite( width ) ? width + '%' : width };
  }

  return (
    <div
      { ...useBlockProps.save( {
        className: wrapperClasses,
        style,
      } ) }
    >
      <InnerBlocks.Content />
    </div>
  );
}
Share Improve this question asked Mar 10, 2021 at 23:38 INTINT 1,2813 gold badges21 silver badges51 bronze badges 2
  • Is this a column or a columnS block that you're comparing with? Also is your version inside a columnS block or is it elsewhere? I don't see anything to restrict it so its only allowed inside a columns block, or can only contain column blocks – Tom J Nowell Commented Mar 11, 2021 at 1:48
  • 1 I've created my own columns and column block (from the core blocks). The issue here was that I was not setting apiVersion: 2 when registering my blocks. It works as expected now! – INT Commented Mar 11, 2021 at 9:00
Add a comment  | 

1 Answer 1

Reset to default 1

Setting apiVersion: 2 while registering the block resolved this issue.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>