formatDelta method

Future<void> formatDelta(
  1. Selection? selection,
  2. Attributes attributes, {
  3. bool withUpdateSelection = true,
  4. Map? selectionExtraInfo,
})

format the delta at the given selection.

If the Selection is not passed in, use the current selection.

Implementation

Future<void> formatDelta(
  Selection? selection,
  Attributes attributes, {
  bool withUpdateSelection = true,
  Map? selectionExtraInfo,
}) async {
  selection ??= this.selection;
  selection = selection?.normalized;

  if (selection == null || selection.isCollapsed) {
    return;
  }

  final nodes = getNodesInSelection(selection);
  if (nodes.isEmpty) {
    return;
  }

  final transaction = this.transaction;

  for (final node in nodes) {
    final delta = node.delta;
    if (delta == null) {
      continue;
    }
    final startIndex = node == nodes.first ? selection.startIndex : 0;
    final endIndex = node == nodes.last ? selection.endIndex : delta.length;
    transaction
      ..formatText(
        node,
        startIndex,
        endIndex - startIndex,
        attributes,
      )
      ..afterSelection = transaction.beforeSelection
      ..selectionExtraInfo = selectionExtraInfo;
  }

  return apply(
    transaction,
    withUpdateSelection: withUpdateSelection,
  );
}