mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 01:36:22 +00:00
Merge pull request #2596 from richardtks/correct-shift-select-behavior
Fix the shift selection and ctrl selection in note list
This commit is contained in:
@@ -83,7 +83,9 @@ class NoteList extends React.Component {
|
|||||||
|
|
||||||
// TODO: not Selected noteKeys but SelectedNote(for reusing)
|
// TODO: not Selected noteKeys but SelectedNote(for reusing)
|
||||||
this.state = {
|
this.state = {
|
||||||
|
ctrlKeyDown: false,
|
||||||
shiftKeyDown: false,
|
shiftKeyDown: false,
|
||||||
|
prevShiftNoteIndex: -1,
|
||||||
selectedNoteKeys: []
|
selectedNoteKeys: []
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +268,7 @@ class NoteList extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleNoteListKeyDown (e) {
|
handleNoteListKeyDown (e) {
|
||||||
if (e.metaKey || e.ctrlKey) return true
|
if (e.metaKey) return true
|
||||||
|
|
||||||
// A key
|
// A key
|
||||||
if (e.keyCode === 65 && !e.shiftKey) {
|
if (e.keyCode === 65 && !e.shiftKey) {
|
||||||
@@ -306,6 +308,8 @@ class NoteList extends React.Component {
|
|||||||
|
|
||||||
if (e.shiftKey) {
|
if (e.shiftKey) {
|
||||||
this.setState({ shiftKeyDown: true })
|
this.setState({ shiftKeyDown: true })
|
||||||
|
} else if (e.ctrlKey) {
|
||||||
|
this.setState({ ctrlKeyDown: true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -313,6 +317,10 @@ class NoteList extends React.Component {
|
|||||||
if (!e.shiftKey) {
|
if (!e.shiftKey) {
|
||||||
this.setState({ shiftKeyDown: false })
|
this.setState({ shiftKeyDown: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!e.ctrlKey) {
|
||||||
|
this.setState({ ctrlKeyDown: false })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getNotes () {
|
getNotes () {
|
||||||
@@ -389,25 +397,65 @@ class NoteList extends React.Component {
|
|||||||
return pinnedNotes.concat(unpinnedNotes)
|
return pinnedNotes.concat(unpinnedNotes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getNoteIndexByKey (noteKey) {
|
||||||
|
return this.notes.findIndex((note) => {
|
||||||
|
if (!note) return -1
|
||||||
|
|
||||||
|
return note.key === noteKey
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
handleNoteClick (e, uniqueKey) {
|
handleNoteClick (e, uniqueKey) {
|
||||||
const { router } = this.context
|
const { router } = this.context
|
||||||
const { location } = this.props
|
const { location } = this.props
|
||||||
let { selectedNoteKeys } = this.state
|
let { selectedNoteKeys, prevShiftNoteIndex } = this.state
|
||||||
const { shiftKeyDown } = this.state
|
const { ctrlKeyDown, shiftKeyDown } = this.state
|
||||||
|
const hasSelectedNoteKey = selectedNoteKeys.length > 0
|
||||||
|
|
||||||
if (shiftKeyDown && selectedNoteKeys.includes(uniqueKey)) {
|
if (ctrlKeyDown && selectedNoteKeys.includes(uniqueKey)) {
|
||||||
const newSelectedNoteKeys = selectedNoteKeys.filter((noteKey) => noteKey !== uniqueKey)
|
const newSelectedNoteKeys = selectedNoteKeys.filter((noteKey) => noteKey !== uniqueKey)
|
||||||
this.setState({
|
this.setState({
|
||||||
selectedNoteKeys: newSelectedNoteKeys
|
selectedNoteKeys: newSelectedNoteKeys
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!shiftKeyDown) {
|
if (!ctrlKeyDown && !shiftKeyDown) {
|
||||||
selectedNoteKeys = []
|
selectedNoteKeys = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!shiftKeyDown) {
|
||||||
|
prevShiftNoteIndex = -1
|
||||||
|
}
|
||||||
|
|
||||||
selectedNoteKeys.push(uniqueKey)
|
selectedNoteKeys.push(uniqueKey)
|
||||||
|
|
||||||
|
if (shiftKeyDown && hasSelectedNoteKey) {
|
||||||
|
let firstShiftNoteIndex = this.getNoteIndexByKey(selectedNoteKeys[0])
|
||||||
|
// Shift selection can either start from first note in the exisiting selectedNoteKeys
|
||||||
|
// or previous first shift note index
|
||||||
|
firstShiftNoteIndex = firstShiftNoteIndex > prevShiftNoteIndex
|
||||||
|
? firstShiftNoteIndex : prevShiftNoteIndex
|
||||||
|
|
||||||
|
const lastShiftNoteIndex = this.getNoteIndexByKey(uniqueKey)
|
||||||
|
|
||||||
|
const startIndex = firstShiftNoteIndex < lastShiftNoteIndex
|
||||||
|
? firstShiftNoteIndex : lastShiftNoteIndex
|
||||||
|
const endIndex = firstShiftNoteIndex > lastShiftNoteIndex
|
||||||
|
? firstShiftNoteIndex : lastShiftNoteIndex
|
||||||
|
|
||||||
|
selectedNoteKeys = []
|
||||||
|
for (let i = startIndex; i <= endIndex; i++) {
|
||||||
|
selectedNoteKeys.push(this.notes[i].key)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prevShiftNoteIndex < 0) {
|
||||||
|
prevShiftNoteIndex = firstShiftNoteIndex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
selectedNoteKeys
|
selectedNoteKeys,
|
||||||
|
prevShiftNoteIndex
|
||||||
})
|
})
|
||||||
|
|
||||||
router.push({
|
router.push({
|
||||||
|
|||||||
Reference in New Issue
Block a user