From cffff9fb136e0dc3a1bf339a4d23ff796246089b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Est=C3=AAv=C3=A3o?= Date: Wed, 27 May 2020 19:07:01 +0100 Subject: [PATCH] Move selection to end of link after insertion. (#22652) Make sure the selection is refreshed after changed. --- .../format-library/src/link/index.native.js | 13 ++++++------ .../format-library/src/link/modal.native.js | 20 ++++++++----------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/format-library/src/link/index.native.js b/packages/format-library/src/link/index.native.js index a1015cf9179f8c..3b2adb8b6e8c5d 100644 --- a/packages/format-library/src/link/index.native.js +++ b/packages/format-library/src/link/index.native.js @@ -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(); diff --git a/packages/format-library/src/link/modal.native.js b/packages/format-library/src/link/modal.native.js index d288b69262a591..f2edc774da53f9 100644 --- a/packages/format-library/src/link/modal.native.js +++ b/packages/format-library/src/link/modal.native.js @@ -87,7 +87,7 @@ class ModalLinkUI extends Component { opensInNewWindow, text: linkText, } ); - + let newAttributes; if ( isCollapsed( value ) && ! isActive ) { // insert link const toInsert = applyFormat( @@ -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( @@ -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(