1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-15 18:56:22 +00:00

infinite scroll

This commit is contained in:
Dick Choi
2016-09-08 18:54:05 +09:00
parent 54437cec19
commit 8e7b4d2444

View File

@@ -19,6 +19,10 @@ class NoteList extends React.Component {
this.focusHandler = () => { this.focusHandler = () => {
this.refs.root.focus() this.refs.root.focus()
} }
this.state = {
range: 0
}
} }
componentDidMount () { componentDidMount () {
@@ -28,6 +32,29 @@ class NoteList extends React.Component {
ee.on('lost:focus', this.focusHandler) ee.on('lost:focus', this.focusHandler)
} }
componentWillReceiveProps (nextProps) {
if (nextProps.location.pathname !== this.props.location.pathname) {
this.resetScroll()
}
}
resetScroll () {
this.refs.root.scrollTop = 0
this.setState({
range: 0
})
}
handleScroll (e) {
let notes = this.notes
if (e.target.offsetHeight + e.target.scrollTop > e.target.scrollHeight - 250 && notes.length > this.state.range * 10 + 10) {
this.setState({
range: this.state.range + 1
})
}
}
componentWillUnmount () { componentWillUnmount () {
clearInterval(this.refreshTimer) clearInterval(this.refreshTimer)
@@ -36,7 +63,7 @@ class NoteList extends React.Component {
ee.off('lost:focus', this.focusHandler) ee.off('lost:focus', this.focusHandler)
} }
componentDidUpdate () { componentDidUpdate (prevProps) {
let { location } = this.props let { location } = this.props
if (this.notes.length > 0 && location.query.key == null) { if (this.notes.length > 0 && location.query.key == null) {
let { router } = this.context let { router } = this.context
@@ -50,7 +77,7 @@ class NoteList extends React.Component {
} }
// Auto scroll // Auto scroll
if (_.isString(location.query.key)) { if (_.isString(location.query.key) && prevProps.location.query.key !== location.query.key) {
let targetIndex = _.findIndex(this.notes, (note) => { let targetIndex = _.findIndex(this.notes, (note) => {
return note != null && note.storage + '-' + note.key === location.query.key return note != null && note.storage + '-' + note.key === location.query.key
}) })
@@ -204,7 +231,7 @@ class NoteList extends React.Component {
this.notes = notes = this.getNotes() this.notes = notes = this.getNotes()
.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt)) .sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt))
let noteList = notes let noteList = notes.slice(0, 10 + 10 * this.state.range)
.map((note) => { .map((note) => {
if (note == null) return null if (note == null) return null
let storage = data.storageMap.get(note.storage) let storage = data.storageMap.get(note.storage)
@@ -277,6 +304,7 @@ class NoteList extends React.Component {
tabIndex='-1' tabIndex='-1'
onKeyDown={(e) => this.handleNoteListKeyDown(e)} onKeyDown={(e) => this.handleNoteListKeyDown(e)}
style={this.props.style} style={this.props.style}
onScroll={(e) => this.handleScroll(e)}
> >
{noteList} {noteList}
</div> </div>