applyRule method
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.contains('\n')) {
return null;
}
final itr = DeltaIterator(document);
var prev = itr.skip(len == 0 ? index : index + 1);
if (prev == null || prev.data is! String) return null;
if ((prev.data as String).endsWith('\n')) {
if (prev.attributes != null) {
for (final key in prev.attributes!.keys) {
if (!Attribute.inlineKeys.contains(key)) {
return null;
}
}
}
prev = itr
.next(); // at the start of a line, apply the style for the current line and not the style for the preceding line
}
final attributes = prev.attributes;
final text = data;
if (attributes == null || !attributes.containsKey(Attribute.link.key)) {
return Delta()
..retain(index + (len ?? 0))
..insert(text, attributes);
}
attributes.remove(Attribute.link.key);
final delta = Delta()
..retain(index + (len ?? 0))
..insert(text, attributes.isEmpty ? null : attributes);
final next = itr.next();
final nextAttributes = next.attributes ?? const <String, dynamic>{};
if (!nextAttributes.containsKey(Attribute.link.key)) {
return delta;
}
if (attributes[Attribute.link.key] == nextAttributes[Attribute.link.key]) {
return Delta()
..retain(index + (len ?? 0))
..insert(text, attributes);
}
return delta;
}