applyRule method

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

Implementation

@override
Delta? applyRule(Delta document, int index,
    {int? len, Object? data, Attribute? attribute}) {
  final itr = DeltaIterator(document)..skip(index);
  var op = itr.next(1);
  if (op.data != '\n') {
    return null;
  }

  final isNotPlain = op.isNotPlain;
  final attrs = op.attributes;

  itr.skip(len! - 1);
  final delta = Delta()
    ..retain(index)
    ..delete(len);

  while (itr.hasNext) {
    op = itr.next();
    final text = op.data is String ? (op.data as String?)! : '';
    final lineBreak = text.indexOf('\n');
    if (lineBreak == -1) {
      delta.retain(op.length!);
      continue;
    }

    var attributes = op.attributes == null
        ? null
        : op.attributes!.map<String, dynamic>(
            (key, dynamic value) => MapEntry<String, dynamic>(key, null));

    if (isNotPlain) {
      attributes ??= <String, dynamic>{};
      attributes.addAll(attrs!);
    }
    delta
      ..retain(lineBreak)
      ..retain(1, attributes);
    break;
  }
  return delta;
}