Skip to content

Commit 7ef530a

Browse files
committed
fix(highlight-and-scroll): scroll position calc
1 parent 3e65644 commit 7ef530a

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/Electron/Component/WebViewComponentInjection/highlight-and-scroll.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
// close prepare
1818
closeNoHighlightResolvedThread();
1919

20-
// scroll to latest
21-
await scrollToHighlight();
22-
2320
// add highlight indicator
2421
addHighlightIndicator();
22+
23+
// scroll to latest
24+
await scrollToHighlight();
2525
}
2626

2727
function getComments() {
@@ -160,9 +160,14 @@
160160
indicatorEl.appendChild(currentPosEl);
161161

162162
// calc timeline height
163-
const lastCommentBottom = getComments().pop().getBoundingClientRect().bottom;
164-
const timelineRect = document.querySelector('.js-discussion').getBoundingClientRect();
165-
const timelineHeight = timelineRect.height - (timelineRect.bottom - lastCommentBottom);
163+
function getTimelineHeight() {
164+
const lastCommentBottom = getComments().pop().getBoundingClientRect().bottom + window.scrollY;
165+
const timelineRect = document.querySelector('.js-discussion').getBoundingClientRect();
166+
const timelineBottom = timelineRect.bottom + window.scrollY;
167+
const timelineHeight = timelineRect.height - (timelineBottom - lastCommentBottom);
168+
return {timelineHeight, timelineRect};
169+
}
170+
const {timelineHeight, timelineRect} = getTimelineHeight();
166171
const timelineOffset = timelineRect.top + window.pageYOffset; //.js-discussionのheightを使うために、commentの絶対位置をオフセットする必要がある
167172

168173
// タイムラインの高さが小さいときは、インジケータも小さくする
@@ -218,10 +223,13 @@
218223
// scroll position
219224
if (timelineRect.bottom > window.innerHeight) {
220225
window.addEventListener('wheel', async () => {
221-
const top = Math.max(0, window.scrollY - timelineRect.top);
222-
const bottom = Math.min(timelineHeight, (window.scrollY + window.innerHeight) - timelineRect.top);
226+
const windowTop = window.scrollY;
227+
const windowBottom = window.scrollY + window.innerHeight;
228+
const {timelineHeight, timelineRect} = getTimelineHeight();
229+
const timelineTop = timelineRect.top + window.scrollY;
230+
const top = Math.min(timelineHeight, Math.max(0, windowTop - timelineTop));
231+
const bottom = Math.max(0, Math.min(timelineHeight, windowBottom - timelineTop));
223232
const height = (bottom - top) / timelineHeight;
224-
// if (Math.ceil(100 * height) >= 100) return;
225233

226234
currentPosEl.style.top = `${top/timelineHeight * 100}%`;
227235
currentPosEl.style.height = `${height * 100}%`;

0 commit comments

Comments
 (0)