Add debug logging for scroll position restoration

This commit is contained in:
Yuyao Huang 2026-05-09 14:16:15 +08:00
parent 643ba56768
commit 1f4efcd7b3

View File

@ -41,20 +41,26 @@ async function loadTasks() {
isInitializing = true; isInitializing = true;
try { try {
tasks = await get(`/api/tasks?goal_id=${selectedGoalId}`); tasks = await get(`/api/tasks?goal_id=${selectedGoalId}`);
console.log("loadTasks: got", tasks.length, "tasks");
console.log("Task order:", tasks.map(t => ({id: t.id, title: t.title, status: t.status})));
renderTasks(); renderTasks();
initSortable(); initSortable();
const currentGoal = goals.find(g => g.id === selectedGoalId); const currentGoal = goals.find(g => g.id === selectedGoalId);
const savedTaskId = currentGoal ? currentGoal.selected_task_id : null; const savedTaskId = currentGoal ? currentGoal.selected_task_id : null;
console.log("loadTasks: savedTaskId from goal =", savedTaskId);
const savedTaskExists = savedTaskId && tasks.some(t => t.id === savedTaskId); const savedTaskExists = savedTaskId && tasks.some(t => t.id === savedTaskId);
console.log("loadTasks: savedTaskExists =", savedTaskExists);
if (savedTaskExists) { if (savedTaskExists) {
console.log("loadTasks: using savedTaskId path");
scrollToTask(savedTaskId); scrollToTask(savedTaskId);
if (isLandscapeMode()) { if (isLandscapeMode()) {
selectTask(savedTaskId); selectTask(savedTaskId);
} }
} else { } else {
console.log("loadTasks: using fallback path");
const doingTask = tasks.find(t => t.status === "doing"); const doingTask = tasks.find(t => t.status === "doing");
if (doingTask) { if (doingTask) {
scrollToTask(doingTask.id); scrollToTask(doingTask.id);
@ -144,13 +150,19 @@ function initSortable() {
} }
function scrollToTask(taskId) { function scrollToTask(taskId) {
console.log("scrollToTask called:", taskId);
const taskElement = document.querySelector(`[data-task-id="${taskId}"]`); const taskElement = document.querySelector(`[data-task-id="${taskId}"]`);
if (taskElement) { if (taskElement) {
const scrollView = document.getElementById("scroll-view"); const scrollView = document.getElementById("scroll-view");
const taskTop = taskElement.offsetTop; const taskTop = taskElement.offsetTop;
const scrollViewHeight = scrollView.clientHeight; const scrollViewHeight = scrollView.clientHeight;
const taskHeight = taskElement.offsetHeight; const taskHeight = taskElement.offsetHeight;
scrollView.scrollTop = taskTop - (scrollViewHeight / 2) + (taskHeight / 2); const targetScrollTop = taskTop - (scrollViewHeight / 2) + (taskHeight / 2);
console.log("Scrolling to:", targetScrollTop, "element offsetTop:", taskTop);
scrollView.scrollTop = targetScrollTop;
console.log("scrollTop after set:", scrollView.scrollTop);
} else {
console.log("Task element not found for id:", taskId);
} }
} }