Skip to content

Commit 8e7b4d2

Browse files
committed
infinite scroll
1 parent 54437ce commit 8e7b4d2

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

browser/main/NoteList/index.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class NoteList extends React.Component {
1919
this.focusHandler = () => {
2020
this.refs.root.focus()
2121
}
22+
23+
this.state = {
24+
range: 0
25+
}
2226
}
2327

2428
componentDidMount () {
@@ -28,6 +32,29 @@ class NoteList extends React.Component {
2832
ee.on('lost:focus', this.focusHandler)
2933
}
3034

35+
componentWillReceiveProps (nextProps) {
36+
if (nextProps.location.pathname !== this.props.location.pathname) {
37+
this.resetScroll()
38+
}
39+
}
40+
41+
resetScroll () {
42+
this.refs.root.scrollTop = 0
43+
this.setState({
44+
range: 0
45+
})
46+
}
47+
48+
handleScroll (e) {
49+
let notes = this.notes
50+
51+
if (e.target.offsetHeight + e.target.scrollTop > e.target.scrollHeight - 250 && notes.length > this.state.range * 10 + 10) {
52+
this.setState({
53+
range: this.state.range + 1
54+
})
55+
}
56+
}
57+
3158
componentWillUnmount () {
3259
clearInterval(this.refreshTimer)
3360

@@ -36,7 +63,7 @@ class NoteList extends React.Component {
3663
ee.off('lost:focus', this.focusHandler)
3764
}
3865

39-
componentDidUpdate () {
66+
componentDidUpdate (prevProps) {
4067
let { location } = this.props
4168
if (this.notes.length > 0 && location.query.key == null) {
4269
let { router } = this.context
@@ -50,7 +77,7 @@ class NoteList extends React.Component {
5077
}
5178

5279
// Auto scroll
53-
if (_.isString(location.query.key)) {
80+
if (_.isString(location.query.key) && prevProps.location.query.key !== location.query.key) {
5481
let targetIndex = _.findIndex(this.notes, (note) => {
5582
return note != null && note.storage + '-' + note.key === location.query.key
5683
})
@@ -204,7 +231,7 @@ class NoteList extends React.Component {
204231
this.notes = notes = this.getNotes()
205232
.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt))
206233

207-
let noteList = notes
234+
let noteList = notes.slice(0, 10 + 10 * this.state.range)
208235
.map((note) => {
209236
if (note == null) return null
210237
let storage = data.storageMap.get(note.storage)
@@ -277,6 +304,7 @@ class NoteList extends React.Component {
277304
tabIndex='-1'
278305
onKeyDown={(e) => this.handleNoteListKeyDown(e)}
279306
style={this.props.style}
307+
onScroll={(e) => this.handleScroll(e)}
280308
>
281309
{noteList}
282310
</div>

0 commit comments

Comments
 (0)