hsu-utils 0.0.10 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -2
- package/dist/hsu-utils.js +138 -6
- package/dist/hsu-utils.min.js +1 -1
- package/dist/hsu-utils.min.js.LICENSE.txt +31 -1
- package/es/DeepCopy/index.d.ts +1 -1
- package/es/DeepCopy/index.js +18 -2
- package/es/DownloadFile/index.d.ts +1 -0
- package/es/DownloadFile/index.js +34 -0
- package/es/GetStrWidth/index.js +1 -1
- package/es/LoadImage/index.d.ts +1 -0
- package/es/LoadImage/index.js +32 -0
- package/es/RenderPDF/index.d.ts +8 -0
- package/es/RenderPDF/index.js +60 -0
- package/es/Typeof/index.d.ts +4 -2
- package/es/Typeof/index.js +11 -3
- package/es/index.d.ts +4 -1
- package/es/index.js +4 -1
- package/lib/DeepCopy/index.d.ts +1 -1
- package/lib/DeepCopy/index.js +18 -2
- package/lib/DownloadFile/index.d.ts +1 -0
- package/lib/DownloadFile/index.js +37 -0
- package/lib/GetStrWidth/index.js +1 -1
- package/lib/LoadImage/index.d.ts +1 -0
- package/lib/LoadImage/index.js +65 -0
- package/lib/RenderPDF/index.d.ts +8 -0
- package/lib/RenderPDF/index.js +100 -0
- package/lib/Typeof/index.d.ts +4 -2
- package/lib/Typeof/index.js +11 -3
- package/lib/index.d.ts +4 -1
- package/lib/index.js +7 -1
- package/package.json +4 -2
@@ -1,6 +1,6 @@
|
|
1
1
|
/*!
|
2
2
|
*
|
3
|
-
* hsu-utils v0.0.
|
3
|
+
* hsu-utils v0.0.12
|
4
4
|
*
|
5
5
|
* some front-end utils
|
6
6
|
*
|
@@ -27,3 +27,33 @@
|
|
27
27
|
* SOFTWARE.
|
28
28
|
*
|
29
29
|
*/
|
30
|
+
|
31
|
+
/**
|
32
|
+
* @license
|
33
|
+
* web-streams-polyfill v3.3.3
|
34
|
+
* Copyright 2024 Mattias Buelens, Diwank Singh Tomer and other contributors.
|
35
|
+
* This code is released under the MIT license.
|
36
|
+
* SPDX-License-Identifier: MIT
|
37
|
+
*/
|
38
|
+
|
39
|
+
/**
|
40
|
+
* @licstart The following is the entire license notice for the
|
41
|
+
* Javascript code in this page
|
42
|
+
*
|
43
|
+
* Copyright 2022 Mozilla Foundation
|
44
|
+
*
|
45
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
46
|
+
* you may not use this file except in compliance with the License.
|
47
|
+
* You may obtain a copy of the License at
|
48
|
+
*
|
49
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
50
|
+
*
|
51
|
+
* Unless required by applicable law or agreed to in writing, software
|
52
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
53
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
54
|
+
* See the License for the specific language governing permissions and
|
55
|
+
* limitations under the License.
|
56
|
+
*
|
57
|
+
* @licend The above is the entire license notice for the
|
58
|
+
* Javascript code in this page
|
59
|
+
*/
|
package/es/DeepCopy/index.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export default function deepCopy<T =
|
1
|
+
export default function deepCopy<T = any>(data: T): T;
|
package/es/DeepCopy/index.js
CHANGED
@@ -3,7 +3,7 @@ export default function deepCopy(data) {
|
|
3
3
|
if (Typeof(data) === 'date') {
|
4
4
|
return new Date(data.toISOString());
|
5
5
|
}
|
6
|
-
|
6
|
+
if (Typeof(data) === 'formdata') {
|
7
7
|
const _data = data;
|
8
8
|
const _formData = new FormData();
|
9
9
|
_data.forEach((value, key) => {
|
@@ -11,7 +11,23 @@ export default function deepCopy(data) {
|
|
11
11
|
});
|
12
12
|
return _formData;
|
13
13
|
}
|
14
|
-
|
14
|
+
if (Typeof(data) === 'map') {
|
15
|
+
const _data = data;
|
16
|
+
const map = new Map();
|
17
|
+
_data.forEach((value, key) => {
|
18
|
+
map.set(key, value);
|
19
|
+
});
|
20
|
+
return map;
|
21
|
+
}
|
22
|
+
if (Typeof(data) === 'set') {
|
23
|
+
const _data = data;
|
24
|
+
const set = new Set();
|
25
|
+
_data.forEach((value) => {
|
26
|
+
set.add(value);
|
27
|
+
});
|
28
|
+
return set;
|
29
|
+
}
|
30
|
+
if (Array.isArray(data)) {
|
15
31
|
const newData = data.map((item) => {
|
16
32
|
if (typeof item === 'object') {
|
17
33
|
return deepCopy(item);
|
@@ -0,0 +1 @@
|
|
1
|
+
export default function downloadFile(file: ArrayBuffer | string, fileName: string): void;
|
@@ -0,0 +1,34 @@
|
|
1
|
+
function downloadFileByLocalPath(path, fileName) {
|
2
|
+
fetch(path)
|
3
|
+
.then((response) => response.arrayBuffer())
|
4
|
+
.then((arrayBuffer) => {
|
5
|
+
downloadFile(arrayBuffer, fileName);
|
6
|
+
});
|
7
|
+
}
|
8
|
+
function downloadFileByUrl(url, fileName) {
|
9
|
+
if (!url.startsWith('http')) {
|
10
|
+
downloadFileByLocalPath(url, fileName);
|
11
|
+
return;
|
12
|
+
}
|
13
|
+
const downloadElement = document.createElement('a');
|
14
|
+
downloadElement.href = url;
|
15
|
+
downloadElement.download = decodeURI(fileName || '');
|
16
|
+
document.body.appendChild(downloadElement);
|
17
|
+
downloadElement.click();
|
18
|
+
document.body.removeChild(downloadElement);
|
19
|
+
}
|
20
|
+
export default function downloadFile(file, fileName) {
|
21
|
+
if (typeof file === 'string') {
|
22
|
+
downloadFileByUrl(file, fileName);
|
23
|
+
return;
|
24
|
+
}
|
25
|
+
const blob = new Blob([file]);
|
26
|
+
const downloadElement = document.createElement('a');
|
27
|
+
const href = window.URL.createObjectURL(blob);
|
28
|
+
downloadElement.href = href;
|
29
|
+
downloadElement.download = decodeURI(fileName || '');
|
30
|
+
document.body.appendChild(downloadElement);
|
31
|
+
downloadElement.click();
|
32
|
+
document.body.removeChild(downloadElement);
|
33
|
+
window.URL.revokeObjectURL(href);
|
34
|
+
}
|
package/es/GetStrWidth/index.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
export default function get_string_width(str, font = {}) {
|
2
|
-
const { style = 'normal', variant = 'normal', weight = 'normal', size = 12, lineHeight = 1, family: fontFamily = '
|
2
|
+
const { style = 'normal', variant = 'normal', weight = 'normal', size = 12, lineHeight = 1, family: fontFamily = '微软雅黑' } = font;
|
3
3
|
const canvas = document.createElement('canvas');
|
4
4
|
const ctx = canvas.getContext('2d');
|
5
5
|
ctx.font = `${style} ${variant} ${weight} ${size}px/${lineHeight} ${fontFamily}`;
|
@@ -0,0 +1 @@
|
|
1
|
+
export default function loadImage(url: string): Promise<HTMLImageElement>;
|
@@ -0,0 +1,32 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
const imagePromiseCache = {};
|
11
|
+
const imageCache = {};
|
12
|
+
export default function loadImage(url) {
|
13
|
+
return __awaiter(this, void 0, void 0, function* () {
|
14
|
+
if (imageCache[url])
|
15
|
+
return imageCache[url];
|
16
|
+
if (imagePromiseCache[url])
|
17
|
+
return imagePromiseCache[url];
|
18
|
+
const imagePromise = new Promise((resolve, reject) => {
|
19
|
+
const image = new Image();
|
20
|
+
image.src = url;
|
21
|
+
image.onload = () => {
|
22
|
+
resolve(image);
|
23
|
+
imageCache[url] = image;
|
24
|
+
};
|
25
|
+
image.onerror = () => {
|
26
|
+
reject();
|
27
|
+
};
|
28
|
+
});
|
29
|
+
imagePromiseCache[url] = imagePromise;
|
30
|
+
return imagePromise;
|
31
|
+
});
|
32
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { GlobalWorkerOptions, getDocument } from 'pdfjs-dist/legacy/build/pdf.js';
|
11
|
+
GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
|
12
|
+
function clear(containerId) {
|
13
|
+
const container = document.getElementById(containerId);
|
14
|
+
const pages = document.querySelectorAll(`[id^="${containerId}-page-"]`);
|
15
|
+
pages === null || pages === void 0 ? void 0 : pages.forEach((item) => {
|
16
|
+
container === null || container === void 0 ? void 0 : container.removeChild(item);
|
17
|
+
});
|
18
|
+
}
|
19
|
+
function render(pdf, container, num) {
|
20
|
+
pdf.getPage(num).then((page) => {
|
21
|
+
const pageDiv = document.createElement('div');
|
22
|
+
pageDiv.setAttribute('id', `${container.id}-page-${num}`);
|
23
|
+
pageDiv.setAttribute('style', 'position: relative; ');
|
24
|
+
container.appendChild(pageDiv);
|
25
|
+
const canvas = document.createElement('canvas');
|
26
|
+
pageDiv.appendChild(canvas);
|
27
|
+
const ctx = canvas.getContext('2d');
|
28
|
+
const scale = 1;
|
29
|
+
const devicePixelRatio = window.devicePixelRatio * 2;
|
30
|
+
const viewport = page.getViewport({ scale: scale * devicePixelRatio });
|
31
|
+
canvas.style.width = '100%';
|
32
|
+
canvas.style.height = '100%';
|
33
|
+
canvas.width = viewport.width;
|
34
|
+
canvas.height = viewport.height;
|
35
|
+
const renderContext = {
|
36
|
+
canvasContext: ctx,
|
37
|
+
viewport: viewport
|
38
|
+
};
|
39
|
+
page.render(renderContext);
|
40
|
+
});
|
41
|
+
}
|
42
|
+
export default function renderPDF({ pdfUrl, containerId, startPageNum, endPageNum }) {
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
44
|
+
clear(containerId);
|
45
|
+
const loadingTask = getDocument({
|
46
|
+
url: pdfUrl,
|
47
|
+
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
48
|
+
cMapPacked: true
|
49
|
+
});
|
50
|
+
const pdf = yield loadingTask.promise;
|
51
|
+
const container = document.getElementById(containerId);
|
52
|
+
if (!container)
|
53
|
+
return;
|
54
|
+
const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
|
55
|
+
const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
|
56
|
+
for (let i = start; i <= end; i++) {
|
57
|
+
render(pdf, container, i);
|
58
|
+
}
|
59
|
+
});
|
60
|
+
}
|
package/es/Typeof/index.d.ts
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
-
type
|
2
|
-
|
1
|
+
type BaseType = 'string' | 'number' | 'boolean' | 'undefined' | 'function' | 'symbol' | 'bigint';
|
2
|
+
type ObjectType = 'object' | 'array' | 'null' | 'date' | 'formdata' | 'set' | 'map' | 'regexp' | 'arraybuffer' | 'blob';
|
3
|
+
type Type = BaseType | ObjectType;
|
4
|
+
export default function Typeof<T>(value: T, isType?: Type): boolean | Type;
|
3
5
|
export {};
|
package/es/Typeof/index.js
CHANGED
@@ -1,6 +1,14 @@
|
|
1
|
-
export default function Typeof(value) {
|
1
|
+
export default function Typeof(value, isType) {
|
2
2
|
const object_type = Object.prototype.toString.call(value);
|
3
3
|
const reg = new RegExp(' (.+?)]');
|
4
|
-
const type = object_type.match(reg)[1];
|
5
|
-
|
4
|
+
const type = object_type.match(reg)[1].toLowerCase();
|
5
|
+
if (!isType) {
|
6
|
+
return type;
|
7
|
+
}
|
8
|
+
if (typeof value !== 'object') {
|
9
|
+
return typeof value === isType;
|
10
|
+
}
|
11
|
+
else {
|
12
|
+
return type === isType;
|
13
|
+
}
|
6
14
|
}
|
package/es/index.d.ts
CHANGED
@@ -4,6 +4,9 @@ import Equal from './Equal';
|
|
4
4
|
import Typeof from './Typeof';
|
5
5
|
import get_string_width from './GetStrWidth';
|
6
6
|
import ConvertNumbers from './ConvertNumbers';
|
7
|
-
|
7
|
+
import loadImage from './LoadImage';
|
8
|
+
import renderPDF from './RenderPDF';
|
9
|
+
import downloadFile from './DownloadFile';
|
10
|
+
export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile };
|
8
11
|
import { ConsoleData } from './ConsoleTable';
|
9
12
|
export type { ConsoleData };
|
package/es/index.js
CHANGED
@@ -4,4 +4,7 @@ import Equal from './Equal';
|
|
4
4
|
import Typeof from './Typeof';
|
5
5
|
import get_string_width from './GetStrWidth';
|
6
6
|
import ConvertNumbers from './ConvertNumbers';
|
7
|
-
|
7
|
+
import loadImage from './LoadImage';
|
8
|
+
import renderPDF from './RenderPDF';
|
9
|
+
import downloadFile from './DownloadFile';
|
10
|
+
export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile };
|
package/lib/DeepCopy/index.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export default function deepCopy<T =
|
1
|
+
export default function deepCopy<T = any>(data: T): T;
|
package/lib/DeepCopy/index.js
CHANGED
@@ -5,7 +5,7 @@ function deepCopy(data) {
|
|
5
5
|
if ((0, __1.Typeof)(data) === 'date') {
|
6
6
|
return new Date(data.toISOString());
|
7
7
|
}
|
8
|
-
|
8
|
+
if ((0, __1.Typeof)(data) === 'formdata') {
|
9
9
|
var _data = data;
|
10
10
|
var _formData_1 = new FormData();
|
11
11
|
_data.forEach(function (value, key) {
|
@@ -13,7 +13,23 @@ function deepCopy(data) {
|
|
13
13
|
});
|
14
14
|
return _formData_1;
|
15
15
|
}
|
16
|
-
|
16
|
+
if ((0, __1.Typeof)(data) === 'map') {
|
17
|
+
var _data = data;
|
18
|
+
var map_1 = new Map();
|
19
|
+
_data.forEach(function (value, key) {
|
20
|
+
map_1.set(key, value);
|
21
|
+
});
|
22
|
+
return map_1;
|
23
|
+
}
|
24
|
+
if ((0, __1.Typeof)(data) === 'set') {
|
25
|
+
var _data = data;
|
26
|
+
var set_1 = new Set();
|
27
|
+
_data.forEach(function (value) {
|
28
|
+
set_1.add(value);
|
29
|
+
});
|
30
|
+
return set_1;
|
31
|
+
}
|
32
|
+
if (Array.isArray(data)) {
|
17
33
|
var newData = data.map(function (item) {
|
18
34
|
if (typeof item === 'object') {
|
19
35
|
return deepCopy(item);
|
@@ -0,0 +1 @@
|
|
1
|
+
export default function downloadFile(file: ArrayBuffer | string, fileName: string): void;
|
@@ -0,0 +1,37 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
function downloadFileByLocalPath(path, fileName) {
|
4
|
+
fetch(path)
|
5
|
+
.then(function (response) { return response.arrayBuffer(); })
|
6
|
+
.then(function (arrayBuffer) {
|
7
|
+
downloadFile(arrayBuffer, fileName);
|
8
|
+
});
|
9
|
+
}
|
10
|
+
function downloadFileByUrl(url, fileName) {
|
11
|
+
if (!url.startsWith('http')) {
|
12
|
+
downloadFileByLocalPath(url, fileName);
|
13
|
+
return;
|
14
|
+
}
|
15
|
+
var downloadElement = document.createElement('a');
|
16
|
+
downloadElement.href = url;
|
17
|
+
downloadElement.download = decodeURI(fileName || '');
|
18
|
+
document.body.appendChild(downloadElement);
|
19
|
+
downloadElement.click();
|
20
|
+
document.body.removeChild(downloadElement);
|
21
|
+
}
|
22
|
+
function downloadFile(file, fileName) {
|
23
|
+
if (typeof file === 'string') {
|
24
|
+
downloadFileByUrl(file, fileName);
|
25
|
+
return;
|
26
|
+
}
|
27
|
+
var blob = new Blob([file]);
|
28
|
+
var downloadElement = document.createElement('a');
|
29
|
+
var href = window.URL.createObjectURL(blob);
|
30
|
+
downloadElement.href = href;
|
31
|
+
downloadElement.download = decodeURI(fileName || '');
|
32
|
+
document.body.appendChild(downloadElement);
|
33
|
+
downloadElement.click();
|
34
|
+
document.body.removeChild(downloadElement);
|
35
|
+
window.URL.revokeObjectURL(href);
|
36
|
+
}
|
37
|
+
exports.default = downloadFile;
|
package/lib/GetStrWidth/index.js
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
function get_string_width(str, font) {
|
4
4
|
if (font === void 0) { font = {}; }
|
5
|
-
var _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.variant, variant = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 12 : _d, _e = font.lineHeight, lineHeight = _e === void 0 ? 1 : _e, _f = font.family, fontFamily = _f === void 0 ? '
|
5
|
+
var _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.variant, variant = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 12 : _d, _e = font.lineHeight, lineHeight = _e === void 0 ? 1 : _e, _f = font.family, fontFamily = _f === void 0 ? '微软雅黑' : _f;
|
6
6
|
var canvas = document.createElement('canvas');
|
7
7
|
var ctx = canvas.getContext('2d');
|
8
8
|
ctx.font = "".concat(style, " ").concat(variant, " ").concat(weight, " ").concat(size, "px/").concat(lineHeight, " ").concat(fontFamily);
|
@@ -0,0 +1 @@
|
|
1
|
+
export default function loadImage(url: string): Promise<HTMLImageElement>;
|
@@ -0,0 +1,65 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
var imagePromiseCache = {};
|
40
|
+
var imageCache = {};
|
41
|
+
function loadImage(url) {
|
42
|
+
return __awaiter(this, void 0, void 0, function () {
|
43
|
+
var imagePromise;
|
44
|
+
return __generator(this, function (_a) {
|
45
|
+
if (imageCache[url])
|
46
|
+
return [2, imageCache[url]];
|
47
|
+
if (imagePromiseCache[url])
|
48
|
+
return [2, imagePromiseCache[url]];
|
49
|
+
imagePromise = new Promise(function (resolve, reject) {
|
50
|
+
var image = new Image();
|
51
|
+
image.src = url;
|
52
|
+
image.onload = function () {
|
53
|
+
resolve(image);
|
54
|
+
imageCache[url] = image;
|
55
|
+
};
|
56
|
+
image.onerror = function () {
|
57
|
+
reject();
|
58
|
+
};
|
59
|
+
});
|
60
|
+
imagePromiseCache[url] = imagePromise;
|
61
|
+
return [2, imagePromise];
|
62
|
+
});
|
63
|
+
});
|
64
|
+
}
|
65
|
+
exports.default = loadImage;
|
@@ -0,0 +1,100 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
var pdf_js_1 = require("pdfjs-dist/legacy/build/pdf.js");
|
40
|
+
pdf_js_1.GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
|
41
|
+
function clear(containerId) {
|
42
|
+
var container = document.getElementById(containerId);
|
43
|
+
var pages = document.querySelectorAll("[id^=\"".concat(containerId, "-page-\"]"));
|
44
|
+
pages === null || pages === void 0 ? void 0 : pages.forEach(function (item) {
|
45
|
+
container === null || container === void 0 ? void 0 : container.removeChild(item);
|
46
|
+
});
|
47
|
+
}
|
48
|
+
function render(pdf, container, num) {
|
49
|
+
pdf.getPage(num).then(function (page) {
|
50
|
+
var pageDiv = document.createElement('div');
|
51
|
+
pageDiv.setAttribute('id', "".concat(container.id, "-page-").concat(num));
|
52
|
+
pageDiv.setAttribute('style', 'position: relative; ');
|
53
|
+
container.appendChild(pageDiv);
|
54
|
+
var canvas = document.createElement('canvas');
|
55
|
+
pageDiv.appendChild(canvas);
|
56
|
+
var ctx = canvas.getContext('2d');
|
57
|
+
var scale = 1;
|
58
|
+
var devicePixelRatio = window.devicePixelRatio * 2;
|
59
|
+
var viewport = page.getViewport({ scale: scale * devicePixelRatio });
|
60
|
+
canvas.style.width = '100%';
|
61
|
+
canvas.style.height = '100%';
|
62
|
+
canvas.width = viewport.width;
|
63
|
+
canvas.height = viewport.height;
|
64
|
+
var renderContext = {
|
65
|
+
canvasContext: ctx,
|
66
|
+
viewport: viewport
|
67
|
+
};
|
68
|
+
page.render(renderContext);
|
69
|
+
});
|
70
|
+
}
|
71
|
+
function renderPDF(_a) {
|
72
|
+
var pdfUrl = _a.pdfUrl, containerId = _a.containerId, startPageNum = _a.startPageNum, endPageNum = _a.endPageNum;
|
73
|
+
return __awaiter(this, void 0, void 0, function () {
|
74
|
+
var loadingTask, pdf, container, start, end, i;
|
75
|
+
return __generator(this, function (_b) {
|
76
|
+
switch (_b.label) {
|
77
|
+
case 0:
|
78
|
+
clear(containerId);
|
79
|
+
loadingTask = (0, pdf_js_1.getDocument)({
|
80
|
+
url: pdfUrl,
|
81
|
+
cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
|
82
|
+
cMapPacked: true
|
83
|
+
});
|
84
|
+
return [4, loadingTask.promise];
|
85
|
+
case 1:
|
86
|
+
pdf = _b.sent();
|
87
|
+
container = document.getElementById(containerId);
|
88
|
+
if (!container)
|
89
|
+
return [2];
|
90
|
+
start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
|
91
|
+
end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
|
92
|
+
for (i = start; i <= end; i++) {
|
93
|
+
render(pdf, container, i);
|
94
|
+
}
|
95
|
+
return [2];
|
96
|
+
}
|
97
|
+
});
|
98
|
+
});
|
99
|
+
}
|
100
|
+
exports.default = renderPDF;
|
package/lib/Typeof/index.d.ts
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
-
type
|
2
|
-
|
1
|
+
type BaseType = 'string' | 'number' | 'boolean' | 'undefined' | 'function' | 'symbol' | 'bigint';
|
2
|
+
type ObjectType = 'object' | 'array' | 'null' | 'date' | 'formdata' | 'set' | 'map' | 'regexp' | 'arraybuffer' | 'blob';
|
3
|
+
type Type = BaseType | ObjectType;
|
4
|
+
export default function Typeof<T>(value: T, isType?: Type): boolean | Type;
|
3
5
|
export {};
|
package/lib/Typeof/index.js
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
function Typeof(value) {
|
3
|
+
function Typeof(value, isType) {
|
4
4
|
var object_type = Object.prototype.toString.call(value);
|
5
5
|
var reg = new RegExp(' (.+?)]');
|
6
|
-
var type = object_type.match(reg)[1];
|
7
|
-
|
6
|
+
var type = object_type.match(reg)[1].toLowerCase();
|
7
|
+
if (!isType) {
|
8
|
+
return type;
|
9
|
+
}
|
10
|
+
if (typeof value !== 'object') {
|
11
|
+
return typeof value === isType;
|
12
|
+
}
|
13
|
+
else {
|
14
|
+
return type === isType;
|
15
|
+
}
|
8
16
|
}
|
9
17
|
exports.default = Typeof;
|
package/lib/index.d.ts
CHANGED
@@ -4,6 +4,9 @@ import Equal from './Equal';
|
|
4
4
|
import Typeof from './Typeof';
|
5
5
|
import get_string_width from './GetStrWidth';
|
6
6
|
import ConvertNumbers from './ConvertNumbers';
|
7
|
-
|
7
|
+
import loadImage from './LoadImage';
|
8
|
+
import renderPDF from './RenderPDF';
|
9
|
+
import downloadFile from './DownloadFile';
|
10
|
+
export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile };
|
8
11
|
import { ConsoleData } from './ConsoleTable';
|
9
12
|
export type { ConsoleData };
|
package/lib/index.js
CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
|
6
|
+
exports.downloadFile = exports.renderPDF = exports.loadImage = exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
|
7
7
|
var ConsoleTable_1 = __importDefault(require("./ConsoleTable"));
|
8
8
|
exports.console_table = ConsoleTable_1.default;
|
9
9
|
var DeepCopy_1 = __importDefault(require("./DeepCopy"));
|
@@ -16,3 +16,9 @@ var GetStrWidth_1 = __importDefault(require("./GetStrWidth"));
|
|
16
16
|
exports.get_string_width = GetStrWidth_1.default;
|
17
17
|
var ConvertNumbers_1 = __importDefault(require("./ConvertNumbers"));
|
18
18
|
exports.ConvertNumbers = ConvertNumbers_1.default;
|
19
|
+
var LoadImage_1 = __importDefault(require("./LoadImage"));
|
20
|
+
exports.loadImage = LoadImage_1.default;
|
21
|
+
var RenderPDF_1 = __importDefault(require("./RenderPDF"));
|
22
|
+
exports.renderPDF = RenderPDF_1.default;
|
23
|
+
var DownloadFile_1 = __importDefault(require("./DownloadFile"));
|
24
|
+
exports.downloadFile = DownloadFile_1.default;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "hsu-utils",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.13",
|
4
4
|
"description": "some front-end utils",
|
5
5
|
"repository": "git@github.com:VitaTsui/hsu-utils.git",
|
6
6
|
"author": "VitaHsu <vitahsu7@gmail.com>",
|
@@ -53,5 +53,7 @@
|
|
53
53
|
"webpack": "^5.65.0",
|
54
54
|
"webpack-cli": "^4.9.1"
|
55
55
|
},
|
56
|
-
"dependencies": {
|
56
|
+
"dependencies": {
|
57
|
+
"pdfjs-dist": "2.13.216"
|
58
|
+
}
|
57
59
|
}
|