Code Monkey home page Code Monkey logo

Comments (3)

brijeshb42 avatar brijeshb42 commented on June 7, 2024

createEditorState expects the json value of the result of convertToRaw(editorState). It internally converts the JSON to ContentState. You can take a look here.

If you are initialising the editor only once, as per your code, you can try -

createEditorState (convertToRaw(rawState));

from medium-draft.

crucialfelix avatar crucialfelix commented on June 7, 2024

convertToRaw(rawState) oh, that's counter-intuitive. Seems like it would already be rawState.

Thanks, I'll give it a try !

from medium-draft.

crucialfelix avatar crucialfelix commented on June 7, 2024

Working, thanks !

import React from 'react';
import ReactDOM from 'react-dom';

import 'medium-draft/lib/index.css';
import mediumDraftExporter from 'medium-draft/lib/exporter';
import { convertFromHTML } from 'draft-convert';

// https://github.com/brijeshb42/medium-draft
import {
  Editor,
  createEditorState,
} from 'medium-draft';
import {
  convertToRaw
} from 'draft-js';

export default class TextEditor extends React.Component {

  constructor(props) {
    super(props);

    let editorState;
    if (this.props.text) {
      const rawState = convertToRaw(convertFromHTML(this.props.text));
      editorState = createEditorState(rawState);
    } else {
      editorState = createEditorState();
    }

    this.state = {
      editorState
    };

    // This calls render for every character you type.
    this.onChange = (editorState) => {
      this.setState({ editorState });
    };
  }

  componentDidMount() {
    this.refs.editor.focus();
    this.props.getHtmlCallback(() => this.getHtml());
  }

  getHtml() {
    return mediumDraftExporter(this.state.editorState.getCurrentContent());
  }

  render() {
    return (
      <Editor
        ref='editor'
        editorState={this.state.editorState}
        onChange={this.onChange}
      />
    );
  }
}

from medium-draft.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.