206 lines
6.9 KiB
TypeScript
206 lines
6.9 KiB
TypeScript
|
|
import { App, PluginSettingTab, Setting, Notice } from 'obsidian';
|
|||
|
|
import OmniBoxSyncPlugin from './main';
|
|||
|
|
import { OmniBoxResource } from './api';
|
|||
|
|
|
|||
|
|
export interface OmniBoxSettings {
|
|||
|
|
apiKey: string;
|
|||
|
|
syncFolder: string;
|
|||
|
|
autoSyncOnStart: boolean;
|
|||
|
|
syncInterval: number;
|
|||
|
|
overwriteLocal: boolean;
|
|||
|
|
/** 已完整下载(图文齐全)的文档直接跳过 */
|
|||
|
|
skipCompleted: boolean;
|
|||
|
|
/** 每次批量图文最多处理多少篇 */
|
|||
|
|
batchLimit: number;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export const DEFAULT_SETTINGS: OmniBoxSettings = {
|
|||
|
|
apiKey: '',
|
|||
|
|
syncFolder: 'OmniBox',
|
|||
|
|
autoSyncOnStart: false,
|
|||
|
|
syncInterval: 0,
|
|||
|
|
overwriteLocal: false,
|
|||
|
|
skipCompleted: true,
|
|||
|
|
batchLimit: 3
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// ========== 独立缓存数据结构 ==========
|
|||
|
|
export interface OmniBoxCache {
|
|||
|
|
cachedResources: OmniBoxResource[];
|
|||
|
|
lastSyncTime: string;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export const DEFAULT_CACHE: OmniBoxCache = {
|
|||
|
|
cachedResources: [],
|
|||
|
|
lastSyncTime: ''
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export class OmniBoxSettingTab extends PluginSettingTab {
|
|||
|
|
plugin: OmniBoxSyncPlugin;
|
|||
|
|
|
|||
|
|
constructor(app: App, plugin: OmniBoxSyncPlugin) {
|
|||
|
|
super(app, plugin);
|
|||
|
|
this.plugin = plugin;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
display(): void {
|
|||
|
|
const { containerEl } = this;
|
|||
|
|
containerEl.empty();
|
|||
|
|
|
|||
|
|
// 顶部标题 - 去掉左边距
|
|||
|
|
const titleEl = containerEl.createEl('h2', { text: 'OmniBox 同步设置' });
|
|||
|
|
titleEl.style.marginLeft = '0';
|
|||
|
|
titleEl.style.paddingLeft = '0';
|
|||
|
|
|
|||
|
|
// API Key 设置
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('API Key')
|
|||
|
|
.setDesc('从小黑后台获取的 API Key。创建时请勾选「资源权限」的读取、创建、更新。')
|
|||
|
|
.addText(text => text
|
|||
|
|
.setPlaceholder('请输入你的 API Key...')
|
|||
|
|
.setValue(this.plugin.settings.apiKey)
|
|||
|
|
.onChange(async (value) => {
|
|||
|
|
this.plugin.settings.apiKey = value.trim();
|
|||
|
|
await this.plugin.saveSettings();
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 本地同步文件夹
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('本地同步文件夹')
|
|||
|
|
.setDesc('云端资源将同步到此文件夹下(相对于库根目录)')
|
|||
|
|
.addText(text => text
|
|||
|
|
.setPlaceholder('OmniBox')
|
|||
|
|
.setValue(this.plugin.settings.syncFolder)
|
|||
|
|
.onChange(async (value) => {
|
|||
|
|
this.plugin.settings.syncFolder = value.trim() || 'OmniBox';
|
|||
|
|
await this.plugin.saveSettings();
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 自动同步开关
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('启动时自动同步')
|
|||
|
|
.setDesc('插件加载后自动刷新并同步资源列表')
|
|||
|
|
.addToggle(toggle => toggle
|
|||
|
|
.setValue(this.plugin.settings.autoSyncOnStart)
|
|||
|
|
.onChange(async (value) => {
|
|||
|
|
this.plugin.settings.autoSyncOnStart = value;
|
|||
|
|
await this.plugin.saveSettings();
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 同步间隔(分钟)
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('自动同步间隔')
|
|||
|
|
.setDesc('设置自动同步的间隔时间(分钟),设为0表示不自动同步。注意:基础版每日仅10次API调用')
|
|||
|
|
.addText(text => text
|
|||
|
|
.setPlaceholder('0')
|
|||
|
|
.setValue(String(this.plugin.settings.syncInterval))
|
|||
|
|
.onChange(async (value) => {
|
|||
|
|
const num = parseInt(value);
|
|||
|
|
if (!isNaN(num) && num >= 0) {
|
|||
|
|
this.plugin.settings.syncInterval = num;
|
|||
|
|
await this.plugin.saveSettings();
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 覆盖策略
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('覆盖本地文件')
|
|||
|
|
.setDesc('当云端文件比本地文件更新时,是否自动覆盖。关闭则跳过')
|
|||
|
|
.addToggle(toggle => toggle
|
|||
|
|
.setValue(this.plugin.settings.overwriteLocal)
|
|||
|
|
.onChange(async (value) => {
|
|||
|
|
this.plugin.settings.overwriteLocal = value;
|
|||
|
|
await this.plugin.saveSettings();
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 测试连接 - 移动到覆盖策略下面
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('测试连接')
|
|||
|
|
.setDesc('点击测试 API Key 是否有效')
|
|||
|
|
.addButton(button => button
|
|||
|
|
.setButtonText('测试连接')
|
|||
|
|
.onClick(async () => {
|
|||
|
|
const { api } = this.plugin;
|
|||
|
|
if (!api) return;
|
|||
|
|
|
|||
|
|
button.setButtonText('测试中...');
|
|||
|
|
button.setDisabled(true);
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const result = await api.testConnection();
|
|||
|
|
if (result) {
|
|||
|
|
const info = await api.getApiKeyInfo();
|
|||
|
|
const used = info?.open_api_requests_quota?.used || 0;
|
|||
|
|
const limit = info?.open_api_requests_quota?.limit || 0;
|
|||
|
|
const resetAt = info?.open_api_requests_quota?.reset_at || '';
|
|||
|
|
let resetMsg = '';
|
|||
|
|
if (resetAt) {
|
|||
|
|
const resetDate = new Date(resetAt);
|
|||
|
|
const hours = Math.ceil((resetDate.getTime() - Date.now()) / (1000 * 60 * 60));
|
|||
|
|
if (hours > 0) {
|
|||
|
|
resetMsg = `,约 ${hours} 小时后重置`;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
new Notice(`连接成功!今日已使用 ${used}/${limit} 次API调用${resetMsg}`);
|
|||
|
|
} else {
|
|||
|
|
new Notice('连接失败,请检查 API Key');
|
|||
|
|
}
|
|||
|
|
} catch (e: any) {
|
|||
|
|
new Notice(`连接失败: ${e.message}`);
|
|||
|
|
} finally {
|
|||
|
|
button.setButtonText('测试连接');
|
|||
|
|
button.setDisabled(false);
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// ========== 批量下载节流 ==========
|
|||
|
|
containerEl.createEl('h3', { text: '批量下载节流' });
|
|||
|
|
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('跳过已下载')
|
|||
|
|
.setDesc('图文已经齐全的文档不再重复下载,只补未下载或图片缺失的(配合断点续传,分批跑完)')
|
|||
|
|
.addToggle(toggle => toggle
|
|||
|
|
.setValue(this.plugin.settings.skipCompleted)
|
|||
|
|
.onChange(async (value) => {
|
|||
|
|
this.plugin.settings.skipCompleted = value;
|
|||
|
|
await this.plugin.saveSettings();
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('每批下载数量')
|
|||
|
|
.setDesc('点一次「下载图文」最多处理多少篇。基础版每天仅 10 次调用,建议设 2~3 篇分批跑')
|
|||
|
|
.addText(text => text
|
|||
|
|
.setPlaceholder('3')
|
|||
|
|
.setValue(String(this.plugin.settings.batchLimit))
|
|||
|
|
.onChange(async (value) => {
|
|||
|
|
const num = parseInt(value);
|
|||
|
|
if (!isNaN(num) && num >= 1) {
|
|||
|
|
this.plugin.settings.batchLimit = num;
|
|||
|
|
await this.plugin.saveSettings();
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// ========== 新增:清空缓存按钮(放在设置页面最后) ==========
|
|||
|
|
containerEl.createEl('hr');
|
|||
|
|
|
|||
|
|
new Setting(containerEl)
|
|||
|
|
.setName('清空缓存')
|
|||
|
|
.setDesc('清空本地缓存的资源列表(data_cache.json),不影响 API Key 等设置。清空后需重新刷新加载。')
|
|||
|
|
.addButton(button => button
|
|||
|
|
.setButtonText('清空缓存')
|
|||
|
|
.setWarning()
|
|||
|
|
.onClick(async () => {
|
|||
|
|
await this.plugin.clearCache();
|
|||
|
|
new Notice('✅ 缓存已清空');
|
|||
|
|
})
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|