Skip to content

Commit

Permalink
Move selection to end of link after insertion. (#22652)
Browse files Browse the repository at this point in the history
Make sure the selection is refreshed after changed.
  • Loading branch information
SergioEstevao committed May 27, 2020
1 parent 6cda2e3 commit de4ea80
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
13 changes: 7 additions & 6 deletions packages/format-library/src/link/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ export const link = {
const text = getTextContent( slice( value ) );

if ( text && isURL( text ) ) {
onChange(
applyFormat( value, {
type: name,
attributes: { url: text },
} )
);
const newValue = applyFormat( value, {
type: name,
attributes: { url: text },
} );
newValue.start = newValue.end;
newValue.activeFormats = [];
onChange( { ...newValue, needsSelectionUpdate: true } );
} else {
this.setState( { addingLink: true } );
this.getURLFromClipboard();
Expand Down
20 changes: 8 additions & 12 deletions packages/format-library/src/link/modal.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ModalLinkUI extends Component {
opensInNewWindow,
text: linkText,
} );

let newAttributes;
if ( isCollapsed( value ) && ! isActive ) {
// insert link
const toInsert = applyFormat(
Expand All @@ -96,8 +96,7 @@ class ModalLinkUI extends Component {
0,
linkText.length
);
const newAttributes = insert( value, toInsert );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
newAttributes = insert( value, toInsert );
} else if ( text !== getTextContent( slice( value ) ) ) {
// edit text in selected link
const toInsert = applyFormat(
Expand All @@ -106,18 +105,15 @@ class ModalLinkUI extends Component {
0,
text.length
);
const newAttributes = insert(
value,
toInsert,
value.start,
value.end
);
onChange( { ...newAttributes, needsSelectionUpdate: true } );
newAttributes = insert( value, toInsert, value.start, value.end );
} else {
// transform selected text into link
const newAttributes = applyFormat( value, format );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
newAttributes = applyFormat( value, format );
}
//move selection to end of link
newAttributes.start = newAttributes.end;
newAttributes.activeFormats = [];
onChange( { ...newAttributes, needsSelectionUpdate: true } );

if ( ! isValidHref( url ) ) {
speak(
Expand Down

0 comments on commit de4ea80

Please sign in to comment.