- 覆盖率 96.42%(行):新增 59 测试共 385 个,覆盖 DOM 交互事件、对话框按钮交互、Intl/localStorage 异常路径、React 渲染生命周期 - 测试环境重构:SSR 分支用 defineProperty 真正覆盖 document/window;新增 tests/dom.test.ts、tests/setup.ts - jest 覆盖率门禁 lines >= 95%;CI lint 改必过;新增 publish.yml(v* tag 自动发布到 Gitea registry) - 修复:Toast.off 空数组残留、Toast.on 取消函数 this 绑定、action close:false 冒泡关闭、use() 重复注册钩子、i18n catch 引用错误 - 删除死代码 autoApplySystemTheme;lint 零警告(no-console 策略 + caughtErrorsIgnorePattern)
123 lines
3.1 KiB
TypeScript
123 lines
3.1 KiB
TypeScript
/**
|
|
* MetonaToast — 轻量级Toast通知库
|
|
* @module metona-toast
|
|
* @version 0.5.0
|
|
* @author thzxx
|
|
* @license MIT
|
|
*/
|
|
|
|
import { meToast } from './api.js';
|
|
import { Toast } 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 { VERSION } from './constants.js';
|
|
import type { ToastInstance } from './types.js';
|
|
|
|
/**
|
|
* 增强的 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();
|
|
}
|
|
|
|
// 全局钩子 — 主题/语言变化转发到 Toast 钩子系统
|
|
if (typeof themeUtils.addThemeListener === 'function') {
|
|
themeUtils.addThemeListener(() => Toast.trigger('themeChange'));
|
|
}
|
|
if (typeof i18nUtils.addLocaleListener === 'function') {
|
|
i18nUtils.addLocaleListener(() => Toast.trigger('localeChange'));
|
|
}
|
|
|
|
// 浏览器环境全局注册
|
|
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';
|