72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { App, PluginSettingTab, Setting, Notice } from 'obsidian';
|
||
import BlogFlarePlugin from './main';
|
||
|
||
export interface BlogFlareSettings {
|
||
apiEndpoint: string;
|
||
apiToken: string;
|
||
}
|
||
|
||
export const DEFAULT_SETTINGS: BlogFlareSettings = {
|
||
apiEndpoint: '',
|
||
apiToken: ''
|
||
};
|
||
|
||
export class BlogFlareSettingTab extends PluginSettingTab {
|
||
plugin: BlogFlarePlugin;
|
||
|
||
constructor(app: App, plugin: BlogFlarePlugin) {
|
||
super(app, plugin);
|
||
this.plugin = plugin;
|
||
}
|
||
|
||
display(): void {
|
||
const { containerEl } = this;
|
||
containerEl.empty();
|
||
|
||
new Setting(containerEl)
|
||
.setName('API 地址')
|
||
.setDesc('BlogFlare 实例的访问地址(如 https://yourblog.com)')
|
||
.addText(text => text
|
||
.setPlaceholder('输入地址...')
|
||
.setValue(this.plugin.settings.apiEndpoint)
|
||
.onChange(async (value) => {
|
||
this.plugin.settings.apiEndpoint = value;
|
||
await this.plugin.saveSettings();
|
||
}));
|
||
|
||
new Setting(containerEl)
|
||
.setName('API 令牌 (Token)')
|
||
.setDesc('用于请求接口认证的 API Token')
|
||
.addText(text => text
|
||
.setPlaceholder('输入你的 Token')
|
||
.setValue(this.plugin.settings.apiToken)
|
||
.onChange(async (value) => {
|
||
this.plugin.settings.apiToken = value;
|
||
await this.plugin.saveSettings();
|
||
}));
|
||
|
||
new Setting(containerEl)
|
||
.setName('测试连接')
|
||
.setDesc('验证当前配置的 Endpoint 与 API 令牌是否能正确连接到 Blogflare 后端')
|
||
.addButton((button) => {
|
||
button
|
||
.setButtonText('测试')
|
||
.setCta()
|
||
.onClick(async () => {
|
||
button.setButtonText('测试中...');
|
||
button.setDisabled(true);
|
||
try {
|
||
await this.plugin.api.getCategories();
|
||
new Notice('✅ 连接成功!API 令牌有效。');
|
||
} catch (error) {
|
||
console.error(error);
|
||
new Notice('❌ 连接失败:API 令牌无效或网络错误,请检查设置。');
|
||
} finally {
|
||
button.setButtonText('测试');
|
||
button.setDisabled(false);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
}
|