invert method

Delta invert(
  1. Delta base
)

Returned an inverted delta that has the opposite effect of against a base document delta.

Implementation

Delta invert(Delta base) {
  final inverted = Delta();
  _operations.fold(0, (int previousValue, op) {
    if (op is TextInsert) {
      inverted.delete(op.length);
    } else if (op is TextRetain && op.attributes == null) {
      inverted.retain(op.length);
      return previousValue + op.length;
    } else if (op is TextDelete || op is TextRetain) {
      final length = op.length;
      final slice = base.slice(previousValue, previousValue + length);
      for (final baseOp in slice._operations) {
        if (op is TextDelete) {
          inverted.add(baseOp);
        } else if (op is TextRetain && op.attributes != null) {
          inverted.retain(
            baseOp.length,
            attributes: invertAttributes(baseOp.attributes, op.attributes),
          );
        }
      }
      return previousValue + length;
    }
    return previousValue;
  });
  return inverted..chop();
}