@@ -5,8 +5,10 @@ import NutrientCache from "./NutrientCache";
55import FoodSuggest from "./FoodSuggest" ;
66import NutritionTotal from "./NutritionTotal" ;
77import FoodHighlightExtension from "./FoodHighlightExtension" ;
8+ import GoalsHighlightExtension from "./GoalsHighlightExtension" ;
89import DocumentTotalManager from "./DocumentTotalManager" ;
910import { SettingsService , FoodTrackerPluginSettings , DEFAULT_SETTINGS } from "./SettingsService" ;
11+ import GoalsService from "./GoalsService" ;
1012
1113export default class FoodTrackerPlugin extends Plugin {
1214 settings : FoodTrackerPluginSettings ;
@@ -16,7 +18,9 @@ export default class FoodTrackerPlugin extends Plugin {
1618 statusBarItem : HTMLElement ;
1719 documentTotalManager : DocumentTotalManager ;
1820 settingsService : SettingsService ;
21+ goalsService : GoalsService ;
1922 private foodHighlightExtension : FoodHighlightExtension ;
23+ private goalsHighlightExtension : GoalsHighlightExtension ;
2024
2125 async onload ( ) {
2226 await this . loadSettings ( ) ;
@@ -28,14 +32,18 @@ export default class FoodTrackerPlugin extends Plugin {
2832 this . settingsService = new SettingsService ( ) ;
2933 this . settingsService . initialize ( this . settings ) ;
3034
35+ this . goalsService = new GoalsService ( this . app , this . settings . goalsFile || "" ) ;
36+ // Delay goals loading until vault is ready
37+ this . app . workspace . onLayoutReady ( ( ) => this . goalsService . loadGoals ( ) ) ;
38+
3139 // Register food autocomplete
3240 this . foodSuggest = new FoodSuggest ( this . app , this . settingsService , this . nutrientCache ) ;
3341 this . registerEditorSuggest ( this . foodSuggest ) ;
3442
3543 // Initialize nutrition total
3644 this . nutritionTotal = new NutritionTotal ( this . nutrientCache ) ;
3745 this . statusBarItem = this . addStatusBarItem ( ) ;
38- this . statusBarItem . setText ( "" ) ;
46+ this . statusBarItem . innerHTML = "" ;
3947
4048 // Initialize document total manager
4149 this . documentTotalManager = new DocumentTotalManager ( ) ;
@@ -112,6 +120,15 @@ export default class FoodTrackerPlugin extends Plugin {
112120 } )
113121 ) ;
114122
123+ this . registerEvent (
124+ this . app . vault . on ( "modify" , file => {
125+ if ( file . path === this . settings . goalsFile || file . name === this . settings . goalsFile ) {
126+ void this . goalsService . loadGoals ( ) ;
127+ void this . updateNutritionTotal ( ) ;
128+ }
129+ } )
130+ ) ;
131+
115132 // Update nutrition total when active file changes
116133 this . registerEvent (
117134 this . app . workspace . on ( "active-leaf-change" , ( ) => {
@@ -123,6 +140,10 @@ export default class FoodTrackerPlugin extends Plugin {
123140 this . foodHighlightExtension = new FoodHighlightExtension ( this . settingsService ) ;
124141 this . registerEditorExtension ( this . foodHighlightExtension . createExtension ( ) ) ;
125142
143+ // Register CodeMirror extension for goals highlighting
144+ this . goalsHighlightExtension = new GoalsHighlightExtension ( this . settingsService ) ;
145+ this . registerEditorExtension ( this . goalsHighlightExtension . createExtension ( ) ) ;
146+
126147 // Initial total update
127148 void this . updateNutritionTotal ( ) ;
128149 }
@@ -132,6 +153,9 @@ export default class FoodTrackerPlugin extends Plugin {
132153 if ( this . foodHighlightExtension ) {
133154 this . foodHighlightExtension . destroy ( ) ;
134155 }
156+ if ( this . goalsHighlightExtension ) {
157+ this . goalsHighlightExtension . destroy ( ) ;
158+ }
135159 this . foodSuggest ?. suggestionCore ?. destroy ( ) ;
136160 }
137161
@@ -150,6 +174,13 @@ export default class FoodTrackerPlugin extends Plugin {
150174 this . nutritionTotal = new NutritionTotal ( this . nutrientCache ) ;
151175 }
152176
177+ if ( this . goalsService ) {
178+ this . goalsService . setGoalsFile ( this . settings . goalsFile || "" ) ;
179+ await this . goalsService . loadGoals ( ) ;
180+ }
181+
182+ // Goals highlighting extension automatically updates via SettingsService subscription
183+
153184 // Update settings service
154185 if ( this . settingsService ) {
155186 this . settingsService . updateSettings ( this . settings ) ;
@@ -175,14 +206,15 @@ export default class FoodTrackerPlugin extends Plugin {
175206 const totalText = this . nutritionTotal . calculateTotalNutrients (
176207 content ,
177208 this . settingsService . currentEscapedFoodTag ,
178- true
209+ true ,
210+ this . goalsService . currentGoals
179211 ) ;
180212
181213 if ( this . settings . totalDisplayMode === "status-bar" ) {
182- this . statusBarItem ?. setText ( totalText ) ;
214+ if ( this . statusBarItem ) this . statusBarItem . innerHTML = totalText ;
183215 this . documentTotalManager . remove ( ) ;
184216 } else {
185- this . statusBarItem ?. setText ( "" ) ;
217+ if ( this . statusBarItem ) this . statusBarItem . innerHTML = "" ;
186218 this . documentTotalManager . show ( totalText , activeView ) ;
187219 }
188220 } catch ( error ) {
@@ -192,7 +224,7 @@ export default class FoodTrackerPlugin extends Plugin {
192224 }
193225
194226 private clearTotal ( ) : void {
195- this . statusBarItem ?. setText ( "" ) ;
227+ if ( this . statusBarItem ) this . statusBarItem . innerHTML = "" ;
196228 this . documentTotalManager . remove ( ) ;
197229 }
198230}
0 commit comments