@qy_better_lib/hooks 0.2.6 → 0.2.7
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 +0 -49
- package/lib/index.mjs +935 -0
- package/lib/{use-chart.js → use-chart/index.mjs} +76 -3
- package/package.json +4 -1
- package/lib/index.js +0 -16
- package/lib/node_modules/@qy_better_lib/core/lib/utils/dom.js +0 -14
- package/lib/node_modules/@qy_better_lib/core/lib/utils/echarts.js +0 -160
- package/lib/use-chart/config.js +0 -78
- package/lib/use-emit/extend.js +0 -42
- package/lib/use-emit/index.js +0 -27
- package/lib/use-fullscreen/index.js +0 -51
- package/lib/use-image/index.js +0 -192
- package/lib/use-image/type.js +0 -9
- package/lib/use-print/index.js +0 -301
- package/lib/use-watermark/index.js +0 -131
- package/lib/use-websocket/index.js +0 -205
- /package/lib/{use-layout-flow.js → use-layout-flow/index.mjs} +0 -0
- /package/lib/{use-mqtt.js → use-mqtt/index.mjs} +0 -0
|
@@ -1,7 +1,80 @@
|
|
|
1
1
|
import { onUnmounted } from "vue";
|
|
2
|
-
import { deep_assign, deep_clone } from "@qy_better_lib/core";
|
|
3
|
-
import { destroy_chart, get_chart_gradient_color, generate_chart_colors, remove_chart_listener, add_chart_listener, export_chart_image, resize_chart, merge_chart_options, update_chart, init_chart, make_chart_responsive } from "
|
|
4
|
-
|
|
2
|
+
import { auto_size, deep_assign, deep_clone } from "@qy_better_lib/core";
|
|
3
|
+
import { destroy_chart, get_chart_gradient_color, generate_chart_colors, remove_chart_listener, add_chart_listener, export_chart_image, resize_chart, merge_chart_options, update_chart, init_chart, make_chart_responsive } from "@qy_better_lib/core/echarts";
|
|
4
|
+
const options = {
|
|
5
|
+
textStyle: {
|
|
6
|
+
fontSize: auto_size(12),
|
|
7
|
+
fontFamily: "Helvetica Neue, Arial, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, sans-serif"
|
|
8
|
+
},
|
|
9
|
+
color: [],
|
|
10
|
+
title: {
|
|
11
|
+
textStyle: {
|
|
12
|
+
fontSize: auto_size(14)
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
legend: {
|
|
16
|
+
icon: "rect",
|
|
17
|
+
itemHeight: auto_size(12),
|
|
18
|
+
itemWidth: auto_size(12),
|
|
19
|
+
textStyle: { color: "#606266", fontSize: auto_size(12) },
|
|
20
|
+
itemStyle: {
|
|
21
|
+
shadowColor: "rgba(161, 163, 170, 0.25)",
|
|
22
|
+
shadowBlur: auto_size(4),
|
|
23
|
+
shadowOffsetX: auto_size(4),
|
|
24
|
+
shadowOffsetY: auto_size(4)
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
tooltip: {
|
|
28
|
+
trigger: "axis",
|
|
29
|
+
axisPointer: {
|
|
30
|
+
lineStyle: {
|
|
31
|
+
color: "#D4D9E2",
|
|
32
|
+
width: auto_size(1)
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
backgroundColor: "rgba(0,0,0,0.6)",
|
|
36
|
+
textStyle: {
|
|
37
|
+
color: "#fff",
|
|
38
|
+
fontWeight: 400,
|
|
39
|
+
width: auto_size(10),
|
|
40
|
+
height: auto_size(10),
|
|
41
|
+
fontSize: auto_size(12)
|
|
42
|
+
},
|
|
43
|
+
borderWidth: 0,
|
|
44
|
+
padding: [auto_size(8), auto_size(16)],
|
|
45
|
+
formatter: "{a}<br/>{b}: {c}"
|
|
46
|
+
},
|
|
47
|
+
xAxis: {
|
|
48
|
+
axisLine: {
|
|
49
|
+
lineStyle: { color: "#D4D9E2", width: auto_size(1) }
|
|
50
|
+
},
|
|
51
|
+
axisLabel: { color: "#606266", fontSize: auto_size(12) },
|
|
52
|
+
nameTextStyle: { color: "#606266", fontSize: auto_size(12) },
|
|
53
|
+
splitLine: {
|
|
54
|
+
show: false,
|
|
55
|
+
lineStyle: {
|
|
56
|
+
color: "#EBECEF",
|
|
57
|
+
type: "dashed",
|
|
58
|
+
width: auto_size(1)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
yAxis: {
|
|
63
|
+
axisLine: {
|
|
64
|
+
show: false,
|
|
65
|
+
lineStyle: { color: "#D4D9E2", width: auto_size(1) }
|
|
66
|
+
},
|
|
67
|
+
axisLabel: { color: "#606266", fontSize: auto_size(12) },
|
|
68
|
+
nameTextStyle: { color: "#606266", fontSize: auto_size(12) },
|
|
69
|
+
splitLine: {
|
|
70
|
+
lineStyle: {
|
|
71
|
+
color: "#EBECEF",
|
|
72
|
+
type: "dashed",
|
|
73
|
+
width: auto_size(1)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
5
78
|
function get_chart_theme(theme) {
|
|
6
79
|
{
|
|
7
80
|
return {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qy_better_lib/hooks",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.7",
|
|
5
5
|
"description": "qy better lib hooks",
|
|
6
6
|
"author": "luhuiming",
|
|
7
7
|
"license": "MIT",
|
|
@@ -97,5 +97,8 @@
|
|
|
97
97
|
"@qy_better_lib/core": {
|
|
98
98
|
"optional": false
|
|
99
99
|
}
|
|
100
|
+
},
|
|
101
|
+
"dependencies": {
|
|
102
|
+
"@qy_better_lib/hooks": "^0.2.6"
|
|
100
103
|
}
|
|
101
104
|
}
|
package/lib/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { use_emit } from "./use-emit/index.js";
|
|
2
|
-
import { use_print } from "./use-print/index.js";
|
|
3
|
-
import { use_water_mark } from "./use-watermark/index.js";
|
|
4
|
-
import { use_web_socket } from "./use-websocket/index.js";
|
|
5
|
-
import { use_image } from "./use-image/index.js";
|
|
6
|
-
import { use_fullscreen } from "./use-fullscreen/index.js";
|
|
7
|
-
import { EImageType } from "./use-image/type.js";
|
|
8
|
-
export {
|
|
9
|
-
EImageType,
|
|
10
|
-
use_emit,
|
|
11
|
-
use_fullscreen,
|
|
12
|
-
use_image,
|
|
13
|
-
use_print,
|
|
14
|
-
use_water_mark,
|
|
15
|
-
use_web_socket
|
|
16
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
function on(element, event, handler, options) {
|
|
2
|
-
if (element) {
|
|
3
|
-
element.addEventListener(event, handler, options);
|
|
4
|
-
}
|
|
5
|
-
}
|
|
6
|
-
function off(element, event, handler, options) {
|
|
7
|
-
if (element) {
|
|
8
|
-
element.removeEventListener(event, handler, options);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export {
|
|
12
|
-
off,
|
|
13
|
-
on
|
|
14
|
-
};
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { on, off } from "./dom.js";
|
|
2
|
-
function get_chart_gradient_color(type, start_color, end_color) {
|
|
3
|
-
return {
|
|
4
|
-
type: "linear",
|
|
5
|
-
x: 0,
|
|
6
|
-
y: 0,
|
|
7
|
-
x2: type === "v" ? 0 : 1,
|
|
8
|
-
y2: type === "v" ? 1 : 0,
|
|
9
|
-
colorStops: [
|
|
10
|
-
{
|
|
11
|
-
offset: 0,
|
|
12
|
-
color: start_color
|
|
13
|
-
// 0% 处的颜色
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
offset: 1,
|
|
17
|
-
color: end_color
|
|
18
|
-
// 100% 处的颜色
|
|
19
|
-
}
|
|
20
|
-
]
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
function init_chart(container, options, renderer = "canvas", theme) {
|
|
24
|
-
const load_echarts = async () => {
|
|
25
|
-
try {
|
|
26
|
-
let echarts;
|
|
27
|
-
if (typeof window !== "undefined" && window.echarts) {
|
|
28
|
-
echarts = window.echarts;
|
|
29
|
-
} else {
|
|
30
|
-
const imported = await import("echarts");
|
|
31
|
-
echarts = imported.default || imported;
|
|
32
|
-
}
|
|
33
|
-
const chart_dom = typeof container === "string" ? document.getElementById(container) : container;
|
|
34
|
-
if (!chart_dom) {
|
|
35
|
-
console.error("ECharts DOM容器未找到");
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
const chart = echarts.init(chart_dom, theme, {
|
|
39
|
-
renderer
|
|
40
|
-
});
|
|
41
|
-
if (options) {
|
|
42
|
-
chart.setOption(options);
|
|
43
|
-
}
|
|
44
|
-
return chart;
|
|
45
|
-
} catch (error) {
|
|
46
|
-
console.error("加载 ECharts 失败:", error);
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
return load_echarts();
|
|
51
|
-
}
|
|
52
|
-
function destroy_chart(chart) {
|
|
53
|
-
if (chart && typeof chart.dispose === "function") {
|
|
54
|
-
chart.dispose();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function update_chart(chart, options) {
|
|
58
|
-
if (chart && typeof chart.setOption === "function") {
|
|
59
|
-
chart.setOption(options);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
function resize_chart(chart) {
|
|
63
|
-
if (chart && typeof chart.resize === "function") {
|
|
64
|
-
chart.resize();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function make_chart_responsive(chart, debounce_time = 200) {
|
|
68
|
-
if (!chart) return;
|
|
69
|
-
let resize_timer;
|
|
70
|
-
const resize_handler = () => {
|
|
71
|
-
clearTimeout(resize_timer);
|
|
72
|
-
resize_timer = setTimeout(() => {
|
|
73
|
-
resize_chart(chart);
|
|
74
|
-
}, debounce_time);
|
|
75
|
-
};
|
|
76
|
-
on(window, "resize", resize_handler);
|
|
77
|
-
return () => {
|
|
78
|
-
off(window, "resize", resize_handler);
|
|
79
|
-
clearTimeout(resize_timer);
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
function generate_chart_colors(count, base_colors) {
|
|
83
|
-
const default_colors = [
|
|
84
|
-
"#5470c6",
|
|
85
|
-
"#91cc75",
|
|
86
|
-
"#fac858",
|
|
87
|
-
"#ee6666",
|
|
88
|
-
"#73c0de",
|
|
89
|
-
"#3ba272",
|
|
90
|
-
"#fc8452",
|
|
91
|
-
"#9a60b4",
|
|
92
|
-
"#ea7ccc",
|
|
93
|
-
"#67c23a"
|
|
94
|
-
];
|
|
95
|
-
const colors = base_colors || default_colors;
|
|
96
|
-
const result = [];
|
|
97
|
-
for (let i = 0; i < count; i++) {
|
|
98
|
-
result.push(colors[i % colors.length]);
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
102
|
-
function merge_chart_options(defaultOption, customOption) {
|
|
103
|
-
return {
|
|
104
|
-
...defaultOption,
|
|
105
|
-
...customOption,
|
|
106
|
-
// 特殊处理数组类型的配置
|
|
107
|
-
series: customOption.series || defaultOption.series,
|
|
108
|
-
xAxis: customOption.xAxis || defaultOption.xAxis,
|
|
109
|
-
yAxis: customOption.yAxis || defaultOption.yAxis,
|
|
110
|
-
legend: customOption.legend || defaultOption.legend,
|
|
111
|
-
tooltip: customOption.tooltip || defaultOption.tooltip,
|
|
112
|
-
title: customOption.title || defaultOption.title
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function add_chart_listener(chart, eventName, handler) {
|
|
116
|
-
if (chart && typeof chart.on === "function") {
|
|
117
|
-
chart.on(eventName, handler);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function remove_chart_listener(chart, eventName, handler) {
|
|
121
|
-
if (chart && typeof chart.off === "function") {
|
|
122
|
-
chart.off(eventName, handler);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
function export_chart_image(chart, options = {}) {
|
|
126
|
-
if (!chart || typeof chart.getDataURL !== "function") {
|
|
127
|
-
return null;
|
|
128
|
-
}
|
|
129
|
-
const {
|
|
130
|
-
type = "png",
|
|
131
|
-
pixel_ratio = 2,
|
|
132
|
-
background_color = "#fff",
|
|
133
|
-
exclude_components = [],
|
|
134
|
-
file_name = "echarts-image"
|
|
135
|
-
} = options;
|
|
136
|
-
const data_url = chart.getDataURL({
|
|
137
|
-
type,
|
|
138
|
-
pixelRatio: pixel_ratio,
|
|
139
|
-
backgroundColor: background_color,
|
|
140
|
-
excludeComponents: exclude_components
|
|
141
|
-
});
|
|
142
|
-
const link = document.createElement("a");
|
|
143
|
-
link.download = `${file_name}.${type}`;
|
|
144
|
-
link.href = data_url;
|
|
145
|
-
link.click();
|
|
146
|
-
return data_url;
|
|
147
|
-
}
|
|
148
|
-
export {
|
|
149
|
-
add_chart_listener,
|
|
150
|
-
destroy_chart,
|
|
151
|
-
export_chart_image,
|
|
152
|
-
generate_chart_colors,
|
|
153
|
-
get_chart_gradient_color,
|
|
154
|
-
init_chart,
|
|
155
|
-
make_chart_responsive,
|
|
156
|
-
merge_chart_options,
|
|
157
|
-
remove_chart_listener,
|
|
158
|
-
resize_chart,
|
|
159
|
-
update_chart
|
|
160
|
-
};
|
package/lib/use-chart/config.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { auto_size } from "@qy_better_lib/core";
|
|
2
|
-
const options = {
|
|
3
|
-
textStyle: {
|
|
4
|
-
fontSize: auto_size(12),
|
|
5
|
-
fontFamily: "Helvetica Neue, Arial, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, sans-serif"
|
|
6
|
-
},
|
|
7
|
-
color: [],
|
|
8
|
-
title: {
|
|
9
|
-
textStyle: {
|
|
10
|
-
fontSize: auto_size(14)
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
legend: {
|
|
14
|
-
icon: "rect",
|
|
15
|
-
itemHeight: auto_size(12),
|
|
16
|
-
itemWidth: auto_size(12),
|
|
17
|
-
textStyle: { color: "#606266", fontSize: auto_size(12) },
|
|
18
|
-
itemStyle: {
|
|
19
|
-
shadowColor: "rgba(161, 163, 170, 0.25)",
|
|
20
|
-
shadowBlur: auto_size(4),
|
|
21
|
-
shadowOffsetX: auto_size(4),
|
|
22
|
-
shadowOffsetY: auto_size(4)
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
tooltip: {
|
|
26
|
-
trigger: "axis",
|
|
27
|
-
axisPointer: {
|
|
28
|
-
lineStyle: {
|
|
29
|
-
color: "#D4D9E2",
|
|
30
|
-
width: auto_size(1)
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
backgroundColor: "rgba(0,0,0,0.6)",
|
|
34
|
-
textStyle: {
|
|
35
|
-
color: "#fff",
|
|
36
|
-
fontWeight: 400,
|
|
37
|
-
width: auto_size(10),
|
|
38
|
-
height: auto_size(10),
|
|
39
|
-
fontSize: auto_size(12)
|
|
40
|
-
},
|
|
41
|
-
borderWidth: 0,
|
|
42
|
-
padding: [auto_size(8), auto_size(16)],
|
|
43
|
-
formatter: "{a}<br/>{b}: {c}"
|
|
44
|
-
},
|
|
45
|
-
xAxis: {
|
|
46
|
-
axisLine: {
|
|
47
|
-
lineStyle: { color: "#D4D9E2", width: auto_size(1) }
|
|
48
|
-
},
|
|
49
|
-
axisLabel: { color: "#606266", fontSize: auto_size(12) },
|
|
50
|
-
nameTextStyle: { color: "#606266", fontSize: auto_size(12) },
|
|
51
|
-
splitLine: {
|
|
52
|
-
show: false,
|
|
53
|
-
lineStyle: {
|
|
54
|
-
color: "#EBECEF",
|
|
55
|
-
type: "dashed",
|
|
56
|
-
width: auto_size(1)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
yAxis: {
|
|
61
|
-
axisLine: {
|
|
62
|
-
show: false,
|
|
63
|
-
lineStyle: { color: "#D4D9E2", width: auto_size(1) }
|
|
64
|
-
},
|
|
65
|
-
axisLabel: { color: "#606266", fontSize: auto_size(12) },
|
|
66
|
-
nameTextStyle: { color: "#606266", fontSize: auto_size(12) },
|
|
67
|
-
splitLine: {
|
|
68
|
-
lineStyle: {
|
|
69
|
-
color: "#EBECEF",
|
|
70
|
-
type: "dashed",
|
|
71
|
-
width: auto_size(1)
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
export {
|
|
77
|
-
options as default
|
|
78
|
-
};
|
package/lib/use-emit/extend.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import mitt from "mitt";
|
|
2
|
-
function createAsyncEmitter() {
|
|
3
|
-
if (typeof mitt === "undefined") {
|
|
4
|
-
return {};
|
|
5
|
-
}
|
|
6
|
-
const emitter = mitt();
|
|
7
|
-
emitter.emitAsync = async function(type, ...args) {
|
|
8
|
-
const handlers = this.all?.get(type);
|
|
9
|
-
if (!handlers || handlers.length === 0) {
|
|
10
|
-
return [];
|
|
11
|
-
}
|
|
12
|
-
const promises = handlers.map((handler) => {
|
|
13
|
-
try {
|
|
14
|
-
const result = handler(...args);
|
|
15
|
-
return result instanceof Promise ? result : Promise.resolve(result);
|
|
16
|
-
} catch (error) {
|
|
17
|
-
return Promise.reject(error);
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
return Promise.all(promises);
|
|
21
|
-
};
|
|
22
|
-
emitter.emit;
|
|
23
|
-
emitter.emit = function(type, ...args) {
|
|
24
|
-
const handlers = this.all?.get(type);
|
|
25
|
-
if (!handlers || handlers.length === 0) {
|
|
26
|
-
return void 0;
|
|
27
|
-
}
|
|
28
|
-
let lastResult;
|
|
29
|
-
handlers.forEach((handler) => {
|
|
30
|
-
try {
|
|
31
|
-
lastResult = handler(...args);
|
|
32
|
-
} catch (error) {
|
|
33
|
-
console.error(`Error in event handler for ${type}:`, error);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
return lastResult;
|
|
37
|
-
};
|
|
38
|
-
return emitter;
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
createAsyncEmitter
|
|
42
|
-
};
|
package/lib/use-emit/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import mitt from "mitt";
|
|
2
|
-
import { onMounted, onUnmounted } from "vue";
|
|
3
|
-
import { createAsyncEmitter } from "./extend.js";
|
|
4
|
-
const emitter = createAsyncEmitter();
|
|
5
|
-
function use_emit(event_list) {
|
|
6
|
-
if (typeof mitt === "undefined") return { emitter: {} };
|
|
7
|
-
onMounted(() => {
|
|
8
|
-
if (event_list && event_list.length > 0) {
|
|
9
|
-
for (const event of event_list) {
|
|
10
|
-
emitter.on(event.key, event.value);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
onUnmounted(() => {
|
|
15
|
-
if (event_list && event_list.length > 0) {
|
|
16
|
-
for (const event of event_list) {
|
|
17
|
-
emitter.off(event.key, event.value);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
return {
|
|
22
|
-
emitter
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
export {
|
|
26
|
-
use_emit
|
|
27
|
-
};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { ref, shallowRef, onMounted, onUnmounted } from "vue";
|
|
2
|
-
import { get_element, on, off } from "@qy_better_lib/core";
|
|
3
|
-
function use_fullscreen(selector) {
|
|
4
|
-
const full = ref(false);
|
|
5
|
-
const container = shallowRef(null);
|
|
6
|
-
function fullscreen_change() {
|
|
7
|
-
full.value = document.fullscreenElement === container.value;
|
|
8
|
-
}
|
|
9
|
-
onMounted(() => {
|
|
10
|
-
container.value = get_element(selector);
|
|
11
|
-
if (container.value) {
|
|
12
|
-
on(container.value, "fullscreenchange", fullscreen_change);
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
onUnmounted(() => {
|
|
16
|
-
if (container.value) {
|
|
17
|
-
off(container.value, "fullscreenchange", fullscreen_change);
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
function enter_fullscreen() {
|
|
21
|
-
if (!full.value && container.value?.requestFullscreen) {
|
|
22
|
-
container.value.requestFullscreen().catch((error) => {
|
|
23
|
-
console.error("进入全屏失败:", error);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function exit_fullscreen() {
|
|
28
|
-
if (document.exitFullscreen) {
|
|
29
|
-
document.exitFullscreen().catch((error) => {
|
|
30
|
-
console.error("退出全屏失败:", error);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function toggle_fullscreen() {
|
|
35
|
-
if (full.value) {
|
|
36
|
-
exit_fullscreen();
|
|
37
|
-
} else {
|
|
38
|
-
enter_fullscreen();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return {
|
|
42
|
-
container,
|
|
43
|
-
full,
|
|
44
|
-
toggle_fullscreen,
|
|
45
|
-
enter_fullscreen,
|
|
46
|
-
exit_fullscreen
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
export {
|
|
50
|
-
use_fullscreen
|
|
51
|
-
};
|
package/lib/use-image/index.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import { download_file, to_base64, compress_image } from "@qy_better_lib/core";
|
|
2
|
-
import { EImageType } from "./type.js";
|
|
3
|
-
function use_image() {
|
|
4
|
-
function check_image_type(type) {
|
|
5
|
-
return Object.values(EImageType).includes(type);
|
|
6
|
-
}
|
|
7
|
-
async function canvas_to_data_url(canvas, quality = 0.92, type = EImageType.JPEG) {
|
|
8
|
-
if (!check_image_type(type)) {
|
|
9
|
-
type = EImageType.JPEG;
|
|
10
|
-
}
|
|
11
|
-
return canvas.toDataURL(type, quality);
|
|
12
|
-
}
|
|
13
|
-
async function canvas_to_file(canvas, file_name = "image", quality = 0.92, type = EImageType.JPEG) {
|
|
14
|
-
const data_url = await canvas_to_data_url(canvas, quality, type);
|
|
15
|
-
return data_url_to_file(data_url, type, file_name);
|
|
16
|
-
}
|
|
17
|
-
async function data_url_to_file(data_url, type = EImageType.JPEG, file_name = "image") {
|
|
18
|
-
if (!check_image_type(type)) {
|
|
19
|
-
type = EImageType.JPEG;
|
|
20
|
-
}
|
|
21
|
-
const arr = data_url.split(",");
|
|
22
|
-
const mime = arr[0].match(/:(.*?);/)?.[1] || type;
|
|
23
|
-
const bstr = atob(arr[1]);
|
|
24
|
-
let n = bstr.length;
|
|
25
|
-
const u8arr = new Uint8Array(n);
|
|
26
|
-
while (n--) {
|
|
27
|
-
u8arr[n] = bstr.charCodeAt(n);
|
|
28
|
-
}
|
|
29
|
-
const extension = mime.split("/")[1];
|
|
30
|
-
const full_name = `${file_name}.${extension}`;
|
|
31
|
-
return new File([u8arr], full_name, { type: mime });
|
|
32
|
-
}
|
|
33
|
-
async function data_url_to_image(data_url) {
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
const image = new Image();
|
|
36
|
-
image.onload = () => resolve(image);
|
|
37
|
-
image.onerror = reject;
|
|
38
|
-
image.src = data_url;
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function download_file$1(blob, file_name = "download") {
|
|
42
|
-
const url = URL.createObjectURL(blob);
|
|
43
|
-
download_file({ path: url, name: file_name });
|
|
44
|
-
URL.revokeObjectURL(url);
|
|
45
|
-
}
|
|
46
|
-
async function file_to_data_url(file) {
|
|
47
|
-
const result = await to_base64(file);
|
|
48
|
-
if (!result) {
|
|
49
|
-
throw new Error("转换文件为DataURL失败");
|
|
50
|
-
}
|
|
51
|
-
return result;
|
|
52
|
-
}
|
|
53
|
-
async function image_to_canvas(image, config = {}) {
|
|
54
|
-
const canvas = document.createElement("canvas");
|
|
55
|
-
const context = canvas.getContext("2d");
|
|
56
|
-
if (!context) {
|
|
57
|
-
throw new Error("创建Canvas上下文失败");
|
|
58
|
-
}
|
|
59
|
-
const width = config.width || image.width;
|
|
60
|
-
const height = config.height || image.height;
|
|
61
|
-
const scale = config.scale || 1;
|
|
62
|
-
canvas.width = width * scale;
|
|
63
|
-
canvas.height = height * scale;
|
|
64
|
-
context.scale(scale, scale);
|
|
65
|
-
context.drawImage(image, 0, 0, width, height);
|
|
66
|
-
return canvas;
|
|
67
|
-
}
|
|
68
|
-
async function url_to_blob(url) {
|
|
69
|
-
const response = await fetch(url);
|
|
70
|
-
if (!response.ok) {
|
|
71
|
-
throw new Error(`获取图片失败: ${response.statusText}`);
|
|
72
|
-
}
|
|
73
|
-
return response.blob();
|
|
74
|
-
}
|
|
75
|
-
async function url_to_image(url) {
|
|
76
|
-
return new Promise((resolve, reject) => {
|
|
77
|
-
const image = new Image();
|
|
78
|
-
image.onload = () => resolve(image);
|
|
79
|
-
image.onerror = reject;
|
|
80
|
-
image.src = url;
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
async function compress(file, config = {}) {
|
|
84
|
-
if (!(file instanceof Blob)) {
|
|
85
|
-
throw new Error("compress(): 第一个参数必须是Blob对象或File对象");
|
|
86
|
-
}
|
|
87
|
-
const quality = Math.max(0, Math.min(1, isNaN(Number(config.quality)) ? 0.92 : Number(config.quality)));
|
|
88
|
-
config.quality = quality;
|
|
89
|
-
const data_url = await file_to_data_url(file);
|
|
90
|
-
const original_mime = data_url.split(",")[0].match(/:(.*?);/)?.[1];
|
|
91
|
-
let mime = EImageType.JPEG;
|
|
92
|
-
if (check_image_type(config.type)) {
|
|
93
|
-
mime = config.type;
|
|
94
|
-
}
|
|
95
|
-
const image = await data_url_to_image(data_url);
|
|
96
|
-
const canvas = await image_to_canvas(image, config);
|
|
97
|
-
const compress_data_url = await canvas_to_data_url(canvas, config.quality, mime);
|
|
98
|
-
const compress_file = await data_url_to_file(compress_data_url, original_mime || mime);
|
|
99
|
-
if (compress_file.size > file.size) {
|
|
100
|
-
return file;
|
|
101
|
-
}
|
|
102
|
-
return compress_file;
|
|
103
|
-
}
|
|
104
|
-
async function compress_accurately(file, config = {}) {
|
|
105
|
-
if (!(file instanceof Blob)) {
|
|
106
|
-
throw new Error("compressAccurately(): 第一个参数必须是Blob对象或File对象");
|
|
107
|
-
}
|
|
108
|
-
const size = Math.max(0, isNaN(Number(config.size)) ? 0 : Number(config.size));
|
|
109
|
-
config.size = size;
|
|
110
|
-
const accuracy = Math.max(0.8, Math.min(0.99, isNaN(Number(config.accuracy)) ? 0.95 : Number(config.accuracy)));
|
|
111
|
-
config.accuracy = accuracy;
|
|
112
|
-
if (isNaN(config.size)) {
|
|
113
|
-
return file;
|
|
114
|
-
}
|
|
115
|
-
if (config.size * 1024 > file.size) {
|
|
116
|
-
return file;
|
|
117
|
-
}
|
|
118
|
-
if (!config.accuracy || config.accuracy < 0.8 || config.accuracy > 0.99) {
|
|
119
|
-
config.accuracy = 0.95;
|
|
120
|
-
}
|
|
121
|
-
const result_size = {
|
|
122
|
-
max: config.size * (2 - config.accuracy) * 1024,
|
|
123
|
-
accurate: config.size * 1024,
|
|
124
|
-
min: config.size * config.accuracy * 1024
|
|
125
|
-
};
|
|
126
|
-
const data_url = await file_to_data_url(file);
|
|
127
|
-
const original_mime = data_url.split(",")[0].match(/:(.*?);/)?.[1];
|
|
128
|
-
let mime = EImageType.JPEG;
|
|
129
|
-
if (check_image_type(config.type)) {
|
|
130
|
-
mime = config.type;
|
|
131
|
-
}
|
|
132
|
-
const image = await data_url_to_image(data_url);
|
|
133
|
-
const canvas = await image_to_canvas(image, config);
|
|
134
|
-
const proportion = 0.75;
|
|
135
|
-
let image_quality = 1;
|
|
136
|
-
let compress_data_url = "";
|
|
137
|
-
const temp_data_urls = [];
|
|
138
|
-
for (let x = 1; x <= 7; x++) {
|
|
139
|
-
compress_data_url = await canvas_to_data_url(canvas, image_quality, mime);
|
|
140
|
-
const calculation_size = compress_data_url.length * proportion;
|
|
141
|
-
if (x === 7) {
|
|
142
|
-
if (result_size.max < calculation_size || result_size.min > calculation_size) {
|
|
143
|
-
compress_data_url = [compress_data_url, ...temp_data_urls].filter(Boolean).sort(
|
|
144
|
-
(a, b) => Math.abs(a.length * proportion - result_size.accurate) - Math.abs(b.length * proportion - result_size.accurate)
|
|
145
|
-
)[0];
|
|
146
|
-
}
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
if (result_size.max < calculation_size) {
|
|
150
|
-
temp_data_urls[1] = compress_data_url;
|
|
151
|
-
image_quality -= 0.5 ** (x + 1);
|
|
152
|
-
} else if (result_size.min > calculation_size) {
|
|
153
|
-
temp_data_urls[0] = compress_data_url;
|
|
154
|
-
image_quality += 0.5 ** (x + 1);
|
|
155
|
-
} else {
|
|
156
|
-
break;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
const compress_file = await data_url_to_file(
|
|
160
|
-
compress_data_url,
|
|
161
|
-
original_mime || mime
|
|
162
|
-
);
|
|
163
|
-
if (compress_file.size > file.size) {
|
|
164
|
-
return file;
|
|
165
|
-
}
|
|
166
|
-
return compress_file;
|
|
167
|
-
}
|
|
168
|
-
async function compress_image$1(file, max_width = 800, max_height = 800, quality = 0.8) {
|
|
169
|
-
return compress_image(file, max_width, max_height, quality);
|
|
170
|
-
}
|
|
171
|
-
return {
|
|
172
|
-
// 压缩功能
|
|
173
|
-
compress,
|
|
174
|
-
compress_accurately,
|
|
175
|
-
compress_image: compress_image$1,
|
|
176
|
-
// 转换功能
|
|
177
|
-
canvas_to_data_url,
|
|
178
|
-
canvas_to_file,
|
|
179
|
-
data_url_to_file,
|
|
180
|
-
data_url_to_image,
|
|
181
|
-
file_to_data_url,
|
|
182
|
-
image_to_canvas,
|
|
183
|
-
url_to_blob,
|
|
184
|
-
url_to_image,
|
|
185
|
-
// 工具功能
|
|
186
|
-
download_file: download_file$1
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
export {
|
|
190
|
-
EImageType,
|
|
191
|
-
use_image
|
|
192
|
-
};
|
package/lib/use-image/type.js
DELETED