Skip to content

Commit 60c4cac

Browse files
authored
Merge pull request #2596 from richardtks/correct-shift-select-behavior
Fix the shift selection and ctrl selection in note list
2 parents 30e6fc5 + 82987cd commit 60c4cac

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

browser/main/NoteList/index.js

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ class NoteList extends React.Component {
8383

8484
// TODO: not Selected noteKeys but SelectedNote(for reusing)
8585
this.state = {
86+
ctrlKeyDown: false,
8687
shiftKeyDown: false,
88+
prevShiftNoteIndex: -1,
8789
selectedNoteKeys: []
8890
}
8991

@@ -266,7 +268,7 @@ class NoteList extends React.Component {
266268
}
267269

268270
handleNoteListKeyDown (e) {
269-
if (e.metaKey || e.ctrlKey) return true
271+
if (e.metaKey) return true
270272

271273
// A key
272274
if (e.keyCode === 65 && !e.shiftKey) {
@@ -306,13 +308,19 @@ class NoteList extends React.Component {
306308

307309
if (e.shiftKey) {
308310
this.setState({ shiftKeyDown: true })
311+
} else if (e.ctrlKey) {
312+
this.setState({ ctrlKeyDown: true })
309313
}
310314
}
311315

312316
handleNoteListKeyUp (e) {
313317
if (!e.shiftKey) {
314318
this.setState({ shiftKeyDown: false })
315319
}
320+
321+
if (!e.ctrlKey) {
322+
this.setState({ ctrlKeyDown: false })
323+
}
316324
}
317325

318326
getNotes () {
@@ -389,25 +397,65 @@ class NoteList extends React.Component {
389397
return pinnedNotes.concat(unpinnedNotes)
390398
}
391399

400+
getNoteIndexByKey (noteKey) {
401+
return this.notes.findIndex((note) => {
402+
if (!note) return -1
403+
404+
return note.key === noteKey
405+
})
406+
}
407+
392408
handleNoteClick (e, uniqueKey) {
393409
const { router } = this.context
394410
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
397414

398-
if (shiftKeyDown && selectedNoteKeys.includes(uniqueKey)) {
415+
if (ctrlKeyDown && selectedNoteKeys.includes(uniqueKey)) {
399416
const newSelectedNoteKeys = selectedNoteKeys.filter((noteKey) => noteKey !== uniqueKey)
400417
this.setState({
401418
selectedNoteKeys: newSelectedNoteKeys
402419
})
403420
return
404421
}
405-
if (!shiftKeyDown) {
422+
if (!ctrlKeyDown && !shiftKeyDown) {
406423
selectedNoteKeys = []
407424
}
425+
426+
if (!shiftKeyDown) {
427+
prevShiftNoteIndex = -1
428+
}
429+
408430
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+
409456
this.setState({
410-
selectedNoteKeys
457+
selectedNoteKeys,
458+
prevShiftNoteIndex
411459
})
412460

413461
router.push({

0 commit comments

Comments
 (0)