Add minimal settings

This commit is contained in:
Silvano Cerza 2025-01-05 13:26:22 +01:00
parent 09e1020c2a
commit 7ab945ca80
3 changed files with 97 additions and 0 deletions

View file

@ -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);
}
}

13
src/settings/settings.ts Normal file
View file

@ -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",
};

68
src/settings/tab.ts Normal file
View file

@ -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();
}),
);
});
}
}