183 lines
5.4 KiB
TypeScript
183 lines
5.4 KiB
TypeScript
import {
|
|
App,
|
|
Notice,
|
|
PluginSettingTab,
|
|
Setting,
|
|
} from "obsidian";
|
|
import type RssUnionPlugin from "../main";
|
|
import { ping } from "../utils/api";
|
|
import { clearReadState } from "../utils/read_state";
|
|
import i18n from "../utils/i18n";
|
|
|
|
export class RssUnionSettingTab extends PluginSettingTab {
|
|
private plugin: RssUnionPlugin;
|
|
private verifyEl: HTMLElement | null = null;
|
|
|
|
constructor(app: App, plugin: RssUnionPlugin) {
|
|
super(app, plugin);
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
display(): void {
|
|
const { containerEl } = this;
|
|
|
|
containerEl.empty();
|
|
containerEl.createEl("h2", { text: i18n.t("setting.title") });
|
|
|
|
new Setting(containerEl)
|
|
.setName(i18n.t("setting.siteUrl"))
|
|
.setDesc(i18n.t("setting.siteUrlDesc"))
|
|
.addText((text) =>
|
|
text
|
|
.setPlaceholder("https://example.com")
|
|
.setValue(this.plugin.settings.siteUrl)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.siteUrl = value.trim();
|
|
await this.plugin.saveSettings();
|
|
})
|
|
);
|
|
|
|
new Setting(containerEl)
|
|
.setName(i18n.t("setting.authCode"))
|
|
.setDesc(i18n.t("setting.authCodeDesc"))
|
|
.addText((text) => {
|
|
text.inputEl.addClass("ru-setting-code");
|
|
text.setPlaceholder("ruo_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
|
|
.setValue(this.plugin.settings.authCode)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.authCode = value.trim();
|
|
await this.plugin.saveSettings();
|
|
});
|
|
});
|
|
|
|
const verify = new Setting(containerEl).setName(i18n.t("setting.verify"));
|
|
|
|
this.verifyEl = verify.descEl.createDiv({ cls: "ru-setting-verify" });
|
|
this.paintVerify();
|
|
|
|
verify.addButton((button) =>
|
|
button.setButtonText(i18n.t("setting.verify")).setCta().onClick(async () => {
|
|
button.setDisabled(true);
|
|
button.setButtonText(i18n.t("setting.verifying"));
|
|
|
|
try {
|
|
const res = await ping();
|
|
|
|
this.plugin.settings.verifyName = res.user?.name || "";
|
|
this.plugin.settings.verifyAt = new Date().toISOString();
|
|
await this.plugin.saveSettings();
|
|
|
|
this.paintVerify();
|
|
new Notice(i18n.t("setting.verified", { name: res.user?.name || "-", sites: res.siteCount || 0 }));
|
|
await this.plugin.refreshViews();
|
|
} catch (error: any) {
|
|
this.plugin.settings.verifyName = "";
|
|
this.plugin.settings.verifyAt = "";
|
|
await this.plugin.saveSettings();
|
|
|
|
this.paintVerify(String(error?.message || error));
|
|
new Notice(`验证失败:${error?.message || error}`);
|
|
} finally {
|
|
button.setDisabled(false);
|
|
button.setButtonText(i18n.t("setting.verify"));
|
|
}
|
|
})
|
|
);
|
|
|
|
new Setting(containerEl)
|
|
.setName(i18n.t("setting.pageSize"))
|
|
.setDesc(i18n.t("setting.pageSizeDesc"))
|
|
.addSlider((slider) =>
|
|
slider
|
|
.setLimits(10, 100, 5)
|
|
.setValue(this.plugin.settings.pageSize)
|
|
.setDynamicTooltip()
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.pageSize = value;
|
|
await this.plugin.saveSettings();
|
|
})
|
|
);
|
|
|
|
new Setting(containerEl)
|
|
.setName(i18n.t("setting.autoRefresh"))
|
|
.setDesc(i18n.t("setting.autoRefreshDesc"))
|
|
.addDropdown((drop) => {
|
|
drop.addOption("0", i18n.t("setting.autoRefreshOff"));
|
|
|
|
for (const minutes of [1, 5, 10, 15, 30, 60]) {
|
|
drop.addOption(String(minutes), i18n.t("setting.minutes", { n: minutes }));
|
|
}
|
|
|
|
drop.setValue(String(this.plugin.settings.autoRefreshMin || 0)).onChange(async (value) => {
|
|
this.plugin.settings.autoRefreshMin = Number(value) || 0;
|
|
await this.plugin.saveSettings();
|
|
// 只重启定时器,不重置当前选中的分类和搜索词
|
|
await this.plugin.refreshViews(false);
|
|
});
|
|
});
|
|
|
|
new Setting(containerEl)
|
|
.setName(i18n.t("setting.openMode"))
|
|
.setDesc(i18n.t("setting.openModeDesc"))
|
|
.addDropdown((drop) =>
|
|
drop
|
|
.addOption("split", i18n.t("setting.openModeSplit"))
|
|
.addOption("tab", i18n.t("setting.openModeTab"))
|
|
.setValue(this.plugin.settings.openMode)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.openMode = value === "tab" ? "tab" : "split";
|
|
await this.plugin.saveSettings();
|
|
})
|
|
);
|
|
|
|
new Setting(containerEl)
|
|
.setName(i18n.t("setting.saveFolder"))
|
|
.setDesc(i18n.t("setting.saveFolderDesc"))
|
|
.addText((text) =>
|
|
text
|
|
.setPlaceholder("RssUnion")
|
|
.setValue(this.plugin.settings.saveFolder)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.saveFolder = value.trim();
|
|
await this.plugin.saveSettings();
|
|
})
|
|
);
|
|
|
|
new Setting(containerEl)
|
|
.setName(i18n.t("setting.clearRead"))
|
|
.setDesc(i18n.t("setting.clearReadDesc"))
|
|
.addButton((button) =>
|
|
button.setButtonText(i18n.t("setting.clearReadBtn")).onClick(async () => {
|
|
clearReadState();
|
|
await this.plugin.refreshViews(false);
|
|
new Notice(i18n.t("setting.clearReadDone"));
|
|
})
|
|
);
|
|
|
|
containerEl.createEl("h3", { text: i18n.t("setting.howTo") });
|
|
containerEl.createEl("p", { cls: "ru-setting-help", text: i18n.t("setting.howToBody") });
|
|
}
|
|
|
|
private paintVerify(errorMessage?: string): void {
|
|
if (!this.verifyEl) return;
|
|
|
|
this.verifyEl.empty();
|
|
this.verifyEl.removeClass("is-ok");
|
|
this.verifyEl.removeClass("is-error");
|
|
|
|
if (errorMessage) {
|
|
this.verifyEl.addClass("is-error");
|
|
this.verifyEl.setText(errorMessage);
|
|
return;
|
|
}
|
|
|
|
if (this.plugin.settings.verifyName) {
|
|
this.verifyEl.addClass("is-ok");
|
|
this.verifyEl.setText(`已连接:${this.plugin.settings.verifyName}`);
|
|
return;
|
|
}
|
|
|
|
this.verifyEl.setText(i18n.t("setting.verifyNeedInput"));
|
|
}
|
|
}
|