Skip to content

Commit 89ee93d

Browse files
authored
fix(0.2.6): 评论分页等 (#37)
* 新增 评论分页 * 修改 “查看更多...”的样式,避免深色模式下的“手电筒” * 完善文档
1 parent 2c7310c commit 89ee93d

File tree

8 files changed

+89
-32
lines changed

8 files changed

+89
-32
lines changed

docs/.vuepress/theme/layouts/Layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<template #page-bottom>
44
<div class="page-edit">
55
<div id="twikoo"></div>
6-
<script src="https://cdn.jsdelivr.net/npm/twikoo@0.2.5/dist/twikoo.all.min.js" ref="twikooJs"></script>
6+
<script src="https://cdn.jsdelivr.net/npm/twikoo@0.2.6/dist/twikoo.all.min.js" ref="twikooJs"></script>
77
</div>
88
</template>
99
</ParentLayout>

docs/quick-start.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 快速上手
22

3+
[查看视频](https://www.bilibili.com/video/BV1MZ4y1G7VB?zw)
4+
35
## 环境初始化
46

57
Twikoo 使用云开发作为评论后台,每个云开发用户均长期享受1个免费的标准型基础版1资源套餐。如果您已经拥有了一个免费版云开发环境,在环境配置符合要求的情况下,Twikoo 理论可以与其他项目共用一个环境。
@@ -90,7 +92,7 @@ Butterfly 目前支持 Twikoo,请查看 [Butterfly 安裝文檔(四) 主題配
9092

9193
``` html
9294
<div id="tcomment"></div>
93-
<script src="https://cdn.jsdelivr.net/npm/twikoo@0.2.5/dist/twikoo.all.min.js"></script>
95+
<script src="https://cdn.jsdelivr.net/npm/twikoo@0.2.6/dist/twikoo.all.min.js"></script>
9496
<script>twikoo.init({ envId: '您的环境id', el: '#tcomment' })</script>
9597
```
9698

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twikoo",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "A simple comment system based on Tencent CloudBase (tcb).",
55
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
66
"license": "MIT",

src/function/twikoo/index.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Twikoo cloudbase function v0.2.5
2+
* Twikoo cloudbase function v0.2.6
33
* (c) 2020-2020 iMaeGoo
44
* Released under the MIT License.
55
*/
@@ -21,7 +21,7 @@ const db = app.database()
2121
const _ = db.command
2222

2323
// 常量 / constants
24-
const VERSION = '0.2.5'
24+
const VERSION = '0.2.6'
2525
const RES_CODE = {
2626
SUCCESS: 0,
2727
FAIL: 1000,
@@ -194,22 +194,49 @@ function getAdminTicket (credentials) {
194194
}
195195

196196
// 读取评论
197-
// TODO: 分页
198197
async function commentGet (event) {
199198
const res = {}
200-
let uid
201199
try {
202200
validate(event, ['url'])
203-
uid = await auth.getEndUserInfo().userInfo.uid
204-
const data = await db
201+
const uid = await auth.getEndUserInfo().userInfo.uid
202+
const query = {}
203+
const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
204+
let more = false
205+
query.url = event.url
206+
query.rid = _.in(['', null])
207+
query.isSpam = _.neq(true)
208+
// 读取总条数
209+
const count = await db
210+
.collection('comment')
211+
.where(query)
212+
.count()
213+
// 读取主楼
214+
if (event.before) {
215+
query.created = _.lt(event.before)
216+
}
217+
const main = await db
218+
.collection('comment')
219+
.where(query)
220+
.orderBy('created', 'desc')
221+
// 流式分页,通过多读 1 条的方式,确认是否还有更多评论
222+
.limit(limit + 1)
223+
.get()
224+
if (main.data.length > limit) {
225+
// 还有更多评论
226+
more = true
227+
// 删除多读的 1 条
228+
main.data.splice(limit - 1, 1)
229+
}
230+
// 读取回复楼
231+
const reply = await db
205232
.collection('comment')
206233
.where({
207-
url: event.url,
208-
isSpam: _.neq(true)
234+
rid: _.in(main.data.map((item) => item._id))
209235
})
210-
.orderBy('created', 'desc')
211236
.get()
212-
res.data = parseComment(data.data, uid)
237+
res.data = parseComment([...main.data, ...reply.data], uid)
238+
res.more = more
239+
res.count = count.total
213240
} catch (e) {
214241
res.data = []
215242
res.message = e.message
@@ -310,7 +337,10 @@ async function commentSetForAdmin (event) {
310337
const data = await db
311338
.collection('comment')
312339
.doc(event.id)
313-
.update(event.set)
340+
.update({
341+
...event.set,
342+
updated: new Date().getTime()
343+
})
314344
res.code = RES_CODE.SUCCESS
315345
res.updated = data.updated
316346
} else {

src/view/App.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,20 @@ export default {
3838
height: 100%;
3939
fill: currentColor;
4040
}
41+
42+
/* 全局 CSS */
43+
.tk-expand {
44+
width: 100%;
45+
cursor: pointer;
46+
padding: 0.75em;
47+
text-align: center;
48+
background-color: #00000010;
49+
transition: all 0.5s;
50+
}
51+
.tk-expand:hover {
52+
background-color: #00000020;
53+
}
54+
.tk-expand:active {
55+
background-color: #00000030;
56+
}
4157
</style>

src/view/components/TkAdminConfig.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export default {
3333
items: [
3434
{ key: 'SITE_NAME', desc: '网站名称', ph: '示例:虹墨空间站', value: '' },
3535
{ key: 'SITE_URL', desc: '网站地址', ph: '示例:https://www.imaegoo.com', value: '' },
36-
{ key: 'BLOGGER_EMAIL', desc: '博主的邮箱地址,用于邮件通知、博主标识。', ph: '示例:12345@qq.com', value: '' }
36+
{ key: 'BLOGGER_EMAIL', desc: '博主的邮箱地址,用于邮件通知、博主标识。', ph: '示例:12345@qq.com', value: '' },
37+
{ key: 'COMMENT_PAGE_SIZE', desc: '评论列表分页大小,默认为 8。', ph: '示例:8', value: '' }
3738
]
3839
},
3940
{

src/view/components/TkComment.vue

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
:comment="reply"
3333
@load="onLoad"
3434
@reply="onReplyReply" />
35-
<div class="tk-expand" v-if="showExpand" @click="onExpand">查看更多...</div>
3635
</div>
3736
<!-- 回复框 -->
3837
<tk-submit v-if="replying"
3938
:reply-id="comment.id"
4039
:pid="pid"
4140
@load="onLoad"
4241
@cancel="onCancel" />
42+
<div class="tk-expand" v-if="showExpand" @click="onExpand">查看更多...</div>
4343
</div>
4444
</div>
4545
</template>
@@ -242,18 +242,6 @@ export default {
242242
.tk-replies-expand {
243243
max-height: none;
244244
}
245-
.tk-expand {
246-
position: absolute;
247-
bottom: 0;
248-
left: 0;
249-
right: 0;
250-
height: 100px;
251-
display: flex;
252-
align-items: flex-end;
253-
justify-content: center;
254-
background: linear-gradient(rgba(255,255,255,0), rgba(255,255,255,1));
255-
cursor: pointer;
256-
}
257245
.tk-submit {
258246
margin-top: 1rem;
259247
}

src/view/components/TkComments.vue

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<tk-submit @load="initComments" />
44
<div class="tk-comments-container" v-loading="loading">
55
<div class="tk-comments-title">
6-
<span>{{ comments.length }} 条评论</span>
6+
<span>{{ count }} 条评论</span>
77
<span class="tk-icon" v-html="iconSetting" @click="openAdmin"></span>
88
</div>
99
<div class="tk-comments-no" v-if="!loading && !comments.length">没有评论</div>
@@ -13,6 +13,7 @@
1313
:replying="replyId === comment.id"
1414
@reply="onReply"
1515
@load="initComments" />
16+
<div class="tk-expand" v-if="showExpand" @click="onExpand" v-loading="loadingMore">查看更多...</div>
1617
</div>
1718
</div>
1819
</template>
@@ -31,21 +32,40 @@ export default {
3132
data () {
3233
return {
3334
loading: true,
35+
loadingMore: false,
3436
comments: [],
37+
showExpand: true,
38+
count: 0,
3539
replyId: '',
3640
iconSetting
3741
}
3842
},
3943
methods: {
4044
async initComments () {
4145
this.loading = true
42-
const comments = await call(this.$tcb, 'COMMENT_GET', {
46+
await this.getComments({
4347
url: window.location.pathname
4448
})
49+
this.loading = false
50+
},
51+
async onExpand () {
52+
this.loadingMore = true
53+
const before = this.comments
54+
.map((item) => item.created)
55+
.sort((a, b) => a - b)[0] // 最小值
56+
await this.getComments({
57+
url: window.location.pathname,
58+
before
59+
})
60+
this.loadingMore = false
61+
},
62+
async getComments (event) {
63+
const comments = await call(this.$tcb, 'COMMENT_GET', event)
4564
if (comments && comments.result && comments.result.data) {
46-
this.comments = comments.result.data
65+
this.comments = event.before ? this.comments.concat(comments.result.data) : comments.result.data
66+
this.showExpand = comments.result.more
67+
this.count = comments.result.count || this.comments.length
4768
}
48-
this.loading = false
4969
},
5070
onReply (id) {
5171
this.replyId = id

0 commit comments

Comments
 (0)