pubo-web 1.0.201 → 1.0.203
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/README.md +265 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# pubo-web
|
|
2
|
+
|
|
3
|
+
Web 前端工具库,提供脚本加载、文件操作、存储、WebSocket、DOM 拖拽等功能。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install pubo-web
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API 文档
|
|
12
|
+
|
|
13
|
+
### loadScript
|
|
14
|
+
|
|
15
|
+
动态加载脚本。
|
|
16
|
+
|
|
17
|
+
**函数签名**
|
|
18
|
+
```typescript
|
|
19
|
+
export const loadScript = (url: string, options: any = {}) => Promise<any>
|
|
20
|
+
```
|
|
21
|
+
- `url`: 脚本 URL
|
|
22
|
+
- `options`: 附加到 script 元素的属性(如 `async`, `defer` 等)
|
|
23
|
+
- 返回:Promise,脚本加载完成后解析为 URL
|
|
24
|
+
|
|
25
|
+
### blob2text
|
|
26
|
+
|
|
27
|
+
Blob 转文本。
|
|
28
|
+
|
|
29
|
+
**函数签名**
|
|
30
|
+
```typescript
|
|
31
|
+
export const blob2text = (blob: any) => Promise<string>
|
|
32
|
+
```
|
|
33
|
+
- `blob`: Blob 对象
|
|
34
|
+
- 返回:Promise,解析为文本内容
|
|
35
|
+
|
|
36
|
+
### blob2base64
|
|
37
|
+
|
|
38
|
+
Blob 转 Base64 字符串。
|
|
39
|
+
|
|
40
|
+
**函数签名**
|
|
41
|
+
```typescript
|
|
42
|
+
export const blob2base64 = (blob: any) => Promise<string>
|
|
43
|
+
```
|
|
44
|
+
- `blob`: Blob 对象
|
|
45
|
+
- 返回:Promise,解析为 Base64 字符串
|
|
46
|
+
|
|
47
|
+
### blob2file
|
|
48
|
+
|
|
49
|
+
Blob 转 File 对象。
|
|
50
|
+
|
|
51
|
+
**函数签名**
|
|
52
|
+
```typescript
|
|
53
|
+
export const blob2file = (blob: any, name: string, type: string) => File
|
|
54
|
+
```
|
|
55
|
+
- `blob`: Blob 对象
|
|
56
|
+
- `name`: 文件名
|
|
57
|
+
- `type`: 文件类型
|
|
58
|
+
- 返回:File 对象
|
|
59
|
+
|
|
60
|
+
### downloadFile
|
|
61
|
+
|
|
62
|
+
下载文件。
|
|
63
|
+
|
|
64
|
+
**函数签名**
|
|
65
|
+
```typescript
|
|
66
|
+
export const downloadFile = (uri: string, name: string): void
|
|
67
|
+
```
|
|
68
|
+
- `uri`: 文件 URL 或 Data URL
|
|
69
|
+
- `name`: 下载文件名(可选)
|
|
70
|
+
|
|
71
|
+
### WebStorage
|
|
72
|
+
|
|
73
|
+
Web 存储封装,支持 localStorage 和 sessionStorage,可选压缩。
|
|
74
|
+
|
|
75
|
+
**构造函数**
|
|
76
|
+
```typescript
|
|
77
|
+
constructor(props: WebStorageProps)
|
|
78
|
+
```
|
|
79
|
+
- `props.type`: 存储类型,`'sessionStorage'` 或 `'localStorage'`,默认 `'sessionStorage'`
|
|
80
|
+
- `props.key`: 存储键名
|
|
81
|
+
- `props.zip`: 压缩对象,需包含 `deflate` 和 `inflate` 方法
|
|
82
|
+
|
|
83
|
+
**属性**
|
|
84
|
+
- `state: any` - 获取或设置存储状态(自动 JSON 序列化/反序列化)
|
|
85
|
+
- `key: string` - 存储键名
|
|
86
|
+
|
|
87
|
+
**方法**
|
|
88
|
+
- `merge(data: any): void` - 合并数据到当前状态
|
|
89
|
+
- `clear(): void` - 清除存储
|
|
90
|
+
|
|
91
|
+
### WebsocketClient
|
|
92
|
+
|
|
93
|
+
WebSocket 客户端,支持自动重连。
|
|
94
|
+
|
|
95
|
+
**构造函数**
|
|
96
|
+
```typescript
|
|
97
|
+
constructor({ url }: { url: string })
|
|
98
|
+
```
|
|
99
|
+
- `url`: WebSocket 服务器地址
|
|
100
|
+
|
|
101
|
+
**属性**
|
|
102
|
+
- `emitter: Emitter` - 事件发射器
|
|
103
|
+
- `status: number` - 连接状态(0: 默认, 1: 已连接, 2: 断开连接, 3: 重连)
|
|
104
|
+
|
|
105
|
+
**方法**
|
|
106
|
+
- `connect(): void` - 连接服务器
|
|
107
|
+
- `close(): void` - 关闭连接
|
|
108
|
+
- `send(data: any, isJson = false): void` - 发送数据,`isJson` 为 true 时自动 JSON 序列化
|
|
109
|
+
|
|
110
|
+
**事件**
|
|
111
|
+
- `'connect'` - 连接建立时触发
|
|
112
|
+
- `'message'` - 收到消息时触发,payload 为消息数据
|
|
113
|
+
|
|
114
|
+
### DragMethod
|
|
115
|
+
|
|
116
|
+
DOM 拖拽处理类,支持鼠标和触摸事件。
|
|
117
|
+
|
|
118
|
+
**构造函数**
|
|
119
|
+
```typescript
|
|
120
|
+
constructor({ key = '', onMove, onMoveEnd }: DragMethodProps = {})
|
|
121
|
+
```
|
|
122
|
+
- `key`: 可选标识符
|
|
123
|
+
- `onMove`: 拖拽移动回调
|
|
124
|
+
- `onMoveEnd`: 拖拽结束回调
|
|
125
|
+
|
|
126
|
+
**属性**
|
|
127
|
+
- `onMouseDown: (e: any) => void` - 鼠标按下事件处理函数
|
|
128
|
+
- `onTouchStart: (e: any) => void` - 触摸开始事件处理函数
|
|
129
|
+
- `onMove?: OnMove` - 移动回调(可重新赋值)
|
|
130
|
+
- `onMoveEnd?: OnMoveEnd` - 结束回调(可重新赋值)
|
|
131
|
+
|
|
132
|
+
**回调参数**
|
|
133
|
+
- `onMove` 接收对象:`{ offsetX, offsetY, pageX, pageY, startX, startY, key }`
|
|
134
|
+
|
|
135
|
+
### pickFiles
|
|
136
|
+
|
|
137
|
+
打开文件选择对话框。
|
|
138
|
+
|
|
139
|
+
**函数签名**
|
|
140
|
+
```typescript
|
|
141
|
+
export const pickFiles = (): Promise<FileList>
|
|
142
|
+
```
|
|
143
|
+
- 返回:Promise,用户选择的文件列表
|
|
144
|
+
|
|
145
|
+
### getCookieValue
|
|
146
|
+
|
|
147
|
+
获取指定 Cookie 值。
|
|
148
|
+
|
|
149
|
+
**函数签名**
|
|
150
|
+
```typescript
|
|
151
|
+
export const getCookieValue = (key: string): string
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### getCookie
|
|
155
|
+
|
|
156
|
+
获取所有 Cookie 并解析为对象。
|
|
157
|
+
|
|
158
|
+
**函数签名**
|
|
159
|
+
```typescript
|
|
160
|
+
export const getCookie = (): Record<string, string>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### setCookieItem
|
|
164
|
+
|
|
165
|
+
设置 Cookie。
|
|
166
|
+
|
|
167
|
+
**函数签名**
|
|
168
|
+
```typescript
|
|
169
|
+
export const setCookieItem = (key: string, value: string) => void
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### IndexedStorage
|
|
173
|
+
|
|
174
|
+
IndexedDB 存储封装,简化操作。
|
|
175
|
+
|
|
176
|
+
**构造函数**
|
|
177
|
+
```typescript
|
|
178
|
+
constructor({ name, version, tables }: StorageFactoryType)
|
|
179
|
+
```
|
|
180
|
+
- `name`: 数据库名称
|
|
181
|
+
- `version`: 数据库版本
|
|
182
|
+
- `tables`: 表定义数组(可选)
|
|
183
|
+
|
|
184
|
+
**方法**
|
|
185
|
+
- `register(tables: IndexedTable[]): void` - 注册表定义
|
|
186
|
+
- `connect(): Promise<void>` - 连接数据库
|
|
187
|
+
- `get(store: string): Storage` - 获取指定存储对象的操作接口
|
|
188
|
+
|
|
189
|
+
**Storage 接口**
|
|
190
|
+
- `getState(): Promise<any>` - 获取状态
|
|
191
|
+
- `setState(values: any): void` - 设置状态
|
|
192
|
+
- `merge(values: any): Promise<void>` - 合并状态
|
|
193
|
+
|
|
194
|
+
## 类型定义
|
|
195
|
+
|
|
196
|
+
### WebStorageProps
|
|
197
|
+
```typescript
|
|
198
|
+
interface WebStorageProps {
|
|
199
|
+
type?: 'sessionStorage' | 'localStorage';
|
|
200
|
+
key: string;
|
|
201
|
+
zip?: {
|
|
202
|
+
deflate: (data: string) => string;
|
|
203
|
+
inflate: (data: string) => string;
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### DragMethodProps
|
|
209
|
+
```typescript
|
|
210
|
+
interface DragMethodProps {
|
|
211
|
+
key?: string;
|
|
212
|
+
onMove?: (n: {
|
|
213
|
+
offsetX: number;
|
|
214
|
+
offsetY: number;
|
|
215
|
+
key?: string;
|
|
216
|
+
pageX: number;
|
|
217
|
+
pageY: number;
|
|
218
|
+
startX: number;
|
|
219
|
+
startY: number;
|
|
220
|
+
}) => void;
|
|
221
|
+
onMoveEnd?: () => void;
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### StorageFactoryType
|
|
226
|
+
```typescript
|
|
227
|
+
interface StorageFactoryType {
|
|
228
|
+
name: string;
|
|
229
|
+
version: number;
|
|
230
|
+
tables?: IndexedTable[];
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## 示例
|
|
235
|
+
|
|
236
|
+
```javascript
|
|
237
|
+
import { loadScript, WebStorage, pickFiles } from 'pubo-web';
|
|
238
|
+
|
|
239
|
+
// 加载脚本
|
|
240
|
+
await loadScript('https://example.com/script.js', { async: true });
|
|
241
|
+
|
|
242
|
+
// 使用 WebStorage
|
|
243
|
+
const storage = new WebStorage({ type: 'localStorage', key: 'my-app' });
|
|
244
|
+
storage.state = { user: 'john' };
|
|
245
|
+
const data = storage.state;
|
|
246
|
+
|
|
247
|
+
// 选择文件
|
|
248
|
+
const files = await pickFiles();
|
|
249
|
+
|
|
250
|
+
// 使用 WebSocket
|
|
251
|
+
const ws = new WebsocketClient({ url: 'ws://localhost:8080' });
|
|
252
|
+
ws.emitter.on('message', (data) => console.log('received:', data));
|
|
253
|
+
ws.connect();
|
|
254
|
+
|
|
255
|
+
// 拖拽
|
|
256
|
+
const drag = new DragMethod({
|
|
257
|
+
onMove: ({ offsetX, offsetY }) => console.log('dragging', offsetX, offsetY),
|
|
258
|
+
onMoveEnd: () => console.log('drag ended')
|
|
259
|
+
});
|
|
260
|
+
element.addEventListener('mousedown', drag.onMouseDown);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## 许可证
|
|
264
|
+
|
|
265
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pubo-web",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.203",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"module": "./es/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"node": ">=8.0.0"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"pubo-utils": "^1.0.
|
|
22
|
+
"pubo-utils": "^1.0.203"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "7825910956ac8ecfa3cb0a80b2f193c10aae409b",
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"del": "^5.1.0",
|
|
27
27
|
"eslint": "^8.42.0",
|