From 6685cbcb25e76abd9304319e6f2f16dfc53e58f8 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sat, 29 Apr 2017 06:13:03 +0100 Subject: [PATCH] make MessageComposerInput (new and old) warn on unload new needs binding due to class this ref being softer couldn't do this nicely in MessageComposer/Input as isTyping wasn't propagated. Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/rooms/MessageComposerInput.js | 10 ++++++++++ src/components/views/rooms/MessageComposerInputOld.js | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 8efd2fa579..672279ef54 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -93,6 +93,7 @@ export default class MessageComposerInput extends React.Component { this.onEscape = this.onEscape.bind(this); this.setDisplayedCompletion = this.setDisplayedCompletion.bind(this); this.onMarkdownToggleClicked = this.onMarkdownToggleClicked.bind(this); + this.onPageUnload = this.onPageUnload.bind(this); const isRichtextEnabled = UserSettingsStore.getSyncedSetting('MessageComposerInput.isRichTextEnabled', false); @@ -233,11 +234,13 @@ export default class MessageComposerInput extends React.Component { this.refs.editor, this.props.room.roomId ); + window.addEventListener('beforeunload', this.onPageUnload); } componentWillUnmount() { dis.unregister(this.dispatcherRef); this.sentHistory.saveLastTextEntry(); + window.removeEventListener('beforeunload', this.onPageUnload); } componentWillUpdate(nextProps, nextState) { @@ -249,6 +252,13 @@ export default class MessageComposerInput extends React.Component { } } + onPageUnload(event) { + if (this.isTyping) { + return event.returnValue = + 'You seem to be typing a message, are you sure you want to quit?'; + } + } + onAction(payload) { let editor = this.refs.editor; let contentState = this.state.editorState.getCurrentContent(); diff --git a/src/components/views/rooms/MessageComposerInputOld.js b/src/components/views/rooms/MessageComposerInputOld.js index 378644478c..2a992cdf5f 100644 --- a/src/components/views/rooms/MessageComposerInputOld.js +++ b/src/components/views/rooms/MessageComposerInputOld.js @@ -177,11 +177,20 @@ export default React.createClass({ if (this.props.tabComplete) { this.props.tabComplete.setTextArea(this.refs.textarea); } + window.addEventListener('beforeunload', this.onPageUnload); }, componentWillUnmount: function() { dis.unregister(this.dispatcherRef); this.sentHistory.saveLastTextEntry(); + window.removeEventListener('beforeunload', this.onPageUnload); + }, + + onPageUnload(event) { + if (this.isTyping) { + return event.returnValue = + 'You seem to be typing a message, are you sure you want to quit?'; + } }, onAction: function(payload) {