|
7 | 7 | * implements the plugin lifecycle methods. |
8 | 8 | */ |
9 | 9 |
|
10 | | -import { Notice, Plugin, TFile, debounce, moment } from "obsidian"; |
| 10 | +import { Notice, Plugin, TAbstractFile, TFile, debounce } from "obsidian"; |
11 | 11 |
|
12 | 12 | import { |
13 | | - ChangelogSettings, |
| 13 | + type ChangelogSettings, |
14 | 14 | ChangelogSettingsTab, |
15 | 15 | DEFAULT_SETTINGS, |
16 | 16 | } from "./settings"; |
@@ -89,21 +89,33 @@ export default class ChangelogPlugin extends Plugin { |
89 | 89 | */ |
90 | 90 | enableAutoUpdate() { |
91 | 91 | 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 | + ); |
97 | 100 |
|
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 | + } |
107 | 119 | } |
108 | 120 |
|
109 | 121 | /** |
@@ -137,9 +149,9 @@ export default class ChangelogPlugin extends Plugin { |
137 | 149 |
|
138 | 150 | let changelogContent = ""; |
139 | 151 | 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); |
143 | 155 | changelogContent += `- ${formattedTime} · [[${file.basename}]]\n`; |
144 | 156 | }); |
145 | 157 |
|
|
0 commit comments