@@ -42,7 +42,7 @@ var sdk = require('../../../index');
4242var dis = require ( "../../../dispatcher" ) ;
4343var KeyCode = require ( "../../../KeyCode" ) ;
4444
45- import { contentStateToHTML , HTMLtoContentState , getScopedDecorator } from '../../../RichText' ;
45+ import * as RichText from '../../../RichText' ;
4646
4747const TYPING_USER_TIMEOUT = 10000 , TYPING_SERVER_TIMEOUT = 30000 ;
4848
@@ -69,7 +69,7 @@ export default class MessageComposerInput extends React.Component {
6969 this . onInputClick = this . onInputClick . bind ( this ) ;
7070
7171 this . state = {
72- isRichtextEnabled : true ,
72+ isRichtextEnabled : false ,
7373 editorState : null
7474 } ;
7575
@@ -95,7 +95,7 @@ export default class MessageComposerInput extends React.Component {
9595 let func = contentState ? EditorState . createWithContent : EditorState . createEmpty ;
9696 let args = contentState ? [ contentState ] : [ ] ;
9797 if ( this . state . isRichtextEnabled ) {
98- args . push ( getScopedDecorator ( this . props ) ) ;
98+ args . push ( RichText . getScopedDecorator ( this . props ) ) ;
9999 }
100100 return func . apply ( null , args ) ;
101101 }
@@ -114,7 +114,7 @@ export default class MessageComposerInput extends React.Component {
114114 // The textarea element to set text to.
115115 element : null ,
116116
117- init : function ( element , roomId ) {
117+ init : function ( element , roomId ) {
118118 this . roomId = roomId ;
119119 this . element = element ;
120120 this . position = - 1 ;
@@ -129,7 +129,7 @@ export default class MessageComposerInput extends React.Component {
129129 }
130130 } ,
131131
132- push : function ( text ) {
132+ push : function ( text ) {
133133 // store a message in the sent history
134134 this . data . unshift ( text ) ;
135135 window . sessionStorage . setItem (
@@ -142,7 +142,7 @@ export default class MessageComposerInput extends React.Component {
142142 } ,
143143
144144 // move in the history. Returns true if we managed to move.
145- next : function ( offset ) {
145+ next : function ( offset ) {
146146 if ( this . position === - 1 ) {
147147 // user is going into the history, save the current line.
148148 this . originalText = this . element . value ;
@@ -175,15 +175,15 @@ export default class MessageComposerInput extends React.Component {
175175 return true ;
176176 } ,
177177
178- saveLastTextEntry : function ( ) {
178+ saveLastTextEntry : function ( ) {
179179 // save the currently entered text in order to restore it later.
180180 // NB: This isn't 'originalText' because we want to restore
181181 // sent history items too!
182182 let contentJSON = JSON . stringify ( convertToRaw ( component . state . editorState . getCurrentContent ( ) ) ) ;
183183 window . sessionStorage . setItem ( "input_" + this . roomId , contentJSON ) ;
184184 } ,
185185
186- setLastTextEntry : function ( ) {
186+ setLastTextEntry : function ( ) {
187187 let contentJSON = window . sessionStorage . getItem ( "input_" + this . roomId ) ;
188188 if ( contentJSON ) {
189189 let content = convertFromRaw ( JSON . parse ( contentJSON ) ) ;
@@ -404,7 +404,7 @@ export default class MessageComposerInput extends React.Component {
404404 this . refs . editor . focus ( ) ;
405405 }
406406
407- onChange ( editorState ) {
407+ onChange ( editorState : EditorState ) {
408408 this . setState ( { editorState} ) ;
409409
410410 if ( editorState . getCurrentContent ( ) . hasText ( ) ) {
@@ -414,30 +414,60 @@ export default class MessageComposerInput extends React.Component {
414414 }
415415 }
416416
417- handleKeyCommand ( command ) {
418- if ( command === 'toggle-mode' ) {
417+ enableRichtext ( enabled : boolean ) {
418+ this . setState ( {
419+ isRichtextEnabled : enabled
420+ } ) ;
421+
422+ if ( ! this . state . isRichtextEnabled ) {
423+ let html = mdownToHtml ( this . state . editorState . getCurrentContent ( ) . getPlainText ( ) ) ;
419424 this . setState ( {
420- isRichtextEnabled : ! this . state . isRichtextEnabled
425+ editorState : this . createEditorState ( RichText . HTMLtoContentState ( html ) )
421426 } ) ;
427+ } else {
428+ let markdown = stateToMarkdown ( this . state . editorState . getCurrentContent ( ) ) ;
429+ let contentState = ContentState . createFromText ( markdown ) ;
430+ this . setState ( {
431+ editorState : this . createEditorState ( contentState )
432+ } ) ;
433+ }
434+ }
422435
423- if ( ! this . state . isRichtextEnabled ) {
424- let html = mdownToHtml ( this . state . editorState . getCurrentContent ( ) . getPlainText ( ) ) ;
425- this . setState ( {
426- editorState : this . createEditorState ( HTMLtoContentState ( html ) )
427- } ) ;
428- } else {
429- let markdown = stateToMarkdown ( this . state . editorState . getCurrentContent ( ) ) ;
430- let contentState = ContentState . createFromText ( markdown ) ;
431- this . setState ( {
432- editorState : this . createEditorState ( contentState )
433- } ) ;
434- }
435-
436+ handleKeyCommand ( command : string ) : boolean {
437+ if ( command === 'toggle-mode' ) {
438+ this . enableRichtext ( ! this . state . isRichtextEnabled ) ;
436439 return true ;
437440 }
438441
439- let newState = RichUtils . handleKeyCommand ( this . state . editorState , command ) ;
440- if ( newState ) {
442+ let newState : ?EditorState = null ;
443+
444+ // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown.
445+ if ( ! this . state . isRichtextEnabled ) {
446+ let contentState = this . state . editorState . getCurrentContent ( ) ,
447+ selection = this . state . editorState . getSelection ( ) ;
448+
449+ let modifyFn = {
450+ bold : text => `**${ text } **` ,
451+ italic : text => `*${ text } *` ,
452+ underline : text => `_${ text } _` , // there's actually no valid underline in Markdown, but *shrug*
453+ code : text => `\`${ text } \``
454+ } [ command ] ;
455+
456+ if ( modifyFn ) {
457+ newState = EditorState . push (
458+ this . state . editorState ,
459+ RichText . modifyText ( contentState , selection , modifyFn ) ,
460+ 'insert-characters'
461+ ) ;
462+ }
463+ console . log ( modifyFn ) ;
464+ console . log ( newState ) ;
465+ }
466+
467+ if ( newState == null )
468+ newState = RichUtils . handleKeyCommand ( this . state . editorState , command ) ;
469+
470+ if ( newState != null ) {
441471 this . onChange ( newState ) ;
442472 return true ;
443473 }
@@ -455,7 +485,7 @@ export default class MessageComposerInput extends React.Component {
455485 let contentText = contentState . getPlainText ( ) , contentHTML ;
456486
457487 if ( this . state . isRichtextEnabled ) {
458- contentHTML = contentStateToHTML ( contentState ) ;
488+ contentHTML = RichText . contentStateToHTML ( contentState ) ;
459489 } else {
460490 contentHTML = mdownToHtml ( contentText ) ;
461491 }
0 commit comments