# All files / composer/utils/draft-js-custom-plugins/editor-contents-reset/modifiers resetEditorContents.js

11.11% Statements 1/9
100% Branches 0/0
0% Functions 0/1
11.11% Lines 1/9

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    7x

     /**
     * Reset an editor's contents
     */
    import { EditorState, Modifier, SelectionState } from '@bufferapp/draft-js';

    const resetEditorContents = (editorState) => {
      let contentState = editorState.getCurrentContent();
      const contentBlock = contentState.getFirstBlock();
      const contentBlockKey = contentBlock.getKey();

      const allSelected = new SelectionState({
        anchorKey: contentBlockKey,
        anchorOffset: 0,
        focusKey: contentBlockKey,
        focusOffset: contentBlock.getLength(),
        hasFocus: true,
      });

      contentState = Modifier.removeRange(contentState, allSelected, 'backward');
      let newEditorState = EditorState.push(editorState, contentState, 'remove-range');
      newEditorState = EditorState.acceptSelection(
        newEditorState,
        SelectionState.createEmpty(contentBlockKey)
      );

      return newEditorState;
    };

    export default resetEditorContents;