implement cut functionality (cmd+x)

Hunter
Sunday, August 4th, 2024 at 10:43 PM

index.html+32-13
readme.md+2-1

index.html
139  			right: 25%;
140  			top: 50%;
141  			height: 2px;
    -			background-color: var(--accent-color);
142 +			background-color: var(--accent);
143  			transform: translateY(-50%);
144  		}
145  	</style>

354  
355  				taskInput.addEventListener('keydown', keydownHandler);
356  				taskInput.addEventListener('keyup', keyupHandler);
    -				taskInput.addEventListener('keydown', copyActiveTaskText);
357 +				taskInput.addEventListener('keydown', handleCopyAndCut);
358  
359  				taskInput.addEventListener('input', () => {
360  					task.text = taskInput.value;

632  					if (currentIndex > 0) {
633  						const prevTask = currentIndex === 1 ? currentTask : tasks[currentIndex - 2];
634  						selectAndFocusTask(prevTask);
    -						console.log('placing cursor at beginning');
635  						placeCursorAtBeginning(currentElement);
636  					}
637  				} else {

743  				}
744  			}
745  
    -			function copyActiveTaskText(e) {
    -				// Check if the key combination is Ctrl+C (Windows) or Cmd+C (Mac)
    -				if ((e.ctrlKey || e.metaKey) && e.key === 'c') {
746 +			function handleCopyAndCut(e) {
747 +				if ((e.ctrlKey || e.metaKey) && (e.key === 'c' || e.key === 'x')) {
748  					const activeTaskInput = document.querySelector('.task-container.active input[type="text"]');
749  					if (activeTaskInput) {
    -						// Check if there's a selection within the active task input
    -						const selectedText = activeTaskInput.value.substring(activeTaskInput.selectionStart, activeTaskInput.selectionEnd);
    -						
    -						const textToCopy = selectedText || activeTaskInput.value;
    -						
    -						e.preventDefault(); // Prevent default copy behavior
    -						navigator.clipboard.writeText(textToCopy);
750 +						e.preventDefault();
751 +
752 +						// If there's no selection, select all text
753 +						if (activeTaskInput.selectionStart === activeTaskInput.selectionEnd) {
754 +							activeTaskInput.select();
755 +						}
756 +
757 +						// Use execCommand for copy or cut
758 +						if (e.key === 'c') {
759 +							document.execCommand('copy');
760 +						} else if (e.key === 'x') {
761 +							document.execCommand('cut');
762 +
763 +							// Update the task object
764 +							const taskContainer = activeTaskInput.closest('.task-container');
765 +							const taskId = taskContainer.dataset.id;
766 +							const task = currentTask.id === taskId ? currentTask : currentTask.subtasks.find(t => t.id === taskId);
767 +							if (task) {
768 +								task.text = activeTaskInput.value;
769 +								updateParentTaskState(task);
770 +								scheduleSave();
771 +							}
772 +						}
773 +
774 +						// Deselect text if we had to select all
775 +						if (e.key === 'c' && activeTaskInput.selectionStart === 0 && activeTaskInput.selectionEnd === activeTaskInput.value.length) {
776 +							activeTaskInput.setSelectionRange(activeTaskInput.value.length, activeTaskInput.value.length);
777 +						}
778  					}
779  				}
780  			}
readme.md
34  
35  ### text editing
36  - `Left/Right` move text cursor within selected task
   -- `Command/Ctrl + C` copy the selected task's text (or highlighted substring)
37 +- `Command/Ctrl + C` copy the active task's text (or highlighted substring)
38 +- `Command/Ctrl + X` cut the active task's text (or highlighted substring)
39  - `Command/Ctrl + V` paste text content from the clipboard
40  - `Command/Ctrl + Z` undo text edit
41  - `Command/Ctrl + Shift + Z` redo text edit