Skip to content

Commit

Permalink
Mobile - RichText - Set a default value for selectionStart / selectio…
Browse files Browse the repository at this point in the history
…nEnd for undefined values (#40581)
  • Loading branch information
Gerardo Pacheco authored Apr 26, 2022
1 parent 574bb66 commit 2d199cb
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/rich-text/src/component/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,8 @@ export class RichText extends Component {
accessibilityLabel,
disableEditingMenu = false,
baseGlobalStyles,
selectionStart,
selectionEnd,
} = this.props;
const { currentFontSize } = this.state;

Expand All @@ -1065,12 +1067,14 @@ export class RichText extends Component {
defaultTextDecorationColor
);

const currentSelectionStart = selectionStart ?? 0;
const currentSelectionEnd = selectionEnd ?? 0;
let selection = null;
if ( this.needsSelectionUpdate ) {
this.needsSelectionUpdate = false;
selection = {
start: this.props.selectionStart,
end: this.props.selectionEnd,
start: currentSelectionStart,
end: currentSelectionEnd,
};

// On AztecAndroid, setting the caret to an out-of-bounds position will crash the editor so, let's check for some cases.
Expand All @@ -1086,12 +1090,11 @@ export class RichText extends Component {
brBeforeParaMatches[ 0 ].match( /br/g ) || []
).length;
if ( count > 0 ) {
let newSelectionStart =
this.props.selectionStart - count;
let newSelectionStart = currentSelectionStart - count;
if ( newSelectionStart < 0 ) {
newSelectionStart = 0;
}
let newSelectionEnd = this.props.selectionEnd - count;
let newSelectionEnd = currentSelectionEnd - count;
if ( newSelectionEnd < 0 ) {
newSelectionEnd = 0;
}
Expand Down

0 comments on commit 2d199cb

Please sign in to comment.