From 6b9d420def11736ed821c0a8015dfe1287d87a0e Mon Sep 17 00:00:00 2001 From: Mike Farr Date: Sat, 29 Mar 2025 08:51:10 -0400 Subject: [PATCH] Allow multiple files to be archived (#3) --- README.md | 5 ++++- main.ts | 47 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d6c141f..39656f1 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ # Simple Archiver for Obsidian -Simple Archiver moves a single file or an entire folder to an archive folder that you specify in the plugin's settings. The item is moved to the same relative path in the archive. If the base archive folder doesn't exist when you archive a file, it will be created automatically. +> _Move old, stinky notes and folders to an archive, where they belong!_ + +Simple Archiver moves files or an entire folder to an archive folder that you specify in the plugin's settings. The items are moved to the same relative path in the archive. If the base archive folder doesn't exist when you send something to the archive, it will be created automatically. Archiving can be done via: - `Simple Archive: Move to archive` command - `Move to archive` file menu item +- `Move all to archive` multi-file menu item ## Known Issues & Limitations diff --git a/main.ts b/main.ts index 76d3c35..1eff7b0 100644 --- a/main.ts +++ b/main.ts @@ -32,7 +32,7 @@ export default class SimpleArchiver extends Plugin { editor: Editor, view: MarkdownView ) => { - let canBeArchived = !view.file?.path.startsWith( + const canBeArchived = !view.file?.path.startsWith( this.settings.archiveFolder ); @@ -60,15 +60,33 @@ export default class SimpleArchiver extends Plugin { item.setTitle("Move to archive") .setIcon("archive") .onClick(async () => { - await this.moveToArchive(file); + if (await this.moveToArchive(file)) { + new Notice(`${file.name} archived`); + } + }); + }); + }) + ); + + this.registerEvent( + this.app.workspace.on("files-menu", (menu, files) => { + menu.addItem((item) => { + item.setTitle("Move all to archive") + .setIcon("archive") + .onClick(async () => { + await this.moveAllToArchive(files); }); }); }) ); } - async moveToArchive(file: TAbstractFile) { - let existingItem = this.app.vault.getAbstractFileByPath( + async moveToArchive(file: TAbstractFile): Promise { + if (file.path.startsWith(this.settings.archiveFolder)) { + return false; + } + + const existingItem = this.app.vault.getAbstractFileByPath( `${this.settings.archiveFolder}/${file.path}` ); @@ -77,12 +95,13 @@ export default class SimpleArchiver extends Plugin { `Unable to archive ${file.name}, item already exists in archive` ); - return; + return false; } - let destinationPath = `${this.settings.archiveFolder}/${file.parent?.path}`; + const destinationPath = `${this.settings.archiveFolder}/${file.parent?.path}`; - let destinationFolder = this.app.vault.getFolderByPath(destinationPath); + const destinationFolder = + this.app.vault.getFolderByPath(destinationPath); if (destinationFolder == null) { await this.app.vault.createFolder(destinationPath); @@ -93,7 +112,19 @@ export default class SimpleArchiver extends Plugin { `${this.settings.archiveFolder}/${file.path}` ); - new Notice(`${file.name} archived`); + return true; + } + + async moveAllToArchive(files: TAbstractFile[]) { + let archived = 0; + + for (const file of files) { + if (await this.moveToArchive(file)) { + archived++; + } + } + + new Notice(`${archived} files archived`); } async loadSettings() {