243 lines
8.4 KiB
React
243 lines
8.4 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
import React, { useContext, useMemo, useRef, useState, useEffect } from 'react';
|
|
import { StatusContext } from '../../context/Status';
|
|
import { Megaphone, X } from 'lucide-react';
|
|
import { Button } from 'antd';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useMarquee } from '../../hooks/common/useMarquee';
|
|
|
|
const Marquee = () => {
|
|
const { t } = useTranslation();
|
|
const [statusState] = useContext(StatusContext);
|
|
const { visible, dismiss } = useMarquee();
|
|
const marqueeContent = statusState?.status?.marquee_content || '';
|
|
const containerRef = useRef(null);
|
|
const measureRef = useRef(null);
|
|
const [ready, setReady] = useState(false);
|
|
const [scrollConfig, setScrollConfig] = useState({
|
|
padding: 0,
|
|
duration: 20,
|
|
});
|
|
|
|
// 将 | 分隔的多条公告拼接成一条,用分隔符分开
|
|
const displayText = useMemo(() => {
|
|
if (!marqueeContent.trim()) return '';
|
|
const items = marqueeContent
|
|
.split('|')
|
|
.map((s) => s.trim())
|
|
.filter(Boolean);
|
|
if (items.length === 0) return '';
|
|
// 每条公告之间添加装饰分隔符
|
|
return items.join(' ✦ ');
|
|
}, [marqueeContent]);
|
|
|
|
useEffect(() => {
|
|
if (!displayText) {
|
|
setReady(false);
|
|
return;
|
|
}
|
|
|
|
const update = () => {
|
|
const container = containerRef.current;
|
|
const measureEl = measureRef.current;
|
|
if (!container || !measureEl) return;
|
|
|
|
const containerWidth = container.offsetWidth;
|
|
const textWidth = measureEl.offsetWidth;
|
|
|
|
// 每份文本后留出容器宽度的间距
|
|
// 这样文本1从左边消失时,文本2刚好从右边进入
|
|
// 总 "单元" 宽度 = textWidth + containerWidth
|
|
// translateX(-50%) 正好移动一个单元,形成无缝循环
|
|
const scrollDistance = textWidth + containerWidth;
|
|
const speed = 50; // 像素/秒
|
|
const duration = Math.max(scrollDistance / speed, 5);
|
|
|
|
setScrollConfig({ padding: containerWidth, duration });
|
|
setReady(true);
|
|
};
|
|
|
|
// 等待下一帧让测量元素渲染完成
|
|
const raf = requestAnimationFrame(update);
|
|
window.addEventListener('resize', update);
|
|
return () => {
|
|
cancelAnimationFrame(raf);
|
|
window.removeEventListener('resize', update);
|
|
};
|
|
}, [displayText]);
|
|
|
|
if (!displayText || !visible) return null;
|
|
|
|
return (
|
|
<div className='marquee-bar relative w-full overflow-hidden z-[99]'>
|
|
{/* 顶部发光线 */}
|
|
<div className='absolute top-0 left-0 right-0 h-[1px] bg-gradient-to-r from-transparent via-white/50 to-transparent dark:via-white/20' />
|
|
|
|
<div className='flex items-center h-10 px-5'>
|
|
{/* 左侧图标 - 固定不动 */}
|
|
<div className='flex-shrink-0 flex items-center gap-2 mr-4 z-10'>
|
|
<div className='marquee-icon flex items-center justify-center w-6 h-6'>
|
|
<Megaphone size={13} />
|
|
</div>
|
|
<span className='text-[13px] font-semibold hidden sm:inline'>
|
|
{t('公告')}
|
|
</span>
|
|
</div>
|
|
|
|
{/* 跑马灯滚动区域 */}
|
|
<div ref={containerRef} className='flex-1 overflow-hidden relative'>
|
|
{/* 隐藏的测量元素 - 用于获取文本自然宽度,不受动态 padding 影响 */}
|
|
<span
|
|
ref={measureRef}
|
|
className='marquee-measure invisible absolute whitespace-nowrap text-[14px] font-medium tracking-wide'
|
|
style={{ top: -9999, left: -9999 }}
|
|
aria-hidden='true'
|
|
>
|
|
{displayText}
|
|
</span>
|
|
|
|
{/* 左侧渐变遮罩 */}
|
|
<div className='marquee-mask-left absolute left-0 top-0 bottom-0 w-10 z-10' />
|
|
{/* 右侧渐变遮罩 */}
|
|
<div className='marquee-mask-right absolute right-0 top-0 bottom-0 w-10 z-10' />
|
|
|
|
{ready && (
|
|
<div
|
|
className='marquee-scroll inline-flex whitespace-nowrap'
|
|
style={{ animationDuration: `${scrollConfig.duration}s` }}
|
|
>
|
|
<span
|
|
className='marquee-text inline-block shrink-0 text-[14px] text-white/95 dark:text-white/90 font-medium tracking-wide'
|
|
style={{ paddingRight: scrollConfig.padding }}
|
|
>
|
|
{displayText}
|
|
</span>
|
|
<span
|
|
className='marquee-text inline-block shrink-0 text-[14px] text-white/95 dark:text-white/90 font-medium tracking-wide'
|
|
style={{ paddingRight: scrollConfig.padding }}
|
|
aria-hidden='true'
|
|
>
|
|
{displayText}
|
|
</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<Button
|
|
type='text'
|
|
size='small'
|
|
className='marquee-close flex-shrink-0 ml-4 z-10'
|
|
aria-label={t('关闭')}
|
|
icon={<X size={14} aria-hidden />}
|
|
onClick={dismiss}
|
|
/>
|
|
</div>
|
|
|
|
{/* 底部发光线 */}
|
|
<div className='absolute bottom-0 left-0 right-0 h-[1px] bg-gradient-to-r from-transparent via-white/25 to-transparent dark:via-white/10' />
|
|
|
|
<style>{`
|
|
/* 亮色模式 - 跟随主题色的 5 段渐变 */
|
|
.marquee-bar {
|
|
background: linear-gradient(
|
|
90deg,
|
|
var(--brand-primary-hover) 0%,
|
|
var(--brand-gradient-via) 25%,
|
|
var(--brand-gradient-to) 50%,
|
|
var(--brand-primary) 75%,
|
|
var(--brand-primary-hover) 100%
|
|
);
|
|
background-size: 200% 100%;
|
|
animation: marquee-bg-shift 8s ease-in-out infinite;
|
|
box-shadow:
|
|
0 2px 8px color-mix(in srgb, var(--brand-primary) 30%, transparent),
|
|
inset 0 1px 0 rgba(255, 255, 255, 0.15);
|
|
}
|
|
|
|
.marquee-mask-left {
|
|
background: linear-gradient(to right, var(--brand-primary-hover), transparent);
|
|
}
|
|
|
|
.marquee-mask-right {
|
|
background: linear-gradient(to left, var(--brand-primary), transparent);
|
|
}
|
|
|
|
/* 深色模式 - 同色调但与黑色混合更沉稳 */
|
|
html.dark .marquee-bar {
|
|
background: linear-gradient(
|
|
90deg,
|
|
color-mix(in srgb, var(--brand-primary) 35%, #000) 0%,
|
|
color-mix(in srgb, var(--brand-gradient-via) 35%, #000) 25%,
|
|
color-mix(in srgb, var(--brand-primary) 40%, #000) 50%,
|
|
color-mix(in srgb, var(--brand-primary) 35%, #000) 75%,
|
|
color-mix(in srgb, var(--brand-primary) 35%, #000) 100%
|
|
);
|
|
background-size: 200% 100%;
|
|
box-shadow:
|
|
0 2px 12px color-mix(in srgb, var(--brand-primary) 30%, #000),
|
|
inset 0 1px 0 rgba(255, 255, 255, 0.05);
|
|
}
|
|
|
|
html.dark .marquee-mask-left {
|
|
background: linear-gradient(to right, color-mix(in srgb, var(--brand-primary) 35%, #000), transparent);
|
|
}
|
|
|
|
html.dark .marquee-mask-right {
|
|
background: linear-gradient(to left, color-mix(in srgb, var(--brand-primary) 35%, #000), transparent);
|
|
}
|
|
|
|
@keyframes marquee-bg-shift {
|
|
0%, 100% { background-position: 0% 0; }
|
|
50% { background-position: 100% 0; }
|
|
}
|
|
|
|
@keyframes marquee-scroll-anim {
|
|
0% {
|
|
transform: translateX(0);
|
|
}
|
|
100% {
|
|
transform: translateX(-50%);
|
|
}
|
|
}
|
|
|
|
.marquee-scroll {
|
|
animation: marquee-scroll-anim 20s linear infinite;
|
|
}
|
|
|
|
.marquee-scroll:hover {
|
|
animation-play-state: paused;
|
|
}
|
|
|
|
.marquee-text {
|
|
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15), 0 0 12px rgba(255, 255, 255, 0.1);
|
|
}
|
|
|
|
html.dark .marquee-text {
|
|
text-shadow:
|
|
0 1px 3px rgba(0, 0, 0, 0.4),
|
|
0 0 8px color-mix(in srgb, var(--brand-gradient-to) 15%, transparent);
|
|
}
|
|
`}</style>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Marquee;
|