@@ -15,36 +15,38 @@ See the License for the specific language governing permissions and
15
15
limitations under the License.
16
16
*/
17
17
18
- import { ContentState } from 'draft-js' ;
18
+ import { ContentState , convertToRaw , convertFromRaw } from 'draft-js' ;
19
19
import * as RichText from './RichText' ;
20
20
import Markdown from './Markdown' ;
21
- import _flow from 'lodash/flow' ;
22
21
import _clamp from 'lodash/clamp' ;
23
22
24
23
type MessageFormat = 'html' | 'markdown' ;
25
24
26
25
class HistoryItem {
27
- message : string = '' ;
26
+
27
+ // Keeping message for backwards-compatibility
28
+ message : string ;
29
+ rawContentState : RawDraftContentState ;
28
30
format : MessageFormat = 'html' ;
29
31
30
- constructor ( message : string , format : MessageFormat ) {
31
- this . message = message ;
32
+ constructor ( contentState : ? ContentState , format : ? MessageFormat ) {
33
+ this . rawContentState = contentState ? convertToRaw ( contentState ) : null ;
32
34
this . format = format ;
33
35
}
34
36
35
- toContentState ( format : MessageFormat ) : ContentState {
36
- let { message } = this ;
37
- if ( format === 'markdown' ) {
37
+ toContentState ( outputFormat : MessageFormat ) : ContentState {
38
+ const contentState = convertFromRaw ( this . rawContentState ) ;
39
+ if ( outputFormat === 'markdown' ) {
38
40
if ( this . format === 'html' ) {
39
- message = _flow ( [ RichText . htmlToContentState , RichText . stateToMarkdown ] ) ( message ) ;
41
+ return ContentState . createFromText ( RichText . stateToMarkdown ( contentState ) ) ;
40
42
}
41
- return ContentState . createFromText ( message ) ;
42
43
} else {
43
44
if ( this . format === 'markdown' ) {
44
- message = new Markdown ( message ) . toHTML ( ) ;
45
+ return RichText . htmlToContentState ( new Markdown ( contentState . getPlainText ( ) ) . toHTML ( ) ) ;
45
46
}
46
- return RichText . htmlToContentState ( message ) ;
47
47
}
48
+ // history item has format === outputFormat
49
+ return contentState ;
48
50
}
49
51
}
50
52
@@ -67,8 +69,8 @@ export default class ComposerHistoryManager {
67
69
this.lastIndex = this.currentIndex;
68
70
}
69
71
70
- addItem ( message : string , format : MessageFormat ) {
71
- const item = new HistoryItem ( message , format ) ;
72
+ save ( contentState : ContentState , format : MessageFormat ) {
73
+ const item = new HistoryItem ( contentState , format ) ;
72
74
this . history . push ( item ) ;
73
75
this . currentIndex = this . lastIndex + 1 ;
74
76
sessionStorage . setItem ( `${ this . prefix } [${ this . lastIndex ++ } ]` , JSON . stringify ( item ) ) ;
0 commit comments