Skip to content

Commit acc7e77

Browse files
Changes based on Obsidian Bot's feedback to remove casting, fix event to be wrapped with this.registerEvent
1 parent e75ada6 commit acc7e77

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

Diff for: main.ts

+29-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Plugin,
66
PluginSettingTab,
77
Setting,
8+
TAbstractFile,
89
TFile,
910
getIcon,
1011
} from "obsidian";
@@ -70,6 +71,15 @@ const DEFAULT_SETTINGS: CanvasDailyNotePluginSettings = {
7071
skipSunday: false,
7172
};
7273

74+
interface DailyNotePluginOptions {
75+
folder: string;
76+
}
77+
78+
interface DailyNotePlugin {
79+
getDailyNote(): TFile;
80+
options: DailyNotePluginOptions;
81+
}
82+
7383
/**
7484
* This allows a "live-reload" of Obsidian when developing the plugin.
7585
* Any changes to the code will force reload Obsidian.
@@ -83,7 +93,7 @@ if (process.env.NODE_ENV === "development") {
8393

8494
export default class CanvasDailyNotePlugin extends Plugin {
8595
settings: CanvasDailyNotePluginSettings;
86-
dailyNotePlugin: any;
96+
dailyNotePlugin: DailyNotePlugin;
8797

8898
async onload() {
8999
await this.loadSettings();
@@ -97,7 +107,9 @@ export default class CanvasDailyNotePlugin extends Plugin {
97107
this.addSettingTab(new CanvasDailyNotePluginSettingTab(this.app, this));
98108

99109
// 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+
);
101113
}
102114

103115
/**
@@ -154,10 +166,11 @@ export default class CanvasDailyNotePlugin extends Plugin {
154166
}
155167

156168
// 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();
159170

160-
this.addDailyNote(canvas, dailyFile);
171+
if (dailyFile instanceof TFile) {
172+
this.addDailyNote(canvas, dailyFile);
173+
}
161174
});
162175
}
163176
}
@@ -193,23 +206,24 @@ export default class CanvasDailyNotePlugin extends Plugin {
193206
canvas.removeNode(node);
194207
canvas.requestSave();
195208

196-
dailyFile =
197-
(await this.dailyNotePlugin.getDailyNote()) as TFile;
209+
dailyFile = await this.dailyNotePlugin.getDailyNote();
198210

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+
}
205219
}
206220
});
207221
}
208222

209223
/**
210224
* Gets the existing daily note based on the daily notes plugin settings or returns null if it does not exist.
211225
*/
212-
getExistingDailyFile(): TFile | null {
226+
getExistingDailyFile(): TFile | TAbstractFile | null | undefined {
213227
const dailyFolder = this.dailyNotePlugin.options.folder;
214228
const expectedNotePath = `${dailyFolder}/${new Date().getFullYear()}-${String(
215229
new Date().getMonth() + 1
@@ -219,7 +233,7 @@ export default class CanvasDailyNotePlugin extends Plugin {
219233
)}.md`;
220234
let dailyFile = this.app.vault
221235
.getAllLoadedFiles()
222-
.find((file) => file.path === expectedNotePath) as TFile;
236+
.find((file) => file.path === expectedNotePath);
223237

224238
return dailyFile;
225239
}

0 commit comments

Comments
 (0)