Files
MetonaToast/src/index.ts
T
thzxx b5c670ece3
CI / test (18.x) (push) Successful in 10m1s
CI / test (20.x) (push) Successful in 9m59s
CI / test (22.x) (push) Successful in 9m58s
CI / test (24.x) (push) Successful in 9m55s
release: v0.2.1 — bug修复、代码去重、功能增强、覆盖率88.57%
2026-07-25 13:01:59 +08:00

117 lines
2.8 KiB
TypeScript

/**
* MetonaToast — 轻量级Toast通知库
* @module metona-toast
* @version 0.2.1
* @author thzxx
* @license MIT
*/
import { meToast } from './api.js';
import { Toast, _containerCache } from './toast.js';
import { animationUtils } from './animations.js';
import { themeUtils } from './themes.js';
import { i18nUtils } from './i18n.js';
import { pluginUtils, presetPlugins } from './plugins.js';
import type { ToastInstance } from './types.js';
// 版本信息
const VERSION = '0.2.1';
/**
* 增强的 MeToast 对象 — 在 meToast 基础上注入子模块
*/
const enhancedMeToast = {
...meToast,
version: VERSION,
animations: animationUtils,
themes: themeUtils,
i18n: i18nUtils,
plugins: pluginUtils,
presetPlugins,
};
// 初始化主题和国际化
if (typeof themeUtils.initTheme === 'function') {
themeUtils.initTheme();
}
if (typeof i18nUtils.initI18n === 'function') {
i18nUtils.initI18n();
}
// 浏览器环境全局注册
if (typeof window !== 'undefined') {
window.MeToast = enhancedMeToast;
window.Met = enhancedMeToast;
// Notification API — 页面不可见时自动发送系统通知
const sendNotification = (toast: ToastInstance): void => {
try {
new Notification(toast.title || toast.type, { body: toast.message });
} catch (_e) { /* noop */ }
};
(enhancedMeToast as unknown as Record<string, unknown>)._notify = (toast: ToastInstance) => {
if (typeof Notification === 'undefined') return;
if (Notification.permission === 'denied') return;
if (!document.hidden) return;
if (Notification.permission === 'default') {
Notification.requestPermission().then(p => { if (p === 'granted') sendNotification(toast); });
return;
}
sendNotification(toast);
};
Toast.on('afterShow', (toast: ToastInstance) => {
const config = toast.config;
if (config.notifyWhenHidden) {
((enhancedMeToast as unknown as Record<string, (t: ToastInstance) => void>)._notify)(toast);
}
});
}
// 导出
export default enhancedMeToast;
export { enhancedMeToast as meToast, enhancedMeToast as Met, enhancedMeToast as MeToast, Toast, VERSION };
// 类型重导出(供 TypeScript 消费者 import type 使用)
export type {
ToastType,
ToastPosition,
ToastTheme,
ToastAnimation,
ToastProgressDirection,
ToastConfig,
ToastOptions,
ToastInstance,
TypeColor,
ErrorInfo,
LoadingControl,
ProgressControl,
CountdownControl,
ActionControl,
QueueControl,
GroupAPI,
ActionButton,
PromiseOptions,
ConfirmOptions,
PromptOptions,
ProgressOptions,
CountdownOptions,
QueueOptions,
StackOptions,
InitOptions,
StatusInfo,
AnimationConfig,
AnimationUtils,
ThemeConfig,
ThemePreview,
ThemeUtils,
LocaleInfo,
I18nUtils,
Plugin,
PluginManager,
PluginUtils,
} from './types.js';