-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
151 lines (133 loc) · 4.16 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
let size = 20;
let p = document.querySelector('#area');
let div = document.querySelector('div');
let span = document.querySelector('span');
let clearBtn = document.querySelector('#clear');
let genBtn = document.querySelector('#generate');
let saveBtn = document.querySelector('#save');
let sortBtn = document.querySelector('#sort');
let select = document.querySelector('select');
let monoBtn = document.querySelector('#mono');
let aside = document.querySelector('aside');
let monospaceMode = false;
span.innerText = size;
p.style.fontSize = size + 'px';
div.style.fontSize = size + 'px';
let wordsSet = [];
function clear() {
div.innerText = ""
}
clearBtn.addEventListener('click', clear);
function resizeText(flag) {
if (flag) {
++size;
}
else {
--size;
}
span.innerText = size;
p.style.fontSize = size + 'px';
div.style.fontSize = size + 'px';
}
function generate() {
div.innerText = "";
let res = p.innerText.toLowerCase().match(/[a-z]+-?[a-z]+/gi);
wordsSet = [...new Set(res)]
printList();
}
genBtn.addEventListener('click', generate)
document.getElementById('inputfile').addEventListener('change', function () {
div.innerText = "";
p.innerText = "";
let reader = new FileReader();
reader.onload = () => {
let data = reader.result;
wordsSet = [...new Set(data.toLowerCase().match(/[a-z]+-?[a-z]+/gi))]
printList();
}
reader.readAsText(this.files[0]);
})
function printList() {
if(wordsSet.length<1) return;
wordsSet.forEach(el => {
let w = document.createElement('span');
w.innerText = el;
div.appendChild(w);
div.appendChild(document.createElement('br'))
})
aside.innerText = wordsSet.length + ' words';
}
function save() {
if (div.innerHTML.includes('span')) {
var blob = new Blob([div.innerText], { type: "text/plain;charset=utf-8" });
saveAs(blob, "list.txt");
}
else {
div.innerText = 'First create a list';
}
}
saveBtn.addEventListener('click', save);
function sort() {
div.innerHTML = "";
if(select.value==1) {
wordsSet.sort();
}
else if(select.value==2) {
wordsSet.sort().reverse();
}
else if(select.value==3) {
wordsSet.sort();
wordsSet.sort((a,b) => {
if(a.charAt(0) == b.charAt(0) && a.length < b.length) {return -1}
else if(a.charAt(0) == b.charAt(0) && a.length > b.length) {return 1}
else return 0
})
}
else if(select.value==4) {
wordsSet.sort();
wordsSet.sort((a,b) => {
if(a.charAt(0) == b.charAt(0) && a.length < b.length) {return 1}
else if(a.charAt(0) == b.charAt(0) && a.length > b.length) {return -1}
else return 0
})
}
else if(select.value==5) {
wordsSet.sort().reverse();
wordsSet.sort((a,b) => {
if(a.charAt(0) == b.charAt(0) && a.length < b.length) {return -1}
else if(a.charAt(0) == b.charAt(0) && a.length > b.length) {return 1}
else return 0
})
}
else if(select.value==6) {
wordsSet.sort().reverse();
wordsSet.sort((a,b) => {
if(a.charAt(0) == b.charAt(0) && a.length < b.length) {return 1}
else if(a.charAt(0) == b.charAt(0) && a.length > b.length) {return -1}
else return 0
})
}
else if(select.value==7) {
wordsSet.sort((a,b) => a.length - b.length);
}
else if(select.value==8) {
wordsSet.sort((a,b) => a.length - b.length).reverse();
}
else if(select.value==9) {
wordsSet.sort((a,b) => Math.random() > .5)
}
printList();
}
sortBtn.addEventListener('click', sort);
function handleMonospace() {
monospaceMode = !monospaceMode;
if(monospaceMode) {
monoBtn.style.background = 'darkgreen';
div.style.fontFamily = 'monospace';
}
else {
monoBtn.style.background = '';
div.style.fontFamily = '';
}
}
monoBtn.addEventListener('click', handleMonospace)