Skip to content

Commit

Permalink
更新字数统计组件
Browse files Browse the repository at this point in the history
  • Loading branch information
Nriver committed Feb 21, 2023
1 parent f6cd05d commit 2ae746e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
8 changes: 8 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ const notes = await api.runOnBackend(async () => {

---

# 0.58 升级到 0.59 之后版本的问题

## 字数统计组件报错

由于API变化,字数统计组件的代码需要修改,请将字数统计组件的代码替换为最新的 [word count 字数统计组件.js](https://github.com/Nriver/trilium-translation/blob/main/demo-cn/示例笔记%20-%20请不要删除/Trilium%20扩展/Trilium%20组件%20widget/word%20count%20字数统计组件.js)

---

# 关于本项目使用的字体

本项目使用的字体文件为免费字体.
Expand Down
Binary file modified demo-cn.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,57 @@ const TPL = `<div style="padding: 10px; border-top: 1px solid var(--main-border-
<strong>字符数: </strong>
<span class="character-count"></span>
</div`;
</div>`;

class WordCountWidget extends api.TabAwareWidget {
class WordCountWidget extends api.NoteContextAwareWidget {
get position() { return 100; } // higher value means position towards the bottom/right

get parentWidget() { return 'center-pane'; }


isEnabled() {
// 只在有 "字数统计" 这个标签的笔记里才显示组件
return super.isEnabled()
&& this.note.type === 'text'
&& this.note.hasLabel('字数统计');
}

doRender() {
this.$widget = $(TPL);
this.$wordCount = this.$widget.find('.word-count');
this.$characterCount = this.$widget.find('.character-count');
return this.$widget;
}

async refreshWithNote(note) {
if (note.type !== 'text' || !note.hasLabel('字数统计')) {
// 只在有 "字数统计" 这个标签的笔记里才显示组件
this.toggleInt(false); // 隐藏

return;
}

this.toggleInt(true); // 显示

const {content} = await note.getNoteComplement();

const text = $(content).text(); // get plain text only

const counts = this.getCounts(text);

this.$wordCount.text(counts.words);
this.$characterCount.text(counts.characters);
}

getCounts(text) {
const chunks = text
.split(/[\s-+:,/\\]+/)
.filter(chunk => chunk !== '');

let words;

if (chunks.length === 1 && chunks[0] === '') {
words = 0;
}
else {
words = chunks.length;
}

const characters = chunks.join('').length;

return {words, characters};
}

async entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteContentReloaded(this.noteId)) {
this.refresh();
Expand Down

0 comments on commit 2ae746e

Please sign in to comment.