@qy_better_lib/hooks 0.0.2
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/dist/@qy_better_lib/hooks.min.js +1 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +24 -0
- package/lib/use-emit/index.d.ts +14 -0
- package/lib/use-file/index.d.ts +12 -0
- package/lib/use-image/canvastoDataURL.d.ts +12 -0
- package/lib/use-image/canvastoDataURL.js +7 -0
- package/lib/use-image/canvastoFile.d.ts +12 -0
- package/lib/use-image/canvastoFile.js +9 -0
- package/lib/use-image/dataURLtoFile.d.ts +11 -0
- package/lib/use-image/dataURLtoFile.js +17 -0
- package/lib/use-image/dataURLtoImage.d.ts +7 -0
- package/lib/use-image/dataURLtoImage.js +9 -0
- package/lib/use-image/downloadFile.d.ts +7 -0
- package/lib/use-image/downloadFile.js +9 -0
- package/lib/use-image/filetoDataURL.d.ts +7 -0
- package/lib/use-image/filetoDataURL.js +12 -0
- package/lib/use-image/imagetoCanvas.d.ts +27 -0
- package/lib/use-image/imagetoCanvas.js +41 -0
- package/lib/use-image/index.d.ts +55 -0
- package/lib/use-image/index.js +74 -0
- package/lib/use-image/type.d.ts +25 -0
- package/lib/use-image/type.js +8 -0
- package/lib/use-image/urltoBlob.d.ts +8 -0
- package/lib/use-image/urltoBlob.js +6 -0
- package/lib/use-image/urltoImage.d.ts +7 -0
- package/lib/use-image/urltoImage.js +13 -0
- package/lib/use-print/index.d.ts +11 -0
- package/lib/use-waterMark/index.d.ts +17 -0
- package/lib/use-websocket/index.d.ts +15 -0
- package/package.json +50 -0
- package/src/index.ts +6 -0
- package/src/use-emit/index.ts +46 -0
- package/src/use-file/index.ts +39 -0
- package/src/use-image/canvastoDataURL.ts +21 -0
- package/src/use-image/canvastoFile.ts +20 -0
- package/src/use-image/dataURLtoFile.ts +29 -0
- package/src/use-image/dataURLtoImage.ts +17 -0
- package/src/use-image/downloadFile.ts +16 -0
- package/src/use-image/filetoDataURL.ts +13 -0
- package/src/use-image/imagetoCanvas.ts +107 -0
- package/src/use-image/index.ts +200 -0
- package/src/use-image/type.ts +31 -0
- package/src/use-image/urltoBlob.ts +10 -0
- package/src/use-image/urltoImage.ts +19 -0
- package/src/use-print/index.ts +128 -0
- package/src/use-waterMark/index.ts +101 -0
- package/src/use-websocket/index.ts +117 -0
- package/tsconfig.json +5 -0
- package/types/index.d.ts +1 -0
- package/types/json.d.ts +4 -0
- package/types/vue.shim.d.ts +5 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(n,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],h):(n=typeof globalThis<"u"?globalThis:n||self,h(n["@qy_better_lib/hooks"]={}))})(this,function(n){"use strict";var h=(a=>(a.PNG="image/png",a.JPEG="image/jpeg",a.GIF="image/gif",a))(h||{});function w(a){return["image/png","image/jpeg","image/gif"].some(e=>e===a)}async function y(a,e=.92,r=h.JPEG){return w(r)||(r=h.JPEG),a.toDataURL(r,e)}function P(a,e=.92,r=h.JPEG){return new Promise(t=>a.toBlob(i=>t(i),r,e))}async function g(a,e){var s;const r=a.split(",");let t=(s=r[0].match(/:(.*?);/))==null?void 0:s[1];const i=atob(r[1]);let o=i.length;const c=new Uint8Array(o);for(;o--;)c[o]=i.charCodeAt(o);return w(e)&&(t=e),new Blob([c],{type:t})}function p(a){return new Promise((e,r)=>{const t=new Image;t.onload=()=>e(t),t.onerror=()=>r(new Error("dataURLtoImage(): dataURL is illegal")),t.src=a})}function j(a,e){const r=document.createElement("a");r.href=window.URL.createObjectURL(a),r.download=e||Date.now().toString(36),document.body.appendChild(r);const t=document.createEvent("MouseEvents");t.initEvent("click",!1,!1),r.dispatchEvent(t),document.body.removeChild(r)}function I(a){return new Promise(e=>{const r=new FileReader;r.onloadend=t=>{var i;return e((i=t.target)==null?void 0:i.result)},r.readAsDataURL(a)})}async function f(a,e={}){const r={...e},t=document.createElement("canvas"),i=t.getContext("2d");let o,c;for(const s in r)Object.prototype.hasOwnProperty.call(r,s)&&(r[s]=Number(r[s]));if(!r.scale)c=r.width||(r.height||0)*a.width/a.height||a.width,o=r.height||(r.width||0)*a.height/a.width||a.height;else{const s=r.scale>0&&r.scale<10?r.scale:1;c=a.width*s,o=a.height*s}switch([5,6,7,8].some(s=>s===r.orientation)?(t.height=c,t.width=o):(t.height=o,t.width=c),r.orientation){case 3:i.rotate(180*Math.PI/180),i.drawImage(a,-t.width,-t.height,t.width,t.height);break;case 6:i.rotate(90*Math.PI/180),i.drawImage(a,0,-t.width,t.height,t.width);break;case 8:i.rotate(270*Math.PI/180),i.drawImage(a,-t.height,0,t.height,t.width);break;case 2:i.translate(t.width,0),i.scale(-1,1),i.drawImage(a,0,0,t.width,t.height);break;case 4:i.translate(t.width,0),i.scale(-1,1),i.rotate(180*Math.PI/180),i.drawImage(a,-t.width,-t.height,t.width,t.height);break;case 5:i.translate(t.width,0),i.scale(-1,1),i.rotate(90*Math.PI/180),i.drawImage(a,0,-t.width,t.height,t.width);break;case 7:i.translate(t.width,0),i.scale(-1,1),i.rotate(270*Math.PI/180),i.drawImage(a,-t.height,0,t.height,t.width);break;default:i.drawImage(a,0,0,t.width,t.height)}return t}function z(a){return fetch(a).then(e=>e.blob())}function k(a){return new Promise((e,r)=>{const t=new Image;t.onload=()=>e(t),t.onerror=()=>r(new Error("urltoImage(): Image failed to load, please check the image URL")),t.src=a})}async function E(a,e={}){var d;if(!(a instanceof Blob))throw new Error("compress(): First arg must be a Blob object or a File object.");if(typeof e!="object"&&(e=Object.assign({quality:e})),e.quality=Number(e.quality),Number.isNaN(e.quality))return a;const r=await I(a);let t=(d=r.split(",")[0].match(/:(.*?);/))==null?void 0:d[1],i=h.JPEG;w(e.type)&&(i=e.type,t=e.type);const o=await p(r),c=await f(o,Object.assign({},e)),s=await y(c,e.quality,i),u=await g(s,t);return u.size>a.size?a:u}async function F(a,e={}){var L;if(!(a instanceof Blob))throw new Error("compressAccurately(): First arg must be a Blob object or a File object.");if(typeof e!="object"&&(e=Object.assign({size:e})),e.size=Number(e.size),Number.isNaN(e.size)||e.size*1024>a.size)return a;e.accuracy=Number(e.accuracy),(!e.accuracy||e.accuracy<.8||e.accuracy>.99)&&(e.accuracy=.95);const r={max:e.size*(2-e.accuracy)*1024,accurate:e.size*1024,min:e.size*e.accuracy*1024},t=await I(a);let i=(L=t.split(",")[0].match(/:(.*?);/))==null?void 0:L[1],o=h.JPEG;w(e.type)&&(o=e.type,i=e.type);const c=await p(t),s=await f(c,Object.assign({},e)),u=.75;let d=1,l;const R=new Array(2);for(let m=1;m<=7;m++){l=await y(s,d,o);const b=l.length*u;if(m===7){(r.max<b||r.min>b)&&(l=[l,...R].filter(U=>U).sort((U,M)=>Math.abs(U.length*u-r.accurate)-Math.abs(M.length*u-r.accurate))[0]);break}if(r.max<b)R[1]=l,d-=.5**(m+1);else if(r.min>b)R[0]=l,d+=.5**(m+1);else break}const v=await g(l,i);return v.size>a.size?a:v}n.canvastoDataURL=y,n.canvastoFile=P,n.compress=E,n.compressAccurately=F,n.dataURLtoFile=g,n.dataURLtoImage=p,n.downloadFile=j,n.filetoDataURL=I,n.imagetoCanvas=f,n.urltoBlob=z,n.urltoImage=k,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import "vue";
|
|
2
|
+
import { compress as e, compressAccurately as r } from "./use-image/index.js";
|
|
3
|
+
import { default as l } from "./use-image/canvastoDataURL.js";
|
|
4
|
+
import { default as s } from "./use-image/canvastoFile.js";
|
|
5
|
+
import { default as p } from "./use-image/dataURLtoFile.js";
|
|
6
|
+
import { default as x } from "./use-image/dataURLtoImage.js";
|
|
7
|
+
import { default as i } from "./use-image/downloadFile.js";
|
|
8
|
+
import { default as L } from "./use-image/filetoDataURL.js";
|
|
9
|
+
import { default as U } from "./use-image/imagetoCanvas.js";
|
|
10
|
+
import { default as v } from "./use-image/urltoBlob.js";
|
|
11
|
+
import { default as D } from "./use-image/urltoImage.js";
|
|
12
|
+
export {
|
|
13
|
+
l as canvastoDataURL,
|
|
14
|
+
s as canvastoFile,
|
|
15
|
+
e as compress,
|
|
16
|
+
r as compressAccurately,
|
|
17
|
+
p as dataURLtoFile,
|
|
18
|
+
x as dataURLtoImage,
|
|
19
|
+
i as downloadFile,
|
|
20
|
+
L as filetoDataURL,
|
|
21
|
+
U as imagetoCanvas,
|
|
22
|
+
v as urltoBlob,
|
|
23
|
+
D as urltoImage
|
|
24
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Emitter, EventType } from 'mitt';
|
|
2
|
+
|
|
3
|
+
interface UseEitter {
|
|
4
|
+
/**
|
|
5
|
+
* 全局事件对象
|
|
6
|
+
*/
|
|
7
|
+
emitter: Emitter<Record<EventType, unknown>>;
|
|
8
|
+
}
|
|
9
|
+
type event = {
|
|
10
|
+
key: string;
|
|
11
|
+
value: (obj?: any) => void;
|
|
12
|
+
};
|
|
13
|
+
export default function useEitter(eventlist?: Array<event>): UseEitter;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EImageType } from './type';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 将一个Canvas对象转变为一个dataURL字符串
|
|
5
|
+
* 该方法可以做压缩处理
|
|
6
|
+
*
|
|
7
|
+
* @param {canvas} canvas
|
|
8
|
+
* @param {number=} quality - 传入范围 0-1,表示图片压缩质量,默认0.92
|
|
9
|
+
* @param {string=} type - 确定转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif",默认"image/jpeg"
|
|
10
|
+
* @returns {Promise(string)} Promise含有一个dataURL字符串参数
|
|
11
|
+
*/
|
|
12
|
+
export default function canvastoDataURL(canvas: HTMLCanvasElement, quality?: number, type?: EImageType): Promise<string>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EImageType } from './type';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 将一个canvas对象转变为一个File(Blob)对象
|
|
5
|
+
* 该方法可以做压缩处理
|
|
6
|
+
*
|
|
7
|
+
* @param {canvas} canvas
|
|
8
|
+
* @param {number=} quality - 传入范围 0-1,表示图片压缩质量,默认0.92
|
|
9
|
+
* @param {string=} type - 确定转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif",默认"image/jpeg"
|
|
10
|
+
* @returns {Promise(Blob)}
|
|
11
|
+
*/
|
|
12
|
+
export default function canvastoFile(canvas: HTMLCanvasElement, quality?: number, type?: EImageType): Promise<Blob>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EImageType } from './type';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 将一个dataURL字符串转变为一个File(Blob)对象
|
|
5
|
+
* 转变时可以确定File对象的类型
|
|
6
|
+
*
|
|
7
|
+
* @param {string} dataURL
|
|
8
|
+
* @param {string=} type - 确定转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif"
|
|
9
|
+
* @returns {Promise(Blob)}
|
|
10
|
+
*/
|
|
11
|
+
export default function dataURLtoFile(dataURL: string, type: EImageType): Promise<Blob>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { checkImageType as l } from "./type.js";
|
|
2
|
+
async function m(i, e) {
|
|
3
|
+
var c;
|
|
4
|
+
const r = i.split(",");
|
|
5
|
+
let a = (c = r[0].match(/:(.*?);/)) == null ? void 0 : c[1];
|
|
6
|
+
const n = atob(r[1]);
|
|
7
|
+
let t = n.length;
|
|
8
|
+
const o = new Uint8Array(t);
|
|
9
|
+
for (; t--; )
|
|
10
|
+
o[t] = n.charCodeAt(t);
|
|
11
|
+
return l(e) && (a = e), new Blob([o], {
|
|
12
|
+
type: a
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
m as default
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Image2CanvasConfig } from './type';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 将一个image对象转变为一个canvas对象
|
|
5
|
+
*
|
|
6
|
+
* @param {image} image
|
|
7
|
+
*
|
|
8
|
+
* @typedef {Object=} config - 转变为canvas时的一些参数配置
|
|
9
|
+
* @param {number} width - canvas图像的宽度,默认为image的宽度
|
|
10
|
+
* @param {number} height - canvas图像的高度,默认为image的高度
|
|
11
|
+
* @param {number} scale - 相对于image的缩放比例,范围0-10,默认不缩放;
|
|
12
|
+
* 设置config.scale后会覆盖config.width和config.height的设置;
|
|
13
|
+
* @param {number} orientation - 图片旋转参数,默认不旋转,参考如下:
|
|
14
|
+
* 参数 旋转方向
|
|
15
|
+
* 1 0°
|
|
16
|
+
* 2 水平翻转
|
|
17
|
+
* 3 180°
|
|
18
|
+
* 4 垂直翻转
|
|
19
|
+
* 5 顺时针90°+水平翻转
|
|
20
|
+
* 6 顺时针90°
|
|
21
|
+
* 7 顺时针90°+垂直翻转
|
|
22
|
+
* 8 逆时针90°
|
|
23
|
+
* @type {config}
|
|
24
|
+
*
|
|
25
|
+
* @returns {Promise(canvas)}
|
|
26
|
+
*/
|
|
27
|
+
export default function imagetoCanvas(image: HTMLImageElement, config?: Image2CanvasConfig): Promise<HTMLCanvasElement>;
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { default as canvastoDataURL } from './canvastoDataURL';
|
|
2
|
+
import { default as canvastoFile } from './canvastoFile';
|
|
3
|
+
import { default as dataURLtoFile } from './dataURLtoFile';
|
|
4
|
+
import { default as dataURLtoImage } from './dataURLtoImage';
|
|
5
|
+
import { default as downloadFile } from './downloadFile';
|
|
6
|
+
import { default as filetoDataURL } from './filetoDataURL';
|
|
7
|
+
import { default as imagetoCanvas } from './imagetoCanvas';
|
|
8
|
+
import { default as urltoBlob } from './urltoBlob';
|
|
9
|
+
import { default as urltoImage } from './urltoImage';
|
|
10
|
+
import { ICompressConfig, compressAccuratelyConfig } from './type';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 压缩File(Blob)对象
|
|
14
|
+
* @param {Blob} file - 一个File(Blob)对象
|
|
15
|
+
* @param {(number|object)} config - 如果传入是number类型,传入范围 0-1,表示图片压缩质量,默认0.92;也可以传入object类型,以便更详细的配置
|
|
16
|
+
* @example
|
|
17
|
+
* imageConversion.compress(file,0.8)
|
|
18
|
+
*
|
|
19
|
+
* imageConversion.compress(file,{
|
|
20
|
+
* quality: 0.8, //图片压缩质量
|
|
21
|
+
* type:"image/png", //转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif"
|
|
22
|
+
* width: 300, //生成图片的宽度
|
|
23
|
+
* height:200, //生产图片的高度
|
|
24
|
+
* scale: 0.5, //相对于原始图片的缩放比率,设置config.scale后会覆盖config.width和config.height的设置;
|
|
25
|
+
* orientation:2, //图片旋转方向
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* @returns {Promise(Blob)}
|
|
29
|
+
*/
|
|
30
|
+
declare function compress(file: File, config?: ICompressConfig): Promise<Blob>;
|
|
31
|
+
/**
|
|
32
|
+
* 根据体积压缩File(Blob)对象
|
|
33
|
+
*
|
|
34
|
+
* @param {Blob} file - 一个File(Blob)对象
|
|
35
|
+
* @param {(number|object)} config - 如果传入是number类型,则指定压缩图片的体积,单位Kb;也可以传入object类型,以便更详细的配置
|
|
36
|
+
* @param {number} size - 指定压缩图片的体积,单位Kb
|
|
37
|
+
* @param {number} accuracy - 相对于指定压缩体积的精确度,范围0.8-0.99,默认0.95;
|
|
38
|
+
* 如果设置 图片体积1000Kb,精确度0.9,则压缩结果为900Kb-1100Kb的图片都算合格;
|
|
39
|
+
* @example
|
|
40
|
+
* imageConversion.compress(file,100) //压缩后图片大小为100kb
|
|
41
|
+
*
|
|
42
|
+
* imageConversion.compress(file,{
|
|
43
|
+
* size: 100, //图片压缩体积,单位Kb
|
|
44
|
+
* accuracy: 0.9, //图片压缩体积的精确度,默认0.95
|
|
45
|
+
* type:"image/png", //转换后的图片类型,选项有 "image/png", "image/jpeg", "image/gif"
|
|
46
|
+
* width: 300, //生成图片的宽度
|
|
47
|
+
* height: 200, //生产图片的高度
|
|
48
|
+
* scale: 0.5, //相对于原始图片的缩放比率,设置config.scale后会覆盖config.width和config.height的设置;
|
|
49
|
+
* orientation:2, //图片旋转方向
|
|
50
|
+
* })
|
|
51
|
+
*
|
|
52
|
+
* @returns {Promise(Blob)}
|
|
53
|
+
*/
|
|
54
|
+
declare function compressAccurately(file: Blob, config?: compressAccuratelyConfig): Promise<Blob>;
|
|
55
|
+
export { canvastoDataURL, canvastoFile, dataURLtoFile, dataURLtoImage, downloadFile, filetoDataURL, imagetoCanvas, urltoBlob, urltoImage, compress, compressAccurately, };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import h from "./canvastoDataURL.js";
|
|
2
|
+
import { EImageType as j, checkImageType as L } from "./type.js";
|
|
3
|
+
import N from "./dataURLtoFile.js";
|
|
4
|
+
import R from "./dataURLtoImage.js";
|
|
5
|
+
import U from "./filetoDataURL.js";
|
|
6
|
+
import F from "./imagetoCanvas.js";
|
|
7
|
+
async function M(t, a = {}) {
|
|
8
|
+
var i;
|
|
9
|
+
if (!(t instanceof Blob))
|
|
10
|
+
throw new Error(
|
|
11
|
+
"compress(): First arg must be a Blob object or a File object."
|
|
12
|
+
);
|
|
13
|
+
if (typeof a != "object" && (a = Object.assign({
|
|
14
|
+
quality: a
|
|
15
|
+
})), a.quality = Number(a.quality), Number.isNaN(a.quality))
|
|
16
|
+
return t;
|
|
17
|
+
const e = await U(t);
|
|
18
|
+
let m = (i = e.split(",")[0].match(/:(.*?);/)) == null ? void 0 : i[1], c = j.JPEG;
|
|
19
|
+
L(a.type) && (c = a.type, m = a.type);
|
|
20
|
+
const u = await R(e), y = await F(u, Object.assign({}, a)), l = await h(y, a.quality, c), s = await N(l, m);
|
|
21
|
+
return s.size > t.size ? t : s;
|
|
22
|
+
}
|
|
23
|
+
async function O(t, a = {}) {
|
|
24
|
+
var z;
|
|
25
|
+
if (!(t instanceof Blob))
|
|
26
|
+
throw new Error(
|
|
27
|
+
"compressAccurately(): First arg must be a Blob object or a File object."
|
|
28
|
+
);
|
|
29
|
+
if (typeof a != "object" && (a = Object.assign({
|
|
30
|
+
size: a
|
|
31
|
+
})), a.size = Number(a.size), Number.isNaN(a.size) || a.size * 1024 > t.size)
|
|
32
|
+
return t;
|
|
33
|
+
a.accuracy = Number(a.accuracy), (!a.accuracy || a.accuracy < 0.8 || a.accuracy > 0.99) && (a.accuracy = 0.95);
|
|
34
|
+
const e = {
|
|
35
|
+
max: a.size * (2 - a.accuracy) * 1024,
|
|
36
|
+
accurate: a.size * 1024,
|
|
37
|
+
min: a.size * a.accuracy * 1024
|
|
38
|
+
}, m = await U(t);
|
|
39
|
+
let c = (z = m.split(",")[0].match(/:(.*?);/)) == null ? void 0 : z[1], u = j.JPEG;
|
|
40
|
+
L(a.type) && (u = a.type, c = a.type);
|
|
41
|
+
const y = await R(m), l = await F(y, Object.assign({}, a)), s = 0.75;
|
|
42
|
+
let i = 1, r;
|
|
43
|
+
const b = new Array(2);
|
|
44
|
+
for (let o = 1; o <= 7; o++) {
|
|
45
|
+
r = await h(l, i, u);
|
|
46
|
+
const p = r.length * s;
|
|
47
|
+
if (o === 7) {
|
|
48
|
+
(e.max < p || e.min > p) && (r = [r, ...b].filter((n) => n).sort(
|
|
49
|
+
(n, q) => Math.abs(n.length * s - e.accurate) - Math.abs(q.length * s - e.accurate)
|
|
50
|
+
)[0]);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
if (e.max < p)
|
|
54
|
+
b[1] = r, i -= 0.5 ** (o + 1);
|
|
55
|
+
else if (e.min > p)
|
|
56
|
+
b[0] = r, i += 0.5 ** (o + 1);
|
|
57
|
+
else
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
const w = await N(
|
|
61
|
+
r,
|
|
62
|
+
c
|
|
63
|
+
);
|
|
64
|
+
return w.size > t.size ? t : w;
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
h as canvastoDataURL,
|
|
68
|
+
M as compress,
|
|
69
|
+
O as compressAccurately,
|
|
70
|
+
N as dataURLtoFile,
|
|
71
|
+
R as dataURLtoImage,
|
|
72
|
+
U as filetoDataURL,
|
|
73
|
+
F as imagetoCanvas
|
|
74
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare enum EImageType {
|
|
2
|
+
"PNG" = "image/png",
|
|
3
|
+
"JPEG" = "image/jpeg",
|
|
4
|
+
"GIF" = "image/gif"
|
|
5
|
+
}
|
|
6
|
+
interface IBaseConfig {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
export interface Image2CanvasConfig extends IBaseConfig {
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
scale?: number;
|
|
13
|
+
orientation?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface ICompressConfig extends Image2CanvasConfig {
|
|
16
|
+
quality?: number;
|
|
17
|
+
type?: EImageType;
|
|
18
|
+
}
|
|
19
|
+
export interface compressAccuratelyConfig extends Image2CanvasConfig {
|
|
20
|
+
size?: number;
|
|
21
|
+
accuracy?: number;
|
|
22
|
+
type?: EImageType;
|
|
23
|
+
}
|
|
24
|
+
export declare function checkImageType(type: EImageType): boolean;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface UsePrintReturn {
|
|
2
|
+
printHtml: (domNode: string) => void;
|
|
3
|
+
printHtmlElement: (elementArray: Element[]) => void;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* ==============================================
|
|
7
|
+
* 打印页面元素
|
|
8
|
+
* ==============================================
|
|
9
|
+
*/
|
|
10
|
+
export default function usePrintHtml(): UsePrintReturn;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type UseWaterMark = {
|
|
2
|
+
/**
|
|
3
|
+
* 创建水印
|
|
4
|
+
*/
|
|
5
|
+
createWaterMark: (warper: string | null, str1: string, str2?: string, textColor?: string) => void;
|
|
6
|
+
/**
|
|
7
|
+
* 移除水印
|
|
8
|
+
*/
|
|
9
|
+
removeWatermark: () => void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* ==============================================
|
|
13
|
+
* 水印功能
|
|
14
|
+
* ==============================================
|
|
15
|
+
*/
|
|
16
|
+
export default function useWaterMark(): UseWaterMark;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type WebSocketOption = {
|
|
2
|
+
/**服务端地址 */
|
|
3
|
+
server: string;
|
|
4
|
+
/**消息接收处理方法 */
|
|
5
|
+
receive: (res: any) => void;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* WebSocket
|
|
9
|
+
*/
|
|
10
|
+
export default function useWebSocket(option: WebSocketOption): {
|
|
11
|
+
ws: undefined;
|
|
12
|
+
create: () => void;
|
|
13
|
+
socketClose: () => void;
|
|
14
|
+
sendMessage: (msg: any) => void;
|
|
15
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@qy_better_lib/hooks",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"description": "qy better lib hooks",
|
|
6
|
+
"author": "luhuiming",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build:lib": "vite build --config vite.lib.config.ts",
|
|
10
|
+
"build:dist-min": "vite build --config vite.dist.min.config.ts",
|
|
11
|
+
"build": "yarn build:lib && yarn build:dist-min"
|
|
12
|
+
},
|
|
13
|
+
"main": "lib/index.js",
|
|
14
|
+
"module": "lib/index.js",
|
|
15
|
+
"types": "lib/index.d.ts",
|
|
16
|
+
"typesVersions": {
|
|
17
|
+
"*": {
|
|
18
|
+
"lib/index.js": [
|
|
19
|
+
"lib/index.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"@qy-better-lib/core": [
|
|
22
|
+
"lib/index.d.ts"
|
|
23
|
+
],
|
|
24
|
+
"*": [
|
|
25
|
+
"*",
|
|
26
|
+
"dist/*",
|
|
27
|
+
"lib/*",
|
|
28
|
+
"lib/*.d.ts",
|
|
29
|
+
"lib/*/index.d.ts"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./lib/index.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"registry": "https://registry.npmjs.org/"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^20.14.9",
|
|
44
|
+
"@vitejs/plugin-vue": "^5.0.5",
|
|
45
|
+
"mitt": "^3.0.1",
|
|
46
|
+
"vite": "^5.3.2",
|
|
47
|
+
"vite-plugin-dts": "^3.9.1",
|
|
48
|
+
"vue": "^3.4.31"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import mitt, { Emitter, EventType } from "mitt";
|
|
2
|
+
import { onMounted, onUnmounted } from "vue";
|
|
3
|
+
|
|
4
|
+
interface UseEitter {
|
|
5
|
+
/**
|
|
6
|
+
* 全局事件对象
|
|
7
|
+
*/
|
|
8
|
+
emitter: Emitter<Record<EventType, unknown>>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type event = { key: string; value: (obj?: any) => void };
|
|
12
|
+
|
|
13
|
+
const emitter: Emitter<Record<EventType, unknown>> = mitt();
|
|
14
|
+
|
|
15
|
+
/**重写调用,返回事件执行结果 */
|
|
16
|
+
emitter.emit = (type: string, evt?: any): any => {
|
|
17
|
+
let handlers = emitter.all!.get(type);
|
|
18
|
+
if (handlers) {
|
|
19
|
+
return handlers[0]?.(evt!, undefined) as any;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default function useEitter(eventlist?: Array<event>): UseEitter {
|
|
25
|
+
onMounted((): void => {
|
|
26
|
+
if (eventlist && eventlist.length > 0) {
|
|
27
|
+
for (let i = 0; i < eventlist.length; i++) {
|
|
28
|
+
emitter.on(eventlist[i].key, eventlist[i].value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
onUnmounted((): void => {
|
|
34
|
+
if (eventlist && eventlist.length > 0) {
|
|
35
|
+
for (let i = 0; i < eventlist.length; i++) {
|
|
36
|
+
emitter.off(eventlist[i].key, eventlist[i].value);
|
|
37
|
+
// 从map中移除
|
|
38
|
+
emitter.all.delete(eventlist[i].key);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
emitter,
|
|
45
|
+
};
|
|
46
|
+
}
|