Skip to content

Commit

Permalink
Format library: improve unknown format performance (WordPress#48761)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org>
  • Loading branch information
3 people authored Mar 29, 2024
1 parent 88728dd commit 2f88a19
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/format-library/src/unknown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { removeFormat, slice } from '@wordpress/rich-text';
import { removeFormat, slice, isCollapsed } from '@wordpress/rich-text';
import { RichTextToolbarButton } from '@wordpress/block-editor';
import { help } from '@wordpress/icons';

const name = 'core/unknown';
const title = __( 'Clear Unknown Formatting' );

function selectionContainsUnknownFormats( value ) {
if ( isCollapsed( value ) ) {
return false;
}

const selectedValue = slice( value );
return selectedValue.formats.some( ( formats ) => {
return formats.some( ( format ) => format.type === name );
} );
}

export const unknown = {
name,
title,
tagName: '*',
className: null,
edit( { isActive, value, onChange, onFocus } ) {
if ( ! isActive && ! selectionContainsUnknownFormats( value ) ) {
return null;
}

function onClick() {
onChange( removeFormat( value, name ) );
onFocus();
}

const selectedValue = slice( value );
const hasUnknownFormats = selectedValue.formats.some( ( formats ) => {
return formats.some( ( format ) => format.type === name );
} );

if ( ! isActive && ! hasUnknownFormats ) {
return null;
}

return (
<RichTextToolbarButton
name="unknown"
Expand Down

0 comments on commit 2f88a19

Please sign in to comment.