Skip to content

Commit bdd25fe

Browse files
committed
新增:在前端项目中引入防抖工具,优化提示词翻译功能,添加 README 文档以说明项目结构和工具函数使用
1 parent 376611c commit bdd25fe

File tree

7 files changed

+1299
-51
lines changed

7 files changed

+1299
-51
lines changed

frontend/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# SD Models Manager - 前端项目
2+
3+
本目录包含 SD Models Manager 的前端项目代码。
4+
5+
## 目录结构
6+
7+
```1
8+
frontend/
9+
├── public/ # 静态资源文件
10+
├── src/ # 源代码
11+
│ ├── api/ # API 接口定义
12+
│ ├── assets/ # 静态资源
13+
│ ├── components/ # Vue 组件
14+
│ ├── pages/ # 页面组件
15+
│ ├── router/ # 路由配置
16+
│ └── utils/ # 工具函数
17+
├── tests/ # 单元测试
18+
└── ... # 配置文件
19+
```
20+
21+
## 工具函数
22+
23+
### 防抖工具 (debounce)
24+
25+
位于 `src/utils/debounce.ts`,提供两种防抖实现:
26+
27+
#### 1. useDebounce - 组合式API风格的防抖函数
28+
29+
提供完整的状态和控制能力,适合需要监视防抖状态的场景。
30+
31+
```typescript
32+
import { useDebounce } from '../utils/debounce';
33+
34+
// 在组件中使用
35+
setup() {
36+
// 创建防抖函数,延迟800ms
37+
const translateDebounce = useDebounce(async () => {
38+
// 这里是需要延迟执行的代码
39+
await fetchTranslation(text);
40+
}, 800);
41+
42+
// 触发防抖
43+
function handleInput() {
44+
translateDebounce.triggerDebounce();
45+
}
46+
47+
// 监听防抖状态
48+
const isLoading = computed(() => {
49+
return translateDebounce.isActive.value || translateDebounce.isExecuting.value;
50+
});
51+
52+
// 组件卸载时取消防抖
53+
onBeforeUnmount(() => {
54+
translateDebounce.cancel();
55+
});
56+
57+
return {
58+
handleInput,
59+
isLoading,
60+
// 可以直接返回防抖状态
61+
isTranslating: translateDebounce.isActive
62+
};
63+
}
64+
```
65+
66+
#### 2. debounce - 简单函数式防抖
67+
68+
适合简单场景,无需监视状态。
69+
70+
```typescript
71+
import { debounce } from '../utils/debounce';
72+
73+
// 创建防抖函数
74+
const debouncedSearch = debounce((query) => {
75+
searchAPI(query);
76+
}, 500);
77+
78+
// 调用防抖函数
79+
function handleSearch(query) {
80+
debouncedSearch(query);
81+
}
82+
```
83+
84+
### 防抖状态说明
85+
86+
`useDebounce` 提供的状态:
87+
88+
- `isActive`: 防抖是否处于等待执行阶段(延迟时间内)
89+
- `isExecuting`: 回调函数是否正在执行中(特别适用于异步函数)
90+
91+
这两个状态可用于UI展示,例如显示"准备中..."或"执行中..."的提示。

0 commit comments

Comments
 (0)