-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextStorm.js
477 lines (416 loc) · 18 KB
/
TextStorm.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/***************************File*Header**********************************
* File Name: TextStorm.js
*
* JS Description: This JS File is the major JS File, which contains boot
* background processes, major functions, etc...
*
* *** IMPORTANT: THE MOST PRECIOUS FILE FOR TEXTSTORM
****************************************************************************/
console.log("Welcome to TextStorm");
const VERSION = "v.2.4";
// Global Modes
let dark_mode_status_home = false; // Keep Dark Mode Off in Desktop (Default)
let dark_mode_status = false; // Keep Dark Mode Off in Editor (Default)
// Document Elements
let body = document.querySelector("body");
let dark_mode_btn_home = document.querySelector(".dark_mode_btn_home");
let dark_mode_btn = document.querySelector(".dark_mode_btn");
let fullscreen_mode_btn = document.querySelector(".fullscreen_mode_btn");
//Audio Load
let startup_audio = new Audio("audio/startup.mp3");
let success_audio = new Audio("audio/success.mp3");
let error_audio = new Audio("audio/error.mp3");
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
alert("We are currently working on integrating TextStorm for tablet & mobile devices. The update will be releasing soon.. STAY TUNED !!");
location.replace("https://dwijottam-dutta.github.io/portfolio#textstorm");
}
function TEXTSTORM_STARTUP() {
document.getElementById("editor").style.display = "none"; // CSS with JS
return new Promise(function (resolve) {
document.getElementById("STARTUP_SCREEN_PARENT").style.display = "none";
setTimeout(() => {
document.getElementById("STARTUP_SCREEN_PARENT").style.display = "flex";
setTimeout(() => {
document.getElementById("welcome_loader").style.visibility = "visible";
resolve();
}, 1500);
}, 1000);
});
}
function SETTING() {
return new Promise(function (resolve) {
// Changing DESKTOP Mode in last user preference Mode
let mode = localStorage.getItem("TextStorm_Mode");
if (mode == "$dark") {
dark_mode_status_home = true;
body.classList.toggle('dark_mode_active_home');
dark_mode_btn_home.innerHTML = '<i class="far fa-sun"></i>';
} else if (mode == "$light") {
dark_mode_status_home = false;
body.classList.remove('dark_mode_active_home');
dark_mode_btn_home.innerHTML = '<i class="far fa-moon"></i>';
}
// Changing EDITOR Mode in last user preference Mode
let editor_mode = localStorage.getItem("TextStorm_Editor_Mode");
if (editor_mode == "$dark") {
dark_mode_btn.innerHTML = '<i class="far fa-sun"></i>';
dark_mode_status = true;
document.getElementById("editor").classList.toggle("dark_mode_active");
} else if (editor_mode == "$light") {
dark_mode_btn.innerHTML = '<i class="far fa-moon"></i>';
dark_mode_status = false;
document.getElementById("editor").classList.remove("dark_mode_active");
}
// Changing EDITOR_FONT in last user preference Mode
let editor_font = localStorage.getItem("TextStorm_Font_Preferences");
try { // Try that the preference actually exists or not
FONTS_OBJECTS[editor_font]();
} catch (error) {
FONTS_OBJECTS["Questrial"](); // If no preference or corrupted preference then shift to default
}
setTimeout(() => {
document.getElementById("STARTUP_SCREEN").style.background = "transparent";
setTimeout(() => {
document.getElementById("welcome_loader").style.visibility = "hidden";
resolve();
}, 1000);
}, 6000);
});
}
var user_first_name;
function START() {
return new Promise(function (resolve_start) {
firebase.auth().onIdTokenChanged((user) => {
if (!user) {
document.getElementById("hide_while_other").style.visibility = "hidden";
document.getElementById("user_welcome_button").style.visibility = "hidden";
document.getElementById("STARTUP_SCREEN").style.display = "none";
document.getElementById("editor").style.display = "none";
document.getElementById("signup_wrapper").style.display = "block"; // SignUp Window would appear
localStorage.setItem("TextStorm_User", "Guest");
} else {
var head_ID = document.getElementsByTagName("head")[0];
var script_element = document.createElement('script');
script_element.type = 'text/javascript';
script_element.id = 'my-test-script';
script_element.src = 'Js/TextStorm_main.js';
// Create script element
var script_element_two = document.createElement('script');
script_element_two.type = 'text/javascript';
script_element_two.id = 'my-test-script-two';
script_element_two.src = 'Js/TextStorm_window.js';
head_ID.appendChild(script_element_two);
head_ID.appendChild(script_element);
const database_ref = firebase.database();
database_ref.ref("Users_Data/" + user.uid).on("value", function (snapshot) {
let user_name = snapshot.val().User; //Get user name
let user_email = snapshot.val().Email; //Get user email
let user_filename = snapshot.val().File_Data; // Get user file name
let user_data = snapshot.val().Data; //Get user text/notes data
let split_username = user_name.split(" "); // Splitting user name
user_first_name = split_username[0]; // Getting first name by removing last name
localStorage.setItem("TextStorm_User", user_first_name);
document.getElementById("fileid").innerHTML = user_filename; //Changing file name
document.title = "TextStorm | " + user_filename; //Changing website tab title
document.getElementById("apnatext").value = user_data; //Changing text/notes data
document.getElementById("user_name_account").innerHTML = user_name;
document.getElementById("user_email_account").innerHTML = user_email;
document.getElementById("STARTUP_SCREEN").style.display = "none";
// ADD SHORTCUTS
window.addEventListener('keydown', e => {
if (e.ctrlKey && e.keyCode == 'S'.charCodeAt(0)) {
e.preventDefault();
saveText();
}
if (e.ctrlKey && e.keyCode == 'O'.charCodeAt(0)) {
e.preventDefault();
openFile();
}
if (e.altKey && e.keyCode == 'C'.charCodeAt(0)) {
myCopy();
}
if (e.altKey && e.keyCode == 'D'.charCodeAt(0)) {
e.preventDefault();
downloadText();
}
if (e.altKey && e.keyCode == 'N'.charCodeAt(0)) {
e.preventDefault();
newFile();
}
});
document.getElementById("editor").style.display = "flex";
resolve_start();
});
}
});
});
}
function STABILITY() {
return new Promise(function (resolve) {
firebase.auth().onIdTokenChanged((user) => {
if (user) {
if (window.File && window.Blob && window.showOpenFilePicker) {
console.log("TEXTSTORM IS STABLE IN YOUR BROWSER AS YOU ARE USING CHROME");
} else {
const bypass_id = localStorage.getItem("$bypass_id");
if (bypass_id != "crack" || bypass_id == null) {
error_audio.play();
OPEN_WINDOW("in_error_id");
console.log("TEXTSTORM IS LITTLE UNSTABLE IN YOUR BROWSER AS YOU ARE USING FIREFOX/SAFARI/INTERNET EXPLORER");
}
}
setInterval(() => {
if (document.body.clientWidth < 620 || document.body.clientHeight < 510) {
OPEN_WINDOW("error_id");
document.getElementById("navbar").style.display = "none";
} else {
CLOSE_WINDOW("error_id");
document.getElementById("navbar").style.display = "flex";
}
}, 1000);
}
});
resolve();
});
}
function STARTUP_NOTIFICATION() {
return new Promise(function (resolve) {
firebase.auth().onIdTokenChanged((user) => {
if (user) {
let body_id;
let body_id_final;
let title_id;
let confetti;
let startup_notification_time;
const database = firebase.database();
database.ref("Notification").once('value', function (snapshot) {
body_id = snapshot.val().body;
title_id = snapshot.val().title;
confetti = snapshot.val().confetti;
startup_notification_time = snapshot.val().time;
if (confetti == true) {
startit();
stopit();
}
setTimeout(function () {
let USER_NAME = localStorage.getItem("TextStorm_User");
if (body_id.includes("$USER_NAME")) {
body_id_final = body_id.replace("$USER_NAME", USER_NAME);
} else {
body_id_final = body_id;
}
TEXTSTORM_NOTIFICATION_SHOW(null, title_id, body_id_final, startup_notification_time, "startup");
resolve();
}, 3000);
});
}
});
});
}
// $BOOT
TEXTSTORM_STARTUP().then(SETTING).then(START).then(STABILITY).then(STARTUP_NOTIFICATION);
// Confetti Functions initialize !
const startit = () => {
setTimeout(function () {
confetti.start();
}, 1000);
};
const stopit = () => {
setTimeout(function () {
confetti.stop();
}, 3000);
};
// Keeping Limit Function in boot as it would be required while boot
function limitText(id, limitNum) {
if (id.value.length > limitNum) {
id.value = id.value.substring(0, limitNum);
}
}
// $ROOT FUNCTIONS
// Close Function
function TEXTSTORM_CLOSE() {
if (window.navigator.onLine) {
firebase.auth().onIdTokenChanged((user) => {
if (user) {
let filecontent = document.getElementById("fileid").innerHTML;
let content = document.getElementById("apnatext").value;
let user_details_updating = {
Data: content,
File_Data: filecontent
};
firebase
.database()
.ref("Users_Data/" + user.uid)
.update(user_details_updating)
.catch((error) => {
console.log("Something went wrong");
});
}
});
setTimeout(() => {
document.getElementById("STARTUP_SCREEN").style.display = "block";
setTimeout(() => {
document.getElementById("STARTUP_SCREEN").style.background = "rgb(17, 17, 17)";
setTimeout(() => {
document.getElementById("welcome_loader").style.visibility = "visible";
document.getElementById("welcome_loader").innerHTML = "Shutting Down";
setTimeout(() => {
window.close();
}, 7000);
}, 3000);
}, 200);
}, 1000);
}
else {
TEXTSTORM_NOTIFICATION_SHOW(null, "TextStorm Shutdown", "Can't shutdown securely by saving preferences, please have a stable connection", 10000, "error");
}
};
// Restart Function
function TEXTSTORM_RESTART() {
if (window.navigator.onLine) {
firebase.auth().onIdTokenChanged((user) => {
if (user) {
let filecontent = document.getElementById("fileid").innerHTML;
let content = document.getElementById("apnatext").value;
let user_details_updating = {
Data: content,
File_Data: filecontent
};
firebase
.database()
.ref("Users_Data/" + user.uid)
.update(user_details_updating)
.catch((error) => {
console.log("Something went wrong");
});
}
});
setTimeout(() => {
document.getElementById("STARTUP_SCREEN").style.display = "block";
setTimeout(() => {
document.getElementById("STARTUP_SCREEN").style.background = "rgb(17, 17, 17)";
setTimeout(() => {
document.getElementById("welcome_loader").style.visibility = "visible";
document.getElementById("welcome_loader").innerHTML = "Restarting";
setTimeout(() => {
location.reload();
}, 7000);
}, 3000);
}, 200);
}, 1000);
}
else {
TEXTSTORM_NOTIFICATION_SHOW(null, "TextStorm Reboot", "Can't restart, please have a stable connection while restarting TextStorm...", 10000, "error")
}
}
// $MAIN FUNCTIONS
// Navigation Bar FullScreen Button Function
let myDocument = document.documentElement;
let full_screen_mode = false;
fullscreen_mode_btn.addEventListener("click", () => {
if (full_screen_mode == false) {
if (myDocument.requestFullscreen) {
myDocument.requestFullscreen();
} else if (myDocument.msRequestFullscreen) {
myDocument.msRequestFullscreen();
} else if (myDocument.mozRequestFullScreen) {
myDocument.mozRequestFullScreen();
} else if (myDocument.webkitRequestFullscreen) {
myDocument.webkitRequestFullscreen();
}
fullscreen_mode_btn.innerHTML = '<i class="fas fa-compress"></i>';
full_screen_mode = true;
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msexitFullscreen) {
document.msexitFullscreen();
} else if (document.mozexitFullscreen) {
document.mozexitFullscreen();
} else if (document.webkitexitFullscreen) {
document.webkitexitFullscreen();
}
fullscreen_mode_btn.innerHTML = '<i class="fas fa-expand"></i>';
full_screen_mode = false;
}
});
// Navigation Bar Dark Mode Button Function
dark_mode_btn_home.addEventListener("click", function () {
body.classList.toggle("dark_mode_active_home");
if (dark_mode_status_home == false) {
dark_mode_btn_home.innerHTML =
'<i class="far fa-sun"></i>';
dark_mode_status_home = true;
localStorage.setItem("TextStorm_Mode", "$dark");
} else {
dark_mode_btn_home.innerHTML =
'<i class="far fa-moon"></i>';
dark_mode_status_home = false;
localStorage.setItem("TextStorm_Mode", "$light");
}
});
// $BACKGROUND PROCESSES
// Time Function
setInterval(showTime, 1000);
if (navigator.getBattery) {
setInterval(showBattery, 1000);
}
else {
document.getElementById("battery_indicator_button").style.display = "none";
document.getElementById("battery_level_indication_int").style.display = "none";
}
function showTime() {
let var_date = new Date();
let hour = var_date.getHours();
let min = var_date.getMinutes();
am_pm = "AM";
if (hour > 12) {
hour -= 12;
am_pm = "PM";
}
if (hour == 0) {
hr = 12;
am_pm = "AM";
}
const weekday = new Array(7);
weekday[0] = "Sun";
weekday[1] = "Mon";
weekday[2] = "Tue";
weekday[3] = "Wed";
weekday[4] = "Thu";
weekday[5] = "Fri";
weekday[6] = "Sat";
let current_day = weekday[var_date.getDay()];
hour = hour < 10 ? "0" + hour : hour;
min = min < 10 ? "0" + min : min;
let currentTime = hour + ":" +
min + " " + am_pm;
document.getElementById("clock")
.innerHTML = `<b>` + current_day + `</b>` + " " + currentTime;
}
showTime();
function showBattery() {
navigator.getBattery()
.then(function (battery) {
// Icon Battery Indicator Conditions
let battery_ico = document.getElementById("battery_indicator_button");
if (battery.level > 0.94) {
battery_ico.innerHTML = `<i class="fas fa-battery-full"></i>`;
}
else if (battery.level < 0.96 && battery.level > 0.69) {
battery_ico.innerHTML = `<i class="fas fa-battery-three-quarters"></i>`;
}
else if (battery.level < 0.71 && battery.level > 0.29) {
battery_ico.innerHTML = `<i class="fas fa-battery-half"></i>`;
}
else if (battery.level < 0.31 && battery.level > 0.09) {
battery_ico.innerHTML = `<i class="fas fa-battery-quarter"></i>`;
}
else if (battery.level < 0.11) {
battery_ico.innerHTML = `<i class="fas fa-battery-empty"></i>`;
}
// Numerical Battery Indicator Conditions
let battery_exclude_decimal = (battery.level * 100).toString();
document.getElementById("battery_level_indication_int").innerHTML = `${Math.round(battery_exclude_decimal)}%`;
});
}