reorder subtasks with shift+up/down

Hunter
Tuesday, July 30th, 2024 at 10:43 AM

todo.html+43-9

todo.html
 68  				taskInput.type = 'text';
 69  				taskInput.value = task.text;
 70  				taskInput.addEventListener('keydown', (e) => handleKeyDown(e, task));
    -				taskInput.addEventListener('input', () => task.text = taskInput.value);
 71 +				taskInput.addEventListener('input', () => {
 72 +					task.text = taskInput.value;
 73 +				});
 74  				taskInput.addEventListener('focus', () => setActiveTask(taskInput, task));
 75  
 76  				taskContainer.appendChild(checkbox);

190  			}
191  
192  			function handleKeyDown(e, task) {
    -				if (e.key === 'Enter') {
193 +				if (e.key === 'Enter' && !e.shiftKey) {
194  					e.preventDefault();
    -					if (e.shiftKey) {
    -						toggleTaskState(task);
    -					} else {
    -						addNewTask(currentTask, task);
    -					}
195 +					addNewTask(currentTask, task);
196 +				} else if (e.key === 'Enter' && e.shiftKey) {
197 +					e.preventDefault();
198 +					toggleTaskState(task);
199  				} else if (e.key === 'Backspace' && e.target.value === '' && task !== currentTask) {
200  					e.preventDefault();
201  					deleteTask(task);
    -				} else if (e.key === 'ArrowUp') {
202 +				} else if (e.key === 'ArrowUp' && !e.shiftKey) {
203  					e.preventDefault();
204  					navigateTasks('up');
    -				} else if (e.key === 'ArrowDown') {
205 +				} else if (e.key === 'ArrowDown' && !e.shiftKey) {
206  					e.preventDefault();
207  					navigateTasks('down');
208 +				} else if (e.key === 'ArrowUp' && e.shiftKey) {
209 +					e.preventDefault();
210 +					moveTask(task, 'up');
211 +				} else if (e.key === 'ArrowDown' && e.shiftKey) {
212 +					e.preventDefault();
213 +					moveTask(task, 'down');
214  				} else if (e.key === 'ArrowRight' && e.shiftKey) {
215  					e.preventDefault();
216  					if (task !== currentTask) {

222  				}
223  			}
224  
225 +			function moveTask(task, direction) {
226 +				const parent = findParentTask(task);
227 +				if (!parent) return; // Can't move the root task
228 +
229 +				const index = parent.subtasks.findIndex(t => t.id === task.id);
230 +				if (index === -1) return; // Task not found in parent's subtasks
231 +
232 +				if (direction === 'up' && index > 0) {
233 +					// Move task up
234 +					[parent.subtasks[index - 1], parent.subtasks[index]] = [parent.subtasks[index], parent.subtasks[index - 1]];
235 +				} else if (direction === 'down' && index < parent.subtasks.length - 1) {
236 +					// Move task down
237 +					[parent.subtasks[index], parent.subtasks[index + 1]] = [parent.subtasks[index + 1], parent.subtasks[index]];
238 +				}
239 +
240 +				renderCurrentView();
241 +				selectAndFocusTask(task);
242 +			}
243 +
244  			function addNewTask(parentTask, currentSubtask = null) {
245  				const newTask = { id: Date.now(), text: '', state: 0, subtasks: [], selectedSubtaskId: null };
246  				if (currentSubtask) {

399  
400  				const parentCheckbox = parentElement.querySelector('input[type="checkbox"]');
401  				updateCheckboxState(parentCheckbox, currentTask.state);
402 +
403 +				// Maintain focus on the selected task
404 +				if (currentTask.selectedSubtaskId) {
405 +					const selectedTask = currentTask.subtasks.find(t => t.id === currentTask.selectedSubtaskId);
406 +					if (selectedTask) {
407 +						selectAndFocusTask(selectedTask);
408 +					}
409 +				}
410  			}
411  
412  			renderCurrentView();