forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS_project_notepad part1
100 lines (93 loc) · 2.38 KB
/
DS_project_notepad part1
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
class Stack{
arr_undo=[]
arr_redo=[]
buffer=4
topelement = () =>
{
return this.arr_undo[this.arr_undo.length-1]
}
popping = () => {
if(this.arr_undo.length!=0)
{
let top = this.arr_undo.pop()
return top
}
return [-1,""]
}
pushing = (type,charac) => {
if(this.arr_undo.length==0)
{
if(type=="inserting")
{
this.arr_undo.push([type,charac])
}
}
else
{
let top = this.topelement()
if(type===top[0] && top[1].length<this.buffer)
{
let st = this.popping()
st[1] = charac + st[1];
this.arr_undo.push([type,st[1]])
}
else
{
this.arr_undo.push([type,charac])
}
}
}
reverse = (s1) => {
let s = "";
let i=s1.length-1;
while(i>=0)
{
s=s.concat(s1.substring(i,i+1))
i--;
}
return s;
}
}
const textarea = document.getElementById("textarea")
const undo = document.getElementById("undo")
const redo = document.getElementById("redo")
const stack = new Stack()
textarea.addEventListener("keydown",(e) => {
switch(e.key)
{
case 'Backspace': stack.pushing("deleting",textarea.value.substring(textarea.value.length-1,textarea.value.length));
break;
default : stack.pushing("inserting",e.key);
}
})
undo.addEventListener("click",() => {
let temp = stack.popping();
if(temp[0]!=-1)
{
stack.arr_redo.push([temp[0],temp[1]])
if(temp[0]=="inserting")
{
let templen = temp[1].length
textarea.value = textarea.value.substring(0,textarea.value.length-templen)
}
else
{
textarea.value+=temp[1]
}
}
})
redo.addEventListener("click",() => {
let temp = stack.arr_redo.pop();
if(temp[0]=="inserting")
{
let s = stack.reverse(temp[1])
textarea.value+=s
stack.pushing("inserting",temp[1])
}
else
{
let templen = temp[1].length
stack.pushing("deleting", temp[1])
textarea.value = textarea.value.substring(0,textarea.value.length-templen)
}
})