1
1
<template >
2
2
<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 " >
4
4
{{ title }}
5
5
</h2 >
6
6
<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 >
19
12
</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 >
20
19
</div >
21
20
<div class =" ListContainer" >
22
21
<ul class =" ListItem" >
25
24
<div class =" item-container" @click =" focusEditable(index)" >
26
25
<button class =" remove-button" @click =" removeItem(index)" >X</button >
27
26
<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)"
32
29
@blur =" updateItem(index, $event)" spellcheck =" false" >{{ item }}</div >
33
30
</div >
34
31
</li >
@@ -43,17 +40,17 @@ export default {
43
40
props: {
44
41
listName: {
45
42
type: String ,
46
- required: true
43
+ required: false
47
44
}
48
45
},
49
46
data () {
50
47
return {
51
48
title: this .listName ,
52
- showPopup: false ,
53
49
newItem: ' ' ,
54
50
itemsArray: [],
55
- listsArray: [],
56
- draggedIndex: null
51
+ titlesArray: [],
52
+ draggedIndex: null ,
53
+ isDropdownOpen: false
57
54
};
58
55
},
59
56
created () {
@@ -67,32 +64,55 @@ export default {
67
64
this .title = storedTitle;
68
65
}
69
66
70
- const storedItemsArray = localStorage .getItem (' itemsArray ' );
67
+ const storedItemsArray = localStorage .getItem (this . title );
71
68
if (storedItemsArray) {
72
69
this .itemsArray = JSON .parse (storedItemsArray);
73
70
}
71
+
72
+ const storedTitlesArray = localStorage .getItem (' titlesArray' );
73
+ if (storedTitlesArray){
74
+ this .titlesArray = JSON .parse (storedTitlesArray);
75
+ }
74
76
},
75
77
updateTitle () {
78
+ this .saveList ();// Save the current list items to the title before changing the title
76
79
const titleElement = this .$refs .titleInput .innerText ;
77
80
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
+ }
78
95
localStorage .setItem (' title' , this .title );
96
+ let newItems = localStorage .getItem (this .title )
97
+ this .itemsArray = JSON .parse (newItems);
79
98
},
80
99
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 ));
82
104
},
83
105
addItem () {
84
106
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 );
85
111
this .itemsArray .push (this .newItem .trim ());
86
112
this .newItem = ' ' ;
87
113
this .saveList ();
88
114
}
89
115
},
90
- togglePopup () {
91
- this .showPopup = ! this .showPopup ;
92
- },
93
- closePopup () {
94
- this .showPopup = false ;
95
- },
96
116
dragStart (index ) {
97
117
this .draggedIndex = index;
98
118
},
@@ -139,13 +159,11 @@ export default {
139
159
},
140
160
handleBackspace (index , event ) {
141
161
if (window .getSelection ().anchorOffset === 0 && index > 0 ) {
142
- event .preventDefault (); // Prevent default backspace behavior
143
162
144
163
const currentText = event .target .innerText ;
145
164
const previousText = this .itemsArray [index - 1 ];
146
165
const newText = previousText + currentText;
147
166
148
- // Update the itemsArray correctly
149
167
this .itemsArray .splice (index - 1 , 1 , newText);
150
168
this .itemsArray .splice (index, 1 );
151
169
@@ -161,7 +179,7 @@ export default {
161
179
}
162
180
},
163
181
handleArrowUp (index , event ) {
164
- if (event .target .innerText .length === 0 ) {
182
+ if (event .target .innerText .length === 0 ) {
165
183
event .preventDefault (); // Prevent default arrow key behavior
166
184
this .focusEditable (index - 1 , this .itemsArray [index - 1 ].length );
167
185
}
@@ -173,35 +191,35 @@ export default {
173
191
// Check if the caret is in the top row
174
192
const isTopRow = rect .top === elementRect .top ;
175
193
176
- if (isTopRow && index > 0 || event .target .innerText .length === 0 ) {
194
+ if (isTopRow && index > 0 || event .target .innerText .length === 0 ) {
177
195
event .preventDefault (); // Prevent default arrow key behavior
178
196
this .focusEditable (index - 1 , this .itemsArray [index - 1 ].length );
179
197
}
180
198
},
181
199
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 ();
190
208
191
- const isBottomRow = Math .abs (rect .bottom - elementRect .bottom ) < 1 ;
209
+ const isBottomRow = Math .abs (rect .bottom - elementRect .bottom ) < 1 ;
192
210
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
+ },
198
216
updateItem (index , event ) {
199
217
this .itemsArray .splice (index, 1 , event .target .innerText );
200
218
this .saveList ();
201
219
},
202
220
removeItem (index ) {
203
221
this .itemsArray .splice (index, 1 );
204
- localStorage . setItem ( ' itemsArray ' , JSON . stringify ( this .itemsArray ) );
222
+ this .saveList ( );
205
223
206
224
// Check if the array is not empty and the index is valid before focusing
207
225
if (this .itemsArray .length > 0 ) {
@@ -229,12 +247,57 @@ export default {
229
247
clearStorage () {
230
248
localStorage .clear ();
231
249
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
232
258
}
233
259
}
234
260
}
235
261
</script >
236
262
237
263
<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
+
238
301
.text-cursor {
239
302
cursor : text ;
240
303
}
@@ -274,11 +337,13 @@ export default {
274
337
}
275
338
276
339
.input-container {
277
- margin-bottom : 10 px ;
340
+ margin-bottom : 0 px ;
278
341
display : flex ;
279
342
align-items : center ;
280
343
}
281
344
345
+
346
+
282
347
.input-field {
283
348
padding : 8px 12px ;
284
349
border : 1px solid #ccc ;
0 commit comments