0

I'm trying desesperatelly to understand Draft Js logic but I don't find a good tutorial or documentation on the subject.

What I want for the moment is just a function (triggered by a button below) that can add at the end of the editor one of my react component.

Then I would like to be able to edit the content inside and why not change component type without loosing the raw text.

I've seen many many things, API documentation, wierd functions, maps etc... But I still don't know how to do this.

PS: My code is just the draft basic editor for the moment.

Thanks,

Lucien

Lucien Perouze
  • 590
  • 1
  • 4
  • 13
  • What kind of component are you wanting to add? Is it something that uses information from the editor? – Phil Jan 29 '18 at 21:59
  • Basically any data displayer component, for the moment I just want to succeed with a simple component that returns

    Hello World !

    .
    – Lucien Perouze Jan 29 '18 at 22:50

1 Answers1

0

Short answer: Use a container component that holds the editor, button, and the added component.

React solves the problem of building networks of "things" that interact with Composition (as opposed to Inheritance). Create a stateful ("class-based") component, store "all the stuff that changes" inside that component's state, and then you won't lose any data when components inside that component change. Pass data to child components as props.

The DraftJS documentation tells you how to store editor data in state.

It will look something like this:

class Container extends React.Component {
  constructor(props) {
    this.state = {
      editorState: EditorState.createEmpty(),
      // Make sure 'MyComponent` is a React component defined in this file or imported from somewhere
      addedComponentType: MyComponent,
      showAddedComponent: false,
    }
  }

  handleEditorChange = (editorState) => this.setState({editorState})
  // shorthand for `this.setState({editorState: editorState})`

  toggleShowAddedComponent = () => this.setState({
    showAddedComponent: !this.state.showAddedComponent
  })

  render() {
    const {addedComponentType: AddedComponent} = this.state
    // Shorthand for `const AddedComponent = this.state.addedComponentType`
    return (
      <Editor editorState={this.state.editorState} onChange={this.handleEditorChange} />
      {this.state.showAddedComponent && <AddedComponent>Hello World!</AddedComponent>}
      <button onClick={this.toggleShowAddedComponent}>
        {this.state.showAddedComponent ? 'Hide' : 'Show'} Added Component
      </button>
    )
  }
}
Phil
  • 141
  • 1
  • 7
  • Thank you for your time but that is not exactly what I'm trying to to, I would like to add the component inside the editor, there your example has nothing to do with the editor. A use case I may need would be an user wants to add a slider of photos inside his editor, I want to provide to him a react component called Slider that you can add in the draft with a button. And so you can add as many slider as you want wherever you want. – Lucien Perouze Jan 30 '18 at 13:58
  • So, for example, if the editor is for a blog post, if a user added a `Slider` to the editor, it would show up in the blog post when published? – Phil Jan 31 '18 at 21:30
  • Yes, exactly. And I will use the editor in a read only mode to display the posts the same way. – Lucien Perouze Jan 31 '18 at 23:19
  • It doesn't look like that's the kind of thing you can solve with DraftJS alone. You would need some sort of additional data model to encode the placement, type, and content of the elements in the post. The editor is for displaying rich text. You couldn't make it display a react component just by embedding the *text* of the JSX into the thing. – Phil Feb 01 '18 at 02:30
  • For one, it would be a pretty huge security hole to have (essentially) `eval`ed javascript in user-submitted text. You'd have to have extensive parsing/validation even just to make it safe. – Phil Feb 01 '18 at 02:31