5
5
Plugin ,
6
6
PluginSettingTab ,
7
7
Setting ,
8
+ TAbstractFile ,
8
9
TFile ,
9
10
getIcon ,
10
11
} from "obsidian" ;
@@ -70,6 +71,15 @@ const DEFAULT_SETTINGS: CanvasDailyNotePluginSettings = {
70
71
skipSunday : false ,
71
72
} ;
72
73
74
+ interface DailyNotePluginOptions {
75
+ folder : string ;
76
+ }
77
+
78
+ interface DailyNotePlugin {
79
+ getDailyNote ( ) : TFile ;
80
+ options : DailyNotePluginOptions ;
81
+ }
82
+
73
83
/**
74
84
* This allows a "live-reload" of Obsidian when developing the plugin.
75
85
* Any changes to the code will force reload Obsidian.
@@ -83,7 +93,7 @@ if (process.env.NODE_ENV === "development") {
83
93
84
94
export default class CanvasDailyNotePlugin extends Plugin {
85
95
settings : CanvasDailyNotePluginSettings ;
86
- dailyNotePlugin : any ;
96
+ dailyNotePlugin : DailyNotePlugin ;
87
97
88
98
async onload ( ) {
89
99
await this . loadSettings ( ) ;
@@ -97,7 +107,9 @@ export default class CanvasDailyNotePlugin extends Plugin {
97
107
this . addSettingTab ( new CanvasDailyNotePluginSettingTab ( this . app , this ) ) ;
98
108
99
109
// Hook into the file open event
100
- this . app . workspace . on ( "file-open" , this . handleFileOpen . bind ( this ) ) ;
110
+ this . registerEvent (
111
+ this . app . workspace . on ( "file-open" , this . handleFileOpen . bind ( this ) )
112
+ ) ;
101
113
}
102
114
103
115
/**
@@ -154,10 +166,11 @@ export default class CanvasDailyNotePlugin extends Plugin {
154
166
}
155
167
156
168
// This will either get the existing note or create a new one. Either way, returns the file.
157
- dailyFile =
158
- ( await this . dailyNotePlugin . getDailyNote ( ) ) as TFile ;
169
+ dailyFile = await this . dailyNotePlugin . getDailyNote ( ) ;
159
170
160
- this . addDailyNote ( canvas , dailyFile ) ;
171
+ if ( dailyFile instanceof TFile ) {
172
+ this . addDailyNote ( canvas , dailyFile ) ;
173
+ }
161
174
} ) ;
162
175
}
163
176
}
@@ -193,23 +206,24 @@ export default class CanvasDailyNotePlugin extends Plugin {
193
206
canvas . removeNode ( node ) ;
194
207
canvas . requestSave ( ) ;
195
208
196
- dailyFile =
197
- ( await this . dailyNotePlugin . getDailyNote ( ) ) as TFile ;
209
+ dailyFile = await this . dailyNotePlugin . getDailyNote ( ) ;
198
210
199
- this . addDailyNote ( canvas , dailyFile , {
200
- x : node . x ,
201
- y : node . y ,
202
- width : node . width ,
203
- height : node . height ,
204
- } ) ;
211
+ if ( dailyFile instanceof TFile ) {
212
+ this . addDailyNote ( canvas , dailyFile , {
213
+ x : node . x ,
214
+ y : node . y ,
215
+ width : node . width ,
216
+ height : node . height ,
217
+ } ) ;
218
+ }
205
219
}
206
220
} ) ;
207
221
}
208
222
209
223
/**
210
224
* Gets the existing daily note based on the daily notes plugin settings or returns null if it does not exist.
211
225
*/
212
- getExistingDailyFile ( ) : TFile | null {
226
+ getExistingDailyFile ( ) : TFile | TAbstractFile | null | undefined {
213
227
const dailyFolder = this . dailyNotePlugin . options . folder ;
214
228
const expectedNotePath = `${ dailyFolder } /${ new Date ( ) . getFullYear ( ) } -${ String (
215
229
new Date ( ) . getMonth ( ) + 1
@@ -219,7 +233,7 @@ export default class CanvasDailyNotePlugin extends Plugin {
219
233
) } .md`;
220
234
let dailyFile = this . app . vault
221
235
. getAllLoadedFiles ( )
222
- . find ( ( file ) => file . path === expectedNotePath ) as TFile ;
236
+ . find ( ( file ) => file . path === expectedNotePath ) ;
223
237
224
238
return dailyFile ;
225
239
}
0 commit comments