insert method

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

Insert data at specified character index with style style.

Implementation

@override
void insert(int index, Object data, NotusStyle? style) {
  if (data is EmbeddableObject) {
    // We do not check whether this line already has any children here as
    // inserting an embed into a line with other text is acceptable from the
    // Delta format perspective.
    // We rely on heuristic rules to ensure that embeds occupy an entire line.
    _insertSafe(index, data, style);
    return;
  }

  assert(data is String);

  final text = data as String;
  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);
}