|
5 | 5 | </h2>
|
6 | 6 | <button @click="clearStorage">clearStorage</button>
|
7 | 7 | <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"> |
9 | 10 | <button @click="addItem" class="add-button">Add Item</button>
|
10 | 11 | </div>
|
11 | 12 | <button @click="togglePopup" class="toggle-popup-button">Toggle Popup</button>
|
|
23 | 24 | @dragover="dragOver" @drop="drop(index)">
|
24 | 25 | <div class="item-container" @click="focusEditable(index)">
|
25 | 26 | <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> |
29 | 33 | </div>
|
30 | 34 | </li>
|
31 | 35 | </ul>
|
@@ -156,12 +160,41 @@ export default {
|
156 | 160 | });
|
157 | 161 | }
|
158 | 162 | },
|
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(); |
160 | 172 |
|
161 |
| - }, |
162 |
| - handArrowDown() { |
| 173 | + // Check if the caret is in the top row |
| 174 | + const isTopRow = rect.top === elementRect.top; |
163 | 175 |
|
| 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 | + } |
164 | 180 | },
|
| 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 | + }, |
165 | 198 | updateItem(index, event) {
|
166 | 199 | this.itemsArray.splice(index, 1, event.target.innerText);
|
167 | 200 | this.saveList();
|
@@ -202,7 +235,6 @@ export default {
|
202 | 235 | </script>
|
203 | 236 |
|
204 | 237 | <style scoped>
|
205 |
| -
|
206 | 238 | .text-cursor {
|
207 | 239 | cursor: text;
|
208 | 240 | }
|
|
0 commit comments