diff --git a/README.md b/README.md index fdc1c83..64328fe 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ Crear una aplicación "Mis Notas" que permita ver notas creadas, crear nuevas y eliminarlas. *Notica adicional -> Las notas deben ser recordadas si la pestaña es recargada. -[] Dar estilos al botón editar -[] Agregar botones guardar y cancelar -[] convertir p en input mientras edición +[X] Dar estilos al botón editar +[X] Agregar botones guardar y cancelar +[X] convertir p en input mientras edición +[] Improve scrollbar style +[] Enable color options +[] Fix position color options + diff --git a/app.js b/app.js index a236144..86d3e57 100644 --- a/app.js +++ b/app.js @@ -26,7 +26,9 @@ function displayAllNotes() { - + + + `; @@ -77,7 +79,9 @@ function addNote (){ - + + + `; @@ -89,30 +93,79 @@ function addNote (){ } } +// Editar el texto de la nota function editNote(button){ const liElement = button.closest('li'); const pEditable = liElement.querySelector('p'); - const noteId = liElement.getAttribute('data-id'); + const newInput = document.createElement('input'); + + newInput.id = 'editable'; + newInput.type = 'text'; + newInput.value = pEditable.textContent + + liElement.replaceChild(newInput, pEditable); + liElement.classList.add('edit-mode') - console.log(pEditable.textContent); - let newText = prompt("Escribe en nuevo texto para tu nota: "); - pEditable.textContent = newText; + // Estado de visibilidad botones + const btnSave = liElement.querySelector('.save'); + const btnCancel = liElement.querySelector('.cancel'); + btnSave.classList.remove('hide') + btnCancel.classList.remove('hide') +} + +// Guardar la nota con la edición realizada +function saveNoteEdit(button){ + const liElement = button.closest('li'); + const noteId = liElement.getAttribute('data-id'); + const newInput = document.querySelector('input'); let existingNotes = JSON.parse(localStorage.getItem('notes')) || []; - existingNotes = existingNotes.map(function(note){ - - if(note.id == noteId){ - note.text = newText; + existingNotes = existingNotes.map(note => { + if(note.id === noteId){ + note.text = newInput.value; } return note - }) + }); // Actualizar las notas en localStorage localStorage.setItem('notes', JSON.stringify(existingNotes)); + const pEditado = document.createElement('p'); + pEditado.textContent = newInput.value; + liElement.replaceChild(pEditado, newInput); + liElement.classList.remove('edit-mode'); + + // Estado de visibilidad botones + const btnSave = liElement.querySelector('.save'); + const btnCancel = liElement.querySelector('.cancel'); + + btnSave.classList.add('hide') + btnCancel.classList.add('hide') +} + +// Cancelar la edición de la nota +function cancelNoteEdit(button){ + const liElement = button.closest('li'); + const noteId = liElement.getAttribute('data-id'); + const newInput = document.querySelector('input'); + + let existingNotes = JSON.parse(localStorage.getItem('notes')) || []; + const originalNote = existingNotes.find(note => note.id === noteId); + + const pEditado = document.createElement('p'); + pEditado.textContent = originalNote.text; + liElement.replaceChild(pEditado, newInput); + liElement.classList.remove('edit-mode'); + + // Estado de visibilidad botones + const btnSave = liElement.querySelector('.save'); + const btnCancel = liElement.querySelector('.cancel'); + + btnSave.classList.add('hide') + btnCancel.classList.add('hide') } // función para eliminar nota diff --git a/index.html b/index.html index e0a4a05..e5b89ca 100644 --- a/index.html +++ b/index.html @@ -11,7 +11,7 @@