Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 5eb91e6

Browse files
fix: LSDV-4708: Scroll page to canvas only if needed (#1268)
* fix: LSDV-4708: Scroll page to canvas only if needed If canvas is already in the viewport, don't scroll the page! * Scroll only if image region is not visible scrollIntoViewIfNeeded scolls even if it's not really needed :( * Add comments; fix zoomed image; add smooth - hard to calculate region position on zoomed image and it can be hidden, so don't scroll if image is zoomed in - add behavior: smooth for better UX * Improve scroll to region * Huge improvement for zoomed and huge images Zoomed in images makes it hard to calculate region bboxes, so we are trying to make the whole image visible enough. * Rename method for better readability --------- Co-authored-by: yyassi-heartex <[email protected]>
1 parent 958cb30 commit 5eb91e6

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/mixins/KonvaRegion.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,57 @@ export const KonvaRegionMixin = types.model({})
4545
self.updateImageSize?.(width / naturalWidth, height / naturalHeight, width, height);
4646
}
4747
},
48+
49+
selectRegion() {
50+
self.scrollToRegion();
51+
},
52+
53+
/**
54+
* Scrolls to region if possible or scrolls to whole image if needed
55+
*/
56+
scrollToRegion() {
57+
const zoomedIn = self.object.zoomScale > 1;
58+
const canvas = self.shapeRef?.parent?.canvas?._canvas;
59+
let viewport = canvas;
60+
61+
// `.lsf-main-content` is the main scrollable container for LSF
62+
while (viewport && !viewport.scrollTop && !viewport.className.includes('main-content')) {
63+
viewport = viewport.parentElement;
64+
}
65+
if (!viewport) return;
66+
67+
// minimum percent of region area to consider it visible
68+
const VISIBLE_AREA = 0.6;
69+
// infobar is positioned absolutely, covering part of UI
70+
const INFOBAR_HEIGHT = 36;
71+
72+
const vBBox = viewport.getBoundingClientRect();
73+
const cBBox = canvas.getBoundingClientRect();
74+
// bbox inside canvas; for zoomed images calculations are tough,
75+
// so we use the whole image so it should be visible enough at the end
76+
const rBBox = zoomedIn ? { top: 0, bottom: cBBox.height } : self.bboxCoordsCanvas;
77+
const height = rBBox.bottom - rBBox.top;
78+
// comparing the closest point of region from top or bottom image edge
79+
// and how deep is this edge hidden behind respective edge of viewport
80+
const overTop = rBBox.top - (vBBox.top - cBBox.top);
81+
const overBottom = (canvas.clientHeight - rBBox.bottom) - (cBBox.bottom - vBBox.bottom) - INFOBAR_HEIGHT;
82+
// huge images should be scrolled to the closest edge, not to hidden one
83+
const isHuge = zoomedIn && canvas.clientHeight > viewport.clientHeight;
84+
85+
// huge region or image cut off by viewport edges — do nothing
86+
if (overTop < 0 && overBottom < 0) return;
87+
88+
if (overTop < 0 && -overTop / height > (1 - VISIBLE_AREA)) {
89+
// if image is still visible enough — don't scroll
90+
if (zoomedIn && (cBBox.bottom - vBBox.top) / viewport.clientHeight > (1 - VISIBLE_AREA)) return;
91+
viewport.scrollBy({ top: isHuge ? -overBottom : overTop, left: 0, behavior: 'smooth' });
92+
} else if (overBottom < 0 && -overBottom / height > (1 - VISIBLE_AREA)) {
93+
// if image is still visible enough — don't scroll
94+
if (zoomedIn && (vBBox.bottom - cBBox.top) / viewport.clientHeight > (1 - VISIBLE_AREA)) return;
95+
viewport.scrollBy({ top: isHuge ? overTop : -overBottom, left: 0, behavior: 'smooth' });
96+
}
97+
},
98+
4899
onClickRegion(e) {
49100
const annotation = self.annotation;
50101
const ev = e?.evt || e;

src/stores/RegionStore.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ const SelectionMap = types.model(
124124
highlight(region) {
125125
self.clear();
126126
self.select(region);
127-
128-
if (region?.type === 'polygonregion') return;
129-
region?.shapeRef?.parent?.canvas?._canvas?.scrollIntoView?.();
130127
},
131128
};
132129
});

0 commit comments

Comments
 (0)