3c9c9e740e
docs: 添加组件文档注释和类型导入 refactor: 统一使用 SnackbarOptions 类型 style: 优化导入语句顺序和格式 refactor: 简化假数据生成器中的faker导入和使用 feat(form-recognizer): 增强表单识别功能并优化UI交互 refactor: 移除未使用的组件文件 refactor(页面头部): 提取通用 PageHeader 组件 style(组件): 调整自动刷新开关和存储选项网格的样式 style(ui): 调整时间戳页面和结果视图的样式 ci(workflow): 移除Firefox测试以简化CI流程 refactor(qrcode): 重构二维码解析功能并提取为独立模块 style(页面样式): 统一页面头部图标颜色并优化表单样式 feat(formMapping): 添加表单映射功能 feat(表单映射): 添加配置导出功能及状态提示 feat(表单填充): 新增智能表单填充功能 ci: 精简触发CI的工作流分支 test: 更新路由测试以匹配新增的路由数量 test: 将测试文件中的描述和断言翻译为中文 test(PageHeader): 添加组件测试用例 refactor(theme): 重构主题样式并优化仪表盘卡片组件 refactor(Button): 统一按钮样式并移除重复样式定义 docs: 更新 README 文件中的目录结构说明 refactor: 优化代码类型声明和UI布局 refactor(消息通信): 使用 @webext-core/messaging 重构消息处理逻辑 fix: 修复依赖项缺失导致的潜在问题 refactor: 重构表单识别和消息处理逻辑 feat(侧边栏): 添加侧边栏状态变化通知功能 refactor: 优化高亮组件渲染逻辑并添加防抖处理 refactor: 统一组件导出方式为默认导出 refactor: 重构样式系统并迁移至 MUI 主题 docs: 更新项目文档以反映新增功能和技术栈细节 feat: 添加错误边界组件以捕获子组件错误 refactor(ui): 使用全局 snackbar 替换本地通知组件 refactor(snackbar): 重构全局消息提示为 SnackbarProvider 组件 refactor(Snackbar): 重构消息提示组件并优化样式 refactor: 重构路由和功能配置系统 refactor(GlobalSnackbar): 合并SnackbarProvider到GlobalSnackbar组件 feat(表单映射): 增强高亮器功能并优化元素定位 refactor: 移除表单映射相关功能 feat: 实现页面组件懒加载并优化加载状态显示 refactor: 移除未使用的数据生成和验证工具类 chore: 更新应用图标文件 refactor(qrcode): 替换二维码生成和解析库以减小体积 feat: 添加在标签页打开功能并优化错误处理 feat: 支持独立标签页模式并重构路由配置 feat(ui): 优化响应式布局和样式设计 refactor: 移除URL工具相关代码 refactor: 简化页面标题和存储选项网格的响应式布局 refactor: 优化样式和错误处理 refactor: 重构页面组件结构 style(DashboardPage): 优化仪表盘布局为响应式网格 refactor(ToolCard): 重构工具卡片组件并添加详细注释 feat: 新增文本统计功能 feat(jwt): 添加 JWT 解析工具功能 docs: 更新 README 以更清晰地描述项目功能和结构 docs: 更新AGENTS.md文档以反映项目最新状态 test: 更新特征计数测试以匹配新增工具 style: 统一导入语句顺序 refactor(pages): 重构工具页面组件结构并优化功能实现 feat(顶部栏): 添加搜索功能和历史记录 feat: 添加多语言支持(i18n) feat(顶部栏): 添加语言切换功能并优化国际化支持 feat(i18n): 增加语言归一化和同步功能 fix(剪贴板): 使用国际化文本替换剪贴板消息 test(顶部栏): 更新测试以匹配国际化标题 refactor: 重构组件目录结构 refactor(Dashboard): 优化卡片组件结构并移除无用属性 refactor(timestamp): 合并输入处理函数并优化样式结构 refactor(dashboard): 重构仪表盘卡片组件并优化布局 refactor(storageCleaner): 重构样式代码并优化国际化处理 refactor(StorageCleaner): 优化条件判断和清理未使用的代码 refactor(QrCode): 重构二维码页面样式和组件结构 refactor(ErrorBoundary): 移除冗余状态更新并调整错误背景色 feat(TopBar): 优化顶部导航栏功能并添加样式配置 chore: 在.gitignore中添加docs目录 feat(JsonDiff): 添加差异引擎类型定义 feat: 新增 JSON 差异比较工具功能 feat(主题): 添加暗色模式支持并重构主题系统
286 lines
8.7 KiB
TypeScript
286 lines
8.7 KiB
TypeScript
import { useState, useEffect, useCallback, useRef } from 'react';
|
|
import {
|
|
Accordion,
|
|
AccordionDetails,
|
|
AccordionSummary,
|
|
Alert,
|
|
Box,
|
|
Button,
|
|
CircularProgress,
|
|
IconButton,
|
|
InputAdornment,
|
|
Stack,
|
|
TextField,
|
|
Typography,
|
|
} from '@mui/material';
|
|
import LinkIcon from '@mui/icons-material/Link';
|
|
import ImageIcon from '@mui/icons-material/Image';
|
|
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
|
|
import ClearIcon from '@mui/icons-material/Clear';
|
|
import CopyButton from '@/components/CopyButton';
|
|
import { qrCodePageStyles } from '@/config/pageTheme';
|
|
import { useSnackbar } from '@/components/GlobalSnackbar';
|
|
import { parseQrCodeFromFile } from '@/utils/qrCodeParser';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
interface QrCodeToUrlSectionProps {
|
|
expanded: boolean;
|
|
onExpandedChange: (expanded: boolean) => void;
|
|
}
|
|
|
|
const QrCodeToUrlSection = ({ expanded, onExpandedChange }: QrCodeToUrlSectionProps) => {
|
|
const { t } = useTranslation(['qrCode']);
|
|
const { showMessage } = useSnackbar();
|
|
const [qrCodeFile, setQrCodeFile] = useState<File | null>(null);
|
|
const [previewUrl, setPreviewUrl] = useState<string>('');
|
|
const [parsedUrl, setParsedUrl] = useState('');
|
|
const [parseError, setParseError] = useState('');
|
|
const [parsing, setParsing] = useState(false);
|
|
const [dragging, setDragging] = useState(false);
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
// 清理预览 URL,防止内存泄漏
|
|
useEffect(() => {
|
|
return () => {
|
|
if (previewUrl) {
|
|
URL.revokeObjectURL(previewUrl);
|
|
}
|
|
};
|
|
}, [previewUrl]);
|
|
|
|
const handleFileChange = useCallback((file: File) => {
|
|
setQrCodeFile(file);
|
|
setPreviewUrl(URL.createObjectURL(file));
|
|
setParseError('');
|
|
setParsedUrl('');
|
|
}, []);
|
|
|
|
const handleClearFile = () => {
|
|
setQrCodeFile(null);
|
|
if (previewUrl) {
|
|
URL.revokeObjectURL(previewUrl);
|
|
}
|
|
setPreviewUrl('');
|
|
setParsedUrl('');
|
|
setParseError('');
|
|
showMessage(t('qrCode:imageCleared'), {
|
|
severity: 'success',
|
|
autoHideDuration: 1000,
|
|
});
|
|
};
|
|
|
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
if (e.target.files && e.target.files.length > 0) {
|
|
handleFileChange(e.target.files[0]);
|
|
}
|
|
};
|
|
|
|
const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => {
|
|
e.preventDefault();
|
|
setDragging(true);
|
|
};
|
|
|
|
const handleDragLeave = () => {
|
|
setDragging(false);
|
|
};
|
|
|
|
const handleDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
|
e.preventDefault();
|
|
setDragging(false);
|
|
const droppedFile = e.dataTransfer.files?.[0];
|
|
if (droppedFile) {
|
|
handleFileChange(droppedFile);
|
|
}
|
|
};
|
|
|
|
const parseQrCode = async () => {
|
|
if (!qrCodeFile) {
|
|
showMessage(t('qrCode:selectImage'), { severity: 'error', autoHideDuration: 300 });
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setParsing(true);
|
|
setParseError('');
|
|
setParsedUrl('');
|
|
|
|
const result = await parseQrCodeFromFile(qrCodeFile);
|
|
|
|
if (result.success && result.data) {
|
|
setParsedUrl(result.data);
|
|
showMessage(t('qrCode:parseSuccess'), { severity: 'success', autoHideDuration: 1000 });
|
|
} else {
|
|
showMessage(result.error || t('qrCode:noQrDetected'), {
|
|
severity: 'error',
|
|
autoHideDuration: 1000,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('解析二维码失败:', error);
|
|
showMessage(t('qrCode:parseError'), { severity: 'error', autoHideDuration: 300 });
|
|
} finally {
|
|
setParsing(false);
|
|
}
|
|
};
|
|
|
|
// 监听粘贴事件
|
|
useEffect(() => {
|
|
const handlePaste = async (e: ClipboardEvent) => {
|
|
if (!expanded) return;
|
|
|
|
const items = e.clipboardData?.items;
|
|
if (!items) return;
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
if (items[i].type.startsWith('image/')) {
|
|
e.preventDefault();
|
|
|
|
const file = items[i].getAsFile();
|
|
if (file) {
|
|
try {
|
|
handleFileChange(file);
|
|
showMessage(t('qrCode:imagePasted'), { severity: 'success', autoHideDuration: 1000 });
|
|
} catch (error) {
|
|
console.error('处理粘贴图片失败:', error);
|
|
showMessage(t('qrCode:imagePasteError'), {
|
|
severity: 'error',
|
|
autoHideDuration: 3000,
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
document.addEventListener('paste', handlePaste);
|
|
|
|
return () => {
|
|
document.removeEventListener('paste', handlePaste);
|
|
};
|
|
}, [expanded, showMessage, handleFileChange, t]);
|
|
|
|
return (
|
|
<Accordion
|
|
expanded={expanded}
|
|
onChange={(_, isExpanded) => onExpandedChange(isExpanded)}
|
|
sx={qrCodePageStyles.ACCORDION}
|
|
>
|
|
<AccordionSummary expandIcon={<ExpandMoreIcon />} sx={qrCodePageStyles.ACCORDION_SUMMARY}>
|
|
<Box sx={qrCodePageStyles.ACCORDION_TITLE_ICON}>
|
|
<LinkIcon color="success" />
|
|
<Typography variant="subtitle1" sx={qrCodePageStyles.ACCORDION_TITLE_TEXT}>
|
|
{t('qrCode:qrToUrl')}
|
|
</Typography>
|
|
</Box>
|
|
</AccordionSummary>
|
|
<AccordionDetails>
|
|
<Stack spacing={3}>
|
|
<Box
|
|
sx={qrCodePageStyles.DROPZONE(dragging, !!qrCodeFile)}
|
|
onDragOver={handleDragOver}
|
|
onDragLeave={handleDragLeave}
|
|
onDrop={handleDrop}
|
|
>
|
|
<input
|
|
ref={fileInputRef}
|
|
type="file"
|
|
accept="image/*"
|
|
onChange={handleInputChange}
|
|
style={{ display: 'none' }}
|
|
id="qr-code-upload"
|
|
/>
|
|
<label
|
|
htmlFor="qr-code-upload"
|
|
style={{ cursor: 'pointer', textAlign: 'center', width: '100%' }}
|
|
>
|
|
{qrCodeFile ? (
|
|
<Box sx={qrCodePageStyles.IMAGE_PREVIEW_WRAPPER}>
|
|
<Box sx={qrCodePageStyles.IMAGE_PREVIEW_BOX}>
|
|
<img
|
|
src={previewUrl}
|
|
alt="QR Code Preview"
|
|
style={qrCodePageStyles.IMAGE_PREVIEW_IMG}
|
|
/>
|
|
<IconButton
|
|
size="small"
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
handleClearFile();
|
|
}}
|
|
sx={qrCodePageStyles.CLEAR_BUTTON}
|
|
>
|
|
<ClearIcon fontSize="small" />
|
|
</IconButton>
|
|
</Box>
|
|
<Typography variant="body2" color="text.secondary" sx={{ mt: 2 }}>
|
|
{qrCodeFile.name}
|
|
</Typography>
|
|
<Typography variant="caption" color="text.secondary">
|
|
{t('qrCode:clickToChange')}
|
|
</Typography>
|
|
</Box>
|
|
) : (
|
|
<>
|
|
<ImageIcon sx={{ fontSize: 48, color: 'text.disabled', mb: 2 }} />
|
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 1 }}>
|
|
{t('qrCode:clickToUpload')}
|
|
</Typography>
|
|
<Typography variant="caption" color="text.secondary">
|
|
{t('qrCode:supportFormats')}
|
|
</Typography>
|
|
</>
|
|
)}
|
|
</label>
|
|
</Box>
|
|
|
|
<Button
|
|
variant="contained"
|
|
startIcon={parsing ? <CircularProgress size={16} color="inherit" /> : <LinkIcon />}
|
|
onClick={parseQrCode}
|
|
disabled={parsing}
|
|
sx={qrCodePageStyles.PRIMARY_BUTTON}
|
|
>
|
|
{parsing ? t('qrCode:parsing') : t('qrCode:parseButton')}
|
|
</Button>
|
|
|
|
<Box sx={qrCodePageStyles.RESULT_INPUT}>
|
|
<TextField
|
|
label={t('qrCode:resultLabel')}
|
|
value={parsedUrl}
|
|
fullWidth
|
|
variant="outlined"
|
|
slotProps={{
|
|
input: {
|
|
readOnly: true,
|
|
endAdornment: (
|
|
<InputAdornment position="end">
|
|
<CopyButton
|
|
text={parsedUrl}
|
|
tooltip={t('qrCode:copyTooltip')}
|
|
size="small"
|
|
color={qrCodePageStyles.primaryColor}
|
|
showMessage={showMessage}
|
|
/>
|
|
</InputAdornment>
|
|
),
|
|
},
|
|
}}
|
|
sx={qrCodePageStyles.INPUT_STYLE}
|
|
/>
|
|
</Box>
|
|
|
|
{parseError && (
|
|
<Alert severity="error" sx={{ borderRadius: 3 }}>
|
|
{parseError}
|
|
</Alert>
|
|
)}
|
|
</Stack>
|
|
</AccordionDetails>
|
|
</Accordion>
|
|
);
|
|
};
|
|
|
|
export default QrCodeToUrlSection;
|