From 3030944ad8c7237afbf0a69d46360e84ea343fed Mon Sep 17 00:00:00 2001 From: philwing100 Date: Thu, 6 Jun 2024 10:45:43 -0500 Subject: [PATCH 1/2] Handle enter, backspace, up, and down arrows now working --- src/components/ListItems/ListElement.vue | 48 ++++++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/components/ListItems/ListElement.vue b/src/components/ListItems/ListElement.vue index 771c927..c57be4e 100644 --- a/src/components/ListItems/ListElement.vue +++ b/src/components/ListItems/ListElement.vue @@ -5,7 +5,8 @@
- +
@@ -23,9 +24,12 @@ @dragover="dragOver" @drop="drop(index)">
-
{{ item }}
+
{{ item }}
@@ -156,12 +160,41 @@ export default { }); } }, - handleArrowUp() { + handleArrowUp(index, event) { + if (event.target.innerText.length===0) { + event.preventDefault(); // Prevent default arrow key behavior + this.focusEditable(index - 1, this.itemsArray[index - 1].length); + } + const selection = window.getSelection(); + const range = selection.getRangeAt(0); + const rect = range.getBoundingClientRect(); + const elementRect = event.target.getBoundingClientRect(); - }, - handArrowDown() { + // Check if the caret is in the top row + const isTopRow = rect.top === elementRect.top; + if (isTopRow && index > 0 || event.target.innerText.length===0) { + event.preventDefault(); // Prevent default arrow key behavior + this.focusEditable(index - 1, this.itemsArray[index - 1].length); + } }, + handleArrowDown(index, event) { + if (event.target.innerText.length===0) { + event.preventDefault(); // Prevent default arrow key behavior + this.focusEditable(index +1, 0); + } + const selection = window.getSelection(); + const range = selection.getRangeAt(0); + const rect = range.getBoundingClientRect(); + const elementRect = event.target.getBoundingClientRect(); + + const isBottomRow = Math.abs(rect.bottom - elementRect.bottom) < 1; + + if (isBottomRow && index < this.itemsArray.length || event.target.innerText.length===0) { + event.preventDefault(); // Prevent default arrow key behavior + this.focusEditable(index +1, 0); + } + }, updateItem(index, event) { this.itemsArray.splice(index, 1, event.target.innerText); this.saveList(); @@ -202,7 +235,6 @@ export default {