From faa2481f03f2c7fcc0752ce834816f125fdc9ab3 Mon Sep 17 00:00:00 2001 From: Marcelo Schmidt Date: Tue, 7 Jul 2015 00:04:38 -0300 Subject: [PATCH] Make startTyping and stopTyping trigger on keyup instead of keydown (better checking of input field); --- client/lib/chatMessages.coffee | 95 +++++++++++++++++++--------------- client/views/app/room.coffee | 4 ++ 2 files changed, 58 insertions(+), 41 deletions(-) diff --git a/client/lib/chatMessages.coffee b/client/lib/chatMessages.coffee index d73a1f626e6..f0d2ca6eb52 100644 --- a/client/lib/chatMessages.coffee +++ b/client/lib/chatMessages.coffee @@ -4,6 +4,7 @@ input = {} editing = {} selfTyping = new ReactiveVar false + typingTimeout = {} init = -> wrapper = $(".messages-container").find(".wrapper") @@ -90,7 +91,7 @@ KonchatNotification.removeRoomNotification(rid) msg = input.value input.value = '' - stopTyping() + stopTyping(rid) Meteor.call 'sendMessage', { rid: rid, msg: msg, day: window.day } deleteMsg = (element) -> @@ -99,25 +100,32 @@ if error return Errors.throw error.reason - update = (id, input) -> + update = (id, rid, input) -> if _.trim(input.value) isnt '' msg = input.value Meteor.call 'updateMessage', { id: id, msg: msg } clearEditing() + stopTyping(rid) startTyping = (rid, input) -> if _.trim(input.value) isnt '' - unless self.typingTimeout + unless typingTimeout?[rid] if Meteor.userId()? selfTyping.set true - Meteor.call 'typingStatus', rid, true - self.typingTimeout = Meteor.setTimeout -> - stopTyping() - , 30000 + Meteor.call 'typingStatus', rid + typingTimeout[rid] = Meteor.setTimeout -> + stopTyping(rid) + , 10000 + else + stopTyping(rid) - stopTyping = -> + stopTyping = (rid) -> selfTyping.set false - self.typingTimeout = null + if typingTimeout?[rid]? + clearTimeout(typingTimeout[rid]) + typingTimeout[rid] = null + + Meteor.call 'typingStatus', rid, false bindEvents = -> if wrapper?.length @@ -126,6 +134,32 @@ resize() # toBottom() if self.scrollable + keyup = (rid, event) -> + input = event.currentTarget + k = event.which + keyCodes = [ + 13, # Enter + 20, # Caps lock + 16, # Shift + 9, # Tab + 27, # Escape Key + 17, # Control Key + 91, # Windows Command Key + 19, # Pause Break + 18, # Alt Key + 93, # Right Click Point Key + 45, # Insert Key + 34, # Page Down + 35, # Page Up + 144, # Num Lock + 145 # Scroll Lock + ] + keyCodes.push i for i in [35..40] # Home, End, Arrow Keys + keyCodes.push i for i in [112..123] # F1 - F12 + + unless k in keyCodes + startTyping(rid, input) + keydown = (rid, event) -> input = event.currentTarget k = event.which @@ -134,7 +168,7 @@ event.preventDefault() event.stopPropagation() if editing.id - update(editing.id, input) + update(editing.id, rid, input) else send(rid, input) return @@ -144,42 +178,21 @@ event.stopPropagation() clearEditing() return - else - keyCodes = [ - 20, # Caps lock - 16, # Shift - 9, # Tab - 27, # Escape Key - 17, # Control Key - 91, # Windows Command Key - 19, # Pause Break - 18, # Alt Key - 93, # Right Click Point Key - 45, # Insert Key - 34, # Page Down - 35, # Page Up - 144, # Num Lock - 145 # Scroll Lock - ] - keyCodes.push i for i in [35..40] # Home, End, Arrow Keys - keyCodes.push i for i in [112..123] # F1 - F12 - - unless k in keyCodes - startTyping(rid, input) - else if k is 38 or k is 40 # Arrow Up or down - if k is 38 - return if input.value.slice(0, input.selectionStart).match(/[\n]/) isnt null - toPrevMessage() - else - return if input.value.slice(input.selectionEnd, input.value.length).match(/[\n]/) isnt null - toNextMessage() + else if k is 38 or k is 40 # Arrow Up or down + if k is 38 + return if input.value.slice(0, input.selectionStart).match(/[\n]/) isnt null + toPrevMessage() + else + return if input.value.slice(input.selectionEnd, input.value.length).match(/[\n]/) isnt null + toNextMessage() - event.preventDefault() - event.stopPropagation() + event.preventDefault() + event.stopPropagation() # isScrollable: isScrollable # toBottom: toBottom keydown: keydown + keyup: keyup deleteMsg: deleteMsg send: send init: init diff --git a/client/views/app/room.coffee b/client/views/app/room.coffee index dd0e67be564..48f168a6660 100644 --- a/client/views/app/room.coffee +++ b/client/views/app/room.coffee @@ -318,6 +318,10 @@ Template.room.events console.log 'room focus .input-message' if window.rocketDebug KonchatNotification.removeRoomNotification(this._id) + 'keyup .input-message': (event) -> + console.log 'room keyup .input-message',this._id if window.rocketDebug + ChatMessages.keyup(this._id, event, Template.instance()) + 'keydown .input-message': (event) -> console.log 'room keydown .input-message',this._id if window.rocketDebug ChatMessages.keydown(this._id, event, Template.instance())