canMergeWith method

  1. @override
bool canMergeWith(
  1. EditOperation other
)
override

Check if this operation can be merged with another (for grouping rapid edits)

Implementation

@override
bool canMergeWith(EditOperation other) {
  if (other is! InsertOperation) return false;

  final timeDiff = other.timestamp.difference(timestamp).inMilliseconds.abs();
  if (timeDiff > 500) return false;

  if (other.offset == offset + text.length) {
    if (text.contains('\n') || other.text.contains('\n')) return false;
    final thisEndsWithSpace = text.endsWith(' ') || text.endsWith('\t');
    final otherStartsWithSpace =
        other.text.startsWith(' ') || other.text.startsWith('\t');
    if (thisEndsWithSpace != otherStartsWithSpace &&
        text.isNotEmpty &&
        other.text.isNotEmpty) {
      return false;
    }
    return true;
  }
  return false;
}