From b84361b5190674b33848b59c0d9c5ce32ba93d8f Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Sun, 5 Jan 2025 17:33:14 +0100 Subject: [PATCH] Add more settings --- src/settings/settings.ts | 8 ++ src/settings/tab.ts | 196 +++++++++++++++++++++++++++++---------- 2 files changed, 154 insertions(+), 50 deletions(-) diff --git a/src/settings/settings.ts b/src/settings/settings.ts index e03ebfc..c5bfdb6 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -3,6 +3,10 @@ export interface GitHubSyncSettings { githubOwner: string; githubRepo: string; githubBranch: string; + repoContentPath: string; + localPath: string; + syncStrategy: "save" | "manual" | "interval"; + syncInterval: number; } export const DEFAULT_SETTINGS: GitHubSyncSettings = { @@ -10,4 +14,8 @@ export const DEFAULT_SETTINGS: GitHubSyncSettings = { githubOwner: "", githubRepo: "", githubBranch: "main", + repoContentPath: "", + localPath: "", + syncStrategy: "save", + syncInterval: 1, }; diff --git a/src/settings/tab.ts b/src/settings/tab.ts index 682dfee..3b4eb09 100644 --- a/src/settings/tab.ts +++ b/src/settings/tab.ts @@ -1,39 +1,5 @@ -import { PluginSettingTab, App, Setting } from "obsidian"; +import { PluginSettingTab, App, Setting, TextComponent } from "obsidian"; import GitHubSyncPlugin from "src/main"; -import { GitHubSyncSettings } from "./settings"; - -const SETTINGS_DATA: { - Name: string; - Description: string; - Field: keyof GitHubSyncSettings; - Placeholder: string; -}[] = [ - { - Name: "GitHub Token", - Description: - "A personal access token or a fine-grained token with read and write access to your repository", - Field: "githubToken", - Placeholder: "Token", - }, - { - Name: "Owner", - Description: "Owner of the repository to sync", - Field: "githubOwner", - Placeholder: "Owner", - }, - { - Name: "Repository", - Description: "Name of the repository to sync", - Field: "githubRepo", - Placeholder: "Repository", - }, - { - Name: "Repository branch", - Description: "Branch to sync", - Field: "githubBranch", - Placeholder: "Branch name", - }, -]; export default class GitHubSyncSettingsTab extends PluginSettingTab { plugin: GitHubSyncPlugin; @@ -48,21 +14,151 @@ export default class GitHubSyncSettingsTab extends PluginSettingTab { containerEl.empty(); - containerEl.createEl("h2", { text: "Settings for GitHub Sync" }); + containerEl.createEl("h1", { text: "GitHub Sync Settings" }); - SETTINGS_DATA.forEach((setting) => { - new Setting(containerEl) - .setName(setting.Name) - .setDesc(setting.Description) - .addText((text) => - text - .setPlaceholder(setting.Placeholder) - .setValue(this.plugin.settings[setting.Field]) - .onChange(async (value) => { - this.plugin.settings[setting.Field] = value; - await this.plugin.saveSettings(); - }), - ); - }); + containerEl.createEl("h2", { text: "Repository settings" }); + + let tokenInput: TextComponent; + new Setting(containerEl) + .setName("GitHub Token") + .setDesc( + "A personal access token or a fine-grained token with read and write access to your repository", + ) + .addButton((button) => + button.setIcon("eye-off").onClick((e) => { + if (tokenInput.inputEl.type === "password") { + tokenInput.inputEl.type = "text"; + button.setIcon("eye"); + } else { + tokenInput.inputEl.type = "password"; + button.setIcon("eye-off"); + } + }), + ) + .addText((text) => { + text + .setPlaceholder("Token") + .setValue(this.plugin.settings.githubToken) + .onChange(async (value) => { + this.plugin.settings.githubToken = value; + await this.plugin.saveSettings(); + }).inputEl.type = "password"; + tokenInput = text; + }); + + new Setting(containerEl) + .setName("Owner") + .setDesc("Owner of the repository to sync") + .addText((text) => + text + .setPlaceholder("Owner") + .setValue(this.plugin.settings.githubOwner) + .onChange(async (value) => { + this.plugin.settings.githubOwner = value; + await this.plugin.saveSettings(); + }), + ); + + new Setting(containerEl) + .setName("Repository") + .setDesc("Name of the repository to sync") + .addText((text) => + text + .setPlaceholder("Repository") + .setValue(this.plugin.settings.githubRepo) + .onChange(async (value) => { + this.plugin.settings.githubRepo = value; + await this.plugin.saveSettings(); + }), + ); + + new Setting(containerEl) + .setName("Repository branch") + .setDesc("Branch to sync") + .addText((text) => + text + .setPlaceholder("Branch name") + .setValue(this.plugin.settings.githubBranch) + .onChange(async (value) => { + this.plugin.settings.githubBranch = value; + await this.plugin.saveSettings(); + }), + ); + + containerEl.createEl("h2", { text: "Sync settings" }); + + new Setting(containerEl) + .setName("Repository content path") + .setDesc( + `The path to sync, relative to the repository root. + If not set the whole repository will be synced.`, + ) + .addText((text) => + text + .setPlaceholder("Exaple: blog/content") + .setValue(this.plugin.settings.repoContentPath) + .onChange(async (value) => { + // TODO: Change the local path if already fetched + this.plugin.settings.repoContentPath = value; + await this.plugin.saveSettings(); + }), + ); + + new Setting(containerEl) + .setName("Local path") + .setDesc( + `The local path to sync, relative to the vault root. + Defaults to the repository name if not set.`, + ) + .addText((text) => + text + .setPlaceholder("Exaple: folder/blog-posts") + .setValue(this.plugin.settings.localPath) + .onChange(async (value) => { + // TODO: Move the folder if already fetched + this.plugin.settings.localPath = value; + await this.plugin.saveSettings(); + }), + ); + + const saveStrategies = { + save: "On file save", + manual: "Manually", + interval: "On Interval", + }; + const saveStrategySetting = new Setting(containerEl) + .setName("Sync strategy") + .setDesc("When to sync the local files with the remote repository"); + + let syncInterval = "1"; + if (this.plugin.settings.syncInterval) { + syncInterval = this.plugin.settings.syncInterval.toString(); + } + const intervalSettings = new Setting(containerEl) + .setName("Sync interval") + .setDesc("Sync interval in minutes between automatic synchronizations") + .addText((text) => + text + .setPlaceholder("Interval in minutes") + .setValue(syncInterval) + .onChange(async (value) => { + this.plugin.settings.syncInterval = parseInt(value) || 1; + await this.plugin.saveSettings(); + }), + ); + intervalSettings.setDisabled( + this.plugin.settings.syncStrategy !== "interval", + ); + + saveStrategySetting.addDropdown((dropdown) => + dropdown + .addOptions(saveStrategies) + .setValue(this.plugin.settings.syncStrategy) + .onChange(async (value: keyof typeof saveStrategies) => { + intervalSettings.setDisabled(value !== "interval"); + this.plugin.settings.syncStrategy = value; + await this.plugin.saveSettings(); + }), + ); } }