insert method

  1. @override
void insert(
  1. int index,
  2. String text,
  3. NotusStyle? style
)
override

Insert text at specified character index with style style.

Implementation

@override
void insert(int index, String text, NotusStyle? style) {
  final lf = text.indexOf('\n');
  if (lf == -1) {
    _insertSafe(index, text, style);
    // No need to update line or block format since those attributes can only
    // be attached to `\n` character and we already know it's not present.
    return;
  }

  final substring = text.substring(0, lf);
  _insertSafe(index, substring, style);
  if (substring.isNotEmpty) index += substring.length;

  final nextLine = splitAt(index); // Next line inherits our format.

  // Reset our format and unwrap from a block if needed.
  clearStyle();
  if (parent is BlockNode) unwrap();

  // Now we can apply new format and re-layout.
  _formatAndOptimize(style);

  // Continue with remaining part.
  final remaining = text.substring(lf + 1);
  nextLine.insert(0, remaining, style);
}