diff --git a/src/main.ts b/src/main.ts index 17c4b9c..4ad7d88 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,10 +7,10 @@ * implements the plugin lifecycle methods. */ -import { Notice, Plugin, TFile, debounce, moment } from "obsidian"; +import { Notice, Plugin, TAbstractFile, TFile, debounce } from "obsidian"; import { - ChangelogSettings, + type ChangelogSettings, ChangelogSettingsTab, DEFAULT_SETTINGS, } from "./settings"; @@ -89,21 +89,33 @@ export default class ChangelogPlugin extends Plugin { */ enableAutoUpdate() { if (this.settings.autoUpdate) { - this.registerEvent(this.app.vault.on("modify", this.onVaultChange)); - this.registerEvent(this.app.vault.on("delete", this.onVaultChange)); - this.registerEvent(this.app.vault.on("rename", this.onVaultChange)); - } - } + // Handler for modify events + this.registerEvent( + this.app.vault.on("modify", (file: TAbstractFile) => { + if (file instanceof TFile) { + this.onVaultChange(file); + } + }), + ); - /** - * Disables automatic changelog updates by removing file system event listeners - * - * @see {@link https://github.com/obsidianmd/obsidian-api/blob/master/obsidian.d.ts#L3255|Vault.off} - */ - disableAutoUpdate() { - this.app.vault.off("modify", this.onVaultChange); - this.app.vault.off("delete", this.onVaultChange); - this.app.vault.off("rename", this.onVaultChange); + // Handler for delete events + this.registerEvent( + this.app.vault.on("delete", (file: TAbstractFile) => { + if (file instanceof TFile) { + this.onVaultChange(file); + } + }), + ); + + // Handler for rename events (has different signature with oldPath parameter) + this.registerEvent( + this.app.vault.on("rename", (file: TAbstractFile) => { + if (file instanceof TFile) { + this.onVaultChange(file); + } + }), + ); + } } /** @@ -137,9 +149,9 @@ export default class ChangelogPlugin extends Plugin { let changelogContent = ""; recentFiles.forEach((file) => { - const formattedTime = moment(file.stat.mtime).format( - this.settings.datetimeFormat, - ); + // Use window.moment to prevent TypeScript error with the imported moment + const m = window.moment(file.stat.mtime); + const formattedTime = m.format(this.settings.datetimeFormat); changelogContent += `- ${formattedTime} ยท [[${file.basename}]]\n`; }); diff --git a/src/settings.ts b/src/settings.ts index e224433..de5b47b 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -15,7 +15,6 @@ import { Notice, PluginSettingTab, Setting, - moment, normalizePath, } from "obsidian"; @@ -127,8 +126,6 @@ export class ChangelogSettingsTab extends PluginSettingTab { await this.plugin.saveSettings(); if (value) { this.plugin.enableAutoUpdate(); - } else { - this.plugin.disableAutoUpdate(); } }), ); @@ -159,7 +156,9 @@ export class ChangelogSettingsTab extends PluginSettingTab { .onChange(async (format) => { // Attempt to format current date with the new format string // Returns "Invalid date" if the format is invalid - const isValid = moment().format(format) !== "Invalid date"; + // Use window.moment to prevent TypeScript error + const m = window.moment(); + const isValid = m.format(format) !== "Invalid date"; if (!isValid) { // Revert to previous valid format and notify user diff --git a/tsconfig.json b/tsconfig.json index 0c72f0a..7244225 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,27 @@ { "compilerOptions": { - "target": "ES2018", - "moduleResolution": "node", - "lib": ["DOM", "ES2018"], - "strict": true + // Language and Environment + "lib": ["DOM", "ESNext"], + "target": "ESNext", + + // Modules + "module": "ESNext", + "moduleResolution": "bundler", + "verbatimModuleSyntax": true, + "esModuleInterop": true, + + // Emit + "noEmit": true, + "declaration": false, + + // Type Checking + "strict": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + + // Completeness + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true }, "include": ["src/**/*.ts"] }