1- export default class VimStatusBar {
2- constructor ( node , editor , sanitizer = null ) {
1+ import { editor } from 'monaco-editor'
2+
3+ export class VimStatusBar {
4+ private node : HTMLElement | null
5+
6+ private editor : editor . IStandaloneCodeEditor
7+
8+ private modeInfoNode = document . createElement ( 'span' )
9+
10+ private secInfoNode = document . createElement ( 'span' )
11+
12+ private notifNode = document . createElement ( 'span' )
13+
14+ private keyInfoNode = document . createElement ( 'span' )
15+
16+ private input : null | { node : HTMLInputElement ; options : any ; callback : any } = null
17+
18+ private notifTimeout : number | null = null
19+
20+ private sanitizer : null | ( ( content : string ) => HTMLElement )
21+
22+ constructor ( node : HTMLElement | null , editor : editor . IStandaloneCodeEditor , sanitizer = null ) {
323 this . node = node
4- this . modeInfoNode = document . createElement ( 'span' )
5- this . secInfoNode = document . createElement ( 'span' )
6- this . notifNode = document . createElement ( 'span' )
24+ if ( this . node ) {
25+ this . node . appendChild ( this . modeInfoNode )
26+ this . node . appendChild ( this . secInfoNode )
27+ this . node . appendChild ( this . notifNode )
28+ this . node . appendChild ( this . keyInfoNode )
29+ }
730 this . notifNode . className = 'vim-notification'
8- this . keyInfoNode = document . createElement ( 'span' )
931 this . keyInfoNode . setAttribute ( 'style' , 'float: right' )
10- this . node . appendChild ( this . modeInfoNode )
11- this . node . appendChild ( this . secInfoNode )
12- this . node . appendChild ( this . notifNode )
13- this . node . appendChild ( this . keyInfoNode )
1432 this . toggleVisibility ( false )
1533 this . editor = editor
1634 this . sanitizer = sanitizer
1735 }
1836
19- setMode ( ev ) {
37+ setMode ( ev : { mode : string ; subMode : string } ) {
2038 if ( ev . mode === 'visual' && ev . subMode === 'linewise' ) {
2139 this . setText ( '--VISUAL LINE--' )
2240 return
@@ -25,11 +43,11 @@ export default class VimStatusBar {
2543 this . setText ( `--${ ev . mode . toUpperCase ( ) } --` )
2644 }
2745
28- setKeyBuffer ( key ) {
46+ setKeyBuffer ( key : string ) {
2947 this . keyInfoNode . textContent = key
3048 }
3149
32- setSec ( text , callback , options ) {
50+ setSec ( text : string , callback ?: any , options ?: any ) {
3351 this . notifNode . textContent = ''
3452 if ( text === undefined ) {
3553 return
@@ -62,22 +80,20 @@ export default class VimStatusBar {
6280 return this . closeInput
6381 }
6482
65- setText ( text ) {
83+ setText ( text : string ) {
6684 this . modeInfoNode . textContent = text
6785 }
6886
69- toggleVisibility ( toggle ) {
70- if ( toggle ) {
71- this . node . style . display = 'block'
72- } else {
73- this . node . style . display = 'none'
87+ toggleVisibility ( toggle : boolean ) {
88+ if ( this . node ) {
89+ this . node . style . display = toggle ? 'block' : 'none'
7490 }
7591
7692 if ( this . input ) {
7793 this . removeInputListeners ( )
7894 }
7995
80- clearInterval ( this . notifTimeout )
96+ this . notifTimeout && clearInterval ( this . notifTimeout )
8197 }
8298
8399 closeInput = ( ) => {
@@ -94,22 +110,30 @@ export default class VimStatusBar {
94110 this . setInnerHtml_ ( this . node , '' )
95111 }
96112
97- inputKeyUp = ( e ) => {
98- const { options } = this . input
99- if ( options && options . onKeyUp ) {
100- options . onKeyUp ( e , e . target . value , this . closeInput )
113+ inputKeyUp = ( e : any ) => {
114+ if ( this . input ) {
115+ const { options } = this . input
116+ if ( options && options . onKeyUp ) {
117+ options . onKeyUp ( e , e . target . value , this . closeInput )
118+ }
101119 }
102120 }
103121
104122 inputBlur = ( ) => {
123+ if ( ! this . input ) {
124+ return
125+ }
105126 const { options } = this . input
106127
107128 if ( options . closeOnBlur ) {
108129 this . closeInput ( )
109130 }
110131 }
111132
112- inputKeyDown = ( e ) => {
133+ inputKeyDown = ( e : any ) => {
134+ if ( ! this . input ) {
135+ return
136+ }
113137 const { options, callback } = this . input
114138
115139 if ( options && options . onKeyDown && options . onKeyDown ( e , e . target . value , this . closeInput ) ) {
@@ -130,10 +154,12 @@ export default class VimStatusBar {
130154 }
131155
132156 addInputListeners ( ) {
157+ if ( ! this . input ) {
158+ return
159+ }
133160 const { node } = this . input
134161 node . addEventListener ( 'keyup' , this . inputKeyUp )
135162 node . addEventListener ( 'keydown' , this . inputKeyDown )
136- node . addEventListener ( 'input' , this . inputKeyInput )
137163 node . addEventListener ( 'blur' , this . inputBlur )
138164 }
139165
@@ -145,20 +171,22 @@ export default class VimStatusBar {
145171 const { node } = this . input
146172 node . removeEventListener ( 'keyup' , this . inputKeyUp )
147173 node . removeEventListener ( 'keydown' , this . inputKeyDown )
148- node . removeEventListener ( 'input' , this . inputKeyInput )
149174 node . removeEventListener ( 'blur' , this . inputBlur )
150175 }
151176
152- showNotification ( text ) {
177+ showNotification ( text : string ) {
153178 const sp = document . createElement ( 'span' )
154179 this . setInnerHtml_ ( sp , text )
155180 this . notifNode . textContent = sp . textContent
156- this . notifTimeout = setTimeout ( ( ) => {
181+ this . notifTimeout = window . setTimeout ( ( ) => {
157182 this . notifNode . textContent = ''
158183 } , 5000 )
159184 }
160185
161- setInnerHtml_ ( element , htmlContents ) {
186+ setInnerHtml_ ( element : HTMLElement | null , htmlContents : string ) {
187+ if ( ! element ) {
188+ return
189+ }
162190 if ( this . sanitizer ) {
163191 // Clear out previous contents first.
164192 while ( element . children . length ) {
0 commit comments