85 lines
2.5 KiB
TypeScript
85 lines
2.5 KiB
TypeScript
import { Plugin, WorkspaceLeaf, addIcon } from "obsidian";
|
|
import { DEFAULT_SETTINGS, RssUnionSettings } from "./setting/plugin_settings";
|
|
import { setSettings } from "./setting/store";
|
|
import { RssUnionSettingTab } from "./setting/setting_tab";
|
|
import { RssUnionView, VIEW_TYPE_MAIN } from "./view/main_view";
|
|
import { ReaderView, VIEW_TYPE_READER } from "./view/reader_view";
|
|
import i18n from "./utils/i18n";
|
|
|
|
/** 左侧功能区的图标:几道 RSS 波纹,和插件用途对上 */
|
|
const ICON_RSS = `<path d="M4 11a9 9 0 0 1 9 9"/><path d="M4 4a16 16 0 0 1 16 16"/><circle cx="5" cy="19" r="1"/>`;
|
|
|
|
export default class RssUnionPlugin extends Plugin {
|
|
settings: RssUnionSettings = Object.assign({}, DEFAULT_SETTINGS);
|
|
|
|
async onload(): Promise<void> {
|
|
await this.loadSettings();
|
|
|
|
addIcon("ru-rss", ICON_RSS);
|
|
|
|
this.registerView(VIEW_TYPE_MAIN, (leaf: WorkspaceLeaf) => new RssUnionView(leaf));
|
|
this.registerView(VIEW_TYPE_READER, (leaf: WorkspaceLeaf) => new ReaderView(leaf));
|
|
|
|
this.addRibbonIcon("ru-rss", i18n.t("view.title"), () => {
|
|
void this.activateView();
|
|
});
|
|
|
|
this.addCommand({
|
|
id: "open-rss-union-reader",
|
|
name: i18n.t("view.open"),
|
|
callback: () => {
|
|
void this.activateView();
|
|
},
|
|
});
|
|
|
|
this.addSettingTab(new RssUnionSettingTab(this.app, this));
|
|
}
|
|
|
|
async loadSettings(): Promise<void> {
|
|
const loaded = await this.loadData();
|
|
|
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, loaded || {});
|
|
setSettings(this.settings);
|
|
}
|
|
|
|
async saveSettings(): Promise<void> {
|
|
await this.saveData(this.settings);
|
|
setSettings(this.settings);
|
|
}
|
|
|
|
/** 打开(或聚焦)主面板 */
|
|
async activateView(): Promise<void> {
|
|
const { workspace } = this.app;
|
|
const existing = workspace.getLeavesOfType(VIEW_TYPE_MAIN);
|
|
|
|
if (existing.length > 0) {
|
|
workspace.revealLeaf(existing[0]);
|
|
|
|
return;
|
|
}
|
|
|
|
const leaf = workspace.getLeaf("tab");
|
|
|
|
if (!leaf) return;
|
|
|
|
await leaf.setViewState({ type: VIEW_TYPE_MAIN, active: true });
|
|
workspace.revealLeaf(leaf);
|
|
}
|
|
|
|
/**
|
|
* 让已经打开的面板重新取一遍数据
|
|
*
|
|
* @param reset true = 顺带把选中的分类、搜索词清空(改了站点 / 授权码时用)
|
|
* false = 保持当前浏览位置(只改刷新间隔时用)
|
|
*/
|
|
async refreshViews(reset = true): Promise<void> {
|
|
for (const leaf of this.app.workspace.getLeavesOfType(VIEW_TYPE_MAIN)) {
|
|
const view = leaf.view as unknown as {
|
|
reloadFromSettings?: (reset?: boolean) => Promise<void>;
|
|
};
|
|
|
|
await view.reloadFromSettings?.(reset);
|
|
}
|
|
}
|
|
}
|