同步插件源代码并补齐标准项目文件
This commit is contained in:
36
setting/plugin_settings.ts
Normal file
36
setting/plugin_settings.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
export interface RssUnionSettings {
|
||||
/** 网站根地址,例如 https://example.com(不带结尾斜杠) */
|
||||
siteUrl: string;
|
||||
/** 会员中心生成的授权码 */
|
||||
authCode: string;
|
||||
/** 文章列表每页条数 */
|
||||
pageSize: number;
|
||||
/** 阅读窗口打开位置:tab = 新标签页(稳),split = 当前面板右侧分栏 */
|
||||
openMode: "split" | "tab";
|
||||
/** 「保存为笔记」时的目录 */
|
||||
saveFolder: string;
|
||||
/** 自动刷新间隔(分钟),0 = 关闭 */
|
||||
autoRefreshMin: number;
|
||||
/** 上次验证得到的会员名,设置页展示用 */
|
||||
verifyName: string;
|
||||
/** 上次验证时间(ISO 字符串) */
|
||||
verifyAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认用「新标签页」
|
||||
*
|
||||
* split 出来的新 leaf 在部分 Obsidian 版本里,setViewState 时 header 还没建好,
|
||||
* Obsidian 内部会对 null 的 title 元素调 setText 抛错,整个 view 打不开。
|
||||
* tab 走的路径稳定,默认给它;想要分栏的可以自己在设置里改。
|
||||
*/
|
||||
export const DEFAULT_SETTINGS: RssUnionSettings = {
|
||||
siteUrl: "",
|
||||
authCode: "",
|
||||
pageSize: 30,
|
||||
openMode: "tab",
|
||||
saveFolder: "RssUnion",
|
||||
autoRefreshMin: 5,
|
||||
verifyName: "",
|
||||
verifyAt: "",
|
||||
};
|
||||
182
setting/setting_tab.ts
Normal file
182
setting/setting_tab.ts
Normal file
@@ -0,0 +1,182 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
23
setting/store.ts
Normal file
23
setting/store.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { DEFAULT_SETTINGS, RssUnionSettings } from "./plugin_settings";
|
||||
|
||||
/** 全局设置引用:main.ts 加载完写入,其余模块只读,避免各处 import main 造成循环依赖 */
|
||||
let current: RssUnionSettings = Object.assign({}, DEFAULT_SETTINGS);
|
||||
|
||||
export function setSettings(next: RssUnionSettings) {
|
||||
current = next;
|
||||
}
|
||||
|
||||
export function getSettings(): RssUnionSettings {
|
||||
return current;
|
||||
}
|
||||
|
||||
/** 站点地址是否已经填了(授权码可以为空,验证时会单独提示) */
|
||||
export function hasSite(): boolean {
|
||||
return normalizeSiteUrl(current.siteUrl) !== "";
|
||||
}
|
||||
|
||||
export function normalizeSiteUrl(raw: string): string {
|
||||
return String(raw || "")
|
||||
.trim()
|
||||
.replace(/\/+$/, "");
|
||||
}
|
||||
Reference in New Issue
Block a user