From 0e3ebc79f7a9c9ba70c07c22444c5bb70a73956e Mon Sep 17 00:00:00 2001 From: Carlo Zottmann Date: Mon, 24 Jun 2024 14:21:13 +0200 Subject: [PATCH] [NEW] Adds settings tab Part of ZCO-449 --- src/main.ts | 25 ++++++++++++-- src/settings.ts | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ src/types.d.ts | 4 +++ 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/settings.ts diff --git a/src/main.ts b/src/main.ts index b9d00a1..9385dd8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,21 @@ import { Plugin } from "obsidian"; import { PLUGIN_INFO } from "./plugin-info"; -import type { RealLifeWorkspaceLeaf } from "./types"; +import type { MononoteSettings, RealLifeWorkspaceLeaf } from "./types"; +import { MononoteSettingsTab } from "./settings"; + +const DEFAULT_SETTINGS: MononoteSettings = { + delayInMs: 100, +}; export default class Mononote extends Plugin { private pluginName = `Plugin Mononote v${PLUGIN_INFO.pluginVersion}`; private processors: Map> = new Map(); + settings: MononoteSettings; + async onload() { + await this.loadSettings(); + const { workspace } = this.app; workspace.onLayoutReady(() => { this.registerEvent( @@ -15,12 +24,22 @@ export default class Mononote extends Plugin { console.log(`${this.pluginName} initialized`); }); + + this.addSettingTab(new MononoteSettingsTab(this.app, this)); } onunload() { console.log(`${this.pluginName} unloaded`); } + async loadSettings() { + this.settings = { ...DEFAULT_SETTINGS, ...await this.loadData() }; + } + + async saveSettings() { + await this.saveData(this.settings); + } + private async onActiveLeafChange( activeLeaf: RealLifeWorkspaceLeaf, ): Promise { @@ -135,11 +154,11 @@ export default class Mononote extends Plugin { if (hasEphemeralState) { targetToFocus.setEphemeralState(ephemeralState); } - }, 100); + }, this.settings.delayInMs); // Resolve the promise. resolve(); - }, 100); + }, this.settings.delayInMs); }); } diff --git a/src/settings.ts b/src/settings.ts new file mode 100644 index 0000000..cd0ce42 --- /dev/null +++ b/src/settings.ts @@ -0,0 +1,91 @@ +import { App, PluginSettingTab, Setting, TFolder } from "obsidian"; +import Mononote from "./main"; + +export class MononoteSettingsTab extends PluginSettingTab { + plugin: Mononote; + + private delayOptions: number[] = [ + 100, + 150, + 200, + 300, + 500, + ]; + + constructor(app: App, plugin: Mononote) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const { containerEl, plugin } = this; + + containerEl.empty(); + containerEl.createEl("h2", { text: "Mononote Settings" }); + + const delayOptionsRecord = this.delayOptions + .reduce( + (acc, current) => { + acc[`${current}`] = `${current}ms`; + return acc; + }, + {} as Record, + ); + + // Output format + new Setting(containerEl) + .setName("Delay before applying tab switching rules") + .setDesc(` + Depending on your machine and the size of your vault, Obsidian might need a bit of time before Mononote's tab switching rules can be applied. + + Example: If you load a note N1 in an tab T1, and N1 is already shown in in T2, Mononote should switch to T2. But if you experience Mononote switching to T2, and then immediately back to T1, that means Obsidian needs more time. In that case, try increasing the delay.`) + .addDropdown((dropdown) => { + dropdown + .addOptions(delayOptionsRecord) + .setValue(`${plugin.settings.delayInMs}`) + .onChange( + async (value) => { + plugin.settings.delayInMs = +value; + await plugin.saveSettings(); + this.display(); + }, + ); + }); + + // Sponsoring + const afoURL = + "https://actions.work/actions-for-obsidian?ref=plugin-mononote"; + containerEl.createEl("div", { + attr: { + style: ` + border-radius: 0.5rem; + border: 1px dashed var(--text-muted); + color: var(--text-muted); + display: grid; + font-size: 85%; + grid-gap: 1rem; + grid-template-columns: auto 1fr; + margin-top: 4rem; + opacity: 0.75; + padding: 1rem; + `, + }, + }) + .innerHTML = ` + + Actions for Obsidian icon, a cog wheel on a glossy black background + + + Mononote is brought to you by + Actions for Obsidian, + a macOS/iOS app made by the same developer as this plugin. AFO is the + missing link between Obsidian and macOS / iOS: 50+ Shortcuts + actions to bring your notes and your automations together. + Take a look! + + `; + } +} diff --git a/src/types.d.ts b/src/types.d.ts index d967915..1e17a2e 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,5 +1,9 @@ import { WorkspaceLeaf } from "obsidian"; +export type MononoteSettings = { + delayInMs: number; +}; + export type RealLifeWorkspaceLeaf = WorkspaceLeaf & { activeTime: number; history: {