function removeTextWithStrategy()

in src/component/handlers/edit/commands/removeTextWithStrategy.js [29:79]


function removeTextWithStrategy(
  editorState: EditorState,
  strategy: (editorState: EditorState) => SelectionState,
  direction: DraftRemovalDirection,
): ContentState {
  const selection = editorState.getSelection();
  const content = editorState.getCurrentContent();
  let target = selection;
  const anchorKey = selection.getAnchorKey();
  const focusKey = selection.getFocusKey();
  const anchorBlock = content.getBlockForKey(anchorKey);
  if (experimentalTreeDataSupport) {
    if (direction === 'forward') {
      if (anchorKey !== focusKey) {
        // For now we ignore forward delete across blocks,
        // if there is demand for this we will implement it.
        return content;
      }
    }
  }
  if (selection.isCollapsed()) {
    if (direction === 'forward') {
      if (editorState.isSelectionAtEndOfContent()) {
        return content;
      }
      if (experimentalTreeDataSupport) {
        const isAtEndOfBlock =
          selection.getAnchorOffset() ===
          content.getBlockForKey(anchorKey).getLength();
        if (isAtEndOfBlock) {
          const anchorBlockSibling = content.getBlockForKey(
            anchorBlock.nextSibling,
          );
          if (!anchorBlockSibling || anchorBlockSibling.getLength() === 0) {
            // For now we ignore forward delete at the end of a block,
            // if there is demand for this we will implement it.
            return content;
          }
        }
      }
    } else if (editorState.isSelectionAtStartOfContent()) {
      return content;
    }

    target = strategy(editorState);
    if (target === selection) {
      return content;
    }
  }
  return DraftModifier.removeRange(content, target, direction);
}