Skip to content

Commit

Permalink
move the function to another util file
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardoj committed Apr 14, 2023
1 parent be7f2a5 commit aeb0286
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/components/Composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import Growl from '../../libs/Growl';
import themeColors from '../../styles/themes/default';
import updateIsFullComposerAvailable from '../../libs/ComposerUtils/updateIsFullComposerAvailable';
import getNumberOfLines from '../../libs/ComposerUtils/index';
import {getNumberOfLines} from '../../libs/ComposerUtils/index';
import * as Browser from '../../libs/Browser';
import Clipboard from '../../libs/Clipboard';
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions';
Expand Down
16 changes: 15 additions & 1 deletion src/libs/ComposerUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ function getNumberOfLines(maxLines, lineHeight, paddingTopAndBottom, scrollHeigh
return newNumberOfLines;
}

export default getNumberOfLines;
/**
* Replace substring between selection with a text.
* @param {String} text
* @param {Object} selection
* @param {String} textToInsert
* @returns {String}
*/
function insertText(text, selection, textToInsert) {
return text.slice(0, selection.start) + textToInsert + text.slice(selection.end, text.length);
}

export {
getNumberOfLines,
insertText,
};
12 changes: 0 additions & 12 deletions src/libs/EmojiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,6 @@ function suggestEmojis(text, limit = 5) {
return [];
}

/**
* Replace substring between selection with an emoji.
* @param {String} text
* @param {Object} selection
* @param {String} emoji
* @returns {String}
*/
function insertEmoji(text, selection, emoji) {
return text.slice(0, selection.start) + emoji + text.slice(selection.end, text.length);
}

export {
getHeaderEmojis,
mergeEmojisWithFrequentlyUsedEmojis,
Expand All @@ -284,5 +273,4 @@ export {
suggestEmojis,
trimEmojiUnicode,
getEmojiCodeWithSkinColor,
insertEmoji,
};
4 changes: 3 additions & 1 deletion src/pages/home/report/ReportActionCompose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import withKeyboardState, {keyboardStatePropTypes} from '../../../../components/
import ArrowKeyFocusManager from '../../../../components/ArrowKeyFocusManager';
import KeyboardShortcut from '../../../../libs/KeyboardShortcut';
import KeyDownAction from './keyDownAction';
import * as ComposerUtils from '../../../../libs/ComposerUtils';

const propTypes = {
/** Beta features list */
Expand Down Expand Up @@ -507,7 +508,8 @@ class ReportActionCompose extends React.Component {
start: prevState.selection.start + text.length,
end: prevState.selection.start + text.length,
},
}), this.updateComment(EmojiUtils.insertEmoji(this.comment, this.state.selection, text)));
}));
this.updateComment(ComposerUtils.insertText(this.comment, this.state.selection, text));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import withWindowDimensions, {windowDimensionsPropTypes} from '../../../componen
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import withKeyboardState, {keyboardStatePropTypes} from '../../../components/withKeyboardState';
import ONYXKEYS from '../../../ONYXKEYS';
import * as ComposerUtils from '../../../libs/ComposerUtils';

const propTypes = {
/** All the data of the action */
Expand Down Expand Up @@ -235,7 +236,7 @@ class ReportActionItemMessageEdit extends React.Component {
end: prevState.selection.start + emoji.length,
},
}));
this.updateDraft(EmojiUtils.insertEmoji(this.state.draft, this.state.selection, emoji));
this.updateDraft(ComposerUtils.insertText(this.state.draft, this.state.selection, emoji));
}

/**
Expand Down
7 changes: 0 additions & 7 deletions tests/unit/EmojiTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,4 @@ describe('EmojiTest', () => {
],
}]);
});

it('replace substring between selection with an emoji', () => {
expect(EmojiUtils.insertEmoji('', {start: 0, end: 0}, '👍')).toBe('👍');
expect(EmojiUtils.insertEmoji('Hi', {start: 2, end: 2}, '👍')).toBe('Hi👍');
expect(EmojiUtils.insertEmoji('Hello world', {start: 5, end: 5}, '👍')).toBe('Hello👍 world');
expect(EmojiUtils.insertEmoji('Hello world', {start: 2, end: 8}, '👍')).toBe('He👍rld');
});
});

0 comments on commit aeb0286

Please sign in to comment.