import { Plugin, Notice } from 'obsidian'; import { OmniBoxSettingTab, DEFAULT_SETTINGS, OmniBoxSettings, OmniBoxCache, DEFAULT_CACHE } from './settings'; import { OmniBoxSidebarView } from './sidebar'; import { OmniBoxAPI } from './api'; import { OmniBoxResource } from './api'; export default class OmniBoxSyncPlugin extends Plugin { settings: OmniBoxSettings; api: OmniBoxAPI; cache: OmniBoxCache; // 独立缓存 async onload() { await this.loadSettings(); await this.loadCache(); // 加载独立缓存 // 初始化API客户端 this.api = new OmniBoxAPI(this.settings.apiKey); // 注册设置页面 this.addSettingTab(new OmniBoxSettingTab(this.app, this)); // 注册边栏视图 this.registerView( 'omnibox-sidebar', (leaf) => new OmniBoxSidebarView(leaf, this) ); // 添加Ribbon图标 this.addRibbonIcon('cloud', 'OmniBox 同步', () => { this.activateView(); }); // 添加命令 this.addCommand({ id: 'refresh-omnibox-list', name: '刷新云端资源列表', callback: () => this.refreshSidebar() }); this.addCommand({ id: 'sync-all-omnibox-text', name: '下载全部文字', callback: () => this.downloadAllText() }); this.addCommand({ id: 'sync-all-omnibox-full', name: '下载全部图文', callback: () => this.downloadAllFull() }); // 清空缓存命令 this.addCommand({ id: 'clear-omnibox-cache', name: '清空本地缓存', callback: async () => { await this.clearCache(); new Notice('✅ 缓存已清空'); } }); } // ========== 加载设置 ========== async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); } // ========== 保存设置 ========== async saveSettings() { await this.saveData(this.settings); if (this.api) { this.api.updateApiKey(this.settings.apiKey); } } // ========== 加载缓存 ========== async loadCache(): Promise { try { const cachePath = `${this.app.vault.configDir}/plugins/omnibox-sync-plugin/data_cache.json`; const cacheData = await this.app.vault.adapter.read(cachePath); this.cache = JSON.parse(cacheData); console.log('📋 缓存加载成功:', this.cache.cachedResources.length, '个资源'); } catch (e) { this.cache = JSON.parse(JSON.stringify(DEFAULT_CACHE)); console.log('📋 使用默认缓存(空)'); } } // ========== 保存缓存 ========== async saveCache(): Promise { try { const pluginDir = `${this.app.vault.configDir}/plugins/omnibox-sync-plugin`; const cachePath = `${pluginDir}/data_cache.json`; const dirExists = await this.app.vault.adapter.exists(pluginDir); if (!dirExists) { await this.app.vault.adapter.mkdir(pluginDir); console.log('📁 创建插件目录:', pluginDir); } await this.app.vault.adapter.write(cachePath, JSON.stringify(this.cache, null, 2)); console.log('💾 缓存保存成功:', this.cache.cachedResources.length, '个资源'); } catch (e) { console.error('❌ 缓存保存失败:', e); } } // ========== 清空缓存 ========== async clearCache(): Promise { this.cache = JSON.parse(JSON.stringify(DEFAULT_CACHE)); await this.saveCache(); await this.refreshSidebar(); } // ========== 获取缓存的资源列表 ========== getCachedResources(): OmniBoxResource[] { return this.cache.cachedResources || []; } // ========== 获取上次同步时间 ========== getLastSyncTime(): string { return this.cache.lastSyncTime || ''; } // ========== 更新缓存 ========== async updateCache(resources: OmniBoxResource[]): Promise { this.cache.cachedResources = resources; this.cache.lastSyncTime = new Date().toISOString(); await this.saveCache(); } // ========== 激活边栏 ========== async activateView() { const { workspace } = this.app; let leaf = workspace.getLeavesOfType('omnibox-sidebar')[0]; if (!leaf) { const rightLeaf = workspace.getRightLeaf(false); if (!rightLeaf) { new Notice('无法创建边栏,请检查 Obsidian 布局'); return; } leaf = rightLeaf; await leaf.setViewState({ type: 'omnibox-sidebar', active: true }); } workspace.revealLeaf(leaf); } async refreshSidebar() { const leaves = this.app.workspace.getLeavesOfType('omnibox-sidebar'); if (leaves.length > 0) { const view = leaves[0].view as OmniBoxSidebarView; await view.refresh(); } } // ========== 命令:下载全部文字 ========== async downloadAllText() { const leaves = this.app.workspace.getLeavesOfType('omnibox-sidebar'); if (leaves.length > 0) { const view = leaves[0].view as OmniBoxSidebarView; await view.downloadAllText(); } else { new Notice('请先打开 OmniBox 侧边栏'); } } // ========== 命令:下载全部图文 ========== async downloadAllFull() { const leaves = this.app.workspace.getLeavesOfType('omnibox-sidebar'); if (leaves.length > 0) { const view = leaves[0].view as OmniBoxSidebarView; await view.downloadAllFull(); } else { new Notice('请先打开 OmniBox 侧边栏'); } } onunload() { this.app.workspace.detachLeavesOfType('omnibox-sidebar'); } }