Skip to content

Commit ecaecb9

Browse files
committed
refactor: improve TypeScript type safety and modernize tsconfig
1 parent 18bb1b9 commit ecaecb9

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

src/main.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* implements the plugin lifecycle methods.
88
*/
99

10-
import { Notice, Plugin, TFile, debounce, moment } from "obsidian";
10+
import { Notice, Plugin, TAbstractFile, TFile, debounce } from "obsidian";
1111

1212
import {
13-
ChangelogSettings,
13+
type ChangelogSettings,
1414
ChangelogSettingsTab,
1515
DEFAULT_SETTINGS,
1616
} from "./settings";
@@ -89,21 +89,33 @@ export default class ChangelogPlugin extends Plugin {
8989
*/
9090
enableAutoUpdate() {
9191
if (this.settings.autoUpdate) {
92-
this.registerEvent(this.app.vault.on("modify", this.onVaultChange));
93-
this.registerEvent(this.app.vault.on("delete", this.onVaultChange));
94-
this.registerEvent(this.app.vault.on("rename", this.onVaultChange));
95-
}
96-
}
92+
// Handler for modify events
93+
this.registerEvent(
94+
this.app.vault.on("modify", (file: TAbstractFile) => {
95+
if (file instanceof TFile) {
96+
this.onVaultChange(file);
97+
}
98+
}),
99+
);
97100

98-
/**
99-
* Disables automatic changelog updates by removing file system event listeners
100-
*
101-
* @see {@link https://github.com/obsidianmd/obsidian-api/blob/master/obsidian.d.ts#L3255|Vault.off}
102-
*/
103-
disableAutoUpdate() {
104-
this.app.vault.off("modify", this.onVaultChange);
105-
this.app.vault.off("delete", this.onVaultChange);
106-
this.app.vault.off("rename", this.onVaultChange);
101+
// Handler for delete events
102+
this.registerEvent(
103+
this.app.vault.on("delete", (file: TAbstractFile) => {
104+
if (file instanceof TFile) {
105+
this.onVaultChange(file);
106+
}
107+
}),
108+
);
109+
110+
// Handler for rename events (has different signature with oldPath parameter)
111+
this.registerEvent(
112+
this.app.vault.on("rename", (file: TAbstractFile) => {
113+
if (file instanceof TFile) {
114+
this.onVaultChange(file);
115+
}
116+
}),
117+
);
118+
}
107119
}
108120

109121
/**
@@ -137,9 +149,9 @@ export default class ChangelogPlugin extends Plugin {
137149

138150
let changelogContent = "";
139151
recentFiles.forEach((file) => {
140-
const formattedTime = moment(file.stat.mtime).format(
141-
this.settings.datetimeFormat,
142-
);
152+
// Use window.moment to prevent TypeScript error with the imported moment
153+
const m = window.moment(file.stat.mtime);
154+
const formattedTime = m.format(this.settings.datetimeFormat);
143155
changelogContent += `- ${formattedTime} · [[${file.basename}]]\n`;
144156
});
145157

src/settings.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
Notice,
1616
PluginSettingTab,
1717
Setting,
18-
moment,
1918
normalizePath,
2019
} from "obsidian";
2120

@@ -127,8 +126,6 @@ export class ChangelogSettingsTab extends PluginSettingTab {
127126
await this.plugin.saveSettings();
128127
if (value) {
129128
this.plugin.enableAutoUpdate();
130-
} else {
131-
this.plugin.disableAutoUpdate();
132129
}
133130
}),
134131
);
@@ -159,7 +156,9 @@ export class ChangelogSettingsTab extends PluginSettingTab {
159156
.onChange(async (format) => {
160157
// Attempt to format current date with the new format string
161158
// Returns "Invalid date" if the format is invalid
162-
const isValid = moment().format(format) !== "Invalid date";
159+
// Use window.moment to prevent TypeScript error
160+
const m = window.moment();
161+
const isValid = m.format(format) !== "Invalid date";
163162

164163
if (!isValid) {
165164
// Revert to previous valid format and notify user

tsconfig.json

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2018",
4-
"moduleResolution": "node",
5-
"lib": ["DOM", "ES2018"],
6-
"strict": true
3+
// Language and Environment
4+
"lib": ["DOM", "ESNext"],
5+
"target": "ESNext",
6+
7+
// Modules
8+
"module": "ESNext",
9+
"moduleResolution": "bundler",
10+
"verbatimModuleSyntax": true,
11+
"esModuleInterop": true,
12+
13+
// Emit
14+
"noEmit": true,
15+
"declaration": false,
16+
17+
// Type Checking
18+
"strict": true,
19+
"noFallthroughCasesInSwitch": true,
20+
"noUncheckedIndexedAccess": true,
21+
22+
// Completeness
23+
"skipLibCheck": true,
24+
"forceConsistentCasingInFileNames": true
725
},
826
"include": ["src/**/*.ts"]
927
}

0 commit comments

Comments
 (0)