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