From 7bc2bf6ace5517cba49d76e1dd51aceb7d75e3f9 Mon Sep 17 00:00:00 2001 From: tianhao Date: Tue, 16 Jun 2026 12:41:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(#3,#4,#6,#8):=20=5Fpalette=E4=BB=8ETHEMES?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E9=A2=9C=E8=89=B2;=20countdown=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E6=9B=BF=E6=8D=A2;=20queue=E4=BF=9D=E7=95=99=E7=94=A8?= =?UTF-8?q?=E6=88=B7onClose;=20WeakMap=E2=86=92Map=E5=B9=B6=E6=B8=85?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/core.js b/src/core.js index cf39a3f..0d1170e 100644 --- a/src/core.js +++ b/src/core.js @@ -6,13 +6,15 @@ */ import { generateId, escapeHTML, prefersDark } from './utils.js'; -import { DEFAULTS, ICONS, TYPE_COLORS } from './constants.js'; +import { DEFAULTS, ICONS, TYPE_COLORS, THEMES } from './constants.js'; import { injectStyles } from './styles.js'; import { getTheme, applyTheme } from './themes.js'; import { t as i18nT, setCurrentLocale } from './i18n.js'; // 模块级共享容器缓存,所有 Toast 实例共用 -const _containerCache = new WeakMap(); +// 使用 Map 而非 WeakMap:document.body 在页面存活期间永不被 GC, +// 容器需要显式通过 destroy() 清理,WeakMap 在此场景无实际收益。 +const _containerCache = new Map(); /** * 参数标准化工具 @@ -90,17 +92,8 @@ class Toast { _palette() { const theme = getTheme(this.config.theme); const c = TYPE_COLORS[this.type] || TYPE_COLORS.default; - const t = theme === 'dark' - ? { - bg: 'rgba(28,32,40,.94)', - border: 'rgba(255,255,255,.08)', - shadow: '0 10px 36px -10px rgba(0,0,0,.6),0 4px 14px -4px rgba(0,0,0,.4)' - } - : { - bg: 'rgba(255,255,255,.96)', - border: 'rgba(0,0,0,.06)', - shadow: '0 10px 36px -10px rgba(0,0,0,.18),0 4px 14px -4px rgba(0,0,0,.08)' - }; + const tc = THEMES[theme] || THEMES.light; + const t = { bg: tc.bg, border: tc.border, shadow: tc.shadow }; return { theme, c, t }; } @@ -922,7 +915,7 @@ const meToast = { const t = this._emit({ ...opts, type: opts.type || 'warning', - message: message.replace('{seconds}', remaining), + message: message.replace(/\{seconds\}/g, remaining), duration: 0, closeButton: true, showProgress: false, @@ -946,7 +939,7 @@ const meToast = { } t.update({ - message: message.replace('{seconds}', remaining), + message: message.replace(/\{seconds\}/g, remaining), }); }; @@ -986,6 +979,7 @@ const meToast = { return new Promise((resolve) => { let index = 0; const delay = opts.delay || 1000; + const userOnClose = opts.onClose; const showNext = () => { if (index >= messages.length) { @@ -1003,7 +997,10 @@ const meToast = { ...((typeof message === 'object' && message !== null) ? message : {}), message: msg, duration: opts.duration || 3000, - onClose: () => { + onClose: (toast) => { + if (typeof userOnClose === 'function') { + try { userOnClose(toast); } catch (_) {} + } setTimeout(showNext, delay); }, }); @@ -1064,6 +1061,7 @@ const meToast = { } this._toasts.clear(); + _containerCache.clear(); }, getAll() {