insertImageNode method

Future<void> insertImageNode(
  1. String src
)

Implementation

Future<void> insertImageNode(
  String src,
) async {
  final selection = this.selection;
  if (selection == null || !selection.isCollapsed) {
    return;
  }
  final node = getNodeAtPath(selection.end.path);
  if (node == null) {
    return;
  }
  final transaction = this.transaction;
  // if the current node is empty paragraph, replace it with image node
  if (node.type == ParagraphBlockKeys.type &&
      (node.delta?.isEmpty ?? false)) {
    transaction
      ..insertNode(
        node.path,
        imageNode(
          url: src,
        ),
      )
      ..deleteNode(node);
  } else {
    transaction.insertNode(
      node.path.next,
      imageNode(
        url: src,
      ),
    );
  }

  transaction.afterSelection = Selection.collapsed(
    Position(
      path: node.path.next,
      offset: 0,
    ),
  );

  return apply(transaction);
}