Skip to content

Instantly share code, notes, and snippets.

@phpbits
Last active April 27, 2021 17:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpbits/69e96d64cdb29005c9c1e41569eacb1c to your computer and use it in GitHub Desktop.
Save phpbits/69e96d64cdb29005c9c1e41569eacb1c to your computer and use it in GitHub Desktop.
Create custom Underline rich text format for Gutenberg editor. Learn more at https://jeffreycarandang.com/how-to-create-custom-text-formats-for-gutenberg-block-editor/
/**
* WordPress dependencies
*/
const { __ } = wp.i18n;
const { Fragment } = wp.element;
const { toggleFormat } = wp.richText;
const { RichTextToolbarButton, RichTextShortcut } = wp.editor;
const { registerFormatType } = wp.richText;
/**
* Block constants
*/
const name = 'editorskit/underline';
export const underline = {
name,
title: __( 'Underline' ),
tagName: 'span',
className: null,
attributes: {
style: 'style',
},
edit( { isActive, value, onChange } ) {
const onToggle = () => {
onChange(
toggleFormat( value, {
type: name,
attributes: {
style: 'text-decoration: underline;',
},
} )
);
};
return (
<Fragment>
<RichTextShortcut
type="primary"
character="u"
onUse={ onToggle }
/>
<RichTextToolbarButton
icon="editor-underline"
title={ __( 'Underline' ) }
onClick={ onToggle }
isActive={ isActive }
shortcutType="primary"
shortcutCharacter="u"
/>
</Fragment>
);
},
};
function registerFormats () {
[
underline,
].forEach( ( { name, ...settings } ) => registerFormatType( name, settings ) );
};
registerFormats();
@rosallx
Copy link

rosallx commented Dec 22, 2019

hi, i am trying to add the underline button in the toolbar. where should i add the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment