// @flow
import { ContentBlock, ContentState, EditorState, genKey } from "draft-js";
import { List } from "immutable";

/**
 * Condense an array of content blocks into a single block
 */
export function condenseBlocks(editorState: EditorState) {
  const blocks = editorState.getCurrentContent().getBlocksAsArray();

  if (blocks.length < 2) {
    return editorState;
  }

  let text = List();
  let characterList = List();

  // Gather all the text/characterList and concat them
  blocks.forEach((block) => {
    // Atomic blocks should be ignored (stripped)
    if (block.getType() !== "atomic") {
      text = text.push(block.getText());
      characterList = characterList.concat(block.getCharacterList());
    }
  });

  const contentBlock = new ContentBlock({
    key: genKey(),
    text: text.join(""),
    type: "unstyled",
    characterList,
    depth: 0,
  });

  // Update the editor state with the compressed version.
  const newContentState = ContentState.createFromBlockArray([contentBlock]);
  // Create the new state as an undoable action.
  const nextState = EditorState.push(
    editorState,
    newContentState,
    "remove-range",
  );
  // Move the selection to the end.
  return EditorState.moveFocusToEnd(nextState);
}

/**
 * Single Line Plugin
 */
const singleLinePlugin = () => {
  return {
    onChange(editorState: EditorState) {
      return condenseBlocks(editorState);
    },

    /**
     * Stop new lines being inserted by always handling the return
     */
    handleReturn() {
      return "handled";
    },
  };
};

export default singleLinePlugin;

@@ -81,42 +81,8 @@
 js%22;
-%0Aimport %7B List %7D from %22immutable%22;
 %0A%0A/*
@@ -344,22 +344,18 @@
  text = 
-List()
+%22%22
 ;%0A  let 
@@ -371,17 +371,8 @@
 List
- = List()
 ;%0A%0A 
@@ -560,20 +560,11 @@
 ext 
++
 = 
-text.push(
 bloc
@@ -574,17 +574,16 @@
 etText()
-)
 ;%0A      
@@ -597,16 +597,40 @@
 erList =
+ characterList%0A        ?
  charact
@@ -668,16 +668,59 @@
 rList())
+%0A        : block.getCharacterList().slice()
 ;%0A    %7D%0A
@@ -796,48 +796,47 @@
    t
-ext: text.join(%22%22),%0A    type: %22unstyled%22
+ype: %22unstyled%22,%0A    depth: 0,%0A    text
 ,%0A  
@@ -852,30 +852,16 @@
 erList,%0A
-    depth: 0,%0A
   %7D);%0A%0A 

Use CharacterList API instead of underlying immutable List