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 and cbravobernal committed Apr 9, 2024
1 parent 1318f5f commit 872f210
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/format-library/src/unknown/index.js
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 872f210

Please sign in to comment.