Last active
May 15, 2023 14:28
-
-
Save pragmat1c1/96250f0b7753b1eaa21ba13cd39045cd to your computer and use it in GitHub Desktop.
Obsidian plugin for opening a modal to show, filter, jump to existing folder in Vault.
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
// Updated version | |
// v5 | |
// Monday, 15. May 2023 16:27 | |
// Open modal for searching folder names, and then revealing a folder in file explorer once return is hit. | |
import { Plugin, TFolder, Notice, FuzzySuggestModal, App, FileSystemAdapter } from 'obsidian'; | |
class FolderSuggestModal extends FuzzySuggestModal<string> { | |
folderPaths: string[]; | |
constructor(app: App, folderPaths: string[]) { | |
super(app); | |
this.folderPaths = folderPaths; | |
this.inputEl.setAttribute('placeholder', 'Type to filter folders...'); | |
this.setPlaceholder("Type to filter folders..."); | |
} | |
onOpen() { | |
super.onOpen(); | |
this.inputEl.dispatchEvent(new InputEvent('input')); | |
} | |
getItems(): string[] { | |
const inputValue = this.inputEl.value.toLowerCase(); | |
// Searching for `pm resources books` | |
// should filter for items hat contain the keywords `pm` and `resources` and`books`. | |
return this.folderPaths.filter(folderPath => { | |
const folderPathLower = folderPath.toLowerCase(); | |
const keywords = inputValue.toLowerCase().split(' '); | |
// Check if all keywords are included in the folderPath | |
return keywords.every(keyword => folderPathLower.includes(keyword)); | |
}); | |
} | |
getItemText(item: string): string { | |
return item; | |
} | |
onChooseItem(item: string): void { | |
const fileExplorer = this.app.workspace.getLeavesOfType("file-explorer")[0]?.view; | |
if (fileExplorer && this.app.vault.adapter instanceof FileSystemAdapter) { | |
this.app.vault.adapter.exists(item, true).then(exists => { | |
if (exists) { | |
const folder = this.app.vault.getAbstractFileByPath(item) as TFolder; | |
// Reveal the folder path in file explorer | |
this.app.internalPlugins.getEnabledPluginById('file-explorer').revealInFolder(folder); | |
} else { | |
new Notice("Selected folder not found."); | |
} | |
}); | |
} else { | |
new Notice("File explorer not found. Please enable the file explorer core plugin."); | |
} | |
} | |
} | |
export default class JumpToFolderPlugin extends Plugin { | |
async onload() { | |
this.addCommand({ | |
id: "jump-to-folder", | |
name: "Jump to Folder", | |
callback: () => this.jumpToFolder(), | |
}); | |
} | |
async jumpToFolder() { | |
const rootFolder = this.app.vault.getAbstractFileByPath('/'); | |
const folderPaths = this.getFolderPaths(rootFolder as TFolder); | |
//console.log(folderPaths); | |
new FolderSuggestModal(this.app, folderPaths).open(); | |
} | |
getFolderPaths(folder: TFolder): string[] { | |
const folderPaths = [folder.path]; | |
folder.children.forEach(child => { | |
if (child instanceof TFolder) { | |
folderPaths.push(...this.getFolderPaths(child)); | |
} | |
}); | |
return folderPaths; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment