From 7e7e97fece432aae702a201a4f3bf91ddd9b3e73 Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann Date: Sun, 23 Jun 2024 11:10:47 +0200 Subject: [PATCH] Add setting to ignore 'Append link' when no files are open --- docs/docs/Choices/TemplateChoice.md | 2 + src/engine/TemplateChoiceEngine.ts | 6 ++ .../ChoiceBuilder/templateChoiceBuilder.ts | 101 +++++++----------- src/types/choices/ITemplateChoice.ts | 1 + 4 files changed, 50 insertions(+), 60 deletions(-) diff --git a/docs/docs/Choices/TemplateChoice.md b/docs/docs/Choices/TemplateChoice.md index 54bd361..6992741 100644 --- a/docs/docs/Choices/TemplateChoice.md +++ b/docs/docs/Choices/TemplateChoice.md @@ -21,3 +21,5 @@ If you specify multiple folders, you'll get a suggester asking which of the fold **Open**. Will open the file you've created. By default, it opens in the active pane. If you enable **New tab**, it'll open in a new tab in the direction you specified. ![image](https://user-images.githubusercontent.com/29108628/121773888-3f680980-cb7f-11eb-919b-97d56ef9268e.png) + +**Ignore Append Link When No File Open**. When enabled, the system allows file creation with `Append link` selected in a Template even if no file is currently open, by ignoring the `Append link` option. diff --git a/src/engine/TemplateChoiceEngine.ts b/src/engine/TemplateChoiceEngine.ts index b4da5be..891ac69 100644 --- a/src/engine/TemplateChoiceEngine.ts +++ b/src/engine/TemplateChoiceEngine.ts @@ -21,6 +21,7 @@ import { TemplateEngine } from "./TemplateEngine"; import type { IChoiceExecutor } from "../IChoiceExecutor"; import GenericSuggester from "../gui/GenericSuggester/genericSuggester"; import invariant from "src/utils/invariant"; +import { settingsStore } from "../settingsStore"; // Added import for settingsStore export class TemplateChoiceEngine extends TemplateEngine { public choice: ITemplateChoice; @@ -140,7 +141,12 @@ export class TemplateChoiceEngine extends TemplateEngine { } } + // Check if the setting to ignore append link when no file is open is enabled if (this.choice.appendLink && createdFile) { + if (!this.app.workspace.getActiveFile() && !settingsStore.getState().ignoreAppendLinkWhenNoFileOpen) { + log.logWarning("No active file to append link to."); + return; + } appendToCurrentLine( this.app.fileManager.generateMarkdownLink(createdFile, ""), this.app diff --git a/src/gui/ChoiceBuilder/templateChoiceBuilder.ts b/src/gui/ChoiceBuilder/templateChoiceBuilder.ts index 614c234..cfb92ae 100644 --- a/src/gui/ChoiceBuilder/templateChoiceBuilder.ts +++ b/src/gui/ChoiceBuilder/templateChoiceBuilder.ts @@ -46,6 +46,7 @@ export class TemplateChoiceBuilder extends ChoiceBuilder { this.addFileAlreadyExistsSetting(); this.addOpenFileSetting(); if (this.choice.openFile) this.addOpenFileInNewTabSetting(); + if (this.choice.appendLink) this.addIgnoreAppendLinkWhenNoActiveFileSetting(); // Show setting depending on whether AppendLink is enabled } private addTemplatePathSetting(): void { @@ -285,35 +286,30 @@ export class TemplateChoiceBuilder extends ChoiceBuilder { toggle.setValue(this.choice.setFileExistsBehavior); toggle.onChange((value) => { this.choice.setFileExistsBehavior = value; + this.reload(); }); - }) - .addDropdown((dropdown) => { - dropdown.selectEl.style.marginLeft = "10px"; - - if (!this.choice.fileExistsMode) - this.choice.fileExistsMode = fileExistsDoNothing; - - dropdown - .addOption( - fileExistsAppendToBottom, - fileExistsAppendToBottom - ) - .addOption(fileExistsAppendToTop, fileExistsAppendToTop) - .addOption(fileExistsIncrement, fileExistsIncrement) - .addOption(fileExistsOverwriteFile, fileExistsOverwriteFile) - .addOption(fileExistsDoNothing, fileExistsDoNothing) - .setValue(this.choice.fileExistsMode) - .onChange( - (value: typeof fileExistsChoices[number]) => - (this.choice.fileExistsMode = value) - ); }); + + if (this.choice.setFileExistsBehavior) { + fileAlreadyExistsSetting + .addDropdown((dropdown) => { + dropdown.addOption(fileExistsIncrement, "Increment the file name"); + dropdown.addOption(fileExistsAppendToTop, "Append to the top of the file"); + dropdown.addOption(fileExistsAppendToBottom, "Append to the bottom of the file"); + dropdown.addOption(fileExistsOverwriteFile, "Overwrite the file"); + dropdown.addOption(fileExistsDoNothing, "Nothing"); + dropdown.setValue(this.choice.fileExistsMode); + dropdown.onChange((value: typeof fileExistsChoices[number]) => { + this.choice.fileExistsMode = value; + }); + }); + } } private addOpenFileSetting(): void { - const noOpenSetting: Setting = new Setting(this.contentEl); - noOpenSetting - .setName("Open") + const openFileSetting: Setting = new Setting(this.contentEl); + openFileSetting + .setName("Open File") .setDesc("Open the created file.") .addToggle((toggle) => { toggle.setValue(this.choice.openFile); @@ -321,58 +317,43 @@ export class TemplateChoiceBuilder extends ChoiceBuilder { this.choice.openFile = value; this.reload(); }); - }) - .addDropdown((dropdown) => { - dropdown.selectEl.style.marginLeft = "10px"; - - if (!this.choice.openFileInMode) - this.choice.openFileInMode = "default"; - - dropdown - .addOption("source", "Source") - .addOption("preview", "Preview") - .addOption("default", "Default") - .setValue(this.choice.openFileInMode) - .onChange( - (value) => - (this.choice.openFileInMode = value as FileViewMode) - ); }); } private addOpenFileInNewTabSetting(): void { - const newTabSetting = new Setting(this.contentEl); - newTabSetting - .setName("New split") - .setDesc("Split your editor and open file in new split.") + const openFileInNewTabSetting: Setting = new Setting(this.contentEl); + openFileInNewTabSetting + .setName("Open File in New Tab") + .setDesc("Open the created file in a new tab.") .addToggle((toggle) => { toggle.setValue(this.choice.openFileInNewTab.enabled); - toggle.onChange( - (value) => (this.choice.openFileInNewTab.enabled = value) - ); + toggle.onChange((value) => { + this.choice.openFileInNewTab.enabled = value; + this.reload(); + }); }) .addDropdown((dropdown) => { - dropdown.selectEl.style.marginLeft = "10px"; dropdown.addOption(NewTabDirection.vertical, "Vertical"); dropdown.addOption(NewTabDirection.horizontal, "Horizontal"); dropdown.setValue(this.choice.openFileInNewTab.direction); - dropdown.onChange( - (value) => - (this.choice.openFileInNewTab.direction = < - NewTabDirection - >value) - ); + dropdown.onChange((value: NewTabDirection) => { + this.choice.openFileInNewTab.direction = value; + }); }); + } + private addIgnoreAppendLinkWhenNoActiveFileSetting(): void { new Setting(this.contentEl) - .setName("Focus new pane") - .setDesc("Focus the opened tab immediately after opening") + .setName("Ignore Append Link When No File Open") + .setDesc( + "When enabled, the system allows file creation with 'Append link' selected in a Template even if no file is currently open, by ignoring the 'Append link' option." + ) .addToggle((toggle) => toggle - .setValue(this.choice.openFileInNewTab.focus) - .onChange( - (value) => (this.choice.openFileInNewTab.focus = value) - ) + .setValue(this.choice.ignoreAppendLinkWhenNoActiveFile) + .onChange((value) => { + this.choice.ignoreAppendLinkWhenNoActiveFile = value; + }) ); } } diff --git a/src/types/choices/ITemplateChoice.ts b/src/types/choices/ITemplateChoice.ts index a84706c..2b6a5ed 100644 --- a/src/types/choices/ITemplateChoice.ts +++ b/src/types/choices/ITemplateChoice.ts @@ -23,4 +23,5 @@ export default interface ITemplateChoice extends IChoice { openFileInMode: FileViewMode; fileExistsMode: typeof fileExistsChoices[number]; setFileExistsBehavior: boolean; + ignoreAppendLinkWhenNoActiveFile: boolean; // Added new property }