addLinkFromExisting method

void addLinkFromExisting(
  1. Link link, {
  2. String? eventId,
  3. bool isHandled = false,
})

This method is used to add a link from an existing link object.

This method is used when loading a project from a file or in copy/paste operations and preserves all properties of the link object.

Emits an AddLinkEvent event.

Implementation

void addLinkFromExisting(
  Link link, {
  String? eventId,
  bool isHandled = false,
}) {
  if (!nodes.containsKey(link.fromTo.from) ||
      !nodes.containsKey(link.fromTo.fromPort)) {
    return;
  }

  final fromNode = nodes[link.fromTo.from]!;
  final toNode = nodes[link.fromTo.fromPort]!;

  if (!fromNode.ports.containsKey(link.fromTo.to) ||
      !toNode.ports.containsKey(link.fromTo.toPort)) {
    return;
  }

  final fromPort = nodes[link.fromTo.from]!.ports[link.fromTo.to]!;
  final toPort = nodes[link.fromTo.fromPort]!.ports[link.fromTo.toPort]!;

  fromPort.links.add(link);
  toPort.links.add(link);

  linksById.putIfAbsent(
    link.id,
    () => link,
  );

  if (link.state.isSelected) selectedLinkIds.add(link.id);

  linksDataDirty = true;

  eventBus.emit(
    AddLinkEvent(
      id: eventId ?? const Uuid().v4(),
      link,
      isHandled: isHandled,
    ),
  );
}