From 51895702575158a6c525c85659470c9dc20d269b Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 30 Oct 2023 17:41:32 +0400 Subject: [PATCH 1/2] Block Editor: Optimize 'anchor' inspector controls --- packages/block-editor/src/hooks/anchor.js | 139 ++++++++++++---------- 1 file changed, 75 insertions(+), 64 deletions(-) diff --git a/packages/block-editor/src/hooks/anchor.js b/packages/block-editor/src/hooks/anchor.js index 6e8acd5eb7f2e7..b6a6b7861584a1 100644 --- a/packages/block-editor/src/hooks/anchor.js +++ b/packages/block-editor/src/hooks/anchor.js @@ -52,6 +52,69 @@ export function addAttribute( settings ) { return settings; } +function BlockEditAnchorControl( { blockName, attributes, setAttributes } ) { + const blockEditingMode = useBlockEditingMode(); + + const isWeb = Platform.OS === 'web'; + const textControl = ( + + { __( + 'Enter a word or two — without spaces — to make a unique web address just for this block, called an “anchor.” Then, you’ll be able to link directly to this section of your page.' + ) } + + { isWeb && ( + + { __( 'Learn more about anchors' ) } + + ) } + + } + value={ attributes.anchor || '' } + placeholder={ ! isWeb ? __( 'Add an anchor' ) : null } + onChange={ ( nextValue ) => { + nextValue = nextValue.replace( ANCHOR_REGEX, '-' ); + setAttributes( { + anchor: nextValue, + } ); + } } + autoCapitalize="none" + autoComplete="off" + /> + ); + + return ( + <> + { isWeb && blockEditingMode === 'default' && ( + + { textControl } + + ) } + { /* + * We plan to remove scoping anchors to 'core/heading' to support + * anchors for all eligble blocks. Additionally we plan to explore + * leveraging InspectorAdvancedControls instead of a custom + * PanelBody title. https://github.com/WordPress/gutenberg/issues/28363 + */ } + { ! isWeb && blockName === 'core/heading' && ( + + + { textControl } + + + ) } + + ); +} + /** * Override the default edit UI to include a new block inspector control for * assigning the anchor ID, if block supports anchor. @@ -64,71 +127,19 @@ export const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) => { return ( props ) => { const hasAnchor = hasBlockSupport( props.name, 'anchor' ); - const blockEditingMode = useBlockEditingMode(); - - if ( hasAnchor && props.isSelected ) { - const isWeb = Platform.OS === 'web'; - const textControl = ( - - { __( - 'Enter a word or two — without spaces — to make a unique web address just for this block, called an “anchor.” Then, you’ll be able to link directly to this section of your page.' - ) } - - { isWeb && ( - - { __( 'Learn more about anchors' ) } - - ) } - - } - value={ props.attributes.anchor || '' } - placeholder={ ! isWeb ? __( 'Add an anchor' ) : null } - onChange={ ( nextValue ) => { - nextValue = nextValue.replace( ANCHOR_REGEX, '-' ); - props.setAttributes( { - anchor: nextValue, - } ); - } } - autoCapitalize="none" - autoComplete="off" - /> - ); - - return ( - <> - - { isWeb && blockEditingMode === 'default' && ( - - { textControl } - - ) } - { /* - * We plan to remove scoping anchors to 'core/heading' to support - * anchors for all eligble blocks. Additionally we plan to explore - * leveraging InspectorAdvancedControls instead of a custom - * PanelBody title. https://github.com/WordPress/gutenberg/issues/28363 - */ } - { ! isWeb && props.name === 'core/heading' && ( - - - { textControl } - - - ) } - - ); - } - return ; + return ( + <> + + { hasAnchor && props.isSelected && ( + + ) } + + ); }; }, 'withInspectorControl' From fdf2f1027a6e74e9fc24168ade61dacd67420a90 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 1 Nov 2023 15:49:05 +0400 Subject: [PATCH 2/2] Lazy evaludate block support --- packages/block-editor/src/hooks/anchor.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/block-editor/src/hooks/anchor.js b/packages/block-editor/src/hooks/anchor.js index b6a6b7861584a1..297fcc49d2123c 100644 --- a/packages/block-editor/src/hooks/anchor.js +++ b/packages/block-editor/src/hooks/anchor.js @@ -126,18 +126,17 @@ function BlockEditAnchorControl( { blockName, attributes, setAttributes } ) { export const withInspectorControl = createHigherOrderComponent( ( BlockEdit ) => { return ( props ) => { - const hasAnchor = hasBlockSupport( props.name, 'anchor' ); - return ( <> - { hasAnchor && props.isSelected && ( - - ) } + { props.isSelected && + hasBlockSupport( props.name, 'anchor' ) && ( + + ) } ); };