executeHandleEnter function

bool executeHandleEnter(
  1. FluentDocument document
)

Handles the ENTER key.

  • Paragraphs: creates a new paragraph below or splits content if in the middle
  • Cells: creates a new fragment with line break or new row if at the end
  • Lists: creates a new list item

Implementation

bool executeHandleEnter(FluentDocument document) {
  final root = document.content;
  final cursor = document.cursor;

  // If there's an active selection, remove it before handling ENTER.
  // We reuse executeHandleReplaceSelection passing an empty string:
  // clears the selection and places the cursor at the base point, ready for split.
  if (!cursor.isCollapsed) {
    executeHandleReplaceSelection('', document);
  }

  // Find the current cursor container (after possible collapse)
  final container = findLogicalContainer(root, cursor.anchorId);
  if (container == null) return false;

  // Link handling (which extends Paragraph and implements Fragment)
  // Must be checked BEFORE Paragraph because Link is a Paragraph
  if (container is Link) {
    return _handleLinkEnter(document, container);
  }

  // If the container is a Paragraph inside a ListItem -> list handling
  if (container is Paragraph) {
    final ancestorItem = _findEnclosingListItem(root, container);
    if (ancestorItem != null) {
      return _handleListEnter(document, ancestorItem, container);
    }
    final ancestorCell = _findEnclosingCell(root, container);
    if (ancestorCell != null) {
      return _handleCellEnter(document, ancestorCell);
    }
  }

  // Standalone paragraph handling (and other inline containers)
  return _handleParagraphEnter(document, container);
}