delete method

  1. @override
void delete(
  1. int index,
  2. int? length
)
override

Delete length characters of this node starting from index.

Implementation

@override
void delete(int index, int? length) {
  final local = math.min(this.length - index, length!);
  final isLFDeleted = (index + local == this.length);
  if (isLFDeleted) {
    // Our newline character deleted with all style information.
    clearStyle();
    if (local > 1) {
      // Exclude newline character from delete range for children.
      super.delete(index, local - 1);
    }
  } else {
    super.delete(index, local);
  }

  final remaining = length - local;
  if (remaining > 0) {
    assert(nextLine != null);
    nextLine!.delete(0, remaining);
  }
  if (isLFDeleted && isNotEmpty) {
    // Since we lost our line-break and still have child text nodes those must
    // migrate to the next line.

    // nextLine might have been unmounted since last assert so we need to
    // check again we still have a line after us.
    assert(nextLine != null);

    // Move remaining children in this line to the next line so that all
    // attributes of nextLine are preserved.
    nextLine!.moveChildren(this); // TODO: avoid double move
    moveChildren(nextLine);
  }

  if (isLFDeleted) {
    // Now we can remove this line.
    final block = parent!; // remember reference before un-linking.
    unlink();
    block.optimize();
  }
}