diff --git a/src/main.ts b/src/main.ts index b79c928..2e01e57 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,27 @@ import { Plugin } from "obsidian"; +import { GitHubSyncSettings, DEFAULT_SETTINGS } from "./settings/settings"; +import GitHubSyncSettingsTab from "./settings/tab"; export default class GitHubSyncPlugin extends Plugin { + settings: GitHubSyncSettings; + async onload() { + await this.loadSettings(); + + this.addSettingTab(new GitHubSyncSettingsTab(this.app, this)); + console.log("GitHubSyncPlugin loaded"); } async onunload() { console.log("GitHubSyncPlugin unloaded"); } + + async loadSettings() { + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + } + + async saveSettings() { + await this.saveData(this.settings); + } } diff --git a/src/settings/settings.ts b/src/settings/settings.ts new file mode 100644 index 0000000..e03ebfc --- /dev/null +++ b/src/settings/settings.ts @@ -0,0 +1,13 @@ +export interface GitHubSyncSettings { + githubToken: string; + githubOwner: string; + githubRepo: string; + githubBranch: string; +} + +export const DEFAULT_SETTINGS: GitHubSyncSettings = { + githubToken: "", + githubOwner: "", + githubRepo: "", + githubBranch: "main", +}; diff --git a/src/settings/tab.ts b/src/settings/tab.ts new file mode 100644 index 0000000..682dfee --- /dev/null +++ b/src/settings/tab.ts @@ -0,0 +1,68 @@ +import { PluginSettingTab, App, Setting } 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; + + constructor(app: App, plugin: GitHubSyncPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const { containerEl } = this; + + containerEl.empty(); + + containerEl.createEl("h2", { text: "Settings for GitHub Sync" }); + + 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(); + }), + ); + }); + } +}