generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from turulix/waxxxd/master
Waxxxd/master
- Loading branch information
Showing
7 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ data.json | |
|
||
# Exclude macOS Finder (System Explorer) View States | ||
.DS_Store | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { TFolder, Notice, App } from 'obsidian'; | ||
import FolderIndexPlugin from "../main"; | ||
|
||
export class ContextMenuModule { | ||
// eslint-disable-next-line no-unused-vars | ||
constructor(private app: App, private plugin: FolderIndexPlugin) { | ||
} | ||
|
||
addFolderContextMenu() { | ||
this.app.workspace.on("file-menu", (menu, folder) => { | ||
if (folder instanceof TFolder) { | ||
const indexFileForFolder = this.getIndexFileForFolder(folder.path) | ||
|
||
if (!this.doesIndexFileExistForFolder(indexFileForFolder)) { | ||
menu.addItem((item) => { | ||
item.setTitle("Create Index File") | ||
.setIcon("any-icon") | ||
.onClick(() => this.createIndexFileForFolder(indexFileForFolder)); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private doesIndexFileExistForFolder(fullPath:string): boolean { | ||
return this.app.vault.getAbstractFileByPath(fullPath) != null | ||
} | ||
|
||
private getIndexFileForFolder(path:string): string { | ||
return path + "/" + this.getIndexFileName(path) + ".md"; | ||
} | ||
|
||
private getIndexFileName(path: string) { | ||
return (this.plugin.settings.indexFileUserSpecified) | ||
? this.plugin.settings.indexFilename | ||
: path.split("/").pop() || ""; | ||
} | ||
|
||
private async createIndexFileForFolder(indexFileForFolder: string) { | ||
const filePath = indexFileForFolder.substring(0, indexFileForFolder.lastIndexOf("/")) | ||
try { | ||
// Create a new markdown file | ||
const newFile = await this.app.vault.create(indexFileForFolder, this.plugin.settings.indexFileInitText.replace("{{folder}}", this.getIndexFileForFolder(indexFileForFolder))) | ||
|
||
// Notify the user | ||
new Notice(`File "${newFile.name}" created successfully in folder "${newFile.path}".`); | ||
// eslint-disable-next-line no-console | ||
console.log(`File created at path: ${newFile.path}`); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Failed to create file at path: ${filePath}`, error); | ||
new Notice("Failed to create file. See console for details."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters