Skip to content

Commit 2ae746e

Browse files
committed
更新字数统计组件
1 parent f6cd05d commit 2ae746e

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

README_CN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,14 @@ const notes = await api.runOnBackend(async () => {
513513

514514
---
515515

516+
# 0.58 升级到 0.59 之后版本的问题
517+
518+
## 字数统计组件报错
519+
520+
由于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)
521+
522+
---
523+
516524
# 关于本项目使用的字体
517525

518526
本项目使用的字体文件为免费字体.

demo-cn.zip

39.4 KB
Binary file not shown.

demo-cn/示例笔记 - 请不要删除/Trilium 扩展/Trilium 组件 widget/word count 字数统计组件.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,57 @@ const TPL = `<div style="padding: 10px; border-top: 1px solid var(--main-border-
1212
1313
<strong>字符数: </strong>
1414
<span class="character-count"></span>
15-
</div`;
15+
</div>`;
1616

17-
class WordCountWidget extends api.TabAwareWidget {
17+
class WordCountWidget extends api.NoteContextAwareWidget {
1818
get position() { return 100; } // higher value means position towards the bottom/right
19-
19+
2020
get parentWidget() { return 'center-pane'; }
21-
21+
22+
isEnabled() {
23+
// 只在有 "字数统计" 这个标签的笔记里才显示组件
24+
return super.isEnabled()
25+
&& this.note.type === 'text'
26+
&& this.note.hasLabel('字数统计');
27+
}
28+
2229
doRender() {
2330
this.$widget = $(TPL);
2431
this.$wordCount = this.$widget.find('.word-count');
2532
this.$characterCount = this.$widget.find('.character-count');
2633
return this.$widget;
2734
}
28-
35+
2936
async refreshWithNote(note) {
30-
if (note.type !== 'text' || !note.hasLabel('字数统计')) {
31-
// 只在有 "字数统计" 这个标签的笔记里才显示组件
32-
this.toggleInt(false); // 隐藏
33-
34-
return;
35-
}
36-
37-
this.toggleInt(true); // 显示
38-
3937
const {content} = await note.getNoteComplement();
40-
38+
4139
const text = $(content).text(); // get plain text only
42-
40+
4341
const counts = this.getCounts(text);
4442

4543
this.$wordCount.text(counts.words);
4644
this.$characterCount.text(counts.characters);
4745
}
48-
46+
4947
getCounts(text) {
5048
const chunks = text
5149
.split(/[\s-+:,/\\]+/)
5250
.filter(chunk => chunk !== '');
53-
51+
5452
let words;
55-
53+
5654
if (chunks.length === 1 && chunks[0] === '') {
5755
words = 0;
5856
}
5957
else {
6058
words = chunks.length;
6159
}
62-
60+
6361
const characters = chunks.join('').length;
64-
62+
6563
return {words, characters};
6664
}
67-
65+
6866
async entitiesReloadedEvent({loadResults}) {
6967
if (loadResults.isNoteContentReloaded(this.noteId)) {
7068
this.refresh();

0 commit comments

Comments
 (0)