setting buttons work, changeSettings function in settings

This commit is contained in:
idan.liberman 2024-10-10 22:25:23 +03:00
parent d93a181ece
commit bc528b106b
2 changed files with 38 additions and 36 deletions

View file

@ -5,14 +5,16 @@ import { App, PluginSettingTab, Setting } from "obsidian";
export default class RedirectSettingsTab extends PluginSettingTab {
plugin: RedirectPlugin;
showViewActiveTabSetting: boolean;
openTargetNoteInNewTabState: boolean;
showSwitchToNewTabSetting: boolean;
// openTargetNoteInNewTab: boolean;
// switchToNewTab: boolean;
constructor(app: App, plugin: RedirectPlugin) {
super(app, plugin);
this.plugin = plugin;
this.showViewActiveTabSetting = false;
this.openTargetNoteInNewTabState = false;
// this.openTargetNoteInNewTab = plugin.settings.openInNewTab; // Whether to open target note in new tab
this.showSwitchToNewTabSetting = false; // When the target note opens in a new tab, whether to show the setting that controls automatically switching to the new tab.
// this.switchToNewTab = plugin.settings.switchToNewTab; // Whether to switch to the new tab with the target note
}
display(): void {
@ -24,47 +26,38 @@ export default class RedirectSettingsTab extends PluginSettingTab {
.setName("Open target note in new tab.")
.setDesc("Whether to open the target note in a new tab.")
.addToggle((toggle) => {
toggle.setValue(this.openTargetNoteInNewTabState);
toggle.setValue(this.plugin.settings.openInNewTab);
// toggle.setTooltip("");
toggle.onChange((value) => {
// console.log(value);
if (value) {
console.log("Yes, open target note in new tab.");
this.openTargetNoteInNewTabState = value;
this.showViewActiveTabSetting = true;
// show the next setting: a toggle whether to automatically change view to the new tab
this.display();
this.showSwitchToNewTabSetting = true;
this.plugin.settings.changeSettings(true, false);
} else {
console.log("No, open target note in same tab.");
this.openTargetNoteInNewTabState = false;
this.showViewActiveTabSetting = false;
// viewActiveTabSetting(false);
this.display();
this.showSwitchToNewTabSetting = false;
this.plugin.settings.changeSettings(false, false);
}
this.display();
});
});
// if (!this.showViewActiveTabSetting) {
// return;
// }
this.showViewActiveTabSetting &&
this.showSwitchToNewTabSetting &&
new Setting(containerEl)
.setName("Switch view to new tab.")
.setDesc(
"When the target note opens in a new tab, whether to switch view to the new tab."
)
.addToggle((toggle) => {
toggle.setValue(true);
// toggle.setTooltip("");
toggle.setValue(this.plugin.settings.switchToNewTab);
toggle.onChange((value) => {
console.log(value);
if (value) {
console.log("Yes, switch to new tab.");
// show the next setting: a toggle whether to automatically change view to the new tab
console.log("Yes, switch to the new tab.");
this.plugin.settings.changeSettings(true, true);
} else {
console.log("No, switch to new tab.");
// hide the next setting: a toggle whether to automatically change view to the new tab
console.log("No, don't switch to the new tab.");
this.plugin.settings.changeSettings(true, true);
}
});
});
@ -75,8 +68,8 @@ export default class RedirectSettingsTab extends PluginSettingTab {
"Before uninstalling plugin, manually delete the `_redirects` folder to remove unnecessary files."
)
.addButton((button) => {
console.log("button clicked");
console.log("button: ", button);
// console.log("button clicked");
// console.log("button: ", button);
button.setButtonText("Delete");
button.setTooltip("Delete the Redirects folder.");
button.onClick((evt) => {

27
main.ts
View file

@ -6,20 +6,29 @@ import {
TFolder,
TFile,
} from "obsidian";
// import vault from "components/Vault";
import RedirectSettingsTab from "components/Settings";
interface RedirectSettings {
mySetting: string;
openInNewTab: boolean;
switchToNewTab: boolean;
changeSettings(newTab: boolean, switchToTab: boolean): void;
}
const DEFAULT_SETTINGS: RedirectSettings = {
mySetting: "EDIT",
openInNewTab: false,
switchToNewTab: false,
changeSettings: function (newTab, switchToTab) {
// this is probably a bad idea because it alters properties of the instance as it is being initialized.
this.openInNewTab = newTab;
this.switchToNewTab = switchToTab;
},
};
export default class RedirectPlugin extends Plugin {
settings: RedirectSettings;
redirectsFolder: TFolder | null = null;
openInNewTab = false;
switchToNewTab = false;
async onload() {
await this.loadSettings();
@ -45,16 +54,16 @@ export default class RedirectPlugin extends Plugin {
// If first note is a redirecting note
this.app.workspace.onLayoutReady(() => {
this.redirect(false, false);
this.redirect();
});
// Redirecting on active note change
this.app.workspace.on("active-leaf-change", (leaf) => {
this.redirect(false, false);
this.redirect();
});
}
redirect(newTab: boolean, viewNewTab: boolean) {
redirect() {
const currentMdView =
this.app.workspace.getActiveViewOfType(MarkdownView);
@ -74,10 +83,10 @@ export default class RedirectPlugin extends Plugin {
);
this.app.workspace.openLinkText(
targetNoteName || "",
targetNoteName,
targetNotePath?.path as string,
newTab,
{ active: viewNewTab }
this.settings.openInNewTab,
{ active: this.settings.switchToNewTab }
);
this.moveRedirectNoteToRedirectsFolder();