@@ -29,7 +29,6 @@ import {
29
29
} from '../../../editor/serialize' ;
30
30
import { CommandPartCreator } from '../../../editor/parts' ;
31
31
import BasicMessageComposer from "./BasicMessageComposer" ;
32
- import RoomViewStore from '../../../stores/RoomViewStore' ;
33
32
import ReplyThread from "../elements/ReplyThread" ;
34
33
import { parseEvent } from '../../../editor/deserialize' ;
35
34
import { findEditableEvent } from '../../../utils/EventUtils' ;
@@ -41,7 +40,6 @@ import {_t, _td} from '../../../languageHandler';
41
40
import ContentMessages from '../../../ContentMessages' ;
42
41
import { Key } from "../../../Keyboard" ;
43
42
import MatrixClientContext from "../../../contexts/MatrixClientContext" ;
44
- import { MatrixClientPeg } from "../../../MatrixClientPeg" ;
45
43
import RateLimitedFunc from '../../../ratelimitedfunc' ;
46
44
import { Action } from "../../../dispatcher/actions" ;
47
45
@@ -61,7 +59,7 @@ function addReplyToMessageContent(content, repliedToEvent, permalinkCreator) {
61
59
}
62
60
63
61
// exported for tests
64
- export function createMessageContent ( model , permalinkCreator ) {
62
+ export function createMessageContent ( model , permalinkCreator , replyToEvent ) {
65
63
const isEmote = containsEmote ( model ) ;
66
64
if ( isEmote ) {
67
65
model = stripEmoteCommand ( model ) ;
@@ -70,21 +68,20 @@ export function createMessageContent(model, permalinkCreator) {
70
68
model = stripPrefix ( model , "/" ) ;
71
69
}
72
70
model = unescapeMessage ( model ) ;
73
- const repliedToEvent = RoomViewStore . getQuotingEvent ( ) ;
74
71
75
72
const body = textSerialize ( model ) ;
76
73
const content = {
77
74
msgtype : isEmote ? "m.emote" : "m.text" ,
78
75
body : body ,
79
76
} ;
80
- const formattedBody = htmlSerializeIfNeeded ( model , { forceHTML : ! ! repliedToEvent } ) ;
77
+ const formattedBody = htmlSerializeIfNeeded ( model , { forceHTML : ! ! replyToEvent } ) ;
81
78
if ( formattedBody ) {
82
79
content . format = "org.matrix.custom.html" ;
83
80
content . formatted_body = formattedBody ;
84
81
}
85
82
86
- if ( repliedToEvent ) {
87
- addReplyToMessageContent ( content , repliedToEvent , permalinkCreator ) ;
83
+ if ( replyToEvent ) {
84
+ addReplyToMessageContent ( content , replyToEvent , permalinkCreator ) ;
88
85
}
89
86
90
87
return content ;
@@ -95,6 +92,7 @@ export default class SendMessageComposer extends React.Component {
95
92
room : PropTypes . object . isRequired ,
96
93
placeholder : PropTypes . string ,
97
94
permalinkCreator : PropTypes . object . isRequired ,
95
+ replyToEvent : PropTypes . object ,
98
96
} ;
99
97
100
98
static contextType = MatrixClientContext ;
@@ -104,12 +102,13 @@ export default class SendMessageComposer extends React.Component {
104
102
this . model = null ;
105
103
this . _editorRef = null ;
106
104
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 ) ) {
109
106
this . _prepareToEncrypt = new RateLimitedFunc ( ( ) => {
110
- cli . prepareToEncrypt ( this . props . room ) ;
107
+ this . context . prepareToEncrypt ( this . props . room ) ;
111
108
} , 60000 ) ;
112
109
}
110
+
111
+ window . addEventListener ( "beforeunload" , this . _saveStoredEditorState ) ;
113
112
}
114
113
115
114
_setEditorRef = ref => {
@@ -145,7 +144,7 @@ export default class SendMessageComposer extends React.Component {
145
144
if ( e . shiftKey || e . metaKey ) return ;
146
145
147
146
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 ;
149
148
150
149
if ( shouldSelectHistory ) {
151
150
// Try select composer history
@@ -187,9 +186,13 @@ export default class SendMessageComposer extends React.Component {
187
186
this . sendHistoryManager . currentIndex = this . sendHistoryManager . history . length ;
188
187
return ;
189
188
}
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 ) ;
193
196
this . _editorRef . focus ( ) ;
194
197
}
195
198
}
@@ -299,12 +302,12 @@ export default class SendMessageComposer extends React.Component {
299
302
}
300
303
}
301
304
305
+ const replyToEvent = this . props . replyToEvent ;
302
306
if ( shouldSend ) {
303
- const isReply = ! ! RoomViewStore . getQuotingEvent ( ) ;
304
307
const { roomId} = this . props . room ;
305
- const content = createMessageContent ( this . model , this . props . permalinkCreator ) ;
308
+ const content = createMessageContent ( this . model , this . props . permalinkCreator , replyToEvent ) ;
306
309
this . context . sendMessage ( roomId , content ) ;
307
- if ( isReply ) {
310
+ if ( replyToEvent ) {
308
311
// Clear reply_to_event as we put the message into the queue
309
312
// if the send fails, retry will handle resending.
310
313
dis . dispatch ( {
@@ -315,7 +318,7 @@ export default class SendMessageComposer extends React.Component {
315
318
dis . dispatch ( { action : "message_sent" } ) ;
316
319
}
317
320
318
- this . sendHistoryManager . save ( this . model ) ;
321
+ this . sendHistoryManager . save ( this . model , replyToEvent ) ;
319
322
// clear composer
320
323
this . model . reset ( [ ] ) ;
321
324
this . _editorRef . clearUndoHistory ( ) ;
@@ -325,6 +328,8 @@ export default class SendMessageComposer extends React.Component {
325
328
326
329
componentWillUnmount ( ) {
327
330
dis . unregister ( this . dispatcherRef ) ;
331
+ window . removeEventListener ( "beforeunload" , this . _saveStoredEditorState ) ;
332
+ this . _saveStoredEditorState ( ) ;
328
333
}
329
334
330
335
// TODO: [REACT-WARNING] Move this to constructor
@@ -333,11 +338,11 @@ export default class SendMessageComposer extends React.Component {
333
338
const parts = this . _restoreStoredEditorState ( partCreator ) || [ ] ;
334
339
this . model = new EditorModel ( parts , partCreator ) ;
335
340
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_ ' ) ;
337
342
}
338
343
339
344
get _editorStateKey ( ) {
340
- return `cider_editor_state_ ${ this . props . room . roomId } ` ;
345
+ return `mx_cider_state_ ${ this . props . room . roomId } ` ;
341
346
}
342
347
343
348
_clearStoredEditorState ( ) {
@@ -347,17 +352,28 @@ export default class SendMessageComposer extends React.Component {
347
352
_restoreStoredEditorState ( partCreator ) {
348
353
const json = localStorage . getItem ( this . _editorStateKey ) ;
349
354
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
+ }
353
368
}
354
369
}
355
370
356
371
_saveStoredEditorState = ( ) => {
357
372
if ( this . model . isEmpty ) {
358
373
this . _clearStoredEditorState ( ) ;
359
374
} 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 ) ) ;
361
377
}
362
378
}
363
379
@@ -449,7 +465,6 @@ export default class SendMessageComposer extends React.Component {
449
465
room = { this . props . room }
450
466
label = { this . props . placeholder }
451
467
placeholder = { this . props . placeholder }
452
- onChange = { this . _saveStoredEditorState }
453
468
onPaste = { this . _onPaste }
454
469
/>
455
470
</ div >
0 commit comments