-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (94 loc) · 3.43 KB
/
Copy pathapp.js
File metadata and controls
99 lines (94 loc) · 3.43 KB
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
window.onload = function () {
// Load the selected file in the DOM
document.getElementById("myBtn").onchange = function (evt) {
f = evt.target.files[0];
var reader = new FileReader();
reader.onload = function (e) {
document.body.innerText = e.target.result;
};
reader.readAsText(f);
};
// Alert when leaving the page
window.onbeforeunload = function () {
return "Did you save your stuff?"
}
var Undo = new Array();
var Redo = new Array();
document.onkeydown = function (event) {
event.preventDefault();
var sel = window.getSelection();
var texto = sel.toString();
if (event.key == 'F11' || event.key == 'F12' || texto.length > 0) {
var tag = null;
switch (event.key) {
// H1
case 'F1':
tag = document.createElement("h1");
var text = document.createTextNode(texto);
tag.appendChild(text);
break;
// H2
case 'F2':
tag = document.createElement("h2");
var text = document.createTextNode(texto);
tag.appendChild(text);
break;
// Paragraph
case 'F3':
tag = document.createElement("p");
var text = document.createTextNode(texto);
tag.appendChild(text);
break;
// List
case 'F4':
tag = document.createElement("ul");
texto.split("\n").filter(Boolean).forEach(function (ele) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(ele))
tag.appendChild(li);
});
break;
// Special case
case 'F5':
tag = document.createElement("div");
tag.style.backgroundColor = "red"
var text = document.createTextNode(texto);
tag.appendChild(text);
break;
// Undo
case 'F11':
var content = Undo.pop();
if (content) {
Redo.push(document.body.innerHTML);
document.body.innerHTML = content;
}
return;
// Redo
case 'F12':
var content = Redo.pop();
if (content) {
Undo.push(document.body.innerHTML);
document.body.innerHTML = content;
}
return;
// Delete
case 'Delete':
Undo.push(document.body.innerHTML);
Redo = new Array();
range = sel.getRangeAt(0);
range.deleteContents();
return;
default:
break;
}
Undo.push(document.body.innerHTML);
Redo = new Array();
// The new label is inserted
if (tag != null) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(tag);
}
}
};
};