-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes-edit.js
50 lines (42 loc) · 1.42 KB
/
notes-edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const titleElement = document.querySelector('#note-title')
const bodyElement = document.querySelector('#note-body')
const removeElement = document.querySelector('#remove-note')
const dateElement = document.querySelector('#last-edited')
const noteId = location.hash.substring(1)
let notes = getSavedNotes()
let note = notes.find((note) => note.id === noteId)
if(!note){
location.assign('/index.html')
}
titleElement.value = note.title
bodyElement.value = note.body
dateElement.textContent = generateLastEdited(note.updatedAt)
titleElement.addEventListener('input', (e) => {
note.title = e.target.value
note.updatedAt = moment().valueOf()
dateElement.textContent = generateLastEdited(note.updatedAt)
saveNotes(notes)
})
bodyElement.addEventListener('input', (e) => {
note.body = e.target.value
note.updatedAt = moment().valueOf()
dateElement.textContent = generateLastEdited(note.updatedAt)
saveNotes(notes)
})
removeElement.addEventListener('click', (e) => {
removeNote(note.id)
saveNotes(notes)
location.assign('/index.html')
})
window.addEventListener('storage', (e) => {
if(e.key=== 'notes'){
notes = JSON.parse(e.newValue)
note = notes.find((note) => note.id === noteId)
if(!note){
location.assign('/index.html')
}
titleElement.value = note.title
bodyElement.value = note.body
dateElement.textContent = generateLastEdited(note.updatedAt)
}
})