-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (130 loc) · 4.13 KB
/
script.js
File metadata and controls
152 lines (130 loc) · 4.13 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const judul = document.getElementById("judul");
const warna = document.getElementById("warna");
const isi = document.getElementById("isi-notes");
const file = document.getElementById("file");
const buttonSubmit = document.getElementById("submit");
const kolom = document.getElementById("kolom-data");
//step 2
let notes = JSON.parse(localStorage.getItem("notes")) || [];
function saveNotes() {
localStorage.setItem("notes", JSON.stringify(notes));
}
//bolding
isi.addEventListener("keydown", function(e) {
if (e.ctrlKey && e.key.toLowerCase() === "b") {
e.preventDefault(); // cegah browser default (bold semua halaman)
const start = this.selectionStart;
const end = this.selectionEnd;
const text = this.value;
if (start !== end) {
// bungkus teks yang dipilih dengan <b></b>
const before = text.substring(0, start);
const selected = text.substring(start, end);
const after = text.substring(end);
this.value = before + "<b>" + selected + "</b>" + after;
}
}
});
//italic
isi.addEventListener("keydown", function(e) {
if (e.ctrlKey && e.key.toLowerCase() === "i") {
e.preventDefault(); // cegah browser default (bold semua halaman)
const start = this.selectionStart;
const end = this.selectionEnd;
const text = this.value;
if (start !== end) {
// bungkus teks yang dipilih dengan <b></b>
const before = text.substring(0, start);
const selected = text.substring(start, end);
const after = text.substring(end);
this.value = before + "<i>" + selected + "</i>" + after;
}
}
});
//underline
isi.addEventListener("keydown", function(e) {
if (e.ctrlKey && e.key.toLowerCase() === "u") {
e.preventDefault(); // cegah browser default (bold semua halaman)
const start = this.selectionStart;
const end = this.selectionEnd;
const text = this.value;
if (start !== end) {
// bungkus teks yang dipilih dengan <b></b>
const before = text.substring(0, start);
const selected = text.substring(start, end);
const after = text.substring(end);
this.value = before + "<u>" + selected + "</u>" + after;
}
}
});
function renderNotes() {
kolom.innerHTML = "";
notes.forEach((data) => {
const div = document.createElement("div");
div.className = "N-kotak";
div.style.backgroundColor = data.color;
if (data.color == "black" || "#000000") {
div.style.color = "white";
}
div.innerHTML = `</i></i><h2 class="N-judul">${data.judul}</h2>
<img class='N-gambar' src="${data.image}" alt="">
<p class="N-isi">${data.isi}</p>
<div class="double-button">
<button data-id="${data.id}" class='edit'>edit</button>
<button data-id="${data.id}" class="remove">remove</button>
</div>`;
if (data.image == "") {
div.querySelector(".N-gambar").remove();
}
kolom.appendChild(div);
});
}
saveNotes();
renderNotes();
//step 3
buttonSubmit.addEventListener("click", () => {
const newJudul = judul.value.trim();
const newIsi = isi.value.trim();
const newFile = file.value.trim();
const newWarna = warna.value;
if (newJudul !== "") {
const newNotes = {
id: Date.now(),
color: newWarna,
judul: newJudul,
image: newFile,
isi: newIsi,
};
notes.push(newNotes);
judul.value = "";
isi.value = "";
file.value = "";
saveNotes();
renderNotes();
}
});
//step 4
kolom.addEventListener("click", (event) => {
const target = event.target;
//edit button
if (target.classList.contains("edit")) {
const idEdit = parseInt(target.getAttribute("data-id"));
const idFind = notes.find((notes) => notes.id === idEdit);
if (idFind.id == idEdit) {
judul.value = idFind.judul;
warna.value = idFind.color;
isi.value = idFind.isi;
file.value = idFind.image;
}
notes = notes.filter((notes) => notes.id !== idEdit);
saveNotes();
renderNotes();
}
//remove button
if (target.classList.contains("remove")) {
const idRemove = parseInt(target.getAttribute("data-id"));
notes = notes.filter((notes) => notes.id !== idRemove);
saveNotes();
renderNotes();
}
});