Allow multiple files to be archived (#3)

This commit is contained in:
Mike Farr 2025-03-29 08:51:10 -04:00 committed by GitHub
parent 9d2e8aa724
commit 6b9d420def
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 9 deletions

View file

@ -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

47
main.ts
View file

@ -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<boolean> {
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() {