-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
60 lines (51 loc) · 1.71 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
import { Stack } from './stack.js';
document.onkeydown = function(event) {
if (event.ctrlKey || event.metaKey) {
event.preventDefault();
}
};
onload = function () {
// Get reference to elements
const textbox = document.getElementById('comment');
const undo = document.getElementById('undo');
const clear = document.getElementById('clear');
const temptext = document.getElementById('temptext');
textbox.value = "";
let text = "";
let stack = new Stack();
textbox.onclick = function () {
textbox.selectionStart = textbox.selectionEnd = textbox.value.length;
};
clear.onclick = function () {
stack.clear();
text = "";
textbox.value = "";
temptext.innerHTML = "Sequence of operations will be shown here !";
};
textbox.oninput = function(event){
// console.log(event);
switch(event.inputType){
case "insertText":
stack.push(0, event.data);
break;
case "deleteContentBackward":
stack.push(1, text[text.length-1]);
break;
}
temptext.innerHTML = "On stack "+stack.top()+"<br>"+temptext.innerHTML;
text = textbox.value;
};
undo.onclick = function () {
let operation = stack.pop();
if(operation[0]!==-1){
temptext.innerHTML = "Performing undo operation<br>"+temptext.innerHTML;
if(operation[0] === 0){
let len = operation[1].length;
textbox.value = textbox.value.substring(0,textbox.value.length-len);
} else{
textbox.value += operation[1];
}
text = textbox.value;
}
};
};