-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
326 lines (258 loc) · 8.54 KB
/
script.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
let allNotes = {};
let selectedKey;
let orderedKeys;
let checkboxInLine = false;
function loadNotes() {
document.getElementById("notes-menu").innerHTML = null;
orderedKeys = Object.keys(localStorage)
.map((key) => parseInt(key.split("paste")[1], 10) || 0)
.sort(function (a, b) {
return a - b;
});
if (orderedKeys.length === 0) {
selectedKey = "paste";
}
if (orderedKeys.length === 1) {
selectedKey = "paste" + (orderedKeys[0] ? orderedKeys[0] : "");
}
if (orderedKeys.length > 1) {
for (let [index, keyIndex] of orderedKeys.entries()) {
const key = "paste" + (keyIndex ? keyIndex : "");
let value = localStorage[key];
document.getElementById("notes-menu").appendChild(createLi(index, key));
allNotes[key] = value;
}
}
var paste = document.getElementById("paste");
paste.scrollTop = 0;
paste.innerHTML = localStorage.getItem(selectedKey);
placeCaretAtEnd(paste);
countWords(paste.innerText);
}
function createLi(index, key) {
var li = document.createElement("li");
li.innerHTML = '<div class="border-item"></div>' + (index + 1) + ". note";
li.setAttribute("data-key", key);
li.addEventListener("click", function () {
changeNote(this.getAttribute("data-key"));
});
var a = document.createElement("a");
a.innerHTML =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M11.9997 10.5865L16.9495 5.63672L18.3637 7.05093L13.4139 12.0007L18.3637 16.9504L16.9495 18.3646L11.9997 13.4149L7.04996 18.3646L5.63574 16.9504L10.5855 12.0007L5.63574 7.05093L7.04996 5.63672L11.9997 10.5865Z"></path></svg>';
a.setAttribute("data-key", key);
a.addEventListener("click", function (event) {
event.stopPropagation();
removeNote(this.getAttribute("data-key"));
});
li.appendChild(a);
if (!selectedKey || selectedKey === key) {
selectedKey = key;
li.classList.add("active");
}
return li;
}
document.addEventListener("DOMContentLoaded", function () {
loadNotes();
if (paste.addEventListener) {
paste.addEventListener("input", function () {
localStorage.setItem(selectedKey, paste.innerHTML);
countWords(paste.innerText);
});
paste.addEventListener("click", function (event) {
deletePopup();
if (event.target.tagName === "A") {
createPopup(event.target.innerText.trim(), event);
}
});
paste.addEventListener("change", function (event) {
if (!event.target.hasAttribute("checked")) {
event.target.setAttribute("checked", "checked");
} else {
event.target.removeAttribute("checked");
}
localStorage.setItem(selectedKey, paste.innerHTML);
});
paste.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
checkboxInLine = false;
const range = window.getSelection().getRangeAt(0);
const currentNode = range.commonAncestorContainer;
let currentLine = currentNode;
while (currentLine && currentLine !== paste) {
var inputs = isElement(currentLine)
? currentLine.getElementsByTagName("input")
: [];
if (inputs.length > 0 ? inputs[0].type === "checkbox" : false) {
checkboxInLine = true;
break;
}
currentLine = currentLine.parentNode;
}
}
});
paste.addEventListener("keyup", function (event) {
if (event.key === "Enter" && checkboxInLine) {
document.execCommand("insertHTML", true, '<input type="checkbox">');
}
});
} else if (paste.attachEvent) {
paste.attachEvent("onpropertychange", function () {
localStorage.setItem(selectedKey, paste.innerHTML);
countWords(paste.innerText);
});
}
document.querySelector("#about-icon").onclick = function () {
document.querySelector("#about").showModal();
};
document.querySelector("#keyboard-icon").onclick = function () {
document.querySelector("#keyboard").showModal();
};
document.getElementById("save-note").onclick = function () {
this.download = "note.txt";
this.href = URL.createObjectURL(
new Blob([document.querySelector("#paste").innerText], {
type: "text/plain",
})
);
};
});
window.onfocus = function () {
var paste = document.getElementById("paste");
paste.innerHTML = localStorage.getItem(selectedKey);
};
function createLink() {
const selection = window.getSelection().toString().trim();
document.execCommand("createLink", false, selection);
}
function deleteLink(element) {
deletePopup();
selectElementContents(element);
document.execCommand("unlink", false, null);
window.getSelection().removeAllRanges();
}
function createPopup(linkText, event) {
var popup = document.createElement("div");
popup.setAttribute("class", "popup");
popup.style.top = event.clientY + 15 + "px";
popup.style.left = event.offsetX + "px";
var link = document.createElement("a");
link.setAttribute("target", "_blank");
link.setAttribute("href", linkText);
link.setAttribute("rel", "noopener noreferrer");
link.innerHTML = linkText;
popup.innerHTML = "Visit URL: ";
popup.appendChild(link);
var removeLink = document.createElement("a");
removeLink.innerHTML = "Remove link";
removeLink.setAttribute("href", "#");
removeLink.onclick = function () {
deleteLink(event.target);
};
var textNode = document.createTextNode(" - ");
popup.appendChild(textNode);
popup.appendChild(removeLink);
document.getElementById("container").appendChild(popup);
}
function deletePopup() {
var popup = document.getElementsByClassName("popup");
if (popup.length > 0) {
popup[0].parentNode.removeChild(popup[0]);
}
}
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/sw.js", { scope: "/" });
}
function changeNote(noteKey) {
var paste = document.getElementById("paste");
paste.innerHTML = localStorage.getItem(noteKey);
countWords(paste.innerText);
document
.querySelector(`li[data-key="${selectedKey}"]`)
.classList.remove("active");
selectedKey = noteKey;
document
.querySelector(`li[data-key="${selectedKey}"]`)
.classList.add("active");
placeCaretAtEnd(paste);
}
function removeNote(noteKey) {
var result = confirm("Are you sure you want to delete this note?");
if (result) {
localStorage.removeItem(noteKey);
if (noteKey === selectedKey) {
orderedKeys.splice(orderedKeys.indexOf(noteKey), 1);
selectedKey = "paste" + (orderedKeys[0] ? orderedKeys[0] : "");
}
loadNotes();
}
}
function newNote() {
const keys = Object.keys(localStorage)
.map((key) => parseInt(key.split("paste")[1], 10) || 0)
.sort(function (a, b) {
return a - b;
});
let lastIndex = keys.slice(-1);
let newKey = "paste" + (parseInt(lastIndex, 10) + 1);
localStorage.setItem(newKey, "");
loadNotes();
changeNote(newKey);
}
function getNextNode(node) {
if (node.firstChild) return node.firstChild;
while (node) {
if (node.nextSibling) return node.nextSibling;
node = node.parentNode;
}
}
function getElement(node) {
return isElement(node) ? node : node.parentElement;
}
function insertCheckbox(element) {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
element.prepend(checkbox);
placeCaretAtEnd(element);
}
function addCheckboxesToSelection() {
const selection = window.getSelection();
const range = selection.getRangeAt(0);
let currentNode = range.startContainer;
let lines = [];
if (range.startContainer === range.endContainer) {
lines.push(getElement(range.startContainer));
}
while (currentNode && currentNode !== range.endContainer) {
const addElement = getElement(currentNode);
if (lines.indexOf(addElement) === -1 && addElement.tagName === "DIV") {
lines.push(addElement);
}
currentNode = getNextNode(currentNode);
}
for (line of lines) {
if (line.getElementsByTagName("input").length > 0) {
line.getElementsByTagName("input")[0].remove();
placeCaretAtEnd(line);
} else {
insertCheckbox(line);
}
}
localStorage.setItem(selectedKey, paste.innerHTML);
}
document.addEventListener("keydown", function (event) {
if (event.ctrlKey && event.key === "n") {
newNote();
}
if (event.ctrlKey && event.key === "l") {
document.execCommand("insertUnorderedList", false, null);
}
if (event.ctrlKey && event.key === "o") {
document.execCommand("insertOrderedList", false, null);
}
if (event.ctrlKey && event.key === "q") {
addCheckboxesToSelection();
}
if (event.ctrlKey && event.key === "w") {
createLink();
}
});