chore: Send typing events on keyup, not on focus (#1461)

This commit is contained in:
Pranav Raj S 2020-11-27 00:17:55 +05:30 committed by GitHub
parent a988724c91
commit f397c0c087
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 6 deletions

View file

@ -19,6 +19,8 @@
class="input" class="input"
:placeholder="messagePlaceHolder" :placeholder="messagePlaceHolder"
:min-height="4" :min-height="4"
@typing-off="onTypingOff"
@typing-on="onTypingOn"
@focus="onFocus" @focus="onFocus"
@blur="onBlur" @blur="onBlur"
/> />
@ -298,13 +300,17 @@ export default {
hideCannedResponse() { hideCannedResponse() {
this.showCannedResponsesList = false; this.showCannedResponsesList = false;
}, },
onTypingOn() {
this.toggleTyping('on');
},
onTypingOff() {
this.toggleTyping('off');
},
onBlur() { onBlur() {
this.isFocused = false; this.isFocused = false;
this.toggleTyping('off');
}, },
onFocus() { onFocus() {
this.isFocused = true; this.isFocused = true;
this.toggleTyping('on');
}, },
toggleTyping(status) { toggleTyping(status) {
if (this.isAWebWidgetInbox && !this.isPrivate) { if (this.isAWebWidgetInbox && !this.isPrivate) {

View file

@ -5,11 +5,14 @@
:value="value" :value="value"
@input="onInput" @input="onInput"
@focus="onFocus" @focus="onFocus"
@keyup="onKeyup"
@blur="onBlur" @blur="onBlur"
/> />
</template> </template>
<script> <script>
const TYPING_INDICATOR_IDLE_TIME = 4000;
export default { export default {
props: { props: {
placeholder: { placeholder: {
@ -25,6 +28,11 @@ export default {
default: 2, default: 2,
}, },
}, },
data() {
return {
idleTimer: null,
};
},
watch: { watch: {
value() { value() {
this.resizeTextarea(); this.resizeTextarea();
@ -42,7 +50,28 @@ export default {
this.$emit('input', event.target.value); this.$emit('input', event.target.value);
this.resizeTextarea(); this.resizeTextarea();
}, },
resetTyping() {
this.$emit('typing-off');
this.idleTimer = null;
},
turnOffIdleTimer() {
if (this.idleTimer) {
clearTimeout(this.idleTimer);
}
},
onKeyup() {
if (!this.idleTimer) {
this.$emit('typing-on');
}
this.turnOffIdleTimer();
this.idleTimer = setTimeout(
() => this.resetTyping(),
TYPING_INDICATOR_IDLE_TIME
);
},
onBlur() { onBlur() {
this.turnOffIdleTimer();
this.resetTyping();
this.$emit('blur'); this.$emit('blur');
}, },
onFocus() { onFocus() {

View file

@ -4,8 +4,8 @@
v-model="userInput" v-model="userInput"
:placeholder="$t('CHAT_PLACEHOLDER')" :placeholder="$t('CHAT_PLACEHOLDER')"
class="form-input user-message-input" class="form-input user-message-input"
@focus="onFocus" @typing-off="onTypingOff"
@blur="onBlur" @typing-on="onTypingOn"
/> />
<div class="button-wrap"> <div class="button-wrap">
<chat-attachment-button <chat-attachment-button
@ -111,10 +111,10 @@ export default {
emojiOnClick(emoji) { emojiOnClick(emoji) {
this.userInput = `${this.userInput}${emoji} `; this.userInput = `${this.userInput}${emoji} `;
}, },
onBlur() { onTypingOff() {
this.toggleTyping('off'); this.toggleTyping('off');
}, },
onFocus() { onTypingOn() {
this.toggleTyping('on'); this.toggleTyping('on');
}, },
toggleTyping(typingStatus) { toggleTyping(typingStatus) {