@qy_better_lib/hooks 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DOCUMENTATION.md +704 -0
- package/USAGE.md +444 -0
- package/__tests__/use-chart/index.test.ts +287 -0
- package/__tests__/use-emit/index.test.ts +248 -0
- package/__tests__/use-fullscreen/index.test.ts +162 -0
- package/__tests__/use-image/index.test.ts +230 -0
- package/__tests__/use-layout-flow/index.test.ts +382 -0
- package/__tests__/use-mqtt/index.test.ts +392 -0
- package/__tests__/use-print/index.test.ts +378 -0
- package/__tests__/use-watermark/index.test.ts +277 -0
- package/__tests__/use-websocket/index.test.ts +402 -0
- package/dist/hooks.min.js +76 -0
- package/lib/_virtual/dayjs.min.js +2 -0
- package/lib/_virtual/dayjs.min2.js +4 -0
- package/lib/index.d.ts +5 -2
- package/lib/index.js +20 -31
- package/lib/node_modules/dayjs/dayjs.min.js +286 -0
- package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/directives/click_outside.js +19 -0
- package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/dom.js +25 -0
- package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/echarts.js +166 -0
- package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/file.js +55 -0
- package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/is.js +8 -0
- package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/object.js +83 -0
- package/lib/packages/hooks/node_modules/@qy_better_lib/core/lib/utils/random.js +11 -0
- package/lib/use-chart/config.d.ts +2 -3
- package/lib/use-chart/config.js +80 -0
- package/lib/use-chart/index.d.ts +5 -13
- package/lib/use-chart/index.js +199 -0
- package/lib/use-chart/type.d.ts +92 -4
- package/lib/use-emit/extend.d.ts +2 -1
- package/lib/use-emit/extend.js +34 -15
- package/lib/use-emit/index.d.ts +2 -13
- package/lib/use-emit/index.js +22 -17
- package/lib/use-emit/type.d.ts +16 -0
- package/lib/use-fullscreen/index.d.ts +23 -0
- package/lib/use-fullscreen/index.js +53 -0
- package/lib/use-image/index.d.ts +18 -52
- package/lib/use-image/index.js +189 -67
- package/lib/use-image/type.d.ts +8 -10
- package/lib/use-image/type.js +7 -6
- package/lib/use-layout-flow/index.d.ts +14 -40
- package/lib/use-layout-flow/index.js +286 -0
- package/lib/use-layout-flow/type.d.ts +46 -0
- package/lib/use-mqtt/index.d.ts +9 -18
- package/lib/use-mqtt/index.js +179 -0
- package/lib/use-mqtt/type.d.ts +78 -0
- package/lib/use-print/index.d.ts +5 -9
- package/lib/use-print/index.js +274 -40
- package/lib/use-print/type.d.ts +58 -0
- package/lib/use-watermark/index.d.ts +7 -0
- package/lib/use-watermark/index.js +134 -0
- package/lib/use-watermark/type.d.ts +55 -0
- package/lib/use-websocket/index.d.ts +6 -13
- package/lib/use-websocket/index.js +192 -39
- package/lib/use-websocket/type.d.ts +54 -0
- package/package.json +9 -3
- package/dist/@qy_better_lib/hooks.min.js +0 -15
- package/lib/use-chart/utils.d.ts +0 -7
- package/lib/use-file/index.d.ts +0 -14
- package/lib/use-file/index.js +0 -26
- package/lib/use-image/canvastoDataURL.d.ts +0 -11
- package/lib/use-image/canvastoDataURL.js +0 -7
- package/lib/use-image/canvastoFile.d.ts +0 -11
- package/lib/use-image/canvastoFile.js +0 -9
- package/lib/use-image/dataURLtoFile.d.ts +0 -10
- package/lib/use-image/dataURLtoFile.js +0 -16
- package/lib/use-image/dataURLtoImage.d.ts +0 -7
- package/lib/use-image/dataURLtoImage.js +0 -9
- package/lib/use-image/downloadFile.d.ts +0 -7
- package/lib/use-image/downloadFile.js +0 -9
- package/lib/use-image/filetoDataURL.d.ts +0 -7
- package/lib/use-image/filetoDataURL.js +0 -9
- package/lib/use-image/imagetoCanvas.d.ts +0 -26
- package/lib/use-image/imagetoCanvas.js +0 -41
- package/lib/use-image/urltoBlob.d.ts +0 -8
- package/lib/use-image/urltoBlob.js +0 -6
- package/lib/use-image/urltoImage.d.ts +0 -7
- package/lib/use-image/urltoImage.js +0 -13
- package/lib/use-utils/index.d.ts +0 -1
- package/lib/use-utils/use-fullscreen.d.ts +0 -9
- package/lib/use-waterMark/index.d.ts +0 -17
- package/lib/use-waterMark/index.js +0 -29
package/DOCUMENTATION.md
ADDED
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
# QY Better Lib Hooks - 使用文档
|
|
2
|
+
|
|
3
|
+
本文档详细介绍了 QY Better Lib Hooks 包中的各种hooks,包括功能描述、参数说明、返回类型和使用示例。
|
|
4
|
+
|
|
5
|
+
## hooks函数列表
|
|
6
|
+
|
|
7
|
+
### 1. use-print - 打印功能
|
|
8
|
+
|
|
9
|
+
#### 功能描述
|
|
10
|
+
适用于需要在前端实现打印功能的场景,支持打印HTML内容和DOM元素,包括打印预览功能。
|
|
11
|
+
|
|
12
|
+
#### 方法列表
|
|
13
|
+
|
|
14
|
+
##### print_html
|
|
15
|
+
- **功能**: 打印指定DOM选择器的内容
|
|
16
|
+
- **参数**:
|
|
17
|
+
- `dom_selector: string` - DOM节点选择器
|
|
18
|
+
- `options?: PrintOptions` - 打印选项
|
|
19
|
+
- **返回类型**: `Promise<void>`
|
|
20
|
+
- **示例**:
|
|
21
|
+
```typescript
|
|
22
|
+
import { use_print } from '@qy_better_lib/hooks';
|
|
23
|
+
|
|
24
|
+
const { print_html } = use_print();
|
|
25
|
+
|
|
26
|
+
// 打印ID为content的元素
|
|
27
|
+
await print_html('#content');
|
|
28
|
+
|
|
29
|
+
// 打印并预览
|
|
30
|
+
await print_html('#content', { preview: true });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
##### print_html_element
|
|
34
|
+
- **功能**: 打印多个DOM元素
|
|
35
|
+
- **参数**:
|
|
36
|
+
- `element_array: Element[]` - DOM元素数组
|
|
37
|
+
- `options?: PrintOptions` - 打印选项
|
|
38
|
+
- **返回类型**: `Promise<void>`
|
|
39
|
+
- **示例**:
|
|
40
|
+
```typescript
|
|
41
|
+
import { use_print } from '@qy_better_lib/hooks';
|
|
42
|
+
|
|
43
|
+
const { print_html_element } = use_print();
|
|
44
|
+
|
|
45
|
+
// 获取多个元素并打印
|
|
46
|
+
const elements = document.querySelectorAll('.printable');
|
|
47
|
+
await print_html_element(Array.from(elements));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
##### preview_html
|
|
51
|
+
- **功能**: 预览指定DOM选择器的内容
|
|
52
|
+
- **参数**:
|
|
53
|
+
- `dom_selector: string` - DOM节点选择器
|
|
54
|
+
- `options?: PrintOptions` - 打印选项
|
|
55
|
+
- **返回类型**: `void`
|
|
56
|
+
- **示例**:
|
|
57
|
+
```typescript
|
|
58
|
+
import { use_print } from '@qy_better_lib/hooks';
|
|
59
|
+
|
|
60
|
+
const { preview_html } = use_print();
|
|
61
|
+
|
|
62
|
+
// 预览ID为content的元素
|
|
63
|
+
preview_html('#content');
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
##### preview_html_element
|
|
67
|
+
- **功能**: 预览多个DOM元素
|
|
68
|
+
- **参数**:
|
|
69
|
+
- `element_array: Element[]` - DOM元素数组
|
|
70
|
+
- `options?: PrintOptions` - 打印选项
|
|
71
|
+
- **返回类型**: `void`
|
|
72
|
+
- **示例**:
|
|
73
|
+
```typescript
|
|
74
|
+
import { use_print } from '@qy_better_lib/hooks';
|
|
75
|
+
|
|
76
|
+
const { preview_html_element } = use_print();
|
|
77
|
+
|
|
78
|
+
// 获取多个元素并预览
|
|
79
|
+
const elements = document.querySelectorAll('.printable');
|
|
80
|
+
preview_html_element(Array.from(elements));
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
##### close_preview
|
|
84
|
+
- **功能**: 关闭预览
|
|
85
|
+
- **返回类型**: `void`
|
|
86
|
+
- **示例**:
|
|
87
|
+
```typescript
|
|
88
|
+
import { use_print } from '@qy_better_lib/hooks';
|
|
89
|
+
|
|
90
|
+
const { close_preview } = use_print();
|
|
91
|
+
|
|
92
|
+
// 关闭预览
|
|
93
|
+
close_preview();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 2. use-fullscreen - 全屏功能
|
|
97
|
+
|
|
98
|
+
#### 功能描述
|
|
99
|
+
适用于需要在前端实现全屏功能的场景,支持切换、进入和退出全屏。
|
|
100
|
+
|
|
101
|
+
#### 参数
|
|
102
|
+
- `selector: string` - 目标元素选择器
|
|
103
|
+
|
|
104
|
+
#### 返回值
|
|
105
|
+
- **full**: `Ref<boolean>` - 是否全屏
|
|
106
|
+
- **container**: `ShallowRef<Element | null>` - 目标元素
|
|
107
|
+
- **toggle_fullscreen**: `() => void` - 切换全屏状态
|
|
108
|
+
- **enter_fullscreen**: `() => void` - 进入全屏
|
|
109
|
+
- **exit_fullscreen**: `() => void` - 退出全屏
|
|
110
|
+
|
|
111
|
+
#### 示例
|
|
112
|
+
```typescript
|
|
113
|
+
import { use_fullscreen } from '@qy_better_lib/hooks';
|
|
114
|
+
|
|
115
|
+
const { full, toggle_fullscreen, enter_fullscreen, exit_fullscreen } = use_fullscreen('#container');
|
|
116
|
+
|
|
117
|
+
// 切换全屏状态
|
|
118
|
+
toggle_fullscreen();
|
|
119
|
+
|
|
120
|
+
// 进入全屏
|
|
121
|
+
enter_fullscreen();
|
|
122
|
+
|
|
123
|
+
// 退出全屏
|
|
124
|
+
exit_fullscreen();
|
|
125
|
+
|
|
126
|
+
// 监听全屏状态变化
|
|
127
|
+
console.log(full.value); // 是否全屏
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 3. use-watermark - 水印功能
|
|
131
|
+
|
|
132
|
+
#### 功能描述
|
|
133
|
+
适用于需要在前端添加水印的场景,支持创建、移除、更新水印,包括响应式水印。
|
|
134
|
+
|
|
135
|
+
#### 方法列表
|
|
136
|
+
|
|
137
|
+
##### create_water_mark
|
|
138
|
+
- **功能**: 创建水印
|
|
139
|
+
- **参数**:
|
|
140
|
+
- `options: WaterMarkOptions` - 水印配置选项
|
|
141
|
+
- **返回类型**: `void`
|
|
142
|
+
- **示例**:
|
|
143
|
+
```typescript
|
|
144
|
+
import { use_water_mark } from '@qy_better_lib/hooks';
|
|
145
|
+
|
|
146
|
+
const { create_water_mark } = use_water_mark();
|
|
147
|
+
|
|
148
|
+
// 创建水印
|
|
149
|
+
create_water_mark({
|
|
150
|
+
text1: '测试水印',
|
|
151
|
+
text2: '2024-01-01',
|
|
152
|
+
font_size: 16,
|
|
153
|
+
font_family: 'Arial',
|
|
154
|
+
rotate: -15,
|
|
155
|
+
text_color: '#999',
|
|
156
|
+
opacity: 0.1,
|
|
157
|
+
responsive: true
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
##### remove_water_mark
|
|
162
|
+
- **功能**: 移除水印
|
|
163
|
+
- **返回类型**: `void`
|
|
164
|
+
- **示例**:
|
|
165
|
+
```typescript
|
|
166
|
+
import { use_water_mark } from '@qy_better_lib/hooks';
|
|
167
|
+
|
|
168
|
+
const { remove_water_mark } = use_water_mark();
|
|
169
|
+
|
|
170
|
+
// 移除水印
|
|
171
|
+
remove_water_mark();
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
##### update_water_mark
|
|
175
|
+
- **功能**: 更新水印
|
|
176
|
+
- **参数**:
|
|
177
|
+
- `options: Partial<WaterMarkOptions>` - 水印配置选项
|
|
178
|
+
- **返回类型**: `void`
|
|
179
|
+
- **示例**:
|
|
180
|
+
```typescript
|
|
181
|
+
import { use_water_mark } from '@qy_better_lib/hooks';
|
|
182
|
+
|
|
183
|
+
const { update_water_mark } = use_water_mark();
|
|
184
|
+
|
|
185
|
+
// 更新水印
|
|
186
|
+
update_water_mark({
|
|
187
|
+
text1: '更新后的水印',
|
|
188
|
+
opacity: 0.15
|
|
189
|
+
});
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
##### has_water_mark
|
|
193
|
+
- **功能**: 检查水印是否存在
|
|
194
|
+
- **返回类型**: `boolean` - 是否存在水印
|
|
195
|
+
- **示例**:
|
|
196
|
+
```typescript
|
|
197
|
+
import { use_water_mark } from '@qy_better_lib/hooks';
|
|
198
|
+
|
|
199
|
+
const { has_water_mark } = use_water_mark();
|
|
200
|
+
|
|
201
|
+
// 检查水印是否存在
|
|
202
|
+
const exists = has_water_mark();
|
|
203
|
+
console.log(exists);
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### 4. use-websocket - WebSocket功能
|
|
207
|
+
|
|
208
|
+
#### 功能描述
|
|
209
|
+
适用于需要在前端实现WebSocket通信的场景,支持自动重连、心跳检测、消息队列等功能。
|
|
210
|
+
|
|
211
|
+
#### 参数
|
|
212
|
+
- `options: WebSocketOptions` - WebSocket配置选项
|
|
213
|
+
|
|
214
|
+
#### 返回值
|
|
215
|
+
- **ws**: `WebSocket | undefined` - WebSocket实例
|
|
216
|
+
- **create**: `() => void` - 创建连接
|
|
217
|
+
- **close**: `() => void` - 关闭连接
|
|
218
|
+
- **send_message**: `(msg: any) => boolean` - 发送消息
|
|
219
|
+
- **get_status**: `() => WebSocketStatus` - 获取连接状态
|
|
220
|
+
- **reconnect**: `() => void` - 重连
|
|
221
|
+
- **start_heartbeat**: `() => void` - 启动心跳检测
|
|
222
|
+
- **stop_heartbeat**: `() => void` - 停止心跳检测
|
|
223
|
+
|
|
224
|
+
#### 示例
|
|
225
|
+
```typescript
|
|
226
|
+
import { use_web_socket } from '@qy_better_lib/hooks';
|
|
227
|
+
|
|
228
|
+
const { create, close, send_message, get_status } = use_web_socket({
|
|
229
|
+
server: 'ws://localhost:8080',
|
|
230
|
+
receive: (event) => {
|
|
231
|
+
console.log('收到消息:', event.data);
|
|
232
|
+
},
|
|
233
|
+
on_open: () => {
|
|
234
|
+
console.log('连接成功');
|
|
235
|
+
},
|
|
236
|
+
on_error: (error) => {
|
|
237
|
+
console.error('连接错误:', error);
|
|
238
|
+
},
|
|
239
|
+
on_close: () => {
|
|
240
|
+
console.log('连接关闭');
|
|
241
|
+
},
|
|
242
|
+
auto_reconnect: true,
|
|
243
|
+
heartbeat_interval: 30000
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// 发送消息
|
|
247
|
+
send_message('Hello WebSocket');
|
|
248
|
+
|
|
249
|
+
// 获取连接状态
|
|
250
|
+
const status = get_status();
|
|
251
|
+
console.log(status);
|
|
252
|
+
|
|
253
|
+
// 关闭连接
|
|
254
|
+
close();
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### 5. use-mqtt - MQTT功能
|
|
258
|
+
|
|
259
|
+
#### 功能描述
|
|
260
|
+
适用于需要MQTT通信的场景,支持连接管理、消息订阅和发布。
|
|
261
|
+
|
|
262
|
+
#### 参数
|
|
263
|
+
- `url: string` - MQTT broker地址
|
|
264
|
+
- `options: any` - MQTT连接选项
|
|
265
|
+
|
|
266
|
+
#### 返回值
|
|
267
|
+
- **status**: `MqttConnectionStatus` - 连接状态
|
|
268
|
+
- **connect**: `({ success, error, receive }: MqttConnectOptions) => void` - 连接MQTT
|
|
269
|
+
- **close**: `(callback?: MqttCloseCallback) => void` - 关闭MQTT连接
|
|
270
|
+
- **subscribe**: `(topic: string | string[], options?: any, callback?: MqttSubscribeCallback) => void` - 订阅主题
|
|
271
|
+
- **unsubscribe**: `(topic: string | string[], callback?: MqttUnsubscribeCallback) => void` - 取消订阅
|
|
272
|
+
- **publish**: `(topic: string, message: string | Buffer, options?: any, callback?: MqttPublishCallback) => void` - 发布消息
|
|
273
|
+
|
|
274
|
+
#### 示例
|
|
275
|
+
```typescript
|
|
276
|
+
import { use_mqtt } from '@qy_better_lib/hooks';
|
|
277
|
+
|
|
278
|
+
const { connect, close, subscribe, publish } = use_mqtt('mqtt://localhost:1883', {
|
|
279
|
+
clientId: 'test-client',
|
|
280
|
+
reconnectPeriod: 5000
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// 连接MQTT
|
|
284
|
+
connect({
|
|
285
|
+
success: () => {
|
|
286
|
+
console.log('MQTT连接成功');
|
|
287
|
+
// 订阅主题
|
|
288
|
+
subscribe('test/topic', {}, (err, granted) => {
|
|
289
|
+
if (!err) {
|
|
290
|
+
console.log('订阅成功:', granted);
|
|
291
|
+
// 发布消息
|
|
292
|
+
publish('test/topic', 'Hello MQTT');
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
},
|
|
296
|
+
error: (err) => {
|
|
297
|
+
console.error('MQTT连接错误:', err);
|
|
298
|
+
},
|
|
299
|
+
receive: (topic, message) => {
|
|
300
|
+
console.log('收到消息:', topic, message.toString());
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// 关闭连接
|
|
305
|
+
close(() => {
|
|
306
|
+
console.log('MQTT连接已关闭');
|
|
307
|
+
});
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### 6. use-image - 图片处理功能
|
|
311
|
+
|
|
312
|
+
#### 功能描述
|
|
313
|
+
适用于需要在前端处理图片的场景,支持图片压缩、转换、下载等功能。
|
|
314
|
+
|
|
315
|
+
#### 方法列表
|
|
316
|
+
|
|
317
|
+
##### compress
|
|
318
|
+
- **功能**: 压缩File(Blob)对象
|
|
319
|
+
- **参数**:
|
|
320
|
+
- `file: File | Blob` - 一个File(Blob)对象
|
|
321
|
+
- `config?: ICompressConfig` - 压缩配置
|
|
322
|
+
- **返回类型**: `Promise<Blob>` - 压缩后的Blob对象
|
|
323
|
+
- **示例**:
|
|
324
|
+
```typescript
|
|
325
|
+
import { use_image } from '@qy_better_lib/hooks';
|
|
326
|
+
|
|
327
|
+
const { compress } = use_image();
|
|
328
|
+
|
|
329
|
+
// 压缩图片
|
|
330
|
+
const compressedFile = await compress(file, {
|
|
331
|
+
quality: 0.8,
|
|
332
|
+
width: 800,
|
|
333
|
+
height: 600
|
|
334
|
+
});
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
##### compress_accurately
|
|
338
|
+
- **功能**: 根据体积压缩File(Blob)对象
|
|
339
|
+
- **参数**:
|
|
340
|
+
- `file: File | Blob` - 一个File(Blob)对象
|
|
341
|
+
- `config?: CompressAccuratelyConfig` - 压缩配置
|
|
342
|
+
- **返回类型**: `Promise<Blob>` - 压缩后的Blob对象
|
|
343
|
+
- **示例**:
|
|
344
|
+
```typescript
|
|
345
|
+
import { use_image } from '@qy_better_lib/hooks';
|
|
346
|
+
|
|
347
|
+
const { compress_accurately } = use_image();
|
|
348
|
+
|
|
349
|
+
// 压缩图片到指定大小(100KB)
|
|
350
|
+
const compressedFile = await compress_accurately(file, {
|
|
351
|
+
size: 100, // 目标大小,单位KB
|
|
352
|
+
accuracy: 0.9 // 精度
|
|
353
|
+
});
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
##### compress_image
|
|
357
|
+
- **功能**: 使用core中的图片压缩功能
|
|
358
|
+
- **参数**:
|
|
359
|
+
- `file: File` - 图片文件
|
|
360
|
+
- `max_width: number` - 最大宽度,默认 800
|
|
361
|
+
- `max_height: number` - 最大高度,默认 800
|
|
362
|
+
- `quality: number` - 压缩质量,默认 0.8
|
|
363
|
+
- **返回类型**: `Promise<File | undefined>` - 压缩后的文件
|
|
364
|
+
- **示例**:
|
|
365
|
+
```typescript
|
|
366
|
+
import { use_image } from '@qy_better_lib/hooks';
|
|
367
|
+
|
|
368
|
+
const { compress_image } = use_image();
|
|
369
|
+
|
|
370
|
+
// 压缩图片
|
|
371
|
+
const compressedFile = await compress_image(file, 1024, 768, 0.7);
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
##### download_file
|
|
375
|
+
- **功能**: 下载文件
|
|
376
|
+
- **参数**:
|
|
377
|
+
- `blob: Blob` - Blob对象
|
|
378
|
+
- `file_name: string` - 文件名,默认 'download'
|
|
379
|
+
- **返回类型**: `void`
|
|
380
|
+
- **示例**:
|
|
381
|
+
```typescript
|
|
382
|
+
import { use_image } from '@qy_better_lib/hooks';
|
|
383
|
+
|
|
384
|
+
const { download_file } = use_image();
|
|
385
|
+
|
|
386
|
+
// 下载图片
|
|
387
|
+
download_file(blob, 'image.jpg');
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### 7. use-chart - 图表管理
|
|
391
|
+
|
|
392
|
+
#### 功能描述
|
|
393
|
+
适用于需要在前端使用ECharts的场景,支持图表渲染、主题切换、响应式设计等功能。
|
|
394
|
+
|
|
395
|
+
#### 参数
|
|
396
|
+
- `options?: UseChartOptions` - 全局配置选项
|
|
397
|
+
|
|
398
|
+
#### 方法列表
|
|
399
|
+
|
|
400
|
+
##### render_chart
|
|
401
|
+
- **功能**: 渲染图表
|
|
402
|
+
- **参数**:
|
|
403
|
+
- `option: Record<string, any>` - 图表配置
|
|
404
|
+
- `dom_id: string` - DOM元素选择器
|
|
405
|
+
- `render_options?: Partial<UseChartOptions>` - 渲染选项
|
|
406
|
+
- **返回类型**: `Promise<any>` - 图表实例
|
|
407
|
+
- **示例**:
|
|
408
|
+
```typescript
|
|
409
|
+
import { use_chart } from '@qy_better_lib/hooks';
|
|
410
|
+
|
|
411
|
+
const { render_chart } = use_chart();
|
|
412
|
+
|
|
413
|
+
// 渲染图表
|
|
414
|
+
const chart = await render_chart({
|
|
415
|
+
title: {
|
|
416
|
+
text: '测试图表'
|
|
417
|
+
},
|
|
418
|
+
xAxis: {
|
|
419
|
+
type: 'category',
|
|
420
|
+
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
|
421
|
+
},
|
|
422
|
+
yAxis: {
|
|
423
|
+
type: 'value'
|
|
424
|
+
},
|
|
425
|
+
series: [{
|
|
426
|
+
data: [120, 200, 150, 80, 70, 110, 130],
|
|
427
|
+
type: 'bar'
|
|
428
|
+
}]
|
|
429
|
+
}, '#chart-container');
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
##### update_chart
|
|
433
|
+
- **功能**: 更新图表配置
|
|
434
|
+
- **参数**:
|
|
435
|
+
- `dom_id: string` - DOM元素选择器
|
|
436
|
+
- `option: Record<string, any>` - 图表配置
|
|
437
|
+
- **返回类型**: `boolean` - 是否更新成功
|
|
438
|
+
- **示例**:
|
|
439
|
+
```typescript
|
|
440
|
+
import { use_chart } from '@qy_better_lib/hooks';
|
|
441
|
+
|
|
442
|
+
const { update_chart } = use_chart();
|
|
443
|
+
|
|
444
|
+
// 更新图表
|
|
445
|
+
update_chart('#chart-container', {
|
|
446
|
+
series: [{
|
|
447
|
+
data: [200, 300, 250, 180, 170, 210, 230],
|
|
448
|
+
type: 'bar'
|
|
449
|
+
}]
|
|
450
|
+
});
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
##### remove_chart
|
|
454
|
+
- **功能**: 删除图表
|
|
455
|
+
- **参数**:
|
|
456
|
+
- `dom_id: string` - DOM元素选择器
|
|
457
|
+
- **返回类型**: `boolean` - 是否删除成功
|
|
458
|
+
- **示例**:
|
|
459
|
+
```typescript
|
|
460
|
+
import { use_chart } from '@qy_better_lib/hooks';
|
|
461
|
+
|
|
462
|
+
const { remove_chart } = use_chart();
|
|
463
|
+
|
|
464
|
+
// 删除图表
|
|
465
|
+
remove_chart('#chart-container');
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
##### export_chart_image
|
|
469
|
+
- **功能**: 导出图表为图片
|
|
470
|
+
- **参数**:
|
|
471
|
+
- `dom_id: string` - DOM元素选择器
|
|
472
|
+
- `options?: any` - 导出选项
|
|
473
|
+
- **返回类型**: `string | null` - 图片URL
|
|
474
|
+
- **示例**:
|
|
475
|
+
```typescript
|
|
476
|
+
import { use_chart } from '@qy_better_lib/hooks';
|
|
477
|
+
|
|
478
|
+
const { export_chart_image } = use_chart();
|
|
479
|
+
|
|
480
|
+
// 导出图表为图片
|
|
481
|
+
const imageUrl = export_chart_image('#chart-container');
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
### 8. use-layout-flow - 流程图布局
|
|
485
|
+
|
|
486
|
+
#### 功能描述
|
|
487
|
+
适用于需要自动布局的流程图场景,支持多种布局方向和自定义元素。
|
|
488
|
+
|
|
489
|
+
#### 参数
|
|
490
|
+
- `options: LayoutFlow` - 布局配置选项
|
|
491
|
+
|
|
492
|
+
#### 方法列表
|
|
493
|
+
|
|
494
|
+
##### init
|
|
495
|
+
- **功能**: 初始化画布
|
|
496
|
+
- **参数**:
|
|
497
|
+
- `container: HTMLElement` - 容器元素
|
|
498
|
+
- **返回类型**: `Promise<boolean>` - 是否初始化成功
|
|
499
|
+
- **示例**:
|
|
500
|
+
```typescript
|
|
501
|
+
import { use_layout_flow } from '@qy_better_lib/hooks';
|
|
502
|
+
|
|
503
|
+
const { init } = use_layout_flow({});
|
|
504
|
+
|
|
505
|
+
// 初始化画布
|
|
506
|
+
const container = document.getElementById('flow-container');
|
|
507
|
+
await init(container);
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
##### layout
|
|
511
|
+
- **功能**: 自动布局
|
|
512
|
+
- **参数**:
|
|
513
|
+
- `cfg: LayoutFlowNodeOption` - 布局配置
|
|
514
|
+
- `dir: LayoutDirection` - 布局方向,默认为 'LR'(从左到右)
|
|
515
|
+
- **返回类型**: `Promise<boolean>` - 是否布局成功
|
|
516
|
+
- **示例**:
|
|
517
|
+
```typescript
|
|
518
|
+
import { use_layout_flow } from '@qy_better_lib/hooks';
|
|
519
|
+
|
|
520
|
+
const { layout } = use_layout_flow({});
|
|
521
|
+
|
|
522
|
+
// 执行布局
|
|
523
|
+
await layout({
|
|
524
|
+
node_w: 120,
|
|
525
|
+
node_h: 60,
|
|
526
|
+
nodesep: 50,
|
|
527
|
+
ranksep: 50
|
|
528
|
+
}, 'LR');
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
##### create_nodes
|
|
532
|
+
- **功能**: 创建节点
|
|
533
|
+
- **参数**:
|
|
534
|
+
- `node_options: any[]` - 节点配置数组
|
|
535
|
+
- **返回类型**: `any[]` - 创建的节点数组
|
|
536
|
+
- **示例**:
|
|
537
|
+
```typescript
|
|
538
|
+
import { use_layout_flow } from '@qy_better_lib/hooks';
|
|
539
|
+
|
|
540
|
+
const { create_nodes } = use_layout_flow({});
|
|
541
|
+
|
|
542
|
+
// 创建节点
|
|
543
|
+
const nodes = create_nodes([
|
|
544
|
+
{
|
|
545
|
+
id: 'node1',
|
|
546
|
+
x: 100,
|
|
547
|
+
y: 100,
|
|
548
|
+
width: 120,
|
|
549
|
+
height: 60,
|
|
550
|
+
label: '节点1'
|
|
551
|
+
},
|
|
552
|
+
{
|
|
553
|
+
id: 'node2',
|
|
554
|
+
x: 300,
|
|
555
|
+
y: 100,
|
|
556
|
+
width: 120,
|
|
557
|
+
height: 60,
|
|
558
|
+
label: '节点2'
|
|
559
|
+
}
|
|
560
|
+
]);
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
##### create_edges
|
|
564
|
+
- **功能**: 创建连线
|
|
565
|
+
- **参数**:
|
|
566
|
+
- `edge_options: any[]` - 边配置数组
|
|
567
|
+
- **返回类型**: `any[]` - 创建的边数组
|
|
568
|
+
- **示例**:
|
|
569
|
+
```typescript
|
|
570
|
+
import { use_layout_flow } from '@qy_better_lib/hooks';
|
|
571
|
+
|
|
572
|
+
const { create_edges } = use_layout_flow({});
|
|
573
|
+
|
|
574
|
+
// 创建连线
|
|
575
|
+
const edges = create_edges([
|
|
576
|
+
{
|
|
577
|
+
source_id: 'node1',
|
|
578
|
+
target_id: 'node2',
|
|
579
|
+
shape: 'edge'
|
|
580
|
+
}
|
|
581
|
+
]);
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
### 9. use-emit - 事件总线(异步支持)
|
|
585
|
+
|
|
586
|
+
#### 功能描述
|
|
587
|
+
适用于需要在前端使用事件发射的场景,基于mitt库实现,支持异步事件和自动清理监听器。
|
|
588
|
+
|
|
589
|
+
#### 参数
|
|
590
|
+
- `event_list?: Array<EventItem>` - 事件列表
|
|
591
|
+
|
|
592
|
+
#### 返回值
|
|
593
|
+
- **emitter**: `ExtendEmitter` - 事件发射器
|
|
594
|
+
|
|
595
|
+
#### 示例
|
|
596
|
+
```typescript
|
|
597
|
+
import { use_emit } from '@qy_better_lib/hooks';
|
|
598
|
+
|
|
599
|
+
const { emitter } = use_emit([
|
|
600
|
+
{
|
|
601
|
+
key: 'test',
|
|
602
|
+
value: (data) => {
|
|
603
|
+
console.log('收到test事件:', data);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
]);
|
|
607
|
+
|
|
608
|
+
// 发射事件
|
|
609
|
+
emitter.emit('test', { message: 'Hello' });
|
|
610
|
+
|
|
611
|
+
// 监听事件
|
|
612
|
+
emitter.on('test', (data) => {
|
|
613
|
+
console.log('监听test事件:', data);
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
// 移除监听
|
|
617
|
+
emitter.off('test', listener);
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
### 10. use-websocket - WebSocket功能
|
|
621
|
+
|
|
622
|
+
#### 功能描述
|
|
623
|
+
适用于需要在前端实现WebSocket通信的场景,支持自动重连、心跳检测、消息队列等功能。
|
|
624
|
+
|
|
625
|
+
#### 参数
|
|
626
|
+
- `options: WebSocketOptions` - WebSocket配置选项
|
|
627
|
+
|
|
628
|
+
#### 返回值
|
|
629
|
+
- **ws**: `WebSocket | undefined` - WebSocket实例
|
|
630
|
+
- **create**: `() => void` - 创建连接
|
|
631
|
+
- **close**: `() => void` - 关闭连接
|
|
632
|
+
- **send_message**: `(msg: any) => boolean` - 发送消息
|
|
633
|
+
- **get_status**: `() => WebSocketStatus` - 获取连接状态
|
|
634
|
+
- **reconnect**: `() => void` - 重连
|
|
635
|
+
- **start_heartbeat**: `() => void` - 启动心跳检测
|
|
636
|
+
- **stop_heartbeat**: `() => void` - 停止心跳检测
|
|
637
|
+
|
|
638
|
+
#### 示例
|
|
639
|
+
```typescript
|
|
640
|
+
import { use_web_socket } from '@qy_better_lib/hooks';
|
|
641
|
+
|
|
642
|
+
const { create, close, send_message, get_status } = use_web_socket({
|
|
643
|
+
server: 'ws://localhost:8080',
|
|
644
|
+
receive: (event) => {
|
|
645
|
+
console.log('收到消息:', event.data);
|
|
646
|
+
},
|
|
647
|
+
on_open: () => {
|
|
648
|
+
console.log('连接成功');
|
|
649
|
+
},
|
|
650
|
+
on_error: (error) => {
|
|
651
|
+
console.error('连接错误:', error);
|
|
652
|
+
},
|
|
653
|
+
on_close: () => {
|
|
654
|
+
console.log('连接关闭');
|
|
655
|
+
},
|
|
656
|
+
auto_reconnect: true,
|
|
657
|
+
heartbeat_interval: 30000
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
// 发送消息
|
|
661
|
+
send_message('Hello WebSocket');
|
|
662
|
+
|
|
663
|
+
// 获取连接状态
|
|
664
|
+
const status = get_status();
|
|
665
|
+
console.log(status);
|
|
666
|
+
|
|
667
|
+
// 关闭连接
|
|
668
|
+
close();
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
## 安装和使用
|
|
673
|
+
|
|
674
|
+
### 安装
|
|
675
|
+
```bash
|
|
676
|
+
npm install @qy_better_lib/hooks
|
|
677
|
+
# 或
|
|
678
|
+
yarn add @qy_better_lib/hooks
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
### 全局引入
|
|
682
|
+
```typescript
|
|
683
|
+
// main.ts
|
|
684
|
+
import { createApp } from 'vue'
|
|
685
|
+
import App from './App.vue'
|
|
686
|
+
import QYHooks from '@qy_better_lib/hooks'
|
|
687
|
+
|
|
688
|
+
const app = createApp(App)
|
|
689
|
+
app.use(QYHooks)
|
|
690
|
+
app.mount('#app')
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
### 按需引入(推荐)
|
|
694
|
+
```typescript
|
|
695
|
+
import { use_print, use_fullscreen, use_water_mark } from '@qy_better_lib/hooks'
|
|
696
|
+
```
|
|
697
|
+
|
|
698
|
+
## 注意事项
|
|
699
|
+
|
|
700
|
+
1. **use-chart** 需要安装 ECharts 依赖:`npm install echarts`
|
|
701
|
+
2. **use-layout-flow** 需要安装 @antv/x6 和 dagre 依赖:`npm install @antv/x6 dagre`
|
|
702
|
+
3. **use-mqtt** 需要安装 mqtt 依赖:`npm install mqtt`
|
|
703
|
+
4. **use-websocket** 需要确保服务器端支持 WebSocket 协议
|
|
704
|
+
5. **use-print** 在某些浏览器中可能存在兼容性问题,建议在现代浏览器中使用
|