-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes-edit.js
53 lines (43 loc) · 1.46 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
51
52
53
"use strict";
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("/notes-app/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("/notes-app/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);
}
});