From 8f45f168d5224e56dc1cdb5cf83e5a498cbb76c6 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Sat, 11 Jun 2016 23:51:18 +0200 Subject: [PATCH] Fix highlighting behaviour on switch Hopefully made the code a little bit clearer. --- .../views/rooms/MessageComposerInput.js | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index eb096d5bc3..da61631b4d 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -97,13 +97,16 @@ export default class MessageComposerInput extends React.Component { * - whether we've got rich text mode enabled * - contentState was passed in */ - createEditorState(contentState: ?ContentState): EditorState { - let func = contentState ? EditorState.createWithContent : EditorState.createEmpty; - const decoratorFunc = this.state.isRichtextEnabled ? RichText.getScopedRTDecorators : - RichText.getScopedMDDecorators; - let args = contentState ? [contentState] : []; - args.push(new CompositeDecorator(decoratorFunc(this.props))); - return func(...args); + createEditorState(richText: boolean, contentState: ?ContentState): EditorState { + let decorators = richText ? RichText.getScopedRTDecorators(this.props) : + RichText.getScopedMDDecorators(this.props), + compositeDecorator = new CompositeDecorator(decorators); + + if (contentState) { + return EditorState.createWithContent(contentState, compositeDecorator); + } else { + return EditorState.createEmpty(compositeDecorator); + } } componentWillMount() { @@ -194,7 +197,7 @@ export default class MessageComposerInput extends React.Component { if (contentJSON) { let content = convertFromRaw(JSON.parse(contentJSON)); component.setState({ - editorState: component.createEditorState(content) + editorState: component.createEditorState(this.state.isRichtextEnabled, content) }); } } @@ -341,16 +344,16 @@ export default class MessageComposerInput extends React.Component { } enableRichtext(enabled: boolean) { - if(enabled) { + if (enabled) { let html = mdownToHtml(this.state.editorState.getCurrentContent().getPlainText()); this.setState({ - editorState: this.createEditorState(RichText.HTMLtoContentState(html)) + editorState: this.createEditorState(enabled, RichText.HTMLtoContentState(html)) }); } else { - let markdown = stateToMarkdown(this.state.editorState.getCurrentContent()); - let contentState = ContentState.createFromText(markdown); + let markdown = stateToMarkdown(this.state.editorState.getCurrentContent()), + contentState = ContentState.createFromText(markdown); this.setState({ - editorState: this.createEditorState(contentState) + editorState: this.createEditorState(enabled, contentState) }); }