如何禁用核心块的工具栏?这可能吗?
How can I disable the toolbars for core blocks? Is this possible at all?
我试图禁用所有核心块的工具栏,以防止编辑器不必要地格式化内容。这可能吗?
我目前的做法是:
wp.blocks.getBlockTypes().forEach((blockType) => {
// unregister all default styles (from the right sidebar)
let blockName = blockType.name;
if ( blockType.hasOwnProperty('styles')) {
blockType.styles.forEach( (style) => {
wp.blocks.unregisterBlockStyle( blockName, style.name );
});
}
});
我可以通过某种方式访问此循环中的工具栏吗?我是否正确理解我必须覆盖核心块的编辑和保存方法,可能使用过滤器?
谢谢,帕特里克
我刚刚解决了这个问题,但与预期的不同。
基本上,我的解决方案是注销所需的核心块,更改编辑和保存方法,然后重新注册块。
Riad Benguella 的这篇博客文章提供了很大帮助:
https://riad.blog/2017/10/16/one-thousand-and-one-way-to-extend-gutenberg-today/
这是一个基于核心/引用块的示例:
const TextControl = wp.components.TextControl;
import './style.scss';
import './editor.scss';
wp.domReady( () => {
let unregisteredBlock = wp.blocks.unregisterBlockType('core/quote');
unregisteredBlock.title = 'Quotation';
unregisteredBlock.icon = 'format-quote';
unregisteredBlock.edit = ({ attributes, setAttributes} ) => {
const updateFirstValue = ( val ) => {
setAttributes({
value: val
});
};
const updateSecondValue = ( val ) => {
setAttributes({
citation: val
});
};
return (
<div>
<TextControl
label='Quote'
value={ attributes.value }
onChange={ updateFirstValue }
/>
<TextControl
label='Citation'
value={ attributes.citation }
onChange={ updateSecondValue }
/>
</div>
);
};
unregisteredBlock.save = ( { attributes, className } ) => {
return (
<blockquote className={className}>
<p>{attributes.value}</p>
<cite>{attributes.citation}</cite>
</blockquote>
)
};
wp.blocks.registerBlockType('core/quote', unregisteredBlock);
});
原则上,这里的edit和save方法都被替换掉了,只是重用了核心block中的block属性。由于使用了新的元素来输入内容,因此工具栏不再是问题。
希望对遇到同样问题的人有所帮助。
干杯,
帕特里克
我试图禁用所有核心块的工具栏,以防止编辑器不必要地格式化内容。这可能吗?
我目前的做法是:
wp.blocks.getBlockTypes().forEach((blockType) => {
// unregister all default styles (from the right sidebar)
let blockName = blockType.name;
if ( blockType.hasOwnProperty('styles')) {
blockType.styles.forEach( (style) => {
wp.blocks.unregisterBlockStyle( blockName, style.name );
});
}
});
我可以通过某种方式访问此循环中的工具栏吗?我是否正确理解我必须覆盖核心块的编辑和保存方法,可能使用过滤器?
谢谢,帕特里克
我刚刚解决了这个问题,但与预期的不同。 基本上,我的解决方案是注销所需的核心块,更改编辑和保存方法,然后重新注册块。
Riad Benguella 的这篇博客文章提供了很大帮助: https://riad.blog/2017/10/16/one-thousand-and-one-way-to-extend-gutenberg-today/
这是一个基于核心/引用块的示例:
const TextControl = wp.components.TextControl;
import './style.scss';
import './editor.scss';
wp.domReady( () => {
let unregisteredBlock = wp.blocks.unregisterBlockType('core/quote');
unregisteredBlock.title = 'Quotation';
unregisteredBlock.icon = 'format-quote';
unregisteredBlock.edit = ({ attributes, setAttributes} ) => {
const updateFirstValue = ( val ) => {
setAttributes({
value: val
});
};
const updateSecondValue = ( val ) => {
setAttributes({
citation: val
});
};
return (
<div>
<TextControl
label='Quote'
value={ attributes.value }
onChange={ updateFirstValue }
/>
<TextControl
label='Citation'
value={ attributes.citation }
onChange={ updateSecondValue }
/>
</div>
);
};
unregisteredBlock.save = ( { attributes, className } ) => {
return (
<blockquote className={className}>
<p>{attributes.value}</p>
<cite>{attributes.citation}</cite>
</blockquote>
)
};
wp.blocks.registerBlockType('core/quote', unregisteredBlock);
});
原则上,这里的edit和save方法都被替换掉了,只是重用了核心block中的block属性。由于使用了新的元素来输入内容,因此工具栏不再是问题。
希望对遇到同样问题的人有所帮助。
干杯, 帕特里克