@@ -78,7 +78,8 @@ export default class BasicMessageEditor extends React.Component {
7878 _replaceEmoticon = ( caret , inputType , diff ) => {
7979 const { model} = this . props ;
8080 const range = model . startRange ( caret ) ;
81- // expand range max 8 characters backwards from caret
81+ // expand range max 8 characters backwards from caret,
82+ // as a space to look for an emoticon
8283 let n = 8 ;
8384 range . expandBackwardsWhile ( ( index , offset ) => {
8485 const part = model . parts [ index ] ;
@@ -90,8 +91,14 @@ export default class BasicMessageEditor extends React.Component {
9091 const query = emoticonMatch [ 1 ] . toLowerCase ( ) . replace ( "-" , "" ) ;
9192 const data = EMOJIBASE . find ( e => e . emoticon ? e . emoticon . toLowerCase ( ) === query : false ) ;
9293 if ( data ) {
93- // + 1 because index is reported without preceding space
94- range . moveStart ( emoticonMatch . index + 1 ) ;
94+ const hasPrecedingSpace = emoticonMatch [ 0 ] [ 0 ] === " " ;
95+ // we need the range to only comprise of the emoticon
96+ // because we'll replace the whole range with an emoji,
97+ // so move the start forward to the start of the emoticon.
98+ // Take + 1 because index is reported without the possible preceding space.
99+ range . moveStart ( emoticonMatch . index + ( hasPrecedingSpace ? 1 : 0 ) ) ;
100+ // this returns the amount of added/removed characters during the replace
101+ // so the caret position can be adjusted.
95102 return range . replace ( [ this . props . model . partCreator . plain ( data . unicode + " " ) ] ) ;
96103 }
97104 }
0 commit comments