Skip to content

Commit

Permalink
[Mobile]Update caret position on insert link (#14317)
Browse files Browse the repository at this point in the history
* Update selection on text change if needed

* Update caret position when editing the link

* Revert putting the caret at end when we transform the selected text into link
  • Loading branch information
pinarol authored Mar 13, 2019
1 parent 38377fe commit de77ad8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 9 additions & 1 deletion packages/editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ export class RichText extends Component {
} );
if ( newContent && newContent !== this.props.value ) {
this.props.onChange( newContent );
if ( record.needsSelectionUpdate && record.start && record.end ) {
this.setState( { start: record.start, end: record.end } );
}
this.setState( {
needsSelectionUpdate: record.needsSelectionUpdate,
} );
} else {
// make sure the component rerenders without refreshing the text on gutenberg
// (this can trigger other events that might update the active formats on aztec)
Expand Down Expand Up @@ -525,6 +531,8 @@ export class RichText extends Component {
minHeight = style.minHeight;
}

const selection = this.state.needsSelectionUpdate ? { start: this.state.start, end: this.state.end } : null;

return (
<View>
{ isSelected && (
Expand All @@ -544,7 +552,7 @@ export class RichText extends Component {
...style,
minHeight: Math.max( minHeight, this.state.height ),
} }
text={ { text: html, eventCount: this.lastEventCount } }
text={ { text: html, eventCount: this.lastEventCount, selection } }
placeholder={ this.props.placeholder }
placeholderTextColor={ this.props.placeholderTextColor || styles[ 'editor-rich-text' ].textDecorationColor }
onChange={ this.onChange }
Expand Down
9 changes: 6 additions & 3 deletions packages/format-library/src/link/modal.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ class ModalLinkUI extends Component {

if ( isCollapsed( value ) && ! isActive ) { // insert link
const toInsert = applyFormat( create( { text: linkText } ), [ ...placeholderFormats, format ], 0, linkText.length );
onChange( insert( value, toInsert ) );
const newAttributes = insert( value, toInsert );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
} else if ( text !== getTextContent( slice( value ) ) ) { // edit text in selected link
const toInsert = applyFormat( create( { text } ), [ ...placeholderFormats, format ], 0, text.length );
onChange( insert( value, toInsert, value.start, value.end ) );
const newAttributes = insert( value, toInsert, value.start, value.end );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
} else { // transform selected text into link
onChange( applyFormat( value, [ ...placeholderFormats, format ] ) );
const newAttributes = applyFormat( value, [ ...placeholderFormats, format ] );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
}

if ( ! isValidHref( url ) ) {
Expand Down

0 comments on commit de77ad8

Please sign in to comment.