评论倒序

github issue获取comments的api不支持倒序,替换下原有获取comments的逻辑即可。
具体是这个函数:Gitment.loadComments,改写代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{
key: 'loadComments',
value: function loadComments() {
var _this8 = this;

var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.state.currentPage;
var reverse = true
return this.getIssue().then(function (issue) {
if (!reverse) {
return _utils.http.get(issue.comments_url, { page: page, per_page: _this8.perPage }, '');
} else {
var pageSize = Math.min(Math.max(_this8.perPage, 0), 50)

var total = issue.comments
var totalPageCount = Math.floor((total + pageSize - 1) / pageSize)
console.log(total, totalPageCount)

var pageNo = Math.min(Math.max(page, 0), totalPageCount)

var start = (pageNo - 1) * pageSize + 1
var end = Math.min(pageNo * pageSize, total)

var inverseStart = total + 1 - end
var inverseEnd = total + 1 - start
console.log(inverseStart, inverseEnd)

var inverseStartPageNo = Math.floor((inverseStart - 1) / pageSize + 1)
console.log(inverseStartPageNo)

var comments_url = issue.comments_url
return _utils.http.get(comments_url, {
page: inverseStartPageNo,
per_page: pageSize
}, ''
).then(function(startPageComments) {
var comments = []
var preCount = (inverseStartPageNo - 1) * pageSize
var to = Math.min(startPageComments.length, inverseEnd-preCount)
for (var i = inverseStart-1-preCount; i < to; i++) {
comments.push(startPageComments[i])
}
if (startPageComments.length < inverseEnd-preCount) {
return _utils.http.get(comments_url, {
page: inverseStartPageNo+1,
per_page: pageSize
}, ''
).then(function(nextPageComments) {
var preCount = inverseStartPageNo * pageSize
for (var i = 0; i < inverseEnd-preCount; i++) {
comments.push(nextPageComments[i])
}
return comments
});
} else {
return comments
}
}).then(function(comments) {
return comments.reverse()
});
}
}).then(function (comments) {
_this8.state.comments = comments;
return comments;
});
}
},