Skip to content

Commit

Permalink
Merge pull request #87 from turulix/waxxxd/master
Browse files Browse the repository at this point in the history
Waxxxd/master
  • Loading branch information
turulix authored Aug 29, 2024
2 parents 343b46c + a53bff5 commit b1e438c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install modules
run: npm i
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ data.json

# Exclude macOS Finder (System Explorer) View States
.DS_Store
yarn.lock
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {GraphManipulatorModule} from "./modules/GraphManipulatorModule";
import {EventEmitter} from "events";
import {DEFAULT_SETTINGS, PluginSetting, PluginSettingsTab} from "./models/PluginSettingsTab";
import {FolderNoteModule} from "./modules/FolderNoteModule";
import {ContextMenuModule} from "./modules/ContextMenuModule";


// Remember to rename these classes and interfaces!
export default class FolderIndexPlugin extends Plugin {
Expand All @@ -17,7 +19,8 @@ export default class FolderIndexPlugin extends Plugin {
eventManager: EventEmitter
oldGraphSetting = false
static PLUGIN: FolderIndexPlugin;

// @ts-ignore
private contextMenuModule: ContextMenuModule;

constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
Expand Down Expand Up @@ -50,6 +53,9 @@ export default class FolderIndexPlugin extends Plugin {
if (this.settings.graphOverwrite) {
this.graphManipulator = new GraphManipulatorModule(this.app, this)
}

this.contextMenuModule = new ContextMenuModule(this.app, this);
this.contextMenuModule.addFolderContextMenu();
}

onSettingsUpdate() {
Expand Down
26 changes: 26 additions & 0 deletions src/models/PluginSettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {App, PluginSettingTab, Setting, TextAreaComponent} from "obsidian";
import FolderIndexPlugin from "../main";

export enum SortBy {
// eslint-disable-next-line no-unused-vars
None = "Disabled",
// eslint-disable-next-line no-unused-vars
Alphabetically = "Alphabetically",
// eslint-disable-next-line no-unused-vars
ReverseAlphabetically = "Reverse Alphabetically"
}

Expand All @@ -27,6 +30,8 @@ export interface PluginSetting {
excludeFolders: string[];
recursionLimit: number;
headlineLimit: number;
indexFileUserSpecified: boolean;
indexFilename: string;
}

export const DEFAULT_SETTINGS: PluginSetting = {
Expand All @@ -49,6 +54,8 @@ export const DEFAULT_SETTINGS: PluginSetting = {
excludeFolders: [],
recursionLimit: -1,
headlineLimit: 6,
indexFileUserSpecified: false,
indexFilename: "!"
}

export class PluginSettingsTab extends PluginSettingTab {
Expand Down Expand Up @@ -144,6 +151,25 @@ export class PluginSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings()
}))

new Setting(containerEl)
.setName("User defined index filename")
.setDesc("This will automatically create an IndexFile with the user defined name")
.addToggle(component => component.setValue(this.plugin.settings.indexFileUserSpecified)
.onChange(async (value) => {
this.plugin.settings.indexFileUserSpecified = value
await this.plugin.saveSettings()
}))

new Setting(containerEl)
.setName("Index filename")
.setDesc("the filename that is used as the folder index")
.addText(component => component.setValue(this.plugin.settings.indexFilename)
.setPlaceholder("!.md")
.onChange(async (value) => {
this.plugin.settings.indexFilename = value
await this.plugin.saveSettings()
}))

new Setting(containerEl)
.setName("Hide IndexFile")
.setDesc("This will hide IndexFiles from the file explorer (Disabled as it causes bugs right now)")
Expand Down
55 changes: 55 additions & 0 deletions src/modules/ContextMenuModule.ts
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.");
}
}
}
12 changes: 10 additions & 2 deletions src/modules/FolderNoteModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export class FolderNoteModule {
return null
}

private indexFilePathOnClick(dataPath: string) {
if (this.plugin.settings.indexFileUserSpecified) {
return dataPath + "/" + this.plugin.settings.indexFilename + ".md"
} else {
const folderName = dataPath.split("/").pop()
return dataPath + "/" + folderName + ".md"
}
}

private async onClick(event: MouseEvent) {
const target = this.getTargetFromEvent(event)
if (target == null)
Expand All @@ -73,8 +82,7 @@ export class FolderNoteModule {
dataPath = dataPathAttribute.value
}

const folderName = dataPath.split("/").pop()
let indexFilePath = dataPath + "/" + folderName + ".md"
let indexFilePath = this.indexFilePathOnClick(dataPath)

// This is the root folder, so we open the root index file
if (indexFilePath == "//.md") {
Expand Down
3 changes: 3 additions & 0 deletions src/types/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export function isIndexFile(path: string) {
if (pathParts.length < 2)
return false
const fileName = pathParts[pathParts.length - 1]
if (FolderIndexPlugin.PLUGIN.settings.indexFileUserSpecified) {
return fileName == FolderIndexPlugin.PLUGIN.settings.indexFilename + ".md";
}
const folderName = pathParts[pathParts.length - 2] + ".md"
return fileName == folderName || fileName == FolderIndexPlugin.PLUGIN.settings.rootIndexFile;
}
Expand Down

0 comments on commit b1e438c

Please sign in to comment.