/**
 * Removes a sticker from the editor state
 */

import {
  EditorState,
  Modifier,
  SelectionState
} from 'draft-js';

export default (editorState: Object, blockKey: String) => {
  const content = editorState.getCurrentContent();
  const afterKey = content.getKeyAfter(blockKey);

  let withoutSticker;
  if (afterKey !== undefined) {
    const targetRange = new SelectionState({
      anchorKey: blockKey,
      anchorOffset: 0,
      focusKey: afterKey,
      focusOffset: 0,
    });
    withoutSticker = Modifier.removeRange(content, targetRange, 'backward');
    withoutSticker = Modifier.setBlockType(
      withoutSticker,
      withoutSticker.getSelectionAfter(),
      'unstyled'
    );
  } else {
    withoutSticker = Modifier.setBlockType(
      content,
      content.getSelectionAfter(),
      'unstyled'
    );
  }

  const newState = EditorState.push(editorState, withoutSticker, 'remove-sticker');
  return EditorState.forceSelection(newState, withoutSticker.getSelectionAfter());
};

@@ -180,20 +180,18 @@
  =%3E %7B%0A  
-cons
+le
 t conten
@@ -239,494 +239,1008 @@
 nst 
-afterKey = content.getKeyAfter(blockKey);%0A%0A  let withoutSticker;%0A  if (afterKey !== undefined) %7B%0A    const targetRange = new SelectionState(%7B%0A      anchorKey: blockKey,%0A      anchorOffset: 0,%0A      focusKey: afterKey,%0A      focusOffset: 0,%0A    %7D);%0A    withoutSticker = Modifier.removeRange(content, targetRange, 'backward');%0A    withoutSticker = Modifier.setBlockType(%0A      withoutSticker,%0A      withoutSticker.getSelectionAfter(),%0A      'unstyled'
+newSelection = new SelectionState(%7B%0A    anchorKey: blockKey,%0A    anchorOffset: 0,%0A    focusKey: blockKey,%0A    focusOffset: 0,%0A  %7D);%0A%0A  const afterKey = content.getKeyAfter(blockKey);%0A  const afterBlock = content.getBlockForKey(afterKey);%0A  let targetRange;%0A%0A  // Only if the following block the last with with no text then the whole block%0A  // should be removed. Otherwise the block should be reduced to an unstyled block%0A  // without any characters.%0A  if (afterBlock &&%0A      afterBlock.getType() === 'unstyled' &&%0A      afterBlock.getLength() === 0 &&%0A      afterBlock === content.getBlockMap().last()) %7B%0A    targetRange = new SelectionState(%7B%0A      anchorKey: blockKey,%0A      anchorOffset: 0,%0A      focusKey: afterKey,%0A      focusOffset: 0,%0A    %7D);%0A  %7D else %7B%0A    targetRange = new SelectionState(%7B%0A      anchorKey: blockKey,%0A      anchorOffset: 0,%0A      focusKey: blockKey,%0A      focusOffset: 1,
 %0A    
+%7D
 );%0A  %7D
- else %7B%0A    withoutSticker
+%0A%0A  // change the blocktype and remove the characterList entry with the sticker%0A  content
  = M
@@ -1261,26 +1261,24 @@
 ckType(%0A    
-  
 content,%0A   
@@ -1282,45 +1282,25 @@
     
-  content.getSelectionAfter()
+targetRange
 ,%0A    
-  
 'uns
@@ -1312,17 +1312,107 @@
 '%0A  
-  
+);%0A  content = Modifier.removeRange(content, targetRange, 'backward'
 );%0A
+%0A
   
-%7D%0A
+// force to new selection
 %0A  c
@@ -1457,30 +1457,23 @@
 rState, 
-withoutSticker
+content
 , 'remov
@@ -1535,26 +1535,11 @@
 te, 
-withoutSticker.get
+new
 Sele
@@ -1547,15 +1547,8 @@
 tion
-After()
 );%0A%7D

fix sticker remove behavior - close #7