-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
373 lines (329 loc) · 11.5 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const notepad = document.getElementById('notepad');
const body = document.body;
const preferencesModal = document.getElementById('preferencesModal');
const darkModeToggle = document.getElementById('darkModeBtn');
const textLinesToggle = document.getElementById('textLinesToggle');
const autoSaveToggle = document.getElementById('autoSaveToggle');
const fontSizeSelect = document.getElementById('fontSizeSelect');
const fontFamilySelect = document.getElementById('fontFamilySelect');
const fontWeightSelect = document.getElementById('fontWeightSelect');
const downloadModal = document.getElementById('downloadModal');
const fileNameInput = document.getElementById('fileName');
const fileFormatSelect = document.getElementById('fileFormat');
const replaceModal = document.getElementById('replaceModal');
const scrollToTopBtn = document.getElementById('scrollToTopBtn');
// Auto-save functionality
function autoSave() {
localStorage.setItem('notepadContent', notepad.value);
}
// Load saved content and preferences from local storage on page load
window.onload = function() {
if (localStorage.getItem('notepadContent')) {
notepad.value = localStorage.getItem('notepadContent');
}
if (localStorage.getItem('darkMode') === 'enabled') {
body.classList.add('dark-mode');
notepad.classList.add('dark-mode');
darkModeToggle.innerHTML = '<i class="fas fa-adjust"></i> Light Mode';
darkModeToggle.style.backgroundColor = 'white';
darkModeToggle.style.color = 'black';
}
if (localStorage.getItem('textLines') === 'hidden') {
notepad.classList.add('no-lines');
textLinesToggle.checked = false;
} else {
textLinesToggle.checked = true;
}
if (localStorage.getItem('autoSave') === 'enabled' || autoSaveToggle.checked) {
autoSaveToggle.checked = true;
notepad.addEventListener('input', autoSave);
}
if (localStorage.getItem('fontSize')) {
notepad.style.fontSize = localStorage.getItem('fontSize');
notepad.style.lineHeight = parseInt(localStorage.getItem('fontSize')) * 1.5 + 'px';
fontSizeSelect.value = localStorage.getItem('fontSize');
}
if (localStorage.getItem('fontFamily')) {
notepad.style.fontFamily = localStorage.getItem('fontFamily');
fontFamilySelect.value = localStorage.getItem('fontFamily');
}
if (localStorage.getItem('fontWeight')) {
notepad.style.fontWeight = localStorage.getItem('fontWeight');
fontWeightSelect.value = localStorage.getItem('fontWeight');
}
};
// Scroll to top functionality
window.onscroll = function() {
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
scrollToTopBtn.style.display = "block";
} else {
scrollToTopBtn.style.display = "none";
}
};
function scrollToTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
// Event listeners for auto-save
if (autoSaveToggle.checked) {
notepad.addEventListener('input', autoSave);
} else {
notepad.removeEventListener('input', autoSave);
}
// Cut, copy, paste, and replace functionality
notepad.addEventListener('cut', autoSave);
notepad.addEventListener('copy', autoSave);
notepad.addEventListener('paste', autoSave);
// Show preferences modal
function showPreferences() {
preferencesModal.style.display = 'block';
}
// Close preferences modal
function closePreferences() {
preferencesModal.style.display = 'none';
}
// Dark mode toggle
darkModeToggle.addEventListener('click', function() {
body.classList.toggle('dark-mode');
notepad.classList.toggle('dark-mode');
if (body.classList.contains('dark-mode')) {
localStorage.setItem('darkMode', 'enabled');
darkModeToggle.innerHTML = '<i class="fas fa-adjust"></i> Light Mode';
darkModeToggle.style.backgroundColor = 'white';
darkModeToggle.style.color = 'black';
} else {
localStorage.removeItem('darkMode');
darkModeToggle.innerHTML = '<i class="fas fa-adjust"></i> Dark Mode';
darkModeToggle.style.backgroundColor = 'black';
darkModeToggle.style.color = 'white';
}
});
// Text lines toggle
textLinesToggle.addEventListener('change', function() {
notepad.classList.toggle('no-lines');
if (textLinesToggle.checked) {
localStorage.removeItem('textLines');
} else {
localStorage.setItem('textLines', 'hidden');
}
});
// Auto save toggle
autoSaveToggle.addEventListener('change', function() {
if (autoSaveToggle.checked) {
localStorage.setItem('autoSave', 'enabled');
notepad.addEventListener('input', autoSave);
} else {
localStorage.removeItem('autoSave');
notepad.removeEventListener('input', autoSave);
}
});
// Font size change
fontSizeSelect.addEventListener('change', function() {
notepad.style.fontSize = fontSizeSelect.value;
notepad.style.lineHeight = parseInt(fontSizeSelect.value) * 1.5 + 'px';
localStorage.setItem('fontSize', fontSizeSelect.value);
});
// Font family change
fontFamilySelect.addEventListener('change', function() {
notepad.style.fontFamily = fontFamilySelect.value;
localStorage.setItem('fontFamily', fontFamilySelect.value);
});
// Font weight change
fontWeightSelect.addEventListener('change', function() {
notepad.style.fontWeight = fontWeightSelect.value;
localStorage.setItem('fontWeight', fontWeightSelect.value);
});
// Cut text function
function cutText() {
document.execCommand('cut');
autoSave();
}
// Copy text function
function copyText() {
document.execCommand('copy');
}
// Paste text function
function pasteText() {
navigator.clipboard.readText().then(text => {
const start = notepad.selectionStart;
const end = notepad.selectionEnd;
notepad.value = notepad.value.substring(0, start) + text + notepad.value.substring(end);
notepad.selectionStart = notepad.selectionEnd = start + text.length;
autoSave();
});
}
// Show replace popup
function showReplacePopup() {
replaceModal.style.display = 'block';
}
// Close replace popup
function closeReplacePopup() {
replaceModal.style.display = 'none';
}
// Replace text function
function replaceText() {
const replaceText = document.getElementById('replaceText').value;
const start = notepad.selectionStart;
const end = notepad.selectionEnd;
notepad.value = notepad.value.substring(0, start) + replaceText + notepad.value.substring(end);
notepad.selectionStart = notepad.selectionEnd = start + replaceText.length;
autoSave();
closeReplacePopup();
}
// Select all text function
function selectAllText() {
notepad.select();
}
// Delete all text function
function deleteAllText() {
notepad.value = '';
autoSave();
}
// Print text function
function printText() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
const margin = 10;
const pageHeight = doc.internal.pageSize.height;
const textLines = doc.splitTextToSize(notepad.value, doc.internal.pageSize.width - margin * 2);
let y = margin;
textLines.forEach(line => {
if (y + 10 > pageHeight - margin) { // Check if we need to add a new page
doc.addPage();
y = margin;
}
doc.text(line, margin, y);
y += 10;
});
doc.text(margin, pageHeight - margin, "This File was created on online notepad by bkpkvideo.com", {
fontSize: 8,
color: 'rgba(0, 0, 0, 0.5)',
});
doc.output('dataurlnewwindow');
}
// Toggle share menu
function toggleShareMenu() {
const shareMenu = document.getElementById('shareMenu');
shareMenu.classList.toggle('show');
}
// Share text function
function shareText(platform) {
let shareUrl;
const text = encodeURIComponent(notepad.value);
const url = encodeURIComponent(window.location.href);
switch (platform) {
case 'facebook':
shareUrl = `https://www.facebook.com/sharer/sharer.php?u=${url}"e=${text}`;
break;
case 'email':
shareUrl = `mailto:?subject=Online Notepad&body=${text}`;
break;
case 'whatsapp':
shareUrl = `https://api.whatsapp.com/send?text=${text}`;
break;
case 'twitter':
shareUrl = `https://twitter.com/intent/tweet?text=${text}&url=${url}`;
break;
case 'linkedin':
shareUrl = `https://www.linkedin.com/shareArticle?mini=true&url=${url}&title=Online Notepad&summary=${text}`;
break;
}
window.open(shareUrl, '_blank');
}
// Zoom in function
function zoomIn() {
const currentFontSize = parseFloat(window.getComputedStyle(notepad, null).getPropertyValue('font-size'));
notepad.style.fontSize = (currentFontSize + 2) + 'px';
notepad.style.lineHeight = (currentFontSize + 2) * 1.5 + 'px';
}
function zoomOut() {
const currentFontSize = parseFloat(window.getComputedStyle(notepad, null).getPropertyValue('font-size'));
if (currentFontSize > 8) {
notepad.style.fontSize = (currentFontSize - 2) + 'px';
notepad.style.lineHeight = (currentFontSize - 2) * 1.5 + 'px';
}
}
// Undo function
function undoText() {
document.execCommand('undo');
}
// Redo function
function redoText() {
document.execCommand('redo');
}
// Show download popup
function showDownloadPopup(format) {
fileFormatSelect.value = format;
downloadModal.style.display = 'block';
}
// Close download popup
function closeDownloadPopup() {
downloadModal.style.display = 'none';
}
// Download file function
function downloadFile() {
const fileName = fileNameInput.value;
const fileFormat = fileFormatSelect.value;
const content = notepad.value;
let blob;
let mimeType;
switch (fileFormat) {
case 'text':
mimeType = 'text/plain';
blob = new Blob([content], { type: mimeType });
break;
case 'pdf':
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
const margin = 10;
const pageHeight = doc.internal.pageSize.height;
const textLines = doc.splitTextToSize(content, doc.internal.pageSize.width - margin * 2);
let y = margin;
textLines.forEach(line => {
if (y + 10 > pageHeight - margin) { // Check if we need to add a new page
doc.addPage();
y = margin;
}
doc.text(line, margin, y);
y += 10;
});
doc.text(margin, pageHeight - margin, "This File was created on online notepad by bkpkvideo.com", {
fontSize: 8,
color: 'rgba(0, 0, 0, 0.5)',
});
doc.save(`${fileName}.pdf`);
return;
case 'css':
mimeType = 'text/css';
blob = new Blob([content], { type: mimeType });
break;
case 'html':
mimeType = 'text/html';
blob = new Blob([content], { type: mimeType });
break;
case 'javascript':
mimeType = 'application/javascript';
blob = new Blob([content], { type: mimeType });
break;
case 'php':
mimeType = 'application/x-httpd-php';
blob = new Blob([content], { type: mimeType });
break;
case 'word':
mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
const link = document.createElement('a');
link.href = `data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,${btoa(content)}`;
link.download = `${fileName}.docx`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
return;
}
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `${fileName}.${fileFormat}`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
closeDownloadPopup();
}