Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change text editor to https://github.com/jpuri/react-draft-wysiwyg #1519

Open
Hartorn opened this issue Nov 25, 2017 · 1 comment
Open

Change text editor to https://github.com/jpuri/react-draft-wysiwyg #1519

Hartorn opened this issue Nov 25, 2017 · 1 comment
Assignees

Comments

@Hartorn
Copy link
Contributor

Hartorn commented Nov 25, 2017

No description provided.

@Hartorn
Copy link
Contributor Author

Hartorn commented Dec 12, 2017

https://raw.githubusercontent.com/FightForSub/ffs-zera/master/app/components/rich-text-editor.js

import React, { Component } from 'react';
import { EditorState, convertToRaw, ContentState } from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';

class EditorConvertToHTML extends Component {
    constructor(props) {
        super(props);
        this.state = {
            editorState: this.buildEditorState(this.props.value)
        }
        this.customOnChange = this.customOnChange.bind(this);
        this.catchEvent = this.catchEvent.bind(this);
    }

    buildEditorState(value) {
        const contentBlock = htmlToDraft(value || '');
        const contentState = contentBlock ? ContentState.createFromBlockArray(contentBlock.contentBlocks) : ContentState.createFromText('');
        const editorState = EditorState.createWithContent(contentState);
        return editorState;
    }

    componentWillReceiveProps({ value }) {
        if ((value && !this.props.value) || (!this.props.isEdit && value !== !this.props.value)) {
            this.setState({ editorState: this.buildEditorState(value) });
        }
    }

    customOnChange(editorState) {
        this.setState({
            editorState: editorState
        });
        this.props.onChange(draftToHtml(convertToRaw(editorState.getCurrentContent())));
    }

    catchEvent(evt) {
        evt.preventDefault();
        evt.stopPropagation();
    }

    render() {
        return (
            <div onKeyUp={this.catchEvent} >
                <Editor
                    toolbarHidden={!this.props.isEdit}
                    editorState={this.state.editorState}
                    wrapperClassName='rich-text-wrapper'
                    editorClassName='rich-text-editor'
                    onEditorStateChange={this.customOnChange}
                    readOnly={!this.props.isEdit}
                />
            </div>
        );
    }
}

export default EditorConvertToHTML;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants