diff --git a/Settings/SettingsTab.ts b/Settings/SettingsTab.ts index 6971dfa..8d9a769 100644 --- a/Settings/SettingsTab.ts +++ b/Settings/SettingsTab.ts @@ -13,9 +13,12 @@ export class SettingsTab extends obsidian.PluginSettingTab { display(): void { const { containerEl } = this; - // maybe not required - containerEl.empty(); + containerEl.createEl("h1", { text: "AutoMover settings" }); + containerEl.createSpan({ + text: "This plugin will move files to a specified folder based on a regex match.", + }); + containerEl.createEl("h2", { text: "Automatic moving" }); new obsidian.Setting(containerEl) .setName("Move on open") .setDesc("Should the file be moved when it is opened?") @@ -26,6 +29,18 @@ export class SettingsTab extends obsidian.PluginSettingTab { }), ); + new obsidian.Setting(containerEl) + .setName("Manually move") + .setDesc( + "Execute the command to go through your notes and move them according to the rules specified below.", + ) + .addButton((button) => { + button.setButtonText("Move files"); + button.onClick(async () => { + this.plugin.goThroughAllFiles(); + }); + }); + // there is no default event to move on save, therefore, i'd need to define a new one // running move on change could be an option, but it would produce an overhead in performance because of constant checking for changes // diff --git a/Utils/MovingUtil.ts b/Utils/MovingUtil.ts index 6b7ef3a..eefd87e 100644 --- a/Utils/MovingUtil.ts +++ b/Utils/MovingUtil.ts @@ -48,9 +48,6 @@ class MovingUtil { * @returns void */ public moveFile(file: obsidian.TFile, newPath: string): void { - console.log(`Moving file ${file.name} to ${newPath}`); - // is folder is saying false for something that is clearly a folder - console.log(`Is folder: ${this.isFolder(newPath)}`); if (this.isFolder(newPath)) { this.app.vault.rename(file, `${newPath}/${file.name}`); } else { diff --git a/main.ts b/main.ts index 9d84931..67696b0 100644 --- a/main.ts +++ b/main.ts @@ -27,26 +27,51 @@ export default class AutoMoverPlugin extends obsidian.Plugin { if (this.settings.moveOnOpen) { this.registerEvent( this.app.workspace.on("file-open", (file: obsidian.TFile) => { - if (file == null || file.path == null) return; - const rule = ruleMatcherUtil.getMatchingRule( - file, - this.settings.movingRules, - ); - if (rule == null || rule.folder == null) return; - if (ruleMatcherUtil.isRegexGrouped(rule)) { - const matches = ruleMatcherUtil.getGroupMatches(file, rule); - const finalDestinationPath = - ruleMatcherUtil.constructFinalDesinationPath(rule, matches!); - movingUtil.moveFile(file, finalDestinationPath); - } else { - movingUtil.moveFile(file, rule.folder); - } + this.matchAndMoveFile(file); }), ); } } } + /** + * Opens all files in the repository so that the even "file-open" is triggered + * + * @returns void + */ + goThroughAllFiles() { + const files = this.app.vault.getFiles(); + for (const file of files) { + this.matchAndMoveFile(file); + } + new obsidian.Notice("All files moved!", 5000); + } + + /** + * Matches the file to the rule and moves it to the destination folder + * + * @param file - File to be matched and moved + * @returns void + */ + matchAndMoveFile(file: obsidian.TFile): void { + if (file == null || file.path == null) return; + const rule = ruleMatcherUtil.getMatchingRule( + file, + this.settings.movingRules, + ); + if (rule == null || rule.folder == null) return; + if (ruleMatcherUtil.isRegexGrouped(rule)) { + const matches = ruleMatcherUtil.getGroupMatches(file, rule); + const finalDestinationPath = ruleMatcherUtil.constructFinalDesinationPath( + rule, + matches!, + ); + movingUtil.moveFile(file, finalDestinationPath); + } else { + movingUtil.moveFile(file, rule.folder); + } + } + async asyncloadSettings() { this.settings = Object.assign( {},