@@ -129,106 +129,110 @@ export class CommandBar {
129
129
if ( ! selection . isCollapsed || ! selection . rangeCount ) return ;
130
130
131
131
if ( event . key === '/' && ! this . _active ) {
132
- this . options . onActivate && this . options . onActivate ( ) ;
132
+ this . open ( { commands : this . options . commands , openOnKeyupTarget : event . target } ) ;
133
+ }
134
+ }
135
+
136
+ open ( options ) {
137
+ this . options . onActivate && this . options . onActivate ( ) ;
133
138
139
+ if ( options . openOnKeyupTarget ) {
134
140
const showOnceOnKeyup = ( ) => {
135
141
this . show ( ) ;
136
- event . target . removeEventListener ( 'keyup' , showOnceOnKeyup , true ) ;
137
- initialTarget = event . target ;
138
- oldValue = event . target . innerText ;
142
+ options . openOnKeyupTarget . removeEventListener ( 'keyup' , showOnceOnKeyup , true ) ;
143
+ initialTarget = options . openOnKeyupTarget ;
144
+ oldValue = options . openOnKeyupTarget . innerText ;
139
145
} ;
140
- event . target . addEventListener ( 'keyup' , showOnceOnKeyup , true ) ;
141
- this . _active = true ;
142
- this . render ( this . options . commands ) ;
143
- this . _resetPosition ( ) ;
146
+ options . openOnKeyupTarget . addEventListener ( 'keyup' , showOnceOnKeyup , true ) ;
147
+ }
148
+ this . _active = true ;
149
+ this . _currentFilteredCommands = options . commands ;
150
+ this . render ( options . commands ) ;
151
+ this . _resetPosition ( ) ;
144
152
145
- let initialTarget ;
146
- let oldValue ;
153
+ let initialTarget ;
154
+ let oldValue ;
147
155
148
- const keyup = event => {
149
- if ( event . key === 'ArrowDown' || event . key === 'ArrowUp' ) {
150
- event . preventDefault ( ) ;
151
- return ;
152
- }
153
- if ( ! initialTarget ) return ;
154
- const diff = patienceDiff (
155
- oldValue . split ( '' ) ,
156
- initialTarget . innerText . split ( '' ) ,
157
- true ,
158
- ) ;
159
- this . _lastText = diff . bMove . join ( '' ) . trim ( ) ;
156
+ const keyup = event => {
157
+ if ( event . key === 'ArrowDown' || event . key === 'ArrowUp' ) {
158
+ event . preventDefault ( ) ;
159
+ return ;
160
+ }
161
+ if ( ! initialTarget ) return ;
162
+ const diff = patienceDiff ( oldValue . split ( '' ) , initialTarget . innerText . split ( '' ) , true ) ;
163
+ this . _lastText = diff . bMove . join ( '' ) . trim ( ) ;
160
164
161
- if ( this . _lastText . match ( / \s / ) ) {
162
- this . _stop ( ) ;
163
- return ;
164
- }
165
- const term = this . _lastText ;
165
+ if ( this . _lastText . match ( / \s / ) ) {
166
+ this . _stop ( ) ;
167
+ return ;
168
+ }
169
+ const term = this . _lastText ;
166
170
167
- this . _currentFilteredCommands = this . _filter ( term ) ;
168
- this . render ( this . _currentFilteredCommands ) ;
169
- this . _resetPosition ( ) ;
170
- } ;
171
- const keydown = e => {
172
- if ( e . key === 'Enter' ) {
173
- e . stopImmediatePropagation ( ) ;
174
- this . _currentValidate ( ) ;
175
- e . preventDefault ( ) ;
176
- } else if ( e . key === 'Escape' ) {
177
- e . stopImmediatePropagation ( ) ;
178
- this . _stop ( ) ;
179
- e . preventDefault ( ) ;
180
- } else if ( e . key === 'Backspace' && ! this . _lastText ) {
181
- this . _stop ( ) ;
182
- } else if ( e . key === 'ArrowDown' || e . key === 'ArrowUp' ) {
183
- e . preventDefault ( ) ;
184
- e . stopImmediatePropagation ( ) ;
171
+ this . _currentFilteredCommands = this . _filter ( term , options . commands ) ;
172
+ this . render ( this . _currentFilteredCommands ) ;
173
+ this . _resetPosition ( ) ;
174
+ } ;
175
+ const keydown = e => {
176
+ if ( e . key === 'Enter' ) {
177
+ e . stopImmediatePropagation ( ) ;
178
+ this . _currentValidate ( ) ;
179
+ e . preventDefault ( ) ;
180
+ } else if ( e . key === 'Escape' ) {
181
+ e . stopImmediatePropagation ( ) ;
182
+ this . _stop ( ) ;
183
+ e . preventDefault ( ) ;
184
+ } else if ( e . key === 'Backspace' && ! this . _lastText ) {
185
+ this . _stop ( ) ;
186
+ } else if ( e . key === 'ArrowDown' || e . key === 'ArrowUp' ) {
187
+ e . preventDefault ( ) ;
188
+ e . stopImmediatePropagation ( ) ;
185
189
186
- const index = this . _currentFilteredCommands . findIndex (
187
- c => c === this . _currentSelectedCommand ,
188
- ) ;
189
- if ( ! this . _currentFilteredCommands . length || index === - 1 ) {
190
- this . _currentSelectedCommand = undefined ;
191
- } else {
192
- const n = e . key === 'ArrowDown' ? 1 : - 1 ;
193
- const newIndex = cycle ( index + n , this . _currentFilteredCommands . length - 1 ) ;
194
- this . _currentSelectedCommand = this . _currentFilteredCommands [ newIndex ] ;
195
- }
196
- e . preventDefault ( ) ;
197
- this . render ( this . _currentFilteredCommands ) ;
190
+ const index = this . _currentFilteredCommands . findIndex (
191
+ c => c === this . _currentSelectedCommand ,
192
+ ) ;
193
+ if ( ! this . _currentFilteredCommands . length || index === - 1 ) {
194
+ this . _currentSelectedCommand = undefined ;
195
+ } else {
196
+ const n = e . key === 'ArrowDown' ? 1 : - 1 ;
197
+ const newIndex = cycle ( index + n , this . _currentFilteredCommands . length - 1 ) ;
198
+ this . _currentSelectedCommand = this . _currentFilteredCommands [ newIndex ] ;
198
199
}
199
- } ;
200
- const mousemove = ( ) => {
201
- this . _hoverActive = true ;
202
- } ;
200
+ e . preventDefault ( ) ;
201
+ this . render ( this . _currentFilteredCommands ) ;
202
+ }
203
+ } ;
204
+ const mousemove = ( ) => {
205
+ this . _hoverActive = true ;
206
+ } ;
203
207
204
- this . _stop = ( ) => {
205
- this . _active = false ;
206
- this . hide ( ) ;
207
- this . _currentSelectedCommand = undefined ;
208
+ this . _stop = ( ) => {
209
+ this . _active = false ;
210
+ this . hide ( ) ;
211
+ this . _currentSelectedCommand = undefined ;
208
212
209
- document . removeEventListener ( 'mousedown' , this . _stop ) ;
210
- document . removeEventListener ( 'keyup' , keyup ) ;
211
- document . removeEventListener ( 'keydown' , keydown , true ) ;
212
- document . removeEventListener ( 'mousemove' , mousemove ) ;
213
+ document . removeEventListener ( 'mousedown' , this . _stop ) ;
214
+ document . removeEventListener ( 'keyup' , keyup ) ;
215
+ document . removeEventListener ( 'keydown' , keydown , true ) ;
216
+ document . removeEventListener ( 'mousemove' , mousemove ) ;
213
217
214
- this . options . onStop && this . options . onStop ( ) ;
215
- } ;
216
- this . _currentValidate = ( ) => {
217
- const command = this . _currentFilteredCommands . find (
218
- c => c === this . _currentSelectedCommand ,
219
- ) ;
220
- if ( command ) {
221
- this . options . preValidate && this . options . preValidate ( ) ;
222
- command . callback ( ) ;
223
- this . options . postValidate && this . options . postValidate ( ) ;
224
- }
225
- this . _stop ( ) ;
226
- } ;
227
- document . addEventListener ( 'mousedown' , this . _stop ) ;
228
- document . addEventListener ( 'keyup' , keyup ) ;
229
- document . addEventListener ( 'keydown' , keydown , true ) ;
230
- document . addEventListener ( 'mousemove' , mousemove ) ;
231
- }
218
+ this . options . onStop && this . options . onStop ( ) ;
219
+ } ;
220
+ this . _currentValidate = ( ) => {
221
+ const command = this . _currentFilteredCommands . find (
222
+ c => c === this . _currentSelectedCommand ,
223
+ ) ;
224
+ if ( command ) {
225
+ this . options . preValidate && this . options . preValidate ( ) ;
226
+ command . callback ( ) ;
227
+ this . options . postValidate && this . options . postValidate ( ) ;
228
+ }
229
+ this . _stop ( ) ;
230
+ } ;
231
+ document . addEventListener ( 'mousedown' , this . _stop ) ;
232
+ document . addEventListener ( 'keyup' , keyup ) ;
233
+ document . addEventListener ( 'keydown' , keydown , true ) ;
234
+ document . addEventListener ( 'mousemove' , mousemove ) ;
235
+ if ( ! options . openOnKeyupTarget ) this . show ( ) ;
232
236
}
233
237
234
238
show ( ) {
@@ -246,8 +250,8 @@ export class CommandBar {
246
250
// private
247
251
// -------------------------------------------------------------------------
248
252
249
- _filter ( term ) {
250
- let commands = this . options . commands ;
253
+ _filter ( term , commands ) {
254
+ const initalCommands = commands ;
251
255
term = term . toLowerCase ( ) ;
252
256
term = term . replaceAll ( / \s / g, '\\s' ) ;
253
257
const regex = new RegExp (
@@ -257,7 +261,7 @@ export class CommandBar {
257
261
. join ( '.*' ) ,
258
262
) ;
259
263
if ( term . length ) {
260
- commands = this . options . commands . filter ( command => {
264
+ commands = initalCommands . filter ( command => {
261
265
const commandText = ( command . groupName + ' ' + command . title ) . toLowerCase ( ) ;
262
266
return commandText . match ( regex ) ;
263
267
} ) ;
0 commit comments