diff --git a/components/Settings.ts b/components/Settings.ts new file mode 100644 index 0000000..9fbbeae --- /dev/null +++ b/components/Settings.ts @@ -0,0 +1,89 @@ +import RedirectPlugin from "../main"; + +import { App, PluginSettingTab, Setting } from "obsidian"; + +export default class RedirectSettingsTab extends PluginSettingTab { + plugin: RedirectPlugin; + showViewActiveTabSetting: boolean; + + constructor(app: App, plugin: RedirectPlugin) { + super(app, plugin); + this.plugin = plugin; + this.showViewActiveTabSetting = false; + } + + display(): void { + const { containerEl } = this; + + containerEl.empty(); + + new Setting(containerEl) + .setName("Automatic Redirect to Synonym") + .setDesc("Whether to automatically redirect to the target note.") + .addToggle((toggle) => { + toggle.setValue(true); + toggle.setTooltip( + "Automatically redirects you to the target note." + ); + toggle.onChange((value) => { + console.log(value); + if (value) { + console.log("true"); + } else { + console.log("false"); + } + }); + }); + + new Setting(containerEl) + .setName("Open target note in new tab.") + .setDesc("Whether to open the target note in a new tab.") + .addToggle((toggle) => { + toggle.setValue(true); + // toggle.setTooltip(""); + toggle.onChange((value) => { + // console.log(value); + if (value) { + console.log("Yes, open target note in new tab."); + this.showViewActiveTabSetting = true; + // show the next setting: a toggle whether to automatically change view to the new tab + viewActiveTabSetting(true); + } else { + console.log("No, open target note in same tab."); + this.showViewActiveTabSetting = false; + viewActiveTabSetting(false); + + // hide the next setting: a toggle whether to automatically change view to the new tab. Also make sure to DISABLE THE DEPENDENT OPTION - whether to switch to the new tab + } + }); + }); + + // if (this.showViewActiveTabSetting) { + function viewActiveTabSetting(show: boolean) { + console.log("in viewActiveTabSetting"); + if (!show) { + // hide existing tab + return; + } + 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.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 + } else { + console.log("No, switch to new tab."); + // hide the next setting: a toggle whether to automatically change view to the new tab + } + }); + }); + } + } +} diff --git a/main.ts b/main.ts index aae4a16..885c4c6 100644 --- a/main.ts +++ b/main.ts @@ -1,15 +1,6 @@ -import { - App, - Editor, - FileView, - getLinkpath, - MarkdownView, - Modal, - Plugin, - PluginSettingTab, - Setting, - TFile, -} from "obsidian"; +import { getLinkpath, MarkdownView, Plugin } from "obsidian"; + +import RedirectSettingsTab from "components/Settings"; interface RedirectSettings { mySetting: string; @@ -22,43 +13,34 @@ const DEFAULT_SETTINGS: RedirectSettings = { export default class RedirectPlugin extends Plugin { settings: RedirectSettings; - addRedirectCount(): string { - const markdownView = - this.app.workspace.getActiveViewOfType(MarkdownView); - // console.log(markdownView); - if (!markdownView) { - return ""; - } - return "0 redirects"; - } - async onload() { await this.loadSettings(); + this.addSettingTab(new RedirectSettingsTab(this.app, this)); + this.app.workspace.on("active-leaf-change", (leaf) => { console.log("active-leaf-change: ", leaf); - this.test(); + this.redirect(false, false); }); // This adds a settings tab so the user can configure various aspects of the plugin - this.addSettingTab(new RedirectSettingsTab(this.app, this)); this.app.workspace.onLayoutReady(() => { - // this.getTargetFile(); - this.test(); + this.redirect(false, false); }); } - test() { - const currentFile = this.app.workspace.getActiveFile(); - console.log("currentFile: ", currentFile); + + redirect(newTab: boolean, viewNewTab: boolean) { + // const currentFile = this.app.workspace.getActiveFile(); + // console.log("currentFile: ", currentFile); const currentMdView = this.app.workspace.getActiveViewOfType(MarkdownView); - console.log( - "currentMdView.getViewData: ", - currentMdView?.getViewData() - ); + // console.log( + // "currentMdView.getViewData: ", + // currentMdView?.getViewData() + // ); const linkTextRegex = /::>\[\[(.*[\w\s]*)\]\]/i; const targetNoteName = currentMdView @@ -66,23 +48,23 @@ export default class RedirectPlugin extends Plugin { .match(linkTextRegex) ?.at(1); - console.log("redirectTargetNote: ", targetNoteName); + // console.log("redirectTargetNote: ", targetNoteName); - if (targetNoteName) { - const targetNotePath = this.app.metadataCache.getFirstLinkpathDest( - getLinkpath(targetNoteName), - "" - ); - console.log("targetNotePath: ", targetNotePath); - this.app.workspace.openLinkText( - targetNoteName || "", - targetNotePath?.path as string - // true, - // { active: true } - ); - } else { - console.log("no redirect found"); + if (!targetNoteName) { + return; } + + const targetNotePath = this.app.metadataCache.getFirstLinkpathDest( + getLinkpath(targetNoteName), + "" + ); + // console.log("targetNotePath: ", targetNotePath); + this.app.workspace.openLinkText( + targetNoteName || "", + targetNotePath?.path as string, + newTab, + { active: viewNewTab } + ); } onunload() {} @@ -98,91 +80,4 @@ export default class RedirectPlugin extends Plugin { async saveSettings() { await this.saveData(this.settings); } - - // getTargetFile(): TFile | null { - // // const currentFile = this.app.workspace.getActiveFile(); - // const targetFile = this.app.vault.getFileByPath("_redirects/js.md"); - // console.log(targetFile); - - // this.app.workspace.openLinkText( - // targetFile?.name as string, - // targetFile?.path as string, - // false, - // { - // active: false, - // } - // ); - // return targetFile; - // } -} - -class RedirectSettingsTab extends PluginSettingTab { - plugin: RedirectPlugin; - - constructor(app: App, plugin: RedirectPlugin) { - super(app, plugin); - this.plugin = plugin; - } - - display(): void { - const { containerEl } = this; - - containerEl.empty(); - - new Setting(containerEl) - .setName("Automatic Redirect to Synonym") - .setDesc("Whether to automatically redirect to the target note.") - .addToggle((toggle) => { - toggle.setValue(true); - toggle.setTooltip("Well whaddaya know..."); - toggle.onChange((value) => { - console.log(value); - if (value) { - console.log("true"); - } else { - console.log("false"); - } - }); - }); - - new Setting(containerEl) - .setName("Open target note in new tab.") - .setDesc("Whether to open the target note in a new tab.") - .addToggle((toggle) => { - toggle.setValue(true); - // toggle.setTooltip(""); - toggle.onChange((value) => { - console.log(value); - if (value) { - console.log("Yes, open target note in new tab."); - // show the next setting: a toggle whether to automatically change view to the new tab - } else { - console.log("No, open target note in same tab."); - // hide the next setting: a toggle whether to automatically change view to the new tab. Also make sure to DISABLE THE DEPENDENT OPTION - whether to switch to the new tab - } - }); - }); - // .clear(); // Clears (ie removes) whatever the setting instance contains (in this case, the toggle switch) - - 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.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 - } else { - console.log("No, switch to new tab."); - // hide the next setting: a toggle whether to automatically change view to the new tab - } - }); - }); - // .clear(); // Clears (ie removes) whatever the setting instance contains (in this case, the toggle switch) - } } diff --git a/tsconfig.json b/tsconfig.json index c44b729..f5facde 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,24 +1,18 @@ { - "compilerOptions": { - "baseUrl": ".", - "inlineSourceMap": true, - "inlineSources": true, - "module": "ESNext", - "target": "ES6", - "allowJs": true, - "noImplicitAny": true, - "moduleResolution": "node", - "importHelpers": true, - "isolatedModules": true, - "strictNullChecks": true, - "lib": [ - "DOM", - "ES5", - "ES6", - "ES7" - ] - }, - "include": [ - "**/*.ts" - ] + "compilerOptions": { + "baseUrl": ".", + "inlineSourceMap": true, + "inlineSources": true, + "module": "ESNext", + "target": "ES6", + "allowJs": true, + // "allowImportingTsExtensions": true, + "noImplicitAny": true, + "moduleResolution": "node", + "importHelpers": true, + "isolatedModules": true, + "strictNullChecks": true, + "lib": ["DOM", "ES5", "ES6", "ES7"] + }, + "include": ["**/*.ts"] }