mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
refactor: add utils
This commit is contained in:
@@ -32,6 +32,18 @@ function sortByUpdatedAt (a, b) {
|
|||||||
return new Date(b.updatedAt) - new Date(a.updatedAt)
|
return new Date(b.updatedAt) - new Date(a.updatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findNoteByKey (notes, noteKey) {
|
||||||
|
return notes.find((note) => `${note.storage}-${note.key}` === noteKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
function findNotesByKeys (notes, noteKeys) {
|
||||||
|
return notes.filter((note) => noteKeys.includes(getNoteKey(note)))
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNoteKey (note) {
|
||||||
|
return `${note.storage}-${note.key}`
|
||||||
|
}
|
||||||
|
|
||||||
class NoteList extends React.Component {
|
class NoteList extends React.Component {
|
||||||
constructor (props) {
|
constructor (props) {
|
||||||
super(props)
|
super(props)
|
||||||
@@ -162,9 +174,7 @@ class NoteList extends React.Component {
|
|||||||
}
|
}
|
||||||
targetIndex--
|
targetIndex--
|
||||||
|
|
||||||
if (!shiftKeyDown) {
|
if (!shiftKeyDown) { selectedNoteKeys = [] }
|
||||||
selectedNoteKeys = []
|
|
||||||
}
|
|
||||||
const priorNote = Object.assign({}, this.notes[targetIndex])
|
const priorNote = Object.assign({}, this.notes[targetIndex])
|
||||||
const priorNoteKey = `${priorNote.storage}-${priorNote.key}`
|
const priorNoteKey = `${priorNote.storage}-${priorNote.key}`
|
||||||
if (selectedNoteKeys.includes(priorNoteKey)) {
|
if (selectedNoteKeys.includes(priorNoteKey)) {
|
||||||
@@ -174,6 +184,8 @@ class NoteList extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.focusNote(selectedNoteKeys, priorNoteKey)
|
this.focusNote(selectedNoteKeys, priorNoteKey)
|
||||||
|
|
||||||
|
ee.emit('list:moved')
|
||||||
}
|
}
|
||||||
|
|
||||||
selectNextNote () {
|
selectNextNote () {
|
||||||
@@ -186,7 +198,7 @@ class NoteList extends React.Component {
|
|||||||
|
|
||||||
let targetIndex = this.getTargetIndex()
|
let targetIndex = this.getTargetIndex()
|
||||||
|
|
||||||
if (targetIndex === this.notes.length - 1) {
|
if (targetIndex === this.notes.length - 1 && shiftKeyDown) {
|
||||||
return
|
return
|
||||||
} else if (targetIndex === this.notes.length - 1) {
|
} else if (targetIndex === this.notes.length - 1) {
|
||||||
targetIndex = 0
|
targetIndex = 0
|
||||||
@@ -196,11 +208,9 @@ class NoteList extends React.Component {
|
|||||||
else if (targetIndex > this.notes.length - 1) targetIndex === this.notes.length - 1
|
else if (targetIndex > this.notes.length - 1) targetIndex === this.notes.length - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!shiftKeyDown) {
|
if (!shiftKeyDown) { selectedNoteKeys = [] }
|
||||||
selectedNoteKeys = []
|
|
||||||
}
|
|
||||||
const nextNote = Object.assign({}, this.notes[targetIndex])
|
const nextNote = Object.assign({}, this.notes[targetIndex])
|
||||||
const nextNoteKey = `${nextNote.storage}-${nextNote.key}`
|
const nextNoteKey = getNoteKey(nextNote)
|
||||||
if (selectedNoteKeys.includes(nextNoteKey)) {
|
if (selectedNoteKeys.includes(nextNoteKey)) {
|
||||||
selectedNoteKeys.pop()
|
selectedNoteKeys.pop()
|
||||||
} else {
|
} else {
|
||||||
@@ -355,8 +365,9 @@ class NoteList extends React.Component {
|
|||||||
let { shiftKeyDown, selectedNoteKeys } = this.state
|
let { shiftKeyDown, selectedNoteKeys } = this.state
|
||||||
|
|
||||||
if (shiftKeyDown && selectedNoteKeys.includes(uniqueKey)) {
|
if (shiftKeyDown && selectedNoteKeys.includes(uniqueKey)) {
|
||||||
|
const newSelectedNoteKeys = [...selectedNoteKeys].filter((noteKey) => noteKey !== uniqueKey)
|
||||||
this.setState({
|
this.setState({
|
||||||
selectedNoteKeys: [...selectedNoteKeys].filter((item) => item !== uniqueKey)
|
selectedNoteKeys: newSelectedNoteKeys
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -367,6 +378,7 @@ class NoteList extends React.Component {
|
|||||||
this.setState({
|
this.setState({
|
||||||
selectedNoteKeys
|
selectedNoteKeys
|
||||||
})
|
})
|
||||||
|
|
||||||
router.push({
|
router.push({
|
||||||
pathname: location.pathname,
|
pathname: location.pathname,
|
||||||
query: {
|
query: {
|
||||||
@@ -418,7 +430,7 @@ class NoteList extends React.Component {
|
|||||||
handleDragStart (e, note) {
|
handleDragStart (e, note) {
|
||||||
const { selectedNoteKeys } = this.state
|
const { selectedNoteKeys } = this.state
|
||||||
const notes = this.notes.map((note) => Object.assign({}, note))
|
const notes = this.notes.map((note) => Object.assign({}, note))
|
||||||
const selectedNotes = notes.filter((note) => selectedNoteKeys.includes(`${note.storage}-${note.key}`))
|
const selectedNotes = findNotesByKeys(notes, selectedNoteKeys)
|
||||||
const noteData = JSON.stringify(selectedNotes)
|
const noteData = JSON.stringify(selectedNotes)
|
||||||
e.dataTransfer.setData('note', noteData)
|
e.dataTransfer.setData('note', noteData)
|
||||||
this.setState({ selectedNoteKeys: [] })
|
this.setState({ selectedNoteKeys: [] })
|
||||||
@@ -427,10 +439,7 @@ class NoteList extends React.Component {
|
|||||||
handleNoteContextMenu (e, uniqueKey) {
|
handleNoteContextMenu (e, uniqueKey) {
|
||||||
const { location } = this.props
|
const { location } = this.props
|
||||||
const { selectedNoteKeys } = this.state
|
const { selectedNoteKeys } = this.state
|
||||||
const note = this.notes.find((note) => {
|
const note = findNoteByKey(this.notes, uniqueKey)
|
||||||
const noteKey = `${note.storage}-${note.key}`
|
|
||||||
return noteKey === uniqueKey
|
|
||||||
})
|
|
||||||
const noteKey = `${note.storage}-${note.key}`
|
const noteKey = `${note.storage}-${note.key}`
|
||||||
|
|
||||||
if (selectedNoteKeys.length === 0 || !selectedNoteKeys.includes(noteKey)) {
|
if (selectedNoteKeys.length === 0 || !selectedNoteKeys.includes(noteKey)) {
|
||||||
@@ -444,7 +453,7 @@ class NoteList extends React.Component {
|
|||||||
if (!location.pathname.match(/\/home|\/starred|\/trash/)) {
|
if (!location.pathname.match(/\/home|\/starred|\/trash/)) {
|
||||||
menu.append(new MenuItem({
|
menu.append(new MenuItem({
|
||||||
label: pinLabel,
|
label: pinLabel,
|
||||||
click: (e) => this.pinToTop(e, uniqueKey)
|
click: this.pinToTop
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
menu.append(new MenuItem({
|
menu.append(new MenuItem({
|
||||||
@@ -454,11 +463,11 @@ class NoteList extends React.Component {
|
|||||||
menu.popup()
|
menu.popup()
|
||||||
}
|
}
|
||||||
|
|
||||||
pinToTop (e, uniqueKey) {
|
pinToTop () {
|
||||||
const { selectedNoteKeys } = this.state
|
const { selectedNoteKeys } = this.state
|
||||||
const { dispatch } = this.props
|
const { dispatch } = this.props
|
||||||
const notes = this.notes.map((note) => Object.assign({}, note))
|
const notes = this.notes.map((note) => Object.assign({}, note))
|
||||||
const selectedNotes = notes.filter((note) => selectedNoteKeys.includes(`${note.storage}-${note.key}`))
|
const selectedNotes = findNotesByKeys(notes, selectedNoteKeys)
|
||||||
|
|
||||||
Promise.all(
|
Promise.all(
|
||||||
selectedNotes.map((note) => {
|
selectedNotes.map((note) => {
|
||||||
@@ -481,9 +490,8 @@ class NoteList extends React.Component {
|
|||||||
deleteNote () {
|
deleteNote () {
|
||||||
const { dispatch } = this.props
|
const { dispatch } = this.props
|
||||||
const { selectedNoteKeys } = this.state
|
const { selectedNoteKeys } = this.state
|
||||||
// not to change objects of this.notes
|
|
||||||
const notes = this.notes.map((note) => Object.assign({}, note))
|
const notes = this.notes.map((note) => Object.assign({}, note))
|
||||||
const selectedNotes = notes.filter((note) => selectedNoteKeys.includes(`${note.storage}-${note.key}`))
|
const selectedNotes = findNotesByKeys(notes, selectedNoteKeys)
|
||||||
const firstNote = selectedNotes[0]
|
const firstNote = selectedNotes[0]
|
||||||
|
|
||||||
if (firstNote.isTrashed) {
|
if (firstNote.isTrashed) {
|
||||||
@@ -592,7 +600,7 @@ class NoteList extends React.Component {
|
|||||||
})
|
})
|
||||||
hashHistory.push({
|
hashHistory.push({
|
||||||
pathname: location.pathname,
|
pathname: location.pathname,
|
||||||
query: {key: `${note.storage}-${note.key}`}
|
query: {key: getNoteKey(note)}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -602,7 +610,7 @@ class NoteList extends React.Component {
|
|||||||
getTargetIndex () {
|
getTargetIndex () {
|
||||||
const { location } = this.props
|
const { location } = this.props
|
||||||
const targetIndex = _.findIndex(this.notes, (note) => {
|
const targetIndex = _.findIndex(this.notes, (note) => {
|
||||||
return `${note.storage}-${note.key}` === location.query.key
|
return getNoteKey(note) === location.query.key
|
||||||
})
|
})
|
||||||
return targetIndex
|
return targetIndex
|
||||||
}
|
}
|
||||||
@@ -630,13 +638,12 @@ class NoteList extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isDefault = config.listStyle === 'DEFAULT'
|
const isDefault = config.listStyle === 'DEFAULT'
|
||||||
const uniqueKey = note.storage + '-' + note.key
|
const uniqueKey = getNoteKey(note)
|
||||||
const isActive = selectedNoteKeys.includes(uniqueKey)
|
const isActive = selectedNoteKeys.includes(uniqueKey)
|
||||||
const dateDisplay = moment(
|
const dateDisplay = moment(
|
||||||
config.sortBy === 'CREATED_AT'
|
config.sortBy === 'CREATED_AT'
|
||||||
? note.createdAt : note.updatedAt
|
? note.createdAt : note.updatedAt
|
||||||
).fromNow()
|
).fromNow()
|
||||||
const key = `${note.storage}-${note.key}`
|
|
||||||
|
|
||||||
if (isDefault) {
|
if (isDefault) {
|
||||||
return (
|
return (
|
||||||
@@ -644,7 +651,7 @@ class NoteList extends React.Component {
|
|||||||
isActive={isActive}
|
isActive={isActive}
|
||||||
note={note}
|
note={note}
|
||||||
dateDisplay={dateDisplay}
|
dateDisplay={dateDisplay}
|
||||||
key={key}
|
key={uniqueKey}
|
||||||
handleNoteContextMenu={this.handleNoteContextMenu.bind(this)}
|
handleNoteContextMenu={this.handleNoteContextMenu.bind(this)}
|
||||||
handleNoteClick={this.handleNoteClick.bind(this)}
|
handleNoteClick={this.handleNoteClick.bind(this)}
|
||||||
handleDragStart={this.handleDragStart.bind(this)}
|
handleDragStart={this.handleDragStart.bind(this)}
|
||||||
@@ -657,7 +664,7 @@ class NoteList extends React.Component {
|
|||||||
<NoteItemSimple
|
<NoteItemSimple
|
||||||
isActive={isActive}
|
isActive={isActive}
|
||||||
note={note}
|
note={note}
|
||||||
key={key}
|
key={uniqueKey}
|
||||||
handleNoteContextMenu={this.handleNoteContextMenu.bind(this)}
|
handleNoteContextMenu={this.handleNoteContextMenu.bind(this)}
|
||||||
handleNoteClick={this.handleNoteClick.bind(this)}
|
handleNoteClick={this.handleNoteClick.bind(this)}
|
||||||
handleDragStart={this.handleDragStart.bind(this)}
|
handleDragStart={this.handleDragStart.bind(this)}
|
||||||
|
|||||||
Reference in New Issue
Block a user