From b16bc0178aecd6aa8775bd71709f7011b2354dba Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 19 Jun 2019 17:37:52 +0200 Subject: [PATCH] insert manually, as insertHTML command moves caret inconsistently across browsers --- src/components/views/elements/MessageEditor.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/views/elements/MessageEditor.js b/src/components/views/elements/MessageEditor.js index 9f5265cfd3..0830708701 100644 --- a/src/components/views/elements/MessageEditor.js +++ b/src/components/views/elements/MessageEditor.js @@ -78,6 +78,14 @@ export default class MessageEditor extends React.Component { this.model.update(text, event.inputType, caret); } + _insertText(textToInsert, inputType = "insertText") { + const sel = document.getSelection(); + const {caret, text} = getCaretOffsetAndText(this._editorRef, sel); + const newText = text.substr(0, caret.offset) + textToInsert + text.substr(caret.offset); + caret.offset += textToInsert.length; + this.model.update(newText, inputType, caret); + } + _isCaretAtStart() { const {caret} = getCaretOffsetAndText(this._editorRef, document.getSelection()); return caret.offset === 0; @@ -92,7 +100,7 @@ export default class MessageEditor extends React.Component { // insert newline on Shift+Enter if (event.shiftKey && event.key === "Enter") { event.preventDefault(); // just in case the browser does support this - document.execCommand("insertHTML", undefined, "\n"); + this._insertText("\n"); return; } // autocomplete or enter to send below shouldn't have any modifier keys pressed.