@@ -15,36 +15,38 @@ See the License for the specific language governing permissions and
1515limitations under the License.
1616*/
1717
18- import { ContentState } from 'draft-js' ;
18+ import { ContentState , convertToRaw , convertFromRaw } from 'draft-js' ;
1919import * as RichText from './RichText' ;
2020import Markdown from './Markdown' ;
21- import _flow from 'lodash/flow' ;
2221import _clamp from 'lodash/clamp' ;
2322
2423type MessageFormat = 'html' | 'markdown' ;
2524
2625class HistoryItem {
27- message : string = '' ;
26+
27+ // Keeping message for backwards-compatibility
28+ message : string ;
29+ rawContentState : RawDraftContentState ;
2830 format : MessageFormat = 'html' ;
2931
30- constructor ( message : string , format : MessageFormat ) {
31- this . message = message ;
32+ constructor ( contentState : ? ContentState , format : ? MessageFormat ) {
33+ this . rawContentState = contentState ? convertToRaw ( contentState ) : null ;
3234 this . format = format ;
3335 }
3436
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' ) {
3840 if ( this . format === 'html' ) {
39- message = _flow ( [ RichText . htmlToContentState , RichText . stateToMarkdown ] ) ( message ) ;
41+ return ContentState . createFromText ( RichText . stateToMarkdown ( contentState ) ) ;
4042 }
41- return ContentState . createFromText ( message ) ;
4243 } else {
4344 if ( this . format === 'markdown' ) {
44- message = new Markdown ( message ) . toHTML ( ) ;
45+ return RichText . htmlToContentState ( new Markdown ( contentState . getPlainText ( ) ) . toHTML ( ) ) ;
4546 }
46- return RichText . htmlToContentState ( message ) ;
4747 }
48+ // history item has format === outputFormat
49+ return contentState ;
4850 }
4951}
5052
@@ -67,8 +69,8 @@ export default class ComposerHistoryManager {
6769 this.lastIndex = this.currentIndex;
6870 }
6971
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 ) ;
7274 this . history . push ( item ) ;
7375 this . currentIndex = this . lastIndex + 1 ;
7476 sessionStorage . setItem ( `${ this . prefix } [${ this . lastIndex ++ } ]` , JSON . stringify ( item ) ) ;
0 commit comments