From ba970a4597180626c471588d9338c2e13e0bc2a3 Mon Sep 17 00:00:00 2001 From: Adam Yarris Date: Wed, 24 Apr 2024 20:33:17 -0400 Subject: [PATCH] Added reverse alphabetical order for files/headers --- src/models/PluginSettingsTab.ts | 56 +++++++++++++++++++++++-------- src/types/MarkdownTextRenderer.ts | 9 +++-- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/models/PluginSettingsTab.ts b/src/models/PluginSettingsTab.ts index eeffc84..5cfe426 100644 --- a/src/models/PluginSettingsTab.ts +++ b/src/models/PluginSettingsTab.ts @@ -1,6 +1,12 @@ import {App, PluginSettingTab, Setting, TextAreaComponent} from "obsidian"; import FolderIndexPlugin from "../main"; +export enum SortBy { + None = "Disabled", + Alphabetically = "Alphabetically", + ReverseAlphabetically = "Reverse Alphabetically" +} + export interface PluginSetting { graphOverwrite: boolean; //skipFirstHeadline: boolean; @@ -12,8 +18,8 @@ export interface PluginSetting { autoRenameIndexFile: boolean; hideIndexFiles: boolean; autoPreviewMode: boolean; - sortIndexFilesAlphabetically: boolean; - sortHeadersAlphabetically: boolean; + sortIndexFiles: SortBy; + sortHeaders: SortBy; recursiveIndexFiles: boolean; renderFolderBold: boolean; renderFolderItalic: boolean; @@ -34,8 +40,8 @@ export const DEFAULT_SETTINGS: PluginSetting = { hideIndexFiles: false, indexFileInitText: "---\ntags: MOCs\n---\n```folder-index-content\n```", autoPreviewMode: false, - sortIndexFilesAlphabetically: true, - sortHeadersAlphabetically: false, + sortIndexFiles: SortBy.Alphabetically, + sortHeaders: SortBy.None, recursiveIndexFiles: false, renderFolderBold: true, renderFolderItalic: false, @@ -214,20 +220,42 @@ export class PluginSettingsTab extends PluginSettingTab { new Setting(containerEl) .setName("Sort Files Alphabetically") .setDesc("This will sort the Files alphabetically") - .addToggle(component => component.setValue(this.plugin.settings.sortIndexFilesAlphabetically) - .onChange(async (value) => { - this.plugin.settings.sortIndexFilesAlphabetically = value - await this.plugin.saveSettings() - })) + .addDropdown(component => { + for (const key in SortBy) { + if (!isNaN(Number(key))) continue; + + const enumKey: SortBy = SortBy[key as keyof typeof SortBy]; + const enumValue: string = SortBy[key as keyof typeof SortBy]; + + component.addOption(enumKey, enumValue); + } + + component.setValue(this.plugin.settings.sortIndexFiles) + .onChange(async (value) => { + this.plugin.settings.sortIndexFiles = value as SortBy + await this.plugin.saveSettings() + }) + }) new Setting(containerEl) .setName("Sort Headers Alphabetically") .setDesc("This will sort the Headers within a file alphabetically") - .addToggle(component => component.setValue(this.plugin.settings.sortHeadersAlphabetically) - .onChange(async (value) => { - this.plugin.settings.sortHeadersAlphabetically = value - await this.plugin.saveSettings() - })) + .addDropdown(component => { + for (const key in SortBy) { + if (!isNaN(Number(key))) continue; + + const enumKey: SortBy = SortBy[key as keyof typeof SortBy]; + const enumValue: string = SortBy[key as keyof typeof SortBy]; + + component.addOption(enumKey, enumValue); + } + + component.setValue(this.plugin.settings.sortHeaders) + .onChange(async (value) => { + this.plugin.settings.sortHeaders = value as SortBy + await this.plugin.saveSettings() + }) + }) new Setting(containerEl) .setName("Build IndexFiles Recursively") diff --git a/src/types/MarkdownTextRenderer.ts b/src/types/MarkdownTextRenderer.ts index 7541bb5..1a1033e 100644 --- a/src/types/MarkdownTextRenderer.ts +++ b/src/types/MarkdownTextRenderer.ts @@ -1,6 +1,7 @@ import {App, HeadingCache, TAbstractFile, TFile, TFolder} from "obsidian"; import FolderIndexPlugin from "../main"; import {isExcludedPath, isIndexFile} from "./Utilities"; +import {SortBy} from "../models/PluginSettingsTab"; type FileTree = (TFile | TFolder)[] type HeaderWrapper = { @@ -71,8 +72,10 @@ export class MarkdownTextRenderer { private buildHeaderMarkdownText(file: TFile, headerTree: HeaderWrapper[], indentLevel: number, headlineLevel: number): string { let markdownText = "" - if (this.plugin.settings.sortHeadersAlphabetically) { + if (this.plugin.settings.sortHeaders === SortBy.Alphabetically) { headerTree.sort((a, b) => a.header.heading.localeCompare(b.header.heading)) + } else if (this.plugin.settings.sortHeaders === SortBy.ReverseAlphabetically) { + headerTree.sort((a, b) => b.header.heading.localeCompare(a.header.heading)) } for (const headerWrapper of headerTree) { @@ -192,8 +195,10 @@ export class MarkdownTextRenderer { fileTree.push(file) } } - if (this.plugin.settings.sortIndexFilesAlphabetically) { + if (this.plugin.settings.sortIndexFiles === SortBy.Alphabetically) { fileTree.sort((a, b) => a.name.localeCompare(b.name)) + } else if (this.plugin.settings.sortIndexFiles === SortBy.ReverseAlphabetically) { + fileTree.sort((a, b) => b.name.localeCompare(a.name)) } return fileTree }