replaceText method

void replaceText(
  1. Node node,
  2. int index,
  3. int length,
  4. String text, {
  5. Attributes? attributes,
})

replace the text at the given index with the text.

Implementation

void replaceText(
  Node node,
  int index,
  int length,
  String text, {
  Attributes? attributes,
}) {
  final delta = node.delta;
  if (delta == null) {
    return;
  }
  var newAttributes = attributes;
  if (index != 0 && attributes == null) {
    newAttributes = attributes ?? delta.sliceAttributes(index);

    if (newAttributes == null) {
      final slicedDelta = delta.slice(index, index + length);
      if (slicedDelta.isNotEmpty) {
        newAttributes = slicedDelta.first.attributes;
      }
    }
  }

  final replace = Delta()
    ..retain(index)
    ..delete(length)
    ..insert(text, attributes: {...newAttributes ?? {}});
  addDeltaToComposeMap(node, replace);

  afterSelection = Selection.collapsed(
    Position(
      path: node.path,
      offset: index + text.length,
    ),
  );
}