insert method

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

Implementation

@override
void insert(int index, Object data, Style? style) {
  if (data is Embeddable) {
    // 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;
  }

  final text = data as String;
  final lineBreak = text.indexOf('\n');
  if (lineBreak < 0) {
    _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 prefix = text.substring(0, lineBreak);
  _insertSafe(index, prefix, style);
  if (prefix.isNotEmpty) {
    index += prefix.length;
  }

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

  // Reset our format and unwrap from a block if needed.
  clearStyle();
  if (parent is Block) {
    _unwrap();
  }

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

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