Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}),
);
}
}

/**
Expand Down Expand Up @@ -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`;
});

Expand Down
7 changes: 3 additions & 4 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
Notice,
PluginSettingTab,
Setting,
moment,
normalizePath,
} from "obsidian";

Expand Down Expand Up @@ -127,8 +126,6 @@ export class ChangelogSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings();
if (value) {
this.plugin.enableAutoUpdate();
} else {
this.plugin.disableAutoUpdate();
}
}),
);
Expand Down Expand Up @@ -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
Expand Down
26 changes: 22 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}