From dc95863e510c213d8896dd71843d2c3b62fd260d Mon Sep 17 00:00:00 2001 From: Ella <4710635+ellatrix@users.noreply.github.com> Date: Tue, 12 Dec 2023 10:03:01 +0100 Subject: [PATCH] Paragraph: store subscription for selected block only (#56967) --- packages/block-library/src/paragraph/edit.js | 85 +++++++++++--------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/packages/block-library/src/paragraph/edit.js b/packages/block-library/src/paragraph/edit.js index 3a0f4f0688ac9b..ac766f69dd846a 100644 --- a/packages/block-library/src/paragraph/edit.js +++ b/packages/block-library/src/paragraph/edit.js @@ -49,6 +49,48 @@ function hasDropCapDisabled( align ) { return align === ( isRTL() ? 'left' : 'right' ) || align === 'center'; } +function DropCapControl( { clientId, attributes, setAttributes } ) { + // Please do no add a useSelect call to the paragraph block unconditionaly. + // Every useSelect added to a (frequestly used) block will degrade the load + // and type bit. By moving it within InspectorControls, the subscription is + // now only added for the selected block(s). + const [ isDropCapFeatureEnabled ] = useSettings( 'typography.dropCap' ); + + if ( ! isDropCapFeatureEnabled ) { + return null; + } + + const { align, dropCap } = attributes; + + let helpText; + if ( hasDropCapDisabled( align ) ) { + helpText = __( 'Not available for aligned text.' ); + } else if ( dropCap ) { + helpText = __( 'Showing large initial letter.' ); + } else { + helpText = __( 'Toggle to show a large initial letter.' ); + } + + return ( + !! dropCap } + label={ __( 'Drop cap' ) } + onDeselect={ () => setAttributes( { dropCap: undefined } ) } + resetAllFilter={ () => ( { dropCap: undefined } ) } + panelId={ clientId } + > + setAttributes( { dropCap: ! dropCap } ) } + help={ helpText } + disabled={ hasDropCapDisabled( align ) ? true : false } + /> + + ); +} + function ParagraphBlock( { attributes, mergeBlocks, @@ -58,7 +100,6 @@ function ParagraphBlock( { clientId, } ) { const { align, content, direction, dropCap, placeholder } = attributes; - const [ isDropCapFeatureEnabled ] = useSettings( 'typography.dropCap' ); const blockProps = useBlockProps( { ref: useOnEnter( { clientId, content } ), className: classnames( { @@ -68,15 +109,6 @@ function ParagraphBlock( { style: { direction }, } ); - let helpText; - if ( hasDropCapDisabled( align ) ) { - helpText = __( 'Not available for aligned text.' ); - } else if ( dropCap ) { - helpText = __( 'Showing large initial letter.' ); - } else { - helpText = __( 'Toggle to show a large initial letter.' ); - } - return ( <> @@ -98,32 +130,13 @@ function ParagraphBlock( { } /> - { isDropCapFeatureEnabled && ( - - !! dropCap } - label={ __( 'Drop cap' ) } - onDeselect={ () => - setAttributes( { dropCap: undefined } ) - } - resetAllFilter={ () => ( { dropCap: undefined } ) } - panelId={ clientId } - > - - setAttributes( { dropCap: ! dropCap } ) - } - help={ helpText } - disabled={ - hasDropCapDisabled( align ) ? true : false - } - /> - - - ) } + + +