scrollToPart for a multiple node selection

"gojs": "^2.2.15",
"gojs-react": "^1.1.1",

scrollToPart works pretty well when there is only one node selected, but I need it to also handle multiple nodes selected. Looking into docs and various other postings it doesn’t seem like this feature is intended for this use, and instead I should perhaps be using scrollToRect?

But while testing it out, everything is either a little off or way off leaving me unsure what could be missing since it doesn’t seem consistent. Though, it is possible that I’m mis-understanding/mis-using or straight up missing one of the many coordinate systems involved. But it also feels like a lot of custom logic for something that may also be built-in somewhere that I just haven’t found…

So my question is; Is there a built-in method for doing this? And if not, am I headed down the right path, but maybe just mis using some coordinates or missing some variable I need to account for?

let top;
let left;
let bottom;
let right;

diagram.selection.toArray().forEach(node => {
  if (!top || node.location.y - node.height / 2 < top) {
    top = node.location.y - node.height / 2;
  }
  if (!left || node.location.x - node.width / 2 < left) {
    left = node.location.x - node.width / 2;
  }
  if (!bottom || node.location.y + node.height / 2 > bottom) {
    bottom = node.location.y + node.height / 2;
  }
  if (!right || node.location.x + node.width / 2 > right) {
    right = node.location.x + node.width / 2;
  }
});

const selectionRect = new Rect(left, top, right, bottom);

diagram.scrollToRect(selectionRect);

Yes, calling Diagram.scrollToRect is probably what you want to do. An alternative is to call Diagram.centerRect.

Computing that Rect can be done more simply via:

  diagram.computePartsBounds(diagram.selection)

Thank you so much, that got me unstuck!

Worth mentioning here that part of the problem that was further confusing me was that because I am not only trying to scroll to the selection but set the scale as well. Setting the scale before the scrollToRect is important. I was previously setting the scale after which was causing me more issues. Only when simplifying the problem to make this post and then re-adding the scale modifications after applying the fix for scroll did I notice this.