Skip to content

Commit 3030944

Browse files
committed
Handle enter, backspace, up, and down arrows now working
1 parent 1d6d43f commit 3030944

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

src/components/ListItems/ListElement.vue

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
</h2>
66
<button @click="clearStorage">clearStorage</button>
77
<div class="input-container">
8-
<input type="text" v-model="newItem" placeholder="Enter an item" @keyup.enter="addItem" class="input-field" spellcheck="false">
8+
<input type="text" v-model="newItem" placeholder="Enter an item" @keyup.enter="addItem" class="input-field"
9+
spellcheck="false">
910
<button @click="addItem" class="add-button">Add Item</button>
1011
</div>
1112
<button @click="togglePopup" class="toggle-popup-button">Toggle Popup</button>
@@ -23,9 +24,12 @@
2324
@dragover="dragOver" @drop="drop(index)">
2425
<div class="item-container" @click="focusEditable(index)">
2526
<button class="remove-button" @click="removeItem(index)">X</button>
26-
<div class="text-cursor item-text" ref="itemSpan" contenteditable="true" @keydown.enter.prevent="handleEnter(index, $event)"
27-
@keydown.backspace="handleBackspace(index, $event)" @blur="updateItem(index, $event)"
28-
spellcheck="false">{{ item }}</div>
27+
<div class="text-cursor item-text" ref="itemSpan" contenteditable="true"
28+
@keydown.enter.prevent="handleEnter(index, $event)"
29+
@keydown.backspace="handleBackspace(index, $event)"
30+
@keydown.up="handleArrowUp(index, $event)"
31+
@keydown.down="handleArrowDown(index, $event)"
32+
@blur="updateItem(index, $event)" spellcheck="false">{{ item }}</div>
2933
</div>
3034
</li>
3135
</ul>
@@ -156,12 +160,41 @@ export default {
156160
});
157161
}
158162
},
159-
handleArrowUp() {
163+
handleArrowUp(index, event) {
164+
if (event.target.innerText.length===0) {
165+
event.preventDefault(); // Prevent default arrow key behavior
166+
this.focusEditable(index - 1, this.itemsArray[index - 1].length);
167+
}
168+
const selection = window.getSelection();
169+
const range = selection.getRangeAt(0);
170+
const rect = range.getBoundingClientRect();
171+
const elementRect = event.target.getBoundingClientRect();
160172
161-
},
162-
handArrowDown() {
173+
// Check if the caret is in the top row
174+
const isTopRow = rect.top === elementRect.top;
163175
176+
if (isTopRow && index > 0 || event.target.innerText.length===0) {
177+
event.preventDefault(); // Prevent default arrow key behavior
178+
this.focusEditable(index - 1, this.itemsArray[index - 1].length);
179+
}
164180
},
181+
handleArrowDown(index, event) {
182+
if (event.target.innerText.length===0) {
183+
event.preventDefault(); // Prevent default arrow key behavior
184+
this.focusEditable(index +1, 0);
185+
}
186+
const selection = window.getSelection();
187+
const range = selection.getRangeAt(0);
188+
const rect = range.getBoundingClientRect();
189+
const elementRect = event.target.getBoundingClientRect();
190+
191+
const isBottomRow = Math.abs(rect.bottom - elementRect.bottom) < 1;
192+
193+
if (isBottomRow && index < this.itemsArray.length || event.target.innerText.length===0) {
194+
event.preventDefault(); // Prevent default arrow key behavior
195+
this.focusEditable(index +1, 0);
196+
}
197+
},
165198
updateItem(index, event) {
166199
this.itemsArray.splice(index, 1, event.target.innerText);
167200
this.saveList();
@@ -202,7 +235,6 @@ export default {
202235
</script>
203236

204237
<style scoped>
205-
206238
.text-cursor {
207239
cursor: text;
208240
}

0 commit comments

Comments
 (0)