chen2226_obsidian-typecho/setting/SettingTab.ts
2025-04-22 15:45:58 +08:00

109 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import plugin from "../main";
import { App, PluginSettingTab, Setting, requestUrl, Notice } from "obsidian";
import { TypechoPluginSettings } from "./PluginSettings";
export class SettingTab extends PluginSettingTab {
plugin: plugin;
settings: TypechoPluginSettings;
constructor(app: App, plugin: plugin, settings: TypechoPluginSettings) {
super(app, plugin);
this.plugin = plugin;
this.settings = settings;
}
async display() {
const { containerEl } = this;
this.initTab(containerEl);
await this.refreshUserList(containerEl);
}
private async initTab(containerEl: HTMLElement): Promise<void> {
containerEl.empty();
new Setting(containerEl)
.setName("Host")
.setDesc("Typecho域名")
.addText((text) =>
text
.setPlaceholder("域名/index.php/api或者配置了伪静态请自行检查")
.setValue(this.settings.Host)
.onChange(async (value) => {
this.settings.Host = value;
await this.plugin.saveSettings();
})
)
.then((setting) => {
setting.controlEl.addEventListener("change", async () => {
await this.refreshUserList(containerEl);
});
});
new Setting(containerEl)
.setName("Token")
.setDesc("插件设置的ApiToken")
.addText((text) =>
text
.setPlaceholder("ApiToken")
.setValue(this.settings.Token)
.onChange(async (value) => {
this.settings.Token = value;
await this.plugin.saveSettings();
})
)
.then((setting) => {
setting.controlEl.addEventListener("change", async () => {
await this.refreshUserList(containerEl);
});
});
}
private async refreshUserList(containerEl: HTMLElement): Promise<void> {
let userList: string | any[] = [];
console.log(this.settings);
if (this.settings.Host && this.settings.Token) {
this.initTab(containerEl);
await requestUrl({
url: this.settings.Host + "/userList",
method: "get",
headers: {
token: this.settings.Token,
"Content-Type": "application/json",
},
})
.then(async (res) => {
console.log(res);
const data = res.json;
if (data.status == "success") {
userList = data.data;
await this.plugin.saveSettings();
} else {
new Notice(data.message);
}
})
.catch((err) => {
console.log(err);
new Notice(err);
});
}
new Setting(containerEl)
.setName("User")
.setDesc("Typecho用户,操作时使用的用户")
.addDropdown((dropdown) => {
for (let i = 0; i < userList.length; i++) {
dropdown.addOption(userList[i].uid, userList[i].screenName);
}
dropdown.setValue(this.settings.User.uid);
dropdown.onChange(async (value) => {
for (let i = 0; i < userList.length; i++) {
if (userList[i].uid == value) {
this.settings.User = userList[i];
await this.plugin.saveSettings();
break;
}
}
});
});
}
}