retain method

  1. @override
void retain(
  1. int index,
  2. int? length,
  3. NotusStyle? style
)
override

Format length characters of this node starting from index with specified style style.

Implementation

@override
void retain(int index, int? length, NotusStyle? style) {
  if (style == null) return;
  final thisLength = this.length;

  final local = math.min(thisLength - index, length!);
  // If index is at line-break character this is line/block format update.
  final isLineFormat = (index + local == thisLength) && local == 1;

  if (isLineFormat) {
    assert(
        style.values.every((attr) => attr.scope == NotusAttributeScope.line),
        'It is not allowed to apply inline attributes to line itself.');
    _formatAndOptimize(style);
  } else {
    // otherwise forward to children as it's inline format update.
    assert(index + local != thisLength,
        'It is not allowed to apply inline attributes to line itself.');
    assert(style.values
        .every((attr) => attr.scope == NotusAttributeScope.inline));
    super.retain(index, local, style);
  }

  final remaining = length - local;
  if (remaining > 0) {
    assert(nextLine != null);
    nextLine!.retain(0, remaining, style);
  }
}