251 lines
7.8 KiB
TypeScript
251 lines
7.8 KiB
TypeScript
import { MarkdownView, Notice, Plugin, addIcon } from "obsidian";
|
||
import { settingTab } from "./setting/setting_tab";
|
||
import { PushModal } from "./view/push";
|
||
import { SiteConfig, generateSiteId } from "./setting/plugin_settings";
|
||
import { EditorView } from "@codemirror/view";
|
||
import { Extension } from "@codemirror/state";
|
||
import {
|
||
CategoryView,
|
||
VIEW_TYPE as ArticleViewType,
|
||
} from "./view/article_type";
|
||
import {
|
||
SiteManagerView,
|
||
VIEW_TYPE as ManagerViewType,
|
||
} from "./view/site_manager";
|
||
import {
|
||
ObsidianToTypechoSettings,
|
||
DEFAULT_SETTINGS,
|
||
} from "./setting/plugin_settings";
|
||
import i18n from "./utils/i18n";
|
||
import { Util } from "./utils/util";
|
||
|
||
let settings: ObsidianToTypechoSettings;
|
||
let pluginInstance: ObsidianToTypechoPlugin;
|
||
|
||
export default class ObsidianToTypechoPlugin extends Plugin {
|
||
/** 侧边栏自动刷新定时器(分钟制,0 = 不启动) */
|
||
private autoRefreshTimer: number | null = null;
|
||
|
||
async onload() {
|
||
pluginInstance = this;
|
||
addIcon(
|
||
"obsidian-to-typecho",
|
||
`<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1745386247342" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1491" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M517.04420574 882.47531656c-289.98573089 0-391.40007045-98.3302631-391.40007043-379.40894092 0-281.09368555 101.41433957-379.39393331 391.40007043-379.39393327 289.98573089 0 391.40007045 98.3002478 391.40007044 379.39393327 0 281.07867783-101.41433957 379.40143714-391.40007044 379.40143709zM306.28397227 386.32169964h421.50545924v-58.3648341H306.28397227v58.37233801z m0 145.92709302h301.07640006V473.8914624H306.28397227v58.36483409z m0 145.92709298h361.28717777v-58.37233793H306.2914761v58.37233793z" p-id="1492"></path></svg>`
|
||
);
|
||
|
||
await this.loadSettings();
|
||
this.addSettingTab(new settingTab(this.app, this, settings));
|
||
|
||
this.addRibbonIcon(
|
||
"obsidian-to-typecho",
|
||
i18n.t("sync.title"),
|
||
(evt: MouseEvent) => {
|
||
new PushModal(this.app).open();
|
||
}
|
||
);
|
||
|
||
this.registerView(ArticleViewType, (leaf) => new CategoryView(leaf));
|
||
this.registerView(ManagerViewType, (leaf) => new SiteManagerView(leaf));
|
||
this.registerPasteListener();
|
||
|
||
this.addRibbonIcon("list", i18n.t("manager.title"), () => {
|
||
this.activateManagerView();
|
||
});
|
||
|
||
this.addCommand({
|
||
id: "open-site-manager",
|
||
name: i18n.t("manager.open"),
|
||
callback: () => this.activateManagerView(),
|
||
});
|
||
|
||
// 启动时把侧边栏的数据重新拉一遍:上次会话留在面板里的列表可能是旧的,
|
||
// 也不该指望用户手动点刷新
|
||
//
|
||
// 恢复右侧栏视图必须等布局就绪:onload 阶段 workspace 还没建好,
|
||
// 此时 getRightLeaf() 会返回 null,直接 setViewState 会抛
|
||
// 「Cannot read properties of null (reading 'children')」并让插件加载失败
|
||
this.app.workspace.onLayoutReady(() => {
|
||
void this.openPluginView();
|
||
if (settings?.refreshOnStartup !== false) {
|
||
this.refreshManagerViews();
|
||
}
|
||
this.restartAutoRefresh();
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 重新加载已打开的站点管理侧边栏(数据 + 数量统计)
|
||
* gentle=true 用于定时自动刷新:只更新数据,不重置分页/搜索/筛选,避免打断阅读
|
||
*/
|
||
refreshManagerViews(gentle = false) {
|
||
for (const leaf of this.app.workspace.getLeavesOfType(ManagerViewType)) {
|
||
const view = leaf.view as unknown as {
|
||
reloadWithSettings?: () => Promise<void>;
|
||
autoRefresh?: () => Promise<void>;
|
||
};
|
||
if (gentle) {
|
||
void view.autoRefresh?.();
|
||
} else {
|
||
void view.reloadWithSettings?.();
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 按当前设置重建自动刷新定时器;间隔 <= 0 则关闭 */
|
||
restartAutoRefresh() {
|
||
if (this.autoRefreshTimer !== null) {
|
||
window.clearInterval(this.autoRefreshTimer);
|
||
this.autoRefreshTimer = null;
|
||
}
|
||
const minutes = Number(settings?.autoRefreshInterval);
|
||
if (!Number.isFinite(minutes) || minutes <= 0) return;
|
||
this.autoRefreshTimer = window.setInterval(() => {
|
||
this.refreshManagerViews(true);
|
||
}, minutes * 60 * 1000);
|
||
}
|
||
|
||
onunload() {
|
||
if (this.autoRefreshTimer !== null) {
|
||
window.clearInterval(this.autoRefreshTimer);
|
||
this.autoRefreshTimer = null;
|
||
}
|
||
}
|
||
|
||
async loadSettings() {
|
||
let loadedData = await this.loadData();
|
||
// 数据迁移:兼容旧版本单网站配置
|
||
if (loadedData && loadedData.Host && !loadedData.sites) {
|
||
loadedData.sites = [{
|
||
id: generateSiteId(),
|
||
name: loadedData.User?.screenName || i18n.t("setting.defaultSiteName"),
|
||
Host: loadedData.Host,
|
||
Token: loadedData.Token,
|
||
User: loadedData.User || { uid: "", url: "", screenName: "", mail: "" },
|
||
}];
|
||
loadedData.activeSiteId = loadedData.sites[0].id;
|
||
}
|
||
settings = Object.assign({}, DEFAULT_SETTINGS, loadedData);
|
||
return settings;
|
||
}
|
||
|
||
async saveSettings() {
|
||
await this.saveData(settings);
|
||
}
|
||
|
||
private async openPluginView() {
|
||
const { workspace } = this.app;
|
||
// 已经打开过就直接复用,避免每次加载都重复创建右侧栏视图
|
||
if (workspace.getLeavesOfType(ArticleViewType).length > 0) {
|
||
return;
|
||
}
|
||
try {
|
||
const newLeaf = workspace.getRightLeaf(false);
|
||
if (newLeaf) {
|
||
await newLeaf.setViewState({
|
||
type: ArticleViewType,
|
||
active: false,
|
||
});
|
||
}
|
||
} catch (error) {
|
||
// 启动早期布局还没建立时可能拿不到 leaf,交给用户手动打开即可
|
||
console.warn("恢复侧边栏视图失败", error);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 打开(或复用已打开的)站点管理侧边栏
|
||
*/
|
||
async activateManagerView() {
|
||
const { workspace } = this.app;
|
||
const existing = workspace.getLeavesOfType(ManagerViewType)[0];
|
||
if (existing) {
|
||
workspace.revealLeaf(existing);
|
||
return;
|
||
}
|
||
const leaf = workspace.getRightLeaf(false);
|
||
if (!leaf) return;
|
||
await leaf.setViewState({ type: ManagerViewType, active: true });
|
||
workspace.revealLeaf(leaf);
|
||
}
|
||
|
||
private registerPasteListener() {
|
||
this.registerEvent(
|
||
this.app.workspace.on("editor-paste", async (evt, editor) => {
|
||
await Util.upload.handleImagePaste(
|
||
this.app,
|
||
editor,
|
||
evt.clipboardData!
|
||
);
|
||
})
|
||
);
|
||
}
|
||
}
|
||
|
||
export function getSettings() {
|
||
return settings;
|
||
}
|
||
|
||
export async function saveSettings() {
|
||
if (pluginInstance) {
|
||
await pluginInstance.saveSettings();
|
||
}
|
||
}
|
||
|
||
// ========== 多站点管理函数 ==========
|
||
|
||
export function getSites(): SiteConfig[] {
|
||
return settings?.sites || [];
|
||
}
|
||
|
||
export function getActiveSite(): SiteConfig | undefined {
|
||
if (!settings?.sites || !settings.activeSiteId) return undefined;
|
||
return settings.sites.find(site => site.id === settings.activeSiteId);
|
||
}
|
||
|
||
export function getSite(siteId: string): SiteConfig | undefined {
|
||
return settings?.sites?.find(site => site.id === siteId);
|
||
}
|
||
|
||
export async function setActiveSite(siteId: string): Promise<boolean> {
|
||
const site = getSite(siteId);
|
||
if (site) {
|
||
settings.activeSiteId = siteId;
|
||
await saveSettings();
|
||
new Notice(i18n.t("sync.switchedTo", { name: site.name }));
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
export async function addSite(site: SiteConfig): Promise<void> {
|
||
if (!site.id) site.id = generateSiteId();
|
||
settings.sites.push(site);
|
||
if (!settings.activeSiteId && settings.sites.length === 1) {
|
||
settings.activeSiteId = site.id;
|
||
}
|
||
await saveSettings();
|
||
}
|
||
|
||
export async function updateSite(siteId: string, updates: Partial<SiteConfig>): Promise<boolean> {
|
||
const index = settings.sites.findIndex(s => s.id === siteId);
|
||
if (index !== -1) {
|
||
settings.sites[index] = { ...settings.sites[index], ...updates };
|
||
await saveSettings();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
export async function deleteSite(siteId: string): Promise<boolean> {
|
||
const index = settings.sites.findIndex(s => s.id === siteId);
|
||
if (index !== -1) {
|
||
settings.sites.splice(index, 1);
|
||
if (settings.activeSiteId === siteId) {
|
||
settings.activeSiteId = settings.sites[0]?.id || "";
|
||
}
|
||
await saveSettings();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|