@@ -29,7 +29,6 @@ import {
2929} from '../../../editor/serialize' ;
3030import { CommandPartCreator } from '../../../editor/parts' ;
3131import BasicMessageComposer from "./BasicMessageComposer" ;
32- import RoomViewStore from '../../../stores/RoomViewStore' ;
3332import ReplyThread from "../elements/ReplyThread" ;
3433import { parseEvent } from '../../../editor/deserialize' ;
3534import { findEditableEvent } from '../../../utils/EventUtils' ;
@@ -41,7 +40,6 @@ import {_t, _td} from '../../../languageHandler';
4140import ContentMessages from '../../../ContentMessages' ;
4241import { Key } from "../../../Keyboard" ;
4342import MatrixClientContext from "../../../contexts/MatrixClientContext" ;
44- import { MatrixClientPeg } from "../../../MatrixClientPeg" ;
4543import RateLimitedFunc from '../../../ratelimitedfunc' ;
4644import { Action } from "../../../dispatcher/actions" ;
4745
@@ -61,7 +59,7 @@ function addReplyToMessageContent(content, repliedToEvent, permalinkCreator) {
6159}
6260
6361// exported for tests
64- export function createMessageContent ( model , permalinkCreator ) {
62+ export function createMessageContent ( model , permalinkCreator , replyToEvent ) {
6563 const isEmote = containsEmote ( model ) ;
6664 if ( isEmote ) {
6765 model = stripEmoteCommand ( model ) ;
@@ -70,21 +68,20 @@ export function createMessageContent(model, permalinkCreator) {
7068 model = stripPrefix ( model , "/" ) ;
7169 }
7270 model = unescapeMessage ( model ) ;
73- const repliedToEvent = RoomViewStore . getQuotingEvent ( ) ;
7471
7572 const body = textSerialize ( model ) ;
7673 const content = {
7774 msgtype : isEmote ? "m.emote" : "m.text" ,
7875 body : body ,
7976 } ;
80- const formattedBody = htmlSerializeIfNeeded ( model , { forceHTML : ! ! repliedToEvent } ) ;
77+ const formattedBody = htmlSerializeIfNeeded ( model , { forceHTML : ! ! replyToEvent } ) ;
8178 if ( formattedBody ) {
8279 content . format = "org.matrix.custom.html" ;
8380 content . formatted_body = formattedBody ;
8481 }
8582
86- if ( repliedToEvent ) {
87- addReplyToMessageContent ( content , repliedToEvent , permalinkCreator ) ;
83+ if ( replyToEvent ) {
84+ addReplyToMessageContent ( content , replyToEvent , permalinkCreator ) ;
8885 }
8986
9087 return content ;
@@ -95,6 +92,7 @@ export default class SendMessageComposer extends React.Component {
9592 room : PropTypes . object . isRequired ,
9693 placeholder : PropTypes . string ,
9794 permalinkCreator : PropTypes . object . isRequired ,
95+ replyToEvent : PropTypes . object ,
9896 } ;
9997
10098 static contextType = MatrixClientContext ;
@@ -104,12 +102,13 @@ export default class SendMessageComposer extends React.Component {
104102 this . model = null ;
105103 this . _editorRef = null ;
106104 this . currentlyComposedEditorState = null ;
107- const cli = MatrixClientPeg . get ( ) ;
108- if ( cli . isCryptoEnabled ( ) && cli . isRoomEncrypted ( this . props . room . roomId ) ) {
105+ if ( this . context . isCryptoEnabled ( ) && this . context . isRoomEncrypted ( this . props . room . roomId ) ) {
109106 this . _prepareToEncrypt = new RateLimitedFunc ( ( ) => {
110- cli . prepareToEncrypt ( this . props . room ) ;
107+ this . context . prepareToEncrypt ( this . props . room ) ;
111108 } , 60000 ) ;
112109 }
110+
111+ window . addEventListener ( "beforeunload" , this . _saveStoredEditorState ) ;
113112 }
114113
115114 _setEditorRef = ref => {
@@ -145,7 +144,7 @@ export default class SendMessageComposer extends React.Component {
145144 if ( e . shiftKey || e . metaKey ) return ;
146145
147146 const shouldSelectHistory = e . altKey && e . ctrlKey ;
148- const shouldEditLastMessage = ! e . altKey && ! e . ctrlKey && up && ! RoomViewStore . getQuotingEvent ( ) ;
147+ const shouldEditLastMessage = ! e . altKey && ! e . ctrlKey && up && ! this . props . replyToEvent ;
149148
150149 if ( shouldSelectHistory ) {
151150 // Try select composer history
@@ -187,9 +186,13 @@ export default class SendMessageComposer extends React.Component {
187186 this . sendHistoryManager . currentIndex = this . sendHistoryManager . history . length ;
188187 return ;
189188 }
190- const serializedParts = this . sendHistoryManager . getItem ( delta ) ;
191- if ( serializedParts ) {
192- this . model . reset ( serializedParts ) ;
189+ const { parts, replyEventId} = this . sendHistoryManager . getItem ( delta ) ;
190+ dis . dispatch ( {
191+ action : 'reply_to_event' ,
192+ event : replyEventId ? this . props . room . findEventById ( replyEventId ) : null ,
193+ } ) ;
194+ if ( parts ) {
195+ this . model . reset ( parts ) ;
193196 this . _editorRef . focus ( ) ;
194197 }
195198 }
@@ -299,12 +302,12 @@ export default class SendMessageComposer extends React.Component {
299302 }
300303 }
301304
305+ const replyToEvent = this . props . replyToEvent ;
302306 if ( shouldSend ) {
303- const isReply = ! ! RoomViewStore . getQuotingEvent ( ) ;
304307 const { roomId} = this . props . room ;
305- const content = createMessageContent ( this . model , this . props . permalinkCreator ) ;
308+ const content = createMessageContent ( this . model , this . props . permalinkCreator , replyToEvent ) ;
306309 this . context . sendMessage ( roomId , content ) ;
307- if ( isReply ) {
310+ if ( replyToEvent ) {
308311 // Clear reply_to_event as we put the message into the queue
309312 // if the send fails, retry will handle resending.
310313 dis . dispatch ( {
@@ -315,7 +318,7 @@ export default class SendMessageComposer extends React.Component {
315318 dis . dispatch ( { action : "message_sent" } ) ;
316319 }
317320
318- this . sendHistoryManager . save ( this . model ) ;
321+ this . sendHistoryManager . save ( this . model , replyToEvent ) ;
319322 // clear composer
320323 this . model . reset ( [ ] ) ;
321324 this . _editorRef . clearUndoHistory ( ) ;
@@ -325,6 +328,8 @@ export default class SendMessageComposer extends React.Component {
325328
326329 componentWillUnmount ( ) {
327330 dis . unregister ( this . dispatcherRef ) ;
331+ window . removeEventListener ( "beforeunload" , this . _saveStoredEditorState ) ;
332+ this . _saveStoredEditorState ( ) ;
328333 }
329334
330335 // TODO: [REACT-WARNING] Move this to constructor
@@ -333,11 +338,11 @@ export default class SendMessageComposer extends React.Component {
333338 const parts = this . _restoreStoredEditorState ( partCreator ) || [ ] ;
334339 this . model = new EditorModel ( parts , partCreator ) ;
335340 this . dispatcherRef = dis . register ( this . onAction ) ;
336- this . sendHistoryManager = new SendHistoryManager ( this . props . room . roomId , 'mx_cider_composer_history_ ' ) ;
341+ this . sendHistoryManager = new SendHistoryManager ( this . props . room . roomId , 'mx_cider_history_ ' ) ;
337342 }
338343
339344 get _editorStateKey ( ) {
340- return `cider_editor_state_ ${ this . props . room . roomId } ` ;
345+ return `mx_cider_state_ ${ this . props . room . roomId } ` ;
341346 }
342347
343348 _clearStoredEditorState ( ) {
@@ -347,17 +352,28 @@ export default class SendMessageComposer extends React.Component {
347352 _restoreStoredEditorState ( partCreator ) {
348353 const json = localStorage . getItem ( this . _editorStateKey ) ;
349354 if ( json ) {
350- const serializedParts = JSON . parse ( json ) ;
351- const parts = serializedParts . map ( p => partCreator . deserializePart ( p ) ) ;
352- return parts ;
355+ try {
356+ const { parts : serializedParts , replyEventId} = JSON . parse ( json ) ;
357+ const parts = serializedParts . map ( p => partCreator . deserializePart ( p ) ) ;
358+ if ( replyEventId ) {
359+ dis . dispatch ( {
360+ action : 'reply_to_event' ,
361+ event : this . props . room . findEventById ( replyEventId ) ,
362+ } ) ;
363+ }
364+ return parts ;
365+ } catch ( e ) {
366+ console . error ( e ) ;
367+ }
353368 }
354369 }
355370
356371 _saveStoredEditorState = ( ) => {
357372 if ( this . model . isEmpty ) {
358373 this . _clearStoredEditorState ( ) ;
359374 } else {
360- localStorage . setItem ( this . _editorStateKey , JSON . stringify ( this . model . serializeParts ( ) ) ) ;
375+ const item = SendHistoryManager . createItem ( this . model , this . props . replyToEvent ) ;
376+ localStorage . setItem ( this . _editorStateKey , JSON . stringify ( item ) ) ;
361377 }
362378 }
363379
@@ -449,7 +465,6 @@ export default class SendMessageComposer extends React.Component {
449465 room = { this . props . room }
450466 label = { this . props . placeholder }
451467 placeholder = { this . props . placeholder }
452- onChange = { this . _saveStoredEditorState }
453468 onPaste = { this . _onPaste }
454469 />
455470 </ div >
0 commit comments