@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
|
@@ -1,54 +1,207 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { is_object } from "../packages/hooks/node_modules/@qy_better_lib/core/lib/utils/is.js";
|
|
2
|
+
import "../_virtual/dayjs.min.js";
|
|
3
|
+
import "../packages/hooks/node_modules/@qy_better_lib/core/lib/directives/click_outside.js";
|
|
4
|
+
function use_web_socket(options) {
|
|
5
|
+
const {
|
|
6
|
+
server,
|
|
7
|
+
receive,
|
|
8
|
+
max_reconnect_attempts = 5,
|
|
9
|
+
reconnect_interval = 5e3,
|
|
10
|
+
heartbeat_interval = 3e4,
|
|
11
|
+
heartbeat_message = "ping",
|
|
12
|
+
on_open,
|
|
13
|
+
on_error,
|
|
14
|
+
on_close,
|
|
15
|
+
auto_reconnect = true,
|
|
16
|
+
auto_connect = true,
|
|
17
|
+
max_message_queue_size = 100
|
|
18
|
+
} = options;
|
|
19
|
+
let reconnect_count = 0;
|
|
20
|
+
let lock_reconnect = false;
|
|
21
|
+
let ws = void 0;
|
|
22
|
+
let reconnect_timer = null;
|
|
23
|
+
let heartbeat_timer = null;
|
|
24
|
+
let message_queue = [];
|
|
25
|
+
let last_heartbeat_time = 0;
|
|
26
|
+
const heartbeat_timeout = heartbeat_interval * 1.5;
|
|
27
|
+
if (typeof window !== "undefined") {
|
|
28
|
+
window.onbeforeunload = () => {
|
|
29
|
+
close();
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function create() {
|
|
8
33
|
try {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
34
|
+
console.log("正在连接WebSocket:", server);
|
|
35
|
+
ws = new WebSocket(server);
|
|
36
|
+
init_event_handlers();
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.error("WebSocket连接建立异常:", error);
|
|
39
|
+
on_error?.(error);
|
|
40
|
+
if (auto_reconnect) {
|
|
41
|
+
reconnect();
|
|
42
|
+
}
|
|
12
43
|
}
|
|
13
44
|
}
|
|
14
|
-
function
|
|
15
|
-
if (
|
|
16
|
-
|
|
45
|
+
function init_event_handlers() {
|
|
46
|
+
if (!ws) return;
|
|
47
|
+
ws.onopen = () => {
|
|
48
|
+
console.log("WebSocket连接成功:", server);
|
|
49
|
+
reconnect_count = 0;
|
|
50
|
+
last_heartbeat_time = Date.now();
|
|
51
|
+
start_heartbeat();
|
|
52
|
+
flush_message_queue();
|
|
53
|
+
on_open?.();
|
|
54
|
+
};
|
|
55
|
+
ws.onerror = (error) => {
|
|
56
|
+
console.error("WebSocket连接错误:", error);
|
|
57
|
+
on_error?.(error);
|
|
58
|
+
};
|
|
59
|
+
ws.onclose = (event) => {
|
|
60
|
+
console.log("WebSocket连接关闭:", (/* @__PURE__ */ new Date()).toLocaleTimeString(), "原因:", event.code, event.reason);
|
|
61
|
+
stop_heartbeat();
|
|
62
|
+
on_close?.();
|
|
63
|
+
if (auto_reconnect && !event.wasClean) {
|
|
64
|
+
reconnect();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
ws.onmessage = (event) => {
|
|
68
|
+
try {
|
|
69
|
+
last_heartbeat_time = Date.now();
|
|
70
|
+
receive(event);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error("WebSocket消息处理错误:", error);
|
|
73
|
+
console.log("原始消息:", event.data);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function reconnect() {
|
|
78
|
+
if (reconnect_count >= max_reconnect_attempts) {
|
|
79
|
+
console.error("WebSocket重连失败次数过多,停止重连");
|
|
80
|
+
close();
|
|
17
81
|
return;
|
|
18
82
|
}
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
83
|
+
if (lock_reconnect) return;
|
|
84
|
+
lock_reconnect = true;
|
|
85
|
+
console.log(`WebSocket尝试重连 ${reconnect_count + 1}/${max_reconnect_attempts}...`);
|
|
86
|
+
reconnect_timer = setTimeout(() => {
|
|
87
|
+
create();
|
|
88
|
+
reconnect_count++;
|
|
89
|
+
lock_reconnect = false;
|
|
90
|
+
clear_reconnect_timer();
|
|
91
|
+
}, reconnect_interval);
|
|
92
|
+
}
|
|
93
|
+
function clear_reconnect_timer() {
|
|
94
|
+
if (reconnect_timer) {
|
|
95
|
+
clearTimeout(reconnect_timer);
|
|
96
|
+
reconnect_timer = null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function start_heartbeat() {
|
|
100
|
+
stop_heartbeat();
|
|
101
|
+
heartbeat_timer = setInterval(() => {
|
|
102
|
+
send_heartbeat();
|
|
103
|
+
check_heartbeat_timeout();
|
|
104
|
+
}, heartbeat_interval);
|
|
105
|
+
}
|
|
106
|
+
function stop_heartbeat() {
|
|
107
|
+
if (heartbeat_timer) {
|
|
108
|
+
clearInterval(heartbeat_timer);
|
|
109
|
+
heartbeat_timer = null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function send_heartbeat() {
|
|
113
|
+
if (ws?.readyState === WebSocket.OPEN) {
|
|
114
|
+
try {
|
|
115
|
+
const heartbeat_msg = is_object(heartbeat_message) ? JSON.stringify(heartbeat_message) : heartbeat_message;
|
|
116
|
+
ws.send(heartbeat_msg);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.error("发送心跳消息失败:", error);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function check_heartbeat_timeout() {
|
|
123
|
+
const now = Date.now();
|
|
124
|
+
if (now - last_heartbeat_time > heartbeat_timeout) {
|
|
125
|
+
console.error("WebSocket心跳超时,尝试重连");
|
|
126
|
+
reconnect();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function send_message(msg) {
|
|
130
|
+
if (ws?.readyState === WebSocket.OPEN) {
|
|
32
131
|
try {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
132
|
+
const message = is_object(msg) ? JSON.stringify(msg) : msg;
|
|
133
|
+
ws.send(message);
|
|
134
|
+
return true;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error("发送消息失败:", error);
|
|
137
|
+
add_to_message_queue(msg);
|
|
138
|
+
return false;
|
|
36
139
|
}
|
|
37
|
-
}
|
|
140
|
+
} else {
|
|
141
|
+
console.warn("WebSocket未连接,消息已加入队列");
|
|
142
|
+
add_to_message_queue(msg);
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function add_to_message_queue(msg) {
|
|
147
|
+
if (message_queue.length >= max_message_queue_size) {
|
|
148
|
+
const removed_msg = message_queue.shift();
|
|
149
|
+
console.warn("消息队列已满,移除最旧的消息:", removed_msg);
|
|
150
|
+
}
|
|
151
|
+
message_queue.push(msg);
|
|
152
|
+
console.log(`消息已加入队列,当前队列大小: ${message_queue.length}/${max_message_queue_size}`);
|
|
38
153
|
}
|
|
39
|
-
function
|
|
40
|
-
|
|
154
|
+
function flush_message_queue() {
|
|
155
|
+
if (ws?.readyState === WebSocket.OPEN && message_queue.length > 0) {
|
|
156
|
+
console.log(`发送队列中的 ${message_queue.length} 条消息`);
|
|
157
|
+
while (message_queue.length > 0) {
|
|
158
|
+
const msg = message_queue.shift();
|
|
159
|
+
send_message(msg);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function close() {
|
|
164
|
+
stop_heartbeat();
|
|
165
|
+
clear_reconnect_timer();
|
|
166
|
+
if (ws) {
|
|
167
|
+
try {
|
|
168
|
+
ws.close();
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.error("关闭WebSocket失败:", error);
|
|
171
|
+
}
|
|
172
|
+
ws = void 0;
|
|
173
|
+
}
|
|
174
|
+
message_queue = [];
|
|
175
|
+
}
|
|
176
|
+
function get_status() {
|
|
177
|
+
if (!ws) return "CLOSED";
|
|
178
|
+
switch (ws.readyState) {
|
|
179
|
+
case WebSocket.CONNECTING:
|
|
180
|
+
return "CONNECTING";
|
|
181
|
+
case WebSocket.OPEN:
|
|
182
|
+
return "OPEN";
|
|
183
|
+
case WebSocket.CLOSING:
|
|
184
|
+
return "CLOSING";
|
|
185
|
+
case WebSocket.CLOSED:
|
|
186
|
+
return "CLOSED";
|
|
187
|
+
default:
|
|
188
|
+
return "UNKNOWN";
|
|
189
|
+
}
|
|
41
190
|
}
|
|
42
|
-
|
|
43
|
-
|
|
191
|
+
if (auto_connect) {
|
|
192
|
+
create();
|
|
44
193
|
}
|
|
45
194
|
return {
|
|
46
|
-
ws
|
|
47
|
-
create
|
|
48
|
-
|
|
49
|
-
|
|
195
|
+
ws,
|
|
196
|
+
create,
|
|
197
|
+
close,
|
|
198
|
+
send_message,
|
|
199
|
+
get_status,
|
|
200
|
+
reconnect,
|
|
201
|
+
start_heartbeat,
|
|
202
|
+
stop_heartbeat
|
|
50
203
|
};
|
|
51
204
|
}
|
|
52
205
|
export {
|
|
53
|
-
|
|
206
|
+
use_web_socket
|
|
54
207
|
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket连接状态
|
|
3
|
+
*/
|
|
4
|
+
export type WebSocketStatus = 'CONNECTING' | 'OPEN' | 'CLOSING' | 'CLOSED' | 'UNKNOWN';
|
|
5
|
+
/**
|
|
6
|
+
* WebSocket选项
|
|
7
|
+
*/
|
|
8
|
+
export interface WebSocketOptions {
|
|
9
|
+
/**服务端地址 */
|
|
10
|
+
server: string;
|
|
11
|
+
/**消息接收处理方法 */
|
|
12
|
+
receive: (res: MessageEvent) => void;
|
|
13
|
+
/**重连次数限制,默认5次 */
|
|
14
|
+
max_reconnect_attempts?: number;
|
|
15
|
+
/**重连间隔,默认5000ms */
|
|
16
|
+
reconnect_interval?: number;
|
|
17
|
+
/**心跳检测间隔,默认30000ms */
|
|
18
|
+
heartbeat_interval?: number;
|
|
19
|
+
/**心跳消息 */
|
|
20
|
+
heartbeat_message?: string | ArrayBufferLike | Blob | ArrayBufferView<ArrayBufferLike>;
|
|
21
|
+
/**连接成功回调 */
|
|
22
|
+
on_open?: () => void;
|
|
23
|
+
/**连接错误回调 */
|
|
24
|
+
on_error?: (error: Event) => void;
|
|
25
|
+
/**连接关闭回调 */
|
|
26
|
+
on_close?: () => void;
|
|
27
|
+
/**是否自动重连,默认true */
|
|
28
|
+
auto_reconnect?: boolean;
|
|
29
|
+
/**是否自动连接,默认true */
|
|
30
|
+
auto_connect?: boolean;
|
|
31
|
+
/**消息队列最大长度,默认100 */
|
|
32
|
+
max_message_queue_size?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* WebSocket钩子返回类型
|
|
36
|
+
*/
|
|
37
|
+
export interface UseWebSocketReturn {
|
|
38
|
+
/**WebSocket实例 */
|
|
39
|
+
ws: WebSocket | undefined;
|
|
40
|
+
/**创建并连接WebSocket */
|
|
41
|
+
create: () => void;
|
|
42
|
+
/**关闭WebSocket连接 */
|
|
43
|
+
close: () => void;
|
|
44
|
+
/**发送消息 */
|
|
45
|
+
send_message: (msg: any) => boolean;
|
|
46
|
+
/**获取连接状态 */
|
|
47
|
+
get_status: () => WebSocketStatus;
|
|
48
|
+
/**手动重连 */
|
|
49
|
+
reconnect: () => void;
|
|
50
|
+
/**启动心跳检测 */
|
|
51
|
+
start_heartbeat: () => void;
|
|
52
|
+
/**停止心跳检测 */
|
|
53
|
+
stop_heartbeat: () => void;
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qy_better_lib/hooks",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "qy better lib hooks",
|
|
6
6
|
"author": "luhuiming",
|
|
7
7
|
"license": "ISC",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build:lib": "vite build --config vite.lib.config.ts",
|
|
10
10
|
"build:dist-min": "vite build --config vite.dist.min.config.ts",
|
|
11
|
-
"build": "yarn build:lib && yarn build:dist-min"
|
|
11
|
+
"build": "yarn build:lib && yarn build:dist-min",
|
|
12
|
+
"test": "vitest run",
|
|
13
|
+
"test:watch": "vitest"
|
|
12
14
|
},
|
|
13
15
|
"main": "lib/index.js",
|
|
14
16
|
"module": "lib/index.js",
|
|
@@ -46,6 +48,7 @@
|
|
|
46
48
|
"mqtt": "^5.14.1",
|
|
47
49
|
"vite": "^7.1.7",
|
|
48
50
|
"vite-plugin-dts": "^4.5.4",
|
|
51
|
+
"vitest": "^4.0.18",
|
|
49
52
|
"vue": "^3.5.22"
|
|
50
53
|
},
|
|
51
54
|
"peerDependencies": {
|
|
@@ -71,5 +74,8 @@
|
|
|
71
74
|
"mqtt": {
|
|
72
75
|
"optional": true
|
|
73
76
|
}
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"@qy_better_lib/core": "^0.1.5"
|
|
74
80
|
}
|
|
75
|
-
}
|
|
81
|
+
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
(function(u,y){typeof exports=="object"&&typeof module<"u"?y(exports,require("mitt"),require("vue")):typeof define=="function"&&define.amd?define(["exports","mitt","vue"],y):(u=typeof globalThis<"u"?globalThis:u||self,y(u["@qy_better_lib/hooks"]={},u.mitt,u.vue))})(this,(function(u,y,R){"use strict";function C(){if(typeof y>"u")return{};const t=y();return t.emitAsync=async function(n,...a){const e=this.all?.get(n);if(!e||e.length===0)return[];const r=e.map(i=>{try{const l=i(...a);return l instanceof Promise?l:Promise.resolve(l)}catch(l){return Promise.reject(l)}});return Promise.all(r)},t}const g=C();function U(t){return typeof y>"u"?{emitter:g}:(R.onMounted(()=>{if(t&&t.length>0)for(let n=0;n<t.length;n++){debugger;g.on(t[n].key,t[n].value)}}),R.onUnmounted(()=>{if(t&&t.length>0)for(let n=0;n<t.length;n++)g.off(t[n].key,t[n].value),g.all.delete(t[n].key)}),{emitter:g})}function M(){function t(e,r){const i=document.createElement("a");i.href=e,i.target="_blank",r&&i.setAttribute("download",r),document.body.appendChild(i),i.click(),document.body.removeChild(i)}function n(e){e.forEach(r=>{t(r.url,r.name)})}async function a(e){if(!e)return;const r=new FileReader;return r.readAsDataURL(e),new Promise((i,l)=>{r.onload=o=>i(o.target?.result),r.onerror=o=>l(void 0)})}return{downLoadFile:t,downLoadFiles:n,toBase64:a}}function F(){function t(o){let c=n(),d=e(o);document.body.appendChild(c),document.body.appendChild(d),r(d).then(()=>{window.print(),document.body.removeChild(c),document.body.removeChild(d)})}function n(o){let c=`#print-container {
|
|
2
|
-
display: none;
|
|
3
|
-
}
|
|
4
|
-
@media print {
|
|
5
|
-
body > :not(.print-container) {
|
|
6
|
-
display: none;
|
|
7
|
-
}
|
|
8
|
-
html,
|
|
9
|
-
body {
|
|
10
|
-
display: block !important;
|
|
11
|
-
}
|
|
12
|
-
#print-container {
|
|
13
|
-
display: block;
|
|
14
|
-
}
|
|
15
|
-
}`,d=document.createElement("style");return d.innerHTML=c,d}function a(){let o=document.getElementById("print-container");o&&document?.querySelector("body")?.removeChild(o)}function e(o){a();let c=document.createElement("div");return c.setAttribute("id","print-container"),c.innerHTML=o,c}function r(o){let c=o.querySelectorAll("img");const d=[].slice.call(c);if(d.length===0)return Promise.resolve();let m=0;return new Promise(h=>{function s(){m++,m===d.length&&h()}d.forEach(f=>{f.addEventListener("load",s),f.addEventListener("error",s)})})}function i(o){const c=document.querySelector(o);c&&t(c.innerHTML)}function l(o){let c="";for(let d=0;d<o.length;d++)c+=o[d].outerHTML;c&&t(c)}return{printHtml:i,printHtmlElement:l}}function j(){const t="A77B030B-E33D-C656-7642-56D6FEA46059";function n(r=null,i,l,o){let c=e(r,i,l,o);document.getElementById(c)===null&&(c=e(r,i,l))}function a(){document.getElementById(t)?.remove()}function e(r=null,i,l,o="#999"){const c=r!=null?document.querySelector(r):document.body,d=document.getElementById(t);d!==null&&c?.removeChild(d);let m=document.createElement("canvas");m.width=250,m.height=150;let h=m.getContext("2d");h&&(h.font="16px Avenir,Helvetica,Arial,sans-serif",h.fillStyle=o.replace(/\,\s(1|(0.\d+)\))/,", 0.1"),h.textAlign="left",h.textBaseline="middle",h.rotate(-15*Math.PI/180),h?.fillText(i,0,m.height),l&&h?.fillText(l,h.measureText(i).width+16,m.height));let s=document.createElement("div");s.id=t,s.style.pointerEvents="none",s.style.top="0px",s.style.left="0px",s.style.position="fixed",s.style.zIndex="100000";const{clientWidth:f,clientHeight:b}=document.documentElement;return s.style.width=f+"px",s.style.height=b+"px",s.style.background="url("+m.toDataURL("image/png")+") left top repeat",c?.appendChild(s),t}return{createWaterMark:n,removeWatermark:a}}function A(t){let n=0,a=!1,e,r;const{server:i,receive:l}=t;window.onbeforeunload=()=>{e?.close()};function o(){try{e=new WebSocket(i),d()}catch(s){console.log("连接建立异常",s)}}function c(){if(n>=5){e?.close();return}if(a)return!1;a=!0,r=setTimeout(function(){o(),a=!1,n++,r&&clearTimeout(r)},5e3)}function d(){e&&(e.onerror=s=>{console.log("连接错误",s),c()},e.onclose=()=>{console.log("连接关闭 - "+new Date().toLocaleTimeString()),c()},e.onopen=()=>{n=0,console.log("连接成功")},e.onmessage=s=>{try{l(s)}catch{console.log("message:"+s.data)}})}function m(s){e?.readyState===WebSocket.OPEN&&e?.send(s)}function h(){e?.close()}return{ws:e,create:o,socketClose:h,sendMessage:m}}var w=(t=>(t.PNG="image/png",t.JPEG="image/jpeg",t.GIF="image/gif",t))(w||{});function p(t){return["image/png","image/jpeg","image/gif"].some(n=>n===t)}async function v(t,n=.92,a=w.JPEG){return p(a)||(a=w.JPEG),t.toDataURL(a,n)}function S(t,n=.92,a=w.JPEG){return new Promise(e=>t.toBlob(r=>e(r),a,n))}async function E(t,n){const a=t.split(",");let e=a[0].match(/:(.*?);/)?.[1];const r=atob(a[1]);let i=r.length;const l=new Uint8Array(i);for(;i--;)l[i]=r.charCodeAt(i);return p(n)&&(e=n),new Blob([l],{type:e})}function k(t){return new Promise((n,a)=>{const e=new Image;e.onload=()=>n(e),e.onerror=()=>a(new Error("dataURLtoImage(): dataURL is illegal")),e.src=t})}function z(t,n){const a=document.createElement("a");a.href=window.URL.createObjectURL(t),a.download=n||Date.now().toString(36),document.body.appendChild(a);const e=document.createEvent("MouseEvents");e.initEvent("click",!1,!1),a.dispatchEvent(e),document.body.removeChild(a)}function L(t){return new Promise(n=>{const a=new FileReader;a.onloadend=e=>n(e.target?.result),a.readAsDataURL(t)})}async function P(t,n={}){const a={...n},e=document.createElement("canvas"),r=e.getContext("2d");let i,l;for(const o in a)Object.prototype.hasOwnProperty.call(a,o)&&(a[o]=Number(a[o]));if(!a.scale)l=a.width||(a.height||0)*t.width/t.height||t.width,i=a.height||(a.width||0)*t.height/t.width||t.height;else{const o=a.scale>0&&a.scale<10?a.scale:1;l=t.width*o,i=t.height*o}switch([5,6,7,8].some(o=>o===a.orientation)?(e.height=l,e.width=i):(e.height=i,e.width=l),a.orientation){case 3:r.rotate(180*Math.PI/180),r.drawImage(t,-e.width,-e.height,e.width,e.height);break;case 6:r.rotate(90*Math.PI/180),r.drawImage(t,0,-e.width,e.height,e.width);break;case 8:r.rotate(270*Math.PI/180),r.drawImage(t,-e.height,0,e.height,e.width);break;case 2:r.translate(e.width,0),r.scale(-1,1),r.drawImage(t,0,0,e.width,e.height);break;case 4:r.translate(e.width,0),r.scale(-1,1),r.rotate(180*Math.PI/180),r.drawImage(t,-e.width,-e.height,e.width,e.height);break;case 5:r.translate(e.width,0),r.scale(-1,1),r.rotate(90*Math.PI/180),r.drawImage(t,0,-e.width,e.height,e.width);break;case 7:r.translate(e.width,0),r.scale(-1,1),r.rotate(270*Math.PI/180),r.drawImage(t,-e.height,0,e.height,e.width);break;default:r.drawImage(t,0,0,e.width,e.height)}return e}function B(t){return fetch(t).then(n=>n.blob())}function D(t){return new Promise((n,a)=>{const e=new Image;e.onload=()=>n(e),e.onerror=()=>a(new Error("urltoImage(): Image failed to load, please check the image URL")),e.src=t})}async function T(t,n={}){if(!(t instanceof Blob))throw new Error("compress(): First arg must be a Blob object or a File object.");if(typeof n!="object"&&(n=Object.assign({quality:n})),n.quality=Number(n.quality),Number.isNaN(n.quality))return t;const a=await L(t);let e=a.split(",")[0].match(/:(.*?);/)?.[1],r=w.JPEG;p(n.type)&&(r=n.type,e=n.type);const i=await k(a),l=await P(i,Object.assign({},n)),o=await v(l,n.quality,r),c=await E(o,e);return c.size>t.size?t:c}async function q(t,n={}){if(!(t instanceof Blob))throw new Error("compressAccurately(): First arg must be a Blob object or a File object.");if(typeof n!="object"&&(n=Object.assign({size:n})),n.size=Number(n.size),Number.isNaN(n.size)||n.size*1024>t.size)return t;n.accuracy=Number(n.accuracy),(!n.accuracy||n.accuracy<.8||n.accuracy>.99)&&(n.accuracy=.95);const a={max:n.size*(2-n.accuracy)*1024,accurate:n.size*1024,min:n.size*n.accuracy*1024},e=await L(t);let r=e.split(",")[0].match(/:(.*?);/)?.[1],i=w.JPEG;p(n.type)&&(i=n.type,r=n.type);const l=await k(e),o=await P(l,Object.assign({},n)),c=.75;let d=1,m;const h=new Array(2);for(let f=1;f<=7;f++){m=await v(o,d,i);const b=m.length*c;if(f===7){(a.max<b||a.min>b)&&(m=[m,...h].filter(I=>I).sort((I,H)=>Math.abs(I.length*c-a.accurate)-Math.abs(H.length*c-a.accurate))[0]);break}if(a.max<b)h[1]=m,d-=.5**(f+1);else if(a.min>b)h[0]=m,d+=.5**(f+1);else break}const s=await E(m,r);return s.size>t.size?t:s}u.canvastoDataURL=v,u.canvastoFile=S,u.compress=T,u.compressAccurately=q,u.dataURLtoFile=E,u.dataURLtoImage=k,u.downloadFile=z,u.filetoDataURL=L,u.imagetoCanvas=P,u.urltoBlob=B,u.urltoImage=D,u.useEmits=U,u.useFile=M,u.usePrintHtml=F,u.useWaterMark=j,u.useWebSocket=A,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})}));
|
package/lib/use-chart/utils.d.ts
DELETED
package/lib/use-file/index.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
interface UseFile {
|
|
2
|
-
/**下载文件 */
|
|
3
|
-
downLoadFile: (filePath: string) => void;
|
|
4
|
-
/**批量下载文件 */
|
|
5
|
-
downLoadFiles: (list: Array<any>) => void;
|
|
6
|
-
/**将二进制文件转换成base64 */
|
|
7
|
-
toBase64: (file: Blob) => Promise<string | undefined>;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* 文件处理
|
|
11
|
-
* @returns
|
|
12
|
-
*/
|
|
13
|
-
export declare function useFile(): UseFile;
|
|
14
|
-
export {};
|
package/lib/use-file/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
function c() {
|
|
2
|
-
function o(r, e) {
|
|
3
|
-
const n = document.createElement("a");
|
|
4
|
-
n.href = r, n.target = "_blank", e && n.setAttribute("download", e), document.body.appendChild(n), n.click(), document.body.removeChild(n);
|
|
5
|
-
}
|
|
6
|
-
function d(r) {
|
|
7
|
-
r.forEach((e) => {
|
|
8
|
-
o(e.url, e.name);
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
async function a(r) {
|
|
12
|
-
if (!r) return;
|
|
13
|
-
const e = new FileReader();
|
|
14
|
-
return e.readAsDataURL(r), new Promise((n, u) => {
|
|
15
|
-
e.onload = (t) => n(t.target?.result), e.onerror = (t) => u(void 0);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
return {
|
|
19
|
-
downLoadFile: o,
|
|
20
|
-
downLoadFiles: d,
|
|
21
|
-
toBase64: a
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
export {
|
|
25
|
-
c as useFile
|
|
26
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { EImageType } from './type';
|
|
2
|
-
/**
|
|
3
|
-
* 将一个Canvas对象转变为一个dataURL字符串
|
|
4
|
-
* 该方法可以做压缩处理
|
|
5
|
-
*
|
|
6
|
-
* @param {canvas} canvas
|
|
7
|
-
* @param {number=} quality - 传入范围 0-1,表示图片压缩质量,默认0.92
|
|
8
|
-
* @param {string=} type - 确定转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif",默认"image/jpeg"
|
|
9
|
-
* @returns {Promise(string)} Promise含有一个dataURL字符串参数
|
|
10
|
-
*/
|
|
11
|
-
export default function canvastoDataURL(canvas: HTMLCanvasElement, quality?: number, type?: EImageType): Promise<string>;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { EImageType } from './type';
|
|
2
|
-
/**
|
|
3
|
-
* 将一个canvas对象转变为一个File(Blob)对象
|
|
4
|
-
* 该方法可以做压缩处理
|
|
5
|
-
*
|
|
6
|
-
* @param {canvas} canvas
|
|
7
|
-
* @param {number=} quality - 传入范围 0-1,表示图片压缩质量,默认0.92
|
|
8
|
-
* @param {string=} type - 确定转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif",默认"image/jpeg"
|
|
9
|
-
* @returns {Promise(Blob)}
|
|
10
|
-
*/
|
|
11
|
-
export default function canvastoFile(canvas: HTMLCanvasElement, quality?: number, type?: EImageType): Promise<Blob>;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { EImageType } from './type';
|
|
2
|
-
/**
|
|
3
|
-
* 将一个dataURL字符串转变为一个File(Blob)对象
|
|
4
|
-
* 转变时可以确定File对象的类型
|
|
5
|
-
*
|
|
6
|
-
* @param {string} dataURL
|
|
7
|
-
* @param {string=} type - 确定转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif"
|
|
8
|
-
* @returns {Promise(Blob)}
|
|
9
|
-
*/
|
|
10
|
-
export default function dataURLtoFile(dataURL: string, type: EImageType): Promise<Blob>;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { checkImageType as i } from "./type.js";
|
|
2
|
-
async function s(c, e) {
|
|
3
|
-
const r = c.split(",");
|
|
4
|
-
let a = r[0].match(/:(.*?);/)?.[1];
|
|
5
|
-
const n = atob(r[1]);
|
|
6
|
-
let t = n.length;
|
|
7
|
-
const o = new Uint8Array(t);
|
|
8
|
-
for (; t--; )
|
|
9
|
-
o[t] = n.charCodeAt(t);
|
|
10
|
-
return i(e) && (a = e), new Blob([o], {
|
|
11
|
-
type: a
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
export {
|
|
15
|
-
s as default
|
|
16
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
function d(n, o) {
|
|
2
|
-
const e = document.createElement("a");
|
|
3
|
-
e.href = window.URL.createObjectURL(n), e.download = o || Date.now().toString(36), document.body.appendChild(e);
|
|
4
|
-
const t = document.createEvent("MouseEvents");
|
|
5
|
-
t.initEvent("click", !1, !1), e.dispatchEvent(t), document.body.removeChild(e);
|
|
6
|
-
}
|
|
7
|
-
export {
|
|
8
|
-
d as default
|
|
9
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Image2CanvasConfig } from './type';
|
|
2
|
-
/**
|
|
3
|
-
* 将一个image对象转变为一个canvas对象
|
|
4
|
-
*
|
|
5
|
-
* @param {image} image
|
|
6
|
-
*
|
|
7
|
-
* @typedef {Object=} config - 转变为canvas时的一些参数配置
|
|
8
|
-
* @param {number} width - canvas图像的宽度,默认为image的宽度
|
|
9
|
-
* @param {number} height - canvas图像的高度,默认为image的高度
|
|
10
|
-
* @param {number} scale - 相对于image的缩放比例,范围0-10,默认不缩放;
|
|
11
|
-
* 设置config.scale后会覆盖config.width和config.height的设置;
|
|
12
|
-
* @param {number} orientation - 图片旋转参数,默认不旋转,参考如下:
|
|
13
|
-
* 参数 旋转方向
|
|
14
|
-
* 1 0°
|
|
15
|
-
* 2 水平翻转
|
|
16
|
-
* 3 180°
|
|
17
|
-
* 4 垂直翻转
|
|
18
|
-
* 5 顺时针90°+水平翻转
|
|
19
|
-
* 6 顺时针90°
|
|
20
|
-
* 7 顺时针90°+垂直翻转
|
|
21
|
-
* 8 逆时针90°
|
|
22
|
-
* @type {config}
|
|
23
|
-
*
|
|
24
|
-
* @returns {Promise(canvas)}
|
|
25
|
-
*/
|
|
26
|
-
export default function imagetoCanvas(image: HTMLImageElement, config?: Image2CanvasConfig): Promise<HTMLCanvasElement>;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
async function w(e, s = {}) {
|
|
2
|
-
const a = { ...s }, t = document.createElement("canvas"), h = t.getContext("2d");
|
|
3
|
-
let d, r;
|
|
4
|
-
for (const i in a)
|
|
5
|
-
Object.prototype.hasOwnProperty.call(a, i) && (a[i] = Number(a[i]));
|
|
6
|
-
if (!a.scale)
|
|
7
|
-
r = a.width || (a.height || 0) * e.width / e.height || e.width, d = a.height || (a.width || 0) * e.height / e.width || e.height;
|
|
8
|
-
else {
|
|
9
|
-
const i = a.scale > 0 && a.scale < 10 ? a.scale : 1;
|
|
10
|
-
r = e.width * i, d = e.height * i;
|
|
11
|
-
}
|
|
12
|
-
switch ([5, 6, 7, 8].some((i) => i === a.orientation) ? (t.height = r, t.width = d) : (t.height = d, t.width = r), a.orientation) {
|
|
13
|
-
case 3:
|
|
14
|
-
h.rotate(180 * Math.PI / 180), h.drawImage(e, -t.width, -t.height, t.width, t.height);
|
|
15
|
-
break;
|
|
16
|
-
case 6:
|
|
17
|
-
h.rotate(90 * Math.PI / 180), h.drawImage(e, 0, -t.width, t.height, t.width);
|
|
18
|
-
break;
|
|
19
|
-
case 8:
|
|
20
|
-
h.rotate(270 * Math.PI / 180), h.drawImage(e, -t.height, 0, t.height, t.width);
|
|
21
|
-
break;
|
|
22
|
-
case 2:
|
|
23
|
-
h.translate(t.width, 0), h.scale(-1, 1), h.drawImage(e, 0, 0, t.width, t.height);
|
|
24
|
-
break;
|
|
25
|
-
case 4:
|
|
26
|
-
h.translate(t.width, 0), h.scale(-1, 1), h.rotate(180 * Math.PI / 180), h.drawImage(e, -t.width, -t.height, t.width, t.height);
|
|
27
|
-
break;
|
|
28
|
-
case 5:
|
|
29
|
-
h.translate(t.width, 0), h.scale(-1, 1), h.rotate(90 * Math.PI / 180), h.drawImage(e, 0, -t.width, t.height, t.width);
|
|
30
|
-
break;
|
|
31
|
-
case 7:
|
|
32
|
-
h.translate(t.width, 0), h.scale(-1, 1), h.rotate(270 * Math.PI / 180), h.drawImage(e, -t.height, 0, t.height, t.width);
|
|
33
|
-
break;
|
|
34
|
-
default:
|
|
35
|
-
h.drawImage(e, 0, 0, t.width, t.height);
|
|
36
|
-
}
|
|
37
|
-
return t;
|
|
38
|
-
}
|
|
39
|
-
export {
|
|
40
|
-
w as default
|
|
41
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
function n(o) {
|
|
2
|
-
return new Promise((r, a) => {
|
|
3
|
-
const e = new Image();
|
|
4
|
-
e.onload = () => r(e), e.onerror = () => a(
|
|
5
|
-
new Error(
|
|
6
|
-
"urltoImage(): Image failed to load, please check the image URL"
|
|
7
|
-
)
|
|
8
|
-
), e.src = o;
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
export {
|
|
12
|
-
n as default
|
|
13
|
-
};
|