@@ -83,7 +83,9 @@ class NoteList extends React.Component {
83
83
84
84
// TODO: not Selected noteKeys but SelectedNote(for reusing)
85
85
this . state = {
86
+ ctrlKeyDown : false ,
86
87
shiftKeyDown : false ,
88
+ prevShiftNoteIndex : - 1 ,
87
89
selectedNoteKeys : [ ]
88
90
}
89
91
@@ -266,7 +268,7 @@ class NoteList extends React.Component {
266
268
}
267
269
268
270
handleNoteListKeyDown ( e ) {
269
- if ( e . metaKey || e . ctrlKey ) return true
271
+ if ( e . metaKey ) return true
270
272
271
273
// A key
272
274
if ( e . keyCode === 65 && ! e . shiftKey ) {
@@ -306,13 +308,19 @@ class NoteList extends React.Component {
306
308
307
309
if ( e . shiftKey ) {
308
310
this . setState ( { shiftKeyDown : true } )
311
+ } else if ( e . ctrlKey ) {
312
+ this . setState ( { ctrlKeyDown : true } )
309
313
}
310
314
}
311
315
312
316
handleNoteListKeyUp ( e ) {
313
317
if ( ! e . shiftKey ) {
314
318
this . setState ( { shiftKeyDown : false } )
315
319
}
320
+
321
+ if ( ! e . ctrlKey ) {
322
+ this . setState ( { ctrlKeyDown : false } )
323
+ }
316
324
}
317
325
318
326
getNotes ( ) {
@@ -389,25 +397,65 @@ class NoteList extends React.Component {
389
397
return pinnedNotes . concat ( unpinnedNotes )
390
398
}
391
399
400
+ getNoteIndexByKey ( noteKey ) {
401
+ return this . notes . findIndex ( ( note ) => {
402
+ if ( ! note ) return - 1
403
+
404
+ return note . key === noteKey
405
+ } )
406
+ }
407
+
392
408
handleNoteClick ( e , uniqueKey ) {
393
409
const { router } = this . context
394
410
const { location } = this . props
395
- let { selectedNoteKeys } = this . state
396
- const { shiftKeyDown } = this . state
411
+ let { selectedNoteKeys, prevShiftNoteIndex } = this . state
412
+ const { ctrlKeyDown, shiftKeyDown } = this . state
413
+ const hasSelectedNoteKey = selectedNoteKeys . length > 0
397
414
398
- if ( shiftKeyDown && selectedNoteKeys . includes ( uniqueKey ) ) {
415
+ if ( ctrlKeyDown && selectedNoteKeys . includes ( uniqueKey ) ) {
399
416
const newSelectedNoteKeys = selectedNoteKeys . filter ( ( noteKey ) => noteKey !== uniqueKey )
400
417
this . setState ( {
401
418
selectedNoteKeys : newSelectedNoteKeys
402
419
} )
403
420
return
404
421
}
405
- if ( ! shiftKeyDown ) {
422
+ if ( ! ctrlKeyDown && ! shiftKeyDown ) {
406
423
selectedNoteKeys = [ ]
407
424
}
425
+
426
+ if ( ! shiftKeyDown ) {
427
+ prevShiftNoteIndex = - 1
428
+ }
429
+
408
430
selectedNoteKeys . push ( uniqueKey )
431
+
432
+ if ( shiftKeyDown && hasSelectedNoteKey ) {
433
+ let firstShiftNoteIndex = this . getNoteIndexByKey ( selectedNoteKeys [ 0 ] )
434
+ // Shift selection can either start from first note in the exisiting selectedNoteKeys
435
+ // or previous first shift note index
436
+ firstShiftNoteIndex = firstShiftNoteIndex > prevShiftNoteIndex
437
+ ? firstShiftNoteIndex : prevShiftNoteIndex
438
+
439
+ const lastShiftNoteIndex = this . getNoteIndexByKey ( uniqueKey )
440
+
441
+ const startIndex = firstShiftNoteIndex < lastShiftNoteIndex
442
+ ? firstShiftNoteIndex : lastShiftNoteIndex
443
+ const endIndex = firstShiftNoteIndex > lastShiftNoteIndex
444
+ ? firstShiftNoteIndex : lastShiftNoteIndex
445
+
446
+ selectedNoteKeys = [ ]
447
+ for ( let i = startIndex ; i <= endIndex ; i ++ ) {
448
+ selectedNoteKeys . push ( this . notes [ i ] . key )
449
+ }
450
+
451
+ if ( prevShiftNoteIndex < 0 ) {
452
+ prevShiftNoteIndex = firstShiftNoteIndex
453
+ }
454
+ }
455
+
409
456
this . setState ( {
410
- selectedNoteKeys
457
+ selectedNoteKeys,
458
+ prevShiftNoteIndex
411
459
} )
412
460
413
461
router . push ( {
0 commit comments