From e5e7dec1318cd3c0b12df79a6f787dd60bfa81e4 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Thu, 29 Jun 2017 17:02:19 +0100 Subject: [PATCH] Fix #4422 by persisting the "currently composed" message This allows for browsing through history without losing the message currently being composed. --- src/ComposerHistoryManager.js | 12 +++++----- .../views/rooms/MessageComposerInput.js | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/ComposerHistoryManager.js b/src/ComposerHistoryManager.js index 3e19a78bfe..1ae836574b 100644 --- a/src/ComposerHistoryManager.js +++ b/src/ComposerHistoryManager.js @@ -52,21 +52,19 @@ export default class ComposerHistoryManager { history: Array = []; prefix: string; lastIndex: number = 0; - currentIndex: number = -1; + currentIndex: number = 0; constructor(roomId: string, prefix: string = 'mx_composer_history_') { this.prefix = prefix + roomId; // TODO: Performance issues? - for(; sessionStorage.getItem(`${this.prefix}[${this.lastIndex}]`); this.lastIndex++, this.currentIndex++) { + let item; + for(; item = sessionStorage.getItem(`${this.prefix}[${this.currentIndex}]`); this.currentIndex++) { this.history.push( - Object.assign( - new HistoryItem(), - JSON.parse(sessionStorage.getItem(`${this.prefix}[${this.lastIndex}]`)), - ), + Object.assign(new HistoryItem(), JSON.parse(item)), ); } - this.currentIndex--; + this.lastIndex = this.currentIndex; } addItem(message: string, format: MessageFormat) { diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index eadd807c08..8764356365 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -134,6 +134,10 @@ export default class MessageComposerInput extends React.Component { // the original editor state, before we started tabbing through completions originalEditorState: null, + + // the virtual state "above" the history stack, the message currently being composed that + // we want to persist whilst browsing history + currentlyComposedEditorState: null, }; // bit of a hack, but we need to do this here since createEditorState needs isRichtextEnabled @@ -579,6 +583,24 @@ export default class MessageComposerInput extends React.Component { selectHistory = async (up) => { const delta = up ? -1 : 1; + // True if we are not currently selecting history, but composing a message + if (this.historyManager.currentIndex === this.historyManager.history.length) { + // We can't go any further - there isn't any more history, so nop. + if (!up) { + return; + } + this.setState({ + currentlyComposedEditorState: this.state.editorState, + }); + } else if (this.historyManager.currentIndex + delta === this.historyManager.history.length) { + // True when we return to the message being composed currently + this.setState({ + editorState: this.state.currentlyComposedEditorState, + }); + this.historyManager.currentIndex = this.historyManager.history.length; + return; + } + const newContent = this.historyManager.getItem(delta, this.state.isRichtextEnabled ? 'html' : 'markdown'); if (!newContent) return false; let editorState = EditorState.push(