@@ -4,35 +4,30 @@ var keyUtil = require("../lib/keys");
4
4
var useragent = require ( "../lib/useragent" ) ;
5
5
var KEY_MODS = keyUtil . KEY_MODS ;
6
6
7
- function HashHandler ( config , platform ) {
8
- this . platform = platform || ( useragent . isMac ? "mac" : "win" ) ;
9
- this . commands = { } ;
10
- this . commandKeyBinding = { } ;
11
- this . addCommands ( config ) ;
12
- this . $singleCommand = true ;
13
- }
14
-
15
- function MultiHashHandler ( config , platform ) {
16
- HashHandler . call ( this , config , platform ) ;
17
- this . $singleCommand = false ;
18
- }
19
-
20
- MultiHashHandler . prototype = HashHandler . prototype ;
7
+ class MultiHashHandler {
8
+ constructor ( config , platform ) {
9
+ this . $init ( config , platform , false ) ;
10
+ }
21
11
22
- ( function ( ) {
23
-
12
+ $init ( config , platform , $singleCommand ) {
13
+ this . platform = platform || ( useragent . isMac ? "mac" : "win" ) ;
14
+ this . commands = { } ;
15
+ this . commandKeyBinding = { } ;
16
+ this . addCommands ( config ) ;
17
+ this . $singleCommand = $singleCommand ;
18
+ }
24
19
25
- this . addCommand = function ( command ) {
20
+ addCommand ( command ) {
26
21
if ( this . commands [ command . name ] )
27
22
this . removeCommand ( command ) ;
28
23
29
24
this . commands [ command . name ] = command ;
30
25
31
26
if ( command . bindKey )
32
27
this . _buildKeyHash ( command ) ;
33
- } ;
28
+ }
34
29
35
- this . removeCommand = function ( command , keepCommand ) {
30
+ removeCommand ( command , keepCommand ) {
36
31
var name = command && ( typeof command === 'string' ? command : command . name ) ;
37
32
command = this . commands [ name ] ;
38
33
if ( ! keepCommand )
@@ -54,9 +49,9 @@ MultiHashHandler.prototype = HashHandler.prototype;
54
49
}
55
50
}
56
51
}
57
- } ;
52
+ }
58
53
59
- this . bindKey = function ( key , command , position ) {
54
+ bindKey ( key , command , position ) {
60
55
if ( typeof key == "object" && key ) {
61
56
if ( position == undefined )
62
57
position = key . position ;
@@ -84,14 +79,9 @@ MultiHashHandler.prototype = HashHandler.prototype;
84
79
var id = KEY_MODS [ binding . hashId ] + binding . key ;
85
80
this . _addCommandToBinding ( chain + id , command , position ) ;
86
81
} , this ) ;
87
- } ;
88
-
89
- function getPosition ( command ) {
90
- return typeof command == "object" && command . bindKey
91
- && command . bindKey . position
92
- || ( command . isDefault ? - 100 : 0 ) ;
93
82
}
94
- this . _addCommandToBinding = function ( keyId , command , position ) {
83
+
84
+ _addCommandToBinding ( keyId , command , position ) {
95
85
var ckb = this . commandKeyBinding , i ;
96
86
if ( ! command ) {
97
87
delete ckb [ keyId ] ;
@@ -117,9 +107,9 @@ MultiHashHandler.prototype = HashHandler.prototype;
117
107
}
118
108
commands . splice ( i , 0 , command ) ;
119
109
}
120
- } ;
110
+ }
121
111
122
- this . addCommands = function ( commands ) {
112
+ addCommands ( commands ) {
123
113
commands && Object . keys ( commands ) . forEach ( function ( name ) {
124
114
var command = commands [ name ] ;
125
115
if ( ! command )
@@ -139,27 +129,27 @@ MultiHashHandler.prototype = HashHandler.prototype;
139
129
140
130
this . addCommand ( command ) ;
141
131
} , this ) ;
142
- } ;
132
+ }
143
133
144
- this . removeCommands = function ( commands ) {
134
+ removeCommands ( commands ) {
145
135
Object . keys ( commands ) . forEach ( function ( name ) {
146
136
this . removeCommand ( commands [ name ] ) ;
147
137
} , this ) ;
148
- } ;
138
+ }
149
139
150
- this . bindKeys = function ( keyList ) {
140
+ bindKeys ( keyList ) {
151
141
Object . keys ( keyList ) . forEach ( function ( key ) {
152
142
this . bindKey ( key , keyList [ key ] ) ;
153
143
} , this ) ;
154
- } ;
144
+ }
155
145
156
- this . _buildKeyHash = function ( command ) {
146
+ _buildKeyHash ( command ) {
157
147
this . bindKey ( command . bindKey , command ) ;
158
- } ;
148
+ }
159
149
160
150
// accepts keys in the form ctrl+Enter or ctrl-Enter
161
151
// keys without modifiers or shift only
162
- this . parseKeys = function ( keys ) {
152
+ parseKeys ( keys ) {
163
153
var parts = keys . toLowerCase ( ) . split ( / [ \- \+ ] ( [ \- \+ ] ) ? / ) . filter ( function ( x ) { return x ; } ) ;
164
154
var key = parts . pop ( ) ;
165
155
@@ -182,14 +172,14 @@ MultiHashHandler.prototype = HashHandler.prototype;
182
172
hashId |= modifier ;
183
173
}
184
174
return { key : key , hashId : hashId } ;
185
- } ;
175
+ }
186
176
187
- this . findKeyCommand = function findKeyCommand ( hashId , keyString ) {
177
+ findKeyCommand ( hashId , keyString ) {
188
178
var key = KEY_MODS [ hashId ] + keyString ;
189
179
return this . commandKeyBinding [ key ] ;
190
- } ;
180
+ }
191
181
192
- this . handleKeyboard = function ( data , hashId , keyString , keyCode ) {
182
+ handleKeyboard ( data , hashId , keyString , keyCode ) {
193
183
if ( keyCode < 0 ) return ;
194
184
var key = KEY_MODS [ hashId ] + keyString ;
195
185
var command = this . commandKeyBinding [ key ] ;
@@ -212,13 +202,33 @@ MultiHashHandler.prototype = HashHandler.prototype;
212
202
data . $keyChain = "" ; // reset keyChain
213
203
}
214
204
return { command : command } ;
215
- } ;
205
+ }
216
206
217
- this . getStatusText = function ( editor , data ) {
207
+ getStatusText ( editor , data ) {
218
208
return data . $keyChain || "" ;
219
- } ;
209
+ }
210
+
211
+ }
212
+
213
+ function getPosition ( command ) {
214
+ return typeof command == "object" && command . bindKey
215
+ && command . bindKey . position
216
+ || ( command . isDefault ? - 100 : 0 ) ;
217
+ }
218
+
219
+ class HashHandler extends MultiHashHandler {
220
+ constructor ( config , platform ) {
221
+ super ( config , platform ) ;
222
+ this . $singleCommand = true ;
223
+ }
224
+ }
220
225
221
- } ) . call ( HashHandler . prototype ) ;
226
+ HashHandler . call = function ( thisArg , config , platform ) {
227
+ MultiHashHandler . prototype . $init . call ( thisArg , config , platform , true ) ;
228
+ } ;
229
+ MultiHashHandler . call = function ( thisArg , config , platform ) {
230
+ MultiHashHandler . prototype . $init . call ( thisArg , config , platform , false ) ;
231
+ } ;
222
232
223
233
exports . HashHandler = HashHandler ;
224
234
exports . MultiHashHandler = MultiHashHandler ;
0 commit comments