forked from browserpad/browserpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
96 lines (85 loc) · 3.74 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
var textbox = document.querySelector('#textbox');
var timeoutID = null;
var filenameBox = document.querySelector('#filename');
// Automatically load/save cache in local storage when opening and closing the page
textbox.value = localStorage.getItem('browserpad') || '';
textbox.setSelectionRange(textbox.value.length, textbox.value.length); // Place caret at end of content
calcStats(); // Update counters after loading
function storeLocally() { localStorage.setItem('browserpad', textbox.value); }
window.beforeunload = storeLocally;
// Allow inputting tabs in the textarea instead of changing focus to the next element
textbox.onkeypress = function (event) {
if (event.key === "Tab") {
event.preventDefault();
var text = this.value, s = this.selectionStart, e = this.selectionEnd;
this.value = text.substring(0, s) + '\t' + text.substring(e);
this.selectionStart = this.selectionEnd = s + 1;
}
};
// Auto-save to local storage and calculate stats on every keystroke
textbox.onkeyup = function () {
calcStats();
window.clearTimeout(timeoutID); // Prevent saving too frequently
timeoutID = window.setTimeout(storeLocally, 1000);
};
// Calculate and display character, words and line counts
function calcStats() {
updateCount('char', textbox.value.length);
updateCount('word', textbox.value === "" ? 0 : textbox.value.replace(/\s+/g, ' ').split(' ').length);
updateCount('line', textbox.value === "" ? 0 : textbox.value.split(/\n/).length);
}
function updateCount(item, value) {
document.querySelector('#' + item + '-count').textContent = value;
}
// Save textarea contents as a text file
document.querySelector('#save a').onclick = function () {
this.download = (filenameBox.value || 'browserpad.txt').replace(/^([^.]*)$/, "$1.txt");
this.href = URL.createObjectURL(new Blob([document.querySelector('#textbox').value], { type: 'text/plain' }));
};
// Load contents from a text file
document.querySelector('#open a').onclick = function () {
document.querySelector('#open input').click();
};
document.querySelector('#open input').onchange = function () {
var reader = new FileReader();
reader.file = this.files[0]; // Custom property so the filenameBox can be set from within reader.onload()
reader.onload = function () {
filenameBox.value = this.file.name;
textbox.value = this.result; // this = FileReader object
};
reader.readAsText(this.files[0]); // this = input element
};
//Toggle Fullwidth vs 80 Column
document.querySelector('#fullwidth').onchange = function () {
if (this.checked == true) {
document.getElementById('textbox').style.width = "calc(80ch + 1.5em + " + (textbox.offsetWidth - textbox.clientWidth) + "px)";
} else {
document.getElementById('textbox').style.width = "100%";
}
};
// Toggle spell-checking
document.querySelector('#spellcheck').onchange = function () {
textbox.spellcheck = this.checked;
};
textbox.spellcheck = document.querySelector('#spellcheck').checked; // Initialize
// Print the content
document.querySelector("#print").onclick = function () {
window.print();
};
// Keyboard shortcuts for the save and load functions (`Ctrl+S`, `Ctrl+O`)
document.onkeydown = function (event) {
if (event.ctrlKey) {
if (event.key === "s") {
document.querySelector('#save a').click();
event.preventDefault();
}
else if (event.key === "o") {
document.querySelector('#open input').click();
event.preventDefault();
}
}
}
// Show the about dialog
document.querySelector("#about-icon").onclick = function () {
document.querySelector("#about").showModal();
};