既然@WebElaine没有指定js方式,我将自己添加它。要添加的完整代码如下:
function setBlockCustomClassName( className, blockName ) {
return blockName === \'gutenberg-examples/example-03-editable-esnext\' ? //this is the block identifier, taken from the registerBlockType
\'new class name\' : //this is class name we want to associate by default to the block, in my case it\'s an empty string since I don\'t want any class
className;
}
wp.hooks.addFilter(
\'blocks.getBlockDefaultClassName\',
\'gutenberg-examples/example-03-editable-esnext\', //this is again the block identifier, taken from the registerBlockType
setBlockCustomClassName
);
总而言之,完整代码是:
registerBlockType( \'gutenberg-examples/example-03-editable-esnext\', {
title: __( \'Example: Editable (ESNext)\', \'gutenberg-examples\' ),
icon: \'universal-access-alt\',
category: \'layout\',
attributes: {
content: {
type: \'array\',
source: \'children\',
selector: \'p\',
},
},
edit: ( props ) => {
const { attributes: { content }, setAttributes, className } = props;
const onChangeContent = ( newContent ) => {
setAttributes( { content: newContent } );
};
return (
<RichText
tagName="p"
className={ className }
onChange={ onChangeContent }
value={ content }
/>
);
},
save: ( props ) => {
return <RichText.Content tagName="p" value={ props.attributes.content } />;
},
});
//Old code ended here
function setBlockCustomClassName( className, blockName ) {
return blockName === \'gutenberg-examples/example-03-editable-esnext\' ? //this is the block identifier, taken from the registerBlockType
\'new class name\' : //this is class name we want to associate by default to the block, in my case it\'s an empty string since I don\'t want any class
className;
}
wp.hooks.addFilter(
\'blocks.getBlockDefaultClassName\',
\'gutenberg-examples/example-03-editable-esnext\', //this is again the block identifier, taken from the registerBlockType
setBlockCustomClassName
);
对于一个简单的块,所有这些内容都会进入主js文件(通常是
index.js
) 与插件关联。