@@ -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