Skip to content

Commit

Permalink
Experimental commit for selecting all text of a label on link change.
Browse files Browse the repository at this point in the history
* Adds @wordpress/dom depenendency for block-library. Can be removed if we don't use placeCaretAtHorizontalEdge
* Set const _experimentalSelectLabelContentOnChange = true; to false to just place caret at end of label instead of selecting entire label text.
  • Loading branch information
jeryj committed Jan 16, 2020
1 parent 5371138 commit 6c46917
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { escape, unescape } from 'lodash';
import { compose } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import { withDispatch, withSelect } from '@wordpress/data';
import { placeCaretAtHorizontalEdge } from '@wordpress/dom';
import {
ExternalLink,
KeyboardShortcuts,
Expand All @@ -35,7 +36,7 @@ import {
RichText,
__experimentalLinkControl as LinkControl,
} from '@wordpress/block-editor';
import { Fragment, useState, useEffect } from '@wordpress/element';
import { Fragment, useState, useEffect, useRef } from '@wordpress/element';

function NavigationLinkEdit( {
attributes,
Expand All @@ -54,6 +55,8 @@ function NavigationLinkEdit( {
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
const itemLabelPlaceholder = __( 'Add link…' );

const ref = useRef();

// Show the LinkControl on mount if the URL is empty
// ( When adding a new menu item)
// This can't be done in the useState call because it cconflicts
Expand All @@ -64,6 +67,9 @@ function NavigationLinkEdit( {
}
}, [] );

// WIP: This is just a test to see if selecting the label content on link change makes sense.
const _experimentalSelectLabelContentOnChange = true;

/**
* The hook shouldn't be necessary but due to a focus loss happening
* when selecting a suggestion in the link popover, we force close on block unselection.
Expand All @@ -74,6 +80,24 @@ function NavigationLinkEdit( {
}
}, [ isSelected ] );

function focusLabel() {
if ( _experimentalSelectLabelContentOnChange ) {
// select all the text and place cursor at the end (although you can't see it).
ref.current.focus();

const selection = window.getSelection();
const range = document.createRange();

range.selectNodeContents( ref.current );

selection.removeAllRanges();
selection.addRange( range );
} else {
// place the cursor it at the end of the content.
placeCaretAtHorizontalEdge( ref.current, true );
}
}

return (
<Fragment>
<BlockControls>
Expand Down Expand Up @@ -152,6 +176,7 @@ function NavigationLinkEdit( {
>
<div>
<RichText
ref={ ref }
className="wp-block-navigation-link__content"
value={ label }
onChange={ ( labelValue ) => setAttributes( { label: labelValue } ) }
Expand Down Expand Up @@ -182,6 +207,7 @@ function NavigationLinkEdit( {
opensInNewTab: newOpensInNewTab,
} );
setIsLinkOpen( false );
focusLabel();
} }
onClose={ () => {
setIsLinkOpen( false );
Expand Down

0 comments on commit 6c46917

Please sign in to comment.