commit 326d6cb66c96e5828371f023dda2c2c0bdbf1bdf
parent b9207ea9c266325edc7e2e08ff3a3f62637c8ba6
Author: Hunter
Date:   Wed,  4 Mar 2026 15:23:46 -0500

consolidate duplicate code

Diffstat:
Mindex.html | 138++++++++++++-------------------------------------------------------------------
1 file changed, 20 insertions(+), 118 deletions(-)

diff --git a/index.html b/index.html @@ -892,13 +892,13 @@ navigateTasks('down'); } else if (e.key === 'ArrowLeft' && (e.metaKey || e.ctrlKey) && e.shiftKey) { e.preventDefault(); - pullSubtaskOutLayerAndNavigate(task); + pullSubtaskOutLayer(task, true); } else if (e.key === 'ArrowUp' && (e.metaKey || e.ctrlKey) && e.shiftKey) { e.preventDefault(); - pushSubtaskIntoTargetAndNavigate(task, 'up'); + pushSubtaskIntoTarget(task, 'up', true); } else if (e.key === 'ArrowDown' && (e.metaKey || e.ctrlKey) && e.shiftKey) { e.preventDefault(); - pushSubtaskIntoTargetAndNavigate(task, 'down'); + pushSubtaskIntoTarget(task, 'down', true); } else if (e.key === 'ArrowUp' && e.shiftKey) { e.preventDefault(); moveSubtask(task, 'up'); @@ -946,60 +946,16 @@ scheduleSave(); } - function pushSubtaskIntoTarget(subtask, direction) { + function pushSubtaskIntoTarget(subtask, direction, navigate = false) { // Don't allow pushing the current parent task if (subtask === currentTask) { return; } - const parentTask = findParentTask(subtask); - if (!parentTask) return; - - const index = parentTask.subtasks.findIndex(t => t.id === subtask.id); - if (index === -1) return; - - let targetTask = null; - if (direction === 'up' && index > 0) { - targetTask = parentTask.subtasks[index - 1]; - } else if (direction === 'down' && index < parentTask.subtasks.length - 1) { - targetTask = parentTask.subtasks[index + 1]; - } - - if (!targetTask) return; - - // Remove the subtask from its current parent - parentTask.subtasks.splice(index, 1); - - // Add the subtask to the target task's subtasks - targetTask.subtasks.push(subtask); - - // Adjust moved task's state for its new parent - adjustMovedTaskState(subtask, targetTask); - - // Update parent task state after removal - updateTaskAndAncestors(parentTask); - - // Update target task state after addition - updateTaskAndAncestors(targetTask); - - // Re-render the view - renderCurrentView(); - - // Focus on the target task that now contains the moved subtask - selectAndFocusTask(targetTask); - - scheduleSave(); - } - - function pushSubtaskIntoTargetAndNavigate(subtask, direction) { - // Don't allow pushing the current parent task - if (subtask === currentTask) { - return; + if (navigate) { + document.documentElement.style.scrollBehavior = 'auto'; } - // Temporarily disable smooth scrolling - document.documentElement.style.scrollBehavior = 'auto'; - const parentTask = findParentTask(subtask); if (!parentTask) return; @@ -1030,13 +986,17 @@ // Update target task state after addition updateTaskAndAncestors(targetTask); - // Navigate into the target task and select the moved subtask - navigateIntoTaskAndSelectSubtask(targetTask, subtask); + if (navigate) { + navigateIntoTaskAndSelectSubtask(targetTask, subtask); + } else { + renderCurrentView(); + selectAndFocusTask(targetTask); + } scheduleSave(); } - function pullSubtaskOutLayer(subtask) { + function pullSubtaskOutLayer(subtask, navigate = false) { // Don't allow pulling the current parent task if (subtask === currentTask) { return; @@ -1047,11 +1007,15 @@ return; } + if (navigate) { + document.documentElement.style.scrollBehavior = 'auto'; + } + const currentParent = findParentTask(subtask); if (!currentParent) return; // Find the grandparent (the outer layer we're pulling into) - const grandParent = findGrandParentTask(currentParent); + const grandParent = findParentTask(currentParent); if (!grandParent) return; // Remove the subtask from its current parent @@ -1076,19 +1040,16 @@ updateTaskAndAncestors(currentParent); updateTaskAndAncestors(grandParent); - // Handle view and selection based on remaining tasks at current level - if (currentParent.subtasks.length === 0 && taskPath.length > 1) { - // No remaining subtasks, navigate to parent level and select the moved task + if (navigate || (currentParent.subtasks.length === 0 && taskPath.length > 1)) { + // Navigate to parent level and select the moved task navigateToParentTaskAndSelectTask(subtask); } else { // Stay at current level and select adjacent task renderCurrentView(); if (currentParent.subtasks.length > 0) { - // Select the task that was above the moved one, or below if at the top const targetIndex = Math.max(0, currentIndex - 1); selectAndFocusTask(currentParent.subtasks[targetIndex]); } else { - // Focus on the parent task if no subtasks remain selectAndFocusTask(currentParent); } } @@ -1096,65 +1057,6 @@ scheduleSave(); } - function pullSubtaskOutLayerAndNavigate(subtask) { - // Don't allow pulling the current parent task - if (subtask === currentTask) { - return; - } - - // Don't allow pulling when at root level - if (taskPath.length <= 1) { - return; - } - - // Temporarily disable smooth scrolling - document.documentElement.style.scrollBehavior = 'auto'; - - const currentParent = findParentTask(subtask); - if (!currentParent) return; - - // Find the grandparent (the outer layer we're pulling into) - const grandParent = findGrandParentTask(currentParent); - if (!grandParent) return; - - // Remove the subtask from its current parent - const currentIndex = currentParent.subtasks.findIndex(t => t.id === subtask.id); - if (currentIndex === -1) return; - currentParent.subtasks.splice(currentIndex, 1); - - // Find where to insert in the grandparent's subtasks - const currentParentIndex = grandParent.subtasks.findIndex(t => t.id === currentParent.id); - if (currentParentIndex === -1) { - // Fallback: add to end of grandparent's subtasks - grandParent.subtasks.push(subtask); - } else { - // Insert after the current parent's position - grandParent.subtasks.splice(currentParentIndex + 1, 0, subtask); - } - - // Adjust moved task's state for its new parent - adjustMovedTaskState(subtask, grandParent); - - // Update task states - updateTaskAndAncestors(currentParent); - updateTaskAndAncestors(grandParent); - - // Always navigate to parent level and select the moved task - navigateToParentTaskAndSelectTask(subtask); - - scheduleSave(); - } - - function findGrandParentTask(parentTask) { - // Find the grandparent of the given parent task - for (let i = taskPath.length - 1; i >= 0; i--) { - const potentialGrandParent = taskPath[i]; - if (potentialGrandParent.subtasks.some(t => t.id === parentTask.id)) { - return potentialGrandParent; - } - } - return null; - } function addNewSubtask(parentTask, currentSubtask = null) { const newSubtask = { id: Date.now(), text: '', state: 0, subtasks: [], selectedSubtaskId: null };