1
1
/* plainterm.js
2
2
https://github.com/mkrl/plainterm.js */
3
3
4
+ /*
5
+ element.scrollIntoViewIfNeeded() implementation for non-webkit browsers
6
+ full creadit goes to
7
+ https://gist.github.com/doxxx/8987233
8
+ and the original gist author
9
+ */
10
+
11
+
12
+ if ( ! Element . prototype . scrollIntoViewIfNeeded ) {
13
+ Element . prototype . scrollIntoViewIfNeeded = function ( centerIfNeeded ) {
14
+ centerIfNeeded = arguments . length === 0 ? true : ! ! centerIfNeeded ;
15
+
16
+ var parent = this . parentNode ,
17
+ parentComputedStyle = window . getComputedStyle ( parent , null ) ,
18
+ parentBorderTopWidth = parseInt ( parentComputedStyle . getPropertyValue ( 'border-top-width' ) ) ,
19
+ parentBorderLeftWidth = parseInt ( parentComputedStyle . getPropertyValue ( 'border-left-width' ) ) ,
20
+ overTop = this . offsetTop - parent . offsetTop < parent . scrollTop ,
21
+ overBottom = ( this . offsetTop - parent . offsetTop + this . clientHeight - parentBorderTopWidth ) > ( parent . scrollTop + parent . clientHeight ) ,
22
+ overLeft = this . offsetLeft - parent . offsetLeft < parent . scrollLeft ,
23
+ overRight = ( this . offsetLeft - parent . offsetLeft + this . clientWidth - parentBorderLeftWidth ) > ( parent . scrollLeft + parent . clientWidth ) ;
24
+
25
+ if ( centerIfNeeded ) {
26
+ if ( overTop || overBottom ) {
27
+ parent . scrollTop = this . offsetTop - parent . offsetTop - parent . clientHeight / 2 - parentBorderTopWidth + this . clientHeight / 2 ;
28
+ }
29
+
30
+ if ( overLeft || overRight ) {
31
+ parent . scrollLeft = this . offsetLeft - parent . offsetLeft - parent . clientWidth / 2 - parentBorderLeftWidth + this . clientWidth / 2 ;
32
+ }
33
+ }
34
+ else {
35
+ if ( overTop ) {
36
+ parent . scrollTop = this . offsetTop - parent . offsetTop - parentBorderTopWidth ;
37
+ }
38
+
39
+ if ( overBottom ) {
40
+ parent . scrollTop = this . offsetTop - parent . offsetTop - parentBorderTopWidth - parent . clientHeight + this . clientHeight ;
41
+ }
42
+
43
+ if ( overLeft ) {
44
+ parent . scrollLeft = this . offsetLeft - parent . offsetLeft - parentBorderLeftWidth ;
45
+ }
46
+
47
+ if ( overRight ) {
48
+ parent . scrollLeft = this . offsetLeft - parent . offsetLeft - parentBorderLeftWidth - parent . clientWidth + this . clientWidth ;
49
+ }
50
+ }
51
+ } ;
52
+ }
53
+
54
+ /*
55
+ end of gist
56
+ */
4
57
5
58
var plainterm = ( function ( ) {
6
59
7
60
//Master object
8
61
var bash = { commands : { } , history : [ ] , last : 0 , process : false ,
9
- version : "0.4.3 "
62
+ version : "0.4.4 "
10
63
} ;
11
64
12
65
//Master command constructor
@@ -38,7 +91,8 @@ var plainterm = (function() {
38
91
function term_init ( settings ) {
39
92
settings . id = settings . id || "terminal" ; /* do not use "||" for default values and use custom function instead */
40
93
settings . welcome = settings . welcome || "plainterm.js v. " + bash . version ;
41
- bash . prompt = settings . prompt || "$ " ;
94
+ settings . prompt = '<span class="terminal-prompt">' + settings . prompt + '</span>' || '<span class="terminal-prompt">$ </span>' ;
95
+ bash . prompt = settings . prompt ;
42
96
if ( settings . help === undefined ) {
43
97
settings . help = true ;
44
98
}
@@ -90,14 +144,15 @@ var plainterm = (function() {
90
144
if ( c === true ) {
91
145
var cmd = document . createElement ( "span" ) ;
92
146
cmd . innerHTML = content ;
93
- cmd . className = "command" ;
147
+ cmd . className = "termainal- command" ;
94
148
line . innerHTML = bash . prompt ;
95
149
bash . container . display . appendChild ( line ) ;
96
150
line . appendChild ( cmd ) ;
97
151
98
152
} else {
99
153
line . innerHTML = content ;
100
154
bash . container . display . appendChild ( line ) ;
155
+ bash . container . input . scrollIntoViewIfNeeded ( ) ;
101
156
}
102
157
}
103
158
@@ -113,7 +168,7 @@ var plainterm = (function() {
113
168
bash . history . unshift ( cmd ) ;
114
169
bash . container . dispatchEvent ( valid_event ) ;
115
170
} else {
116
- println ( comm + " - command not found" ) ;
171
+ println ( '<span class="terminal-error">' + comm + ' - command not found</span>' ) ;
117
172
bash . container . dispatchEvent ( invalid_event ) ;
118
173
}
119
174
}
@@ -127,7 +182,7 @@ var plainterm = (function() {
127
182
}
128
183
cmd . value = "" ;
129
184
bash . last = 0 ;
130
- bash . container . input . scrollIntoView ( ) ;
185
+ bash . container . input . scrollIntoViewIfNeeded ( ) ;
131
186
}
132
187
}
133
188
@@ -160,11 +215,13 @@ var plainterm = (function() {
160
215
line . innerHTML += text . charAt ( i ) ;
161
216
i ++ ;
162
217
setTimeout ( typing , speed ) ;
218
+ line . scrollIntoViewIfNeeded ( ) ;
163
219
} else if ( bash . process === false ) {
164
220
bash . container . area . style . display = "flex" ;
165
221
}
166
222
}
167
223
setTimeout ( typing , speed ) ;
224
+ bash . container . input . scrollIntoViewIfNeeded ( ) ;
168
225
}
169
226
170
227
//History
0 commit comments