Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a common function to insert an emoji into a text #17346

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f3abb8a
create a common function to insert an emoji into a text
bernhardoj Apr 11, 2023
9754214
add return type
bernhardoj Apr 12, 2023
12160a4
fix lint
bernhardoj Apr 12, 2023
be7f2a5
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 13, 2023
aeb0286
move the function to another util file
bernhardoj Apr 14, 2023
28b6f81
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 14, 2023
3ef5fa6
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 14, 2023
114d424
Revert "move the function to another util file"
bernhardoj Apr 14, 2023
61c8bdf
move the function to another util file
bernhardoj Apr 14, 2023
c24368b
fix lint
bernhardoj Apr 14, 2023
1403e4b
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 17, 2023
dc19e14
move to another util file
bernhardoj Apr 17, 2023
f9c3222
fix lint
bernhardoj Apr 17, 2023
980318e
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 17, 2023
317f581
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 18, 2023
530ed07
reorganize common and platform specific file
bernhardoj Apr 19, 2023
e549f83
simplify code
bernhardoj Apr 19, 2023
a50526b
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 19, 2023
307c828
Merge branch 'main' into fix/15951-remove-space-on-add-emoji-from-picker
bernhardoj Apr 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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 * as ComposerUtils from '../../libs/ComposerUtils';
import * as Browser from '../../libs/Browser';
import Clipboard from '../../libs/Clipboard';
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions';
Expand Down Expand Up @@ -343,7 +343,7 @@ class Composer extends React.Component {
const lineHeight = parseInt(computedStyle.lineHeight, 10) || 20;
const paddingTopAndBottom = parseInt(computedStyle.paddingBottom, 10)
+ parseInt(computedStyle.paddingTop, 10);
const computedNumberOfLines = getNumberOfLines(this.props.maxLines, lineHeight, paddingTopAndBottom, this.textInput.scrollHeight);
const computedNumberOfLines = ComposerUtils.getNumberOfLines(this.props.maxLines, lineHeight, paddingTopAndBottom, this.textInput.scrollHeight);
const numberOfLines = computedNumberOfLines === 0 ? this.props.numberOfLines : computedNumberOfLines;
updateIsFullComposerAvailable(this.props, numberOfLines);
this.setState({
Expand Down
17 changes: 17 additions & 0 deletions src/libs/ComposerUtils/getNumberOfLines/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Get the current number of lines in the composer
*
* @param {Number} maxLines
* @param {Number} lineHeight
* @param {Number} paddingTopAndBottom
* @param {Number} scrollHeight
*
* @returns {Number}
*/
function getNumberOfLines(maxLines, lineHeight, paddingTopAndBottom, scrollHeight) {
let newNumberOfLines = Math.ceil((scrollHeight - paddingTopAndBottom) / lineHeight);
newNumberOfLines = maxLines <= 0 ? newNumberOfLines : Math.min(newNumberOfLines, maxLines);
return newNumberOfLines;
}

export default getNumberOfLines;
14 changes: 14 additions & 0 deletions src/libs/ComposerUtils/getNumberOfLines/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Get the current number of lines in the composer
*
* @param {Number} lineHeight
* @param {Number} paddingTopAndBottom
* @param {Number} scrollHeight
*
* @returns {Number}
*/
function getNumberOfLines(lineHeight, paddingTopAndBottom, scrollHeight) {
return Math.ceil((scrollHeight - paddingTopAndBottom) / lineHeight);
}

export default getNumberOfLines;
28 changes: 15 additions & 13 deletions src/libs/ComposerUtils/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import getNumberOfLines from './getNumberOfLines';
import updateNumberOfLines from './updateNumberOfLines';

/**
* Get the current number of lines in the composer
*
* @param {Number} maxLines
* @param {Number} lineHeight
* @param {Number} paddingTopAndBottom
* @param {Number} scrollHeight
*
* @returns {Number}
* Replace substring between selection with a text.
* @param {String} text
* @param {Object} selection
* @param {String} textToInsert
* @returns {String}
*/
function getNumberOfLines(maxLines, lineHeight, paddingTopAndBottom, scrollHeight) {
let newNumberOfLines = Math.ceil((scrollHeight - paddingTopAndBottom) / lineHeight);
newNumberOfLines = maxLines <= 0 ? newNumberOfLines : Math.min(newNumberOfLines, maxLines);
return newNumberOfLines;
function insertText(text, selection, textToInsert) {
return text.slice(0, selection.start) + textToInsert + text.slice(selection.end, text.length);
}

export default getNumberOfLines;
export {
getNumberOfLines,
updateNumberOfLines,
insertText,
};
1 change: 1 addition & 0 deletions src/libs/ComposerUtils/updateNumberOfLines/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import lodashGet from 'lodash/get';
import styles from '../../styles/styles';
import updateIsFullComposerAvailable from './updateIsFullComposerAvailable';

/**
* Get the current number of lines in the composer
*
* @param {Number} lineHeight
* @param {Number} paddingTopAndBottom
* @param {Number} scrollHeight
*
* @returns {Number}
*/
function getNumberOfLines(lineHeight, paddingTopAndBottom, scrollHeight) {
return Math.ceil((scrollHeight - paddingTopAndBottom) / lineHeight);
}
import styles from '../../../styles/styles';
import updateIsFullComposerAvailable from '../updateIsFullComposerAvailable';
import getNumberOfLines from '../getNumberOfLines';

/**
* Check the current scrollHeight of the textarea (minus any padding) and
Expand All @@ -32,7 +20,4 @@ function updateNumberOfLines(props, e) {
updateIsFullComposerAvailable(props, numberOfLines);
}

export {
getNumberOfLines,
updateNumberOfLines,
};
export default updateNumberOfLines;
11 changes: 4 additions & 7 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import withKeyboardState, {keyboardStatePropTypes} from '../../../components/wit
import ArrowKeyFocusManager from '../../../components/ArrowKeyFocusManager';
import OfflineWithFeedback from '../../../components/OfflineWithFeedback';
import KeyboardShortcut from '../../../libs/KeyboardShortcut';
import * as ComposerUtils from '../../../libs/ComposerUtils';
import * as Welcome from '../../../libs/actions/Welcome';
import Permissions from '../../../libs/Permissions';

Expand Down Expand Up @@ -512,17 +513,13 @@ class ReportActionCompose extends React.Component {
* @param {String} emoji
*/
addEmojiToTextBox(emoji) {
const emojiWithSpace = `${emoji} `;
const newComment = this.comment.slice(0, this.state.selection.start)
+ emojiWithSpace
+ this.comment.slice(this.state.selection.end, this.comment.length);
this.setState(prevState => ({
selection: {
start: prevState.selection.start + emojiWithSpace.length,
end: prevState.selection.start + emojiWithSpace.length,
start: prevState.selection.start + emoji.length,
end: prevState.selection.start + emoji.length,
},
}));
this.updateComment(newComment);
this.updateComment(ComposerUtils.insertText(this.comment, this.state.selection, emoji));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, coming from #18515. This PR raise regression Emoji gets added in between the text when selecting text and adding new emoji by search and entering.

The this.state.selection we pass should be the state before it gets updated, it's due to the state update may be asynchronous.

}

/**
Expand Down
5 changes: 2 additions & 3 deletions 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 @@ -229,15 +230,13 @@ class ReportActionItemMessageEdit extends React.Component {
* @param {String} emoji
*/
addEmojiToTextBox(emoji) {
const newComment = this.state.draft.slice(0, this.state.selection.start)
+ emoji + this.state.draft.slice(this.state.selection.end, this.state.draft.length);
this.setState(prevState => ({
selection: {
start: prevState.selection.start + emoji.length,
end: prevState.selection.start + emoji.length,
},
}));
this.updateDraft(newComment);
this.updateDraft(ComposerUtils.insertText(this.state.draft, this.state.selection, emoji));
}

/**
Expand Down