replace method
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 || data is Delta);
var delta = Delta();
if (data is Delta) {
// move to insertion point and add the inserted content
if (index > 0) {
delta.retain(index);
}
// remove any text we are replacing
if (len > 0) {
delta.delete(len);
}
// add the pasted content
for (final op in data.operations) {
delta.push(op);
}
compose(delta, ChangeSource.local);
} else {
final dataIsNotEmpty = (data is String) ? data.isNotEmpty : true;
assert(dataIsNotEmpty || len > 0);
// 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;
}