@@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616import React from 'react' ;
17-
18- var marked = require ( " marked" ) ;
17+ import type SyntheticKeyboardEvent from 'react/lib/SyntheticKeyboardEvent' ;
18+ import marked from ' marked' ;
1919marked . setOptions ( {
2020 renderer : new marked . Renderer ( ) ,
2121 gfm : true ,
@@ -24,7 +24,7 @@ marked.setOptions({
2424 pedantic : false ,
2525 sanitize : true ,
2626 smartLists : true ,
27- smartypants : false
27+ smartypants : false ,
2828} ) ;
2929
3030import { Editor , EditorState , RichUtils , CompositeDecorator ,
@@ -33,14 +33,14 @@ import {Editor, EditorState, RichUtils, CompositeDecorator,
3333
3434import { stateToMarkdown } from 'draft-js-export-markdown' ;
3535
36- var MatrixClientPeg = require ( " ../../../MatrixClientPeg" ) ;
37- var SlashCommands = require ( "../../../SlashCommands" ) ;
38- var Modal = require ( " ../../../Modal" ) ;
39- var MemberEntry = require ( " ../../../TabCompleteEntries" ) . MemberEntry ;
40- var sdk = require ( '../../../index' ) ;
36+ import MatrixClientPeg from ' ../../../MatrixClientPeg' ;
37+ import type { MatrixClient } from 'matrix-js-sdk/lib/matrix' ;
38+ import SlashCommands from ' ../../../SlashCommands' ;
39+ import Modal from ' ../../../Modal' ;
40+ import sdk from '../../../index' ;
4141
42- var dis = require ( " ../../../dispatcher" ) ;
43- var KeyCode = require ( " ../../../KeyCode" ) ;
42+ import dis from ' ../../../dispatcher' ;
43+ import KeyCode from ' ../../../KeyCode' ;
4444
4545import * as RichText from '../../../RichText' ;
4646
@@ -49,8 +49,8 @@ const TYPING_USER_TIMEOUT = 10000, TYPING_SERVER_TIMEOUT = 30000;
4949const KEY_M = 77 ;
5050
5151// FIXME Breaks markdown with multiple paragraphs, since it only strips first and last <p>
52- function mdownToHtml ( mdown ) {
53- var html = marked ( mdown ) || "" ;
52+ function mdownToHtml ( mdown : string ) : string {
53+ let html = marked ( mdown ) || "" ;
5454 html = html . trim ( ) ;
5555 // strip start and end <p> tags else you get 'orrible spacing
5656 if ( html . indexOf ( "<p>" ) === 0 ) {
@@ -66,6 +66,17 @@ function mdownToHtml(mdown) {
6666 * The textInput part of the MessageComposer
6767 */
6868export default class MessageComposerInput extends React . Component {
69+ static getKeyBinding ( e : SyntheticKeyboardEvent ) : string {
70+ // C-m => Toggles between rich text and markdown modes
71+ if ( e . keyCode === KEY_M && KeyBindingUtil . isCtrlKeyCommand ( e ) ) {
72+ return 'toggle-mode' ;
73+ }
74+
75+ return getDefaultKeyBinding ( e ) ;
76+ }
77+
78+ client : MatrixClient ;
79+
6980 constructor ( props , context ) {
7081 super ( props , context ) ;
7182 this . onAction = this . onAction . bind ( this ) ;
@@ -79,7 +90,7 @@ export default class MessageComposerInput extends React.Component {
7990 this . onConfirmAutocompletion = this . onConfirmAutocompletion . bind ( this ) ;
8091
8192 let isRichtextEnabled = window . localStorage . getItem ( 'mx_editor_rte_enabled' ) ;
82- if ( isRichtextEnabled == null ) {
93+ if ( isRichtextEnabled == null ) {
8394 isRichtextEnabled = 'true' ;
8495 }
8596 isRichtextEnabled = isRichtextEnabled === 'true' ;
@@ -95,15 +106,6 @@ export default class MessageComposerInput extends React.Component {
95106 this . client = MatrixClientPeg . get ( ) ;
96107 }
97108
98- static getKeyBinding ( e : SyntheticKeyboardEvent ) : string {
99- // C-m => Toggles between rich text and markdown modes
100- if ( e . keyCode === KEY_M && KeyBindingUtil . isCtrlKeyCommand ( e ) ) {
101- return 'toggle-mode' ;
102- }
103-
104- return getDefaultKeyBinding ( e ) ;
105- }
106-
107109 /**
108110 * "Does the right thing" to create an EditorState, based on:
109111 * - whether we've got rich text mode enabled
@@ -347,15 +349,16 @@ export default class MessageComposerInput extends React.Component {
347349 }
348350
349351 setEditorState ( editorState : EditorState ) {
352+ editorState = RichText . attachImmutableEntitiesToEmoji ( editorState ) ;
350353 this . setState ( { editorState} ) ;
351354
352- if ( editorState . getCurrentContent ( ) . hasText ( ) ) {
353- this . onTypingActivity ( )
355+ if ( editorState . getCurrentContent ( ) . hasText ( ) ) {
356+ this . onTypingActivity ( ) ;
354357 } else {
355358 this . onFinishedTyping ( ) ;
356359 }
357360
358- if ( this . props . onContentChanged ) {
361+ if ( this . props . onContentChanged ) {
359362 this . props . onContentChanged ( editorState . getCurrentContent ( ) . getPlainText ( ) ,
360363 RichText . selectionStateToTextOffsets ( editorState . getSelection ( ) ,
361364 editorState . getCurrentContent ( ) . getBlocksAsArray ( ) ) ) ;
@@ -380,26 +383,26 @@ export default class MessageComposerInput extends React.Component {
380383 }
381384
382385 handleKeyCommand ( command : string ) : boolean {
383- if ( command === 'toggle-mode' ) {
386+ if ( command === 'toggle-mode' ) {
384387 this . enableRichtext ( ! this . state . isRichtextEnabled ) ;
385388 return true ;
386389 }
387390
388391 let newState : ?EditorState = null ;
389392
390393 // Draft handles rich text mode commands by default but we need to do it ourselves for Markdown.
391- if ( ! this . state . isRichtextEnabled ) {
394+ if ( ! this . state . isRichtextEnabled ) {
392395 let contentState = this . state . editorState . getCurrentContent ( ) ,
393396 selection = this . state . editorState . getSelection ( ) ;
394397
395398 let modifyFn = {
396399 bold : text => `**${ text } **` ,
397400 italic : text => `*${ text } *` ,
398401 underline : text => `_${ text } _` , // there's actually no valid underline in Markdown, but *shrug*
399- code : text => `\`${ text } \``
402+ code : text => `\`${ text } \`` ,
400403 } [ command ] ;
401404
402- if ( modifyFn ) {
405+ if ( modifyFn ) {
403406 newState = EditorState . push (
404407 this . state . editorState ,
405408 RichText . modifyText ( contentState , selection , modifyFn ) ,
@@ -408,7 +411,7 @@ export default class MessageComposerInput extends React.Component {
408411 }
409412 }
410413
411- if ( newState == null )
414+ if ( newState == null )
412415 newState = RichUtils . handleKeyCommand ( this . state . editorState , command ) ;
413416
414417 if ( newState != null ) {
@@ -533,9 +536,11 @@ export default class MessageComposerInput extends React.Component {
533536 content
534537 ) ;
535538
536- this . setState ( {
537- editorState : EditorState . push ( this . state . editorState , contentState , 'insert-characters' ) ,
538- } ) ;
539+ let editorState = EditorState . push ( this . state . editorState , contentState , 'insert-characters' ) ;
540+
541+ editorState = EditorState . forceSelection ( editorState , contentState . getSelectionAfter ( ) ) ;
542+
543+ this . setEditorState ( editorState ) ;
539544
540545 // for some reason, doing this right away does not update the editor :(
541546 setTimeout ( ( ) => this . refs . editor . focus ( ) , 50 ) ;
0 commit comments