[FIX] Composer composer's send icon slowness (#528)

This commit is contained in:
Diego Mello 2018-11-05 17:03:17 -02:00 committed by GitHub
parent 0dd4899789
commit fc1796e098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 29 deletions

View File

@ -79,10 +79,10 @@ export default class MessageBox extends React.PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
text: '',
mentions: [], mentions: [],
showEmojiKeyboard: false, showEmojiKeyboard: false,
showFilesAction: false, showFilesAction: false,
showSend: false,
recording: false, recording: false,
file: { file: {
isVisible: false isVisible: false
@ -93,41 +93,37 @@ export default class MessageBox extends React.PureComponent {
this.emojis = []; this.emojis = [];
this.customEmojis = []; this.customEmojis = [];
this.onEmojiSelected = this.onEmojiSelected.bind(this); this.onEmojiSelected = this.onEmojiSelected.bind(this);
this.text = '';
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
const { message, replyMessage } = this.props; const { message, replyMessage } = this.props;
if (message !== nextProps.message && nextProps.message.msg) { if (message !== nextProps.message && nextProps.message.msg) {
this.setState({ text: nextProps.message.msg }); this.setInput(nextProps.message.msg);
this.component.focus(); this.component.focus();
} else if (replyMessage !== nextProps.replyMessage && nextProps.replyMessage.msg) { } else if (replyMessage !== nextProps.replyMessage && nextProps.replyMessage.msg) {
this.component.focus(); this.component.focus();
} else if (!nextProps.message) { } else if (!nextProps.message) {
this.setState({ text: '' }); this.clearInput();
} }
} }
onChangeText(text) { onChangeText(text) {
const { typing } = this.props; const { typing } = this.props;
this.setState({ text }); this.setInput(text);
typing(text.length > 0); typing(text.length > 0);
requestAnimationFrame(() => { requestAnimationFrame(() => {
const { start, end } = this.component._lastNativeSelection; const { start, end } = this.component._lastNativeSelection;
const cursor = Math.max(start, end); const cursor = Math.max(start, end);
const lastNativeText = this.component._lastNativeText; const lastNativeText = this.component._lastNativeText;
const regexp = /(#|@|:)([a-z0-9._-]+)$/im; const regexp = /(#|@|:)([a-z0-9._-]+)$/im;
const result = lastNativeText.substr(0, cursor).match(regexp); const result = lastNativeText.substr(0, cursor).match(regexp);
if (!result) { if (!result) {
return this.stopTrackingMention(); return this.stopTrackingMention();
} }
const [, lastChar, name] = result; const [, lastChar, name] = result;
this.identifyMentionKeyword(name, lastChar); this.identifyMentionKeyword(name, lastChar);
}); });
} }
@ -138,28 +134,22 @@ export default class MessageBox extends React.PureComponent {
onPressMention(item) { onPressMention(item) {
const { trackingType } = this.state; const { trackingType } = this.state;
const msg = this.text;
const msg = this.component._lastNativeText;
const { start, end } = this.component._lastNativeSelection; const { start, end } = this.component._lastNativeSelection;
const cursor = Math.max(start, end); const cursor = Math.max(start, end);
const regexp = /([a-z0-9._-]+)$/im; const regexp = /([a-z0-9._-]+)$/im;
const result = msg.substr(0, cursor).replace(regexp, ''); const result = msg.substr(0, cursor).replace(regexp, '');
const mentionName = trackingType === MENTIONS_TRACKING_TYPE_EMOJIS const mentionName = trackingType === MENTIONS_TRACKING_TYPE_EMOJIS
? `${ item.name || item }:` ? `${ item.name || item }:`
: (item.username || item.name); : (item.username || item.name);
const text = `${ result }${ mentionName } ${ msg.slice(cursor) }`; const text = `${ result }${ mentionName } ${ msg.slice(cursor) }`;
this.component.setNativeProps({ text }); this.setInput(text);
this.setState({ text });
this.component.focus(); this.component.focus();
requestAnimationFrame(() => this.stopTrackingMention()); requestAnimationFrame(() => this.stopTrackingMention());
} }
onEmojiSelected(keyboardId, params) { onEmojiSelected(keyboardId, params) {
const { text } = this.state; const { text } = this;
const { emoji } = params; const { emoji } = params;
let newText = ''; let newText = '';
@ -172,8 +162,7 @@ export default class MessageBox extends React.PureComponent {
// if messagebox doesn't have a cursor, just append selected emoji // if messagebox doesn't have a cursor, just append selected emoji
newText = `${ text }${ emoji }`; newText = `${ text }${ emoji }`;
} }
this.component.setNativeProps({ text: newText }); this.setInput(newText);
this.setState({ text: newText });
} }
get leftButtons() { get leftButtons() {
@ -231,14 +220,14 @@ export default class MessageBox extends React.PureComponent {
} }
get rightButtons() { get rightButtons() {
const { text } = this.state; const { showSend } = this.state;
const icons = []; const icons = [];
if (text) { if (showSend) {
icons.push( icons.push(
<BorderlessButton <BorderlessButton
key='send-message' key='send-message'
onPress={() => this.submit(text)} onPress={() => this.submit()}
style={styles.actionButton} style={styles.actionButton}
testID='messagebox-send-message' testID='messagebox-send-message'
accessibilityLabel={I18n.t('Send message')} accessibilityLabel={I18n.t('Send message')}
@ -385,6 +374,16 @@ export default class MessageBox extends React.PureComponent {
} }
} }
setInput = (text) => {
this.text = text;
this.component.setNativeProps({ text });
this.setState({ showSend: text.length > 0 });
}
clearInput = () => {
this.setInput('');
}
toggleFilesActions = () => { toggleFilesActions = () => {
this.setState(prevState => ({ showFilesAction: !prevState.showFilesAction })); this.setState(prevState => ({ showFilesAction: !prevState.showFilesAction }));
} }
@ -433,7 +432,7 @@ export default class MessageBox extends React.PureComponent {
editCancel = () => { editCancel = () => {
const { editCancel } = this.props; const { editCancel } = this.props;
editCancel(); editCancel();
this.setState({ text: '' }); this.clearInput();
} }
openEmoji = async() => { openEmoji = async() => {
@ -469,12 +468,13 @@ export default class MessageBox extends React.PureComponent {
this.setState({ showEmojiKeyboard: false }); this.setState({ showEmojiKeyboard: false });
} }
submit = async(message) => { submit = async() => {
const { const {
typing, message: editingMessage, editRequest, onSubmit typing, message: editingMessage, editRequest, onSubmit
} = this.props; } = this.props;
const message = this.text;
this.setState({ text: '' }); this.clearInput();
this.closeEmoji(); this.closeEmoji();
this.stopTrackingMention(); this.stopTrackingMention();
typing(false); typing(false);
@ -529,6 +529,11 @@ export default class MessageBox extends React.PureComponent {
} }
stopTrackingMention() { stopTrackingMention() {
const { trackingType } = this.state;
if (!trackingType) {
return;
}
this.setState({ this.setState({
mentions: [], mentions: [],
trackingType: '' trackingType: ''
@ -651,7 +656,7 @@ export default class MessageBox extends React.PureComponent {
} }
renderContent() { renderContent() {
const { recording, text } = this.state; const { recording } = this.state;
const { editing } = this.props; const { editing } = this.props;
if (recording) { if (recording) {
@ -675,7 +680,6 @@ export default class MessageBox extends React.PureComponent {
blurOnSubmit={false} blurOnSubmit={false}
placeholder={I18n.t('New_Message')} placeholder={I18n.t('New_Message')}
onChangeText={t => this.onChangeText(t)} onChangeText={t => this.onChangeText(t)}
value={text}
underlineColorAndroid='transparent' underlineColorAndroid='transparent'
defaultValue='' defaultValue=''
multiline multiline

View File

@ -311,7 +311,7 @@ describe('Room info screen', () => {
await waitFor(element(by.text('Yes, delete it!'))).toBeVisible().withTimeout(5000); await waitFor(element(by.text('Yes, delete it!'))).toBeVisible().withTimeout(5000);
await expect(element(by.text('Yes, delete it!'))).toBeVisible(); await expect(element(by.text('Yes, delete it!'))).toBeVisible();
await element(by.text('Yes, delete it!')).tap(); await element(by.text('Yes, delete it!')).tap();
await waitFor(element(by.id('rooms-list-view'))).toBeVisible().withTimeout(2000); await waitFor(element(by.id('rooms-list-view'))).toBeVisible().withTimeout(10000);
await element(by.id('rooms-list-view-search')).replaceText(''); await element(by.id('rooms-list-view-search')).replaceText('');
await waitFor(element(by.id(`rooms-list-view-item-${ room }`))).toBeNotVisible().withTimeout(60000); await waitFor(element(by.id(`rooms-list-view-item-${ room }`))).toBeNotVisible().withTimeout(60000);
await expect(element(by.id(`rooms-list-view-item-${ room }`))).toBeNotVisible(); await expect(element(by.id(`rooms-list-view-item-${ room }`))).toBeNotVisible();