Fix highlighting behaviour on switch

Hopefully made the code a little bit clearer.
pull/21833/head
Pedro Ferreira 2016-06-11 23:51:18 +02:00
parent a5a3e4e915
commit 8f45f168d5
1 changed files with 16 additions and 13 deletions

View File

@ -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)
});
}