replace method

Delta replace(
  1. int index,
  2. int len,
  3. Object? data
)

Replaces length of characters starting at index with data.

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

Returns an instance of Delta actually composed into this document.

Implementation

Delta replace(int index, int len, Object? data) {
  assert(index >= 0);
  assert(data is String || data is Embeddable);

  final dataIsNotEmpty = (data is String) ? data.isNotEmpty : true;

  assert(dataIsNotEmpty || len > 0);

  var delta = Delta();

  // We have to insert before applying delete rules
  // Otherwise delete would be operating on stale document snapshot.
  if (dataIsNotEmpty) {
    delta = insert(index, data, replaceLength: len);
  }

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

  return delta;
}