Make a custom block like default paragraph block, so that Enter starts a new block

I have been trying to make a custom block that will behave like the default paragraph and headings blocks, so that when

I have been trying to make a custom block that will behave like the default paragraph and headings blocks, so that when Enter is pressed a new paragraph block is started.

Whatever I try, a
tag always seems to be added when Enter is pressed (with, or without the Shit key).

I thought setting the multiline property of RichText to false might do it, but it does not seem to work.

The JS code I have is:

/** IMPORTANT VARS!!! **/
var el = element.createElement;
var RichText = editor.RichText;

blocks.registerBlockType( 'snt/my-heading-border', {
    title: 'My Test Heading',
    icon: 'smiley',
    category: 'common',

    attributes: {
        content: {
            type: 'array',
            source: 'children',
            selector: 'h3'
        }
    },

    edit: function( props ) {
        function onChangeContent( newContent ) {
            console.log(newContent);
            props.setAttributes( { content: newContent } );
        }

        return el(
            RichText,
            {
                tagName: 'h3',
                className: props.className,
                onChange: onChangeContent,
                value: props.attributes.content,
                placeholder: 'Enter a heading'
            }
        );
    },

    save: function( props ) {
        return el( RichText.Content, {
            tagName: 'h3', 
            value: props.attributes.content
        } );
    },
} );

I can't help think I have missed something obvious. Any help much appreciated.

I have been trying to make a custom block that will behave like the default paragraph and headings blocks, so that when Enter is pressed a new paragraph block is started.

Whatever I try, a
tag always seems to be added when Enter is pressed (with, or without the Shit key).

I thought setting the multiline property of RichText to false might do it, but it does not seem to work.

The JS code I have is:

/** IMPORTANT VARS!!! **/
var el = element.createElement;
var RichText = editor.RichText;

blocks.registerBlockType( 'snt/my-heading-border', {
    title: 'My Test Heading',
    icon: 'smiley',
    category: 'common',

    attributes: {
        content: {
            type: 'array',
            source: 'children',
            selector: 'h3'
        }
    },

    edit: function( props ) {
        function onChangeContent( newContent ) {
            console.log(newContent);
            props.setAttributes( { content: newContent } );
        }

        return el(
            RichText,
            {
                tagName: 'h3',
                className: props.className,
                onChange: onChangeContent,
                value: props.attributes.content,
                placeholder: 'Enter a heading'
            }
        );
    },

    save: function( props ) {
        return el( RichText.Content, {
            tagName: 'h3', 
            value: props.attributes.content
        } );
    },
} );

I can't help think I have missed something obvious. Any help much appreciated.

Share Improve this question asked Jun 10, 2019 at 20:39 user3389097user3389097 312 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can check out the code of the paragraph (and heading) block. It's open source code !yay! Let it inspire you.

<RichText 
  ...
  onSplit={ ( value ) => {
    if ( ! value ) {
      return createBlock( name );
    }
    return createBlock( name, {
     ...attributes,
     content: value,
    } );
  } }
  onMerge={ mergeBlocks }
  ...
</RichText>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745411076a4626535.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信