-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
302 lines (230 loc) · 8.45 KB
/
main.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
// TODO: refactor messaging
// TODO: save with identifier
import alphabet from "./alphabet.js";
const copyButton = document.querySelector(".copy-button");
const saveButton = document.querySelector(".save-button");
const downloadButton = document.querySelector(".download-button");
const textInput = document.querySelector("#text");
const controlsForm = document.querySelector(".controls-form");
const controls = {};
const strings = [];
document.addEventListener("DOMContentLoaded", () => {
checkLocalStorage();
controlsForm.addEventListener("change", handleValueChange);
textInput.addEventListener("input", handleValueChange);
copyButton.addEventListener("click", copyToClipboard);
saveButton.addEventListener("click", saveString);
downloadButton.addEventListener("click", downloadSavedStrings);
});
// TODO: Remove in production
function l(text) {
console.log(text);
}
function setControls(options) {
if (!options) {
controls.algo = document.querySelector('[name="algo"]').value;
controls.direction = document.querySelector('[name="direction"]').checked
? "backward"
: "forward";
controls.trim = document.querySelector('[name="trim"]').checked
? true
: false;
}
}
function createListElement(text) {
const li = document.createElement("li");
li.textContent = text;
li.dataset.text = text;
const copyBtn = document.createElement("button");
copyBtn.setAttribute("type", "button");
copyBtn.className = "saved-item-button";
copyBtn.innerHTML = '<img src="./images/copy.svg" alt="copy" />';
copyBtn.addEventListener("click", (e) => copyToClipboard(e, text));
const deleteBtn = document.createElement("button");
deleteBtn.setAttribute("type", "button");
deleteBtn.className = "saved-item-button";
deleteBtn.innerHTML = '<img src="./images/x.svg" alt="delete" />';
deleteBtn.addEventListener("click", () => deleteString(text));
li.appendChild(copyBtn);
li.appendChild(deleteBtn);
return li;
}
function checkLocalStorage() {
const localItems = JSON.parse(localStorage.getItem("rotside"));
const localStrings = JSON.parse(localStorage.getItem("rotside_strings"));
if (!localItems) {
setControls();
localStorage.setItem("rotside", JSON.stringify(controls));
return;
}
const { algo, direction, trim } = localItems;
document.querySelector('[name="algo"]').value = algo;
document.querySelector(
`[name="direction"][value="${direction}"]`
).checked = true;
document.querySelector(`[name="trim"][value="${trim}"]`).checked = true;
const directionIcon = document.querySelector(".direction-icon");
if (direction === "backward") {
directionIcon.classList.add("backward");
} else {
directionIcon.classList.remove("backward");
}
const stringsListElement = document.querySelector(".strings-list");
if (!localStrings) {
localStorage.setItem("rotside_strings", JSON.stringify([]));
return;
}
strings.push(...localStrings);
const stringsList = document.querySelector(".strings-list ul");
strings.forEach((string) => {
const li = createListElement(string);
stringsList.appendChild(li);
});
if (strings.length > 0) {
stringsListElement.classList.add("visible");
}
}
function showOutputMessage(type) {
const output = document.querySelector(".output");
const className = type == "copied" ? "copied" : "saved";
output.classList.add(className);
setTimeout(() => {
output.classList.remove(className);
}, 4000);
}
function copyToClipboard(e, text = null) {
const output = document.querySelector(".output");
navigator.clipboard.writeText(text || output.textContent);
showOutputMessage("copied");
}
function saveString() {
const text = document.querySelector(".output").textContent;
const strings = JSON.parse(localStorage.getItem("rotside_strings"));
if (strings.length) {
if (strings.some((string) => string === text)) {
return;
}
}
strings.push(text);
localStorage.setItem("rotside_strings", JSON.stringify(strings));
const stringsSection = document.querySelector(".strings-list");
const stringsList = document.querySelector(".strings-list ul");
const li = createListElement(text);
stringsList.appendChild(li);
showOutputMessage("saved");
if (!stringsSection.classList.contains("visible")) {
stringsSection.classList.add("visible");
}
}
function deleteString(str) {
const strings = JSON.parse(localStorage.getItem("rotside_strings"));
const filtered = strings.filter((string) => string !== str);
localStorage.setItem("rotside_strings", JSON.stringify(filtered));
const stringDOMElement = document.querySelector(`[data-text="${str}"]`);
stringDOMElement.parentElement.removeChild(stringDOMElement);
}
function downloadSavedStrings() {
const strings = JSON.parse(localStorage.getItem("rotside_strings")).join(
"\n"
);
const link = document.createElement("a");
link.href = "data:text/plain;charset=utf-8," + encodeURIComponent(strings);
link.download = "rotside.txt";
link.click();
}
function handleValueChange() {
const text = document.querySelector("#text").value;
const output = document.querySelector(".output");
const directionIcon = document.querySelector(".direction-icon");
const algo = document.querySelector('[name="algo"]').value;
const direction = document.querySelector('[name="direction"]').checked
? "backward"
: "forward";
const trim = document.querySelector('[name="trim"]').checked ? true : false;
if (direction === "backward") {
directionIcon.classList.add("backward");
} else {
directionIcon.classList.remove("backward");
}
// Update local items
controls.algo = algo;
controls.direction = direction;
controls.trim = trim;
localStorage.setItem("rotside", JSON.stringify(controls));
const rotated =
direction === "forward"
? rotateStringForwards(text, parseInt(algo.substr(3)), trim)
: rotateStringBackwards(text, parseInt(algo.substr(3)), trim);
if (text) {
copyButton.classList.add("shown");
saveButton.classList.add("shown");
} else {
copyButton.classList.remove("shown");
saveButton.classList.remove("shown");
}
// text
if (rotated) {
output.classList.remove("placeholder");
output.textContent = rotated;
} else {
output.classList.add("placeholder");
output.textContent = "Your output here";
}
}
function rotateStringForwards(text = "", places = 0, trim = false) {
const arr = trim ? text.split(" ").join("").split("") : text.split("");
let output = "";
arr.forEach((letter) => {
const currentIndex = alphabet.indexOf(letter.toLowerCase());
const isLetter = currentIndex !== -1;
const isSpecial = letter.match(/[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/g);
const isSpace = letter === " ";
if (isSpace) return (output += letter);
if (isSpecial) return (output += letter);
// Check if digit is a number
if (!isLetter) {
letter = Number(letter);
const targetNumber = letter + places;
const nextNumber = targetNumber > 9 ? targetNumber - 10 : targetNumber;
output += nextNumber;
} else {
const isUppercase = letter === letter.toUpperCase();
const targetIndex = currentIndex + places;
let nextIndex = targetIndex;
if (targetIndex > alphabet.length - 1)
nextIndex = targetIndex - alphabet.length;
output += isUppercase
? alphabet[nextIndex].toUpperCase()
: alphabet[nextIndex];
}
});
return output;
}
function rotateStringBackwards(text = "", places = 0, trim = false) {
const arr = trim ? text.split(" ").join("").split("") : text.split("");
let output = "";
arr.forEach((letter) => {
const currentIndex = alphabet.indexOf(letter.toLowerCase());
const isLetter = currentIndex !== -1;
const isSpecial = letter.match(/[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/g);
const isSpace = letter === " ";
if (isSpace) return (output += letter);
if (isSpecial) return (output += letter);
// Check if digit is a number
if (!isLetter) {
letter = Number(letter);
const targetNumber = letter - places;
const nextNumber = targetNumber < 0 ? 10 + targetNumber : targetNumber;
output += nextNumber;
} else {
const isUppercase = letter === letter.toUpperCase();
const targetIndex = currentIndex - places;
let nextIndex = targetIndex;
if (targetIndex < 0) nextIndex = alphabet.length + targetIndex;
output += isUppercase
? alphabet[nextIndex].toUpperCase()
: alphabet[nextIndex];
}
});
return output;
}