Search code examples
draftjs

How to create a new DraftInlineStyle?


I have tried to create a new state with Modifier.insertText and the third argument is supposed to a draftInlineStyle

let ncs = Modifier.insertText(contentStates, selections, superscriptVar, ["BOLD"]);

This does give bold but when i try to change the style later on it doesn't change. I have figured out this is because draftInlineStyle is supposed to be a constructor. So how do I create a draftInlineStyle constructor if i am supposed to pass a draftInlineStyle constructor? or is there any other way to do this?


Solution

  • You should use OrderedSet.of from Immutable.js.

    let ncs = Modifier.insertText(
      contentStates,
      selections,
      superscriptVar,
      OrderedSet.of('BOLD')
    );
    

    If you want to apply many styles, pass them as arguments: OrderedSet.of('BOLD', 'ITALIC')

    Check the simplified demo in the hidden snippet below:

    const {Editor, RichUtils, Modifier, SelectionState, EditorState} = Draft;
    const { OrderedSet } = Immutable;
    
    class Container extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          editorState: EditorState.createEmpty()
        };
      }
      
      insertTextHandler = (nameOfCustomStyle) => {
        const currentSelection = this.state.editorState.getSelection();
        const currentContent = this.state.editorState.getCurrentContent();
        
        if (!currentSelection.isCollapsed()) return;
    
      	const newContentState = Modifier.insertText(currentContent, currentSelection, 'INSERTED TEXT', OrderedSet.of('BOLD'));
      
        const newEditorState = EditorState.push(
          this.state.editorState,
          newContentState,
          'change-inline-style'
        );
      
       this._handleChange(newEditorState)
      }
      
      toggleBoldStyle = () => {
        this._handleChange(
          RichUtils.toggleInlineStyle(
            this.state.editorState,
            'BOLD'
          )
        );
      }
      
      _handleChange = (editorState) => {
        this.setState({ editorState });
      }
      
      render() {
        return (
          <div>
            <div className="container-root">
              <Editor
                placeholder="Type away :)"
                editorState={this.state.editorState}
                onChange={this._handleChange}
              />
            </div>
            <button onClick={() => this.insertTextHandler()}>
              INSERT TEXT
            </button>
            <button onClick={() => this.toggleBoldStyle()}>
              TOGGLE BOLD STYLE FOR SELECTED TEXT
            </button>
          </div>
        );
      }
    }
    
    ReactDOM.render(<Container />, document.getElementById('react-root'))
    body {
      font-family: Helvetica, sans-serif;
    }
    
    .public-DraftEditor-content {
      border: 1px solid black;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.7.0/Draft.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.0/Draft.js"></script>
    <div id="react-root"></div>