Skip to content

Commit

Permalink
Add setting to ignore 'Append link' when no files are open
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jun 23, 2024
1 parent e1d4a14 commit 7e7e97f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 60 deletions.
2 changes: 2 additions & 0 deletions docs/docs/Choices/TemplateChoice.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions src/engine/TemplateChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
101 changes: 41 additions & 60 deletions src/gui/ChoiceBuilder/templateChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -285,94 +286,74 @@ 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);
toggle.onChange((value) => {
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;
})
);
}
}
1 change: 1 addition & 0 deletions src/types/choices/ITemplateChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export default interface ITemplateChoice extends IChoice {
openFileInMode: FileViewMode;
fileExistsMode: typeof fileExistsChoices[number];
setFileExistsBehavior: boolean;
ignoreAppendLinkWhenNoActiveFile: boolean; // Added new property
}

0 comments on commit 7e7e97f

Please sign in to comment.