update at 2025-10-16 16:10:58
This commit is contained in:
106
src/core/progress-indicator.ts
Normal file
106
src/core/progress-indicator.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* 文件:progress-indicator.ts
|
||||
* 作用:进度指示和用户反馈管理
|
||||
*/
|
||||
|
||||
import { Notice } from 'obsidian';
|
||||
|
||||
export interface ProgressOptions {
|
||||
showProgress?: boolean;
|
||||
duration?: number;
|
||||
autoHide?: boolean;
|
||||
}
|
||||
|
||||
export class ProgressIndicator {
|
||||
private notice: Notice | null = null;
|
||||
private startTime: number = 0;
|
||||
|
||||
start(message: string, options: ProgressOptions = {}): void {
|
||||
this.startTime = Date.now();
|
||||
const { duration = 0 } = options;
|
||||
|
||||
this.notice = new Notice(`🔄 ${message}...`, duration);
|
||||
}
|
||||
|
||||
update(message: string, progress?: number): void {
|
||||
if (!this.notice) return;
|
||||
|
||||
const progressText = progress ? ` (${Math.round(progress)}%)` : '';
|
||||
this.notice.setMessage(`🔄 ${message}${progressText}...`);
|
||||
}
|
||||
|
||||
finish(message: string, success: boolean = true): void {
|
||||
if (this.notice) {
|
||||
this.notice.hide();
|
||||
this.notice = null;
|
||||
}
|
||||
|
||||
const duration = Date.now() - this.startTime;
|
||||
const durationText = duration > 1000 ? ` (${(duration / 1000).toFixed(1)}s)` : '';
|
||||
const icon = success ? '✅' : '❌';
|
||||
|
||||
new Notice(`${icon} ${message}${durationText}`, success ? 3000 : 5000);
|
||||
}
|
||||
|
||||
error(message: string): void {
|
||||
this.finish(message, false);
|
||||
}
|
||||
|
||||
hide(): void {
|
||||
if (this.notice) {
|
||||
this.notice.hide();
|
||||
this.notice = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class BatchProgressIndicator {
|
||||
private currentProgress: ProgressIndicator | null = null;
|
||||
private totalItems: number = 0;
|
||||
private completedItems: number = 0;
|
||||
private failedItems: number = 0;
|
||||
|
||||
start(totalItems: number, operation: string): void {
|
||||
this.totalItems = totalItems;
|
||||
this.completedItems = 0;
|
||||
this.failedItems = 0;
|
||||
|
||||
this.currentProgress = new ProgressIndicator();
|
||||
this.currentProgress.start(`${operation} (0/${totalItems})`);
|
||||
}
|
||||
|
||||
updateItem(itemName: string, success: boolean = true): void {
|
||||
if (success) {
|
||||
this.completedItems++;
|
||||
} else {
|
||||
this.failedItems++;
|
||||
}
|
||||
|
||||
const completed = this.completedItems + this.failedItems;
|
||||
const progress = (completed / this.totalItems) * 100;
|
||||
|
||||
if (this.currentProgress) {
|
||||
this.currentProgress.update(
|
||||
`处理中: ${itemName} (${completed}/${this.totalItems})`,
|
||||
progress
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
finish(operation: string): void {
|
||||
const successCount = this.completedItems;
|
||||
const failCount = this.failedItems;
|
||||
const total = this.totalItems;
|
||||
|
||||
let message = `${operation}完成`;
|
||||
if (failCount === 0) {
|
||||
message += ` - 全部成功 (${successCount}/${total})`;
|
||||
} else {
|
||||
message += ` - 成功: ${successCount}, 失败: ${failCount}`;
|
||||
}
|
||||
|
||||
if (this.currentProgress) {
|
||||
this.currentProgress.finish(message, failCount === 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user