executeHandleDelete function

bool executeHandleDelete(
  1. FluentDocument document, {
  2. bool ctrl = false,
})

Handles the Delete key.

Supported behaviors:

  1. If there's an active selection: delete the selection
  2. If cursor is at the end of a container: merge with the next node
  3. If cursor is on an image: remove the image
  4. If ctrl is pressed: delete the next word
  5. Otherwise: delete the next character in the fragment

Implementation

bool executeHandleDelete(FluentDocument document, {bool ctrl = false}) {
  if (document.registry.dispatchDelete(document, ctrl: ctrl)) return true;

  final cursor = document.cursor;

  if (deleteSelectionIfExists(document)) return true;

  if (ctrl) {
    return deleteWordHelper(document, forward: true);
  }

  final currentNode = document.nodeById(cursor.anchorId);
  if (currentNode is HorizontalRule) {
    return removeNodeAndReposition(document, currentNode, forward: true);
  }

  final currentFrag = resolveFragmentFromCursor(currentNode, cursor.anchorOffset);
  if (currentFrag == null) return false;

  final container = document.findLogicalContainerCached(cursor.anchorId);
  if (container == null) return false;

  if (currentFrag is FluentImage) {
    return removeNodeAndReposition(document, currentFrag, forward: true);
  }

  if (cursor.anchorOffset >= currentFrag.text.length) {
    return _handleDeleteAtEnd(document, container, currentFrag);
  }

  FragmentOperations.deleteTextInFragment(currentFrag, cursor.anchorOffset, count: 1);

  cursor.moveTo(currentFrag.id, cursor.anchorOffset);

  notifyTextMutation(document, container, currentFrag.id, cursor.anchorOffset, -1);

  document.updateContent();
  return true;
}