Skip to content

Commit 3df743f

Browse files
committed
Change the selection behavior to the default behavior
There are two behaviors being changed: 1. When user click on the notes a. Shift - It will select the notes among the first and the last note b. Ctrl - it will only select the selected note 2. When user uses the arrow keys a. Shift - it will extends the selection from the last selected note b. Ctrl - Nothing will happens
1 parent 71ae420 commit 3df743f

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

browser/main/NoteList/index.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ class NoteList extends React.Component {
8484

8585
// TODO: not Selected noteKeys but SelectedNote(for reusing)
8686
this.state = {
87+
ctrlKeyDown: false,
8788
shiftKeyDown: false,
89+
firstShiftSelectedNoteIndex: -1,
8890
selectedNoteKeys: []
8991
}
9092

@@ -267,7 +269,7 @@ class NoteList extends React.Component {
267269
}
268270

269271
handleNoteListKeyDown (e) {
270-
if (e.metaKey || e.ctrlKey) return true
272+
if (e.metaKey) return true
271273

272274
// A key
273275
if (e.keyCode === 65 && !e.shiftKey) {
@@ -307,13 +309,19 @@ class NoteList extends React.Component {
307309

308310
if (e.shiftKey) {
309311
this.setState({ shiftKeyDown: true })
312+
} else if (e.ctrlKey) {
313+
this.setState({ ctrlKeyDown: true })
310314
}
311315
}
312316

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

319327
getNotes () {
@@ -390,25 +398,51 @@ class NoteList extends React.Component {
390398
return pinnedNotes.concat(unpinnedNotes)
391399
}
392400

401+
getNoteIndexByKey (noteKey) {
402+
return this.notes.findIndex((note) => {
403+
if (!note) return -1
404+
405+
return note.key === noteKey
406+
})
407+
}
408+
393409
handleNoteClick (e, uniqueKey) {
394410
const { router } = this.context
395411
const { location } = this.props
396412
let { selectedNoteKeys } = this.state
397-
const { shiftKeyDown } = this.state
413+
const { ctrlKeyDown, shiftKeyDown } = this.state
414+
let firstShiftSelectedNoteIndex = -1
398415

399-
if (shiftKeyDown && selectedNoteKeys.includes(uniqueKey)) {
416+
if (ctrlKeyDown && selectedNoteKeys.includes(uniqueKey)) {
400417
const newSelectedNoteKeys = selectedNoteKeys.filter((noteKey) => noteKey !== uniqueKey)
401418
this.setState({
402419
selectedNoteKeys: newSelectedNoteKeys
403420
})
404421
return
405422
}
406-
if (!shiftKeyDown) {
423+
if (!ctrlKeyDown && !shiftKeyDown) {
407424
selectedNoteKeys = []
408425
}
409426
selectedNoteKeys.push(uniqueKey)
427+
428+
if (shiftKeyDown && selectedNoteKeys.length > 0) {
429+
const firstSelectedNoteIndex =
430+
Math.max(this.getNoteIndexByKey(selectedNoteKeys[0]),
431+
this.state.firstShiftSelectedNoteIndex)
432+
const lastSelectedNoteIndex = this.getNoteIndexByKey(uniqueKey)
433+
const startIndex = Math.min(firstSelectedNoteIndex, lastSelectedNoteIndex)
434+
const endIndex = Math.max(firstSelectedNoteIndex, lastSelectedNoteIndex)
435+
selectedNoteKeys = []
436+
for (let i = startIndex; i <= endIndex; i++) {
437+
selectedNoteKeys.push(this.notes[i].key)
438+
}
439+
440+
firstShiftSelectedNoteIndex = firstSelectedNoteIndex
441+
}
442+
410443
this.setState({
411-
selectedNoteKeys
444+
selectedNoteKeys,
445+
firstShiftSelectedNoteIndex
412446
})
413447

414448
router.push({

0 commit comments

Comments
 (0)