-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
300 lines (258 loc) · 9.14 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
function size() {
var sizetext = document.getElementById("textarea");
var sizeinput = document.getElementById("sizeinput").value;
sizetext.style.fontSize = sizeinput + "px";
}
function name() {
var input = document.getElementById("input").value;
var output = document.getElementById("output");
output.textContent = input;
var namefile = output.textContent;
}
function color() {
var colortext = document.getElementById("textarea");
var colorinput = document.getElementById("colorinput").value;
colortext.style.color = colorinput;
}
function font() {
var fonttext = document.getElementById("textarea");
var fontinput = document.getElementById("fontinput").value;
fonttext.style.fontFamily = fontinput;
}
document.getElementById("sizeinput").onchange = size;
document.getElementById("input").onchange = name;
document.getElementById("colorinput").onchange = color;
document.getElementById("fontinput").onchange = font;
const handleCtrl1 = (e) => {
if (e.ctrlKey && e.keyCode === 49) { // The `Ctrl`+`1` key code is 49
downloadtxt();
}
};
document.addEventListener('keydown', handleCtrl1);
function bold() {
var textarea = document.getElementById("textarea")
textarea.style.fontWeight = "bold"
}
function normal() {
var textarea = document.getElementById("textarea")
var boldbutton = document.getElementById("boldbutton")
if (textarea.style.fontWeight === "bold") {
textarea.style.fontWeight = "400"
boldbutton.textContent = "Bold Font"
} else {
textarea.style.fontWeight = "bold"
boldbutton.textContent = "Normal Font"
}
}
function notunderline() {
var underlinebutton = document.getElementById("underline")
var textarea = document.getElementById("textarea");
if (textarea.style.textDecoration === "underline") {
textarea.style.textDecoration = "none";
underlinebutton.textContent = "Underline"
} else {
textarea.style.textDecoration = "underline";
underlinebutton.textContent = "No Underline"
}
}
function alignleft() {
var textarea = document.getElementById("textarea");
if (textarea.style.textAlign === "left") {
textarea.style.textAlign = "right"
alignbutton.textContent = "Center Align"
} else if (textarea.style.textAlign === "right") {
textarea.style.textAlign = "center"
alignbutton.textContent = "Left Align"
} else {
textarea.style.textAlign = "left"
alignbutton.textContent = "Right Align"
}
}
function aligncenter() {
var textarea = document.getElementById("textarea")
textarea.style.textAlign = "center"
}
function alignright() {
var textarea = document.getElementById("textarea")
textarea.style.textAlign = "right"
}
function newpage() {
page = page + 1
var ppage = document.getElementById("page")
ppage.textContent = "Pages: " + page
const newpage = document.createElement("div");
const center = document.createElement("center")
newpage.id = "textarea"
newpage.contentEditable = "true"
document.body.appendChild(center)
center.appendChild(newpage)
}
function downloadtxt() {
const editableDivs = document.querySelectorAll('div[contentEditable="true"]');
const textValues = [];
editableDivs.forEach(div => {
textValues.push(div.innerHTML);
});
const htmlContent = textValues.join('\n');
const blob = new Blob([htmlContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = document.getElementById("input").value + '.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
function tts() {
const textareas = document.querySelectorAll("div#textarea");
let textToSpeak = '';
textareas.forEach((textarea) => {
textToSpeak += textarea.innerHTML + '\n';
});
const speechSynthesis = window.speechSynthesis;
const utterance = new SpeechSynthesisUtterance(textToSpeak);
const voices = speechSynthesis.getVoices();
utterance.voice = voices[3];
utterance.rate = 0.7;
speechSynthesis.speak(utterance);
}
async function importTxtFile() {
const file = await new Promise((resolve, reject) => {
const input = document.createElement('input');
const textarea = document.getElementById("textarea");
input.type = 'file';
input.accept = '.txt, .png, .jpg, .jpeg, .gif'; // Specify the accepted file types
input.onchange = () => resolve(input.files[0]);
input.style.display = "none";
document.body.appendChild(input);
input.click();
});
if (file.type.startsWith('text/')) {
// Handle text files
const content = await file.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(content, 'text/html');
// Create a div to hold the content
const contentDiv = document.createElement('div');
contentDiv.innerHTML = xmlDoc.body.innerHTML;
textarea.innerHTML = ''; // Clear any existing content
textarea.appendChild(contentDiv);
} else if (file.type.startsWith('image/')) {
// Handle image files
const image = document.createElement('img');
image.src = URL.createObjectURL(file);
textarea.innerHTML = ''; // Clear any existing content
textarea.appendChild(image);
}
}
window.addEventListener('beforeunload', function() {
var textarea = document.getElementById("textarea");
if (textarea.InnerHTML.trim() !== "") {
console.log("Downloading text...");
downloadtxt();
}
});
function cleartextarea() {
var textarea = document.getElementById("textarea")
textarea.innerHTML = ""
}
function lockpage() {
var textarea = document.querySelectorAll("textarea")
var addpage = document.getElementById("addpage")
alert("you can no longer use this unless you download and reimport the file to edit it here again")
addpage.disabled = "true"
textarea.forEach(function(textarea) {
textarea.readOnly = true; // Set the readOnly property to true
});
}
function menu() {
var menuside = document.getElementById("menuside");
if (menuside.style.display === "block" || menuside.style.display === "") {
menuside.style.display = "none";
} else {
menuside.style.display = "block";
}
}
function aboutmenu() {
var versionopen = document.getElementById("versionopen")
if (versionopen.style.display === "block" || versionopen.style.display === "") {
versionopen.style.display = "none";
} else {
versionopen.style.display = "block";
}
}
document.getElementById('highlightbutton').addEventListener('click', function () {
const contentDiv = document.getElementById('textarea');
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const selectedText = range.toString();
if (selectedText !== '') {
const span = document.createElement('span');
span.className = 'highlighted';
span.textContent = selectedText;
range.deleteContents();
range.insertNode(span);
}
});
document.getElementById('underlinebutton').addEventListener('click', function () {
const contentDiv = document.getElementById('textarea');
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const selectedText = range.toString();
if (selectedText !== '') {
const span = document.createElement('span');
span.className = 'underlined';
span.textContent = selectedText;
range.deleteContents();
range.insertNode(span);
}
});
window.addEventListener('beforeunload', function (event) {
var textarea = document.getElementById("textarea")
if (textarea.innerHTML === "") {
} else {
downloadtxt();
}
});
document.getElementById('italicbutton').addEventListener('click', function () {
const contentDiv = document.getElementById('textarea');
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const selectedText = range.toString();
if (selectedText !== '') {
const span = document.createElement('span');
span.className = 'italic';
span.textContent = selectedText;
range.deleteContents();
range.insertNode(span);
}
});
document.getElementById('boldbutton').addEventListener('click', function () {
const contentDiv = document.getElementById('textarea');
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const selectedText = range.toString();
console.log("clicked")
if (selectedText !== '') {
const span = document.createElement('span');
span.className = 'boldstyle';
span.textContent = selectedText;
range.deleteContents();
range.insertNode(span);
}
});
document.getElementById('linkbutton').addEventListener('click', function () {
const contentDiv = document.getElementById('textarea');
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const selectedText = range.toString();
console.log("clicked")
if (selectedText !== '') {
const span = document.createElement('a');
span.className = 'hyperlink';
span.textContent = selectedText;
span.href = selectedText;
range.deleteContents();
range.insertNode(span);
}
});