admin管理员组文章数量:1429645
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 badges1 Answer
Reset to default 1You 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>
本文标签: Make a custom block like default paragraph blockso that Enter starts a new block
版权声明:本文标题:Make a custom block like default paragraph block, so that Enter starts a new block 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745411075a2657471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论