function isCloseToTheEdgeOfCanvas()

in packages/ketcher-react/src/script/ui/state/moveSelectedItems.ts [61:128]


function isCloseToTheEdgeOfCanvas(
  selectedItems: EditorSelection,
  editor: Editor,
  key: ArrowKey,
) {
  if (!selectedItems.atoms) {
    return false;
  }
  const restruct = editor.render.ctab;
  let theMostTopAtom = restruct.atoms.get(selectedItems?.atoms[0]);
  let theMostBottomAtom = restruct.atoms.get(selectedItems?.atoms[0]);
  let theMostRightAtom = restruct.atoms.get(selectedItems?.atoms[0]);
  let theMostLeftAtom = restruct.atoms.get(selectedItems?.atoms[0]);

  selectedItems.atoms.forEach((atomId) => {
    const atom = restruct.atoms.get(atomId);
    const position = atom?.a.pp;
    if (position && theMostTopAtom) {
      theMostTopAtom =
        position.y < theMostTopAtom.a.pp.y ? atom : theMostTopAtom;
    }
    if (position && theMostBottomAtom) {
      theMostBottomAtom =
        position.y > theMostBottomAtom.a.pp.y ? atom : theMostBottomAtom;
    }
    if (position && theMostRightAtom) {
      theMostRightAtom =
        position.x > theMostRightAtom.a.pp.x ? atom : theMostRightAtom;
    }
    if (position && theMostLeftAtom) {
      theMostLeftAtom =
        position.x < theMostLeftAtom.a.pp.x ? atom : theMostLeftAtom;
    }
  });
  if (
    theMostTopAtom &&
    theMostBottomAtom &&
    theMostRightAtom &&
    theMostLeftAtom
  ) {
    const getScreenCoordinates = (atom: ReAtom) =>
      Scale.modelToCanvas(atom.a.pp, editor.render.options);
    const theMostTopAtomYCoordinate = getScreenCoordinates(theMostTopAtom).y;
    const theMostBottomAtomYCoordinate =
      getScreenCoordinates(theMostBottomAtom).y;
    const theMostRightAtomXCoordinate =
      getScreenCoordinates(theMostRightAtom).x;
    const theMostLeftAtomXCoordinate = getScreenCoordinates(theMostLeftAtom).x;

    const viewBox = editor.render.viewBox;
    const isCloseToTop =
      theMostTopAtomYCoordinate <= viewBox.minY + EDGE_OFFSET;
    const isCloseToBottom =
      theMostBottomAtomYCoordinate >=
      viewBox.minY + viewBox.height - EDGE_OFFSET;
    const isCloseToLeft =
      theMostLeftAtomXCoordinate <= viewBox.minX + EDGE_OFFSET;
    const isCloseToRight =
      theMostRightAtomXCoordinate >= viewBox.minX + viewBox.width - EDGE_OFFSET;
    return (
      (isCloseToTop && key === 'ArrowUp') ||
      (isCloseToBottom && key === 'ArrowDown') ||
      (isCloseToLeft && key === 'ArrowLeft') ||
      (isCloseToRight && key === 'ArrowRight')
    );
  }
  return false;
}