mirror of
https://github.com/al0cam/AutoMover.git
synced 2026-07-22 05:41:25 +00:00
added feature to move all files manually
This commit is contained in:
parent
f4f7fc24f7
commit
4b35d50467
3 changed files with 56 additions and 19 deletions
|
|
@ -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
|
||||
//
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
53
main.ts
53
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(
|
||||
{},
|
||||
|
|
|
|||
Loading…
Reference in a new issue