From 15cfe5b8e42a7019d13cbb7c76aa5db645644006 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 30 Jun 2017 14:27:26 +0100 Subject: [PATCH] Only allow history selection when on the first or last line (of a multi-line or otherwise message in the editor) --- .../views/rooms/MessageComposerInput.js | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 3e2748e382..4cd5772663 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -593,12 +593,36 @@ export default class MessageComposerInput extends React.Component { }; onVerticalArrow = (e, up) => { - e.preventDefault(); // Select history only if we are not currently auto-completing if (this.autocomplete.state.completionList.length === 0) { - return this.selectHistory(up); + // Don't go back in history if we're in the middle of a multi-line message + const selection = this.state.editorState.getSelection(); + const blockKey = selection.getStartKey(); + const firstBlock = this.state.editorState.getCurrentContent().getFirstBlock(); + const lastBlock = this.state.editorState.getCurrentContent().getLastBlock(); + + const selectionOffset = selection.getAnchorOffset(); + let canMoveUp = false; + let canMoveDown = false; + if (blockKey === firstBlock.getKey()) { + const textBeforeCursor = firstBlock.getText().slice(0, selectionOffset); + canMoveUp = textBeforeCursor.indexOf('\n') === -1; + } + + if (blockKey === lastBlock.getKey()) { + const textAfterCursor = lastBlock.getText().slice(selectionOffset); + canMoveDown = textAfterCursor.indexOf('\n') === -1; + } + + if ((up && !canMoveUp) || (!up && !canMoveDown)) return; + + const selected = this.selectHistory(up); + if (selected) { + // We're selecting history, so prevent the key event from doing anything else + e.preventDefault(); + } } else { - return this.moveAutocompleteSelection(up); + this.moveAutocompleteSelection(up); } };