From a64ad04a0e92d5227c6aae5a2519e1a4c12fd946 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Thu, 17 Mar 2016 16:29:37 +0000 Subject: [PATCH] handle focus nicely when inserting displaynames and let them be inserted in the middle of in-progress messages --- src/components/views/rooms/MessageComposer.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index 2cb2acb470..53253376c9 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -192,17 +192,28 @@ module.exports = React.createClass({ }, onAction: function(payload) { + var textarea = this.refs.textarea; switch (payload.action) { case 'focus_composer': - this.refs.textarea.focus(); + textarea.focus(); break; case 'insert_displayname': - if (this.refs.textarea.value) { - this.refs.textarea.value = - this.refs.textarea.value.replace(/( ?)$/, " " + payload.displayname); + if (textarea.value) { + var left = textarea.value.substring(0, textarea.selectionStart); + var right = textarea.value.substring(textarea.selectionEnd); + if (right && right.length) { + left += payload.displayname; + } + else { + left = left.replace(/( ?)$/, " " + payload.displayname); + } + textarea.value = left + right; + textarea.focus(); + textarea.setSelectionRange(left.length, left.length); } else { this.refs.textarea.value = payload.displayname + ": "; + this.refs.textarea.focus(); } break; }