applyRule method

  1. @override
Delta? applyRule(
  1. Delta document,
  2. int index, {
  3. int? len,
  4. Object? data,
  5. Attribute? attribute,
})
override

Applies heuristic rule to an operation on a document and returns resulting Delta.

Implementation

@override
Delta? applyRule(Delta document, int index, {int? len, Object? data, Attribute? attribute}) {
  if (data is! String || data != ' ') {
    return null;
  }

  final itr = DeltaIterator(document);
  final prev = itr.skip(index);
  if (prev == null || prev.data is! String) {
    return null;
  }

  try {
    final cand = (prev.data as String).split('\n').last.split(' ').last;
    final link = Uri.parse(cand);
    if (!['https', 'http'].contains(link.scheme)) {
      return null;
    }
    final attributes = prev.attributes ?? <String, dynamic>{};

    if (attributes.containsKey(Attribute.link.key)) {
      return null;
    }

    attributes.addAll(LinkAttribute(link.toString()).toJson());
    return Delta()
      ..retain(index + (len ?? 0) - cand.length)
      ..retain(cand.length, attributes)
      ..insert(data, prev.attributes);
  } on FormatException {
    return null;
  }
}