Skip to content

Instantly share code, notes, and snippets.

@cr0ybot
Created May 1, 2020 23:06
Show Gist options
  • Save cr0ybot/879d7f61808f4174b5b0866335e6d19d to your computer and use it in GitHub Desktop.
Save cr0ybot/879d7f61808f4174b5b0866335e6d19d to your computer and use it in GitHub Desktop.
Gutenberg Line Break Format
/**
* Format: line break
*
* Adds a line break option to any Rich Text editor that adds a <br> tag
*/
const { __ } = wp.i18n;
const { insertObject, registerFormatType } = wp.richText;
const { RichTextToolbarButton } = wp.blockEditor;
const toolbarIcon = 'editor-break';
const name = 'paragon/format-line-break';
registerFormatType(
name,
{
title: __( 'Line Break' ),
tagName: 'br',
className: null,
keywords: [ 'icon', 'font awesome' ],
edit: ( { isObjectActive, onChange, onFocus, value } ) => {
return (
<RichTextToolbarButton
icon={ toolbarIcon }
title={ __( 'Line Break' ) }
isActive={ isObjectActive }
onClick={ () => {
onChange(
insertObject( value, {
type: name,
attributes: {
'data-rich-text-line-break': 'true',
},
} )
);
onFocus();
} }
/>
);
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment