1+ const addBtn = document . getElementById ( 'addBtn' ) ;
2+
3+ const updateNote = ( ) => {
4+ const textAreaData = document . querySelectorAll ( 'textarea' )
5+ const notes = [ ] ;
6+ textAreaData . forEach ( ( note ) => {
7+ if ( note . value != "" ) {
8+ return notes . push ( note . value ) ;
9+ }
10+ } ) ;
11+ localStorage . setItem ( 'notes' , JSON . stringify ( notes ) ) ;
12+ }
13+
14+ const addNewNote = ( txt = '' ) => {
15+
16+ const notesContainer = document . getElementById ( 'notesContainer' ) ;
17+ const note = document . createElement ( 'div' ) ;
18+ const classes = [ 'note' , 'h-52' , 'p-2' , 'bg-purple-800' , 'rounded' , 'flex' , 'flex-col' , 'shadow-lg' , 'border-b-4' , 'border-yellow-400' ] ;
19+ note . classList . add ( ...classes ) ;
20+
21+ const htmlData = `
22+ <div class="operation p-0.5 flex justify-end gap-2 mb-0.5">
23+ <button class="material-icons-round edit bg-green-400 p-1 rounded hover:bg-green-600 hover:text-white transition-colors duration-75">${ txt ? "edit_note" : "done" } </button>
24+ <button class="material-icons-round delete bg-red-600 text-white p-1 rounded hover:bg-red-700 transition-colors duration-75">delete</button>
25+ </div>
26+
27+ <div class="noteTxt p-1 overflow-y-auto pb-4 text-white ${ txt ? "" : "hidden" } "></div>
28+ <textarea class="p-1 outline-none border border-yellow-500 rounded resize-none w-full placeholder-gray-500 ${ txt ? "hidden" : "" } " rows="6" placeholder="Type your note.."></textarea>` ;
29+
30+ note . insertAdjacentHTML ( 'afterbegin' , htmlData ) ;
31+
32+ const editBtn = note . querySelector ( '.edit' ) ;
33+ const delBtn = note . querySelector ( '.delete' ) ;
34+ const noteTxt = note . querySelector ( '.noteTxt' ) ;
35+ const textArea = note . querySelector ( 'textarea' ) ;
36+
37+ // delete note
38+ delBtn . addEventListener ( 'click' , ( ) => {
39+ note . classList . add ( 'animate-ping' , 'opacity-30' ) ;
40+ setTimeout ( ( ) => {
41+ note . remove ( ) ;
42+ updateNote ( ) ;
43+ } , 200 ) ;
44+ } ) ;
45+
46+ textArea . value = txt ;
47+ noteTxt . innerHTML = txt ;
48+
49+ // edit btn event
50+ editBtn . addEventListener ( 'click' , ( ) => {
51+ noteTxt . classList . toggle ( 'hidden' ) ;
52+ textArea . classList . toggle ( 'hidden' ) ;
53+ if ( textArea . classList . contains ( 'hidden' ) ) {
54+ editBtn . textContent = "edit_note" ;
55+ } else {
56+ editBtn . textContent = "done" ;
57+ }
58+ updateNote ( ) ;
59+ } ) ;
60+
61+ textArea . addEventListener ( 'change' , ( event ) => {
62+ const value = event . target . value ;
63+ noteTxt . innerHTML = value ;
64+ if ( note . value != "" ) {
65+ updateNote ( ) ;
66+ }
67+ } ) ;
68+
69+ notesContainer . appendChild ( note ) ;
70+ }
71+
72+ // fetch stored notes from localstorage
73+ const notes = JSON . parse ( localStorage . getItem ( 'notes' ) ) ;
74+ if ( notes ) {
75+ notes . forEach ( ( note ) => addNewNote ( note ) ) ;
76+ }
77+
78+ // add new note btn event
79+ addBtn . addEventListener ( 'click' , ( ) => addNewNote ( ) ) ;
0 commit comments