diff --git a/main.ts b/main.ts index 0f21fcea..524ca1f5 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ import { spawnSync } from "child_process"; -import { FileSystemAdapter, Notice, Plugin, PluginSettingTab, Setting, SuggestModal } from "obsidian"; +import { FileSystemAdapter, FuzzySuggestModal, Notice, Plugin, PluginSettingTab, Setting, SuggestModal } from "obsidian"; import simpleGit, { FileStatusResult, SimpleGit } from "simple-git"; enum PluginState { @@ -68,6 +68,15 @@ export default class ObsidianGit extends Plugin { callback: () => new CustomMessageModal(this).open() }); + this.addCommand({ + id: "list-changed-files", + name: "List changed files", + callback: async () => { + const status = await this.git.status(); + new ChangedFilesModal(this, status.files).open(); + } + }); + this.init(); // init statusBar @@ -596,3 +605,41 @@ class CustomMessageModal extends SuggestModal { } } +class ChangedFilesModal extends FuzzySuggestModal { + plugin: ObsidianGit; + changedFiles: FileStatusResult[]; + + constructor(plugin: ObsidianGit, changedFiles: FileStatusResult[]) { + super(plugin.app); + this.plugin = plugin; + this.changedFiles = changedFiles; + console.log(changedFiles); + this.setPlaceholder("Only files in vault can be openend!"); + } + + getItems(): FileStatusResult[] { + return this.changedFiles; + } + + getItemText(item: FileStatusResult): string { + if (item.index == "?" && item.working_dir == "?") { + return `Untracked | ${item.path}`; + } + + let working_dir = ""; + let index = ""; + + if (item.working_dir != " ") working_dir = `Working dir: ${item.working_dir} `; + if (item.index != " ") index = `Index: ${item.index}`; + + return `${working_dir}${index} | ${item.path}`; + } + + onChooseItem(item: FileStatusResult, _: MouseEvent | KeyboardEvent): void { + if (this.plugin.app.metadataCache.getFirstLinkpathDest(item.path, "") == null) { + new Notice("Can't open file in Obsidian"); + } else { + this.plugin.app.workspace.openLinkText(item.path, "/"); + } + } +}