Skip to content

Commit dee4c1d

Browse files
committed
Working localstorage and multiple lists being live updated :)
1 parent 3030944 commit dee4c1d

File tree

2 files changed

+114
-49
lines changed

2 files changed

+114
-49
lines changed

src/components/ListItems/DropDownMenu.vue

Whitespace-only changes.

src/components/ListItems/ListElement.vue

Lines changed: 114 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
<template>
22
<div class="template-container">
3-
<h2 @blur="updateTitle" contenteditable="true" ref="titleInput" @keydown.enter.prevent="">
3+
<h2 @blur="updateTitle" contenteditable="true" ref="titleInput" @keydown.enter.prevent="" spellcheck="false">
44
{{ title }}
55
</h2>
66
<button @click="clearStorage">clearStorage</button>
7-
<div class="input-container">
8-
<input type="text" v-model="newItem" placeholder="Enter an item" @keyup.enter="addItem" class="input-field"
9-
spellcheck="false">
10-
<button @click="addItem" class="add-button">Add Item</button>
11-
</div>
12-
<button @click="togglePopup" class="toggle-popup-button">Toggle Popup</button>
13-
<div class="popup-overlay" v-if="showPopup">
14-
<div class="popup">
15-
<span class="close" @click="closePopup">X</span>
16-
<div class="popup-content">
17-
<!-- Optional Popup Content -->
18-
</div>
7+
<div>
8+
<div class="input-container">
9+
<input type="text" v-model="newItem" placeholder="Enter an item" @keyup.enter="addItem" class="input-field"
10+
spellcheck="false">
11+
<button @click="addItem" class="add-button">Add Item</button>
1912
</div>
13+
<div class="dropdown">
14+
<button @click="toggleDropdown" class="dropbtn">Dropdown</button>
15+
<div v-if="isDropdownOpen" class="dropdown-content">
16+
<a v-for="(option, index) in titlesArray" :key="index" @click="selectOption(option)">{{ option }}</a>
17+
</div>
18+
</div>
2019
</div>
2120
<div class="ListContainer">
2221
<ul class="ListItem">
@@ -25,10 +24,8 @@
2524
<div class="item-container" @click="focusEditable(index)">
2625
<button class="remove-button" @click="removeItem(index)">X</button>
2726
<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)"
27+
@keydown.enter.prevent="handleEnter(index, $event)" @keydown.backspace="handleBackspace(index, $event)"
28+
@keydown.up="handleArrowUp(index, $event)" @keydown.down="handleArrowDown(index, $event)"
3229
@blur="updateItem(index, $event)" spellcheck="false">{{ item }}</div>
3330
</div>
3431
</li>
@@ -43,17 +40,17 @@ export default {
4340
props: {
4441
listName: {
4542
type: String,
46-
required: true
43+
required: false
4744
}
4845
},
4946
data() {
5047
return {
5148
title: this.listName,
52-
showPopup: false,
5349
newItem: '',
5450
itemsArray: [],
55-
listsArray: [],
56-
draggedIndex: null
51+
titlesArray: [],
52+
draggedIndex: null,
53+
isDropdownOpen: false
5754
};
5855
},
5956
created() {
@@ -67,32 +64,55 @@ export default {
6764
this.title = storedTitle;
6865
}
6966
70-
const storedItemsArray = localStorage.getItem('itemsArray');
67+
const storedItemsArray = localStorage.getItem(this.title);
7168
if (storedItemsArray) {
7269
this.itemsArray = JSON.parse(storedItemsArray);
7370
}
71+
72+
const storedTitlesArray = localStorage.getItem('titlesArray');
73+
if(storedTitlesArray){
74+
this.titlesArray = JSON.parse(storedTitlesArray);
75+
}
7476
},
7577
updateTitle() {
78+
this.saveList();//Save the current list items to the title before changing the title
7679
const titleElement = this.$refs.titleInput.innerText;
7780
this.title = titleElement;
81+
let flag=true;
82+
if(this.titlesArray.length !== 0){
83+
for(let i=0;i<this.titlesArray.length;i++){
84+
// console.log(this.titlesArray[i]);
85+
// console.log(this.title);
86+
if(this.titlesArray[i]===this.title){
87+
flag=false;
88+
}
89+
}
90+
}
91+
if(flag){
92+
this.titlesArray.push(this.title);
93+
this.saveTitlesArray();
94+
}
7895
localStorage.setItem('title', this.title);
96+
let newItems = localStorage.getItem(this.title)
97+
this.itemsArray=JSON.parse(newItems);
7998
},
8099
saveList() {
81-
localStorage.setItem('itemsArray', JSON.stringify(this.itemsArray));
100+
localStorage.setItem(this.title, JSON.stringify(this.itemsArray));
101+
},
102+
saveTitlesArray(){
103+
localStorage.setItem('titlesArray', JSON.stringify(this.titlesArray));
82104
},
83105
addItem() {
84106
if (this.newItem.trim() !== '') {
107+
if (!this.itemsArray) {
108+
this.itemsArray = []; // Ensure itemsArray is initialized
109+
}
110+
console.log(this.title + " " + this.itemsArray + " " + this.titlesArray);
85111
this.itemsArray.push(this.newItem.trim());
86112
this.newItem = '';
87113
this.saveList();
88114
}
89115
},
90-
togglePopup() {
91-
this.showPopup = !this.showPopup;
92-
},
93-
closePopup() {
94-
this.showPopup = false;
95-
},
96116
dragStart(index) {
97117
this.draggedIndex = index;
98118
},
@@ -139,13 +159,11 @@ export default {
139159
},
140160
handleBackspace(index, event) {
141161
if (window.getSelection().anchorOffset === 0 && index > 0) {
142-
event.preventDefault(); // Prevent default backspace behavior
143162
144163
const currentText = event.target.innerText;
145164
const previousText = this.itemsArray[index - 1];
146165
const newText = previousText + currentText;
147166
148-
// Update the itemsArray correctly
149167
this.itemsArray.splice(index - 1, 1, newText);
150168
this.itemsArray.splice(index, 1);
151169
@@ -161,7 +179,7 @@ export default {
161179
}
162180
},
163181
handleArrowUp(index, event) {
164-
if (event.target.innerText.length===0) {
182+
if (event.target.innerText.length === 0) {
165183
event.preventDefault(); // Prevent default arrow key behavior
166184
this.focusEditable(index - 1, this.itemsArray[index - 1].length);
167185
}
@@ -173,35 +191,35 @@ export default {
173191
// Check if the caret is in the top row
174192
const isTopRow = rect.top === elementRect.top;
175193
176-
if (isTopRow && index > 0 || event.target.innerText.length===0) {
194+
if (isTopRow && index > 0 || event.target.innerText.length === 0) {
177195
event.preventDefault(); // Prevent default arrow key behavior
178196
this.focusEditable(index - 1, this.itemsArray[index - 1].length);
179197
}
180198
},
181199
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();
200+
if (event.target.innerText.length === 0) {
201+
event.preventDefault(); // Prevent default arrow key behavior
202+
this.focusEditable(index + 1, 0);
203+
}
204+
const selection = window.getSelection();
205+
const range = selection.getRangeAt(0);
206+
const rect = range.getBoundingClientRect();
207+
const elementRect = event.target.getBoundingClientRect();
190208
191-
const isBottomRow = Math.abs(rect.bottom - elementRect.bottom) < 1;
209+
const isBottomRow = Math.abs(rect.bottom - elementRect.bottom) < 1;
192210
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-
},
211+
if (isBottomRow && index < this.itemsArray.length || event.target.innerText.length === 0) {
212+
event.preventDefault(); // Prevent default arrow key behavior
213+
this.focusEditable(index + 1, 0);
214+
}
215+
},
198216
updateItem(index, event) {
199217
this.itemsArray.splice(index, 1, event.target.innerText);
200218
this.saveList();
201219
},
202220
removeItem(index) {
203221
this.itemsArray.splice(index, 1);
204-
localStorage.setItem('itemsArray', JSON.stringify(this.itemsArray));
222+
this.saveList();
205223
206224
// Check if the array is not empty and the index is valid before focusing
207225
if (this.itemsArray.length > 0) {
@@ -229,12 +247,57 @@ export default {
229247
clearStorage() {
230248
localStorage.clear();
231249
this.loadInitialData();
250+
},
251+
toggleDropdown() {
252+
this.isDropdownOpen = !this.isDropdownOpen;
253+
},
254+
selectOption(option) {
255+
// Do something with the selected option
256+
console.log('Selected option:', option);
257+
this.isDropdownOpen = false; // Close the dropdown after selecting an option
232258
}
233259
}
234260
}
235261
</script>
236262

237263
<style scoped>
264+
.dropbtn{
265+
margin-bottom: 10px;
266+
min-width: 20px;
267+
max-width:200px;
268+
width:200px;
269+
}
270+
271+
272+
.dropdown {
273+
position: relative;
274+
display: inline-block;
275+
}
276+
277+
.dropdown-content {
278+
display: none;
279+
position: absolute;
280+
background-color: #f9f9f9;
281+
min-width: 160px;
282+
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
283+
z-index: 1;
284+
}
285+
286+
.dropdown-content a {
287+
color: black;
288+
padding: 12px 16px;
289+
text-decoration: none;
290+
display: block;
291+
}
292+
293+
.dropdown-content a:hover {
294+
background-color: #f1f1f1;
295+
}
296+
297+
.dropdown:hover .dropdown-content {
298+
display: block;
299+
}
300+
238301
.text-cursor {
239302
cursor: text;
240303
}
@@ -274,11 +337,13 @@ export default {
274337
}
275338
276339
.input-container {
277-
margin-bottom: 10px;
340+
margin-bottom: 0px;
278341
display: flex;
279342
align-items: center;
280343
}
281344
345+
346+
282347
.input-field {
283348
padding: 8px 12px;
284349
border: 1px solid #ccc;

0 commit comments

Comments
 (0)