-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
249 lines (209 loc) · 7.2 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
// UTILITY CODE -->
function refreshPage() {
window.location.href = window.location.href;
}
function copyText(text) {
navigator.clipboard.writeText(text);
}
function notify(message, type, context) {
let notificationDiv;
if (
document.getElementById("notification-area") === undefined ||
document.getElementById("notification-area") === null
) {
notificationDiv = document.createElement("div");
notificationDiv.id = "notification-area";
document.body.append(notificationDiv);
} else {
notificationDiv = document.getElementById("notification-area");
}
let createdDiv = document.createElement("div");
let id = Math.random().toString(36).substring(2, 10);
createdDiv.setAttribute("id", id);
createdDiv.classList.add("notification", type);
if (context) {
createdDiv.classList.add("contextMenu");
}
createdDiv.innerHTML = `
<div class="cross">
<span class="cross__spans span1"></span>
<span class="cross__spans span2""></span>
</div>
${message}
<span class="notif notif-timer"></span>`;
document.getElementById("notification-area").prepend(createdDiv);
setTimeout(() => {
let notifications = document
.getElementById("notification-area")
.getElementsByClassName("notification");
for (let i = 0; i < notifications.length; i++) {
if (notifications[i].getAttribute("id") == id) {
notifications[i].remove();
break;
}
}
}, 5000);
const notifCrosses = document.querySelectorAll(".notification .cross");
notifCrosses.forEach((e) => {
e.addEventListener("click", () => {
e.parentElement.remove();
});
});
}
// THE MAIN CODE ->
const addCodeBtn = document.querySelector(".add-code");
const mainContainer = document.querySelector(".code-container");
let allCodeDivs = document.querySelectorAll(".code-div");
let deleteBtns = document.querySelectorAll(".btn-delete");
let copyBtns = document.querySelectorAll(".btn-copy");
let toggleEditBtns = document.querySelectorAll(".btn-toggle-edit");
let allSelectLangs = document.querySelectorAll(".select-lang");
let codeTitles = document.querySelectorAll(".code-title");
let textAreas = document.querySelectorAll(".code-area");
let editAreas = document.querySelectorAll(".edit-area");
const savedCodes = JSON.parse(localStorage.getItem("codes"));
let locallySavedCodes = savedCodes ?? [];
if (savedCodes) {
savedCodes.forEach((codeObject) => {
const createdEl = document.createElement("div");
createdEl.classList.add("code-div");
createdEl.innerHTML = `
<input type="text" class="code-title" placeholder="untitled" value="${codeObject.title
}" maxlength="35">
<select class="select-lang">
<option value="html">HTML</option>
<option value="xml">XML</option>
<option value="javascript">Javascript</option>
<option value="css">CSS</option>
<option value="go">GO</option>
<option value="python">Python</option>
<option value="bash">BASH</option>
<option value="java">Java</option>
<option value="json">JSON</option>
<option value="typescript">Typescript</option>
<option value="c">C</option>
<option value="csharp">C#</option>
<option value="cpp">C++</option>
<option value="swift">Swift</option>
<option value="lua">Lua</option>
<option value="d">D</option>
<option value="php">PHP</option>
<option value="git">Git</option>
<option value="r">R</option>
<option value="ruby">Ruby</option>
<option value="yaml">YAML</option>
<option value="none">Select language</option>
</select>
<div class="btn-group">
<button class="btn btn-toggle-edit custom">
<i class="bi-pencil-square"></i>
</button>
<button class="btn btn-delete custom">
<i class="bi-trash"></i>
</button>
<button class="btn btn-copy custom">
<i class="bi-clipboard"></i>
</button>
</div>
<textarea class="area edit-area hide" spellcheck="false" placeholder="edit your code...">${codeObject.code
}</textarea>
<pre class="language-${codeObject.lang} area code-area">${codeObject.code
.replaceAll("\n", "\n")
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")}</pre>
`;
mainContainer.append(createdEl);
allCodeDivs = document.querySelectorAll(".code-div");
deleteBtns = document.querySelectorAll(".btn-delete");
copyBtns = document.querySelectorAll(".btn-copy");
toggleEditBtns = document.querySelectorAll(".btn-toggle-edit");
allSelectLangs = document.querySelectorAll(".select-lang");
codeTitles = document.querySelectorAll(".code-title");
textAreas = document.querySelectorAll(".code-area");
editAreas = document.querySelectorAll(".edit-area");
});
}
codeTitles.forEach((title, index) => {
title.addEventListener("input", (e) => {
locallySavedCodes[index].title = e.target.value;
localStorage.setItem("codes", JSON.stringify(locallySavedCodes));
});
});
toggleEditBtns.forEach((btn, index) => {
btn.addEventListener("click", () => {
locallySavedCodes[index].code = editAreas[index].value;
textAreas[index].textContent = editAreas[index].value;
localStorage.setItem("codes", JSON.stringify(locallySavedCodes));
if (btn.querySelector("i").classList.contains("bi-check-lg")) {
refreshPage();
}
btn.querySelector("i").classList.toggle("bi-check-lg");
textAreas[index].classList.toggle("hide");
editAreas[index].classList.toggle("hide");
});
});
copyBtns.forEach((btn, index) => {
btn.addEventListener("click", () => {
copyText(textAreas[index].textContent);
notify(
`Copied code of <bold>${locallySavedCodes[index].title || "untitled"
}</bold>`,
"info"
);
});
});
deleteBtns.forEach((btn, index) => {
btn.addEventListener("click", () => {
allCodeDivs[index].remove();
allCodeDivs = document.querySelectorAll(".code-div");
locallySavedCodes = [];
allCodeDivs.forEach((div) => {
const divTitle = div.querySelector(".code-title").value;
const divCode = div.querySelector(".code-area").textContent;
const lang =
div.querySelector(".select-lang")[
div.querySelector(".select-lang").selectedIndex
].value;
const newData = {
title: divTitle,
code: divCode,
lang: lang,
};
locallySavedCodes.push(newData);
});
localStorage.setItem("codes", JSON.stringify(locallySavedCodes));
refreshPage();
});
});
allSelectLangs.forEach((select, index) => {
select.querySelectorAll("option").forEach((option, i) => {
if (option.value == locallySavedCodes[index].lang) {
option.selected = true;
}
});
select.addEventListener("change", () => {
locallySavedCodes[index].lang = select[select.selectedIndex].value;
localStorage.setItem("codes", JSON.stringify(locallySavedCodes));
refreshPage();
});
});
addCodeBtn.addEventListener("click", () => {
const codeInfo = {
title: "",
code: "",
lang: "none",
};
locallySavedCodes.push(codeInfo);
localStorage.setItem("codes", JSON.stringify(locallySavedCodes));
refreshPage();
});
editAreas.forEach((area) => {
area.addEventListener("keydown", (e) => {
console.log(e.key);
if (e.key === 'Tab'){
e.preventDefault();
area.value += " ";
}
})
})