Files
monkey-ai-web/src/components/playground/ImagePreviewModal.jsx
T
zhanjunhao ac9bbfd472 fix(i18n): 补齐九语翻译并修复国际化检查
补齐历史缺译并统一整句插值。九语覆盖 100%,静态检查及 713 项单测通过。
2026-11-17 14:10:25 +08:00

183 lines
5.7 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 { useTranslation } from 'react-i18next';
import {
ZoomOut as ActionZoomOut,
ZoomIn as ActionZoomIn,
RotateCw as ActionRotateCw,
Download as ActionDownload,
X as ActionX,
} from '@/components/common/icons/ActionIcons';
import React, { useState, useCallback, useEffect } from 'react';
import { Modal } from '@/components/ui';
const ImagePreviewModal = ({ visible, imageUrl, onClose }) => {
const { t } = useTranslation();
const [scale, setScale] = useState(1);
useEffect(() => {
setScale(1);
}, [imageUrl]);
const handleZoomIn = () => setScale((s) => Math.min(s + 0.25, 3));
const handleZoomOut = () => setScale((s) => Math.max(s - 0.25, 0.25));
const handleResetZoom = () => setScale(1);
const handleDownload = useCallback(async () => {
try {
const response = await fetch(imageUrl);
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `image-${Date.now()}.png`;
document.body.appendChild(link);
link.click();
requestAnimationFrame(() => {
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
});
} catch {
window.open(imageUrl, '_blank');
}
}, [imageUrl]);
const handleClose = useCallback(() => {
setScale(1);
onClose();
}, [onClose]);
if (!imageUrl) return null;
const btnClass =
'w-9 h-9 flex items-center justify-center rounded-lg bg-white/10 hover:bg-white/20 text-white/80 hover:text-white transition-all';
return (
<Modal
visible={visible}
onCancel={handleClose}
footer={null}
closable={false}
maskClosable
width='100vw'
className='viewport-preview-modal'
style={{ maxWidth: '100vw', top: 0, padding: 0, margin: 0 }}
bodyStyle={{
padding: 0,
display: 'flex',
flexDirection: 'column',
// 用 dvh 替代 vh:移动端浏览器 URL bar 收缩时 vh 会缩水露出底栏,dvh 始终对齐"实际可见高度"
// 老浏览器不识别 dvh 时 fallback 到 vh,不会崩
height: '100dvh',
background: 'rgba(0,0,0,0.92)',
}}
>
{/* Toolbar — 顶部安全区(刘海屏 / 状态栏)和左右安全区都用 env() 兜底,
没有刘海的设备拿到的是 0,行为一致 */}
<div
className='flex items-center justify-between px-4 py-3 bg-black/40 flex-shrink-0'
style={{
paddingTop: 'max(env(safe-area-inset-top), 0.75rem)',
paddingLeft: 'max(env(safe-area-inset-left), 1rem)',
paddingRight: 'max(env(safe-area-inset-right), 1rem)',
}}
>
<div className='flex items-center gap-1.5'>
<button
onClick={handleZoomOut}
className={['app-action-button', btnClass]
.filter(Boolean)
.join(' ')}
>
<ActionZoomOut size={18} />
</button>
<span className='text-white/70 text-sm min-w-[48px] text-center'>
{Math.round(scale * 100)}%
</span>
<button
onClick={handleZoomIn}
className={['app-action-button', btnClass]
.filter(Boolean)
.join(' ')}
>
<ActionZoomIn size={18} />
</button>
<button
onClick={handleResetZoom}
className={['app-action-button', btnClass]
.filter(Boolean)
.join(' ')}
title={t('重置')}
>
<ActionRotateCw size={16} />
</button>
</div>
<div className='flex items-center gap-1.5'>
<button
onClick={handleDownload}
className={['app-action-button', btnClass]
.filter(Boolean)
.join(' ')}
>
<ActionDownload size={18} />
</button>
<button
onClick={handleClose}
className={['app-action-button', btnClass]
.filter(Boolean)
.join(' ')}
>
<ActionX size={20} />
</button>
</div>
</div>
{/* Image — 底部 home indicator 安全区也避一下 */}
<div
className='flex-1 flex items-center justify-center overflow-auto cursor-zoom-out'
style={{ paddingBottom: 'env(safe-area-inset-bottom)' }}
onClick={handleClose}
onWheel={(e) => {
e.preventDefault();
if (e.deltaY < 0) handleZoomIn();
else handleZoomOut();
}}
>
<img
src={imageUrl}
alt={t('预览')}
style={{
transform: `scale(${scale})`,
transition: 'transform 0.2s ease',
maxWidth: '90vw',
maxHeight: '85vh',
objectFit: 'contain',
}}
className='rounded-lg shadow-2xl'
onClick={(e) => e.stopPropagation()}
/>
</div>
</Modal>
);
};
export default ImagePreviewModal;