From 701425ba0269fa30728cac5ad810012a41ebad97 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 30 Nov 2024 14:42:34 +0000 Subject: [PATCH] Add plugin settings --- plugin/src/settings/settings-tab.ts | 97 +++++++++++++++++++++++++++++ plugin/src/settings/settings.ts | 51 +++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 plugin/src/settings/settings-tab.ts create mode 100644 plugin/src/settings/settings.ts diff --git a/plugin/src/settings/settings-tab.ts b/plugin/src/settings/settings-tab.ts new file mode 100644 index 00000000..cf60f31c --- /dev/null +++ b/plugin/src/settings/settings-tab.ts @@ -0,0 +1,97 @@ +import { + App, + Editor, + MarkdownView, + Modal, + Notice, + Plugin, + PluginSettingTab, + Setting, +} from "obsidian"; + +import SyncPlugin from "src/plugin.js"; +import { SettingsContainer } from "./settings"; + +export class SyncSettingsTab extends PluginSettingTab { + plugin: SyncPlugin; + + constructor( + app: App, + plugin: SyncPlugin, + private settingsContainer: SettingsContainer + ) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const { containerEl } = this; + + containerEl.empty(); + + new Setting(containerEl) + .setName("Remote URL") + .setDesc("Your server's URL") + .setTooltip( + "This is the URL of the server you want to sync with, todo, links to docs" + ) + .addText((text) => + text + .setPlaceholder("https://example.com:8080/obsidian") + .setValue(this.settingsContainer.getSettings().remoteUri) + .onChange((value) => + this.settingsContainer.setSetting("remoteUri", value) + ) + ) + .addButton((button) => button.setButtonText("Test Connection")); + + new Setting(containerEl) + .setName("Access token") + .setDesc( + "Set the access token for the server that you can get from the server" + ) + .setTooltip("todo, links to docs") + .addTextArea((text) => + text + .setPlaceholder("ey...") + .setValue(this.settingsContainer.getSettings().token) + .onChange((value) => + this.settingsContainer.setSetting("token", value) + ) + ); + + new Setting(containerEl) + .setName("Full scan interval (seconds)") + .setDesc( + "How often would you like to do a full scan of the local files" + ) + .setTooltip("todo, links to docs") + .addToggle((toggle) => + toggle + .setValue( + this.settingsContainer.getSettings().fullScanEnabled + ) + .onChange((value) => + this.settingsContainer.setSetting( + "fullScanEnabled", + value + ) + ) + ) + .addSlider((text) => + text + .setLimits(1, 3600, 1) + .setDynamicTooltip() + .setValue( + this.settingsContainer.getSettings() + .fullScanIntervalInSeconds + ) + .onChange((value) => + this.settingsContainer.setSetting( + "fullScanIntervalInSeconds", + value + ) + ) + ); + } +} diff --git a/plugin/src/settings/settings.ts b/plugin/src/settings/settings.ts new file mode 100644 index 00000000..c509cf3a --- /dev/null +++ b/plugin/src/settings/settings.ts @@ -0,0 +1,51 @@ +import SyncPlugin from "src/plugin"; + +export interface SyncSettings { + remoteUri: string; + token: string; + fullScanIntervalInSeconds: number; + fullScanEnabled: boolean; +} + +export const DEFAULT_SETTINGS: SyncSettings = { + remoteUri: "", + token: "", + fullScanIntervalInSeconds: 60, + fullScanEnabled: true, +}; + +export class SettingsContainer { + private _settings: SyncSettings; + + private onChangeHandlers: Array<(settings: SyncSettings) => void> = []; + + public constructor( + private plugin: SyncPlugin, + private loadedSettings: any + ) { + this._settings = Object.assign({}, DEFAULT_SETTINGS, loadedSettings); + } + + public onChange(handler: (settings: SyncSettings) => void) { + this.onChangeHandlers.push(handler); + } + + public getSettings(): SyncSettings { + return this._settings; + } + + public async setSettings(value: SyncSettings): Promise { + this._settings = value; + await this.plugin.saveData(value); + this.onChangeHandlers.forEach((handler) => handler(value)); + } + + public async setSetting( + key: T, + value: SyncSettings[T] + ): Promise { + this._settings[key] = value; + await this.plugin.saveData(value); + this.onChangeHandlers.forEach((handler) => handler(this._settings)); + } +}