admin管理员组文章数量:1122832
I have a question, and I am really sorry when it has been already asked (and answered) - but I am researching for hours now and I can't find the answer. Although...somehow I can't believe it, that nobody needs this, so maybe I am completely wrong.
But let me tell you my aim:
I am creating a website for grammar - so I need to "highlight" many words inline. I would like to keep these designs consistent, so the best would be to define styles, like "Word in foreign language", "Word in my language", "Suffix", etc... And then just use them in the paragraph, like the existing ones:
How does that work? Creating the CSS is no problem, but how can I add it to the dropdown? Or are there any other options, that I am not thinking of?
Thanks a lot for helping! This (and the soon-coming patterns with overrides...) would really help me...
I have a question, and I am really sorry when it has been already asked (and answered) - but I am researching for hours now and I can't find the answer. Although...somehow I can't believe it, that nobody needs this, so maybe I am completely wrong.
But let me tell you my aim:
I am creating a website for grammar - so I need to "highlight" many words inline. I would like to keep these designs consistent, so the best would be to define styles, like "Word in foreign language", "Word in my language", "Suffix", etc... And then just use them in the paragraph, like the existing ones:
How does that work? Creating the CSS is no problem, but how can I add it to the dropdown? Or are there any other options, that I am not thinking of?
Thanks a lot for helping! This (and the soon-coming patterns with overrides...) would really help me...
Share Improve this question asked May 4, 2024 at 12:15 webwurmwebwurm 31 bronze badge1 Answer
Reset to default 1To add custom options to that dropdown, you'd want to look at utilizing the Formatting API. Below are the main points quoted from the Block Editor Handbook step-by-step guide:
Step 1: Register a new format
The first step is to register the new format, add
src/index.js
with the following:import { registerFormatType } from '@wordpress/rich-text'; registerFormatType( 'my-custom-format/sample-output', { title: 'Sample output', tagName: 'samp', className: null, } );
[…]
Step 2: Add a button to the toolbar
With the format available, the next step is to add a button to the UI by registering a component for the edit property.
Using the
RichTextToolbarButton
component, updatesrc/index.js
:import { RichTextToolbarButton } from '@wordpress/block-editor'; const MyCustomButton = ( props ) => { return ( <RichTextToolbarButton icon="editor-code" title="Sample output" onClick={ () => { console.log( 'toggle format' ); } } /> ); }; registerFormatType( 'my-custom-format/sample-output', { // … edit: MyCustomButton, } );
[…]
Step 3: Apply a format when clicked
Next is to update the button to apply a format when clicked.
import { registerFormatType, toggleFormat } from '@wordpress/rich-text'; import { RichTextToolbarButton } from '@wordpress/block-editor'; const MyCustomButton = ( { isActive, onChange, value } ) => { return ( <RichTextToolbarButton icon="editor-code" title="Sample output" onClick={ () => { onChange( toggleFormat( value, { type: 'my-custom-format/sample-output', } ) ); } } isActive={ isActive } /> ); };
Further reading:
registerFormatType
function reference.RichTextToolbarButton
code source (no documentation available).ToolbarButton
reference (RichTextToolbarButton
is based on this React component).
本文标签: Consistent inline styling in a Gutenbergparagraphblock
版权声明:本文标题:Consistent inline styling in a Gutenberg-paragraph-block 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308346a1933629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论