2025-11-18 14:46:51 +08:00
|
|
|
import { singleton } from 'tsyringe';
|
|
|
|
|
import type { INotification, NotificationType } from '@esengine/editor-core';
|
|
|
|
|
|
|
|
|
|
@singleton()
|
|
|
|
|
export class NotificationService implements INotification {
|
|
|
|
|
private showCallback?: (message: string, type?: NotificationType, duration?: number) => void;
|
|
|
|
|
private hideCallback?: (id: string) => void;
|
|
|
|
|
|
|
|
|
|
setCallbacks(
|
|
|
|
|
showCallback: (message: string, type?: NotificationType, duration?: number) => void,
|
|
|
|
|
hideCallback: (id: string) => void
|
|
|
|
|
): void {
|
|
|
|
|
this.showCallback = showCallback;
|
|
|
|
|
this.hideCallback = hideCallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
show(message: string, type: NotificationType = 'info', duration: number = 3000): void {
|
|
|
|
|
if (this.showCallback) {
|
|
|
|
|
this.showCallback(message, type, duration);
|
|
|
|
|
} else {
|
|
|
|
|
console.warn('[NotificationService] showCallback not set, message:', message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 14:49:37 +08:00
|
|
|
warning(title: string, message: string, duration: number = 5000): void {
|
|
|
|
|
this.show(`${title}: ${message}`, 'warning', duration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error(title: string, message: string, duration: number = 5000): void {
|
|
|
|
|
this.show(`${title}: ${message}`, 'error', duration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info(title: string, message: string, duration: number = 3000): void {
|
|
|
|
|
this.show(`${title}: ${message}`, 'info', duration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
success(title: string, message: string, duration: number = 3000): void {
|
|
|
|
|
this.show(`${title}: ${message}`, 'success', duration);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-18 14:46:51 +08:00
|
|
|
hide(id: string): void {
|
|
|
|
|
if (this.hideCallback) {
|
|
|
|
|
this.hideCallback(id);
|
|
|
|
|
} else {
|
|
|
|
|
console.warn('[NotificationService] hideCallback not set');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-23 14:49:37 +08:00
|
|
|
|
|
|
|
|
dispose(): void {
|
|
|
|
|
this.showCallback = undefined;
|
|
|
|
|
this.hideCallback = undefined;
|
|
|
|
|
}
|
2025-11-18 14:46:51 +08:00
|
|
|
}
|