From 80193b9b58123281490a94877278a299038c1fb0 Mon Sep 17 00:00:00 2001 From: Lost Paul <70213368+LostPaul@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:15:01 +0200 Subject: [PATCH] Edit overview button & create folder note for every existing folder --- src/excludedFolder.ts | 3 +- src/folderOverview.ts | 14 +++++++- src/modals/confirmCreation.ts | 65 ++++++++++++++++++++++------------- src/settings.ts | 9 ++--- styles.css | 7 +++- 5 files changed, 66 insertions(+), 32 deletions(-) diff --git a/src/excludedFolder.ts b/src/excludedFolder.ts index bd4d232..9e45a5a 100644 --- a/src/excludedFolder.ts +++ b/src/excludedFolder.ts @@ -54,9 +54,10 @@ export class ExcludePattern { export function getExcludedFolder(plugin: FolderNotesPlugin, path: string) { const folderName = plugin.getFolderNameFromPathString(path); const matchedPattern = getExcludedFolderByPattern(plugin, folderName); + console.log(matchedPattern); if (matchedPattern) { return matchedPattern; } - const excludedFolder = getExcludedFolderByPath(plugin, path); + if (excludedFolder?.path === '') { return; } return excludedFolder; } diff --git a/src/folderOverview.ts b/src/folderOverview.ts index a05bc9c..48169e4 100644 --- a/src/folderOverview.ts +++ b/src/folderOverview.ts @@ -1,6 +1,7 @@ import { MarkdownPostProcessorContext, parseYaml, TAbstractFile, TFolder, TFile } from 'obsidian'; import { extractFolderName, getFolderNote } from './functions/folderNoteFunctions'; import FolderNotesPlugin from './main'; +import { FolderOverviewSettings } from './modals/folderOverview'; export type yamlSettings = { title?: string; disableTitle?: boolean; @@ -46,6 +47,7 @@ export function createOverview(plugin: FolderNotesPlugin, source: string, el: HT const disableFileTag = yaml?.disableFileTag || plugin.settings.defaultOverview.disableFileTag || false; const showEmptyFolders = yaml?.showEmptyFolders || plugin.settings.defaultOverview.showEmptyFolders || false; + el.parentElement?.classList.add('folder-overview-container'); const root = el.createEl('div', { cls: 'folder-overview' }); const titleEl = root.createEl('h1', { cls: 'folder-overview-title' }); const ul = root.createEl('ul', { cls: 'folder-overview-list' }); @@ -58,7 +60,6 @@ export function createOverview(plugin: FolderNotesPlugin, source: string, el: HT if (!sourceFile) return; const sourceFolderPath = plugin.getFolderPathFromString(ctx.sourcePath); let sourceFolder: TFolder | undefined; - if (sourceFile.parent instanceof TFolder) { sourceFolder = sourceFile.parent; } else { @@ -78,6 +79,17 @@ export function createOverview(plugin: FolderNotesPlugin, source: string, el: HT if (!includeTypes.includes('folder')) { files = getAllFiles(files, sourceFolderPath, depth); } + if (files.length === 0) { + // create button to edit the overview + const editButton = root.createEl('button', { cls: 'folder-overview-edit-button' }); + editButton.innerText = 'Edit overview'; + editButton.addEventListener('click', (e) => { + e.stopImmediatePropagation(); + e.preventDefault(); + e.stopPropagation(); + new FolderOverviewSettings(plugin.app, plugin, parseYaml(source), ctx, el).open(); + }, { capture: true }); + } files = sortFiles(files, yaml, plugin); if (style === 'grid') { diff --git a/src/modals/confirmCreation.ts b/src/modals/confirmCreation.ts index 5180abc..b13d0d5 100644 --- a/src/modals/confirmCreation.ts +++ b/src/modals/confirmCreation.ts @@ -1,6 +1,8 @@ -import { App, Modal, Setting, TFolder } from 'obsidian'; +import { App, ButtonComponent, Modal, Setting, TFolder } from 'obsidian'; import FolderNotesPlugin from '../main'; import { createFolderNote } from 'src/functions/folderNoteFunctions'; +import { getTemplatePlugins } from 'src/template'; +import { getExcludedFolder } from 'src/excludedFolder'; export default class ConfirmationModal extends Modal { plugin: FolderNotesPlugin; app: App; @@ -11,6 +13,19 @@ export default class ConfirmationModal extends Modal { this.app = app; } onOpen() { + console.log(this.containerEl) + this.modalEl.addClass('fn-confirmation-modal'); + let templateFolderPath: string; + const { templateFolder, templaterPlugin } = getTemplatePlugins(this.plugin.app); + if ((!templateFolder || templateFolder?.trim() === '') && !templaterPlugin) { + templateFolderPath = ''; + } + if (templaterPlugin) { + templateFolderPath = templaterPlugin.plugin?.settings?.templates_folder as string; + } else { + templateFolderPath = templateFolder; + } + const { contentEl } = this; contentEl.createEl('h2', { text: 'Create folder note for every folder' }); const setting = new Setting(contentEl); @@ -18,29 +33,33 @@ export default class ConfirmationModal extends Modal { setting.infoEl.createEl('p', { text: 'This feature will create a folder note for every folder in your vault.' }); setting.infoEl.createEl('p', { text: 'Every folder that already has a folder note will be ignored.' }); setting.infoEl.createEl('p', { text: 'Every excluded folder will be ignored.' }); - setting.infoEl.parentElement?.classList.add('fn-confirmation-modal'); - const button = setting.infoEl.createEl('button', { text: 'Create' }); - button.classList.add('mod-warning', 'fn-confirmation-modal-button'); - button.addEventListener('click', async () => { - this.close(); - this.app.vault.getAllLoadedFiles().forEach(async (folder) => { - if (folder instanceof TFolder) { - const excludedFolder = this.plugin.settings.excludeFolders.find( - (excludedFolder) => (excludedFolder.path === folder.path) || - (excludedFolder.path === folder.path?.slice(0, folder?.path.lastIndexOf('/') >= 0 ? folder.path?.lastIndexOf('/') : folder.path.length) - && excludedFolder.subFolders)); - if (excludedFolder) return; - const path = folder.path + '/' + this.plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name) + '.md'; - if (this.app.vault.getAbstractFileByPath(path)) return; - await createFolderNote(this.plugin, folder.path, true); - } + new Setting(contentEl) + .addButton((cb: ButtonComponent) => { + cb.setButtonText('Create'); + cb.setCta(); + cb.buttonEl.focus(); + cb.onClick(async () => { + this.close(); + const folders = this.app.vault.getAllLoadedFiles().filter((file) => file.parent instanceof TFolder); + for (const folder of folders) { + if (folder instanceof TFolder) { + const excludedFolder = getExcludedFolder(this.plugin, folder.path); + if (excludedFolder) continue; + console.log(folder.path); + if (folder.path === templateFolderPath) continue; + const path = folder.path + '/' + this.plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name) + '.md'; + if (this.app.vault.getAbstractFileByPath(path)) continue; + await createFolderNote(this.plugin, folder.path, true); + } + } + }); + }) + .addButton((cb: ButtonComponent) => { + cb.setButtonText('Cancel'); + cb.onClick(async () => { + this.close(); + }); }); - }); - button.focus(); - const cancelButton = setting.infoEl.createEl('button', { text: 'Cancel' }); - cancelButton.addEventListener('click', async () => { - this.close(); - }); } onClose() { const { contentEl } = this; diff --git a/src/settings.ts b/src/settings.ts index 8377839..c788993 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,7 +4,7 @@ import { TemplateSuggest } from './suggesters/templateSuggester'; import { extractFolderName, getFolderNote } from './functions/folderNoteFunctions'; import { addExcludeFolderListItem, ExcludedFolder, addExcludedFolder, ExcludePattern, addExcludePatternListItem } from './excludedFolder'; import { FrontMatterTitlePluginHandler } from './events/frontMatterTitle'; -// import ConfirmationModal from "./modals/confirmCreation"; +import ConfirmationModal from "./modals/confirmCreation"; import { yamlSettings } from './folderOverview'; export interface FolderNotesSettings { syncFolderName: boolean; @@ -447,10 +447,7 @@ export class SettingsTab extends PluginSettingTab { ); } - // Due to an issue with templater it has been disabled for now - // If you want to try it yourself make a pr - // The issue was that it only used the first folder for all of the other folder notes - /* + new Setting(containerEl) .setName('Create folder note for every folder') .setDesc('Create a folder note for every folder in the vault') @@ -458,10 +455,10 @@ export class SettingsTab extends PluginSettingTab { cb.setIcon('plus'); cb.setTooltip('Create folder notes'); cb.onClick(async () => { + new ConfirmationModal(this.app, this.plugin).open(); }); }); - */ const manageExcluded = new Setting(containerEl) diff --git a/styles.css b/styles.css index b9e0915..aaf4d7d 100644 --- a/styles.css +++ b/styles.css @@ -167,4 +167,9 @@ flex: 1 1 auto; margin-right: 1.200rem; margin-bottom: 1.200rem; -} \ No newline at end of file +} + +.fn-confirmation-modal .setting-item { + border-top: 0 !important; + padding-top: 0 !important; +}