251 lines
8.3 KiB
JavaScript
251 lines
8.3 KiB
JavaScript
/**
|
|
* Toast 3D Stack - 健壮版本
|
|
*/
|
|
|
|
(function (global) {
|
|
'use strict';
|
|
|
|
const icons = {
|
|
success: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>`,
|
|
error: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>`,
|
|
info: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>`,
|
|
warning: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>`
|
|
};
|
|
|
|
let toastId = 0;
|
|
const MAX_STACK = 5;
|
|
const DISPLAY_TIME = 4000;
|
|
let isReady = false;
|
|
const pendingQueue = [];
|
|
const isMobile = window.matchMedia('(max-width: 640px)').matches;
|
|
|
|
let touchStartY = 0;
|
|
let isDragging = false;
|
|
let currentToast = null;
|
|
|
|
function checkReady() {
|
|
return document.readyState === 'complete' || document.readyState === 'interactive';
|
|
}
|
|
|
|
function ensureReady(callback) {
|
|
if (isReady || checkReady()) {
|
|
isReady = true;
|
|
callback();
|
|
} else {
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
isReady = true;
|
|
callback();
|
|
}, { once: true });
|
|
}
|
|
}
|
|
|
|
function getContainer() {
|
|
let stage = document.getElementById('toast-stage');
|
|
if (!stage) {
|
|
stage = document.createElement('div');
|
|
stage.id = 'toast-stage';
|
|
const container = document.createElement('div');
|
|
container.id = 'toast-container';
|
|
stage.appendChild(container);
|
|
|
|
if (document.body) {
|
|
document.body.appendChild(stage);
|
|
} else {
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
document.body.appendChild(stage);
|
|
}, { once: true });
|
|
}
|
|
}
|
|
return stage.querySelector('#toast-container');
|
|
}
|
|
|
|
function createToast(message, type) {
|
|
const el = document.createElement('div');
|
|
el.className = `toast ${type} entering`;
|
|
el.dataset.id = ++toastId;
|
|
|
|
el.innerHTML = `
|
|
<div style="flex-shrink:0;width:20px;height:20px;">${icons[type] || icons.info}</div>
|
|
<div style="flex:1;min-width:0;">${escapeHtml(message)}</div>
|
|
`;
|
|
|
|
if (isMobile) {
|
|
attachTouchEvents(el);
|
|
}
|
|
|
|
return el;
|
|
}
|
|
|
|
function attachTouchEvents(el) {
|
|
el.addEventListener('touchstart', handleTouchStart, { passive: true });
|
|
el.addEventListener('touchmove', handleTouchMove, { passive: true });
|
|
el.addEventListener('touchend', handleTouchEnd, { passive: true });
|
|
el.addEventListener('click', () => removeToast(el));
|
|
}
|
|
|
|
function handleTouchStart(e) {
|
|
const touch = e.touches[0];
|
|
touchStartY = touch.clientY;
|
|
isDragging = true;
|
|
currentToast = e.currentTarget;
|
|
currentToast.classList.add('swiping');
|
|
}
|
|
|
|
function handleTouchMove(e) {
|
|
if (!isDragging || !currentToast) return;
|
|
const touch = e.touches[0];
|
|
const deltaY = touch.clientY - touchStartY;
|
|
if (deltaY > 0) {
|
|
const opacity = Math.max(0.3, 1 - deltaY / 200);
|
|
currentToast.style.transform = `translateY(${deltaY}px) scale(${Math.max(0.95, 1 - deltaY / 1000)})`;
|
|
currentToast.style.opacity = opacity;
|
|
}
|
|
}
|
|
|
|
function handleTouchEnd(e) {
|
|
if (!isDragging || !currentToast) return;
|
|
const touch = e.changedTouches[0];
|
|
const deltaY = touch.clientY - touchStartY;
|
|
currentToast.classList.remove('swiping');
|
|
if (deltaY > 80) {
|
|
removeToast(currentToast);
|
|
} else {
|
|
currentToast.style.transform = '';
|
|
currentToast.style.opacity = '';
|
|
}
|
|
isDragging = false;
|
|
currentToast = null;
|
|
}
|
|
|
|
function escapeHtml(text) {
|
|
const div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
|
|
function removeToast(el, immediate = false) {
|
|
if (!el || el.classList.contains('exiting')) return;
|
|
if (immediate) {
|
|
el.remove();
|
|
return;
|
|
}
|
|
|
|
el.classList.add('exiting');
|
|
if (isMobile) {
|
|
// 移动端出场:向下滑出 + 缩小,与入场对称
|
|
el.style.transform = 'translateY(20px) scale(0.95)';
|
|
el.style.opacity = '0';
|
|
el.style.transition = 'transform 0.3s ease, opacity 0.25s ease';
|
|
} else {
|
|
el.style.transform = 'translateX(100px) translateZ(-100px) scale(0.8) rotateY(-15deg)';
|
|
el.style.opacity = '0';
|
|
el.style.transition = 'transform 0.35s ease, opacity 0.3s ease';
|
|
}
|
|
el.style.pointerEvents = 'none';
|
|
|
|
setTimeout(() => {
|
|
if (el.parentNode) el.remove();
|
|
}, isMobile ? 300 : 350);
|
|
}
|
|
|
|
function processShow(message, type) {
|
|
const container = getContainer();
|
|
if (!container) {
|
|
pendingQueue.push({ message, type });
|
|
return;
|
|
}
|
|
|
|
if (isMobile && container.children.length > 0) {
|
|
Array.from(container.children).forEach(oldToast => removeToast(oldToast, true));
|
|
}
|
|
|
|
const existingToasts = Array.from(container.children);
|
|
if (!isMobile) {
|
|
existingToasts.forEach(toast => toast.classList.add('shift-down'));
|
|
}
|
|
|
|
const el = createToast(message, type);
|
|
container.prepend(el);
|
|
|
|
// 强制同步重排
|
|
void el.offsetHeight;
|
|
|
|
// 桌面端:强制设置初始状态
|
|
if (!isMobile) {
|
|
el.style.transform = 'translateX(-120px) translateZ(50px) rotateY(25deg)';
|
|
el.style.opacity = '0';
|
|
el.style.transition = 'none';
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
void el.offsetHeight;
|
|
|
|
requestAnimationFrame(() => {
|
|
el.classList.add('show');
|
|
|
|
if (!isMobile) {
|
|
el.style.transform = '';
|
|
el.style.opacity = '';
|
|
el.style.transition = '';
|
|
}
|
|
});
|
|
});
|
|
|
|
setTimeout(() => {
|
|
el.classList.remove('entering', 'show');
|
|
if (!isMobile) {
|
|
el.style.cssText = '';
|
|
existingToasts.forEach(toast => {
|
|
if (toast.parentNode) toast.classList.remove('shift-down');
|
|
});
|
|
}
|
|
}, 600);
|
|
|
|
if (!isMobile && container.children.length > MAX_STACK) {
|
|
const oldest = container.lastElementChild;
|
|
if (oldest) {
|
|
oldest.style.transition = 'all 0.3s ease';
|
|
oldest.style.opacity = '0';
|
|
oldest.style.transform = 'translateZ(-250px) scale(0.7)';
|
|
setTimeout(() => oldest.remove(), 300);
|
|
}
|
|
}
|
|
|
|
setTimeout(() => removeToast(el), DISPLAY_TIME);
|
|
}
|
|
|
|
function show(message, type = 'info') {
|
|
ensureReady(() => processShow(message, type));
|
|
}
|
|
|
|
function processQueue() {
|
|
while (pendingQueue.length > 0) {
|
|
const { message, type } = pendingQueue.shift();
|
|
processShow(message, type);
|
|
}
|
|
}
|
|
|
|
if (checkReady()) {
|
|
processQueue();
|
|
} else {
|
|
window.addEventListener('load', processQueue);
|
|
}
|
|
|
|
global.Toast = {
|
|
show,
|
|
success: (msg) => show(msg, 'success'),
|
|
error: (msg) => show(msg, 'error'),
|
|
info: (msg) => show(msg, 'info'),
|
|
warning: (msg) => show(msg, 'warning'),
|
|
clear: () => {
|
|
const container = getContainer();
|
|
if (container) {
|
|
Array.from(container.children).forEach(el => removeToast(el, true));
|
|
}
|
|
}
|
|
};
|
|
|
|
global.showToast = (msg, type = 'success') => show(msg, type);
|
|
|
|
})(window);
|