diff --git a/packages/block-editor/src/hooks/anchor.js b/packages/block-editor/src/hooks/anchor.js index 6e8acd5eb7f2e7..297fcc49d2123c 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. @@ -63,72 +126,19 @@ export function addAttribute( settings ) { 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 } - + return ( + <> + + { props.isSelected && + hasBlockSupport( props.name, 'anchor' ) && ( + ) } - { /* - * 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 ; + + ); }; }, 'withInspectorControl'