function removeEntitiesAtEdges()

in src/model/transaction/removeEntitiesAtEdges.js [24:62]


function removeEntitiesAtEdges(
  contentState: ContentState,
  selectionState: SelectionState,
): ContentState {
  const blockMap = contentState.getBlockMap();

  const updatedBlocks: {[string]: BlockNodeRecord} = {};

  const startKey = selectionState.getStartKey();
  const startOffset = selectionState.getStartOffset();
  const startBlock = blockMap.get(startKey);
  const updatedStart = removeForBlock(contentState, startBlock, startOffset);

  if (updatedStart !== startBlock) {
    updatedBlocks[startKey] = updatedStart;
  }

  const endKey = selectionState.getEndKey();
  const endOffset = selectionState.getEndOffset();
  let endBlock = blockMap.get(endKey);
  if (startKey === endKey) {
    endBlock = updatedStart;
  }

  const updatedEnd = removeForBlock(contentState, endBlock, endOffset);

  if (updatedEnd !== endBlock) {
    updatedBlocks[endKey] = updatedEnd;
  }

  if (!Object.keys(updatedBlocks).length) {
    return contentState.setSelectionAfter(selectionState);
  }

  return contentState.merge({
    blockMap: blockMap.merge(updatedBlocks),
    selectionAfter: selectionState,
  });
}