fix: use scrollTop instead of viewport center for in-focus detection

handleScrollFocus now finds the task closest to scrollTop (offset)
instead of closest to the vertical center of the viewport. This
ensures that the saved task matches the user's scroll target.
This commit is contained in:
Yuyao Huang 2026-05-09 15:37:34 +08:00
parent eca0cf4193
commit 84181e1ec2

View File

@ -83,7 +83,6 @@ async function persistSelectedTask(taskId) {
if (goal) {
goal.selected_task_id = taskId;
}
console.log("Saved selected task:", taskId);
} catch (error) {
console.error("Failed to persist selected task:", error);
}
@ -172,16 +171,13 @@ function handleScrollFocus() {
const scrollView = document.getElementById("scroll-view");
const taskItems = document.querySelectorAll(".task-item");
const scrollViewRect = scrollView.getBoundingClientRect();
const focusCenter = scrollViewRect.top + scrollViewRect.height / 2;
const scrollTop = scrollView.scrollTop;
let closestItem = null;
let closestDistance = Infinity;
taskItems.forEach(item => {
const itemRect = item.getBoundingClientRect();
const itemCenter = itemRect.top + itemRect.height / 2;
const distance = Math.abs(itemCenter - focusCenter);
const distance = Math.abs(item.offsetTop - scrollTop);
item.classList.remove("in-focus");
@ -205,9 +201,7 @@ function handleScrollFocus() {
}
function handleScrollSave() {
// Save the task that has in-focus class (determined by handleScrollFocus)
const inFocusTask = document.querySelector(".task-item.in-focus");
console.log("handleScrollSave: inFocusTask=", inFocusTask?.dataset.taskId, "isInitializing=", isInitializing);
if (inFocusTask && !isInitializing) {
const taskId = parseInt(inFocusTask.dataset.taskId);