insert method Null safety

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

Insert text at specified character index with style style.


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.

  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.
  if (parent is BlockNode) unwrap();

  // Now we can apply new format and re-layout.

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