Compare commits

..

7 Commits

Author SHA1 Message Date
Cursor Agent d5907ce25b docs(工程文档): 格式化文档表格
Co-authored-by: LingandRX <LingandRX@users.noreply.github.com>
2026-06-26 08:43:20 +00:00
Cursor Agent 3cac909683 docs(工程文档): 同步当前架构说明
Co-authored-by: LingandRX <LingandRX@users.noreply.github.com>
2026-06-26 08:42:28 +00:00
LingandRX a163297de4 develop (#68)
* docs(AGENTS.md): 添加 Git Commit 必须使用中文描述的规范
* docs: 更新测试数据生成器功能设计文档
* refactor: 删除死代码
* docs: 统一测试数据生成器文档,移除 SQL/TypeScript 导出,修复接口命名不一致
* feat: 实现测试数据生成器功能
* docs: 更新 README 和 AGENTS 文档,补充测试数据生成器模块说明
2026-06-07 00:04:53 +08:00
LingandRX a5d86a92c2 develop (#67)
feat: 升级项目 Logo、完善 toast 提示及优化工程化配置

### Features
- **logo**: 替换全套项目图标,更新 wxt.config.ts 浏览器扩展图标配置。

### Bug Fixes
- **ui/toast**: 引入 <Toaster> 组件修复 toast 无法显示问题,并调整位置为正下方。
- **i18n**: 统一将 t() 函数中的 `.` 和 `:` 转换为 `_`,修复国际化文案读取失败问题。
- **context-menu**: 限制 srcUrl 检查仅在二维码图片菜单项生效,避免误判。
- **hooks**: 修复 pre-push 钩子,仅对变更文件进行 eslint 检查,并支持无 upstream 时的回退对比。
- **components**: 修复 CopyButton 动画与组件导入方式,并补齐无障碍属性与测试。
- **async**: 延迟生成二维码以正确渲染 loading 状态,并适配相关异步测试。

### Refactor & Cleans
- **clean**: 清理未使用的组件(ToolCard)、Hook(useDebounce)及多个未使用的工具函数导出。
- **structure**: 统一移动测试文件至 __tests__ 目录,移除不必要的类型导出。

### Documentation
- **docs**: 在 AGENTS.md 中明确规范 Git Commit 必须使用中文描述。
2026-06-06 01:07:27 +08:00
LingandRX c992986789 fix(CopyButton): 修复 review 反馈并补充测试 (#66)
* fix: remove non-functional animation classes from CopyButton
* test(CopyButton): 补充单元测试覆盖边界场景
* fix: unify CopyButton imports to named export across all consumers
* fix(CopyButton): add aria-label for screen reader accessibility
2026-06-05 21:15:48 +08:00
LingandRX 70b799aa2d fix: add missing Toaster component to enable toast display (#65)
The project was calling toast.success/error/warning from sonner in 7 files,
but the <Toaster> component was never rendered in the React tree. This meant
sonner stored toast data internally but no UI was ever displayed to users.

Changes:
- Create src/components/ui/sonner.tsx with shadcn/ui-style Toaster wrapper
  that integrates with ThemeModeProvider for light/dark theme support
- Add <Toaster /> to AppRoot.tsx so it works across all entry points
  (popup, sidepanel, browser-tab)

fix: always convert dots to underscores in i18n t() key lookup

The t() function only converted dots to underscores when the key contained
a colon (namespace:key format). Keys using dot notation like
'messages.copySuccess' were passed directly to chrome.i18n.getMessage()
without dot-to-underscore conversion, causing lookups to fail silently and
return empty strings - resulting in toast notifications with no text.

Now all separators (both ':' and '.') are consistently converted to '_'
regardless of format, matching the messages.json key convention.

fix: position toast at bottom-center instead of bottom-right
2026-06-05 19:29:21 +08:00
LingandRX 58af2af37b fix: 右键菜单 srcUrl 误判、pre-push 变更文件检测、generating 状态不可见 (#64)
* fix: 修复右键菜单 srcUrl 误判、pre-push 全量扫描、generating 状态不可见问题

- contextMenu: srcUrl 检查限定为 qrCode-image 菜单项,避免其他菜单误返回图片 URL
- pre-push: eslint 改为只检查变更文件(git diff 过滤),tsc 保留全项目检查
- useQrCode: confirmGenerate 用 setTimeout 延迟生成,让 loading 状态先被 React 渲染
- tests: 生成相关测试改为 async + waitFor 适配异步 confirmGenerate

* fix(hooks): pre-push 无 upstream 时回退到 origin/main 对比

* fix(hooks): 用 merge-base 替代 rev-parse 修复 pre-push 变更文件检测
2026-06-05 13:10:44 +08:00
101 changed files with 2831 additions and 997 deletions
-17
View File
@@ -1,17 +0,0 @@
# CI/CD 配置
## CI 步骤
严格顺序,任一步骤失败则停止并标记 CI 失败:
1. `setup`(安装依赖、`wxt prepare`
2. 并行运行 `lint``typecheck``test`(三者全部通过才继续)
3. `build`(仅当步骤 2 全部成功时执行)
## Pre-commit Hook
`.husky/pre-commit` 调用 `lint-staged`,任一步骤返回非零则终止提交:
1. 代码文件 (`*.{ts,tsx,js,jsx,mjs}`):运行 `eslint --fix --max-warnings=0 --no-warn-ignored`;若失败则终止并报告错误
2. 同一代码文件:运行 `prettier --write`
3. 其他文件 (`*.{json,css,scss,md}`):运行 `prettier --write`
+42 -38
View File
@@ -285,8 +285,7 @@ import { useThemeMode } from '@/providers/ThemeModeProvider';
import { FeatureConfig, FEATURES } from '@/config/features'; import { FeatureConfig, FEATURES } from '@/config/features';
import { storageUtil } from '@/utils/chromeStorage'; import { storageUtil } from '@/utils/chromeStorage';
// 5. i18n // 5. i18n
import { useTranslation } from 'react-i18next'; import { useI18n } from '@/utils/chromeI18n';
import { normalizeLanguage, SUPPORTED_LANGUAGES } from '@/i18n';
// 6. 本地组件 // 6. 本地组件
import TextMode from './TextMode'; import TextMode from './TextMode';
import { ZONES } from './constants'; import { ZONES } from './constants';
@@ -295,7 +294,7 @@ import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
// 8. 工具函数 / Hook // 8. 工具函数 / Hook
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { useLazyTranslation } from '@/utils/useLazyTranslation'; import { useStorageState } from '@/utils/useStorageState';
// 9. 类型 // 9. 类型
import type { PageType, StorageSchema } from '@/types/storage'; import type { PageType, StorageSchema } from '@/types/storage';
``` ```
@@ -401,22 +400,22 @@ className={cn(
## 5. 错误处理 ## 5. 错误处理
### 5.1 工具函数:结果对象模式 ### 5.1 工具函数:可恢复错误返回可判断结果
工具函数**不抛异常**,返回包含 `hasError``error` 字段的结果对象: 可恢复的解析/校验错误应返回可判断的结果,避免工具层直接弹 Toast。确需保留底层异常的函数
(如 `formatJson` / `minifyJson`)必须在页面 Hook 或 UI 层捕获并转换为用户提示:
```typescript ```typescript
// ✅ 结果对象模式 // ✅ 可恢复校验返回错误消息,调用方据此展示 UI
export function markdownToHtml(markdown: string): MarkdownToHtmlResult { export function validateJson(text: string): string | null {
if (!text.trim()) {
return null;
}
try { try {
... JSON.parse(text.trim());
return { html, originalLength, htmlLength, hasError: false }; return null;
} catch (error) { } catch (e) {
return { return e instanceof SyntaxError ? e.message : 'Invalid JSON';
html: '', ...
hasError: true,
error: error instanceof Error ? error.message : 'Markdown 解析失败',
};
} }
} }
``` ```
@@ -658,7 +657,7 @@ export function useTimestampConverter(): UseTimestampConverterReturn { ... }
``` ```
src/utils/useStorageState.ts — Chrome Storage 状态持久化 src/utils/useStorageState.ts — Chrome Storage 状态持久化
src/utils/useLazyTranslation.ts — i18n 懒加载 src/utils/chromeI18n.ts — chrome.i18n wrapper 与 useI18n
src/utils/useContextMenuData.ts — 右键菜单数据 src/utils/useContextMenuData.ts — 右键菜单数据
src/utils/useDebounce.ts — 防抖 src/utils/useDebounce.ts — 防抖
src/pages/Timestamp/useTimestampConverter.ts — 页面级 Hook src/pages/Timestamp/useTimestampConverter.ts — 页面级 Hook
@@ -671,43 +670,48 @@ src/pages/StorageCleaner/useStorageCleaner.ts — 页面级 Hook
### 9.1 翻译键格式 ### 9.1 翻译键格式
- 命名空间:`common`(默认)、`features` - 使用 Chrome 扩展标准的 `chrome.i18n`,通过 `src/utils/chromeI18n.ts` 暴露 `useI18n`
- 翻译键格式:`namespace:key`(如 `features:timestamp.title` - 翻译 key 存放在 `public/_locales/zh_CN/messages.json`
- 语言:`zh`(默认)、`en` - 直接 key`t('dashboard_title')` → 查找 `dashboard_title`
- 命名空间兼容写法:`t('common:buttons.search')` → 查找 `common_buttons_search`
- 命名空间参数:`useI18n(['common', 'features'])` 会尝试 `common_key``features_key`
### 9.2 翻译文件结构 ### 9.2 翻译文件结构
``` ```
i18n/locales/{zh,en}/common.json — 全局通用翻译 public/_locales/zh_CN/messages.json — Chrome 扩展默认语言包
i18n/locales/{zh,en}/features.json — 功能模块标题和描述 wxt.config.ts — manifest.default_locale = 'zh_CN'
i18n/locales/{zh,en}/{功能名}.json — 各功能独立翻译
``` ```
### 9.3 使用方式 ### 9.3 使用方式
```typescript ```typescript
// ✅ 页面组件 — 使用 useLazyTranslation // ✅ 页面组件 / 子组件 — 使用 useI18n
import { useLazyTranslation } from '@/utils/useLazyTranslation'; import { useI18n } from '@/utils/chromeI18n';
export default function Index() { export default function Index() {
const { t } = useLazyTranslation('timestamp'); const { t } = useI18n('timestamp');
return <h1>{t('timestamp:title')}</h1>; return <h1>{t('timestamp_title')}</h1>;
} }
// ✅ 全局组件 — 使用 useTranslation // ✅ 带占位符
import { useTranslation } from 'react-i18next'; export function NotFoundMessage() {
const { t } = useI18n('router');
export function TopBar() { return <p>{t('router_notFoundDescription', { entryPointType: 'popup' })}</p>;
const { t } = useTranslation(['common', 'features']);
return <span>{t('common:settings')}</span>;
} }
``` ```
### 9.4 添加新翻译 ### 9.4 添加新翻译
1.`i18n/locales/{zh,en}/features.json` 添加功能标题和描述 1.`public/_locales/zh_CN/messages.json` 添加 Chrome 扩展格式的消息:
2. 创建 `i18n/locales/{zh,en}/{功能名}.json` 添加功能专属翻译 ```json
3.`utils/useLazyTranslation.ts``localeModules` 中注册新命名空间 {
"feature_title": { "message": "功能标题" },
"feature_description": { "message": "功能描述" }
}
```
2. key 使用下划线分隔,避免点号;`useI18n` 会把 `namespace:key.path` 兼容转换为下划线
3. `chrome.i18n` 不支持运行时动态切换语言,浏览器语言变化后需要刷新扩展页面
--- ---
@@ -1083,7 +1087,7 @@ export default function Index() {
4. ✅ 业务逻辑提取到 `useXxx.ts` Hookindex.tsx 不超过 150 行) 4. ✅ 业务逻辑提取到 `useXxx.ts` Hookindex.tsx 不超过 150 行)
5. ✅ 需要持久化的 UI 状态使用 `useStorageState` 5. ✅ 需要持久化的 UI 状态使用 `useStorageState`
6. ✅ 常量 ≥3 个时提取到 `constants.ts` 6. ✅ 常量 ≥3 个时提取到 `constants.ts`
7. ✅ 在 `i18n/locales/{zh,en}/` 添加翻译 7. ✅ 在 `public/_locales/zh_CN/messages.json` 添加翻译
8. ✅ 创建 `__tests__/index.test.tsx` 测试文件 8. ✅ 创建 `__tests__/index.test.tsx` 测试文件
9. ✅ 如需新权限,更新 `wxt.config.ts` 的 `manifest.permissions` 9. ✅ 如需新权限,更新 `wxt.config.ts` 的 `manifest.permissions`
10. ✅ 运行 `npm run lint && npm run typecheck && npm run test` 全部通过 10. ✅ 运行 `npm run lint && npm run typecheck && npm run test` 全部通过
@@ -1103,8 +1107,8 @@ export default function Index() {
| `src/hooks/` | 自定义 React Hooks | | `src/hooks/` | 自定义 React Hooks |
| `src/utils/` | 工具函数与服务抽象 | | `src/utils/` | 工具函数与服务抽象 |
| `src/types/` | TypeScript 类型声明 | | `src/types/` | TypeScript 类型声明 |
| `src/lib/` | 通用工具函数(cn、utils | | `src/lib/` | 通用工具函数与生成器库cn、utils、generators |
| `public/` | 静态资源 | | `public/` | 静态资源与 Chrome `_locales` 语言包 |
--- ---
+11 -12
View File
@@ -1,6 +1,6 @@
# Copilot 指令 # Copilot 指令
基于 WXT 框架的浏览器扩展项目(React 19 + TypeScript),为开发者和测试人员提供效率工具:时间戳转换、存储清理、JWT 解析、JSON 工具、二维码、Base64、Markdown 等。 基于 WXT 框架的浏览器扩展项目(React 19 + TypeScript),为开发者和测试人员提供效率工具:时间戳转换、存储清理、JWT 解析、JSON 工具、二维码、Base64、测试数据生成器等。
## 核心命令 ## 核心命令
@@ -74,10 +74,10 @@ pages/FeatureName/
1.`types/storage.d.ts``PageType` 联合类型中添加新成员 1.`types/storage.d.ts``PageType` 联合类型中添加新成员
2.`config/features.tsx``FEATURES` 数组中添加配置(key、翻译键、图标、三种渲染模式组件) 2.`config/features.tsx``FEATURES` 数组中添加配置(key、翻译键、图标、三种渲染模式组件)
3.`pages/` 目录创建页面组件(懒加载): 3.`pages/` 目录创建页面组件(懒加载):
- `index.tsx` — 使用 `useLazyTranslation` 的 UI 组件 - `index.tsx` — 使用 `useI18n` 的 UI 组件
- `useFeatureName.ts` — 业务逻辑 Hook - `useFeatureName.ts` — 业务逻辑 Hook
- `constants.ts` — 常量(可选) - `constants.ts` — 常量(可选)
4.`i18n/locales/{zh,en}/features.json` 添加翻译(复杂功能可新建独立 JSON 文件) 4.`public/_locales/zh_CN/messages.json` 添加 Chrome i18n 翻译
5. 如需新权限,更新 `wxt.config.ts``manifest.permissions` 5. 如需新权限,更新 `wxt.config.ts``manifest.permissions`
6. 添加对应的单元测试 6. 添加对应的单元测试
@@ -102,7 +102,7 @@ pages/FeatureName/
### 代码分割 ### 代码分割
`wxt.config.ts` 通过 `manualChunksForHtmlOnly()` 自动分组 vendor 依赖(vendor-react、vendor-i18n、vendor-qr、vendor-dnd),无需手动配置。 `wxt.config.ts` 通过 `manualChunksForHtmlOnly()` 自动分组 vendor 依赖(vendor-react、vendor-qr、vendor-dnd),无需手动配置。
### 代码风格 ### 代码风格
@@ -118,8 +118,7 @@ pages/FeatureName/
- 全局变量:`vitest/globals`describe、it、expect 等无需导入) - 全局变量:`vitest/globals`describe、it、expect 等无需导入)
- Setup 文件:`vitest.setup.ts` 自动 mock 以下内容: - Setup 文件:`vitest.setup.ts` 自动 mock 以下内容:
- `chrome.*` / `browser.*` APIstorage、tabs、runtime、cookies 等) - `chrome.*` / `browser.*` APIstorage、tabs、runtime、cookies 等)
- `react-i18next`(返回 key 作为翻译) - `@/utils/chromeI18n`(从 `public/_locales/zh_CN/messages.json` 加载真实翻译)
- `@/utils/useLazyTranslation`(返回 `ns:key` 格式)
- `window.matchMedia` - `window.matchMedia`
- 测试文件命名:`__tests__/*.test.{ts,tsx}``*.test.{ts,tsx}` - 测试文件命名:`__tests__/*.test.{ts,tsx}``*.test.{ts,tsx}`
- 使用 `vi.mock()` 进行模块级 mock;避免重复 mock `vitest.setup.ts` 中已有的内容 - 使用 `vi.mock()` 进行模块级 mock;避免重复 mock `vitest.setup.ts` 中已有的内容
@@ -127,12 +126,12 @@ pages/FeatureName/
### 国际化(i18n ### 国际化(i18n
- 命名空间:`common`(默认)、`features` - 使用 Chrome 扩展标准 `chrome.i18n`
- 翻译键格式:`namespace:key`(如 `features:timestamp.title` - 默认语言目录:`public/_locales/zh_CN/messages.json`
- 语言:`zh`(默认)、`en` - 使用方式:`import { useI18n } from '@/utils/chromeI18n'`
- 翻译文件:`i18n/locales/{zh,en}/{common,features}.json` + 各功能独立 JSON 文件 - 翻译键格式:直接 key(如 `timestamp_title`);兼容 `namespace:key.path` 并转换为下划线
- 使用 `useLazyTranslation` Hook 加载功能专属翻译 - 回退策略:缺失翻译返回 key 本身,并在开发模式下记录 warning
- 回退策略:缺失的翻译键回退到 `zh`,若仍缺失则返回占位格式 `namespace:key` - 限制:`chrome.i18n` 跟随浏览器语言,不能在运行时动态切换语言
### WXT 生成文件 ### WXT 生成文件
+77 -8
View File
@@ -1,6 +1,6 @@
# AGENTS.md # AGENTS.md
WXT 浏览器扩展项目 (React 19 + TypeScript)。提供时间戳转换、存储清理、JWT 解析、JSON 工具、二维码、Base64、Markdown、测试数据生成器等测试效率工具。 WXT 浏览器扩展项目 (React 19 + TypeScript)。提供时间戳转换、存储清理、JWT 解析、JSON 工具、二维码、Base64、测试数据生成器等测试效率工具。
## 核心命令 ## 核心命令
@@ -18,9 +18,27 @@ npm run test:watch # vitest 监视模式
npm run test:coverage # 带覆盖率的测试 npm run test:coverage # 带覆盖率的测试
``` ```
运行单个测试: `npx vitest run path/to/file.test.ts`
## 验证流程 ## 验证流程
详见 [CI 配置](./.github/CI.md) CI 步骤(严格顺序,任一步骤失败则停止并标记 CI 失败):
1. `setup`(安装依赖、`wxt prepare`
2. 并行运行 `lint``typecheck``test`(三者全部通过才继续)
3. `build`(仅当步骤 2 全部成功时执行)
Pre-commit hook`.husky/pre-commit` 调用 `lint-staged`,任一步骤返回非零则终止提交):
1. 代码文件 (`*.{ts,tsx,js,jsx,mjs}`):运行 `eslint --fix --max-warnings=0 --no-warn-ignored`;若失败则终止并报告错误
2. 同一代码文件:运行 `prettier --write`
3. 其他文件 (`*.{json,css,scss,md}`):运行 `prettier --write`
## WXT 生成文件
- `.wxt/` 目录由 `postinstall` 自动执行 `wxt prepare` 生成,包含 TypeScript 类型声明和扩展的 tsconfig。
- 生产构建输出到 `.output/` 目录。
- `tsconfig.json` 继承自 `./.wxt/tsconfig.json`
## 项目结构 ## 项目结构
@@ -55,10 +73,42 @@ src/pages/FeatureName/
- 页面入口组件统一命名为 `Index`,通过 `export default function Index()` 导出 - 页面入口组件统一命名为 `Index`,通过 `export default function Index()` 导出
- Hook 负责所有状态管理和业务逻辑,通过返回值暴露给页面 - Hook 负责所有状态管理和业务逻辑,通过返回值暴露给页面
- 子组件可以独立调用全局 Hook - 子组件可以独立调用 `useI18n`全局 Hook
-`index.tsx` 超过 150 行时,必须拆分为 UI + Hook 模式 -`index.tsx` 超过 150 行时,必须拆分为 UI + Hook 模式
- 复杂页面可增加 `contexts/``hooks/``components/` 子目录 - 复杂页面可增加 `contexts/``hooks/``components/` 子目录
### 测试数据生成器模块
```
src/pages/TestDataGenerator/
├── index.tsx # 主页面(字段配置 + 标签页切换)
├── hooks/useGenerator.ts # Web Worker 管理 Hook(创建、复用、通信、销毁)
└── components/
├── FieldList.tsx # 字段列表(虚拟滚动 + @dnd-kit 拖拽排序 + 规则保存)
├── FieldItem.tsx # 字段卡片展示
├── FieldEditor.tsx # 字段编辑器(名称校验、生成器选择、参数配置)
├── GeneratorSelector.tsx # 生成器选择器(分类 + 搜索)
├── GeneratorConfig.tsx # 生成器参数表单(动态渲染 string/number/boolean/select/array
├── GenerateOptions.tsx # 生成选项(数量、格式)
├── GenerateButton.tsx # 生成按钮 + 进度条
├── DataPreview.tsx # 示例数据预览(JSON 语法高亮)
├── ResultPanel.tsx # 生成结果状态面板
├── ExportPanel.tsx # 导出面板(复制/下载 JSON/CSV)
└── RuleManager.tsx # 规则管理(CRUD、搜索、导入/导出)
src/utils/
├── ruleStorage.ts # 规则持久化存储(localStorage
└── dataExporter.ts # 数据导出工具(JSON/CSV 转换、下载、剪贴板)
src/lib/generators/ # 内置生成器定义(个人信息、企业、技术、基础类型)
src/workers/
└── generator.worker.ts # 数据生成 Web Worker
src/types/
└── testDataGenerator.ts # 类型定义(FieldConfig, DataRule, GeneratorDefinition 等)
```
## 关键架构决策 ## 关键架构决策
**路由**: 不使用 React Router。通过 `src/config/features.tsx``FEATURES` 数组管理,`RouterProvider` 根据 `PageType` **路由**: 不使用 React Router。通过 `src/config/features.tsx``FEATURES` 数组管理,`RouterProvider` 根据 `PageType`
@@ -67,6 +117,7 @@ src/pages/FeatureName/
**存储**: 所有 Chrome Storage 键必须在 `src/types/storage.d.ts``StorageSchema` 中定义,键名使用 kebab-case 格式(如 `app/currentRoute`)。 **存储**: 所有 Chrome Storage 键必须在 `src/types/storage.d.ts``StorageSchema` 中定义,键名使用 kebab-case 格式(如 `app/currentRoute`)。
使用 `src/utils/chromeStorage.ts` 及其 Hook。Router 同时使用 `chrome.storage.local``localStorage` 做快照以消除首屏闪烁。 使用 `src/utils/chromeStorage.ts` 及其 Hook。Router 同时使用 `chrome.storage.local``localStorage` 做快照以消除首屏闪烁。
修改 StorageSchema 时,必须在 `src/utils/chromeStorage.ts` 添加版本迁移函数,并在测试中覆盖迁移场景。
**通信**: 使用 `@webext-core/messaging`,协议定义在 `src/utils/messages.ts` **通信**: 使用 `@webext-core/messaging`,协议定义在 `src/utils/messages.ts`
@@ -82,21 +133,39 @@ src/pages/FeatureName/
- 全局变量: `vitest/globals` (describe, it, expect 等无需导入) - 全局变量: `vitest/globals` (describe, it, expect 等无需导入)
- Setup 文件: `vitest.setup.ts` 自动 mock: - Setup 文件: `vitest.setup.ts` 自动 mock:
- `chrome.*` / `browser.*` API (storage, tabs, runtime, cookies 等) - `chrome.*` / `browser.*` API (storage, tabs, runtime, cookies 等)
- `@/utils/chromeI18n` (从 `public/_locales/zh_CN/messages.json` 加载真实翻译)
- `window.matchMedia` - `window.matchMedia`
- 测试文件命名: `__tests__/*.test.{ts,tsx}``*.test.{ts,tsx}` - 测试文件命名: `__tests__/*.test.{ts,tsx}``*.test.{ts,tsx}`
- Mock 模式: 使用 `vi.mock()` 进行模块级 mock,避免在测试文件中重复 mock 代码 - Mock 模式: 使用 `vi.mock()` 进行模块级 mock,避免在测试文件中重复 mock 代码
- 测试工具: `@testing-library/react` + `@testing-library/user-event` 进行组件测试 - 测试工具: `@testing-library/react` + `@testing-library/user-event` 进行组件测试
## i18n (chrome.i18n)
项目使用 Chrome 扩展标准的 `chrome.i18n` API 进行本地化,通过 `src/utils/chromeI18n.ts` 提供类型安全的 React Hook 包装。
- **翻译文件**: `public/_locales/zh_CN/messages.json`Chrome 扩展标准格式)
- **默认语言**: `zh_CN`(在 `wxt.config.ts``manifest.default_locale` 中配置)
- **使用方式**: `import { useI18n } from '@/utils/chromeI18n'`
- **翻译键格式**:
- 直接 key: `t('dashboard_title')` → 查找 `dashboard_title`
- 命名空间格式(兼容旧用法): `t('common:buttons.search')` → 查找 `common_buttons_search`
- 带命名空间参数: `useI18n(['common', 'features'])`,会自动尝试 `common_key``features_key`
- **占位符支持**: `t('router_notFoundDescription', { entryPointType: 'popup' })`
- **Hook 返回值**: `{ t, i18n: { language, changeLanguage }, isLoaded }`
- **回退策略**: 当翻译 key 未命中时,返回 key 本身(开发模式下在控制台记录 warning)
- **限制**: `chrome.i18n` 无法动态切换语言,语言跟随浏览器设置,切换后需刷新页面
## 新功能开发清单 ## 新功能开发清单
1.`src/types/storage.d.ts` 添加 `PageType` 联合类型 1.`src/types/storage.d.ts` 添加 `PageType` 联合类型
2.`src/config/features.tsx``FEATURES` 数组添加配置(指定 key、翻译键、图标、三种渲染模式的组件) 2.`src/config/features.tsx``FEATURES` 数组添加配置(指定 key、翻译键、图标、三种渲染模式的组件)
3.`src/pages/` 创建页面组件 (懒加载) 3.`src/pages/` 创建页面组件 (懒加载)
- `index.tsx` — UI 组件 - `index.tsx` — UI 组件,使用 `useI18n` 获取翻译
- `use{FeatureName}.ts` — 业务逻辑 Hook - `useFeatureName.ts` — 业务逻辑 Hook
- `constants.ts` — 常量(可选) - `constants.ts` — 常量(可选)
4. 如需新权限,更新 `wxt.config.ts``manifest.permissions`;如有不使用的权限,需移除 4. `public/_locales/zh_CN/messages.json` 添加翻译
5. 添加对应的单元测试 5. 如需新权限,更新 `wxt.config.ts``manifest.permissions`
6. 添加对应的单元测试
## 代码规范 ## 代码规范
@@ -108,7 +177,6 @@ src/pages/FeatureName/
- 格式: Prettier (`.prettierrc`: 100 字符宽, 单引号, 尾逗号 all, LF 换行) - 格式: Prettier (`.prettierrc`: 100 字符宽, 单引号, 尾逗号 all, LF 换行)
- ESLint 使用 `typescript-eslint``projectService: true`(无需手动维护 project 路径) - ESLint 使用 `typescript-eslint``projectService: true`(无需手动维护 project 路径)
- **Git Commit**: 必须使用中文描述,遵循 Conventional Commits 规范(如 `fix(组件名): 描述``feat(功能名): 描述` - **Git Commit**: 必须使用中文描述,遵循 Conventional Commits 规范(如 `fix(组件名): 描述``feat(功能名): 描述`
- **测试维护**: 新增/编辑已有功能/组件时,需要针对相关的测试进行更新
## 关键外部库(非显而易见的) ## 关键外部库(非显而易见的)
@@ -117,3 +185,4 @@ src/pages/FeatureName/
- `qrious` + `qr-scanner` — 二维码生成与解析 - `qrious` + `qr-scanner` — 二维码生成与解析
- `dayjs` — 日期处理(时间戳转换) - `dayjs` — 日期处理(时间戳转换)
- `sonner` — Toast 通知(替代传统 snackbar - `sonner` — Toast 通知(替代传统 snackbar
- Web Worker — 批量数据生成(`src/workers/generator.worker.ts`),避免阻塞 UI 线程
+3 -4
View File
@@ -81,7 +81,7 @@
- **UI 组件**: shadcn/ui (基于 Radix UI 的无头组件库) - **UI 组件**: shadcn/ui (基于 Radix UI 的无头组件库)
- **样式**: Tailwind CSS + class-variance-authority + cn() 工具函数 - **样式**: Tailwind CSS + class-variance-authority + cn() 工具函数
- **日期处理**: dayjs (集成 UTC 和 Timezone 插件) - **日期处理**: dayjs (集成 UTC 和 Timezone 插件)
- **国际化**: i18next + react-i18next - **国际化**: Chrome `chrome.i18n` + `public/_locales/zh_CN/messages.json`
- **通信**: @webext-core/messaging - **通信**: @webext-core/messaging
- **存储**: Chrome Storage API (类型安全封装) - **存储**: Chrome Storage API (类型安全封装)
- **解析引擎**: qr-scanner (二维码解析), qrious (二维码生成) - **解析引擎**: qr-scanner (二维码解析), qrious (二维码生成)
@@ -107,8 +107,8 @@
│ ├── hooks/ # 自定义 React Hooks │ ├── hooks/ # 自定义 React Hooks
│ ├── utils/ # 工具函数与服务抽象 │ ├── utils/ # 工具函数与服务抽象
│ ├── types/ # TypeScript 类型声明 │ ├── types/ # TypeScript 类型声明
│ └── lib/ # 通用工具函数 (cn, utils 等) │ └── lib/ # 通用工具函数与生成器库 (cn, utils, generators 等)
├── public/ # 静态资源 (图标、manifest 资源等) ├── public/ # 静态资源 (图标、_locales 本地化资源等)
├── wxt.config.ts # WXT 框架核心配置 ├── wxt.config.ts # WXT 框架核心配置
└── package.json # 项目元数据与依赖管理 └── package.json # 项目元数据与依赖管理
``` ```
@@ -155,7 +155,6 @@
- `sidePanel`: 支持在浏览器侧边栏中运行. - `sidePanel`: 支持在浏览器侧边栏中运行.
- `clipboardWrite`: 提供一键复制功能. - `clipboardWrite`: 提供一键复制功能.
- `contextMenus`: 注册右键菜单,支持快捷操作. - `contextMenus`: 注册右键菜单,支持快捷操作.
- `alarms`: 管理后台定时任务(如清理后自动刷新).
## 浏览器支持 ## 浏览器支持
+11 -1
View File
@@ -2,7 +2,7 @@
> 版本: v1.0 > 版本: v1.0
> 创建时间: 2024-01-20 > 创建时间: 2024-01-20
> 状态: 待 Review > 状态: 已实现(见 `src/pages/TestDataGenerator/`、`src/lib/generators/`、`src/workers/generator.worker.ts`
## 产品定位 ## 产品定位
@@ -19,3 +19,13 @@
- [规则管理](./rule-management.md) - [规则管理](./rule-management.md)
- [界面设计](./ui-design.md) - [界面设计](./ui-design.md)
- [技术实现](./technical-implementation.md) - [技术实现](./technical-implementation.md)
## 当前实现入口
- 页面入口:`src/pages/TestDataGenerator/index.tsx`
- Worker`src/workers/generator.worker.ts`
- 生成器库:`src/lib/generators/`
- 规则存储:`src/utils/ruleStorage.ts`
- 导出工具:`src/utils/dataExporter.ts`
实现约束与任务完成状态记录在 [TASKS.md](./TASKS.md)。
+16
View File
@@ -12,8 +12,24 @@
| `icon/48.png` | 48×48 图标(扩展管理页) | | `icon/48.png` | 48×48 图标(扩展管理页) |
| `icon/96.png` | 96×96 图标 | | `icon/96.png` | 96×96 图标 |
| `icon/128.png` | 128×128 图标(Chrome Web Store | | `icon/128.png` | 128×128 图标(Chrome Web Store |
| `_locales/` | Chrome 扩展本地化资源 |
## 本地化资源
项目使用 Chrome 扩展标准 `chrome.i18n`,默认语言在 `wxt.config.ts` 中配置为 `zh_CN`
| 文件/目录 | 用途 |
| ------------------------------ | -------------------------------------------------- |
| `_locales/zh_CN/messages.json` | 默认中文语言包,包含功能名、描述、按钮、提示等翻译 |
添加或修改文案时:
- 使用 Chrome 扩展消息格式:`"key": { "message": "文本" }`
- key 使用下划线分隔,例如 `testDataGenerator_title`
- 代码中通过 `useI18n``getMessage` 读取,不要新增 `i18n/locales` 目录
## 注意事项 ## 注意事项
- 修改图标后需同步更新 `wxt.config.ts` 中的 manifest 配置 - 修改图标后需同步更新 `wxt.config.ts` 中的 manifest 配置
- 图标格式推荐使用 PNG,确保透明背景 - 图标格式推荐使用 PNG,确保透明背景
- 修改 `_locales` 后需确认 `manifest.default_locale` 与语言目录名一致
File diff suppressed because it is too large Load Diff
+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

+7 -5
View File
@@ -4,6 +4,7 @@ import { copyTextToClipboard } from '@/utils/clipboard';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { buttonVariants, type ButtonProps } from '@/components/ui/button'; import { buttonVariants, type ButtonProps } from '@/components/ui/button';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { useI18n } from '@/utils/chromeI18n';
interface CopyButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> { interface CopyButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> {
text: string; text: string;
@@ -18,6 +19,7 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
className, className,
...props ...props
}) => { }) => {
const { t } = useI18n('common');
const [copied, setCopied] = useState(false); const [copied, setCopied] = useState(false);
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null); const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -31,18 +33,18 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
e.stopPropagation(); e.stopPropagation();
if (!text) { if (!text) {
toast.error('无内容可复制'); toast.error(t('messages.copyEmpty'));
return; return;
} }
const success = await copyTextToClipboard(text); const success = await copyTextToClipboard(text);
if (success) { if (success) {
toast.success('已复制到剪贴板'); toast.success(t('messages.copySuccess'));
setCopied(true); setCopied(true);
if (timerRef.current) clearTimeout(timerRef.current); if (timerRef.current) clearTimeout(timerRef.current);
timerRef.current = setTimeout(() => setCopied(false), 1500); timerRef.current = setTimeout(() => setCopied(false), 1500);
} else { } else {
toast.error('复制失败'); toast.error(t('messages.copyError'));
} }
}; };
@@ -50,8 +52,8 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
<button <button
type="button" type="button"
onClick={handleCopy} onClick={handleCopy}
title={tooltip ?? '复制'} title={tooltip ?? t('buttons.copy')}
aria-label={tooltip ?? '复制'} aria-label={tooltip ?? t('buttons.copy')}
className={cn( className={cn(
buttonVariants({ variant, size }), buttonVariants({ variant, size }),
copied && copied &&
@@ -0,0 +1,194 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { act, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
// unmock the globally-mocked component so we test the real implementation
vi.unmock('@/components/CopyButton');
vi.mock('@/utils/clipboard', () => ({
copyTextToClipboard: vi.fn(),
}));
vi.mock('sonner', () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
},
}));
import { CopyButton } from '@/components/CopyButton';
import { copyTextToClipboard } from '@/utils/clipboard';
import { toast } from 'sonner';
const mockedCopy = vi.mocked(copyTextToClipboard);
const mockedToast = vi.mocked(toast);
describe('CopyButton', () => {
beforeEach(() => {
vi.useFakeTimers({ shouldAdvanceTime: true });
vi.clearAllMocks();
});
afterEach(() => {
vi.useRealTimers();
});
it('复制成功时调用 copyTextToClipboard 并传入正确 text', async () => {
mockedCopy.mockResolvedValue(true);
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
render(<CopyButton text="hello world" />);
await user.click(screen.getByRole('button'));
expect(mockedCopy).toHaveBeenCalledWith('hello world');
expect(mockedToast.success).toHaveBeenCalled();
});
it('复制成功后图标切换为 Check1.5 秒后恢复', async () => {
mockedCopy.mockResolvedValue(true);
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
render(<CopyButton text="test" />);
// 点击后复制成功,按钮获得 emerald 样式(说明切到了 Check 状态)
await user.click(screen.getByRole('button'));
await waitFor(() => {
expect(screen.getByRole('button').className).toContain('text-emerald');
});
// 1.5 秒后样式恢复
act(() => {
vi.advanceTimersByTime(1500);
});
await waitFor(() => {
expect(screen.getByRole('button').className).not.toContain('text-emerald');
});
});
it('复制空文本时弹出 error toast', async () => {
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
render(<CopyButton text="" />);
await user.click(screen.getByRole('button'));
expect(mockedCopy).not.toHaveBeenCalled();
expect(mockedToast.error).toHaveBeenCalled();
});
it('复制失败时弹出 error toast', async () => {
mockedCopy.mockResolvedValue(false);
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
render(<CopyButton text="something" />);
await user.click(screen.getByRole('button'));
expect(mockedCopy).toHaveBeenCalledWith('something');
expect(mockedToast.error).toHaveBeenCalled();
});
// ==================== 新增测试 ====================
it('初始渲染时显示 Copy 图标且无 emerald 样式', () => {
render(<CopyButton text="initial" />);
const button = screen.getByRole('button');
expect(button.className).not.toContain('text-emerald');
// 通过 aria-label 确认按钮存在,图标由 lucide 渲染为 svg
expect(button).toHaveAttribute('aria-label');
});
it('自定义 tooltip 会覆盖默认 title 和 aria-label', () => {
render(<CopyButton text="tooltip-test" tooltip="自定义提示" />);
const button = screen.getByRole('button');
expect(button).toHaveAttribute('title', '自定义提示');
expect(button).toHaveAttribute('aria-label', '自定义提示');
});
it('className 被正确透传到按钮', () => {
render(<CopyButton text="class-test" className="my-custom-class" />);
const button = screen.getByRole('button');
expect(button.className).toContain('my-custom-class');
});
it('点击事件阻止冒泡', async () => {
mockedCopy.mockResolvedValue(true);
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
const parentClick = vi.fn();
render(
<div onClick={parentClick}>
<CopyButton text="stop-propagation" />
</div>,
);
await user.click(screen.getByRole('button'));
expect(mockedCopy).toHaveBeenCalled();
expect(parentClick).not.toHaveBeenCalled();
});
it('组件卸载时清除定时器,不触发状态更新警告', async () => {
mockedCopy.mockResolvedValue(true);
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
const { unmount } = render(<CopyButton text="unmount-test" />);
await user.click(screen.getByRole('button'));
// 在 1.5 秒超时到期前卸载组件
act(() => {
vi.advanceTimersByTime(500);
});
// 卸载不应抛出 "Can't perform a React state update on an unmounted component" 警告
expect(() => unmount()).not.toThrow();
// 前进剩余时间,确认没有异常
act(() => {
vi.advanceTimersByTime(2000);
});
});
it('快速连续点击不会创建多个重叠定时器', async () => {
mockedCopy.mockResolvedValue(true);
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
render(<CopyButton text="rapid-click" />);
const button = screen.getByRole('button');
// 快速点击 3 次
await user.click(button);
await user.click(button);
await user.click(button);
// copyTextToClipboard 应该被调用 3 次(每次点击都执行)
expect(mockedCopy).toHaveBeenCalledTimes(3);
// 但 setTimeout 相关的 clearTimeout + setTimeout 组合应正常工作
// advance 1.5 秒后,copied 状态应恢复为 false
act(() => {
vi.advanceTimersByTime(1500);
});
await waitFor(() => {
expect(button.className).not.toContain('text-emerald');
});
});
it('其他 button props 通过 ...props 透传', () => {
render(<CopyButton text="props-test" data-testid="copy-btn" disabled id="copy-button-id" />);
const button = screen.getByRole('button');
expect(button).toHaveAttribute('data-testid', 'copy-btn');
expect(button).toBeDisabled();
expect(button).toHaveAttribute('id', 'copy-button-id');
});
});
@@ -9,9 +9,8 @@
*/ */
import { Download } from 'lucide-react'; import { Download } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input'; import { formatFileSize } from '@/utils/base64Converter';
import { Label } from '@/components/ui/label'; import { useI18n } from '@/utils/chromeI18n';
import { formatBytes } from '@/utils/format';
interface DecodeResultPaperProps { interface DecodeResultPaperProps {
/** 标题文案,由调用方传入 i18n key 对应的值(如 decodedFileOutput / decodedImageOutput */ /** 标题文案,由调用方传入 i18n key 对应的值(如 decodedFileOutput / decodedImageOutput */
@@ -39,6 +38,8 @@ export default function DecodeResultPaper({
onDownload, onDownload,
children, children,
}: DecodeResultPaperProps) { }: DecodeResultPaperProps) {
const { t } = useI18n('base64Converter');
return ( return (
<div className="p-4 rounded-xl bg-primary/10 border border-primary/30"> <div className="p-4 rounded-xl bg-primary/10 border border-primary/30">
{/* 标题 */} {/* 标题 */}
@@ -50,19 +51,24 @@ export default function DecodeResultPaper({
{/* 文件信息 */} {/* 文件信息 */}
<div className="flex gap-4 mb-3"> <div className="flex gap-4 mb-3">
<span className="text-xs text-muted-foreground"> <span className="text-xs text-muted-foreground">
{'推断的 MIME 类型'}: {mimeType} {t('inferredMimeType')}: {mimeType}
</span> </span>
<span className="text-xs text-muted-foreground"> <span className="text-xs text-muted-foreground">
{'解码大小'}: {formatBytes(blobSize)} {t('decodedSize')}: {formatFileSize(blobSize)}
</span> </span>
</div> </div>
{/* 文件名输入 */} {/* 文件名输入 */}
<div className="mb-3"> <div className="mb-3">
<Label className="block text-xs font-medium text-muted-foreground mb-1"> <label className="block text-xs font-medium text-muted-foreground mb-1">
{'解码后文件名'} {t('decodedFileName')}
</Label> </label>
<Input value={fileName} onChange={(e) => onFileNameChange(e.target.value)} /> <input
type="text"
value={fileName}
onChange={(e) => onFileNameChange(e.target.value)}
className="w-full px-3 py-2 text-sm border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary"
/>
</div> </div>
{/* 下载按钮 */} {/* 下载按钮 */}
@@ -73,7 +79,7 @@ export default function DecodeResultPaper({
className="w-full rounded-lg font-bold" className="w-full rounded-lg font-bold"
> >
<Download className="mr-2 h-4 w-4" /> <Download className="mr-2 h-4 w-4" />
{'下载'} {t('download')}
</Button> </Button>
</div> </div>
); );
+6 -3
View File
@@ -1,6 +1,7 @@
import { Component, ErrorInfo, ReactNode } from 'react'; import { Component, ErrorInfo, ReactNode } from 'react';
import { AlertCircle, RefreshCw } from 'lucide-react'; import { AlertCircle, RefreshCw } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { getMessage } from '@/utils/chromeI18n';
interface Props { interface Props {
children: ReactNode; children: ReactNode;
@@ -43,9 +44,11 @@ class ErrorBoundary extends Component<Props, State> {
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-destructive/10 text-destructive mx-auto mb-4"> <div className="flex h-16 w-16 items-center justify-center rounded-full bg-destructive/10 text-destructive mx-auto mb-4">
<AlertCircle className="h-8 w-8" /> <AlertCircle className="h-8 w-8" />
</div> </div>
<h2 className="text-xl font-extrabold text-destructive mb-2"></h2> <h2 className="text-xl font-extrabold text-destructive mb-2">
{getMessage('errorBoundary_title')}
</h2>
<p className="text-sm text-muted-foreground mb-6"> <p className="text-sm text-muted-foreground mb-6">
{getMessage('errorBoundary_description')}
</p> </p>
{this.state.error && ( {this.state.error && (
<div className="mb-6 p-4 rounded-lg bg-zinc-950 dark:bg-zinc-900 text-left max-h-[200px] overflow-auto border border-border/40"> <div className="mb-6 p-4 rounded-lg bg-zinc-950 dark:bg-zinc-900 text-left max-h-[200px] overflow-auto border border-border/40">
@@ -60,7 +63,7 @@ class ErrorBoundary extends Component<Props, State> {
className="rounded-lg font-bold shadow-sm" className="rounded-lg font-bold shadow-sm"
> >
<RefreshCw className="mr-2 h-4 w-4" /> <RefreshCw className="mr-2 h-4 w-4" />
{getMessage('errorBoundary_refresh')}
</Button> </Button>
</div> </div>
</div> </div>
@@ -1,8 +1,7 @@
import { useCallback, useEffect, useRef } from 'react'; import { useCallback, useEffect, useRef } from 'react';
import { Image, X } from 'lucide-react'; import { Image, X } from 'lucide-react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { Button } from '@/components/ui/button'; import { useI18n } from '@/utils/chromeI18n';
import { Label } from '@/components/ui/label';
interface ImageUploaderProps { interface ImageUploaderProps {
/** 选中的文件 */ /** 选中的文件 */
@@ -30,6 +29,7 @@ const ImageUploader = ({
dragging, dragging,
onDraggingChange, onDraggingChange,
}: ImageUploaderProps) => { }: ImageUploaderProps) => {
const { t } = useI18n('qrCode');
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
const handleFileChange = useCallback( const handleFileChange = useCallback(
@@ -45,8 +45,8 @@ const ImageUploader = ({
URL.revokeObjectURL(previewUrl); URL.revokeObjectURL(previewUrl);
} }
onClearFile(); onClearFile();
toast.success('图片已清除'); toast.success(t('qrCode:imageCleared'));
}, [previewUrl, onClearFile]); }, [previewUrl, onClearFile, t]);
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files.length > 0) { if (e.target.files && e.target.files.length > 0) {
@@ -86,10 +86,10 @@ const ImageUploader = ({
if (file) { if (file) {
try { try {
handleFileChange(file); handleFileChange(file);
toast.success('图片粘贴成功,正在解析...'); toast.success(t('qrCode:imagePasted'));
} catch (error) { } catch (error) {
console.error('处理粘贴图片失败:', error); console.error('处理粘贴图片失败:', error);
toast.error('粘贴图片失败,请重试'); toast.error(t('qrCode:imagePasteError'));
} }
} }
break; break;
@@ -102,7 +102,7 @@ const ImageUploader = ({
return () => { return () => {
document.removeEventListener('paste', handlePaste); document.removeEventListener('paste', handlePaste);
}; };
}, [handleFileChange]); }, [handleFileChange, t]);
return ( return (
<div <div
@@ -111,7 +111,7 @@ const ImageUploader = ({
? 'border-green-600 bg-green-50' ? 'border-green-600 bg-green-50'
: selectedFile : selectedFile
? 'border-green-600 bg-green-50/50' ? 'border-green-600 bg-green-50/50'
: 'border-input bg-muted hover:border-green-600 hover:bg-green-500/10' : 'border-input bg-muted hover:border-green-600 hover:bg-green-500/10/50'
}`} }`}
onDragOver={handleDragOver} onDragOver={handleDragOver}
onDragLeave={handleDragLeave} onDragLeave={handleDragLeave}
@@ -125,7 +125,7 @@ const ImageUploader = ({
className="hidden" className="hidden"
id="qr-code-upload" id="qr-code-upload"
/> />
<Label htmlFor="qr-code-upload" className="cursor-pointer text-center w-full"> <label htmlFor="qr-code-upload" className="cursor-pointer text-center w-full">
{selectedFile ? ( {selectedFile ? (
<div className="text-center w-full relative"> <div className="text-center w-full relative">
<div className="relative inline-block"> <div className="relative inline-block">
@@ -134,22 +134,20 @@ const ImageUploader = ({
alt="QR Code Preview" alt="QR Code Preview"
className="max-w-full max-h-40 rounded-lg object-contain" className="max-w-full max-h-40 rounded-lg object-contain"
/> />
<Button <button
type="button" type="button"
variant="destructive"
size="icon"
data-testid="ClearIcon" data-testid="ClearIcon"
onClick={(e) => { onClick={(e) => {
e.stopPropagation(); e.stopPropagation();
handleClearFile(); handleClearFile();
}} }}
className="absolute -top-2 -right-2 h-6 w-6 rounded-full" className="absolute -top-2 -right-2 w-6 h-6 bg-red-500 text-white rounded-full flex items-center justify-center hover:bg-red-600 transition-colors"
> >
<X className="w-3 h-3" /> <X className="w-3 h-3" />
</Button> </button>
</div> </div>
<span className="block text-sm text-muted-foreground mt-2">{selectedFile.name}</span> <span className="block text-sm text-muted-foreground mt-2">{selectedFile.name}</span>
<span className="block text-xs text-muted-foreground">{'点击更换图片'}</span> <span className="block text-xs text-muted-foreground">{t('qrCode:clickToChange')}</span>
</div> </div>
) : ( ) : (
<> <>
@@ -158,14 +156,14 @@ const ImageUploader = ({
className="w-12 h-12 text-muted-foreground mx-auto mb-2" className="w-12 h-12 text-muted-foreground mx-auto mb-2"
/> />
<span className="block text-sm text-muted-foreground mb-1"> <span className="block text-sm text-muted-foreground mb-1">
{'点击、拖拽或粘贴上传二维码图片'} {t('qrCode:clickToUpload')}
</span> </span>
<span className="block text-xs text-muted-foreground"> <span className="block text-xs text-muted-foreground">
{'支持 PNG、JPG、WEBP、Base64 格式'} {t('qrCode:supportFormats')}
</span> </span>
</> </>
)} )}
</Label> </label>
</div> </div>
); );
}; };
+6 -3
View File
@@ -1,6 +1,7 @@
import { Component, ErrorInfo, ReactNode } from 'react'; import { Component, ErrorInfo, ReactNode } from 'react';
import { AlertCircle, RefreshCw } from 'lucide-react'; import { AlertCircle, RefreshCw } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { getMessage } from '@/utils/chromeI18n';
interface Props { interface Props {
children: ReactNode; children: ReactNode;
@@ -45,9 +46,11 @@ class PageErrorBoundary extends Component<Props, State> {
<AlertCircle className="h-6 w-6" /> <AlertCircle className="h-6 w-6" />
</div> </div>
<h3 className="text-base font-semibold text-foreground mb-1.5"></h3> <h3 className="text-base font-semibold text-foreground mb-1.5">
{getMessage('pageErrorBoundary_title')}
</h3>
<p className="text-xs text-muted-foreground mb-5"> <p className="text-xs text-muted-foreground mb-5">
{getMessage('pageErrorBoundary_description')}
</p> </p>
{this.state.error && ( {this.state.error && (
@@ -65,7 +68,7 @@ class PageErrorBoundary extends Component<Props, State> {
className="font-medium shadow-sm" className="font-medium shadow-sm"
> >
<RefreshCw className="mr-1.5 h-3.5 w-3.5" /> <RefreshCw className="mr-1.5 h-3.5 w-3.5" />
{getMessage('errorBoundary_retry')}
</Button> </Button>
</div> </div>
</div> </div>
@@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { Copy, Download } from 'lucide-react'; import { Copy, Download } from 'lucide-react';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
@@ -23,6 +24,8 @@ const QrCodePreview = ({
className, className,
...props ...props
}: QrCodePreviewProps) => { }: QrCodePreviewProps) => {
const { t } = useI18n('qrCode');
// 空状态下的虚线骨架屏 // 空状态下的虚线骨架屏
if (!qrCodeDataUrl) { if (!qrCodeDataUrl) {
return ( return (
@@ -34,7 +37,7 @@ const QrCodePreview = ({
{...props} {...props}
> >
<p className="text-sm text-muted-foreground text-center"> <p className="text-sm text-muted-foreground text-center">
{placeholderText || '二维码将显示在这里'} {placeholderText || t('qrCode:qrCodeWillShow')}
</p> </p>
</div> </div>
); );
@@ -65,12 +68,12 @@ const QrCodePreview = ({
<div className="flex w-full gap-2 mt-3"> <div className="flex w-full gap-2 mt-3">
<Button variant="outline" size="sm" onClick={onDownload} className="flex-1 h-8"> <Button variant="outline" size="sm" onClick={onDownload} className="flex-1 h-8">
<Download className="w-3.5 h-3.5 text-muted-foreground" /> <Download className="w-3.5 h-3.5 text-muted-foreground" />
<span className="truncate text-xs">{'下载二维码'}</span> <span className="truncate text-xs">{t('qrCode:downloadButton')}</span>
</Button> </Button>
<Button variant="default" size="sm" onClick={onCopy} className="flex-1 h-8"> <Button variant="default" size="sm" onClick={onCopy} className="flex-1 h-8">
<Copy className="w-3.5 h-3.5" /> <Copy className="w-3.5 h-3.5" />
<span className="truncate text-xs">{'复制二维码'}</span> <span className="truncate text-xs">{t('qrCode:copyQrButton')}</span>
</Button> </Button>
</div> </div>
</div> </div>
+4 -2
View File
@@ -1,6 +1,7 @@
import { FEATURES, getEntryPointType } from '@/config/features'; import { FEATURES, getEntryPointType } from '@/config/features';
import { useRouter } from '@/providers/RouterProvider'; import { useRouter } from '@/providers/RouterProvider';
import { Suspense } from 'react'; import { Suspense } from 'react';
import { useI18n } from '@/utils/chromeI18n';
import PageErrorBoundary from '@/components/PageErrorBoundary'; import PageErrorBoundary from '@/components/PageErrorBoundary';
import PageSkeleton from '@/components/PageSkeleton'; import PageSkeleton from '@/components/PageSkeleton';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
@@ -10,6 +11,7 @@ const entryPointType = getEntryPointType();
export default function RouterContainer() { export default function RouterContainer() {
const { currentPage, isLoaded } = useRouter(); const { currentPage, isLoaded } = useRouter();
const { t } = useI18n('common');
const animationClass = const animationClass =
currentPage === 'dashboard' ? 'page-transition-dashboard' : 'page-transition-enter'; currentPage === 'dashboard' ? 'page-transition-dashboard' : 'page-transition-enter';
@@ -41,9 +43,9 @@ export default function RouterContainer() {
<div className="flex h-12 w-12 items-center justify-center rounded-xl bg-destructive/10 text-destructive mb-4"> <div className="flex h-12 w-12 items-center justify-center rounded-xl bg-destructive/10 text-destructive mb-4">
<AlertTriangle className="h-6 w-6" /> <AlertTriangle className="h-6 w-6" />
</div> </div>
<h3 className="text-sm font-semibold text-foreground">{'页面未找到'}</h3> <h3 className="text-sm font-semibold text-foreground">{t('router.notFound')}</h3>
<p className="text-xs text-muted-foreground mt-1 max-w-[240px]"> <p className="text-xs text-muted-foreground mt-1 max-w-[240px]">
{`该功能在当前运行环境(${entryPointType})下不可用或已被移除。`} {t('router.notFoundDescription', { entryPointType })}
</p> </p>
</div> </div>
)} )}
+16 -14
View File
@@ -17,16 +17,6 @@ export interface SwitchButtonGroupProps<T extends string | number = string> exte
buttonClassName?: string; buttonClassName?: string;
} }
const SIZE_CLASSES = {
small: 'text-xs h-8 px-2 py-1 rounded-md',
medium: 'text-sm h-9 px-3 py-1.5 rounded-md',
large: 'text-base h-11 px-4 py-2 rounded-lg',
} as const;
const SELECTED_CLASSES =
'bg-background text-foreground shadow-sm font-semibold animate-in fade-in-50 zoom-in-95 duration-150';
const UNSELECTED_CLASSES = 'hover:bg-background/50 hover:text-foreground/80';
export default function SwitchButtonGroup<T extends string | number = string>({ export default function SwitchButtonGroup<T extends string | number = string>({
value, value,
options, options,
@@ -36,6 +26,12 @@ export default function SwitchButtonGroup<T extends string | number = string>({
buttonClassName, buttonClassName,
...props ...props
}: SwitchButtonGroupProps<T>) { }: SwitchButtonGroupProps<T>) {
const sizeClasses = {
small: 'text-xs h-8 px-2 py-1 rounded-md',
medium: 'text-sm h-9 px-3 py-1.5 rounded-md',
large: 'text-base h-11 px-4 py-2 rounded-lg',
};
return ( return (
<div <div
className={cn( className={cn(
@@ -44,7 +40,10 @@ export default function SwitchButtonGroup<T extends string | number = string>({
)} )}
{...props} {...props}
> >
{options.map((option) => ( {options.map((option) => {
const isSelected = value === option.value;
return (
<button <button
key={option.value} key={option.value}
type="button" type="button"
@@ -53,14 +52,17 @@ export default function SwitchButtonGroup<T extends string | number = string>({
'flex-1 inline-flex items-center justify-center font-medium whitespace-nowrap transition-all', 'flex-1 inline-flex items-center justify-center font-medium whitespace-nowrap transition-all',
'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2', 'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2',
'disabled:pointer-events-none disabled:opacity-50', 'disabled:pointer-events-none disabled:opacity-50',
SIZE_CLASSES[size], sizeClasses[size],
value === option.value ? SELECTED_CLASSES : UNSELECTED_CLASSES, isSelected
? 'bg-background text-foreground shadow-sm font-semibold animate-in fade-in-50 zoom-in-95 duration-150'
: 'hover:bg-background/50 hover:text-foreground/80',
buttonClassName, buttonClassName,
)} )}
> >
{option.label} {option.label}
</button> </button>
))} );
})}
</div> </div>
); );
} }
+13 -6
View File
@@ -1,5 +1,6 @@
import React, { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react'; import React, { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react';
import { X } from 'lucide-react'; import { X } from 'lucide-react';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { toast } from 'sonner'; // 推荐使用 shadcn 的默认 Toast import { toast } from 'sonner'; // 推荐使用 shadcn 的默认 Toast
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
@@ -113,7 +114,8 @@ const TextInputArea = forwardRef<HTMLTextAreaElement, TextInputAreaProps>((props
const [internalValue, setInternalValue] = useState(defaultValue); const [internalValue, setInternalValue] = useState(defaultValue);
const [error, setError] = useState<string>(''); const [error, setError] = useState<string>('');
const placeholder = placeholderProp ?? '请输入文本'; const { t } = useI18n('common');
const placeholder = placeholderProp ?? t('textInputArea.placeholder');
const isControlled = controlledValue !== undefined; const isControlled = controlledValue !== undefined;
const value = isControlled ? controlledValue : internalValue; const value = isControlled ? controlledValue : internalValue;
@@ -157,7 +159,7 @@ const TextInputArea = forwardRef<HTMLTextAreaElement, TextInputAreaProps>((props
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const newVal = e.target.value; const newVal = e.target.value;
if (maxLength && newVal.length > maxLength) { if (maxLength && newVal.length > maxLength) {
const msg = `内容不能超过 ${maxLength} 个字符`; const msg = t('charCount', { count: maxLength });
setError(msg); setError(msg);
toast.warning(msg); toast.warning(msg);
return; return;
@@ -179,9 +181,9 @@ const TextInputArea = forwardRef<HTMLTextAreaElement, TextInputAreaProps>((props
onChange?.(''); onChange?.('');
setError(''); setError('');
internalRef.current?.focus(); internalRef.current?.focus();
toast.success('已清空'); toast.success(t('textInputArea.cleared'));
onClear?.(); onClear?.();
}, [isControlled, onChange, onClear]); }, [isControlled, onChange, onClear, t]);
const handleAction = useCallback( const handleAction = useCallback(
(action: ToolbarAction) => { (action: ToolbarAction) => {
@@ -270,13 +272,18 @@ const TextInputArea = forwardRef<HTMLTextAreaElement, TextInputAreaProps>((props
{/* 右侧系统按钮组 */} {/* 右侧系统按钮组 */}
<div className="flex items-center gap-1.5 ml-auto shrink-0"> <div className="flex items-center gap-1.5 ml-auto shrink-0">
{allowCopy && value && ( {allowCopy && value && (
<CopyButton text={value} tooltip={'复制内容'} size="sm" className="h-7 w-7 p-1" /> <CopyButton
text={value}
tooltip={t('textInputArea.copyContent')}
size="sm"
className="h-7 w-7 p-1"
/>
)} )}
{showClear && value && !disabled && !readOnly && ( {showClear && value && !disabled && !readOnly && (
<button <button
type="button" type="button"
onClick={handleClear} onClick={handleClear}
aria-label={'清空'} aria-label={t('textInputArea.clear')}
className="p-1 h-7 w-7 flex items-center justify-center rounded-md text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" className="p-1 h-7 w-7 flex items-center justify-center rounded-md text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
> >
<X className="h-4 w-4" /> <X className="h-4 w-4" />
+21 -23
View File
@@ -5,6 +5,7 @@ import { useThemeMode } from '@/providers/ThemeModeProvider';
import { FeatureConfig, FEATURES } from '@/config/features'; import { FeatureConfig, FEATURES } from '@/config/features';
import { storageUtil } from '@/utils/chromeStorage'; import { storageUtil } from '@/utils/chromeStorage';
import { openExtensionPage } from '@/utils/chromeTabs'; import { openExtensionPage } from '@/utils/chromeTabs';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
const SEARCH_HISTORY_LIMIT = 10; const SEARCH_HISTORY_LIMIT = 10;
@@ -13,6 +14,7 @@ const SEARCH_HISTORY_DISPLAY = 5;
export default function TopBar() { export default function TopBar() {
const { currentPage, goBack, navigateTo } = useRouter(); const { currentPage, goBack, navigateTo } = useRouter();
const { mode, setMode } = useThemeMode(); const { mode, setMode } = useThemeMode();
const { t } = useI18n(['common', 'features']);
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const [showResults, setShowResults] = useState(false); const [showResults, setShowResults] = useState(false);
@@ -64,9 +66,12 @@ export default function TopBar() {
if (!query) return []; if (!query) return [];
return FEATURES.filter((f) => { return FEATURES.filter((f) => {
if (f.key === 'dashboard') return false; if (f.key === 'dashboard') return false;
return f.label.toLowerCase().includes(query) || f.description.toLowerCase().includes(query); return (
t(f.labelKey).toLowerCase().includes(query) ||
t(f.descriptionKey).toLowerCase().includes(query)
);
}); });
}, [searchQuery]); }, [searchQuery, t]);
const displayedHistory = useMemo(() => { const displayedHistory = useMemo(() => {
if (searchQuery.trim()) return []; if (searchQuery.trim()) return [];
@@ -139,7 +144,7 @@ export default function TopBar() {
<button <button
type="button" type="button"
onClick={goBack} onClick={goBack}
aria-label={'返回'} aria-label={t('common_buttons_back')}
className="flex h-8 w-8 items-center justify-center rounded-md border border-input bg-background text-muted-foreground shadow-sm transition-colors hover:bg-accent hover:text-accent-foreground" className="flex h-8 w-8 items-center justify-center rounded-md border border-input bg-background text-muted-foreground shadow-sm transition-colors hover:bg-accent hover:text-accent-foreground"
> >
<ArrowLeft className="h-4 w-4" /> <ArrowLeft className="h-4 w-4" />
@@ -154,7 +159,7 @@ export default function TopBar() {
<input <input
ref={inputRef} ref={inputRef}
type="text" type="text"
placeholder={'搜索工具...'} placeholder={t('common_buttons_search')}
value={searchQuery} value={searchQuery}
onChange={(e) => { onChange={(e) => {
setSearchQuery(e.target.value); setSearchQuery(e.target.value);
@@ -163,7 +168,7 @@ export default function TopBar() {
}} }}
onFocus={() => setShowResults(true)} onFocus={() => setShowResults(true)}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
aria-label={'搜索工具...'} aria-label={t('common_buttons_search')}
className="w-full h-9 pl-9 pr-16 text-sm rounded-lg border border-border/60 bg-muted/40 transition-all placeholder:text-muted-foreground/50 focus:bg-background focus:outline-none focus:ring-1 focus:ring-ring focus:border-input" className="w-full h-9 pl-9 pr-16 text-sm rounded-lg border border-border/60 bg-muted/40 transition-all placeholder:text-muted-foreground/50 focus:bg-background focus:outline-none focus:ring-1 focus:ring-ring focus:border-input"
/> />
{!searchQuery && ( {!searchQuery && (
@@ -178,7 +183,7 @@ export default function TopBar() {
setSearchQuery(''); setSearchQuery('');
setSelectedIndex(-1); setSelectedIndex(-1);
}} }}
aria-label={'清除搜索'} aria-label={t('common:buttons.clearSearch')}
className="absolute right-2 top-1/2 -translate-y-1/2 p-1 rounded-md text-muted-foreground hover:text-foreground transition-colors" className="absolute right-2 top-1/2 -translate-y-1/2 p-1 rounded-md text-muted-foreground hover:text-foreground transition-colors"
> >
<X className="h-3 w-3" /> <X className="h-3 w-3" />
@@ -214,22 +219,24 @@ export default function TopBar() {
{feature.icon && <feature.icon className="h-4 w-4" />} {feature.icon && <feature.icon className="h-4 w-4" />}
</div> </div>
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<p className="font-medium text-foreground truncate">{feature.label}</p> <p className="font-medium text-foreground truncate">
{t(feature.labelKey)}
</p>
<p className="text-xs text-muted-foreground truncate mt-0.5"> <p className="text-xs text-muted-foreground truncate mt-0.5">
{feature.description} {t(feature.descriptionKey)}
</p> </p>
</div> </div>
</li> </li>
)) ))
) : ( ) : (
<li className="px-4 py-6 text-center text-sm text-muted-foreground"> <li className="px-4 py-6 text-center text-sm text-muted-foreground">
{'未找到相关工具'} {t('common:buttons.noResults')}
</li> </li>
) )
) : ( ) : (
<> <>
<div className="px-3 py-1.5 text-[11px] font-semibold tracking-wider text-muted-foreground/60 uppercase"> <div className="px-3 py-1.5 text-[11px] font-semibold tracking-wider text-muted-foreground/60 uppercase">
{'最近搜索'} {t('common:buttons.recentSearch')}
</div> </div>
{displayedHistory.map((item, index) => ( {displayedHistory.map((item, index) => (
<li <li
@@ -254,10 +261,10 @@ export default function TopBar() {
</div> </div>
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<p className="font-medium text-foreground truncate"> <p className="font-medium text-foreground truncate">
{item.feature?.label} {item.feature && t(item.feature.labelKey)}
</p> </p>
<p className="text-xs text-muted-foreground truncate mt-0.5"> <p className="text-xs text-muted-foreground truncate mt-0.5">
{item.feature?.description} {item.feature && t(item.feature.descriptionKey)}
</p> </p>
</div> </div>
</li> </li>
@@ -271,19 +278,10 @@ export default function TopBar() {
{/* 右侧:操作区 */} {/* 右侧:操作区 */}
<div className="flex items-center gap-1 shrink-0"> <div className="flex items-center gap-1 shrink-0">
<IconButton <IconButton onClick={cycleThemeMode} title={t(`common:buttons.themeMode.${mode}`)}>
onClick={cycleThemeMode}
title={
mode === 'light'
? '切换到深色模式'
: mode === 'dark'
? '切换到系统模式'
: '切换到浅色模式'
}
>
<ThemeIcon className="h-4 w-4" /> <ThemeIcon className="h-4 w-4" />
</IconButton> </IconButton>
<IconButton onClick={handleOpenInTab} title={'在标签页打开'}> <IconButton onClick={handleOpenInTab} title={t('common:buttons.openInTab')}>
<ExternalLink className="h-4 w-4" /> <ExternalLink className="h-4 w-4" />
</IconButton> </IconButton>
</div> </div>
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react'; import { fireEvent, render, screen } from '@testing-library/react';
import DecodeResultPaper from '../components/DecodeResultPaper'; import DecodeResultPaper from '@/components/DecodeResultPaper';
describe('DecodeResultPaper 组件', () => { describe('DecodeResultPaper 组件', () => {
const defaultProps = { const defaultProps = {
@@ -27,7 +27,7 @@ describe('DecodeResultPaper 组件', () => {
expect(screen.getByText(/image\/png/)).toBeInTheDocument(); expect(screen.getByText(/image\/png/)).toBeInTheDocument();
}); });
it('应通过 formatBytes 渲染文件大小', () => { it('应通过 formatFileSize 渲染文件大小', () => {
render(<DecodeResultPaper {...{ ...defaultProps, blobSize: 1536 }} />); render(<DecodeResultPaper {...{ ...defaultProps, blobSize: 1536 }} />);
expect(screen.getByText(/1\.5 KB/)).toBeInTheDocument(); expect(screen.getByText(/1\.5 KB/)).toBeInTheDocument();
}); });
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act, fireEvent, render, screen } from '@testing-library/react'; import { act, fireEvent, render, screen } from '@testing-library/react';
import ImageUploader from '../components/ImageUploader'; import ImageUploader from '@/components/ImageUploader';
// 配置多端一致性常驻桩(WXT 规范) // 配置多端一致性常驻桩(WXT 规范)
const storageOnChangedMock = { addListener: vi.fn(), removeListener: vi.fn() }; const storageOnChangedMock = { addListener: vi.fn(), removeListener: vi.fn() };
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react'; import { fireEvent, render, screen } from '@testing-library/react';
import QrCodePreview from '../components/QrCodePreview'; import QrCodePreview from '@/components/QrCodePreview';
describe('QrCodePreview 组件', () => { describe('QrCodePreview 组件', () => {
const mockOnDownload = vi.fn(); const mockOnDownload = vi.fn();
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react'; import { fireEvent, render, screen } from '@testing-library/react';
import { StorageCleanerConfirm } from '@/pages/StorageCleaner/components/StorageCleanerConfirm'; import { StorageCleanerConfirm } from '@/pages/StorageCleaner/StorageCleanerConfirm';
import type { StorageCleanerOptions } from '@/types/storage'; import type { StorageCleanerOptions } from '@/types/storage';
import React from 'react'; import React from 'react';
+13 -12
View File
@@ -18,12 +18,12 @@ describe('TextInputArea 组件', () => {
it('默认显示清空按钮', () => { it('默认显示清空按钮', () => {
render(<TextInputArea value="有内容" onChange={() => {}} />); render(<TextInputArea value="有内容" onChange={() => {}} />);
expect(screen.getByRole('button', { name: '清空' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'textInputArea.clear' })).toBeInTheDocument();
}); });
it('无内容时清空按钮应隐藏', () => { it('无内容时清空按钮应隐藏', () => {
render(<TextInputArea value="" onChange={() => {}} />); render(<TextInputArea value="" onChange={() => {}} />);
expect(screen.queryByRole('button', { name: '清空' })).not.toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'textInputArea.clear' })).not.toBeInTheDocument();
}); });
it('disabled 时清空按钮应隐藏', () => { it('disabled 时清空按钮应隐藏', () => {
@@ -57,7 +57,7 @@ describe('TextInputArea 组件', () => {
const handleChange = vi.fn(); const handleChange = vi.fn();
render(<TextInputArea value="内容" onChange={handleChange} />); render(<TextInputArea value="内容" onChange={handleChange} />);
fireEvent.click(screen.getByRole('button', { name: '清空' })); fireEvent.click(screen.getByRole('button', { name: 'textInputArea.clear' }));
expect(handleChange).toHaveBeenCalledWith(''); expect(handleChange).toHaveBeenCalledWith('');
}); });
@@ -81,7 +81,7 @@ describe('TextInputArea 组件', () => {
it('清空按钮应清空内容', () => { it('清空按钮应清空内容', () => {
render(<TextInputArea defaultValue="内容" />); render(<TextInputArea defaultValue="内容" />);
fireEvent.click(screen.getByRole('button', { name: '清空' })); fireEvent.click(screen.getByRole('button', { name: 'textInputArea.clear' }));
expect(screen.getByRole('textbox')).toHaveValue(''); expect(screen.getByRole('textbox')).toHaveValue('');
}); });
@@ -90,12 +90,14 @@ describe('TextInputArea 组件', () => {
describe('allowCopy 复制功能', () => { describe('allowCopy 复制功能', () => {
it('allowCopy 且有内容时显示复制按钮', () => { it('allowCopy 且有内容时显示复制按钮', () => {
render(<TextInputArea value="可复制的内容" onChange={() => {}} allowCopy />); render(<TextInputArea value="可复制的内容" onChange={() => {}} allowCopy />);
expect(screen.getByRole('button', { name: '复制内容' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'textInputArea.copyContent' })).toBeInTheDocument();
}); });
it('allowCopy 但无内容时隐藏复制按钮', () => { it('allowCopy 但无内容时隐藏复制按钮', () => {
render(<TextInputArea value="" onChange={() => {}} allowCopy />); render(<TextInputArea value="" onChange={() => {}} allowCopy />);
expect(screen.queryByRole('button', { name: '复制内容' })).not.toBeInTheDocument(); expect(
screen.queryByRole('button', { name: 'textInputArea.copyContent' }),
).not.toBeInTheDocument();
}); });
it('allowCopy=false 时不显示复制按钮', () => { it('allowCopy=false 时不显示复制按钮', () => {
@@ -111,7 +113,7 @@ describe('TextInputArea 组件', () => {
render(<TextInputArea value="测试" onChange={() => {}} allowCopy />); render(<TextInputArea value="测试" onChange={() => {}} allowCopy />);
await user.click(screen.getByRole('button', { name: '复制内容' })); await user.click(screen.getByRole('button', { name: 'textInputArea.copyContent' }));
expect(writeTextSpy).toHaveBeenCalledWith('测试'); expect(writeTextSpy).toHaveBeenCalledWith('测试');
}); });
@@ -124,7 +126,7 @@ describe('TextInputArea 组件', () => {
render(<TextInputArea value="测试" onChange={() => {}} allowCopy />); render(<TextInputArea value="测试" onChange={() => {}} allowCopy />);
await user.click(screen.getByRole('button', { name: '复制内容' })); await user.click(screen.getByRole('button', { name: 'textInputArea.copyContent' }));
expect(writeTextSpy).toHaveBeenCalledWith('测试'); expect(writeTextSpy).toHaveBeenCalledWith('测试');
}); });
@@ -156,7 +158,6 @@ describe('TextInputArea 组件', () => {
fireEvent.change(textarea, { target: { value: '123456' } }); fireEvent.change(textarea, { target: { value: '123456' } });
expect(handleChange).not.toHaveBeenCalledWith('123456'); expect(handleChange).not.toHaveBeenCalledWith('123456');
expect(screen.getByText('内容不能超过 5 个字符')).toBeInTheDocument();
}); });
it('未超出 maxLength 的输入应正常触发', () => { it('未超出 maxLength 的输入应正常触发', () => {
@@ -369,7 +370,7 @@ describe('TextInputArea 组件', () => {
const writeTextSpy = vi.spyOn(navigator.clipboard, 'writeText').mockResolvedValue(undefined); const writeTextSpy = vi.spyOn(navigator.clipboard, 'writeText').mockResolvedValue(undefined);
render(<TextInputArea value="测试" onChange={() => {}} allowCopy />); render(<TextInputArea value="测试" onChange={() => {}} allowCopy />);
await user.click(screen.getByRole('button', { name: '复制内容' })); await user.click(screen.getByRole('button', { name: 'textInputArea.copyContent' }));
expect(writeTextSpy).toHaveBeenCalledWith('测试'); expect(writeTextSpy).toHaveBeenCalledWith('测试');
}); });
@@ -380,7 +381,7 @@ describe('TextInputArea 组件', () => {
const handleClear = vi.fn(); const handleClear = vi.fn();
render(<TextInputArea value="内容" onChange={() => {}} onClear={handleClear} />); render(<TextInputArea value="内容" onChange={() => {}} onClear={handleClear} />);
fireEvent.click(screen.getByRole('button', { name: '清空' })); fireEvent.click(screen.getByRole('button', { name: 'textInputArea.clear' }));
expect(handleClear).toHaveBeenCalledOnce(); expect(handleClear).toHaveBeenCalledOnce();
}); });
@@ -388,7 +389,7 @@ describe('TextInputArea 组件', () => {
it('不传 onClear 时清空按钮应正常工作', () => { it('不传 onClear 时清空按钮应正常工作', () => {
render(<TextInputArea defaultValue="内容" />); render(<TextInputArea defaultValue="内容" />);
fireEvent.click(screen.getByRole('button', { name: '清空' })); fireEvent.click(screen.getByRole('button', { name: 'textInputArea.clear' }));
expect(screen.getByRole('textbox')).toHaveValue(''); expect(screen.getByRole('textbox')).toHaveValue('');
}); });
+2 -3
View File
@@ -19,7 +19,7 @@
- `defaultVisible`:默认是否可见 - `defaultVisible`:默认是否可见
- `components`:三种渲染模式的懒加载组件(`popup``sidepanel``tab` - `components`:三种渲染模式的懒加载组件(`popup``sidepanel``tab`
## 已注册功能(11 个) ## 已注册功能(10 个)
| key | 图标 | 说明 | | key | 图标 | 说明 |
| -------------------- | ----------------- | ----------------- | | -------------------- | ----------------- | ----------------- |
@@ -31,9 +31,8 @@
| `jwt` | Key | JWT 解析工具 | | `jwt` | Key | JWT 解析工具 |
| `jsonDiff` | GitCompareArrows | JSON 差异比较工具 | | `jsonDiff` | GitCompareArrows | JSON 差异比较工具 |
| `base64Converter` | ArrowLeftRight | Base64 转换器 | | `base64Converter` | ArrowLeftRight | Base64 转换器 |
| `markdownToHtml` | Code | Markdown 转 HTML |
| `htmlToMarkdown` | File | HTML 转 Markdown |
| `rightClickRestorer` | MousePointerClick | 右键菜单恢复工具 | | `rightClickRestorer` | MousePointerClick | 右键菜单恢复工具 |
| `testDataGenerator` | FileSpreadsheet | 测试数据生成器 |
## 导出函数 ## 导出函数
+7 -7
View File
@@ -16,13 +16,13 @@ describe('features', () => {
it('should have all required properties for each feature', () => { it('should have all required properties for each feature', () => {
FEATURES.forEach((feature) => { FEATURES.forEach((feature) => {
expect(feature).toHaveProperty('key'); expect(feature).toHaveProperty('key');
expect(feature).toHaveProperty('label'); expect(feature).toHaveProperty('labelKey');
expect(feature).toHaveProperty('description'); expect(feature).toHaveProperty('descriptionKey');
expect(feature).toHaveProperty('defaultVisible'); expect(feature).toHaveProperty('defaultVisible');
expect(feature).toHaveProperty('components'); expect(feature).toHaveProperty('components');
expect(typeof feature.key).toBe('string'); expect(typeof feature.key).toBe('string');
expect(typeof feature.label).toBe('string'); expect(typeof feature.labelKey).toBe('string');
expect(typeof feature.description).toBe('string'); expect(typeof feature.descriptionKey).toBe('string');
expect(typeof feature.defaultVisible).toBe('boolean'); expect(typeof feature.defaultVisible).toBe('boolean');
expect(typeof feature.components).toBe('object'); expect(typeof feature.components).toBe('object');
expect(feature.components).toHaveProperty('popup'); expect(feature.components).toHaveProperty('popup');
@@ -50,14 +50,14 @@ describe('features', () => {
const feature = getFeatureByKey('dashboard'); const feature = getFeatureByKey('dashboard');
expect(feature).toBeDefined(); expect(feature).toBeDefined();
expect(feature?.key).toBe('dashboard'); expect(feature?.key).toBe('dashboard');
expect(feature?.label).toBe('仪表盘'); expect(feature?.labelKey).toBe('dashboard_title');
}); });
it('should return timestamp feature', () => { it('should return timestamp feature', () => {
const feature = getFeatureByKey('timestamp'); const feature = getFeatureByKey('timestamp');
expect(feature).toBeDefined(); expect(feature).toBeDefined();
expect(feature?.key).toBe('timestamp'); expect(feature?.key).toBe('timestamp');
expect(feature?.label).toBe('时间戳'); expect(feature?.labelKey).toBe('timestamp_title');
expect(feature?.themeColorKey).toBeDefined(); expect(feature?.themeColorKey).toBeDefined();
}); });
@@ -65,7 +65,7 @@ describe('features', () => {
const feature = getFeatureByKey('storageCleaner'); const feature = getFeatureByKey('storageCleaner');
expect(feature).toBeDefined(); expect(feature).toBeDefined();
expect(feature?.key).toBe('storageCleaner'); expect(feature?.key).toBe('storageCleaner');
expect(feature?.label).toBe('存储清理'); expect(feature?.labelKey).toBe('storageCleaner_title');
}); });
it('should return undefined for invalid key', () => { it('should return undefined for invalid key', () => {
+22 -22
View File
@@ -29,8 +29,8 @@ const TestDataGeneratorPage = lazy(() => import('@/pages/TestDataGenerator'));
export interface FeatureConfig { export interface FeatureConfig {
key: PageType; key: PageType;
label: string; labelKey: string;
description: string; descriptionKey: string;
themeColorKey?: PaletteColorKey; themeColorKey?: PaletteColorKey;
icon?: ComponentType<LucideProps>; icon?: ComponentType<LucideProps>;
defaultVisible: boolean; defaultVisible: boolean;
@@ -44,8 +44,8 @@ export interface FeatureConfig {
export const FEATURES: FeatureConfig[] = [ export const FEATURES: FeatureConfig[] = [
{ {
key: 'dashboard', key: 'dashboard',
label: '仪表盘', labelKey: 'dashboard_title',
description: '', descriptionKey: '',
defaultVisible: true, defaultVisible: true,
components: { components: {
popup: DashboardPage, popup: DashboardPage,
@@ -55,8 +55,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'timestamp', key: 'timestamp',
label: '时间戳', labelKey: 'timestamp_title',
description: 'Unix 毫秒数转换与格式化', descriptionKey: 'timestamp_description',
themeColorKey: 'primary', themeColorKey: 'primary',
icon: Clock, icon: Clock,
defaultVisible: true, defaultVisible: true,
@@ -68,8 +68,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'storageCleaner', key: 'storageCleaner',
label: '存储清理', labelKey: 'storageCleaner_title',
description: '清理缓存、Cookies 及本地存储', descriptionKey: 'storageCleaner_description',
themeColorKey: 'warning', themeColorKey: 'warning',
icon: Database, icon: Database,
defaultVisible: true, defaultVisible: true,
@@ -81,8 +81,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'qrCode', key: 'qrCode',
label: '二维码工具', labelKey: 'qrCode_title',
description: '生成当前选中的 URL 的二维码', descriptionKey: 'qrCode_description',
themeColorKey: 'success', themeColorKey: 'success',
icon: QrCode, icon: QrCode,
defaultVisible: true, defaultVisible: true,
@@ -94,8 +94,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'textStatistics', key: 'textStatistics',
label: '文本统计', labelKey: 'textStatistics_title',
description: '实时分析文本字符、单词及字节', descriptionKey: 'textStatistics_description',
themeColorKey: 'secondary', themeColorKey: 'secondary',
icon: FileText, icon: FileText,
defaultVisible: true, defaultVisible: true,
@@ -107,8 +107,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'jwt', key: 'jwt',
label: 'JWT 解析', labelKey: 'jwt_title',
description: 'JSON Web Token 解码与查看', descriptionKey: 'jwt_description',
themeColorKey: 'info', themeColorKey: 'info',
icon: Key, icon: Key,
defaultVisible: true, defaultVisible: true,
@@ -120,8 +120,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'jsonDiff', key: 'jsonDiff',
label: 'JSON 工具', labelKey: 'jsonDiff_title',
description: '差异比较、格式化、YAML/TOML 转换及压缩', descriptionKey: 'jsonDiff_description',
themeColorKey: 'primary', themeColorKey: 'primary',
icon: GitCompareArrows, icon: GitCompareArrows,
defaultVisible: true, defaultVisible: true,
@@ -133,8 +133,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'base64Converter', key: 'base64Converter',
label: 'Base64 转换器', labelKey: 'base64Converter_title',
description: '文本、文件与图像的 Base64 编码转换', descriptionKey: 'base64Converter_description',
themeColorKey: 'info', themeColorKey: 'info',
icon: ArrowLeftRight, icon: ArrowLeftRight,
defaultVisible: true, defaultVisible: true,
@@ -146,8 +146,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'rightClickRestorer', key: 'rightClickRestorer',
label: '右键恢复', labelKey: 'rightClickRestorer_title',
description: '检测并恢复被网站禁用的浏览器右键菜单', descriptionKey: 'rightClickRestorer_description',
themeColorKey: 'success', themeColorKey: 'success',
icon: MousePointerClick, icon: MousePointerClick,
defaultVisible: true, defaultVisible: true,
@@ -159,8 +159,8 @@ export const FEATURES: FeatureConfig[] = [
}, },
{ {
key: 'testDataGenerator', key: 'testDataGenerator',
label: '测试数据生成器', labelKey: 'testDataGenerator_title',
description: '自定义规则批量生成测试数据', descriptionKey: 'testDataGenerator_description',
themeColorKey: 'warning', themeColorKey: 'warning',
icon: FileSpreadsheet, icon: FileSpreadsheet,
defaultVisible: true, defaultVisible: true,
+2 -2
View File
@@ -36,7 +36,7 @@ describe('background 菜单注册与分流', () => {
expect(chrome.contextMenus.create).toHaveBeenCalledWith( expect(chrome.contextMenus.create).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
id: 'jwt', id: 'jwt',
title: '解析 JWT', title: '🔑 解析 JWT',
parentId: 'testing-tools-parent', parentId: 'testing-tools-parent',
}), }),
); );
@@ -48,7 +48,7 @@ describe('background 菜单注册与分流', () => {
expect(chrome.contextMenus.create).toHaveBeenCalledWith( expect(chrome.contextMenus.create).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
id: 'qrCode-page', id: 'qrCode-page',
title: '网页链接转二维码', title: '🔗 网页链接转二维码',
contexts: ['page'], contexts: ['page'],
}), }),
); );
@@ -1,10 +1,11 @@
import type { ContextMenuClickedPayload } from '@/utils/messages'; import type { ContextMenuClickedPayload } from '@/utils/messages';
import { MessageAction, onMessage } from '@/utils/messages'; import { MessageAction, onMessage } from '@/utils/messages';
import { getTextStats } from '@/utils/textStatistics'; import { getTextStats } from '@/utils/textStatistics';
import { getMessage } from '@/utils/chromeI18n';
import { hidePopover, showTextStatsResult, showTimestampResult } from './uiPopover'; import { hidePopover, showTextStatsResult, showTimestampResult } from './uiPopover';
function convertTimestamp(input: string): string { function convertTimestamp(input: string): string {
const invalidText = '无效时间戳'; const invalidText = getMessage('invalidTimestamp') || 'Invalid Timestamp';
const num = Number(input.trim()); const num = Number(input.trim());
if (isNaN(num)) { if (isNaN(num)) {
+32 -2
View File
@@ -4,9 +4,10 @@
## 文件说明 ## 文件说明
| 文件 | 用途 | | 文件/目录 | 用途 |
| ---------- | ------------------------------------ | | ------------- | -------------------------------------- |
| `utils.ts` | `cn()` 函数 — shadcn/ui 标准工具函数 | | `utils.ts` | `cn()` 函数 — shadcn/ui 标准工具函数 |
| `generators/` | 测试数据生成器内置生成器库和分类注册表 |
## cn() ## cn()
@@ -24,3 +25,32 @@ import { cn } from '@/lib/utils';
``` ```
所有需要动态合并 Tailwind 类名的场景都应使用 `cn()`,而非手动拼接字符串。 所有需要动态合并 Tailwind 类名的场景都应使用 `cn()`,而非手动拼接字符串。
## generators/
测试数据生成器的核心生成器库,供 `src/workers/generator.worker.ts` 在后台线程中按字段配置生成数据。
| 文件 | 用途 |
| -------------- | ---------------------------------------------- |
| `index.ts` | 聚合所有生成器,导出分类、查找和搜索函数 |
| `personal.ts` | 个人信息生成器(姓名、邮箱、手机号、身份证等) |
| `business.ts` | 业务数据生成器(订单号、价格、日期、状态等) |
| `technical.ts` | 技术数据生成器(UUID、IPv4、URL |
| `basic.ts` | 基础类型生成器(整数、浮点数、布尔值、字符串) |
| `types.ts` | 生成器库内部类型与通用选项 |
使用示例:
```typescript
import { getGeneratorById, searchGenerators } from '@/lib/generators';
const generator = getGeneratorById('chineseName');
const value = generator?.generate({});
const matched = searchGenerators('uuid');
```
约束:
- 新增生成器必须实现 `GeneratorDefinition`,并加入所属分类文件的导出列表。
- 需要唯一值或大批量稳定输出时,优先实现 `generateAtIndex(params, index)`
- 新增分类时同步更新 `generatorCategories`,并确保 `GeneratorSelector` 能展示该分类。
@@ -1,14 +1,14 @@
import { Image as ImageIcon, Trash2, Upload } from 'lucide-react'; import { Image as ImageIcon, Trash2, Upload } from 'lucide-react';
import TextInputArea from '@/components/TextInputArea'; import TextInputArea from '@/components/TextInputArea';
import { useI18n } from '@/utils/chromeI18n';
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
import DecodeResultPaper from './DecodeResultPaper'; import DecodeResultPaper from '@/components/DecodeResultPaper';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { downloadBlob } from '@/utils/base64Converter'; import { downloadBlob, formatFileSize } from '@/utils/base64Converter';
import { formatBytes } from '@/utils/format';
import { useStorageState } from '@/utils/useStorageState'; import { useStorageState } from '@/utils/useStorageState';
import type { Base64ConvertDirection } from '@/types/storage'; import type { Base64ConvertDirection } from '@/types/storage';
import SwitchButtonGroup from '@/components/SwitchButtonGroup'; import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { useBase64Converter } from '../useBase64Converter'; import { useBase64Converter } from './useBase64Converter';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
const isValidDirection = (val: unknown): val is Base64ConvertDirection => const isValidDirection = (val: unknown): val is Base64ConvertDirection =>
@@ -19,6 +19,8 @@ interface Base64ConverterSectionProps {
} }
export default function Base64ConverterSection({ mode }: Base64ConverterSectionProps) { export default function Base64ConverterSection({ mode }: Base64ConverterSectionProps) {
const { t } = useI18n('base64Converter');
const [direction, setDirection] = useStorageState( const [direction, setDirection] = useStorageState(
`base64Converter/${mode}Mode/direction`, `base64Converter/${mode}Mode/direction`,
'encode', 'encode',
@@ -41,6 +43,7 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
setCustomFileName, setCustomFileName,
resetAll, resetAll,
safeFileSelect, safeFileSelect,
maxFileSizeStr,
} = useBase64Converter({ mode }); } = useBase64Converter({ mode });
const handleDirectionChange = (next: Base64ConvertDirection) => { const handleDirectionChange = (next: Base64ConvertDirection) => {
@@ -59,8 +62,8 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
<SwitchButtonGroup <SwitchButtonGroup
value={direction} value={direction}
options={[ options={[
{ value: 'encode', label: '编码' }, { value: 'encode', label: t('encode') },
{ value: 'decode', label: '解码' }, { value: 'decode', label: t('decode') },
]} ]}
onChange={handleDirectionChange} onChange={handleDirectionChange}
size="small" size="small"
@@ -119,10 +122,10 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
{info.name} {info.name}
</span> </span>
<span className="text-xs text-muted-foreground/80 font-mono tabular-nums"> <span className="text-xs text-muted-foreground/80 font-mono tabular-nums">
{formatBytes(info.size)} · {info.type} {formatFileSize(info.size)} · {info.type}
</span> </span>
<span className="text-[11px] font-medium text-primary/80 mt-1"> <span className="text-[11px] font-medium text-primary/80 mt-1">
{'点击或拖拽以替换文件'} {t('clickOrDropToReplace')}
</span> </span>
</div> </div>
) : ( ) : (
@@ -133,14 +136,14 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
<Upload className="w-8 h-8 text-muted-foreground/60" /> <Upload className="w-8 h-8 text-muted-foreground/60" />
)} )}
<span className="text-xs font-bold text-foreground/80"> <span className="text-xs font-bold text-foreground/80">
{mode === 'image' ? '点击或拖拽图像到此处' : '点击或拖拽文件到此处'} {mode === 'image' ? t('clickOrDropToImage') : t('clickOrDropToFile')}
</span> </span>
<span className="text-[10px] font-medium text-muted-foreground/60"> <span className="text-[10px] font-medium text-muted-foreground/60">
{'最大文件大小:{{max}}'} {t('maxFileSize', { max: maxFileSizeStr })}
</span> </span>
{mode === 'image' && ( {mode === 'image' && (
<span className="text-[10px] font-medium text-muted-foreground/50"> <span className="text-[10px] font-medium text-muted-foreground/50">
{'支持 PNG、JPG、WEBP、GIF、BMP、SVG 等格式'} {t('supportedFormats')}
</span> </span>
)} )}
</div> </div>
@@ -157,17 +160,17 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
<div className="p-4 rounded-2xl bg-card border border-border shadow-sm flex flex-col space-y-3"> <div className="p-4 rounded-2xl bg-card border border-border shadow-sm flex flex-col space-y-3">
<div className="flex justify-between items-center select-none"> <div className="flex justify-between items-center select-none">
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90"> <span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90">
{'Base64 编码结果'} {t('base64Output')}
</span> </span>
<div className="flex gap-2"> <div className="flex gap-2">
<CopyButton <CopyButton
text={result.rawBase64} text={result.rawBase64}
tooltip={'复制纯 Base64'} tooltip={t('copyRaw')}
className="h-6 px-2 rounded-md border text-[10px] font-bold" className="h-6 px-2 rounded-md border text-[10px] font-bold"
/> />
<CopyButton <CopyButton
text={result.output} text={result.output}
tooltip={'复制 Data URI'} tooltip={t('copyDataUri')}
className="h-6 px-2 rounded-md border text-[10px] font-bold" className="h-6 px-2 rounded-md border text-[10px] font-bold"
/> />
</div> </div>
@@ -185,16 +188,16 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
<div className="flex items-center justify-between font-mono text-[10px] text-muted-foreground/70 select-none pt-1"> <div className="flex items-center justify-between font-mono text-[10px] text-muted-foreground/70 select-none pt-1">
<div className="flex gap-4 items-center tabular-nums"> <div className="flex gap-4 items-center tabular-nums">
<span> <span>
{'原始大小'}:{' '} {t('originalSize')}:{' '}
<span className="font-semibold text-foreground/80"> <span className="font-semibold text-foreground/80">
{formatBytes(result.originalBytes)} {formatFileSize(result.originalBytes)}
</span> </span>
</span> </span>
<span className="text-border/60">|</span> <span className="text-border/60">|</span>
<span> <span>
{'编码大小'}:{' '} {t('encodedSize')}:{' '}
<span className="font-semibold text-foreground/80"> <span className="font-semibold text-foreground/80">
{formatBytes(result.outputBytes)} {formatFileSize(result.outputBytes)}
</span> </span>
</span> </span>
</div> </div>
@@ -205,7 +208,7 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
className="h-7 rounded-md text-muted-foreground hover:text-destructive text-[11px] gap-1 px-2" className="h-7 rounded-md text-muted-foreground hover:text-destructive text-[11px] gap-1 px-2"
> >
<Trash2 className="w-3.5 h-3.5" /> <Trash2 className="w-3.5 h-3.5" />
{'清空'} {t('clear')}
</Button> </Button>
</div> </div>
</div> </div>
@@ -214,7 +217,7 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
) : ( ) : (
<div className="flex flex-col space-y-4"> <div className="flex flex-col space-y-4">
<TextInputArea <TextInputArea
placeholder={'输入需要解码的 Base64 或 data URI...'} placeholder={t('decodeBase64Placeholder')}
value={decodeInput} value={decodeInput}
onChange={setDecodeInput} onChange={setDecodeInput}
externalError={decodeError || undefined} externalError={decodeError || undefined}
@@ -225,7 +228,7 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
/> />
{decoded && ( {decoded && (
<DecodeResultPaper <DecodeResultPaper
title={mode === 'image' ? '解码图像' : '解码文件'} title={mode === 'image' ? t('decodedImageOutput') : t('decodedFileOutput')}
mimeType={decoded.mimeType} mimeType={decoded.mimeType}
blobSize={decoded.blob.size} blobSize={decoded.blob.size}
fileName={decodedFileName} fileName={decodedFileName}
+162
View File
@@ -0,0 +1,162 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import TextInputArea from '@/components/TextInputArea';
import { useI18n } from '@/utils/chromeI18n';
import { CopyButton } from '@/components/CopyButton';
import { base64ToText, textToBase64 } from '@/utils/base64Converter';
import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { useContextMenuData } from '@/utils/useContextMenuData';
import { Button } from '@/components/ui/button';
const IMAGE_DATA_URI_PATTERN = /^\s*data:image\//i;
const ERROR_MESSAGE_TO_I18N: Record<string, string> = {
'Invalid Base64 string': 'invalidBase64',
'Input appears to be binary data (e.g. an image). Please use the Image tab instead.':
'binaryDataDetected',
};
interface TextModeProps {
onSwitchToImageMode?: () => void;
}
export default function TextMode({ onSwitchToImageMode }: TextModeProps = {}) {
const { t } = useI18n('base64Converter');
const [input, setInput] = useState('');
const [debouncedInput, setDebouncedInput] = useState('');
const [direction, setDirection] = useState<'encode' | 'decode'>('encode');
useEffect(() => {
const handle = setTimeout(() => {
setDebouncedInput(input);
}, 200);
return () => clearTimeout(handle);
}, [input]);
const handleContextMenuData = useCallback((payload: string) => {
setInput(payload);
setDebouncedInput(payload);
setDirection('decode');
}, []);
useContextMenuData({ featureKey: 'base64Converter', onData: handleContextMenuData });
const conversionPipeline = useMemo(() => {
const trimmed = debouncedInput.trim();
if (!trimmed) return { output: '', error: null };
try {
if (direction === 'encode') {
const result = textToBase64(debouncedInput);
return { output: result.output, error: null };
} else {
const decoded = base64ToText(trimmed);
return { output: decoded, error: null };
}
} catch (e) {
const message = e instanceof Error ? e.message : '';
const i18nKey = ERROR_MESSAGE_TO_I18N[message];
return {
output: '',
error: i18nKey ? t(i18nKey) : message || t('conversionFailed'),
};
}
}, [debouncedInput, direction, t]);
const output = conversionPipeline.output;
const error = conversionPipeline.error;
const placeholder =
direction === 'encode' ? t('textInputPlaceholder') : t('base64InputPlaceholder');
const outputLabel = direction === 'encode' ? t('base64Output') : t('textOutput');
const showImageHint = useMemo(
() => direction === 'decode' && IMAGE_DATA_URI_PATTERN.test(input),
[direction, input],
);
const handleDirectionChange = (value: 'encode' | 'decode') => {
if (value === direction) return;
setDirection(value);
setInput('');
setDebouncedInput('');
};
const handleClear = () => {
setInput('');
setDebouncedInput('');
};
return (
<div className="w-full flex flex-col space-y-4 px-2">
{/* 受控方向切流中枢 */}
<div className="flex h-11 items-center px-1.5 bg-secondary/40 rounded-xl border border-border/60 w-fit">
<SwitchButtonGroup
value={direction}
options={[
{ value: 'encode', label: t('encode') },
{ value: 'decode', label: t('decode') },
]}
onChange={handleDirectionChange}
size="small"
/>
</div>
{/* 高性能受控文本输入端 */}
<TextInputArea
placeholder={placeholder}
value={input}
onChange={setInput}
externalError={error || undefined}
showClear={true}
allowCopy={true}
minRows={5}
maxRows={10}
onClear={handleClear}
/>
{/* 图片 URI 类型劫持警告引导区:
- 💡 修复点:彻底废除原生亮色硬编码 hover:bg-blue-100 类名,
- 完美向全站 shadcn 暗黑生态看齐,采用标准的 bg-primary/10 混合变体。
*/}
{showImageHint && (
<div className="flex items-center justify-between p-3.5 rounded-xl bg-primary/10 border border-primary/20">
<span className="text-xs font-semibold text-primary tracking-tight">
{t('imageDataUriHint')}
</span>
<Button
type="button"
variant="ghost"
size="sm"
onClick={onSwitchToImageMode}
className="h-7 rounded-md text-xs font-bold text-primary hover:text-primary hover:bg-primary/20 dark:hover:bg-primary/10 px-2.5"
>
{t('switchToImageMode')}
</Button>
</div>
)}
{/* 5. 编码/解码核心数据承载流卡片 */}
{output && (
<div className="p-4 rounded-2xl bg-card border border-border shadow-sm flex flex-col space-y-3">
<div className="flex justify-between items-center select-none">
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90">
{outputLabel}
</span>
<CopyButton
text={output}
className="h-6 px-2 rounded-md border text-[10px] font-bold"
/>
</div>
<TextInputArea
readOnly
value={output.length > 2000 ? `${output.substring(0, 2000)}...` : output}
showClear={false}
minRows={4}
/>
</div>
)}
</div>
);
}
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import TextMode from '../components/TextMode'; import TextMode from '../TextMode';
// Mock CopyButton // Mock CopyButton
vi.mock('@/components/CopyButton', () => ({ vi.mock('@/components/CopyButton', () => ({
@@ -76,7 +76,7 @@ describe('TextMode', () => {
}); });
await waitFor(() => { await waitFor(() => {
expect(screen.getByText('无效的 Base64 字符串')).toBeInTheDocument(); expect(screen.getByText('Base64 字符串无效')).toBeInTheDocument();
}); });
}); });
@@ -118,7 +118,7 @@ describe('TextMode', () => {
expect(screen.getByRole('button', { name: 'SGVsbG8=' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'SGVsbG8=' })).toBeInTheDocument();
}); });
fireEvent.click(screen.getByRole('button', { name: '清空' })); fireEvent.click(screen.getByRole('button', { name: 'textInputArea.clear' }));
await waitFor(() => { await waitFor(() => {
expect(screen.queryByRole('button', { name: 'SGVsbG8=' })).not.toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'SGVsbG8=' })).not.toBeInTheDocument();
@@ -182,7 +182,9 @@ describe('TextMode', () => {
}); });
await waitFor(() => { await waitFor(() => {
expect(screen.getByText('检测到二进制数据(如图像),请使用图像选项卡')).toBeInTheDocument(); expect(
screen.getByText('输入似乎是二进制数据(如图片)。请切换到「图像」选项卡。'),
).toBeInTheDocument();
}); });
}); });
}); });
@@ -12,7 +12,7 @@ vi.mock('@/config/features', async (importOriginal) => {
}); });
// Mock 子组件 // Mock 子组件
vi.mock('../components/TextMode', () => ({ vi.mock('../TextMode', () => ({
default: ({ onSwitchToImageMode }: { onSwitchToImageMode?: () => void }) => ( default: ({ onSwitchToImageMode }: { onSwitchToImageMode?: () => void }) => (
<div data-testid="text-mode"> <div data-testid="text-mode">
TextMode TextMode
@@ -21,7 +21,7 @@ vi.mock('../components/TextMode', () => ({
), ),
})); }));
vi.mock('../components/Base64ConverterSection', () => ({ vi.mock('../Base64ConverterSection', () => ({
default: ({ mode }: { mode: string }) => <div data-testid={`${mode}-mode`}>{mode}</div>, default: ({ mode }: { mode: string }) => <div data-testid={`${mode}-mode`}>{mode}</div>,
})); }));
@@ -1,90 +0,0 @@
import TextInputArea from '@/components/TextInputArea';
import { CopyButton } from '@/components/CopyButton';
import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { Button } from '@/components/ui/button';
import { useTextMode } from '../useTextMode';
interface TextModeProps {
onSwitchToImageMode?: () => void;
}
export default function TextMode({ onSwitchToImageMode }: TextModeProps = {}) {
const {
input,
setInput,
direction,
handleDirectionChange,
placeholder,
output,
outputLabel,
error,
showImageHint,
handleClear,
} = useTextMode();
return (
<div className="w-full flex flex-col space-y-4 px-2">
<div className="flex h-11 items-center px-1.5 bg-secondary/40 rounded-xl border border-border/60 w-fit">
<SwitchButtonGroup
value={direction}
options={[
{ value: 'encode', label: '编码' },
{ value: 'decode', label: '解码' },
]}
onChange={handleDirectionChange}
size="small"
/>
</div>
<TextInputArea
placeholder={placeholder}
value={input}
onChange={setInput}
externalError={error || undefined}
showClear={true}
allowCopy={true}
minRows={5}
maxRows={10}
onClear={handleClear}
/>
{showImageHint && (
<div className="flex items-center justify-between p-3.5 rounded-xl bg-primary/10 border border-primary/20">
<span className="text-xs font-semibold text-primary tracking-tight">
data URI使
</span>
<Button
type="button"
variant="ghost"
size="sm"
onClick={onSwitchToImageMode}
className="h-7 rounded-md text-xs font-bold text-primary hover:text-primary hover:bg-primary/20 dark:hover:bg-primary/10 px-2.5"
>
</Button>
</div>
)}
{output && (
<div className="p-4 rounded-2xl bg-card border border-border shadow-sm flex flex-col space-y-3">
<div className="flex justify-between items-center select-none">
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90">
{outputLabel}
</span>
<CopyButton
text={output}
className="h-6 px-2 rounded-md border text-[10px] font-bold"
/>
</div>
<TextInputArea
readOnly
value={output.length > 2000 ? `${output.substring(0, 2000)}...` : output}
showClear={false}
minRows={4}
/>
</div>
)}
</div>
);
}
+7 -5
View File
@@ -1,7 +1,8 @@
import { useI18n } from '@/utils/chromeI18n';
import { useStorageState } from '@/utils/useStorageState'; import { useStorageState } from '@/utils/useStorageState';
import type { Base64ConverterPageMode } from '@/types/storage'; import type { Base64ConverterPageMode } from '@/types/storage';
import TextMode from './components/TextMode'; import TextMode from './TextMode';
import Base64ConverterSection from './components/Base64ConverterSection'; import Base64ConverterSection from './Base64ConverterSection';
import SwitchButtonGroup from '@/components/SwitchButtonGroup'; import SwitchButtonGroup from '@/components/SwitchButtonGroup';
const VALID_PAGE_MODES: readonly Base64ConverterPageMode[] = ['text', 'file', 'image']; const VALID_PAGE_MODES: readonly Base64ConverterPageMode[] = ['text', 'file', 'image'];
@@ -11,6 +12,7 @@ const isValidPageMode = (val: unknown): val is Base64ConverterPageMode =>
type PageMode = Base64ConverterPageMode; type PageMode = Base64ConverterPageMode;
export default function Index() { export default function Index() {
const { t } = useI18n('base64Converter');
const [pageMode, setPageMode] = useStorageState( const [pageMode, setPageMode] = useStorageState(
'base64Converter/pageMode', 'base64Converter/pageMode',
'text', 'text',
@@ -22,9 +24,9 @@ export default function Index() {
<SwitchButtonGroup <SwitchButtonGroup
value={pageMode} value={pageMode}
options={[ options={[
{ value: 'text', label: '文本' }, { value: 'text', label: t('base64Converter:textMode') },
{ value: 'file', label: '文件' }, { value: 'file', label: t('base64Converter:fileMode') },
{ value: 'image', label: '图像' }, { value: 'image', label: t('base64Converter:imageMode') },
]} ]}
onChange={(value: PageMode) => setPageMode(value)} onChange={(value: PageMode) => setPageMode(value)}
size="small" size="small"
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useI18n } from '@/utils/chromeI18n';
import type { FileToBase64Result } from '@/utils/base64Converter'; import type { FileToBase64Result } from '@/utils/base64Converter';
import { import {
base64ToBlob, base64ToBlob,
@@ -20,6 +21,8 @@ interface UseBase64ConverterProps {
} }
export function useBase64Converter({ mode }: UseBase64ConverterProps) { export function useBase64Converter({ mode }: UseBase64ConverterProps) {
const { t } = useI18n('base64Converter');
const [result, setResult] = useState<FileToBase64Result | null>(null); const [result, setResult] = useState<FileToBase64Result | null>(null);
const [info, setInfo] = useState<FileInfo | null>(null); const [info, setInfo] = useState<FileInfo | null>(null);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
@@ -59,7 +62,7 @@ export function useBase64Converter({ mode }: UseBase64ConverterProps) {
setInfo(null); setInfo(null);
if (!isFileSizeValid(file.size)) { if (!isFileSizeValid(file.size)) {
setEncodeError(`文件大小超出限制(最大 ${MAX_FILE_SIZE / 1024 / 1024} MB`); setEncodeError(t('fileSizeExceeded', { max: `${MAX_FILE_SIZE / 1024 / 1024} MB` }));
return; return;
} }
@@ -68,7 +71,7 @@ export function useBase64Converter({ mode }: UseBase64ConverterProps) {
!isSupportedImageType(file.type) && !isSupportedImageType(file.type) &&
!isSupportedImageExtension(file.name) !isSupportedImageExtension(file.name)
) { ) {
setEncodeError('不支持的图像格式'); setEncodeError(t('unsupportedImageType'));
return; return;
} }
@@ -84,13 +87,13 @@ export function useBase64Converter({ mode }: UseBase64ConverterProps) {
if (!cancelRef.current) setResult(res); if (!cancelRef.current) setResult(res);
} catch (e) { } catch (e) {
if (!cancelRef.current) { if (!cancelRef.current) {
setEncodeError(e instanceof Error ? e.message : '转换失败'); setEncodeError(e instanceof Error ? e.message : t('conversionFailed'));
} }
} finally { } finally {
if (!cancelRef.current) setIsLoading(false); if (!cancelRef.current) setIsLoading(false);
} }
}, },
[mode], [mode, t],
); );
const safeFileSelect = useCallback( const safeFileSelect = useCallback(
@@ -113,10 +116,10 @@ export function useBase64Converter({ mode }: UseBase64ConverterProps) {
const message = e instanceof Error ? e.message : ''; const message = e instanceof Error ? e.message : '';
return { return {
decoded: null, decoded: null,
error: message === 'Invalid Base64 string' ? 'Base64 字符串无效' : '转换失败', error: message === 'Invalid Base64 string' ? t('invalidBase64') : t('conversionFailed'),
}; };
} }
}, [debouncedDecodeInput]); }, [debouncedDecodeInput, t]);
const decoded = decodePipeline.decoded; const decoded = decodePipeline.decoded;
const decodeError = decodePipeline.error; const decodeError = decodePipeline.error;
-91
View File
@@ -1,91 +0,0 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { base64ToText, textToBase64 } from '@/utils/base64Converter';
import { useContextMenuData } from '@/utils/useContextMenuData';
const IMAGE_DATA_URI_PATTERN = /^\s*data:image\//i;
const ERROR_MESSAGE_TO_I18N: Record<string, string> = {
'Invalid Base64 string': '无效的 Base64 字符串',
'Input appears to be binary data (e.g. an image). Please use the Image tab instead.':
'检测到二进制数据(如图像),请使用图像选项卡',
};
export function useTextMode() {
const [input, setInput] = useState('');
const [debouncedInput, setDebouncedInput] = useState('');
const [direction, setDirection] = useState<'encode' | 'decode'>('encode');
useEffect(() => {
const handle = setTimeout(() => {
setDebouncedInput(input);
}, 200);
return () => clearTimeout(handle);
}, [input]);
const handleContextMenuData = useCallback((payload: string) => {
setInput(payload);
setDebouncedInput(payload);
setDirection('decode');
}, []);
useContextMenuData({ featureKey: 'base64Converter', onData: handleContextMenuData });
const conversionPipeline = useMemo(() => {
const trimmed = debouncedInput.trim();
if (!trimmed) return { output: '', error: null };
try {
if (direction === 'encode') {
const result = textToBase64(debouncedInput);
return { output: result.output, error: null };
} else {
const decoded = base64ToText(trimmed);
return { output: decoded, error: null };
}
} catch (e) {
const message = e instanceof Error ? e.message : '';
const i18nKey = ERROR_MESSAGE_TO_I18N[message];
return {
output: '',
error: i18nKey || message || '转换失败',
};
}
}, [debouncedInput, direction]);
const output = conversionPipeline.output;
const error = conversionPipeline.error;
const placeholder =
direction === 'encode' ? '输入需要编码为 Base64 的文本...' : '输入需要解码的 Base64 字符串...';
const outputLabel = direction === 'encode' ? 'Base64 编码结果' : '解码文本结果';
const showImageHint = useMemo(
() => direction === 'decode' && IMAGE_DATA_URI_PATTERN.test(input),
[direction, input],
);
const handleDirectionChange = (value: 'encode' | 'decode') => {
if (value === direction) return;
setDirection(value);
setInput('');
setDebouncedInput('');
};
const handleClear = () => {
setInput('');
setDebouncedInput('');
};
return {
input,
setInput,
direction,
handleDirectionChange,
placeholder,
output,
outputLabel,
error,
showImageHint,
handleClear,
};
}
+6 -4
View File
@@ -1,10 +1,12 @@
import { useRouter } from '@/providers/RouterProvider'; import { useRouter } from '@/providers/RouterProvider';
import { getFeatureByKey } from '@/config/features'; import { getFeatureByKey } from '@/config/features';
import type { PageType } from '@/types/storage'; import type { PageType } from '@/types/storage';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
export default function Index() { export default function Index() {
const { navigateTo, visiblePages, pageOrder, recentlyUsedTools } = useRouter(); const { navigateTo, visiblePages, pageOrder, recentlyUsedTools } = useRouter();
const { t } = useI18n(['features']);
const visibleSet = new Set<string>(visiblePages); const visibleSet = new Set<string>(visiblePages);
@@ -26,7 +28,7 @@ export default function Index() {
{showRecent && ( {showRecent && (
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
<h3 className="text-xs font-semibold text-muted-foreground/80 uppercase tracking-wider"> <h3 className="text-xs font-semibold text-muted-foreground/80 uppercase tracking-wider">
{'最近使用'} {t('dashboard_recentlyUsed')}
</h3> </h3>
<div className="flex flex-wrap gap-2"> <div className="flex flex-wrap gap-2">
{recentFeatures.map(({ key, feature }) => { {recentFeatures.map(({ key, feature }) => {
@@ -44,7 +46,7 @@ export default function Index() {
)} )}
> >
<IconComponent className="h-3.5 w-3.5 text-muted-foreground/70" /> <IconComponent className="h-3.5 w-3.5 text-muted-foreground/70" />
{feature!.label} {t(feature!.labelKey)}
</button> </button>
); );
})} })}
@@ -55,7 +57,7 @@ export default function Index() {
{/* 全部工具 — 紧凑 Grid */} {/* 全部工具 — 紧凑 Grid */}
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
<h3 className="text-xs font-semibold text-muted-foreground/80 uppercase tracking-wider"> <h3 className="text-xs font-semibold text-muted-foreground/80 uppercase tracking-wider">
{'全部工具'} {t('dashboard_allTools')}
</h3> </h3>
<div className={cn('grid grid-cols-3 sm:grid-cols-4 md:grid-cols-5 lg:grid-cols-6 gap-2')}> <div className={cn('grid grid-cols-3 sm:grid-cols-4 md:grid-cols-5 lg:grid-cols-6 gap-2')}>
{visibleFeatures.map(({ key, feature }) => { {visibleFeatures.map(({ key, feature }) => {
@@ -80,7 +82,7 @@ export default function Index() {
)} )}
/> />
<span className="text-[11px] font-medium text-muted-foreground/80 group-hover:text-foreground leading-tight text-center truncate w-full transition-colors"> <span className="text-[11px] font-medium text-muted-foreground/80 group-hover:text-foreground leading-tight text-center truncate w-full transition-colors">
{feature!.label} {t(feature!.labelKey)}
</span> </span>
</button> </button>
); );
@@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { ChevronLeft, ChevronRight } from 'lucide-react'; import { ChevronLeft, ChevronRight } from 'lucide-react';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
export interface DiffNavigatorProps extends React.HTMLAttributes<HTMLDivElement> { export interface DiffNavigatorProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -18,6 +19,8 @@ export default function DiffNavigator({
className, className,
...props ...props
}: DiffNavigatorProps) { }: DiffNavigatorProps) {
const { t } = useI18n('jsonDiff');
const isFirst = currentIndex <= 0; const isFirst = currentIndex <= 0;
const isLast = currentIndex >= total - 1; const isLast = currentIndex >= total - 1;
@@ -30,7 +33,9 @@ export default function DiffNavigator({
)} )}
{...props} {...props}
> >
<span className="text-xs font-semibold text-muted-foreground/90">{'无差异'}</span> <span className="text-xs font-semibold text-muted-foreground/90">
{t('jsonDiff:noDiffs')}
</span>
</div> </div>
); );
} }
@@ -47,7 +52,7 @@ export default function DiffNavigator({
<button <button
type="button" type="button"
disabled={isFirst} disabled={isFirst}
aria-label={'上一个'} aria-label={t('jsonDiff:previousDiff')}
onClick={onPrev} onClick={onPrev}
className={cn( className={cn(
'p-1 rounded-md text-muted-foreground hover:bg-accent hover:text-foreground', 'p-1 rounded-md text-muted-foreground hover:bg-accent hover:text-foreground',
@@ -67,7 +72,7 @@ export default function DiffNavigator({
<button <button
type="button" type="button"
disabled={isLast} disabled={isLast}
aria-label={'下一个'} aria-label={t('jsonDiff:nextDiff')}
onClick={onNext} onClick={onNext}
className={cn( className={cn(
'p-1 rounded-md text-muted-foreground hover:bg-accent hover:text-foreground', 'p-1 rounded-md text-muted-foreground hover:bg-accent hover:text-foreground',
@@ -1,7 +1,8 @@
import React from 'react'; import React from 'react';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import JsonTree from './JsonTree'; import JsonTree from './JsonTree';
import type { DiffNode, DiffResult as DiffResultType, DiffType, ViewMode } from '../types'; import type { DiffNode, DiffResult as DiffResultType, DiffType, ViewMode } from './types';
export interface DiffResultProps extends React.HTMLAttributes<HTMLDivElement> { export interface DiffResultProps extends React.HTMLAttributes<HTMLDivElement> {
result: DiffResultType; result: DiffResultType;
@@ -16,6 +17,8 @@ export default function DiffResult({
className, className,
...props ...props
}: DiffResultProps) { }: DiffResultProps) {
const { t } = useI18n('jsonDiff');
if (viewMode === 'sideBySide') { if (viewMode === 'sideBySide') {
return ( return (
<div <div
@@ -23,11 +26,11 @@ export default function DiffResult({
{...props} {...props}
> >
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<SectionLabel text={'原始 JSON'} /> <SectionLabel text={t('jsonDiff:leftLabel')} />
<JsonTree node={result.root} side="left" activePath={activePath} /> <JsonTree node={result.root} side="left" activePath={activePath} />
</div> </div>
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<SectionLabel text={'目标 JSON'} /> <SectionLabel text={t('jsonDiff:rightLabel')} />
<JsonTree node={result.root} side="right" activePath={activePath} /> <JsonTree node={result.root} side="right" activePath={activePath} />
</div> </div>
</div> </div>
@@ -1,31 +1,18 @@
import React, { useEffect, useMemo, useState } from 'react'; import React, { useEffect, useMemo, useState } from 'react';
import { useI18n } from '@/utils/chromeI18n';
import { formatBytes } from '@/utils/format'; import { formatBytes } from '@/utils/format';
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
import TextInputArea from '@/components/TextInputArea'; import TextInputArea from '@/components/TextInputArea';
import { validateJson } from '@/utils/jsonFormatter'; import { validateJson } from '@/utils/jsonFormatter';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import type { ConvertFunction, ConvertResult } from '../types';
const CONVERT_LABELS: Record< export interface ConvertResult {
string, output: string;
{ inputPlaceholder: string; outputLabel: string; emptyHint: string } originalBytes: number;
> = { outputBytes: number;
yaml: { }
inputPlaceholder: '输入需要转换的 JSON...',
outputLabel: 'YAML 结果', export type ConvertFunction = (text: string) => ConvertResult;
emptyHint: '输入 JSON 后点击转换',
},
toml: {
inputPlaceholder: '输入需要转换的 JSON...',
outputLabel: 'TOML 结果',
emptyHint: '输入 JSON 后点击转换',
},
minify: {
inputPlaceholder: '输入需要压缩的 JSON...',
outputLabel: '压缩结果',
emptyHint: '输入 JSON 后点击压缩',
},
};
interface JsonConvertSectionProps extends React.HTMLAttributes<HTMLDivElement> { interface JsonConvertSectionProps extends React.HTMLAttributes<HTMLDivElement> {
translationPrefix: string; translationPrefix: string;
@@ -38,11 +25,12 @@ export default function JsonConvertSection({
className, className,
...props ...props
}: JsonConvertSectionProps) { }: JsonConvertSectionProps) {
const { t } = useI18n('jsonFormat');
const [input, setInput] = useState(''); const [input, setInput] = useState('');
const [debouncedInput, setDebouncedInput] = useState(''); const [debouncedInput, setDebouncedInput] = useState('');
const pk = translationPrefix; const pk = translationPrefix;
const labels = CONVERT_LABELS[pk] || CONVERT_LABELS.yaml;
// Debounce input // Debounce input
useEffect(() => { useEffect(() => {
@@ -84,7 +72,7 @@ export default function JsonConvertSection({
<div className={cn('w-full flex flex-col gap-4', className)} {...props}> <div className={cn('w-full flex flex-col gap-4', className)} {...props}>
{/* 输入区 */} {/* 输入区 */}
<TextInputArea <TextInputArea
placeholder={labels.inputPlaceholder} placeholder={t(`jsonFormat:${pk}InputPlaceholder`)}
value={input} value={input}
onChange={setInput} onChange={setInput}
externalError={error || runtimeError || undefined} externalError={error || runtimeError || undefined}
@@ -101,19 +89,19 @@ export default function JsonConvertSection({
<div className="flex h-9 items-center justify-between px-4 border-b border-border bg-muted/50 select-none"> <div className="flex h-9 items-center justify-between px-4 border-b border-border bg-muted/50 select-none">
<div className="flex gap-4 items-center"> <div className="flex gap-4 items-center">
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90"> <span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90">
{labels.outputLabel} {t(`jsonFormat:${pk}OutputLabel`)}
</span> </span>
<div className="hidden sm:flex gap-3 items-center font-mono text-[10px] text-muted-foreground/70 tabular-nums"> <div className="hidden sm:flex gap-3 items-center font-mono text-[10px] text-muted-foreground/70 tabular-nums">
<span> <span>
{'原始大小'}:{' '} {t('jsonFormat:originalSize')}:{' '}
<span className="font-semibold text-foreground/80"> <span className="font-semibold text-foreground/80">
{formatBytes(result.originalBytes)} {formatBytes(result.originalBytes)}
</span> </span>
</span> </span>
<span className="text-border/60">|</span> <span className="text-border/60">|</span>
<span> <span>
{'格式化后大小'}:{' '} {t('jsonFormat:formattedSize')}:{' '}
<span className="font-semibold text-foreground/80"> <span className="font-semibold text-foreground/80">
{formatBytes(result.outputBytes)} {formatBytes(result.outputBytes)}
</span> </span>
@@ -134,7 +122,7 @@ export default function JsonConvertSection({
) : ( ) : (
<div className="p-8 rounded-xl bg-muted/30 border border-dashed border-border/80 text-center flex flex-col items-center justify-center min-h-[120px] select-none"> <div className="p-8 rounded-xl bg-muted/30 border border-dashed border-border/80 text-center flex flex-col items-center justify-center min-h-[120px] select-none">
<p className="text-xs font-semibold text-muted-foreground/80 tracking-wide max-w-[240px] leading-relaxed"> <p className="text-xs font-semibold text-muted-foreground/80 tracking-wide max-w-[240px] leading-relaxed">
{error ? '请修正上方 JSON 的语法错误以开启实时流式格式化' : labels.emptyHint} {error ? t('jsonFormat:fixErrorHint') : t(`jsonFormat:${pk}EmptyHint`)}
</p> </p>
</div> </div>
)} )}
@@ -1,4 +1,5 @@
import { useEffect, useMemo, useState } from 'react'; import { useEffect, useMemo, useState } from 'react';
import { useI18n } from '@/utils/chromeI18n';
import { import {
formatJson, formatJson,
type JsonFormatOptions, type JsonFormatOptions,
@@ -13,6 +14,8 @@ import { Checkbox } from '@/components/ui/checkbox';
import { Label } from '@/components/ui/label'; import { Label } from '@/components/ui/label';
export default function JsonFormatSection() { export default function JsonFormatSection() {
const { t } = useI18n('jsonFormat');
const [input, setInput] = useState(''); const [input, setInput] = useState('');
const [debouncedInput, setDebouncedInput] = useState(''); const [debouncedInput, setDebouncedInput] = useState('');
const [indentSize, setIndentSize] = useState<number>(2); const [indentSize, setIndentSize] = useState<number>(2);
@@ -63,7 +66,7 @@ export default function JsonFormatSection() {
{/* 缩进配置区 */} {/* 缩进配置区 */}
<div className="flex gap-2 items-center shrink-0 select-none"> <div className="flex gap-2 items-center shrink-0 select-none">
<span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider"> <span className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider">
{'缩进'} {t('jsonFormat:indentSize')}
</span> </span>
<SwitchButtonGroup <SwitchButtonGroup
value={indentSize} value={indentSize}
@@ -91,7 +94,7 @@ export default function JsonFormatSection() {
htmlFor="sort-keys-checkbox" htmlFor="sort-keys-checkbox"
className="text-xs font-bold text-foreground/80 cursor-pointer tracking-tight group-hover:text-foreground" className="text-xs font-bold text-foreground/80 cursor-pointer tracking-tight group-hover:text-foreground"
> >
{'键名排序'} {t('jsonFormat:sortKeys')}
</Label> </Label>
</div> </div>
</div> </div>
@@ -99,7 +102,7 @@ export default function JsonFormatSection() {
{/* 满血版输入终端 */} {/* 满血版输入终端 */}
<TextInputArea <TextInputArea
placeholder={'输入需要格式化的 JSON...'} placeholder={t('jsonFormat:inputPlaceholder')}
value={input} value={input}
onChange={setInput} onChange={setInput}
externalError={error || runtimeError || undefined} externalError={error || runtimeError || undefined}
@@ -117,19 +120,19 @@ export default function JsonFormatSection() {
<div className="flex h-9 items-center justify-between px-4 border-b border-border bg-muted/50 select-none"> <div className="flex h-9 items-center justify-between px-4 border-b border-border bg-muted/50 select-none">
<div className="flex gap-4 items-center"> <div className="flex gap-4 items-center">
<span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90"> <span className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/90">
{'格式化结果'} {t('jsonFormat:outputLabel')}
</span> </span>
<div className="hidden sm:flex gap-3 items-center font-mono text-[10px] text-muted-foreground/70 tabular-nums"> <div className="hidden sm:flex gap-3 items-center font-mono text-[10px] text-muted-foreground/70 tabular-nums">
<span> <span>
{'原始大小'}:{' '} {t('jsonFormat:originalSize')}:{' '}
<span className="font-semibold text-foreground/80"> <span className="font-semibold text-foreground/80">
{formatBytes(result.originalBytes)} {formatBytes(result.originalBytes)}
</span> </span>
</span> </span>
<span className="text-border/60">|</span> <span className="text-border/60">|</span>
<span> <span>
{'格式化后大小'}:{' '} {t('jsonFormat:formattedSize')}:{' '}
<span className="font-semibold text-foreground/80"> <span className="font-semibold text-foreground/80">
{formatBytes(result.formattedBytes)} {formatBytes(result.formattedBytes)}
</span> </span>
@@ -151,7 +154,7 @@ export default function JsonFormatSection() {
/* 空状态指示引导区 */ /* 空状态指示引导区 */
<div className="p-8 rounded-xl bg-muted/30 border border-dashed border-border/80 text-center flex flex-col items-center justify-center min-h-[120px] select-none"> <div className="p-8 rounded-xl bg-muted/30 border border-dashed border-border/80 text-center flex flex-col items-center justify-center min-h-[120px] select-none">
<p className="text-xs font-semibold text-muted-foreground/80 tracking-wide max-w-[240px] leading-relaxed"> <p className="text-xs font-semibold text-muted-foreground/80 tracking-wide max-w-[240px] leading-relaxed">
{error ? '请修正上方 JSON 的语法错误以开启实时流式格式化' : '输入 JSON 后点击格式化'} {error ? t('jsonFormat:fixErrorHint') : t('jsonFormat:emptyHint')}
</p> </p>
</div> </div>
)} )}
@@ -1,6 +1,6 @@
import React, { useEffect, useMemo, useRef, useState } from 'react'; import React, { useEffect, useMemo, useRef, useState } from 'react';
import { ChevronDown, ChevronRight } from 'lucide-react'; // 用正统的矢量箭头平替原生的字符 '▾' '▸' import { ChevronDown, ChevronRight } from 'lucide-react'; // 用正统的矢量箭头平替原生的字符 '▾' '▸'
import type { DiffNode, DiffType } from '../types'; import type { DiffNode, DiffType } from './types';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
export type TreeSide = 'left' | 'right'; export type TreeSide = 'left' | 'right';
@@ -1,11 +0,0 @@
import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import Index from '../index';
describe('JsonTools 页面', () => {
it('应该渲染模式切换按钮', () => {
render(<Index />);
// 基本渲染测试:页面含多个模式切换按钮,应至少渲染一个
expect(screen.getAllByRole('button').length).toBeGreaterThan(0);
});
});
@@ -1,4 +1,4 @@
import type { DiffNode, DiffResult, DiffType } from '@/pages/JsonTools/types'; import type { DiffNode, DiffResult, DiffType } from './types';
const ROOT_PATH = '$'; const ROOT_PATH = '$';
const SENTINEL = Symbol('missing'); const SENTINEL = Symbol('missing');
+19 -19
View File
@@ -1,8 +1,9 @@
import JsonDiffInput from './components/JsonDiffInput'; import { useI18n } from '@/utils/chromeI18n';
import DiffResult from './components/DiffResult'; import JsonDiffInput from './JsonDiffInput';
import DiffNavigator from './components/DiffNavigator'; import DiffResult from './DiffResult';
import JsonFormatSection from './components/JsonFormatSection'; import DiffNavigator from './DiffNavigator';
import JsonConvertSection from './components/JsonConvertSection'; import JsonFormatSection from './JsonFormatSection';
import JsonConvertSection from './JsonConvertSection';
import SwitchButtonGroup from '@/components/SwitchButtonGroup'; import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { useJsonTools } from './useJsonTools'; import { useJsonTools } from './useJsonTools';
import type { JsonToolsPageMode } from '@/types/storage'; import type { JsonToolsPageMode } from '@/types/storage';
@@ -11,6 +12,7 @@ import type { ViewMode } from './types';
type PageMode = JsonToolsPageMode; type PageMode = JsonToolsPageMode;
export default function Index() { export default function Index() {
const { t } = useI18n(['jsonDiff', 'jsonFormat']);
const { const {
pageMode, pageMode,
setPageMode, setPageMode,
@@ -39,11 +41,11 @@ export default function Index() {
value={pageMode} value={pageMode}
onChange={(v: PageMode) => setPageMode(v)} onChange={(v: PageMode) => setPageMode(v)}
options={[ options={[
{ value: 'diff', label: '差异比较' }, { value: 'diff', label: t('jsonFormat:diffMode') },
{ value: 'format', label: '格式化' }, { value: 'format', label: t('jsonFormat:formatMode') },
{ value: 'yaml', label: 'YAML' }, { value: 'yaml', label: t('jsonFormat:yamlMode') },
{ value: 'toml', label: 'TOML' }, { value: 'toml', label: t('jsonFormat:tomlMode') },
{ value: 'minify', label: '压缩' }, { value: 'minify', label: t('jsonFormat:minifyMode') },
]} ]}
size="small" size="small"
className="w-full sm:w-auto" className="w-full sm:w-auto"
@@ -56,8 +58,8 @@ export default function Index() {
value={viewMode} value={viewMode}
onChange={(v: ViewMode) => setViewMode(v)} onChange={(v: ViewMode) => setViewMode(v)}
options={[ options={[
{ value: 'sideBySide', label: '并排' }, { value: 'sideBySide', label: t('jsonDiff:sideBySideMode') },
{ value: 'unified', label: '统一' }, { value: 'unified', label: t('jsonDiff:unifiedMode') },
]} ]}
size="small" size="small"
/> />
@@ -65,16 +67,16 @@ export default function Index() {
<div className="flex flex-col md:flex-row gap-4 w-full items-stretch"> <div className="flex flex-col md:flex-row gap-4 w-full items-stretch">
<JsonDiffInput <JsonDiffInput
label={'原始 JSON'} label={t('jsonDiff:leftLabel')}
placeholder={'输入原始 JSON...'} placeholder={t('jsonDiff:leftPlaceholder')}
value={leftInput} value={leftInput}
onChange={setLeftInput} onChange={setLeftInput}
error={leftError} error={leftError}
minRows={9} minRows={9}
/> />
<JsonDiffInput <JsonDiffInput
label={'目标 JSON'} label={t('jsonDiff:rightLabel')}
placeholder={'输入目标 JSON...'} placeholder={t('jsonDiff:rightPlaceholder')}
value={rightInput} value={rightInput}
onChange={setRightInput} onChange={setRightInput}
error={rightError} error={rightError}
@@ -97,9 +99,7 @@ export default function Index() {
) : ( ) : (
<div className="p-8 rounded-xl bg-muted/30 border border-dashed border-border/80 text-center flex flex-col items-center justify-center min-h-[140px]"> <div className="p-8 rounded-xl bg-muted/30 border border-dashed border-border/80 text-center flex flex-col items-center justify-center min-h-[140px]">
<p className="text-xs font-semibold text-muted-foreground/80 tracking-wide max-w-[260px] leading-relaxed"> <p className="text-xs font-semibold text-muted-foreground/80 tracking-wide max-w-[260px] leading-relaxed">
{leftError || rightError {leftError || rightError ? t('jsonDiff:fixErrorHint') : t('jsonDiff:emptyHint')}
? '请修正上方 JSON 的语法错误以开启实时流式比对'
: '输入两侧 JSON 后点击比较'}
</p> </p>
</div> </div>
)} )}
-10
View File
@@ -53,13 +53,3 @@ export interface DiffResult {
/** 差异核心总计数(等价于 diffPaths.length),注入 tabular-nums 配合渲染 */ /** 差异核心总计数(等价于 diffPaths.length),注入 tabular-nums 配合渲染 */
diffCount: number; diffCount: number;
} }
/** JSON 转换结果 */
export interface ConvertResult {
output: string;
originalBytes: number;
outputBytes: number;
}
/** JSON 转换函数类型 */
export type ConvertFunction = (text: string) => ConvertResult;
+7 -4
View File
@@ -1,12 +1,14 @@
import { useCallback, useEffect, useMemo, useState } from 'react'; import { useCallback, useEffect, useMemo, useState } from 'react';
import { useI18n } from '@/utils/chromeI18n';
import { useStorageState } from '@/utils/useStorageState'; import { useStorageState } from '@/utils/useStorageState';
import { diffJson } from '@/utils/diffEngine'; import { diffJson } from './diffEngine';
import { jsonToYaml } from '@/utils/jsonToYaml'; import { jsonToYaml } from '@/utils/jsonToYaml';
import { jsonToToml } from '@/utils/jsonToToml'; import { jsonToToml } from '@/utils/jsonToToml';
import { minifyJson } from '@/utils/jsonFormatter'; import { minifyJson } from '@/utils/jsonFormatter';
import { isValidPageMode, tryParse } from './constants'; import { isValidPageMode, tryParse } from './constants';
import type { JsonToolsPageMode } from '@/types/storage'; import type { JsonToolsPageMode } from '@/types/storage';
import type { ConvertFunction, ViewMode } from './types'; import type { ConvertFunction } from './JsonConvertSection';
import type { ViewMode } from './types';
export interface UseJsonToolsReturn { export interface UseJsonToolsReturn {
pageMode: JsonToolsPageMode; pageMode: JsonToolsPageMode;
@@ -33,6 +35,7 @@ export interface UseJsonToolsReturn {
} }
export function useJsonTools(): UseJsonToolsReturn { export function useJsonTools(): UseJsonToolsReturn {
const { t } = useI18n(['jsonDiff', 'jsonFormat']);
const [pageMode, setPageMode] = useStorageState('jsonTools/pageMode', 'diff', isValidPageMode); const [pageMode, setPageMode] = useStorageState('jsonTools/pageMode', 'diff', isValidPageMode);
// Diff inputs // Diff inputs
@@ -52,12 +55,12 @@ export function useJsonTools(): UseJsonToolsReturn {
// Parse debounced inputs // Parse debounced inputs
const parseState = useMemo(() => { const parseState = useMemo(() => {
const invalidMsg = '无效的 JSON 格式'; const invalidMsg = t('jsonDiff:invalidJson');
return { return {
left: tryParse(debouncedLeft, invalidMsg), left: tryParse(debouncedLeft, invalidMsg),
right: tryParse(debouncedRight, invalidMsg), right: tryParse(debouncedRight, invalidMsg),
}; };
}, [debouncedLeft, debouncedRight]); }, [debouncedLeft, debouncedRight, t]);
const leftError = parseState.left.error; const leftError = parseState.left.error;
const rightError = parseState.right.error; const rightError = parseState.right.error;
+3 -1
View File
@@ -1,4 +1,5 @@
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
import { useI18n } from '@/utils/chromeI18n';
import { stringifyJson } from '@/utils/jwt'; import { stringifyJson } from '@/utils/jwt';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
@@ -17,6 +18,7 @@ export default function JwtSection({
bgClass, bgClass,
borderClass, borderClass,
}: JwtSectionProps) { }: JwtSectionProps) {
const { t } = useI18n('jwt');
return ( return (
<div className={cn('p-4 rounded-xl border border-solid', bgClass, borderClass)}> <div className={cn('p-4 rounded-xl border border-solid', bgClass, borderClass)}>
<div className="flex justify-between items-center mb-2 select-none"> <div className="flex justify-between items-center mb-2 select-none">
@@ -29,7 +31,7 @@ export default function JwtSection({
/> />
</div> </div>
<pre className="m-0 p-3 bg-muted/30 dark:bg-muted/10 rounded-lg text-xs font-mono overflow-x-auto whitespace-pre-wrap break-all border border-border/50 text-foreground/90 leading-relaxed select-text"> <pre className="m-0 p-3 bg-muted/30 dark:bg-muted/10 rounded-lg text-xs font-mono overflow-x-auto whitespace-pre-wrap break-all border border-border/50 text-foreground/90 leading-relaxed select-text">
{content ? stringifyJson(content) : '无法解析'} {content ? stringifyJson(content) : t('jwt:invalidFormat')}
</pre> </pre>
</div> </div>
); );
+10 -6
View File
@@ -2,8 +2,10 @@ import TextInputArea from '@/components/TextInputArea';
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
import JwtSection from './JwtSection'; import JwtSection from './JwtSection';
import { useJwt } from './useJwt'; import { useJwt } from './useJwt';
import { useI18n } from '@/utils/chromeI18n';
export default function Index() { export default function Index() {
const { t } = useI18n(['jwt', 'jsonFormat']);
const { jwtInput, result, handleChange, handleClear } = useJwt(); const { jwtInput, result, handleChange, handleClear } = useJwt();
return ( return (
@@ -12,7 +14,7 @@ export default function Index() {
<TextInputArea <TextInputArea
minRows={5} minRows={5}
maxRows={10} maxRows={10}
placeholder={'在此粘贴 JWT 令牌 (Encoded JWT)...'} placeholder={t('jwt_placeholder')}
value={jwtInput} value={jwtInput}
onChange={handleChange} onChange={handleChange}
allowCopy={true} allowCopy={true}
@@ -24,7 +26,7 @@ export default function Index() {
{result && !result.error && ( {result && !result.error && (
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
<JwtSection <JwtSection
title={'HEADER: 算法 & 令牌类型'} title={t('jwt:headerTitle')}
content={result.header} content={result.header}
colorClass="text-[#fb015b] dark:text-rose-400" colorClass="text-[#fb015b] dark:text-rose-400"
borderClass="border-[#fb015b]/20 dark:border-rose-500/20" borderClass="border-[#fb015b]/20 dark:border-rose-500/20"
@@ -32,7 +34,7 @@ export default function Index() {
/> />
<JwtSection <JwtSection
title={'PAYLOAD: 数据'} title={t('jwt:payloadTitle')}
content={result.payload} content={result.payload}
colorClass="text-[#a03aff] dark:text-purple-400" colorClass="text-[#a03aff] dark:text-purple-400"
borderClass="border-[#a03aff]/20 dark:border-purple-500/20" borderClass="border-[#a03aff]/20 dark:border-purple-500/20"
@@ -42,7 +44,7 @@ export default function Index() {
<div className="p-4 rounded-xl border border-border bg-secondary/40 shadow-sm"> <div className="p-4 rounded-xl border border-border bg-secondary/40 shadow-sm">
<div className="flex justify-between items-center mb-2"> <div className="flex justify-between items-center mb-2">
<span className="text-xs font-bold tracking-wider text-muted-foreground/90 uppercase"> <span className="text-xs font-bold tracking-wider text-muted-foreground/90 uppercase">
{'签名'} {t('jwt:signatureTitle')}
</span> </span>
<CopyButton <CopyButton
text={result.signature || ''} text={result.signature || ''}
@@ -50,7 +52,7 @@ export default function Index() {
/> />
</div> </div>
<span className="block text-xs font-mono break-all text-foreground/80 bg-muted/30 dark:bg-muted/10 p-3 rounded-lg border border-border/50 leading-relaxed select-text"> <span className="block text-xs font-mono break-all text-foreground/80 bg-muted/30 dark:bg-muted/10 p-3 rounded-lg border border-border/50 leading-relaxed select-text">
{result.signature || '无签名'} {result.signature || t('jwt:noSignature')}
</span> </span>
</div> </div>
</div> </div>
@@ -58,7 +60,9 @@ export default function Index() {
{result?.error && ( {result?.error && (
<div className="p-6 rounded-xl bg-muted/30 border border-dashed border-border text-center"> <div className="p-6 rounded-xl bg-muted/30 border border-dashed border-border text-center">
<p className="text-xs font-semibold text-muted-foreground/80">{'无效的 JSON 格式'}</p> <p className="text-xs font-semibold text-muted-foreground/80">
{t('jsonFormat:invalidJson')}
</p>
</div> </div>
)} )}
</div> </div>
+2 -2
View File
@@ -26,11 +26,11 @@ vi.mock('@/config/features', async (importOriginal) => {
}; };
}); });
vi.mock('../components/QrCodePreview', () => ({ vi.mock('@/components/QrCodePreview', () => ({
default: () => <div data-testid="qr-code-preview">QrCodePreview</div>, default: () => <div data-testid="qr-code-preview">QrCodePreview</div>,
})); }));
vi.mock('../components/ImageUploader', () => ({ vi.mock('@/components/ImageUploader', () => ({
default: () => <div data-testid="image-uploader">ImageUploader</div>, default: () => <div data-testid="image-uploader">ImageUploader</div>,
})); }));
@@ -1,6 +1,7 @@
import { Loader2, Pencil, QrCode } from 'lucide-react'; import { Loader2, Pencil, QrCode } from 'lucide-react';
import TextInputArea from '@/components/TextInputArea'; import TextInputArea from '@/components/TextInputArea';
import QrCodePreview from './QrCodePreview'; import QrCodePreview from '@/components/QrCodePreview';
import { useI18n } from '@/utils/chromeI18n';
import { useQrCodeContext } from '../contexts/QrCodeContext'; import { useQrCodeContext } from '../contexts/QrCodeContext';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label'; import { Label } from '@/components/ui/label';
@@ -10,6 +11,7 @@ import { cn } from '@/lib/utils';
const TEXT_PREVIEW_MAX_LENGTH = 80; const TEXT_PREVIEW_MAX_LENGTH = 80;
export default function GeneratePanel() { export default function GeneratePanel() {
const { t } = useI18n('qrCode');
const { const {
generatorState, generatorState,
setTextToEncode, setTextToEncode,
@@ -40,13 +42,13 @@ export default function GeneratePanel() {
> >
<div className="flex flex-col space-y-2.5"> <div className="flex flex-col space-y-2.5">
<Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5"> <Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5">
{'输入 URL 或文本'} {t('qrCode:urlInputLabel')}
</Label> </Label>
<TextInputArea <TextInputArea
value={generatorState.textToEncode} value={generatorState.textToEncode}
onChange={setTextToEncode} onChange={setTextToEncode}
placeholder={'请输入 URL 或文本内容,将自动生成二维码'} placeholder={t('qrCode:urlInputPlaceholder')}
showCount={true} showCount={true}
showClear={true} showClear={true}
allowCopy={false} allowCopy={false}
@@ -64,12 +66,12 @@ export default function GeneratePanel() {
{generatorState.generating ? ( {generatorState.generating ? (
<> <>
<Loader2 className="w-4 h-4 mr-2 animate-spin" /> <Loader2 className="w-4 h-4 mr-2 animate-spin" />
{'生成中...'} {t('qrCode:generating')}
</> </>
) : ( ) : (
<> <>
<QrCode className="w-4 h-4 mr-2" /> <QrCode className="w-4 h-4 mr-2" />
{'生成二维码'} {t('qrCode:generateButton')}
</> </>
)} )}
</Button> </Button>
@@ -86,11 +88,11 @@ export default function GeneratePanel() {
<div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm p-3 mb-3"> <div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm p-3 mb-3">
<div className="flex items-center justify-between mb-1.5"> <div className="flex items-center justify-between mb-1.5">
<Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5"> <Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5">
{'原始文本'} {t('qrCode:textPreviewLabel')}
</Label> </Label>
<Button variant="ghost" size="sm" onClick={backToEdit} className="h-6 px-2 text-xs"> <Button variant="ghost" size="sm" onClick={backToEdit} className="h-6 px-2 text-xs">
<Pencil className="w-3 h-3 mr-1" /> <Pencil className="w-3 h-3 mr-1" />
{'编辑'} {t('qrCode:editButton')}
</Button> </Button>
</div> </div>
<div <div
+12 -10
View File
@@ -2,13 +2,15 @@ import { useCallback, useEffect } from 'react';
import { RefreshCw } from 'lucide-react'; import { RefreshCw } from 'lucide-react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import TextInputArea from '@/components/TextInputArea'; import TextInputArea from '@/components/TextInputArea';
import ImageUploader from './ImageUploader'; import ImageUploader from '@/components/ImageUploader';
import { useI18n } from '@/utils/chromeI18n';
import { useQrCodeContext } from '../contexts/QrCodeContext'; import { useQrCodeContext } from '../contexts/QrCodeContext';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label'; import { Label } from '@/components/ui/label';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
export default function ParsePanel() { export default function ParsePanel() {
const { t } = useI18n('qrCode');
const { parserState, setParserState, handleFileChange, handleClearFile } = useQrCodeContext(); const { parserState, setParserState, handleFileChange, handleClearFile } = useQrCodeContext();
const hasFile = parserState.selectedFile !== null; const hasFile = parserState.selectedFile !== null;
@@ -25,7 +27,7 @@ export default function ParsePanel() {
const file = items[i].getAsFile(); const file = items[i].getAsFile();
if (file) { if (file) {
handleFileChange(file); handleFileChange(file);
toast.success('图片粘贴成功,正在解析...'); toast.success(t('qrCode:imagePasted'));
} }
return; return;
} }
@@ -39,14 +41,14 @@ export default function ParsePanel() {
const blob = await response.blob(); const blob = await response.blob();
const file = new File([blob], 'pasted-image.png', { type: blob.type }); const file = new File([blob], 'pasted-image.png', { type: blob.type });
handleFileChange(file); handleFileChange(file);
toast.success('图片粘贴成功,正在解析...'); toast.success(t('qrCode:imagePasted'));
} catch (error) { } catch (error) {
console.error('处理 Base64 图片失败:', error); console.error('处理 Base64 图片失败:', error);
toast.error('粘贴图片失败,请重试'); toast.error(t('qrCode:imagePasteError'));
} }
} }
}, },
[handleFileChange], [handleFileChange, t],
); );
useEffect(() => { useEffect(() => {
@@ -80,11 +82,11 @@ export default function ParsePanel() {
<div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm p-3 mb-3"> <div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm p-3 mb-3">
<div className="flex items-center justify-between mb-1.5"> <div className="flex items-center justify-between mb-1.5">
<Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5"> <Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5">
{'已上传图片'} {t('qrCode:uploadedImage')}
</Label> </Label>
<Button variant="ghost" size="sm" onClick={handleClearFile} className="h-6 px-2 text-xs"> <Button variant="ghost" size="sm" onClick={handleClearFile} className="h-6 px-2 text-xs">
<RefreshCw className="w-3 h-3 mr-1" /> <RefreshCw className="w-3 h-3 mr-1" />
{'重新上传'} {t('qrCode:reuploadButton')}
</Button> </Button>
</div> </div>
<div className="flex items-center gap-3 bg-muted/50 rounded-md p-2"> <div className="flex items-center gap-3 bg-muted/50 rounded-md p-2">
@@ -96,7 +98,7 @@ export default function ParsePanel() {
<div className="flex-1 min-w-0"> <div className="flex-1 min-w-0">
<p className="text-sm text-foreground truncate">{parserState.selectedFile?.name}</p> <p className="text-sm text-foreground truncate">{parserState.selectedFile?.name}</p>
{parserState.parsing && ( {parserState.parsing && (
<p className="text-xs text-primary animate-pulse mt-1">{'解析中...'}</p> <p className="text-xs text-primary animate-pulse mt-1">{t('qrCode:parsing')}</p>
)} )}
</div> </div>
</div> </div>
@@ -111,7 +113,7 @@ export default function ParsePanel() {
> >
<div className="flex flex-col space-y-2.5"> <div className="flex flex-col space-y-2.5">
<Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider pl-0.5"> <Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider pl-0.5">
{'解析结果'} {t('qrCode:resultLabel')}
</Label> </Label>
<TextInputArea <TextInputArea
@@ -119,7 +121,7 @@ export default function ParsePanel() {
readOnly={true} readOnly={true}
showClear={false} showClear={false}
allowCopy={true} allowCopy={true}
placeholder={parserState.parsing ? '' : '解析结果将显示在此处'} placeholder={parserState.parsing ? '' : t('qrCode:resultPlaceholder')}
minRows={4} minRows={4}
maxRows={8} maxRows={8}
externalError={parserState.parseError || undefined} externalError={parserState.parseError || undefined}
+35 -23
View File
@@ -3,6 +3,7 @@ import QRious from 'qrious';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { parseQrCodeFromFile } from '@/utils/qrCodeParser'; import { parseQrCodeFromFile } from '@/utils/qrCodeParser';
import { useContextMenuData } from '@/utils/useContextMenuData'; import { useContextMenuData } from '@/utils/useContextMenuData';
import { useI18n } from '@/utils/chromeI18n';
import type { QrCodeContextValue } from '../contexts/QrCodeContext'; import type { QrCodeContextValue } from '../contexts/QrCodeContext';
import type { QrCodeGeneratorState, QrCodeMode, QrCodeParserState } from '../types'; import type { QrCodeGeneratorState, QrCodeMode, QrCodeParserState } from '../types';
@@ -77,6 +78,8 @@ function generateQrCodeDataUrl(text: string): string {
} }
export function useQrCode(): QrCodeContextValue { export function useQrCode(): QrCodeContextValue {
const { t } = useI18n('qrCode');
const [mode, setMode] = useState<QrCodeMode>('generate'); const [mode, setMode] = useState<QrCodeMode>('generate');
const [generatorState, setGeneratorState] = useState<QrCodeGeneratorState>({ const [generatorState, setGeneratorState] = useState<QrCodeGeneratorState>({
@@ -109,7 +112,8 @@ export function useQrCode(): QrCodeContextValue {
}, []); }, []);
/** 自动检测URL并生成二维码 */ /** 自动检测URL并生成二维码 */
const autoGenerateIfUrl = useCallback((text: string) => { const autoGenerateIfUrl = useCallback(
(text: string) => {
if (debounceTimerRef.current) { if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current); clearTimeout(debounceTimerRef.current);
} }
@@ -135,11 +139,13 @@ export function useQrCode(): QrCodeContextValue {
setGeneratorState((prev) => ({ setGeneratorState((prev) => ({
...prev, ...prev,
generating: false, generating: false,
inputError: '生成二维码失败,请重试', inputError: t('qrCode:generateError'),
})); }));
} }
}, DEBOUNCE_DELAY); }, DEBOUNCE_DELAY);
}, []); },
[t],
);
const setTextToEncode = useCallback( const setTextToEncode = useCallback(
(text: string) => { (text: string) => {
@@ -154,8 +160,8 @@ export function useQrCode(): QrCodeContextValue {
const text = generatorState.textToEncode.trim(); const text = generatorState.textToEncode.trim();
if (!text) { if (!text) {
setGeneratorState((prev) => ({ ...prev, inputError: '请输入内容' })); setGeneratorState((prev) => ({ ...prev, inputError: t('qrCode:inputRequired') }));
toast.error('请输入内容'); toast.error(t('qrCode:inputRequired'));
return; return;
} }
@@ -170,9 +176,9 @@ export function useQrCode(): QrCodeContextValue {
setGeneratorState((prev) => ({ setGeneratorState((prev) => ({
...prev, ...prev,
generating: false, generating: false,
inputError: '生成二维码失败,请重试', inputError: t('qrCode:generateError'),
})); }));
toast.error('生成二维码失败,请重试'); toast.error(t('qrCode:generateError'));
return; return;
} }
@@ -184,7 +190,7 @@ export function useQrCode(): QrCodeContextValue {
generating: false, generating: false,
})); }));
}, 0); }, 0);
}, [generatorState.textToEncode]); }, [generatorState.textToEncode, t]);
/** 返回编辑态,保留上次输入内容 */ /** 返回编辑态,保留上次输入内容 */
const backToEdit = useCallback(() => { const backToEdit = useCallback(() => {
@@ -201,7 +207,8 @@ export function useQrCode(): QrCodeContextValue {
}, []); }, []);
/** 从图片URL解析二维码 */ /** 从图片URL解析二维码 */
const parseQrCodeFromUrl = useCallback(async (imageUrl: string) => { const parseQrCodeFromUrl = useCallback(
async (imageUrl: string) => {
try { try {
setParserState((prev) => ({ setParserState((prev) => ({
...prev, ...prev,
@@ -223,21 +230,23 @@ export function useQrCode(): QrCodeContextValue {
if (result.success && result.data) { if (result.success && result.data) {
setParserState((prev) => ({ ...prev, decodedResult: result.data! })); setParserState((prev) => ({ ...prev, decodedResult: result.data! }));
toast.success('二维码解析成功'); toast.success(t('qrCode:parseSuccess'));
} else { } else {
const errorMsg = result.error || '未检测到二维码,请确保图片清晰且包含二维码'; const errorMsg = result.error || t('qrCode:noQrDetected');
setParserState((prev) => ({ ...prev, parseError: errorMsg })); setParserState((prev) => ({ ...prev, parseError: errorMsg }));
toast.error(errorMsg); toast.error(errorMsg);
} }
} catch (error) { } catch (error) {
console.error('解析图片二维码失败:', error); console.error('解析图片二维码失败:', error);
const errorMsg = error instanceof Error ? error.message : '解析二维码失败,请重试'; const errorMsg = error instanceof Error ? error.message : t('qrCode:parseError');
setParserState((prev) => ({ ...prev, parseError: errorMsg })); setParserState((prev) => ({ ...prev, parseError: errorMsg }));
toast.error(errorMsg); toast.error(errorMsg);
} finally { } finally {
setParserState((prev) => ({ ...prev, parsing: false })); setParserState((prev) => ({ ...prev, parsing: false }));
} }
}, []); },
[t],
);
/** 右键菜单传入URL时,自动生成二维码或解析图片 */ /** 右键菜单传入URL时,自动生成二维码或解析图片 */
const handleContextMenuData = useCallback( const handleContextMenuData = useCallback(
@@ -285,7 +294,8 @@ export function useQrCode(): QrCodeContextValue {
useContextMenuData({ featureKey: 'qrCode', onData: handleContextMenuData }); useContextMenuData({ featureKey: 'qrCode', onData: handleContextMenuData });
// 反向活态解析二维码算法 // 反向活态解析二维码算法
const parseQrCode = useCallback(async (file: File) => { const parseQrCode = useCallback(
async (file: File) => {
try { try {
setParserState((prev) => ({ ...prev, parsing: true, parseError: '', decodedResult: '' })); setParserState((prev) => ({ ...prev, parsing: true, parseError: '', decodedResult: '' }));
@@ -293,21 +303,23 @@ export function useQrCode(): QrCodeContextValue {
if (result.success && result.data) { if (result.success && result.data) {
setParserState((prev) => ({ ...prev, decodedResult: result.data! })); setParserState((prev) => ({ ...prev, decodedResult: result.data! }));
toast.success('二维码解析成功'); toast.success(t('qrCode:parseSuccess'));
} else { } else {
const errorMsg = result.error || '未检测到二维码,请确保图片清晰且包含二维码'; const errorMsg = result.error || t('qrCode:noQrDetected');
setParserState((prev) => ({ ...prev, parseError: errorMsg })); setParserState((prev) => ({ ...prev, parseError: errorMsg }));
toast.error(errorMsg); toast.error(errorMsg);
} }
} catch (error) { } catch (error) {
console.error('解析二维码失败:', error); console.error('解析二维码失败:', error);
const errorMsg = error instanceof Error ? error.message : '解析二维码失败,请重试'; const errorMsg = error instanceof Error ? error.message : t('qrCode:parseError');
setParserState((prev) => ({ ...prev, parseError: errorMsg })); setParserState((prev) => ({ ...prev, parseError: errorMsg }));
toast.error(errorMsg); toast.error(errorMsg);
} finally { } finally {
setParserState((prev) => ({ ...prev, parsing: false })); setParserState((prev) => ({ ...prev, parsing: false }));
} }
}, []); },
[t],
);
const downloadQrCode = useCallback(() => { const downloadQrCode = useCallback(() => {
if (!generatorState.qrCodeDataUrl) return; if (!generatorState.qrCodeDataUrl) return;
@@ -316,8 +328,8 @@ export function useQrCode(): QrCodeContextValue {
link.href = generatorState.qrCodeDataUrl; link.href = generatorState.qrCodeDataUrl;
link.download = 'qrcode.png'; link.download = 'qrcode.png';
link.click(); link.click();
toast.success('二维码下载成功'); toast.success(t('qrCode:qrCodeDownloadSuccess'));
}, [generatorState.qrCodeDataUrl]); }, [generatorState.qrCodeDataUrl, t]);
const copyQrCode = useCallback(async () => { const copyQrCode = useCallback(async () => {
if (!generatorState.qrCodeDataUrl) return; if (!generatorState.qrCodeDataUrl) return;
@@ -332,12 +344,12 @@ export function useQrCode(): QrCodeContextValue {
}), }),
]); ]);
toast.success('二维码已复制到剪贴板'); toast.success(t('qrCode:qrCodeCopySuccess'));
} catch (error) { } catch (error) {
console.error('复制二维码失败:', error); console.error('复制二维码失败:', error);
toast.error('复制失败,请重试'); toast.error(t('qrCode:copyError'));
} }
}, [generatorState.qrCodeDataUrl]); }, [generatorState.qrCodeDataUrl, t]);
const handleFileChange = useCallback( const handleFileChange = useCallback(
(file: File) => { (file: File) => {
+4 -2
View File
@@ -1,5 +1,6 @@
import SwitchButtonGroup from '@/components/SwitchButtonGroup'; import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { useI18n } from '@/utils/chromeI18n';
import { QrCodeContext } from './contexts/QrCodeContext'; import { QrCodeContext } from './contexts/QrCodeContext';
import { useQrCode } from './hooks/useQrCode'; import { useQrCode } from './hooks/useQrCode';
import GeneratePanel from './components/GeneratePanel'; import GeneratePanel from './components/GeneratePanel';
@@ -7,12 +8,13 @@ import ParsePanel from './components/ParsePanel';
import type { QrCodeMode } from './types'; import type { QrCodeMode } from './types';
export default function Index() { export default function Index() {
const { t } = useI18n('qrCode');
const qrCode = useQrCode(); const qrCode = useQrCode();
// 模式选项驱动骨架 // 模式选项驱动骨架
const modeOptions = [ const modeOptions = [
{ value: 'generate' as QrCodeMode, label: '文本转二维码' }, { value: 'generate' as QrCodeMode, label: t('qrCode:urlToQr') },
{ value: 'parse' as QrCodeMode, label: '二维码转文本' }, { value: 'parse' as QrCodeMode, label: t('qrCode:qrToUrl') },
]; ];
return ( return (
+22 -9
View File
@@ -113,14 +113,6 @@ Base64 编解码工具,支持文本/文件/图片三种模式。
| `TextMode.tsx` | 文本模式 | | `TextMode.tsx` | 文本模式 |
| `Base64ConverterSection.tsx` | 文件/图片通用转换区域 | | `Base64ConverterSection.tsx` | 文件/图片通用转换区域 |
### MarkdownToHtml/
Markdown 转 HTML 工具,支持分栏/预览/源码三种视图模式。
### HtmlToMarkdown/
HTML 转 Markdown 工具,支持分栏/预览/Markdown 三种视图模式。
### RightClickRestorer/ ### RightClickRestorer/
右键菜单恢复工具,解除网站对右键的限制。 右键菜单恢复工具,解除网站对右键的限制。
@@ -130,11 +122,32 @@ HTML 转 Markdown 工具,支持分栏/预览/Markdown 三种视图模式。
| `index.tsx` | 页面 UI | | `index.tsx` | 页面 UI |
| `useRightClickRestorer.ts` | 业务逻辑 Hook | | `useRightClickRestorer.ts` | 业务逻辑 Hook |
### TestDataGenerator/
测试数据生成器,支持可视化字段配置、规则复用、Web Worker 批量生成和 JSON/CSV 导出。
| 文件/目录 | 用途 |
| ---------------------------- | ------------------------------------------------- |
| `index.tsx` | 主页面,管理字段配置、规则标签页和生成结果 |
| `hooks/useGenerator.ts` | Worker 生命周期与消息通信,暴露生成/取消/清理操作 |
| `components/FieldList.tsx` | 字段列表、拖拽排序、虚拟滚动和规则保存入口 |
| `components/FieldEditor.tsx` | 字段名称、生成器、参数、必填/空值率/唯一性配置 |
| `components/RuleManager.tsx` | 规则搜索、加载、编辑、复制、删除、导入和导出 |
| `components/ExportPanel.tsx` | 生成结果复制和 JSON/CSV 下载 |
约束与注意事项:
- 字段数量最多 40 个(`FieldList.MAX_FIELDS`)。
- 生成数量为 1 ~ 100,000,预设值包含 50、100、1,000、5,000、10,000。
- 大批量生成通过 `src/workers/generator.worker.ts` 执行,避免阻塞 UI;Worker 每 1,000 条或结束时回传进度。
- 规则通过 `src/utils/ruleStorage.ts` 存入 `localStorage`,最多保存 20 条规则。
- 内置生成器位于 `src/lib/generators/`,按个人信息、业务数据、技术数据、基础类型分类。
## 新增页面 ## 新增页面
1.`types/storage.d.ts` 添加 `PageType` 联合类型成员 1.`types/storage.d.ts` 添加 `PageType` 联合类型成员
2.`config/features.tsx``FEATURES` 数组添加配置 2.`config/features.tsx``FEATURES` 数组添加配置
3.`pages/` 创建页面目录(遵循上述结构) 3.`pages/` 创建页面目录(遵循上述结构)
4.`i18n/locales/{zh,en}/features.json` 添加翻译 4.`public/_locales/zh_CN/messages.json` 添加 Chrome i18n 翻译
5. 如需新权限,更新 `wxt.config.ts` 5. 如需新权限,更新 `wxt.config.ts`
6. 添加对应的单元测试 6. 添加对应的单元测试
+13 -11
View File
@@ -3,15 +3,17 @@ import { Label } from '@/components/ui/label';
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import { Shield, ShieldCheck, MousePointerClick, AlertTriangle } from 'lucide-react'; import { Shield, ShieldCheck, MousePointerClick, AlertTriangle } from 'lucide-react';
import { useRightClickRestorer } from './useRightClickRestorer'; import { useRightClickRestorer } from './useRightClickRestorer';
import { useI18n } from '@/utils/chromeI18n';
export default function Index() { export default function Index() {
const { t } = useI18n('rightClickRestorer');
const { domain, isLoading, isUnlocked, isUnsupported, unlock } = useRightClickRestorer(); const { domain, isLoading, isUnlocked, isUnsupported, unlock } = useRightClickRestorer();
if (isLoading) { if (isLoading) {
return ( return (
<div className="flex flex-col items-center justify-center py-12 min-h-[280px] w-full"> <div className="flex flex-col items-center justify-center py-12 min-h-[280px] w-full">
<span className="text-xs text-muted-foreground mt-2 font-medium tracking-wide"> <span className="text-xs text-muted-foreground mt-2 font-medium tracking-wide">
{'正在加载...'} {t('rightClickRestorer:loading')}
</span> </span>
</div> </div>
); );
@@ -22,7 +24,7 @@ export default function Index() {
{/* Current Domain */} {/* Current Domain */}
<div className="w-full rounded-xl border border-border bg-card text-card-foreground shadow-sm overflow-hidden"> <div className="w-full rounded-xl border border-border bg-card text-card-foreground shadow-sm overflow-hidden">
<div className="p-4"> <div className="p-4">
<Label className="text-sm font-medium">{'当前域名'}</Label> <Label className="text-sm font-medium">{t('rightClickRestorer:currentDomain')}</Label>
<div className="mt-2 flex items-center justify-between gap-2"> <div className="mt-2 flex items-center justify-between gap-2">
<code className="text-sm bg-muted px-2 py-1 rounded truncate min-w-0 flex-1"> <code className="text-sm bg-muted px-2 py-1 rounded truncate min-w-0 flex-1">
{domain || '—'} {domain || '—'}
@@ -30,17 +32,17 @@ export default function Index() {
{isUnsupported ? ( {isUnsupported ? (
<Badge variant="destructive" className="gap-1 shrink-0"> <Badge variant="destructive" className="gap-1 shrink-0">
<AlertTriangle className="h-3 w-3" /> <AlertTriangle className="h-3 w-3" />
{'不支持'} {t('rightClickRestorer:unsupported')}
</Badge> </Badge>
) : isUnlocked ? ( ) : isUnlocked ? (
<Badge variant="default" className="gap-1 bg-green-600 hover:bg-green-700 shrink-0"> <Badge variant="default" className="gap-1 bg-green-600 hover:bg-green-700 shrink-0">
<ShieldCheck className="h-3 w-3" /> <ShieldCheck className="h-3 w-3" />
{'已解锁'} {t('rightClickRestorer:statusUnlocked')}
</Badge> </Badge>
) : ( ) : (
<Badge variant="secondary" className="gap-1 shrink-0"> <Badge variant="secondary" className="gap-1 shrink-0">
<Shield className="h-3 w-3" /> <Shield className="h-3 w-3" />
{'未解锁'} {t('rightClickRestorer:statusLocked')}
</Badge> </Badge>
)} )}
</div> </div>
@@ -53,18 +55,16 @@ export default function Index() {
{isUnsupported ? ( {isUnsupported ? (
<> <>
<p className="text-xs text-muted-foreground"> <p className="text-xs text-muted-foreground">
{'当前页面为浏览器内部页面或扩展页面,无法解锁右键功能。请切换到普通网页后重试。'} {t('rightClickRestorer:unsupportedDesc')}
</p> </p>
<Button className="w-full gap-2" disabled variant="secondary"> <Button className="w-full gap-2" disabled variant="secondary">
<AlertTriangle className="h-4 w-4" /> <AlertTriangle className="h-4 w-4" />
{'不支持'} {t('rightClickRestorer:unsupported')}
</Button> </Button>
</> </>
) : ( ) : (
<> <>
<p className="text-xs text-muted-foreground"> <p className="text-xs text-muted-foreground">{t('rightClickRestorer:unlockDesc')}</p>
{'点击下方按钮,为当前网站临时解锁右键菜单。刷新页面后需要重新解锁。'}
</p>
<Button <Button
className="w-full gap-2" className="w-full gap-2"
onClick={() => void unlock()} onClick={() => void unlock()}
@@ -72,7 +72,9 @@ export default function Index() {
variant={isUnlocked ? 'secondary' : 'default'} variant={isUnlocked ? 'secondary' : 'default'}
> >
<MousePointerClick className="h-4 w-4" /> <MousePointerClick className="h-4 w-4" />
{isUnlocked ? '右键已解锁' : '解锁当前网站右键'} {isUnlocked
? t('rightClickRestorer:alreadyUnlocked')
: t('rightClickRestorer:unlockBtn')}
</Button> </Button>
</> </>
)} )}
@@ -14,6 +14,7 @@
*/ */
import React from 'react'; import React from 'react';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { Switch } from '@/components/ui/switch'; import { Switch } from '@/components/ui/switch';
import { Label } from '@/components/ui/label'; import { Label } from '@/components/ui/label';
@@ -42,6 +43,8 @@ export default function AutoRefreshToggle({
className, className,
...props ...props
}: AutoRefreshToggleProps) { }: AutoRefreshToggleProps) {
const { t } = useI18n('storageCleaner');
return ( return (
<div <div
className={cn( className={cn(
@@ -54,7 +57,7 @@ export default function AutoRefreshToggle({
htmlFor="auto-refresh-switch" htmlFor="auto-refresh-switch"
className="text-xs font-bold text-muted-foreground/90 cursor-pointer select-none tracking-wide uppercase" className="text-xs font-bold text-muted-foreground/90 cursor-pointer select-none tracking-wide uppercase"
> >
{'清理后自动刷新页面'} {t('storageCleaner:autoRefresh')}
</Label> </Label>
<Switch id="auto-refresh-switch" checked={reloadAfterClean} onCheckedChange={onChange} /> <Switch id="auto-refresh-switch" checked={reloadAfterClean} onCheckedChange={onChange} />
@@ -2,6 +2,7 @@ import React from 'react';
import { CheckCircle, XCircle } from 'lucide-react'; import { CheckCircle, XCircle } from 'lucide-react';
import type { CleaningResult as CleaningResultType } from '@/types/storage'; import type { CleaningResult as CleaningResultType } from '@/types/storage';
import { formatCleaningResult } from '@/utils/storageCleaner'; import { formatCleaningResult } from '@/utils/storageCleaner';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
interface CleaningResultProps extends React.HTMLAttributes<HTMLDivElement> { interface CleaningResultProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -9,6 +10,8 @@ interface CleaningResultProps extends React.HTMLAttributes<HTMLDivElement> {
} }
export default function CleaningResult({ result, className, ...props }: CleaningResultProps) { export default function CleaningResult({ result, className, ...props }: CleaningResultProps) {
const { t } = useI18n('storageCleaner');
if (!result) return null; if (!result) return null;
const isSuccess = result.overallSuccess; const isSuccess = result.overallSuccess;
@@ -30,7 +33,9 @@ export default function CleaningResult({ result, className, ...props }: Cleaning
)} )}
<span className="text-xs sm:text-sm font-semibold leading-relaxed break-all"> <span className="text-xs sm:text-sm font-semibold leading-relaxed break-all">
{isSuccess ? formatCleaningResult(result) : result.error || '部分清理失败'} {isSuccess
? formatCleaningResult(result, t)
: result.error || t('storageCleaner:partialFailure')}
</span> </span>
</div> </div>
</div> </div>
@@ -1,4 +1,5 @@
import { AlertCircle } from 'lucide-react'; import { AlertCircle } from 'lucide-react';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
interface ErrorDisplayProps extends React.HTMLAttributes<HTMLDivElement> { interface ErrorDisplayProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -6,6 +7,8 @@ interface ErrorDisplayProps extends React.HTMLAttributes<HTMLDivElement> {
} }
export default function ErrorDisplay({ error, className, ...props }: ErrorDisplayProps) { export default function ErrorDisplay({ error, className, ...props }: ErrorDisplayProps) {
const { t } = useI18n('storageCleaner');
return ( return (
<div <div
className={cn( className={cn(
@@ -24,7 +27,7 @@ export default function ErrorDisplay({ error, className, ...props }: ErrorDispla
</p> </p>
<p className="text-xs font-medium leading-relaxed text-muted-foreground/90 px-2"> <p className="text-xs font-medium leading-relaxed text-muted-foreground/90 px-2">
{'存储清理功能仅适用于标准网页'} {t('storageCleaner:errorStandardOnly')}
</p> </p>
</div> </div>
</div> </div>
@@ -1,18 +1,10 @@
import React from 'react'; import React from 'react';
import { formatBytes } from '@/utils/format'; import { formatBytes } from '@/utils/format';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import type { StorageSizeInfo } from '../useStorageCleaner'; import type { StorageSizeInfo } from './useStorageCleaner';
import { Checkbox } from '@/components/ui/checkbox'; import { Checkbox } from '@/components/ui/checkbox';
const OPTION_LABELS: Record<string, string> = {
localStorage: 'Local Storage',
sessionStorage: 'Session Storage',
indexedDB: '站点存储',
cookies: 'Cookies',
cacheStorage: 'Cache Storage',
serviceWorkers: 'Service Workers',
};
interface OptionItemProps extends React.HTMLAttributes<HTMLDivElement> { interface OptionItemProps extends React.HTMLAttributes<HTMLDivElement> {
labelKey: string; labelKey: string;
checked: boolean; checked: boolean;
@@ -28,9 +20,10 @@ export default function OptionItem({
className, className,
...props ...props
}: OptionItemProps) { }: OptionItemProps) {
const { t } = useI18n('storageCleaner');
const sizeValue = sizeInfo?.value; const sizeValue = sizeInfo?.value;
const isCount = sizeInfo?.displayType === 'count'; const isCount = sizeInfo?.displayType === 'count';
const label = OPTION_LABELS[labelKey] || labelKey;
return ( return (
<div <div
@@ -51,7 +44,7 @@ export default function OptionItem({
checked ? 'text-foreground' : 'text-foreground/75', checked ? 'text-foreground' : 'text-foreground/75',
)} )}
> >
{label} {t(labelKey)}
</span> </span>
{sizeValue !== undefined && sizeValue > 0 ? ( {sizeValue !== undefined && sizeValue > 0 ? (
@@ -61,11 +54,11 @@ export default function OptionItem({
checked ? 'text-primary/70' : 'text-muted-foreground/70', checked ? 'text-primary/70' : 'text-muted-foreground/70',
)} )}
> >
{isCount ? `${sizeValue} ` : formatBytes(sizeValue)} {isCount ? `${sizeValue} ${t('storageCleaner:countUnit')}` : formatBytes(sizeValue)}
</span> </span>
) : ( ) : (
<span className="block text-[10px] font-medium text-muted-foreground/50 mt-0.5 italic"> <span className="block text-[10px] font-medium text-muted-foreground/50 mt-0.5 italic">
{'无数据'} {t('storageCleaner:noData')}
</span> </span>
)} )}
</div> </div>
@@ -10,6 +10,7 @@ import {
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import { AlertTriangle } from 'lucide-react'; import { AlertTriangle } from 'lucide-react';
import type { StorageCleanerOptions } from '@/types/storage'; import type { StorageCleanerOptions } from '@/types/storage';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
export interface StorageCleanerConfirmProps { export interface StorageCleanerConfirmProps {
@@ -19,24 +20,17 @@ export interface StorageCleanerConfirmProps {
options: StorageCleanerOptions; options: StorageCleanerOptions;
} }
const OPTION_LABELS: Record<string, string> = {
localStorage: 'Local Storage',
sessionStorage: 'Session Storage',
indexedDB: '站点存储',
cookies: 'Cookies',
cacheStorage: 'Cache Storage',
serviceWorkers: 'Service Workers',
};
export function StorageCleanerConfirm({ export function StorageCleanerConfirm({
open, open,
onClose, onClose,
onConfirm, onConfirm,
options, options,
}: StorageCleanerConfirmProps) { }: StorageCleanerConfirmProps) {
const { t } = useI18n('storageCleaner');
const selectedOptions = Object.entries(options) const selectedOptions = Object.entries(options)
.filter(([_, value]) => value) .filter(([_, value]) => value)
.map(([key, _]) => OPTION_LABELS[key] || key); .map(([key, _]) => t(`storageCleaner:options.${key as keyof StorageCleanerOptions}`));
return ( return (
<Dialog open={open} onOpenChange={(isOpen) => !isOpen && onClose()}> <Dialog open={open} onOpenChange={(isOpen) => !isOpen && onClose()}>
@@ -48,14 +42,14 @@ export function StorageCleanerConfirm({
{/* 头部标题区域 */} {/* 头部标题区域 */}
<DialogHeader className="pt-1"> <DialogHeader className="pt-1">
<DialogTitle className="text-center text-lg font-bold tracking-tight text-foreground"> <DialogTitle className="text-center text-lg font-bold tracking-tight text-foreground">
{'确认清理数据?'} {t('storageCleaner:confirmTitle')}
</DialogTitle> </DialogTitle>
</DialogHeader> </DialogHeader>
{/* 内容主体:限制最大宽度,防止内部元素在大分辨率下被横向拉得太松散 */} {/* 内容主体:限制最大宽度,防止内部元素在大分辨率下被横向拉得太松散 */}
<div className="text-center py-4 flex flex-col items-center w-full max-w-[280px] mx-auto"> <div className="text-center py-4 flex flex-col items-center w-full max-w-[280px] mx-auto">
<DialogDescription className="mb-4 text-xs font-medium text-muted-foreground/90 leading-relaxed"> <DialogDescription className="mb-4 text-xs font-medium text-muted-foreground/90 leading-relaxed">
{'您将永久删除当前页面的以下选定存储项。'} {t('storageCleaner:confirmDesc')}
</DialogDescription> </DialogDescription>
{/* 待清理项目徽章群 */} {/* 待清理项目徽章群 */}
@@ -75,7 +69,7 @@ export function StorageCleanerConfirm({
<div className="inline-flex items-center justify-center gap-1.5 px-3.5 py-2 rounded-lg bg-destructive/5 border border-dashed border-destructive/20 w-full max-w-[240px]"> <div className="inline-flex items-center justify-center gap-1.5 px-3.5 py-2 rounded-lg bg-destructive/5 border border-dashed border-destructive/20 w-full max-w-[240px]">
<AlertTriangle className="h-3.5 w-3.5 text-destructive shrink-0" /> <AlertTriangle className="h-3.5 w-3.5 text-destructive shrink-0" />
<span className="text-[11px] font-bold text-destructive leading-none tracking-tight"> <span className="text-[11px] font-bold text-destructive leading-none tracking-tight">
{'此操作不可撤销'} {t('storageCleaner:irreversible')}
</span> </span>
</div> </div>
</div> </div>
@@ -87,7 +81,7 @@ export function StorageCleanerConfirm({
onClick={onConfirm} onClick={onConfirm}
className="w-full text-xs font-bold shadow-sm h-9" className="w-full text-xs font-bold shadow-sm h-9"
> >
{'确认清理'} {t('storageCleaner:confirmAction')}
</Button> </Button>
<Button <Button
variant="outline" variant="outline"
@@ -95,7 +89,7 @@ export function StorageCleanerConfirm({
onClick={onClose} onClick={onClose}
className="w-full text-xs font-semibold shadow-sm h-9 text-muted-foreground hover:text-foreground" className="w-full text-xs font-semibold shadow-sm h-9 text-muted-foreground hover:text-foreground"
> >
{'取消'} {t('common_buttons_cancel')}
</Button> </Button>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>
@@ -1,7 +1,8 @@
import React from 'react'; import React from 'react';
import type { StorageCleanerOptions } from '@/types/storage'; import type { StorageCleanerOptions } from '@/types/storage';
import type { StorageSizeInfo } from '../useStorageCleaner'; import type { StorageSizeInfo } from './useStorageCleaner';
import OptionItem from './OptionItem'; import OptionItem from './OptionItem';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { Checkbox } from '@/components/ui/checkbox'; import { Checkbox } from '@/components/ui/checkbox';
import { Label } from '@/components/ui/label'; import { Label } from '@/components/ui/label';
@@ -25,6 +26,8 @@ export default function StorageOptionsGrid({
className, className,
...props ...props
}: StorageOptionsGridProps) { }: StorageOptionsGridProps) {
const { t } = useI18n('storageCleaner');
const optionKeys: (keyof StorageCleanerOptions)[] = [ const optionKeys: (keyof StorageCleanerOptions)[] = [
'localStorage', 'localStorage',
'sessionStorage', 'sessionStorage',
@@ -45,7 +48,7 @@ export default function StorageOptionsGrid({
{optionKeys.map((key) => ( {optionKeys.map((key) => (
<OptionItem <OptionItem
key={key} key={key}
labelKey={key} labelKey={`storageCleaner:options.${key}`}
checked={options[key]} checked={options[key]}
sizeInfo={sizes[key]} sizeInfo={sizes[key]}
onChange={() => onOptionChange(key)} onChange={() => onOptionChange(key)}
@@ -59,7 +62,7 @@ export default function StorageOptionsGrid({
className="border-t border-border flex justify-between items-center pl-3.5 pr-7 py-2.5 bg-muted/20 hover:bg-muted/40 cursor-pointer select-none transition-colors" className="border-t border-border flex justify-between items-center pl-3.5 pr-7 py-2.5 bg-muted/20 hover:bg-muted/40 cursor-pointer select-none transition-colors"
> >
<Label className="text-xs font-bold text-muted-foreground/90 cursor-pointer tracking-wide uppercase"> <Label className="text-xs font-bold text-muted-foreground/90 cursor-pointer tracking-wide uppercase">
{'全选所有项'} {t('storageCleaner:selectAll')}
</Label> </Label>
<Checkbox <Checkbox
@@ -1,32 +0,0 @@
import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import Index from '../index';
// Mock the chrome APIs
vi.mock('@/utils/chromeStorage', () => ({
storageUtil: {
get: vi.fn().mockResolvedValue(null),
set: vi.fn().mockResolvedValue(undefined),
},
}));
vi.mock('@/utils/storageCleaner', () => ({
getCurrentTab: vi.fn().mockResolvedValue({ id: 1, url: 'https://example.com' }),
isRestrictedUrl: vi.fn().mockReturnValue(false),
getCookieSize: vi.fn().mockResolvedValue(0),
getLocalStorageSize: vi.fn().mockResolvedValue(0),
getSessionStorageSize: vi.fn().mockResolvedValue(0),
getOriginStorageEstimate: vi.fn().mockResolvedValue(0),
getCacheStorageSize: vi.fn().mockResolvedValue(0),
getServiceWorkerCount: vi.fn().mockResolvedValue(0),
clearStorage: vi.fn().mockResolvedValue({ overallSuccess: true }),
formatCleaningResult: vi.fn().mockReturnValue('Cleaned successfully'),
}));
describe('StorageCleaner 页面', () => {
it('应该渲染初始化加载状态', () => {
// storageCleaner:initializing 的中文文案为「正在读取站点数据...」
render(<Index />);
expect(screen.getByText(/正在读取站点数据/)).toBeInTheDocument();
});
});
+11 -8
View File
@@ -1,13 +1,16 @@
import { Loader2 } from 'lucide-react'; import { Loader2 } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import StorageCleanerConfirm from './components/StorageCleanerConfirm'; import StorageCleanerConfirm from './StorageCleanerConfirm';
import { useStorageCleaner } from './useStorageCleaner'; import { useStorageCleaner } from './useStorageCleaner';
import StorageOptionsGrid from './components/StorageOptionsGrid'; import StorageOptionsGrid from './StorageOptionsGrid';
import AutoRefreshToggle from './components/AutoRefreshToggle'; import AutoRefreshToggle from './AutoRefreshToggle';
import ErrorDisplay from './components/ErrorDisplay'; import ErrorDisplay from './ErrorDisplay';
import CleaningResult from './components/CleaningResult'; import CleaningResult from './CleaningResult';
import { useI18n } from '@/utils/chromeI18n';
export default function Index() { export default function Index() {
const { t } = useI18n('storageCleaner');
const { const {
error, error,
isInitializing, isInitializing,
@@ -33,7 +36,7 @@ export default function Index() {
<div className="flex flex-col items-center justify-center py-12 min-h-[280px] w-full"> <div className="flex flex-col items-center justify-center py-12 min-h-[280px] w-full">
<Loader2 className="h-6 w-6 text-muted-foreground/80" /> <Loader2 className="h-6 w-6 text-muted-foreground/80" />
<span className="text-xs text-muted-foreground mt-2 font-medium tracking-wide"> <span className="text-xs text-muted-foreground mt-2 font-medium tracking-wide">
{'正在读取站点数据...'} {t('storageCleaner:initializing')}
</span> </span>
</div> </div>
); );
@@ -72,10 +75,10 @@ export default function Index() {
{loading ? ( {loading ? (
<> <>
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> <Loader2 className="mr-2 h-4 w-4 animate-spin" />
{'正在清理...'} {t('storageCleaner:cleaning')}
</> </>
) : ( ) : (
'立即清理' t('storageCleaner:cleanNow')
)} )}
</Button> </Button>
</div> </div>
+13 -8
View File
@@ -16,6 +16,7 @@ import {
getSessionStorageSize, getSessionStorageSize,
isRestrictedUrl, isRestrictedUrl,
} from '@/utils/storageCleaner'; } from '@/utils/storageCleaner';
import { useI18n } from '@/utils/chromeI18n';
import { toast } from 'sonner'; import { toast } from 'sonner';
const DEFAULT_OPTIONS: StorageCleanerOptions = { const DEFAULT_OPTIONS: StorageCleanerOptions = {
@@ -58,6 +59,7 @@ export interface UseStorageCleanerReturn {
} }
export function useStorageCleaner(): UseStorageCleanerReturn { export function useStorageCleaner(): UseStorageCleanerReturn {
const { t } = useI18n(['storageCleaner', 'common']);
const [error, setError] = useState<string>(''); const [error, setError] = useState<string>('');
const [isInitializing, setIsInitializing] = useState<boolean>(true); const [isInitializing, setIsInitializing] = useState<boolean>(true);
const [options, setOptions] = useState<StorageCleanerOptions>(DEFAULT_OPTIONS); const [options, setOptions] = useState<StorageCleanerOptions>(DEFAULT_OPTIONS);
@@ -91,11 +93,11 @@ export function useStorageCleaner(): UseStorageCleanerReturn {
if (currentRequestId !== requestIdRef.current) return; if (currentRequestId !== requestIdRef.current) return;
if (!tab || !tab.url) { if (!tab || !tab.url) {
setError('无法获取当前标签页'); setError(t('storageCleaner:errorNoTab'));
return; return;
} }
if (isRestrictedUrl(tab.url)) { if (isRestrictedUrl(tab.url)) {
setError('存储清理功能不支持此页面'); setError(t('storageCleaner:errorRestricted'));
return; return;
} }
@@ -133,7 +135,7 @@ export function useStorageCleaner(): UseStorageCleanerReturn {
setIsInitializing(false); setIsInitializing(false);
} }
} }
}, []); }, [t]);
const loadInfoRef = useRef(loadInfo); const loadInfoRef = useRef(loadInfo);
useEffect(() => { useEffect(() => {
@@ -175,7 +177,10 @@ export function useStorageCleaner(): UseStorageCleanerReturn {
if (storageTimerRef.current) clearTimeout(storageTimerRef.current); if (storageTimerRef.current) clearTimeout(storageTimerRef.current);
storageTimerRef.current = setTimeout(async () => { storageTimerRef.current = setTimeout(async () => {
await storageUtil await storageUtil
.set('storageCleaner/preferences', { reloadAfterClean, selectedTypes: options }) .set('storageCleaner/preferences', {
reloadAfterClean,
selectedTypes: options,
})
.catch(console.error); .catch(console.error);
}, 500); }, 500);
}, [options, reloadAfterClean, isInitializing]); }, [options, reloadAfterClean, isInitializing]);
@@ -204,7 +209,7 @@ export function useStorageCleaner(): UseStorageCleanerReturn {
const tab = await getCurrentTab(); const tab = await getCurrentTab();
if (!tab || !tab.id || !tab.url) { if (!tab || !tab.id || !tab.url) {
toast.warning('无法获取当前标签页'); toast.warning(t('storageCleaner:errorNoTab'));
return; return;
} }
@@ -214,18 +219,18 @@ export function useStorageCleaner(): UseStorageCleanerReturn {
setResult(cleaningResult); setResult(cleaningResult);
if (reloadAfterClean && cleaningResult.overallSuccess) { if (reloadAfterClean && cleaningResult.overallSuccess) {
toast.success('清理成功,即将刷新页面'); toast.success(t('storageCleaner:cleanSuccessReload'));
await chrome.tabs.reload(tab.id); await chrome.tabs.reload(tab.id);
} else { } else {
await loadInfo(); await loadInfo();
} }
} catch (err) { } catch (err) {
toast.error(`清理失败: ${String(err)}`); toast.error(`${t('storageCleaner:cleanError')}: ${String(err)}`);
} finally { } finally {
setLoading(false); setLoading(false);
setShowConfirm(false); setShowConfirm(false);
} }
}, [options, reloadAfterClean, loadInfo]); }, [options, reloadAfterClean, loadInfo, t]);
const totalBytes = useMemo(() => { const totalBytes = useMemo(() => {
return Object.values(sizes).reduce((acc, s) => { return Object.values(sizes).reduce((acc, s) => {
@@ -5,6 +5,7 @@
import { useMemo } from 'react'; import { useMemo } from 'react';
import { FileJson, FileText } from 'lucide-react'; import { FileJson, FileText } from 'lucide-react';
import { useI18n } from '@/utils/chromeI18n';
import { getGeneratorById } from '@/lib/generators'; import { getGeneratorById } from '@/lib/generators';
import type { FieldConfig } from '@/types/testDataGenerator'; import type { FieldConfig } from '@/types/testDataGenerator';
@@ -55,6 +56,8 @@ function getValueColor(value: string): string {
} }
export default function DataPreview({ fields }: DataPreviewProps) { export default function DataPreview({ fields }: DataPreviewProps) {
const { t } = useI18n('testDataGenerator');
// 生成一条示例数据 // 生成一条示例数据
const sampleData = useMemo(() => { const sampleData = useMemo(() => {
if (fields.length === 0) return null; if (fields.length === 0) return null;
@@ -80,7 +83,7 @@ export default function DataPreview({ fields }: DataPreviewProps) {
return ( return (
<div className="flex flex-col items-center justify-center h-full text-center"> <div className="flex flex-col items-center justify-center h-full text-center">
<FileJson className="h-8 w-8 text-muted-foreground/30 mb-2" /> <FileJson className="h-8 w-8 text-muted-foreground/30 mb-2" />
<p className="text-xs text-muted-foreground/50">{'配置字段后点击「生成数据」按钮'}</p> <p className="text-xs text-muted-foreground/50">{t('testDataGenerator_noDataHint')}</p>
</div> </div>
); );
} }
@@ -93,10 +96,12 @@ export default function DataPreview({ fields }: DataPreviewProps) {
<div className="h-5 w-5 rounded bg-primary/10 flex items-center justify-center"> <div className="h-5 w-5 rounded bg-primary/10 flex items-center justify-center">
<FileText className="h-3 w-3 text-primary" /> <FileText className="h-3 w-3 text-primary" />
</div> </div>
<span className="text-xs font-medium text-muted-foreground">{'示例数据'}</span> <span className="text-xs font-medium text-muted-foreground">
{t('testDataGenerator_sampleData')}
</span>
</div> </div>
<span className="text-[10px] text-muted-foreground/50 bg-muted px-1.5 py-0.5 rounded"> <span className="text-[10px] text-muted-foreground/50 bg-muted px-1.5 py-0.5 rounded">
{Object.keys(sampleData).length} {'字段'} {Object.keys(sampleData).length} {t('testDataGenerator_fields')}
</span> </span>
</div> </div>
@@ -6,6 +6,7 @@
import { Copy, Download } from 'lucide-react'; import { Copy, Download } from 'lucide-react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { useI18n } from '@/utils/chromeI18n';
import { DataExporter } from '@/utils/dataExporter'; import { DataExporter } from '@/utils/dataExporter';
import type { GenerateResult } from '@/types/testDataGenerator'; import type { GenerateResult } from '@/types/testDataGenerator';
@@ -14,6 +15,8 @@ interface ExportPanelProps {
} }
export default function ExportPanel({ result }: ExportPanelProps) { export default function ExportPanel({ result }: ExportPanelProps) {
const { t } = useI18n('testDataGenerator');
if (!result?.data || result.data.length === 0) { if (!result?.data || result.data.length === 0) {
return null; return null;
} }
@@ -22,9 +25,9 @@ export default function ExportPanel({ result }: ExportPanelProps) {
const content = DataExporter.toJSON(result.data!); const content = DataExporter.toJSON(result.data!);
const success = await DataExporter.copyToClipboard(content); const success = await DataExporter.copyToClipboard(content);
if (success) { if (success) {
toast.success('已复制到剪贴板'); toast.success(t('testDataGenerator_copySuccess'));
} else { } else {
toast.error('复制失败'); toast.error(t('testDataGenerator_copyFailed'));
} }
}; };
@@ -32,9 +35,9 @@ export default function ExportPanel({ result }: ExportPanelProps) {
const content = DataExporter.toCSV(result.data!); const content = DataExporter.toCSV(result.data!);
const success = await DataExporter.copyToClipboard(content); const success = await DataExporter.copyToClipboard(content);
if (success) { if (success) {
toast.success('已复制到剪贴板'); toast.success(t('testDataGenerator_copySuccess'));
} else { } else {
toast.error('复制失败'); toast.error(t('testDataGenerator_copyFailed'));
} }
}; };
@@ -50,27 +53,27 @@ export default function ExportPanel({ result }: ExportPanelProps) {
return ( return (
<div className="space-y-2"> <div className="space-y-2">
<h4 className="text-sm font-medium text-foreground">{'导出数据'}</h4> <h4 className="text-sm font-medium text-foreground">{t('testDataGenerator_export')}</h4>
<div className="grid grid-cols-2 gap-2"> <div className="grid grid-cols-2 gap-2">
<Button variant="outline" size="sm" onClick={handleCopyJSON} className="h-9 gap-1.5"> <Button variant="outline" size="sm" onClick={handleCopyJSON} className="h-9 gap-1.5">
<Copy className="h-4 w-4" /> <Copy className="h-4 w-4" />
{'复制 JSON'} {t('testDataGenerator_copyJSON')}
</Button> </Button>
<Button variant="outline" size="sm" onClick={handleCopyCSV} className="h-9 gap-1.5"> <Button variant="outline" size="sm" onClick={handleCopyCSV} className="h-9 gap-1.5">
<Copy className="h-4 w-4" /> <Copy className="h-4 w-4" />
{'复制 CSV'} {t('testDataGenerator_copyCSV')}
</Button> </Button>
<Button variant="outline" size="sm" onClick={handleDownloadJSON} className="h-9 gap-1.5"> <Button variant="outline" size="sm" onClick={handleDownloadJSON} className="h-9 gap-1.5">
<Download className="h-4 w-4" /> <Download className="h-4 w-4" />
{'下载 JSON'} {t('testDataGenerator_downloadJSON')}
</Button> </Button>
<Button variant="outline" size="sm" onClick={handleDownloadCSV} className="h-9 gap-1.5"> <Button variant="outline" size="sm" onClick={handleDownloadCSV} className="h-9 gap-1.5">
<Download className="h-4 w-4" /> <Download className="h-4 w-4" />
{'下载 CSV'} {t('testDataGenerator_downloadCSV')}
</Button> </Button>
</div> </div>
</div> </div>
@@ -4,8 +4,8 @@
*/ */
import { useState, useCallback } from 'react'; import { useState, useCallback } from 'react';
import { useI18n } from '@/utils/chromeI18n';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch'; import { Switch } from '@/components/ui/switch';
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import { getGeneratorById } from '@/lib/generators'; import { getGeneratorById } from '@/lib/generators';
@@ -21,6 +21,7 @@ interface FieldEditorProps {
} }
export default function FieldEditor({ field, onChange, allFieldNames = [] }: FieldEditorProps) { export default function FieldEditor({ field, onChange, allFieldNames = [] }: FieldEditorProps) {
const { t } = useI18n('testDataGenerator');
const generator = getGeneratorById(field.generatorId); const generator = getGeneratorById(field.generatorId);
const [nameError, setNameError] = useState<string | null>(null); const [nameError, setNameError] = useState<string | null>(null);
@@ -28,20 +29,20 @@ export default function FieldEditor({ field, onChange, allFieldNames = [] }: Fie
(name: string): string | null => { (name: string): string | null => {
const trimmed = name.trim(); const trimmed = name.trim();
if (!trimmed) { if (!trimmed) {
return '字段名称不能为空'; return t('testDataGenerator_fieldNameEmpty');
} }
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(trimmed)) { if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(trimmed)) {
return '字段名称只能包含字母、数字和下划线'; return t('testDataGenerator_fieldNameInvalid');
} }
const isDuplicate = allFieldNames.some( const isDuplicate = allFieldNames.some(
(n, i) => n === trimmed && i !== allFieldNames.indexOf(field.name), (n, i) => n === trimmed && i !== allFieldNames.indexOf(field.name),
); );
if (isDuplicate) { if (isDuplicate) {
return '字段名称已存在'; return t('testDataGenerator_fieldNameDuplicate');
} }
return null; return null;
}, },
[allFieldNames, field.name], [allFieldNames, field.name, t],
); );
const handleNameChange = (name: string) => { const handleNameChange = (name: string) => {
@@ -109,13 +110,15 @@ export default function FieldEditor({ field, onChange, allFieldNames = [] }: Fie
<div className="space-y-4"> <div className="space-y-4">
<div className="space-y-2"> <div className="space-y-2">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<Label className="text-sm font-medium text-foreground">{'字段名称'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_fieldName')}
</label>
<span className="text-xs text-muted-foreground">{field.name.length}/20</span> <span className="text-xs text-muted-foreground">{field.name.length}/20</span>
</div> </div>
<Input <Input
value={field.name} value={field.name}
onChange={(e) => handleNameChange(e.target.value)} onChange={(e) => handleNameChange(e.target.value)}
placeholder={'请输入字段名称'} placeholder={t('testDataGenerator_fieldNamePlaceholder')}
maxLength={20} maxLength={20}
className={`h-9 ${nameError ? 'border-destructive' : ''}`} className={`h-9 ${nameError ? 'border-destructive' : ''}`}
/> />
@@ -124,7 +127,9 @@ export default function FieldEditor({ field, onChange, allFieldNames = [] }: Fie
<div className="space-y-2"> <div className="space-y-2">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<Label className="text-sm font-medium text-foreground">{'字段描述'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_fieldDescription')}
</label>
<span className="text-xs text-muted-foreground"> <span className="text-xs text-muted-foreground">
{(field.description || '').length}/50 {(field.description || '').length}/50
</span> </span>
@@ -132,7 +137,7 @@ export default function FieldEditor({ field, onChange, allFieldNames = [] }: Fie
<Input <Input
value={field.description || ''} value={field.description || ''}
onChange={(e) => handleDescriptionChange(e.target.value)} onChange={(e) => handleDescriptionChange(e.target.value)}
placeholder={'可选,添加字段说明'} placeholder={t('testDataGenerator_fieldDescriptionPlaceholder')}
maxLength={50} maxLength={50}
className="h-9" className="h-9"
/> />
@@ -142,14 +147,18 @@ export default function FieldEditor({ field, onChange, allFieldNames = [] }: Fie
{/* 必填/选填配置 */} {/* 必填/选填配置 */}
<div className="space-y-3"> <div className="space-y-3">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<Label className="text-sm font-medium text-foreground">{'必填'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_required')}
</label>
<Switch checked={field.required} onCheckedChange={handleRequiredChange} /> <Switch checked={field.required} onCheckedChange={handleRequiredChange} />
</div> </div>
{!field.required && ( {!field.required && (
<div className="space-y-2 pl-1"> <div className="space-y-2 pl-1">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">{'空值率'}</span> <span className="text-sm text-muted-foreground">
{t('testDataGenerator_nullRate')}
</span>
<Badge variant="secondary" className="text-xs"> <Badge variant="secondary" className="text-xs">
{field.nullRate}% {field.nullRate}%
</Badge> </Badge>
@@ -189,20 +198,26 @@ export default function FieldEditor({ field, onChange, allFieldNames = [] }: Fie
{/* 唯一性约束 */} {/* 唯一性约束 */}
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<Label className="text-sm font-medium text-foreground">{'唯一性约束'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_uniqueConstraint')}
</label>
<Switch checked={field.unique} onCheckedChange={handleUniqueChange} /> <Switch checked={field.unique} onCheckedChange={handleUniqueChange} />
</div> </div>
{/* 生成器选择 */} {/* 生成器选择 */}
<div className="space-y-2"> <div className="space-y-2">
<Label className="text-sm font-medium text-foreground">{'数据生成器'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_generator')}
</label>
<GeneratorSelector selectedId={field.generatorId} onChange={handleGeneratorChange} /> <GeneratorSelector selectedId={field.generatorId} onChange={handleGeneratorChange} />
</div> </div>
{/* 生成器参数配置 */} {/* 生成器参数配置 */}
{generator && ( {generator && (
<div className="space-y-2"> <div className="space-y-2">
<Label className="text-sm font-medium text-foreground">{'生成器参数'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_generatorParams')}
</label>
<GeneratorConfig <GeneratorConfig
generator={generator} generator={generator}
params={field.params} params={field.params}
@@ -3,6 +3,7 @@
* 展示单个字段的基本信息,适配固定高度卡片 * 展示单个字段的基本信息,适配固定高度卡片
*/ */
import { useI18n } from '@/utils/chromeI18n';
import { getGeneratorById } from '@/lib/generators'; import { getGeneratorById } from '@/lib/generators';
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import type { FieldConfig } from '@/types/testDataGenerator'; import type { FieldConfig } from '@/types/testDataGenerator';
@@ -13,6 +14,7 @@ interface FieldItemProps {
} }
export default function FieldItem({ field, onClick }: FieldItemProps) { export default function FieldItem({ field, onClick }: FieldItemProps) {
const { t } = useI18n('testDataGenerator');
const generator = getGeneratorById(field.generatorId); const generator = getGeneratorById(field.generatorId);
return ( return (
@@ -41,7 +43,7 @@ export default function FieldItem({ field, onClick }: FieldItemProps) {
)} )}
{field.unique && ( {field.unique && (
<Badge variant="outline" className="text-[10px] shrink-0 px-1 py-0 text-blue-500"> <Badge variant="outline" className="text-[10px] shrink-0 px-1 py-0 text-blue-500">
{'唯一'} {t('testDataGenerator_unique')}
</Badge> </Badge>
)} )}
</div> </div>
@@ -10,6 +10,7 @@ import { toast } from 'sonner';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { Dialog, DialogContent } from '@/components/ui/dialog'; import { Dialog, DialogContent } from '@/components/ui/dialog';
import { useI18n } from '@/utils/chromeI18n';
import * as ruleStorage from '@/utils/ruleStorage'; import * as ruleStorage from '@/utils/ruleStorage';
import { import {
DndContext, DndContext,
@@ -122,6 +123,7 @@ export default function FieldList({
editingRule, editingRule,
onRuleSaved, onRuleSaved,
}: FieldListProps) { }: FieldListProps) {
const { t } = useI18n('testDataGenerator');
const [scrollTop, setScrollTop] = useState(0); const [scrollTop, setScrollTop] = useState(0);
const [activeId, setActiveId] = useState<string | null>(null); const [activeId, setActiveId] = useState<string | null>(null);
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
@@ -224,10 +226,10 @@ export default function FieldList({
}); });
if (updated) { if (updated) {
toast.success('规则已更新'); toast.success(t('testDataGenerator_ruleUpdated'));
onRuleSaved?.(); onRuleSaved?.();
} }
}, [editingRule, fields, onRuleSaved]); }, [editingRule, fields, t, onRuleSaved]);
// 新建规则或另存为 // 新建规则或另存为
const handleSave = useCallback( const handleSave = useCallback(
@@ -256,18 +258,18 @@ export default function FieldList({
setShowConfirmOverwrite(false); setShowConfirmOverwrite(false);
setRuleName(''); setRuleName('');
setRuleDescription(''); setRuleDescription('');
toast.success('规则已保存'); toast.success(t('testDataGenerator_ruleSaved'));
onRuleSaved?.(); onRuleSaved?.();
} }
}, },
[ruleName, ruleDescription, fields, onRuleSaved], [ruleName, ruleDescription, fields, t, onRuleSaved],
); );
return ( return (
<div className="flex flex-col h-full"> <div className="flex flex-col h-full">
<div className="flex items-center justify-between mb-3"> <div className="flex items-center justify-between mb-3">
<h3 className="text-sm font-medium text-foreground"> <h3 className="text-sm font-medium text-foreground">
{'字段'} ({fields.length}/{MAX_FIELDS}) {t('testDataGenerator_fields')} ({fields.length}/{MAX_FIELDS})
</h3> </h3>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
{editingRule ? ( {editingRule ? (
@@ -278,10 +280,10 @@ export default function FieldList({
onClick={handleUpdateRule} onClick={handleUpdateRule}
disabled={fields.length === 0} disabled={fields.length === 0}
className="h-8 gap-1.5 px-2.5" className="h-8 gap-1.5 px-2.5"
title={`${'编辑中'}: ${editingRule.name}`} title={`${t('testDataGenerator_editing')}: ${editingRule.name}`}
> >
<Save className="h-3.5 w-3.5" /> <Save className="h-3.5 w-3.5" />
{'更新规则'} {t('testDataGenerator_updateRule')}
</Button> </Button>
<Button <Button
variant="ghost" variant="ghost"
@@ -290,7 +292,7 @@ export default function FieldList({
disabled={fields.length === 0} disabled={fields.length === 0}
className="h-8 px-2" className="h-8 px-2"
> >
{'另存为'} {t('testDataGenerator_saveAs')}
</Button> </Button>
</> </>
) : ( ) : (
@@ -302,7 +304,7 @@ export default function FieldList({
className="h-8 gap-1.5 px-2.5" className="h-8 gap-1.5 px-2.5"
> >
<Save className="h-3.5 w-3.5" /> <Save className="h-3.5 w-3.5" />
{'保存规则'} {t('testDataGenerator_saveRule')}
</Button> </Button>
)} )}
<Button <Button
@@ -313,7 +315,7 @@ export default function FieldList({
className="h-8 gap-1.5 px-2.5" className="h-8 gap-1.5 px-2.5"
> >
<Plus className="h-4 w-4" /> <Plus className="h-4 w-4" />
{'添加字段'} {t('testDataGenerator_addField')}
</Button> </Button>
</div> </div>
</div> </div>
@@ -322,8 +324,10 @@ export default function FieldList({
{fields.length === 0 ? ( {fields.length === 0 ? (
<div className="flex flex-col items-center justify-center py-8 text-center"> <div className="flex flex-col items-center justify-center py-8 text-center">
<GripVertical className="h-10 w-10 text-muted-foreground/40 mb-3" /> <GripVertical className="h-10 w-10 text-muted-foreground/40 mb-3" />
<p className="text-sm text-muted-foreground">{'暂无字段'}</p> <p className="text-sm text-muted-foreground">{t('testDataGenerator_noFields')}</p>
<p className="text-xs text-muted-foreground/70 mt-1">{'点击上方按钮添加第一个字段'}</p> <p className="text-xs text-muted-foreground/70 mt-1">
{t('testDataGenerator_addFieldHint')}
</p>
</div> </div>
) : ( ) : (
<DndContext <DndContext
@@ -415,22 +419,22 @@ export default function FieldList({
<Input <Input
value={ruleName} value={ruleName}
onChange={(e) => setRuleName(e.target.value)} onChange={(e) => setRuleName(e.target.value)}
placeholder={'规则名称'} placeholder={t('testDataGenerator_ruleNamePlaceholder')}
className="h-9" className="h-9"
/> />
<Input <Input
value={ruleDescription} value={ruleDescription}
onChange={(e) => setRuleDescription(e.target.value)} onChange={(e) => setRuleDescription(e.target.value)}
placeholder={'规则描述(可选)'} placeholder={t('testDataGenerator_ruleDescPlaceholder')}
className="h-9" className="h-9"
/> />
</div> </div>
<div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0"> <div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0">
<Button variant="ghost" size="sm" onClick={() => setShowSaveDialog(false)}> <Button variant="ghost" size="sm" onClick={() => setShowSaveDialog(false)}>
{'取消'} {t('testDataGenerator_cancel')}
</Button> </Button>
<Button size="sm" onClick={() => handleSave()} disabled={!ruleName.trim()}> <Button size="sm" onClick={() => handleSave()} disabled={!ruleName.trim()}>
{'确认'} {t('testDataGenerator_confirm')}
</Button> </Button>
</div> </div>
</DialogContent> </DialogContent>
@@ -443,14 +447,16 @@ export default function FieldList({
className="w-[calc(100vw-4rem)] max-w-[420px] p-0 pt-6 flex flex-col" className="w-[calc(100vw-4rem)] max-w-[420px] p-0 pt-6 flex flex-col"
> >
<div className="flex-1 overflow-y-auto px-6 pb-4"> <div className="flex-1 overflow-y-auto px-6 pb-4">
<p className="text-sm text-muted-foreground">{'已存在同名规则,是否覆盖保存?'}</p> <p className="text-sm text-muted-foreground">
{t('testDataGenerator_ruleNameDuplicate')}
</p>
</div> </div>
<div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0"> <div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0">
<Button variant="ghost" size="sm" onClick={() => setShowConfirmOverwrite(false)}> <Button variant="ghost" size="sm" onClick={() => setShowConfirmOverwrite(false)}>
{'取消'} {t('testDataGenerator_cancel')}
</Button> </Button>
<Button size="sm" onClick={() => handleSave(true)}> <Button size="sm" onClick={() => handleSave(true)}>
{'覆盖'} {t('testDataGenerator_overwrite')}
</Button> </Button>
</div> </div>
</DialogContent> </DialogContent>
@@ -5,6 +5,7 @@
import { Play, Square } from 'lucide-react'; import { Play, Square } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { useI18n } from '@/utils/chromeI18n';
import type { GenerateProgress } from '@/types/testDataGenerator'; import type { GenerateProgress } from '@/types/testDataGenerator';
interface GenerateButtonProps { interface GenerateButtonProps {
@@ -22,20 +23,27 @@ export default function GenerateButton({
progress, progress,
disabled, disabled,
}: GenerateButtonProps) { }: GenerateButtonProps) {
const { t } = useI18n('testDataGenerator');
return ( return (
<div className="space-y-3"> <div className="space-y-3">
{isGenerating ? ( {isGenerating ? (
<> <>
<Button variant="destructive" onClick={onCancel} className="w-full h-11 gap-2"> <Button variant="destructive" onClick={onCancel} className="w-full h-11 gap-2">
<Square className="h-5 w-5" /> <Square className="h-5 w-5" />
{'取消'} {t('testDataGenerator_cancel')}
</Button> </Button>
{/* 进度条 */} {/* 进度条 */}
{progress && ( {progress && (
<div className="space-y-2"> <div className="space-y-2">
<div className="flex items-center justify-between text-xs text-muted-foreground"> <div className="flex items-center justify-between text-xs text-muted-foreground">
<span>{'已生成 {{current}} / {{total}} 条'}</span> <span>
{t('testDataGenerator_progress', {
current: progress.generated.toLocaleString(),
total: progress.total.toLocaleString(),
})}
</span>
<span>{progress.progress}%</span> <span>{progress.progress}%</span>
</div> </div>
<div className="h-2 bg-muted rounded-full overflow-hidden"> <div className="h-2 bg-muted rounded-full overflow-hidden">
@@ -46,7 +54,9 @@ export default function GenerateButton({
</div> </div>
{progress.estimatedTimeLeft !== undefined && ( {progress.estimatedTimeLeft !== undefined && (
<p className="text-xs text-muted-foreground text-center"> <p className="text-xs text-muted-foreground text-center">
{'预计剩余 {{time}} 秒'} {t('testDataGenerator_estimatedTime', {
time: Math.ceil(progress.estimatedTimeLeft / 1000),
})}
</p> </p>
)} )}
</div> </div>
@@ -55,7 +65,7 @@ export default function GenerateButton({
) : ( ) : (
<Button onClick={onClick} disabled={disabled} className="w-full h-11 gap-2"> <Button onClick={onClick} disabled={disabled} className="w-full h-11 gap-2">
<Play className="h-5 w-5" /> <Play className="h-5 w-5" />
{'生成数据'} {t('testDataGenerator_generate')}
</Button> </Button>
)} )}
</div> </div>
@@ -3,8 +3,8 @@
* 配置生成数量、数据格式等选项 * 配置生成数量、数据格式等选项
*/ */
import { useI18n } from '@/utils/chromeI18n';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
interface GenerateOptionsProps { interface GenerateOptionsProps {
count: number; count: number;
@@ -26,11 +26,15 @@ export default function GenerateOptions({
format, format,
onFormatChange, onFormatChange,
}: GenerateOptionsProps) { }: GenerateOptionsProps) {
const { t } = useI18n('testDataGenerator');
return ( return (
<div className="space-y-4"> <div className="space-y-4">
{/* 生成数量 */} {/* 生成数量 */}
<div className="space-y-2"> <div className="space-y-2">
<Label className="text-sm font-medium text-foreground">{'生成数量'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_count')}
</label>
<div className="flex flex-wrap gap-2"> <div className="flex flex-wrap gap-2">
{COUNT_PRESETS.map((preset) => ( {COUNT_PRESETS.map((preset) => (
<button <button
@@ -63,7 +67,9 @@ export default function GenerateOptions({
{/* 数据格式 */} {/* 数据格式 */}
<div className="space-y-2"> <div className="space-y-2">
<Label className="text-sm font-medium text-foreground">{'数据格式'}</Label> <label className="text-sm font-medium text-foreground">
{t('testDataGenerator_format')}
</label>
<div className="flex gap-2"> <div className="flex gap-2">
{FORMAT_OPTIONS.map((option) => ( {FORMAT_OPTIONS.map((option) => (
<button <button
@@ -4,7 +4,6 @@
*/ */
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch'; import { Switch } from '@/components/ui/switch';
import { import {
Select, Select,
@@ -13,6 +12,7 @@ import {
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from '@/components/ui/select'; } from '@/components/ui/select';
import { useI18n } from '@/utils/chromeI18n';
import type { GeneratorDefinition } from '@/types/testDataGenerator'; import type { GeneratorDefinition } from '@/types/testDataGenerator';
interface GeneratorConfigProps { interface GeneratorConfigProps {
@@ -22,22 +22,27 @@ interface GeneratorConfigProps {
} }
export default function GeneratorConfig({ generator, params, onChange }: GeneratorConfigProps) { export default function GeneratorConfig({ generator, params, onChange }: GeneratorConfigProps) {
const { t } = useI18n('testDataGenerator');
const handleParamChange = (key: string, value: unknown) => { const handleParamChange = (key: string, value: unknown) => {
onChange({ ...params, [key]: value }); onChange({ ...params, [key]: value });
}; };
if (generator.params.length === 0) { if (generator.params.length === 0) {
return <p className="text-sm text-muted-foreground py-2">{'此生成器无可配置参数'}</p>; return (
<p className="text-sm text-muted-foreground py-2">
{t('testDataGenerator_noGeneratorParams')}
</p>
);
} }
return ( return (
<div className="space-y-4 p-3 rounded-lg bg-muted/30"> <div className="space-y-4 p-3 rounded-lg bg-muted/30">
{generator.params.map((param) => ( {generator.params.map((param) => (
<div key={param.key} className="space-y-2"> <div key={param.key} className="space-y-2">
<Label className="text-sm font-medium text-foreground"> <label className="text-sm font-medium text-foreground">
{param.label} {param.label}
{param.required && <span className="text-destructive ml-1">*</span>} {param.required && <span className="text-destructive ml-1">*</span>}
</Label> </label>
{param.description && ( {param.description && (
<p className="text-xs text-muted-foreground">{param.description}</p> <p className="text-xs text-muted-foreground">{param.description}</p>
)} )}
@@ -65,7 +70,9 @@ export default function GeneratorConfig({ generator, params, onChange }: Generat
{param.type === 'boolean' && ( {param.type === 'boolean' && (
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground"> <span className="text-sm text-muted-foreground">
{params[param.key] !== false ? '启用' : '禁用'} {params[param.key] !== false
? t('testDataGenerator_enabled')
: t('testDataGenerator_disabled')}
</span> </span>
<Switch <Switch
checked={params[param.key] !== false} checked={params[param.key] !== false}
@@ -108,7 +115,7 @@ export default function GeneratorConfig({ generator, params, onChange }: Generat
.filter(Boolean), .filter(Boolean),
) )
} }
placeholder={'用逗号分隔多个值'} placeholder={t('testDataGenerator_commaSeparated')}
className="h-9" className="h-9"
/> />
)} )}
@@ -6,6 +6,7 @@
import { useState } from 'react'; import { useState } from 'react';
import { Search, User, Briefcase, Code, Hash } from 'lucide-react'; import { Search, User, Briefcase, Code, Hash } from 'lucide-react';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { useI18n } from '@/utils/chromeI18n';
import { generatorCategories, getGeneratorsByCategory, searchGenerators } from '@/lib/generators'; import { generatorCategories, getGeneratorsByCategory, searchGenerators } from '@/lib/generators';
interface GeneratorSelectorProps { interface GeneratorSelectorProps {
@@ -21,6 +22,7 @@ const categoryIcons: Record<string, React.ComponentType<{ className?: string }>>
}; };
export default function GeneratorSelector({ selectedId, onChange }: GeneratorSelectorProps) { export default function GeneratorSelector({ selectedId, onChange }: GeneratorSelectorProps) {
const { t } = useI18n('testDataGenerator');
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const [activeCategory, setActiveCategory] = useState<string>(generatorCategories[0]?.id || ''); const [activeCategory, setActiveCategory] = useState<string>(generatorCategories[0]?.id || '');
@@ -36,7 +38,7 @@ export default function GeneratorSelector({ selectedId, onChange }: GeneratorSel
<Input <Input
value={searchQuery} value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)} onChange={(e) => setSearchQuery(e.target.value)}
placeholder={'搜索生成器...'} placeholder={t('testDataGenerator_searchGenerator')}
className="pl-9 h-9" className="pl-9 h-9"
/> />
</div> </div>
@@ -4,6 +4,7 @@
*/ */
import { CheckCircle, AlertTriangle, XCircle, Clock, Database } from 'lucide-react'; import { CheckCircle, AlertTriangle, XCircle, Clock, Database } from 'lucide-react';
import { useI18n } from '@/utils/chromeI18n';
import type { GenerateResult } from '@/types/testDataGenerator'; import type { GenerateResult } from '@/types/testDataGenerator';
interface ResultPanelProps { interface ResultPanelProps {
@@ -11,6 +12,8 @@ interface ResultPanelProps {
} }
export default function ResultPanel({ result }: ResultPanelProps) { export default function ResultPanel({ result }: ResultPanelProps) {
const { t } = useI18n('testDataGenerator');
if (!result) return null; if (!result) return null;
const getStatusIcon = () => { const getStatusIcon = () => {
@@ -25,12 +28,12 @@ export default function ResultPanel({ result }: ResultPanelProps) {
const getStatusText = () => { const getStatusText = () => {
if (result.success && (!result.warnings || result.warnings.length === 0)) { if (result.success && (!result.warnings || result.warnings.length === 0)) {
return '生成成功'; return t('testDataGenerator_success');
} }
if (result.success && result.warnings && result.warnings.length > 0) { if (result.success && result.warnings && result.warnings.length > 0) {
return '部分成功'; return t('testDataGenerator_partialSuccess');
} }
return '生成失败'; return t('testDataGenerator_failed');
}; };
return ( return (
@@ -49,7 +52,9 @@ export default function ResultPanel({ result }: ResultPanelProps) {
<span className="text-lg font-semibold text-foreground"> <span className="text-lg font-semibold text-foreground">
{result.stats.total.toLocaleString()} {result.stats.total.toLocaleString()}
</span> </span>
<span className="text-xs text-muted-foreground">{'总条数'}</span> <span className="text-xs text-muted-foreground">
{t('testDataGenerator_totalCount')}
</span>
</div> </div>
<div className="flex flex-col items-center p-2 rounded-lg bg-muted/30"> <div className="flex flex-col items-center p-2 rounded-lg bg-muted/30">
@@ -57,7 +62,9 @@ export default function ResultPanel({ result }: ResultPanelProps) {
<span className="text-lg font-semibold text-green-500"> <span className="text-lg font-semibold text-green-500">
{result.stats.success.toLocaleString()} {result.stats.success.toLocaleString()}
</span> </span>
<span className="text-xs text-muted-foreground">{'成功'}</span> <span className="text-xs text-muted-foreground">
{t('testDataGenerator_successCount')}
</span>
</div> </div>
<div className="flex flex-col items-center p-2 rounded-lg bg-muted/30"> <div className="flex flex-col items-center p-2 rounded-lg bg-muted/30">
@@ -65,7 +72,7 @@ export default function ResultPanel({ result }: ResultPanelProps) {
<span className="text-lg font-semibold text-foreground"> <span className="text-lg font-semibold text-foreground">
{(result.stats.duration / 1000).toFixed(2)}s {(result.stats.duration / 1000).toFixed(2)}s
</span> </span>
<span className="text-xs text-muted-foreground">{'耗时'}</span> <span className="text-xs text-muted-foreground">{t('testDataGenerator_duration')}</span>
</div> </div>
</div> </div>
)} )}
@@ -75,7 +82,9 @@ export default function ResultPanel({ result }: ResultPanelProps) {
<div className="p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/20"> <div className="p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/20">
<div className="flex items-center gap-2 mb-2"> <div className="flex items-center gap-2 mb-2">
<AlertTriangle className="h-4 w-4 text-yellow-500" /> <AlertTriangle className="h-4 w-4 text-yellow-500" />
<span className="text-sm font-medium text-yellow-500">{'警告'}</span> <span className="text-sm font-medium text-yellow-500">
{t('testDataGenerator_warnings')}
</span>
</div> </div>
<ul className="list-disc list-inside space-y-1"> <ul className="list-disc list-inside space-y-1">
{result.warnings.slice(0, 5).map((warning, index) => ( {result.warnings.slice(0, 5).map((warning, index) => (
@@ -84,7 +93,9 @@ export default function ResultPanel({ result }: ResultPanelProps) {
</li> </li>
))} ))}
{result.warnings.length > 5 && ( {result.warnings.length > 5 && (
<li className="text-xs text-yellow-500/80">... {'还有 {{count}} 条警告'}</li> <li className="text-xs text-yellow-500/80">
... {t('testDataGenerator_moreWarnings', { count: result.warnings.length - 5 })}
</li>
)} )}
</ul> </ul>
</div> </div>
@@ -20,6 +20,7 @@ import { toast } from 'sonner';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { Dialog, DialogContent } from '@/components/ui/dialog'; import { Dialog, DialogContent } from '@/components/ui/dialog';
import { useI18n } from '@/utils/chromeI18n';
import * as ruleStorage from '@/utils/ruleStorage'; import * as ruleStorage from '@/utils/ruleStorage';
import type { DataRule, FieldConfig } from '@/types/testDataGenerator'; import type { DataRule, FieldConfig } from '@/types/testDataGenerator';
@@ -30,6 +31,7 @@ interface RuleManagerProps {
} }
export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleManagerProps) { export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleManagerProps) {
const { t, i18n } = useI18n('testDataGenerator');
const [rules, setRules] = useState<DataRule[]>(() => ruleStorage.getAll()); const [rules, setRules] = useState<DataRule[]>(() => ruleStorage.getAll());
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const [debouncedSearchQuery, setDebouncedSearchQuery] = useState(''); const [debouncedSearchQuery, setDebouncedSearchQuery] = useState('');
@@ -62,9 +64,9 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
onLoad(rule.fields); onLoad(rule.fields);
ruleStorage.recordUse(rule.id); ruleStorage.recordUse(rule.id);
loadRules(); loadRules();
toast.success(`已加载规则「${rule.name}`); toast.success(t('testDataGenerator_ruleLoaded', { name: rule.name }));
}, },
[onLoad, loadRules], [onLoad, loadRules, t],
); );
const handleDelete = useCallback(() => { const handleDelete = useCallback(() => {
@@ -72,20 +74,20 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
ruleStorage.deleteRule(ruleToDelete.id); ruleStorage.deleteRule(ruleToDelete.id);
loadRules(); loadRules();
setRuleToDelete(null); setRuleToDelete(null);
toast.success('规则已删除'); toast.success(t('testDataGenerator_ruleDeleted'));
onRulesChanged?.(); onRulesChanged?.();
}, [ruleToDelete, loadRules, onRulesChanged]); }, [ruleToDelete, loadRules, t, onRulesChanged]);
const handleDuplicate = useCallback( const handleDuplicate = useCallback(
(id: string) => { (id: string) => {
const result = ruleStorage.duplicate(id, '(副本)'); const result = ruleStorage.duplicate(id, t('testDataGenerator_ruleCopySuffix'));
if (result) { if (result) {
loadRules(); loadRules();
toast.success('规则已复制'); toast.success(t('testDataGenerator_ruleDuplicated'));
onRulesChanged?.(); onRulesChanged?.();
} }
}, },
[loadRules, onRulesChanged], [loadRules, t, onRulesChanged],
); );
const handleEdit = useCallback( const handleEdit = useCallback(
@@ -111,14 +113,14 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
} finally { } finally {
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
} }
toast.success('规则已导出'); toast.success(t('testDataGenerator_exportSuccess'));
} catch (error) { } catch (error) {
console.error('[RuleManager] 导出失败:', error); console.error('[RuleManager] 导出失败:', error);
toast.error('导出失败'); toast.error(t('testDataGenerator_exportFailed'));
} finally { } finally {
setIsExporting(false); setIsExporting(false);
} }
}, []); }, [t]);
const handleImport = useCallback(() => { const handleImport = useCallback(() => {
const input = document.createElement('input'); const input = document.createElement('input');
@@ -134,11 +136,11 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
const result = ruleStorage.importRules(text); const result = ruleStorage.importRules(text);
if (result.success > 0) { if (result.success > 0) {
toast.success(`成功导入 ${result.success} 条规则`); toast.success(t('testDataGenerator_importSuccess', { count: result.success }));
} }
if (result.failed > 0) { if (result.failed > 0) {
toast.error(`${result.failed} 条规则导入失败`); toast.error(t('testDataGenerator_importFailed', { count: result.failed }));
console.warn('[RuleManager] 导入警告:', result.errors); console.warn('[RuleManager] 导入警告:', result.errors);
} }
@@ -146,22 +148,25 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
onRulesChanged?.(); onRulesChanged?.();
} catch (error) { } catch (error) {
console.error('[RuleManager] 导入失败:', error); console.error('[RuleManager] 导入失败:', error);
toast.error('规则导入失败'); toast.error(t('testDataGenerator_importFailed'));
} finally { } finally {
setIsImporting(false); setIsImporting(false);
} }
}; };
input.click(); input.click();
}, [loadRules, onRulesChanged]); }, [loadRules, t, onRulesChanged]);
const formatDate = useCallback((timestamp: number) => { const formatDate = useCallback(
return new Date(timestamp).toLocaleDateString('zh-CN', { (timestamp: number) => {
return new Date(timestamp).toLocaleDateString(i18n.language || 'zh-CN', {
month: 'short', month: 'short',
day: 'numeric', day: 'numeric',
hour: '2-digit', hour: '2-digit',
minute: '2-digit', minute: '2-digit',
}); });
}, []); },
[i18n.language],
);
return ( return (
<div className="space-y-4"> <div className="space-y-4">
@@ -173,15 +178,15 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
> >
<div className="flex-1 overflow-y-auto px-6 pb-4"> <div className="flex-1 overflow-y-auto px-6 pb-4">
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
{ruleToDelete && `确定要删除规则「${ruleToDelete.name}」吗?此操作不可撤销。`} {t('testDataGenerator_confirmDeleteDescription', { name: ruleToDelete?.name ?? '' })}
</p> </p>
</div> </div>
<div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0"> <div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0">
<Button variant="ghost" size="sm" onClick={() => setRuleToDelete(null)}> <Button variant="ghost" size="sm" onClick={() => setRuleToDelete(null)}>
{'取消'} {t('testDataGenerator_cancel')}
</Button> </Button>
<Button variant="destructive" size="sm" onClick={handleDelete}> <Button variant="destructive" size="sm" onClick={handleDelete}>
{'确认'} {t('testDataGenerator_confirm')}
</Button> </Button>
</div> </div>
</DialogContent> </DialogContent>
@@ -201,7 +206,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
) : ( ) : (
<Upload className="h-4 w-4" /> <Upload className="h-4 w-4" />
)} )}
{'导入'} {t('testDataGenerator_import')}
</Button> </Button>
<Button <Button
variant="outline" variant="outline"
@@ -215,7 +220,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
) : ( ) : (
<Download className="h-4 w-4" /> <Download className="h-4 w-4" />
)} )}
{'导出数据'} {t('testDataGenerator_export')}
</Button> </Button>
</div> </div>
@@ -225,12 +230,12 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
<Input <Input
value={searchQuery} value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value.slice(0, 20))} onChange={(e) => setSearchQuery(e.target.value.slice(0, 20))}
placeholder={'搜索规则...'} placeholder={t('testDataGenerator_searchRules')}
className="pl-9 pr-24 h-9" className="pl-9 pr-24 h-9"
maxLength={20} maxLength={20}
/> />
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-xs text-muted-foreground pointer-events-none tabular-nums"> <span className="absolute right-3 top-1/2 -translate-y-1/2 text-xs text-muted-foreground pointer-events-none">
{`已保存 ${rules.length}/${ruleStorage.MAX_RULES}`} {t('testDataGenerator_ruleCount', { count: rules.length, max: 20 })}
</span> </span>
</div> </div>
@@ -240,7 +245,9 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
<div className="text-center py-6"> <div className="text-center py-6">
<Tag className="h-8 w-8 text-muted-foreground/40 mx-auto mb-2" /> <Tag className="h-8 w-8 text-muted-foreground/40 mx-auto mb-2" />
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
{debouncedSearchQuery ? '未找到匹配的规则' : '暂无保存的规则'} {debouncedSearchQuery
? t('testDataGenerator_noSearchResults')
: t('testDataGenerator_noRules')}
</p> </p>
</div> </div>
) : ( ) : (
@@ -253,7 +260,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<span className="font-medium text-sm text-foreground truncate">{rule.name}</span> <span className="font-medium text-sm text-foreground truncate">{rule.name}</span>
<span className="text-xs text-muted-foreground"> <span className="text-xs text-muted-foreground">
{rule.fields.length} {'字段'} {rule.fields.length} {t('testDataGenerator_fields')}
</span> </span>
</div> </div>
{rule.description && ( {rule.description && (
@@ -266,7 +273,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
<Clock className="h-3 w-3" /> <Clock className="h-3 w-3" />
{formatDate(rule.updatedAt)} {formatDate(rule.updatedAt)}
</span> </span>
<span>{`使用 ${rule.useCount}`}</span> <span>{t('testDataGenerator_usedTimes', { count: rule.useCount })}</span>
</div> </div>
</div> </div>
@@ -276,7 +283,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
size="icon" size="icon"
className="h-7 w-7" className="h-7 w-7"
onClick={() => handleLoad(rule)} onClick={() => handleLoad(rule)}
title={'加载'} title={t('testDataGenerator_load')}
> >
<FolderOpen className="h-3.5 w-3.5" /> <FolderOpen className="h-3.5 w-3.5" />
</Button> </Button>
@@ -285,7 +292,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
size="icon" size="icon"
className="h-7 w-7" className="h-7 w-7"
onClick={() => handleEdit(rule)} onClick={() => handleEdit(rule)}
title={'编辑'} title={t('testDataGenerator_edit')}
> >
<Edit className="h-3.5 w-3.5" /> <Edit className="h-3.5 w-3.5" />
</Button> </Button>
@@ -294,7 +301,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
size="icon" size="icon"
className="h-7 w-7" className="h-7 w-7"
onClick={() => handleDuplicate(rule.id)} onClick={() => handleDuplicate(rule.id)}
title={'复制'} title={t('testDataGenerator_duplicate')}
> >
<Copy className="h-3.5 w-3.5" /> <Copy className="h-3.5 w-3.5" />
</Button> </Button>
@@ -303,7 +310,7 @@ export default function RuleManager({ onLoad, onEdit, onRulesChanged }: RuleMana
size="icon" size="icon"
className="h-7 w-7 text-destructive hover:text-destructive" className="h-7 w-7 text-destructive hover:text-destructive"
onClick={() => setRuleToDelete(rule)} onClick={() => setRuleToDelete(rule)}
title={'删除'} title={t('testDataGenerator_delete')}
> >
<Trash2 className="h-3.5 w-3.5" /> <Trash2 className="h-3.5 w-3.5" />
</Button> </Button>
@@ -4,7 +4,6 @@ import userEvent from '@testing-library/user-event';
// Mock dependencies // Mock dependencies
vi.mock('@/utils/ruleStorage', () => ({ vi.mock('@/utils/ruleStorage', () => ({
MAX_RULES: 20,
getAll: vi.fn(() => []), getAll: vi.fn(() => []),
save: vi.fn(), save: vi.fn(),
deleteRule: vi.fn(), deleteRule: vi.fn(),
@@ -88,7 +87,8 @@ describe('RuleManager', () => {
expect(defaultProps.onLoad).toHaveBeenCalledWith(mockFields); expect(defaultProps.onLoad).toHaveBeenCalledWith(mockFields);
expect(mockedRuleStorage.recordUse).toHaveBeenCalledWith('rule-1'); expect(mockedRuleStorage.recordUse).toHaveBeenCalledWith('rule-1');
expect(mockedToast.success).toHaveBeenCalledWith('已加载规则「Test Rule」'); // Note: t() mock doesn't handle placeholders, so we just check it was called
expect(mockedToast.success).toHaveBeenCalled();
}); });
it('should show delete confirmation dialog', async () => { it('should show delete confirmation dialog', async () => {
@@ -100,7 +100,7 @@ describe('RuleManager', () => {
const deleteButton = screen.getByTitle('删除'); const deleteButton = screen.getByTitle('删除');
await user.click(deleteButton); await user.click(deleteButton);
expect(screen.getByText('确定要删除规则「Test Rule」吗?此操作不可撤销。')).toBeInTheDocument(); expect(screen.getByText(/确定要删除规则/)).toBeInTheDocument();
}); });
it('should delete rule after confirmation', async () => { it('should delete rule after confirmation', async () => {
@@ -178,14 +178,6 @@ describe('RuleManager', () => {
vi.restoreAllMocks(); vi.restoreAllMocks();
}); });
it('should show saved rule count', () => {
mockedRuleStorage.getAll.mockReturnValue([mockRule]);
render(<RuleManager {...defaultProps} />);
expect(screen.getByText('已保存 1/20 条')).toBeInTheDocument();
});
it('should show field count for each rule', () => { it('should show field count for each rule', () => {
mockedRuleStorage.getAll.mockReturnValue([mockRule]); mockedRuleStorage.getAll.mockReturnValue([mockRule]);
+12 -10
View File
@@ -4,6 +4,7 @@
import { useState, useCallback, useRef, useEffect } from 'react'; import { useState, useCallback, useRef, useEffect } from 'react';
import { Settings, Database, Tag } from 'lucide-react'; import { Settings, Database, Tag } from 'lucide-react';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { useGenerator } from './hooks/useGenerator'; import { useGenerator } from './hooks/useGenerator';
import type { FieldConfig, GenerateResult, DataRule } from '@/types/testDataGenerator'; import type { FieldConfig, GenerateResult, DataRule } from '@/types/testDataGenerator';
@@ -28,6 +29,7 @@ import RuleManager from './components/RuleManager';
type TabType = 'fields' | 'rules'; type TabType = 'fields' | 'rules';
export default function TestDataGeneratorPage() { export default function TestDataGeneratorPage() {
const { t } = useI18n('testDataGenerator');
const { isGenerating, progress, result, error, generate, cancel, clearResult } = useGenerator(); const { isGenerating, progress, result, error, generate, cancel, clearResult } = useGenerator();
// 字段配置 // 字段配置
@@ -50,11 +52,11 @@ export default function TestDataGeneratorPage() {
useEffect(() => { useEffect(() => {
if (result?.success && result.stats && result !== lastToastResultRef.current) { if (result?.success && result.stats && result !== lastToastResultRef.current) {
lastToastResultRef.current = result; lastToastResultRef.current = result;
toast.success('生成完成', { toast.success(t('testDataGenerator_generateSuccess'), {
description: `${result.stats.total} ${'条数据'}`, description: `${result.stats.total} ${t('testDataGenerator_records')}`,
}); });
} }
}, [result]); }, [result, t]);
// 添加新字段 // 添加新字段
const handleAddField = useCallback(() => { const handleAddField = useCallback(() => {
@@ -130,9 +132,9 @@ export default function TestDataGeneratorPage() {
setEditingRule(rule); setEditingRule(rule);
setActiveTab('fields'); setActiveTab('fields');
clearResult(); clearResult();
toast.success('正在编辑规则「{{name}}」'); toast.success(t('testDataGenerator_editingRule', { name: rule.name }));
}, },
[clearResult], [clearResult, t],
); );
// 保存规则成功后清除编辑状态 // 保存规则成功后清除编辑状态
@@ -174,7 +176,7 @@ export default function TestDataGeneratorPage() {
)} )}
> >
<Settings className="h-4 w-4" /> <Settings className="h-4 w-4" />
{'字段配置'} {t('testDataGenerator_fieldConfig')}
</button> </button>
<button <button
onClick={() => setActiveTab('rules')} onClick={() => setActiveTab('rules')}
@@ -186,7 +188,7 @@ export default function TestDataGeneratorPage() {
)} )}
> >
<Tag className="h-4 w-4" /> <Tag className="h-4 w-4" />
{'规则管理'} {t('testDataGenerator_ruleManagement')}
</button> </button>
</div> </div>
@@ -257,7 +259,7 @@ export default function TestDataGeneratorPage() {
<div className="p-4 rounded-xl border border-border bg-card shadow-sm"> <div className="p-4 rounded-xl border border-border bg-card shadow-sm">
<h3 className="text-sm font-medium text-foreground mb-3 flex items-center gap-2"> <h3 className="text-sm font-medium text-foreground mb-3 flex items-center gap-2">
<Database className="h-4 w-4" /> <Database className="h-4 w-4" />
{'数据预览'} {t('testDataGenerator_dataPreview')}
</h3> </h3>
<div className="h-[280px]"> <div className="h-[280px]">
<DataPreview fields={fields} /> <DataPreview fields={fields} />
@@ -291,10 +293,10 @@ export default function TestDataGeneratorPage() {
</div> </div>
<div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0"> <div className="flex justify-end gap-2 px-6 py-2 border-t shrink-0">
<Button variant="ghost" size="sm" onClick={() => setIsEditorOpen(false)}> <Button variant="ghost" size="sm" onClick={() => setIsEditorOpen(false)}>
{'取消'} {t('testDataGenerator_cancel')}
</Button> </Button>
<Button size="sm" onClick={() => setIsEditorOpen(false)}> <Button size="sm" onClick={() => setIsEditorOpen(false)}>
{'完成'} {t('testDataGenerator_done')}
</Button> </Button>
</div> </div>
</DialogContent> </DialogContent>
+7 -5
View File
@@ -1,16 +1,18 @@
import TextInputArea from '@/components/TextInputArea'; import TextInputArea from '@/components/TextInputArea';
import { formatBytes } from '@/utils/format'; import { formatBytes } from '@/utils/format';
import { useI18n } from '@/utils/chromeI18n';
import { useTextStatistics } from './useTextStatistics'; import { useTextStatistics } from './useTextStatistics';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
export default function Index() { export default function Index() {
const { t } = useI18n('textStatistics');
const { text, stats, setText } = useTextStatistics(); const { text, stats, setText } = useTextStatistics();
const statItems = [ const statItems = [
{ label: '字符数', value: stats.characters }, { label: t('textStatistics:characters'), value: stats.characters },
{ label: '单词数', value: stats.words }, { label: t('textStatistics:words'), value: stats.words },
{ label: '行数', value: stats.lines }, { label: t('textStatistics:lines'), value: stats.lines },
{ label: '字节大小', value: formatBytes(stats.bytes) }, { label: t('textStatistics:bytes'), value: formatBytes(stats.bytes) },
]; ];
return ( return (
@@ -18,7 +20,7 @@ export default function Index() {
<TextInputArea <TextInputArea
value={text} value={text}
onChange={setText} onChange={setText}
placeholder={'在此输入或粘贴文本...'} placeholder={t('textStatistics:placeholder')}
minRows={10} minRows={10}
maxRows={18} maxRows={18}
showClear={true} showClear={true}
@@ -2,8 +2,9 @@ import React, { useEffect, useState } from 'react';
import { Clock } from 'lucide-react'; import { Clock } from 'lucide-react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
import type { UnitType } from '../constants'; import type { UnitType } from './constants';
import { msToUnit } from '../constants'; import { msToUnit } from './constants';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
interface LiveClockProps extends React.HTMLAttributes<HTMLDivElement> { interface LiveClockProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -12,6 +13,8 @@ interface LiveClockProps extends React.HTMLAttributes<HTMLDivElement> {
} }
export default function LiveClock({ unit, onUseNow, className, ...props }: LiveClockProps) { export default function LiveClock({ unit, onUseNow, className, ...props }: LiveClockProps) {
const { t } = useI18n('timestamp');
const [rawTime, setRawTime] = useState(() => Date.now()); const [rawTime, setRawTime] = useState(() => Date.now());
useEffect(() => { useEffect(() => {
@@ -33,7 +36,7 @@ export default function LiveClock({ unit, onUseNow, className, ...props }: LiveC
{...props} {...props}
> >
<span className="text-muted-foreground font-bold text-[10px] uppercase tracking-wider whitespace-nowrap shrink-0 selection:bg-transparent select-none"> <span className="text-muted-foreground font-bold text-[10px] uppercase tracking-wider whitespace-nowrap shrink-0 selection:bg-transparent select-none">
{'当前时间戳'} {t('timestamp:currentTs')}
</span> </span>
<span className="flex-1 font-mono font-bold text-foreground text-sm tracking-tight leading-none truncate tabular-nums"> <span className="flex-1 font-mono font-bold text-foreground text-sm tracking-tight leading-none truncate tabular-nums">
@@ -44,15 +47,19 @@ export default function LiveClock({ unit, onUseNow, className, ...props }: LiveC
type="button" type="button"
onClick={() => { onClick={() => {
onUseNow(rawTime); onUseNow(rawTime);
toast.success('已使用当前时间戳'); toast.success(t('timestamp:usedSuccess'));
}} }}
title={'填充到下方'} title={t('timestamp:useNowTooltip')}
className="flex h-7 w-7 items-center justify-center rounded-md border border-input bg-background text-muted-foreground shadow-sm hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" className="flex h-7 w-7 items-center justify-center rounded-md border border-input bg-background text-muted-foreground shadow-sm hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
> >
<Clock className="w-3.5 h-3.5" /> <Clock className="w-3.5 h-3.5" />
</button> </button>
<CopyButton text={text} tooltip={'复制时间戳'} className="h-7 w-7 rounded-md border" /> <CopyButton
text={text}
tooltip={t('timestamp:copyTsTooltip')}
className="h-7 w-7 rounded-md border"
/>
</div> </div>
); );
} }
@@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
interface ResultViewProps extends React.HTMLAttributes<HTMLDivElement> { interface ResultViewProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -13,6 +14,8 @@ export default function ResultView({
className, className,
...props ...props
}: ResultViewProps) { }: ResultViewProps) {
const { t } = useI18n('timestamp');
if (!result) { if (!result) {
if (!showEmptyPlaceholder) return null; if (!showEmptyPlaceholder) return null;
return ( return (
@@ -23,7 +26,7 @@ export default function ResultView({
)} )}
{...props} {...props}
> >
{'请输入并点击转换'} {t('timestamp:resultEmpty')}
</div> </div>
); );
} }
@@ -31,7 +34,7 @@ export default function ResultView({
return ( return (
<div className={cn('flex flex-col w-full', className)} {...props}> <div className={cn('flex flex-col w-full', className)} {...props}>
<span className="block text-muted-foreground/90 mb-2.5 text-xs font-semibold tracking-wider uppercase"> <span className="block text-muted-foreground/90 mb-2.5 text-xs font-semibold tracking-wider uppercase">
{'转换结果'} {t('timestamp:resultLabel')}
</span> </span>
<div className="bg-card text-card-foreground border border-border p-4 sm:p-5 rounded-xl relative shadow-sm flex justify-between items-center gap-4 focus-within:ring-1 focus-within:ring-ring"> <div className="bg-card text-card-foreground border border-border p-4 sm:p-5 rounded-xl relative shadow-sm flex justify-between items-center gap-4 focus-within:ring-1 focus-within:ring-ring">
@@ -40,7 +43,7 @@ export default function ResultView({
</span> </span>
<CopyButton <CopyButton
text={result} text={result}
tooltip={'复制结果'} tooltip={t('timestamp:copyResultTooltip')}
className="h-8 w-8 rounded-md shrink-0 border" className="h-8 w-8 rounded-md shrink-0 border"
/> />
</div> </div>
@@ -1,11 +0,0 @@
import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import Index from '../index';
describe('Timestamp 页面', () => {
it('应该渲染模式切换按钮', () => {
render(<Index />);
// 基本渲染测试:页面含多个模式切换按钮,应至少渲染一个
expect(screen.getAllByRole('button').length).toBeGreaterThan(0);
});
});
+14 -9
View File
@@ -1,9 +1,10 @@
import SwitchButtonGroup from '@/components/SwitchButtonGroup'; import SwitchButtonGroup from '@/components/SwitchButtonGroup';
import { ZONES } from './constants'; import { ZONES } from './constants';
import type { ModeType, UnitType, ZoneType } from './constants'; import type { ModeType, UnitType, ZoneType } from './constants';
import LiveClock from './components/LiveClock'; import LiveClock from './LiveClock';
import ResultView from './components/ResultView'; import ResultView from './ResultView';
import { useTimestampConverter } from './useTimestampConverter'; import { useTimestampConverter } from './useTimestampConverter';
import { useI18n } from '@/utils/chromeI18n';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
@@ -16,16 +17,18 @@ import {
} from '@/components/ui/select'; } from '@/components/ui/select';
const MODE_OPTIONS: { value: ModeType; label: string }[] = [ const MODE_OPTIONS: { value: ModeType; label: string }[] = [
{ value: 'ts2dt', label: '时间戳转日期' }, { value: 'ts2dt', label: 'timestamp:tsToDate' },
{ value: 'dt2ts', label: '日期转时间戳' }, { value: 'dt2ts', label: 'timestamp:dateToTs' },
]; ];
const UNIT_OPTIONS: { value: UnitType; label: string }[] = [ const UNIT_OPTIONS: { value: UnitType; label: string }[] = [
{ value: 'ms', label: '毫秒' }, { value: 'ms', label: 'timestamp:unitMs' },
{ value: 's', label: '' }, { value: 's', label: 'timestamp:unitS' },
]; ];
export default function Index() { export default function Index() {
const { t } = useI18n('timestamp');
const { const {
mode, mode,
input, input,
@@ -50,7 +53,7 @@ export default function Index() {
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
<SwitchButtonGroup <SwitchButtonGroup
value={mode} value={mode}
options={MODE_OPTIONS} options={MODE_OPTIONS.map((o) => ({ ...o, label: t(o.label) }))}
onChange={setMode} onChange={setMode}
size="small" size="small"
/> />
@@ -58,7 +61,9 @@ export default function Index() {
<div className="flex flex-col gap-1.5"> <div className="flex flex-col gap-1.5">
<Input <Input
type="text" type="text"
placeholder={mode === 'ts2dt' ? '输入时间戳...' : 'YYYY-MM-DD HH:mm:ss'} placeholder={
mode === 'ts2dt' ? t('timestamp:placeholderTs') : t('timestamp:placeholderDate')
}
value={input} value={input}
onChange={(e) => setInput(e.target.value)} onChange={(e) => setInput(e.target.value)}
className={cn( className={cn(
@@ -73,7 +78,7 @@ export default function Index() {
<div className="flex flex-col sm:flex-row items-stretch gap-3 w-full"> <div className="flex flex-col sm:flex-row items-stretch gap-3 w-full">
<SwitchButtonGroup <SwitchButtonGroup
value={unit} value={unit}
options={UNIT_OPTIONS} options={UNIT_OPTIONS.map((o) => ({ ...o, label: t(o.label) }))}
onChange={setUnit} onChange={setUnit}
size="small" size="small"
className="sm:w-auto shrink-0" className="sm:w-auto shrink-0"
+6 -4
View File
@@ -2,6 +2,7 @@ import { useMemo, useState } from 'react';
import dayjs from '@/utils/dayjs'; import dayjs from '@/utils/dayjs';
import type { UnitType, ZoneType, ModeType } from './constants'; import type { UnitType, ZoneType, ModeType } from './constants';
import { DATE_FORMAT, msToUnit, dayjsFromTimestamp } from './constants'; import { DATE_FORMAT, msToUnit, dayjsFromTimestamp } from './constants';
import { useI18n } from '@/utils/chromeI18n';
import { useContextMenuData } from '@/utils/useContextMenuData'; import { useContextMenuData } from '@/utils/useContextMenuData';
export interface UseTimestampConverterReturn { export interface UseTimestampConverterReturn {
@@ -28,6 +29,7 @@ function isTimestampLike(input: string): boolean {
} }
export function useTimestampConverter(): UseTimestampConverterReturn { export function useTimestampConverter(): UseTimestampConverterReturn {
const { t } = useI18n('timestamp');
const [mode, setMode] = useState<ModeType>('ts2dt'); const [mode, setMode] = useState<ModeType>('ts2dt');
const [unit, setUnit] = useState<UnitType>('ms'); const [unit, setUnit] = useState<UnitType>('ms');
const [zone, setZone] = useState<ZoneType>('Asia/Shanghai'); const [zone, setZone] = useState<ZoneType>('Asia/Shanghai');
@@ -41,22 +43,22 @@ export function useTimestampConverter(): UseTimestampConverterReturn {
if (mode === 'ts2dt') { if (mode === 'ts2dt') {
const num = Number(rawInput); const num = Number(rawInput);
if (isNaN(num)) { if (isNaN(num)) {
return { result: '', error: '请输入有效数字' }; return { result: '', error: t('timestamp:errors.invalidNumber') };
} }
const d = dayjsFromTimestamp(num, unit); const d = dayjsFromTimestamp(num, unit);
if (!d.isValid()) { if (!d.isValid()) {
return { result: '', error: '无效时间戳' }; return { result: '', error: t('timestamp:errors.invalidTimestamp') };
} }
return { result: d.tz(zone).format(DATE_FORMAT), error: '' }; return { result: d.tz(zone).format(DATE_FORMAT), error: '' };
} else { } else {
const d = dayjs.tz(rawInput, DATE_FORMAT, zone); const d = dayjs.tz(rawInput, DATE_FORMAT, zone);
if (!d.isValid()) { if (!d.isValid()) {
return { result: '', error: '无效的日期格式' }; return { result: '', error: t('timestamp:errors.invalidFormat') };
} }
const ms = d.valueOf(); const ms = d.valueOf();
return { result: String(msToUnit(ms, unit)), error: '' }; return { result: String(msToUnit(ms, unit)), error: '' };
} }
}, [input, mode, unit, zone]); }, [input, mode, unit, zone, t]);
const handleContextMenuData = (payload: string) => { const handleContextMenuData = (payload: string) => {
const trimmed = payload.trim(); const trimmed = payload.trim();
+11 -3
View File
@@ -10,12 +10,10 @@
**页面类型:** **页面类型:**
- `PageType` — 所有页面类型的联合类型(`dashboard` | `timestamp` | `storageCleaner` | ... - `PageType` — 所有页面类型的联合类型(`dashboard``timestamp``storageCleaner``testDataGenerator`
- `JsonToolsPageMode` — JSON 工具子模式(`diff` | `format` | `yaml` | `toml` | `minify` - `JsonToolsPageMode` — JSON 工具子模式(`diff` | `format` | `yaml` | `toml` | `minify`
- `Base64ConverterPageMode` — Base64 子模式(`text` | `file` | `image` - `Base64ConverterPageMode` — Base64 子模式(`text` | `file` | `image`
- `Base64ConvertDirection` — 编解码方向(`encode` | `decode` - `Base64ConvertDirection` — 编解码方向(`encode` | `decode`
- `MarkdownToHtmlPreviewMode` — Markdown 预览模式(`split` | `preview` | `html`
- `HtmlToMarkdownPreviewMode` — HTML 预览模式(`split` | `preview` | `markdown`
**存储 Schema** **存储 Schema**
@@ -34,6 +32,16 @@
`qrious` 库的类型声明,定义 QR 码生成选项和 `QRious` 类。 `qrious` 库的类型声明,定义 QR 码生成选项和 `QRious` 类。
### testDataGenerator.ts
测试数据生成器共享类型,包含:
- `FieldConfig` — 字段配置(字段名、生成器、参数、必填、空值率、唯一性)
- `DataRule` — 可保存/导入/导出的字段规则
- `GeneratorDefinition` / `GeneratorParam` — 内置生成器定义和参数 Schema
- `GenerateResult` / `GenerateProgress` / `WorkerMessage` — Worker 生成结果、进度和消息协议
- `ExportFile` — JSON/CSV 导出文件描述
## 修改 StorageSchema 的注意事项 ## 修改 StorageSchema 的注意事项
修改 `StorageSchema` 时,必须: 修改 `StorageSchema` 时,必须:
+6 -5
View File
@@ -5,7 +5,7 @@
## 工具函数 ## 工具函数
| 文件 | 用途 | | 文件 | 用途 |
| -------------------- | --------------------------------------------------------------------------------------------------- | | ------------------------ | --------------------------------------------------------------------------------------------------- |
| `chromeStorage.ts` | Chrome Storage API 封装:类型安全的 `StorageUtils` 类,提供 `get/set/remove` 方法 | | `chromeStorage.ts` | Chrome Storage API 封装:类型安全的 `StorageUtils` 类,提供 `get/set/remove` 方法 |
| `chromeTabs.ts` | Chrome Tabs API 封装:获取活动标签页、获取域名、在新标签页打开扩展页面 | | `chromeTabs.ts` | Chrome Tabs API 封装:获取活动标签页、获取域名、在新标签页打开扩展页面 |
| `clipboard.ts` | 剪贴板操作:`copyTextToClipboard`(文本)、`copyImageToClipboard`(图片) | | `clipboard.ts` | 剪贴板操作:`copyTextToClipboard`(文本)、`copyImageToClipboard`(图片) |
@@ -16,27 +16,28 @@
| `jsonFormatter.ts` | JSON 格式化/压缩:支持缩进、按键排序、minify | | `jsonFormatter.ts` | JSON 格式化/压缩:支持缩进、按键排序、minify |
| `jsonToYaml.ts` | JSON→YAML 转换 | | `jsonToYaml.ts` | JSON→YAML 转换 |
| `jsonToToml.ts` | JSON→TOML 转换 | | `jsonToToml.ts` | JSON→TOML 转换 |
| `markdownToHtml.ts` | Markdown→HTML 转换:基于 `marked` 库,支持 GFM 和换行转换 |
| `htmlToMarkdown.ts` | HTML→Markdown 转换:基于 DOMParser 解析 |
| `qrCodeParser.ts` | 二维码解析:基于 `qr-scanner` 库从文件中解析二维码 | | `qrCodeParser.ts` | 二维码解析:基于 `qr-scanner` 库从文件中解析二维码 |
| `storageCleaner.ts` | 存储清理:获取当前标签页、检测受限 URL、计算 Cookie/Storage 大小、清理操作 | | `storageCleaner.ts` | 存储清理:获取当前标签页、检测受限 URL、计算 Cookie/Storage 大小、清理操作 |
| `textStatistics.ts` | 文本统计:使用 `Intl.Segmenter` 计算字符数/单词数/行数/字节大小 | | `textStatistics.ts` | 文本统计:使用 `Intl.Segmenter` 计算字符数/单词数/行数/字节大小 |
| `format.ts` | 通用格式化:`formatBytes` 将字节转为可读字符串(B/KB/MB/GB/TB | | `format.ts` | 通用格式化:`formatBytes` 将字节转为可读字符串(B/KB/MB/GB/TB |
| `dayjs.ts` | Day.js 初始化:扩展 UTC、Timezone、RelativeTime 插件,加载中文本地化 | | `dayjs.ts` | Day.js 初始化:扩展 UTC、Timezone、RelativeTime 插件,加载中文本地化 |
| `chromeI18n.ts` | Chrome `chrome.i18n` 包装:提供 `getMessage` 和兼容 React 使用的 `useI18n` Hook |
| `ruleStorage.ts` | 测试数据生成器规则存储:基于 `localStorage` 的 CRUD、搜索、导入/导出和数量限制 |
| `dataExporter.ts` | 测试数据导出:JSON/CSV 转换、文件下载和复制到剪贴板 |
| `rightClickInjection.ts` | 右键恢复注入脚本:在页面上下文恢复 contextmenu/copy/paste 等事件默认行为 |
## 自定义 Hooks ## 自定义 Hooks
| 文件 | 用途 | | 文件 | 用途 |
| ----------------------- | -------------------------------------------------------------------------------------------------------------- | | ----------------------- | -------------------------------------------------------------------------------------------------------------- |
| `useStorageState.ts` | Chrome Storage 状态 Hook:类似 `useState`,值自动同步到 `chrome.storage`,使用 `localStorage` 快照消除首屏闪烁 | | `useStorageState.ts` | Chrome Storage 状态 Hook:类似 `useState`,值自动同步到 `chrome.storage`,使用 `localStorage` 快照消除首屏闪烁 |
| `useLazyTranslation.ts` | 懒加载翻译 Hook:按需动态导入 i18n 命名空间,支持预加载和缓存 |
| `useContextMenuData.ts` | 右键菜单数据 Hook:从 storage 读取待处理数据,匹配 featureKey 后消费并触发回调 | | `useContextMenuData.ts` | 右键菜单数据 Hook:从 storage 读取待处理数据,匹配 featureKey 后消费并触发回调 |
| `useDebounce.ts` | 防抖 Hook:对值进行延迟更新,避免频繁触发 | | `useDebounce.ts` | 防抖 Hook:对值进行延迟更新,避免频繁触发 |
## 使用约定 ## 使用约定
- 工具函数使用**命名导出**`export function xxx()` - 工具函数使用**命名导出**`export function xxx()`
- 工具函数**不抛异常**,返回包含 `hasError``error` 字段的结果对象 - 工具层不直接展示 Toast;可恢复错误返回可判断结果,需要抛出的解析/转换错误由页面 Hook 或 UI 层捕获
- Hook 使用 `use` 前缀命名,定义返回值接口类型 - Hook 使用 `use` 前缀命名,定义返回值接口类型
- 存储操作使用 `chromeStorage.ts``storageUtil` 封装,不要直接调用 `chrome.storage` - 存储操作使用 `chromeStorage.ts``storageUtil` 封装,不要直接调用 `chrome.storage`
- 消息通信使用 `messages.ts``sendMessage`/`onMessage`,不要使用原生 `chrome.runtime.sendMessage` - 消息通信使用 `messages.ts``sendMessage`/`onMessage`,不要使用原生 `chrome.runtime.sendMessage`
@@ -7,6 +7,7 @@ import {
isSupportedImageType, isSupportedImageType,
isSupportedImageExtension, isSupportedImageExtension,
extractMimeTypeFromDataUri, extractMimeTypeFromDataUri,
formatFileSize,
base64ToBytes, base64ToBytes,
sniffMimeFromBytes, sniffMimeFromBytes,
base64ToBlob, base64ToBlob,
@@ -184,6 +185,26 @@ describe('extractMimeTypeFromDataUri', () => {
}); });
}); });
describe('formatFileSize', () => {
it('应该格式化字节', () => {
expect(formatFileSize(0)).toBe('0 B');
expect(formatFileSize(512)).toBe('512 B');
});
it('应该格式化 KB', () => {
expect(formatFileSize(1024)).toBe('1.0 KB');
expect(formatFileSize(1536)).toBe('1.5 KB');
});
it('应该格式化 MB', () => {
expect(formatFileSize(1048576)).toBe('1.00 MB');
});
it('应该格式化 GB', () => {
expect(formatFileSize(1073741824)).toBe('1.00 GB');
});
});
describe('base64ToBytes', () => { describe('base64ToBytes', () => {
it('应该解码标准 ASCII Base64 为字节序列', () => { it('应该解码标准 ASCII Base64 为字节序列', () => {
const bytes = base64ToBytes('aGVsbG8='); const bytes = base64ToBytes('aGVsbG8=');
+2 -2
View File
@@ -50,7 +50,7 @@ describe('contextMenu', () => {
); );
expect(imageMenus).toHaveLength(1); expect(imageMenus).toHaveLength(1);
expect(imageMenus[0].id).toBe('qrCode-image'); expect(imageMenus[0].id).toBe('qrCode-image');
expect(imageMenus[0].title).toBe('解析图片二维码'); expect(imageMenus[0].title).toBe('🖼️ 解析图片二维码');
}); });
}); });
@@ -73,7 +73,7 @@ describe('contextMenu', () => {
expect(chrome.contextMenus.create).toHaveBeenCalledWith({ expect(chrome.contextMenus.create).toHaveBeenCalledWith({
id: 'jwt', id: 'jwt',
title: '解析 JWT', title: '🔑 解析 JWT',
contexts: ['selection'], contexts: ['selection'],
parentId: 'testing-tools-parent', parentId: 'testing-tools-parent',
}); });
+1 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { diffJson } from '../diffEngine'; import { diffJson } from '../../pages/JsonTools/diffEngine';
describe('diffJson', () => { describe('diffJson', () => {
it('should detect added property', () => { it('should detect added property', () => {
+13
View File
@@ -2,6 +2,8 @@
* Base64 转换器工具函数 * Base64 转换器工具函数
*/ */
import { formatBytes } from './format';
/** 最大文件大小限制(10 MB) */ /** 最大文件大小限制(10 MB) */
export const MAX_FILE_SIZE = 10 * 1024 * 1024; export const MAX_FILE_SIZE = 10 * 1024 * 1024;
@@ -210,6 +212,17 @@ export function extractMimeTypeFromDataUri(dataUri: string): string {
return match ? match[1] : 'application/octet-stream'; return match ? match[1] : 'application/octet-stream';
} }
/**
* 格式化文件大小显示
*
* @deprecated 直接使用 {@link formatBytes} 代替
* @param bytes 字节数
* @returns 格式化后的字符串
*/
export function formatFileSize(bytes: number): string {
return formatBytes(bytes);
}
/** /**
* Base64 解码为二进制后的产物 * Base64 解码为二进制后的产物
*/ */
+68
View File
@@ -0,0 +1,68 @@
/**
* chrome.i18n 类型安全 wrapper
* 提供与 react-i18next 兼容的接口
*/
/**
* 获取翻译文本
* @param msgId 翻译 key(如 'timestamp_pageTitle'
* @param substitutions 占位符替换值(可选)
* @returns 翻译后的文本
*/
export function getMessage(msgId: string, substitutions?: string[]): string {
try {
return chrome.i18n.getMessage(msgId, substitutions);
} catch (error) {
console.warn(`[chrome.i18n] 无法获取翻译: ${msgId}`, error);
return msgId; // 回退到 key 本身
}
}
/**
* react-i18next 兼容的 Hook
* 返回 t 函数和相关信息
*/
export function useI18n(namespace?: string | string[]) {
const namespaces = Array.isArray(namespace) ? namespace : namespace ? [namespace] : [];
const t = (key: string, options?: Record<string, unknown>): string => {
// 统一将分隔符转换为下划线,兼容 'namespace:key.path' 和 'key.path' 两种写法
const msgId = key.replace(':', '_').replace(/\./g, '_');
// 先尝试直接查找 key
let message = getMessage(msgId);
// 如果直接查找未命中(空字符串或返回 key 本身),尝试命名空间前缀(使用转换后的 msgId)
if ((!message || message === msgId) && namespaces.length > 0) {
for (const ns of namespaces) {
const candidate = `${ns}_${msgId}`;
const result = getMessage(candidate);
if (result !== candidate) {
message = result;
break;
}
}
}
if (options) {
for (const [placeholder, value] of Object.entries(options)) {
message = message.replace(`{{${placeholder}}}`, String(value));
}
}
return message;
};
return {
t,
i18n: {
language: 'zh',
changeLanguage: (_lng?: string) => {
// chrome.i18n 无法动态切换语言,需要刷新页面
console.warn('[chrome.i18n] 无法动态切换语言,需要刷新页面');
return Promise.resolve();
},
},
isLoaded: true,
};
}
+7 -7
View File
@@ -44,43 +44,43 @@ export const CONTEXT_MENU_CONFIGS: ContextMenuItemConfig[] = [
}, },
{ {
id: 'jwt', id: 'jwt',
title: '解析 JWT', title: '🔑 解析 JWT',
contexts: [chrome.contextMenus.ContextType.SELECTION], contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID, parentId: PARENT_MENU_ID,
}, },
{ {
id: 'base64Converter', id: 'base64Converter',
title: 'Base64 解码', title: '🔄 Base64 解码',
contexts: [chrome.contextMenus.ContextType.SELECTION], contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID, parentId: PARENT_MENU_ID,
}, },
{ {
id: 'textStatistics', id: 'textStatistics',
title: '统计选中文本', title: '📊 统计选中文本',
contexts: [chrome.contextMenus.ContextType.SELECTION], contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID, parentId: PARENT_MENU_ID,
}, },
{ {
id: 'timestamp', id: 'timestamp',
title: '转换时间戳', title: '转换时间戳',
contexts: [chrome.contextMenus.ContextType.SELECTION], contexts: [chrome.contextMenus.ContextType.SELECTION],
parentId: PARENT_MENU_ID, parentId: PARENT_MENU_ID,
}, },
{ {
id: 'storageCleaner', id: 'storageCleaner',
title: '清理当前网站存储', title: '🧹 清理当前网站存储',
contexts: [chrome.contextMenus.ContextType.PAGE], contexts: [chrome.contextMenus.ContextType.PAGE],
parentId: PARENT_MENU_ID, parentId: PARENT_MENU_ID,
}, },
{ {
id: 'qrCode-page', id: 'qrCode-page',
title: '网页链接转二维码', title: '🔗 网页链接转二维码',
contexts: [chrome.contextMenus.ContextType.PAGE], contexts: [chrome.contextMenus.ContextType.PAGE],
parentId: PARENT_MENU_ID, parentId: PARENT_MENU_ID,
}, },
{ {
id: 'qrCode-image', id: 'qrCode-image',
title: '解析图片二维码', title: '🖼️ 解析图片二维码',
contexts: [chrome.contextMenus.ContextType.IMAGE], contexts: [chrome.contextMenus.ContextType.IMAGE],
parentId: PARENT_MENU_ID, parentId: PARENT_MENU_ID,
}, },
+12 -7
View File
@@ -2,6 +2,8 @@
* JWT 解析工具 * JWT 解析工具
*/ */
import { getMessage } from '@/utils/chromeI18n';
interface JwtHeader { interface JwtHeader {
alg: string; alg: string;
typ?: string; typ?: string;
@@ -41,7 +43,7 @@ export function decodeBase64Url(str: string): string {
const pad = base64.length % 4; const pad = base64.length % 4;
if (pad) { if (pad) {
if (pad === 1) { if (pad === 1) {
throw new Error('无效的 Base64URL 字符串'); throw new Error(getMessage('jwt_errors_invalidBase64String'));
} }
base64 += new Array(5 - pad).join('='); base64 += new Array(5 - pad).join('=');
} }
@@ -56,9 +58,10 @@ export function decodeBase64Url(str: string): string {
const decoder = new TextDecoder('utf-8'); const decoder = new TextDecoder('utf-8');
return decoder.decode(bytes); return decoder.decode(bytes);
} catch (e) { } catch (e) {
throw new Error('Base64 解码失败: ' + (e instanceof Error ? e.message : String(e)), { throw new Error(
cause: e, getMessage('jwt_errors_failedToDecode') + (e instanceof Error ? e.message : String(e)),
}); { cause: e },
);
} }
} }
@@ -75,7 +78,7 @@ export function parseJwt(token: string): JwtResult {
payload: null, payload: null,
signature: '', signature: '',
raw: { header: '', payload: '', signature: '' }, raw: { header: '', payload: '', signature: '' },
error: 'JWT 格式无效:应包含 3 个部分(header.payload.signature', error: getMessage('jwt_errors_invalidFormat'),
}; };
} }
@@ -95,7 +98,8 @@ export function parseJwt(token: string): JwtResult {
const headerJson = decodeBase64Url(headerB64); const headerJson = decodeBase64Url(headerB64);
result.header = JSON.parse(headerJson); result.header = JSON.parse(headerJson);
} catch (e) { } catch (e) {
result.error = 'JWT Header 解析失败: ' + (e instanceof Error ? e.message : String(e)); result.error =
getMessage('jwt_errors_parseHeaderFailed') + (e instanceof Error ? e.message : String(e));
return result; return result;
} }
@@ -103,7 +107,8 @@ export function parseJwt(token: string): JwtResult {
const payloadJson = decodeBase64Url(payloadB64); const payloadJson = decodeBase64Url(payloadB64);
result.payload = JSON.parse(payloadJson); result.payload = JSON.parse(payloadJson);
} catch (e) { } catch (e) {
result.error = 'JWT Payload 解析失败: ' + (e instanceof Error ? e.message : String(e)); result.error =
getMessage('jwt_errors_parsePayloadFailed') + (e instanceof Error ? e.message : String(e));
return result; return result;
} }
+1 -1
View File
@@ -9,7 +9,7 @@ import type { DataRule, FieldConfig } from '@/types/testDataGenerator';
const STORAGE_KEY = 'testDataGenerator_rules'; const STORAGE_KEY = 'testDataGenerator_rules';
/** 最大规则数量 */ /** 最大规则数量 */
export const MAX_RULES = 20; const MAX_RULES = 20;
/** /**
* 获取所有规则 * 获取所有规则
+7 -13
View File
@@ -20,15 +20,6 @@ const CLEAN_OPTION_KEYS: (keyof StorageCleanerOptions)[] = [
'serviceWorkers', 'serviceWorkers',
]; ];
const OPTION_LABELS: Record<string, string> = {
localStorage: 'Local Storage',
sessionStorage: 'Session Storage',
indexedDB: '站点存储',
cookies: 'Cookies',
cacheStorage: 'Cache Storage',
serviceWorkers: 'Service Workers',
};
export async function getCurrentTab() { export async function getCurrentTab() {
// For popup pages, we need to get the active tab from the browser window that triggered the popup. // For popup pages, we need to get the active tab from the browser window that triggered the popup.
// We should ONLY care about the currently active tab in the last focused window. // We should ONLY care about the currently active tab in the last focused window.
@@ -371,19 +362,22 @@ export async function clearStorage(
return result; return result;
} }
export function formatCleaningResult(result: CleaningResult): string { export function formatCleaningResult(
result: CleaningResult,
t: (key: string, options?: Record<string, unknown>) => string,
): string {
const parts: string[] = []; const parts: string[] = [];
for (const key of CLEAN_OPTION_KEYS) { for (const key of CLEAN_OPTION_KEYS) {
const r = result[key]; const r = result[key];
if (r?.success && r.count > 0) { if (r?.success && r.count > 0) {
parts.push(`${r.count} ${OPTION_LABELS[key] || key}`); parts.push(`${r.count} ${t(`storageCleaner:options.${key}`)}`);
} }
} }
if (parts.length === 0) { if (parts.length === 0) {
return '该页面没有可清理的存储数据'; return t('storageCleaner:noDataToClean');
} }
return `清理了 ${parts.join(', ')}`; return t('storageCleaner:cleanedSummary', { items: parts.join(', ') });
} }
+35 -14
View File
@@ -1,6 +1,41 @@
import '@testing-library/jest-dom'; import '@testing-library/jest-dom';
import { afterEach, beforeEach, vi } from 'vitest'; import { afterEach, beforeEach, vi } from 'vitest';
import React from 'react'; import React from 'react';
import zhMessages from './public/_locales/zh_CN/messages.json';
// Type assertion to allow string indexing
const zhMessagesMap = zhMessages as Record<string, { message: string }>;
vi.mock('@/utils/chromeI18n', () => ({
useI18n: (ns?: string | string[]) => ({
t: (key: string) => {
let msgId = key;
// Handle namespace:key format
if (key.includes(':')) {
msgId = key.replace(':', '_').replace(/\./g, '_');
}
// Try direct key first
if (zhMessagesMap[msgId]) return zhMessagesMap[msgId].message;
// Try namespace prefix (using converted msgId)
if (ns) {
const namespaces = Array.isArray(ns) ? ns : [ns];
for (const n of namespaces) {
const candidate = `${n}_${msgId}`;
if (zhMessagesMap[candidate]) return zhMessagesMap[candidate].message;
}
}
return msgId;
},
i18n: {
changeLanguage: vi.fn().mockResolvedValue(undefined),
language: 'zh',
},
isLoaded: true,
}),
getMessage: (msgId: string) => zhMessagesMap[msgId]?.message ?? msgId,
getLanguage: () => 'zh',
preloadNamespaces: vi.fn().mockResolvedValue(undefined),
}));
vi.mock('@/components/CopyButton', () => ({ vi.mock('@/components/CopyButton', () => ({
CopyButton: ({ CopyButton: ({
@@ -90,20 +125,6 @@ const webExtensionMock = {
sendMessage: vi.fn().mockResolvedValue(undefined), sendMessage: vi.fn().mockResolvedValue(undefined),
create: vi.fn().mockResolvedValue({}), create: vi.fn().mockResolvedValue({}),
reload: vi.fn().mockResolvedValue(undefined), reload: vi.fn().mockResolvedValue(undefined),
onActivated: {
addListener: vi.fn(),
removeListener: vi.fn(),
},
onUpdated: {
addListener: vi.fn(),
removeListener: vi.fn(),
},
},
windows: {
onFocusChanged: {
addListener: vi.fn(),
removeListener: vi.fn(),
},
}, },
runtime: { runtime: {
id: 'test-extension-id', id: 'test-extension-id',

Some files were not shown because too many files have changed in this diff Show More