11import { debounce , Debouncer , TFile , Vault , Workspace } from "obsidian" ;
2+ import type BetterWordCount from "../main" ;
23import { STATS_FILE } from "../constants" ;
34import type { Day , VaultStatistics } from "./Stats" ;
45import moment from "moment" ;
56import {
67 getCharacterCount ,
78 getSentenceCount ,
9+ getPageCount ,
810 getWordCount ,
911} from "../utils/StatUtils" ;
1012
1113export default class StatsManager {
1214 private vault : Vault ;
1315 private workspace : Workspace ;
16+ private plugin : BetterWordCount ;
1417 private vaultStats : VaultStatistics ;
1518 private today : string ;
1619 public debounceChange ;
1720
18- constructor ( vault : Vault , workspace : Workspace ) {
21+ constructor ( vault : Vault , workspace : Workspace , plugin : BetterWordCount ) {
1922 this . vault = vault ;
2023 this . workspace = workspace ;
24+ this . plugin = plugin ;
2125 this . debounceChange = debounce (
2226 ( text : string ) => this . change ( text ) ,
2327 50 ,
@@ -76,15 +80,18 @@ export default class StatsManager {
7680 const totalWords = await this . calcTotalWords ( ) ;
7781 const totalCharacters = await this . calcTotalCharacters ( ) ;
7882 const totalSentences = await this . calcTotalSentences ( ) ;
83+ const totalPages = await this . calcTotalPages ( ) ;
7984
8085 const newDay : Day = {
8186 words : 0 ,
8287 characters : 0 ,
8388 sentences : 0 ,
89+ pages : 0 ,
8490 files : 0 ,
8591 totalWords : totalWords ,
8692 totalCharacters : totalCharacters ,
8793 totalSentences : totalSentences ,
94+ totalPages : totalPages ,
8895 } ;
8996
9097 this . vaultStats . modifiedFiles = { } ;
@@ -97,6 +104,8 @@ export default class StatsManager {
97104 const currentWords = getWordCount ( text ) ;
98105 const currentCharacters = getCharacterCount ( text ) ;
99106 const currentSentences = getSentenceCount ( text ) ;
107+ const currentPages = getPageCount ( text , this . plugin . settings . pageWords ) ;
108+
100109 if (
101110 this . vaultStats . history . hasOwnProperty ( this . today ) &&
102111 this . today === moment ( ) . format ( "YYYY-MM-DD" )
@@ -110,9 +119,12 @@ export default class StatsManager {
110119 currentCharacters - modFiles [ fileName ] . characters . current ;
111120 this . vaultStats . history [ this . today ] . totalSentences +=
112121 currentSentences - modFiles [ fileName ] . sentences . current ;
122+ this . vaultStats . history [ this . today ] . totalPages +=
123+ currentPages - modFiles [ fileName ] . pages . current ;
113124 modFiles [ fileName ] . words . current = currentWords ;
114125 modFiles [ fileName ] . characters . current = currentCharacters ;
115126 modFiles [ fileName ] . sentences . current = currentSentences ;
127+ modFiles [ fileName ] . pages . current = currentPages ;
116128 } else {
117129 modFiles [ fileName ] = {
118130 words : {
@@ -127,6 +139,10 @@ export default class StatsManager {
127139 initial : currentSentences ,
128140 current : currentSentences ,
129141 } ,
142+ pages : {
143+ initial : currentPages ,
144+ current : currentPages ,
145+ } ,
130146 } ;
131147 }
132148
@@ -145,10 +161,16 @@ export default class StatsManager {
145161 Math . max ( 0 , counts . sentences . current - counts . sentences . initial )
146162 )
147163 . reduce ( ( a , b ) => a + b , 0 ) ;
164+ const pages = Object . values ( modFiles )
165+ . map ( ( counts ) =>
166+ Math . max ( 0 , counts . pages . current - counts . pages . initial )
167+ )
168+ . reduce ( ( a , b ) => a + b , 0 ) ;
148169
149170 this . vaultStats . history [ this . today ] . words = words ;
150171 this . vaultStats . history [ this . today ] . characters = characters ;
151172 this . vaultStats . history [ this . today ] . sentences = sentences ;
173+ this . vaultStats . history [ this . today ] . pages = pages ;
152174 this . vaultStats . history [ this . today ] . files = this . getTotalFiles ( ) ;
153175
154176 await this . update ( ) ;
@@ -167,6 +189,7 @@ export default class StatsManager {
167189 todayHist . totalWords = await this . calcTotalWords ( ) ;
168190 todayHist . totalCharacters = await this . calcTotalCharacters ( ) ;
169191 todayHist . totalSentences = await this . calcTotalSentences ( ) ;
192+ todayHist . totalPages = await this . calcTotalPages ( ) ;
170193 this . update ( ) ;
171194 } else {
172195 this . updateToday ( ) ;
@@ -211,6 +234,20 @@ export default class StatsManager {
211234
212235 return sentence ;
213236 }
237+
238+ private async calcTotalPages ( ) : Promise < number > {
239+ let pages = 0 ;
240+
241+ const files = this . vault . getFiles ( ) ;
242+ for ( const i in files ) {
243+ const file = files [ i ] ;
244+ if ( file . extension === "md" ) {
245+ pages += getPageCount ( await this . vault . cachedRead ( file ) , this . plugin . settings . pageWords ) ;
246+ }
247+ }
248+
249+ return pages ;
250+ }
214251
215252 public getDailyWords ( ) : number {
216253 return this . vaultStats . history [ this . today ] . words ;
@@ -224,6 +261,10 @@ export default class StatsManager {
224261 return this . vaultStats . history [ this . today ] . sentences ;
225262 }
226263
264+ public getDailyPages ( ) : number {
265+ return this . vaultStats . history [ this . today ] . pages ;
266+ }
267+
227268 public getTotalFiles ( ) : number {
228269 return this . vault . getMarkdownFiles ( ) . length ;
229270 }
@@ -242,4 +283,9 @@ export default class StatsManager {
242283 if ( ! this . vaultStats ) return await this . calcTotalSentences ( ) ;
243284 return this . vaultStats . history [ this . today ] . totalSentences ;
244285 }
286+
287+ public async getTotalPages ( ) : Promise < number > {
288+ if ( ! this . vaultStats ) return await this . calcTotalPages ( ) ;
289+ return this . vaultStats . history [ this . today ] . totalPages ;
290+ }
245291}
0 commit comments