replace method

Delta replace (
  1. int index,
  2. int length,
  3. String text
)

Replaces length of characters starting at index text.

This method applies heuristic rules before modifying this document and produces a NotusChange with source set to ChangeSource.local.

Returns an instance of Delta actually composed into this document.

Implementation

Delta replace(int index, int length, String text) {
  assert(index >= 0 && (text.isNotEmpty || length > 0),
      'With index $index, length $length and text "$text"');
  var delta = Delta();
  // We have to compose before applying delete rules
  // Otherwise delete would be operating on stale document snapshot.
  if (text.isNotEmpty) {
    delta = insert(index, text);
    index = delta.transformPosition(index);
  }

  if (length > 0) {
    final deleteDelta = delete(index, length);
    delta = delta.compose(deleteDelta);
  }
  return delta;
}