Last active
April 12, 2022 09:33
-
-
Save wibus-wee/ab7863189b944776432e2e56b2376730 to your computer and use it in GitHub Desktop.
Translate Typecho to TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @FilePath: /typecho 1.2.0-rc.1/Users/wibus/Desktop/test.ts | |
* @author: Wibus | |
* @Date: 2022-04-05 18:12:47 | |
* @LastEditors: Wibus | |
* @LastEditTime: 2022-04-05 18:12:47 | |
* Coding With IU | |
*/ | |
export function execute() { | |
if (!this.parameter.parentId) { // 如果没有指定父级,则不需要进行execute | |
return; | |
} | |
let commentsAuthor = this.Cookieget('__typecho_remember_author'); // 获取cookie中的用户名 | |
let commentsMail = this.Cookieget('__typecho_remember_mail'); // 获取cookie中的邮箱 | |
let select = this.select() // 获取所有的评论 | |
.where('table.comments.cid = ?', this.parameter.parentId) | |
.where( | |
'table.comments.status = ? OR (table.comments.author = ?' + | |
' AND table.comments.mail = ? AND table.comments.status = ?)', | |
'approved', // 已经通过审核的评论 | |
commentsAuthor, | |
commentsMail, | |
'waiting' // 待审核的评论 | |
); | |
let threadedSelect = null; | |
if (this.options.commentsShowCommentOnly) { // 如果只显示评论,则需要进行线索化处理 | |
select.where('table.comments.type = ?', 'comment'); // 只显示评论 | |
} | |
select.order('table.comments.coid', 'ASC'); // 按照评论的id进行排序 | |
this.db.fetchAll(select, [ this, 'push' ]); // 查询并返回结果 | |
let outputComments = []; // 定义一个数组,用于存储所有的评论 | |
if (this.options.commentsThreaded) { // 如果需要线索化处理 | |
for (let coid in this.stack) { // 遍历所有的评论,this.stack是一个对象,对象的key是评论的id,value是评论的内容, coid是评论的id | |
let comment = this.stack[coid]; // 获取评论 | |
let parent = comment['parent']; // 获取评论的父级 | |
if (0 !== parent && this.stack[parent]) { // 如果评论的父级不是0,并且父级存在 | |
if (comment['levels'] >= this.options.commentsMaxNestingLevels) { // 如果评论的层级超过了最大层级 | |
comment['levels'] = this.stack[parent]['levels']; // 将评论的层级设置为父级的层级 | |
parent = this.stack[parent]['parent']; // 将父级的父级设置为评论的父级 | |
comment['parent'] = parent; // 将评论的父级设置为父级的父级 | |
} | |
comment['order'] = this.threadedComments[parent] ? this.threadedComments[parent].length : 1; // 设置评论的顺序 | |
this.threadedComments[parent][coid] = comment; // 将评论添加到线索化的评论中 | |
} else { | |
outputComments[coid] = comment; // 如果评论的父级是0,则直接添加到输出的评论中 | |
} | |
} | |
this.stack = outputComments; // 将输出的评论设置为stack | |
} | |
if ('DESC' === this.options.commentsOrder) { // 如果评论的排序是倒序 | |
this.stack = Object.values(this.stack).reverse(); // 将评论的值反转 | |
this.threadedComments = Object.values(this.threadedComments).map(array => { // 将线索化的评论的值反转 | |
return Object.values(array).reverse(); // 将评论的值反转 | |
}); | |
} | |
this.total = Object.keys(this.stack).length; // 设置评论的总数 | |
if (this.options.commentsPageBreak) { // 如果需要分页 | |
if ('last' === this.options.commentsPageDisplay && !this.parameter.commentPage) { // 如果需要显示最后一页 | |
this.currentPage = Math.ceil(this.total / this.options.commentsPageSize); // 设置当前页 | |
} else { | |
this.currentPage = this.parameter.commentPage ? this.parameter.commentPage : 1; // 设置当前页 | |
} | |
this.stack = Object.values( | |
this.stack.reduce( // 将评论的值反转 | |
(list, comment, index) => { | |
if (index >= ((this.currentPage - 1) * this.options.commentsPageSize) && index < (this.currentPage * this.options.commentsPageSize)) { | |
// 如果评论的索引在当前页的范围内 | |
list[comment['coid']] = comment; // 将评论添加到输出的评论中 | |
} | |
return list; // 返回输出的评论 | |
}, | |
{})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment