Skip to content

Instantly share code, notes, and snippets.

@stokesman
Last active November 3, 2023 22:54
Show Gist options
  • Save stokesman/4961fa04cdd601a08e3124cb1a305834 to your computer and use it in GitHub Desktop.
Save stokesman/4961fa04cdd601a08e3124cb1a305834 to your computer and use it in GitHub Desktop.
Use the Format API to add SVG in RichText
( ( {
blockEditor: { RichTextToolbarButton },
element: { createElement },
richText: { insertObject, registerFormatType, remove },
} ) => {
const formatName = 'svg-format-demo/one';
const format = {
title: 'SVG Format Demo',
tagName: 'u',
contentEditable: false,
className: 'svg-format-demo--one',
attributes: {
className: 'class',
},
edit: ( { isObjectActive, onChange, value } ) => {
const onClick = () => {
if ( isObjectActive )
return onChange( remove( value ) );
const toInsert = {
type: formatName,
attributes: {
viewBox: '0 0 24 24',
},
innerHTML: `<svg viewBox="0 0 24 24" style="width:1em;height:1em;fill:none;stroke:currentColor;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;"><path d="M16 7h.01"/><path d="M3.4 18H12a8 8 0 0 0 8-8V7a4 4 0 0 0-7.28-2.3L2 20"/><path d="m20 7 2 .5-2 .5"/><path d="M10 18v3"/><path d="M14 17.75V21"/><path d="M7 18a6 6 0 0 0 3.84-10.61"/></svg>`,
}
onChange( insertObject( value, toInsert ) );
};
return createElement(
RichTextToolbarButton, {
isActive: isObjectActive,
icon: 'art',
title: 'Put a bird on it',
onClick,
}
)
}
};
wp.richText.registerFormatType( formatName, format );
} )( wp );
<?php
/*
Plugin Name: SVG Format Demo
*/
add_action( 'init', function () {
wp_register_script(
'svg-format-demo-js',
plugins_url( 'svg-format-demo.js', __FILE__ ),
array( 'wp-rich-text', 'wp-element', 'wp-block-editor' )
);
});
add_action( 'enqueue_block_editor_assets', function () {
wp_enqueue_script( 'svg-format-demo-js' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment