Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format library: improve unknown format performance #48761

Merged
merged 1 commit into from Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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