@@ -34,6 +34,7 @@ import {Action} from "../../../dispatcher/actions";
3434import CountlyAnalytics from "../../../CountlyAnalytics" ;
3535import { getKeyBindingsManager , MessageComposerAction } from '../../../KeyBindingsManager' ;
3636import { replaceableComponent } from "../../../utils/replaceableComponent" ;
37+ import SendHistoryManager from '../../../SendHistoryManager' ;
3738
3839function _isReply ( mxEvent ) {
3940 const relatesTo = mxEvent . getContent ( ) [ "m.relates_to" ] ;
@@ -120,6 +121,7 @@ export default class EditMessageComposer extends React.Component {
120121 saveDisabled : true ,
121122 } ;
122123 this . _createEditorModel ( ) ;
124+ window . addEventListener ( "beforeunload" , this . _saveStoredEditorState ) ;
123125 }
124126
125127 _setEditorRef = ref => {
@@ -174,10 +176,43 @@ export default class EditMessageComposer extends React.Component {
174176 }
175177
176178 _cancelEdit = ( ) => {
179+ this . _clearStoredEditorState ( ) ;
177180 dis . dispatch ( { action : "edit_event" , event : null } ) ;
178181 dis . fire ( Action . FocusComposer ) ;
179182 }
180183
184+ get _shouldSaveStoredEditorState ( ) {
185+ return localStorage . getItem ( `mx_edit_state_${ this . props . editState . getEvent ( ) . getRoomId ( ) }
186+ _${ this . props . editState . getEvent ( ) . event . event_id } ` ) !== null ;
187+ }
188+
189+ _restoreStoredEditorState ( partCreator ) {
190+ const json = localStorage . getItem ( this . _editorStateKey ) ;
191+ if ( json ) {
192+ try {
193+ const { parts : serializedParts } = JSON . parse ( json ) ;
194+ const parts = serializedParts . map ( p => partCreator . deserializePart ( p ) ) ;
195+ return parts ;
196+ } catch ( e ) {
197+ console . error ( e ) ;
198+ }
199+ }
200+ }
201+
202+ get _editorStateKey ( ) {
203+ return `mx_edit_state_${ this . props . editState . getEvent ( ) . getRoomId ( ) }
204+ _${ this . props . editState . getEvent ( ) . event . event_id } ` ;
205+ }
206+
207+ _clearStoredEditorState ( ) {
208+ localStorage . removeItem ( this . _editorStateKey ) ;
209+ }
210+
211+ _saveStoredEditorState ( ) {
212+ const item = SendHistoryManager . createItem ( this . model ) ;
213+ localStorage . setItem ( this . _editorStateKey , JSON . stringify ( item ) ) ;
214+ }
215+
181216 _isContentModified ( newContent ) {
182217 // if nothing has changed then bail
183218 const oldContent = this . props . editState . getEvent ( ) . getContent ( ) ;
@@ -195,13 +230,13 @@ export default class EditMessageComposer extends React.Component {
195230 const editedEvent = this . props . editState . getEvent ( ) ;
196231 const editContent = createEditContent ( this . model , editedEvent ) ;
197232 const newContent = editContent [ "m.new_content" ] ;
198-
199233 // If content is modified then send an updated event into the room
200234 if ( this . _isContentModified ( newContent ) ) {
201235 const roomId = editedEvent . getRoomId ( ) ;
202236 this . _cancelPreviousPendingEdit ( ) ;
203237 const prom = this . context . sendMessage ( roomId , editContent ) ;
204238 dis . dispatch ( { action : "message_sent" } ) ;
239+ this . _clearStoredEditorState ( ) ;
205240 CountlyAnalytics . instance . trackSendMessage ( startTime , prom , roomId , true , false , editContent ) ;
206241 }
207242
@@ -235,6 +270,10 @@ export default class EditMessageComposer extends React.Component {
235270 // then when mounting the editor again with the same editor state,
236271 // it will set the cursor at the end.
237272 this . props . editState . setEditorState ( caret , parts ) ;
273+ window . removeEventListener ( "beforeunload" , this . _saveStoredEditorState ) ;
274+ if ( this . _shouldSaveStoredEditorState ) {
275+ this . _saveStoredEditorState ( ) ;
276+ }
238277 }
239278
240279 _createEditorModel ( ) {
@@ -247,10 +286,10 @@ export default class EditMessageComposer extends React.Component {
247286 // restore serialized parts from the state
248287 parts = editState . getSerializedParts ( ) . map ( p => partCreator . deserializePart ( p ) ) ;
249288 } else {
250- // otherwise, parse the body of the event
251- parts = parseEvent ( editState . getEvent ( ) , partCreator ) ;
289+ parts = this . _restoreStoredEditorState ( partCreator ) || parseEvent ( editState . getEvent ( ) , partCreator ) ;
252290 }
253291 this . model = new EditorModel ( parts , partCreator ) ;
292+ this . _saveStoredEditorState ( ) ;
254293 }
255294
256295 _getInitialCaretPosition ( ) {
0 commit comments