fix: restore center alignment in scrollToTask to match handleScrollFocus

Both scrollToTask and handleScrollFocus now use center-of-viewport
calculation, ensuring consistent behavior between scrolling and
in-focus detection.
This commit is contained in:
Yuyao Huang 2026-05-09 15:44:47 +08:00
parent 84181e1ec2
commit 1a23558cad

View File

@ -83,6 +83,7 @@ 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);
}
@ -149,7 +150,9 @@ function scrollToTask(taskId) {
if (taskElement) {
const scrollView = document.getElementById("scroll-view");
const taskTop = taskElement.offsetTop;
scrollView.scrollTop = taskTop;
const scrollViewHeight = scrollView.clientHeight;
const taskHeight = taskElement.offsetHeight;
scrollView.scrollTop = taskTop - (scrollViewHeight / 2) + (taskHeight / 2);
}
}
@ -171,13 +174,16 @@ function handleScrollFocus() {
const scrollView = document.getElementById("scroll-view");
const taskItems = document.querySelectorAll(".task-item");
const scrollTop = scrollView.scrollTop;
const scrollViewRect = scrollView.getBoundingClientRect();
const focusCenter = scrollViewRect.top + scrollViewRect.height / 2;
let closestItem = null;
let closestDistance = Infinity;
taskItems.forEach(item => {
const distance = Math.abs(item.offsetTop - scrollTop);
const itemRect = item.getBoundingClientRect();
const itemCenter = itemRect.top + itemRect.height / 2;
const distance = Math.abs(itemCenter - focusCenter);
item.classList.remove("in-focus");
@ -201,7 +207,9 @@ 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);