Deleting nodes is messing up the diagram

When i am deleting node and all its child node i have to replace the deleted node with a placeholder node. Though it is working as expected but the layout is getting messed up since i am adding the placeholder link later.


Node which was deleted was

How to make sure a link from a particular port goes into a particular direction only. How to fix this. I am using a Tree Layout Horizontal Arrangement

Add the replacement node and the link(s) connecting it before removing the original node and its children.

Or am I misunderstanding the situation?

Did that but didnt work

 deleteConditionNode = (e: any, obj: go.GraphObject) => {
    const node = this.dia.findNodeForKey(obj.part?.data.key);
    const isRootNode = node?.findTreeLevel() === 0;
    if (node !== null) {
      this.dia.startTransaction();
      const links = new go.List().addAll(node.linksConnected);
      let fromPort!: portTypes;
      let fromKey!: string;
      let fromLink;
      links.each(function (link: any) {
        if (link.toNode === node) {
          fromLink = link;
          fromPort = link.data.fromPort;
          fromKey = link.data.from;
        }
      });
      const groupId = node.containingGroup?.data['key'];
      if (isRootNode && node.data.group && !fromPort) {
        // root node within a group, so add placeholder after its deletion
        this.addIfNestedIfPlaceholderNodes(groupId);
      } else if (fromPort === 'r') {
        // add True placeholder nodes
        this.addIfNestedIfPlaceholderNodes(groupId, fromLink);
      } else if (fromPort === 'b') {
        // add False placeholder nodes
        this.addElseIfElsePlaceholderNodes(fromKey, groupId);
      }
      node.findTreeParts().each((a: go.Part) => this.dia.remove(a));