hsu-utils 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.17
3
+ * hsu-utils v0.0.18
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -1,8 +1,15 @@
1
- interface PDFOption {
1
+ interface RenderOption {
2
2
  pdfUrl: string;
3
3
  containerId: string;
4
4
  startPageNum?: number;
5
5
  endPageNum?: number;
6
+ pixelRatio?: number;
7
+ scale?: number;
6
8
  }
7
- export default function renderPDF({ pdfUrl, containerId, startPageNum, endPageNum }: PDFOption): Promise<void>;
8
- export {};
9
+ declare function getNumPages(pdfUrl: string): Promise<number>;
10
+ declare function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }: RenderOption): Promise<void>;
11
+ declare const RenderPDF: {
12
+ render: typeof render;
13
+ getNumPages: typeof getNumPages;
14
+ };
15
+ export default RenderPDF;
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { GlobalWorkerOptions, getDocument } from 'pdfjs-dist/legacy/build/pdf.js';
11
11
  GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
12
+ const PDFMap = new Map();
12
13
  function clear(containerId) {
13
14
  const container = document.getElementById(containerId);
14
15
  const pages = document.querySelectorAll(`[id^="${containerId}-page-"]`);
@@ -16,7 +17,7 @@ function clear(containerId) {
16
17
  container === null || container === void 0 ? void 0 : container.removeChild(item);
17
18
  });
18
19
  }
19
- function render(pdf, container, num) {
20
+ function renderPage({ pdf, container, num, pixelRatio = 2, scale = 1 }) {
20
21
  pdf.getPage(num).then((page) => {
21
22
  const pageDiv = document.createElement('div');
22
23
  pageDiv.setAttribute('id', `${container.id}-page-${num}`);
@@ -25,8 +26,7 @@ function render(pdf, container, num) {
25
26
  const canvas = document.createElement('canvas');
26
27
  pageDiv.appendChild(canvas);
27
28
  const ctx = canvas.getContext('2d');
28
- const scale = 1;
29
- const devicePixelRatio = window.devicePixelRatio * 2;
29
+ const devicePixelRatio = window.devicePixelRatio * pixelRatio;
30
30
  const viewport = page.getViewport({ scale: scale * devicePixelRatio });
31
31
  canvas.style.width = '100%';
32
32
  canvas.style.height = '100%';
@@ -39,22 +39,46 @@ function render(pdf, container, num) {
39
39
  page.render(renderContext);
40
40
  });
41
41
  }
42
- export default function renderPDF({ pdfUrl, containerId, startPageNum, endPageNum }) {
42
+ function getNumPages(pdfUrl) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ let pdf = PDFMap.get(pdfUrl);
45
+ if (!pdf) {
46
+ const loadingTask = getDocument({
47
+ url: pdfUrl,
48
+ cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
49
+ cMapPacked: true
50
+ });
51
+ pdf = yield loadingTask.promise;
52
+ PDFMap.set(pdfUrl, pdf);
53
+ }
54
+ return pdf.numPages;
55
+ });
56
+ }
57
+ function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }) {
43
58
  return __awaiter(this, void 0, void 0, function* () {
44
59
  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;
60
+ let pdf = PDFMap.get(pdfUrl);
61
+ if (!pdf) {
62
+ const loadingTask = getDocument({
63
+ url: pdfUrl,
64
+ cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
65
+ cMapPacked: true
66
+ });
67
+ pdf = yield loadingTask.promise;
68
+ PDFMap.set(pdfUrl, pdf);
69
+ }
51
70
  const container = document.getElementById(containerId);
52
71
  if (!container)
53
72
  return;
54
73
  const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
55
74
  const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
56
75
  for (let i = start; i <= end; i++) {
57
- render(pdf, container, i);
76
+ renderPage({ pdf, container: container, num: i, pixelRatio, scale });
58
77
  }
59
78
  });
60
79
  }
80
+ const RenderPDF = {
81
+ render,
82
+ getNumPages
83
+ };
84
+ export default RenderPDF;
package/es/index.d.ts CHANGED
@@ -5,9 +5,9 @@ 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';
8
+ import RenderPDF from './RenderPDF';
9
9
  import downloadFile from './DownloadFile';
10
10
  import array_is_includes from './ArrayIsIncludes';
11
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile, array_is_includes };
11
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes };
12
12
  import { ConsoleData } from './ConsoleTable';
13
13
  export type { ConsoleData };
package/es/index.js CHANGED
@@ -5,7 +5,7 @@ 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';
8
+ import RenderPDF from './RenderPDF';
9
9
  import downloadFile from './DownloadFile';
10
10
  import array_is_includes from './ArrayIsIncludes';
11
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile, array_is_includes };
11
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes };
@@ -1,8 +1,15 @@
1
- interface PDFOption {
1
+ interface RenderOption {
2
2
  pdfUrl: string;
3
3
  containerId: string;
4
4
  startPageNum?: number;
5
5
  endPageNum?: number;
6
+ pixelRatio?: number;
7
+ scale?: number;
6
8
  }
7
- export default function renderPDF({ pdfUrl, containerId, startPageNum, endPageNum }: PDFOption): Promise<void>;
8
- export {};
9
+ declare function getNumPages(pdfUrl: string): Promise<number>;
10
+ declare function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }: RenderOption): Promise<void>;
11
+ declare const RenderPDF: {
12
+ render: typeof render;
13
+ getNumPages: typeof getNumPages;
14
+ };
15
+ export default RenderPDF;
@@ -38,6 +38,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  var pdf_js_1 = require("pdfjs-dist/legacy/build/pdf.js");
40
40
  pdf_js_1.GlobalWorkerOptions.workerSrc = 'https://cdn.bootcss.com/pdf.js/2.13.216/pdf.worker.js';
41
+ var PDFMap = new Map();
41
42
  function clear(containerId) {
42
43
  var container = document.getElementById(containerId);
43
44
  var pages = document.querySelectorAll("[id^=\"".concat(containerId, "-page-\"]"));
@@ -45,7 +46,8 @@ function clear(containerId) {
45
46
  container === null || container === void 0 ? void 0 : container.removeChild(item);
46
47
  });
47
48
  }
48
- function render(pdf, container, num) {
49
+ function renderPage(_a) {
50
+ var pdf = _a.pdf, container = _a.container, num = _a.num, _b = _a.pixelRatio, pixelRatio = _b === void 0 ? 2 : _b, _c = _a.scale, scale = _c === void 0 ? 1 : _c;
49
51
  pdf.getPage(num).then(function (page) {
50
52
  var pageDiv = document.createElement('div');
51
53
  pageDiv.setAttribute('id', "".concat(container.id, "-page-").concat(num));
@@ -54,8 +56,7 @@ function render(pdf, container, num) {
54
56
  var canvas = document.createElement('canvas');
55
57
  pageDiv.appendChild(canvas);
56
58
  var ctx = canvas.getContext('2d');
57
- var scale = 1;
58
- var devicePixelRatio = window.devicePixelRatio * 2;
59
+ var devicePixelRatio = window.devicePixelRatio * pixelRatio;
59
60
  var viewport = page.getViewport({ scale: scale * devicePixelRatio });
60
61
  canvas.style.width = '100%';
61
62
  canvas.style.height = '100%';
@@ -68,14 +69,39 @@ function render(pdf, container, num) {
68
69
  page.render(renderContext);
69
70
  });
70
71
  }
71
- function renderPDF(_a) {
72
- var pdfUrl = _a.pdfUrl, containerId = _a.containerId, startPageNum = _a.startPageNum, endPageNum = _a.endPageNum;
72
+ function getNumPages(pdfUrl) {
73
73
  return __awaiter(this, void 0, void 0, function () {
74
- var loadingTask, pdf, container, start, end, i;
74
+ var pdf, loadingTask;
75
+ return __generator(this, function (_a) {
76
+ switch (_a.label) {
77
+ case 0:
78
+ pdf = PDFMap.get(pdfUrl);
79
+ if (!!pdf) return [3, 2];
80
+ loadingTask = (0, pdf_js_1.getDocument)({
81
+ url: pdfUrl,
82
+ cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
83
+ cMapPacked: true
84
+ });
85
+ return [4, loadingTask.promise];
86
+ case 1:
87
+ pdf = _a.sent();
88
+ PDFMap.set(pdfUrl, pdf);
89
+ _a.label = 2;
90
+ case 2: return [2, pdf.numPages];
91
+ }
92
+ });
93
+ });
94
+ }
95
+ function render(_a) {
96
+ var pdfUrl = _a.pdfUrl, containerId = _a.containerId, startPageNum = _a.startPageNum, endPageNum = _a.endPageNum, pixelRatio = _a.pixelRatio, scale = _a.scale;
97
+ return __awaiter(this, void 0, void 0, function () {
98
+ var pdf, loadingTask, container, start, end, i;
75
99
  return __generator(this, function (_b) {
76
100
  switch (_b.label) {
77
101
  case 0:
78
102
  clear(containerId);
103
+ pdf = PDFMap.get(pdfUrl);
104
+ if (!!pdf) return [3, 2];
79
105
  loadingTask = (0, pdf_js_1.getDocument)({
80
106
  url: pdfUrl,
81
107
  cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
@@ -84,17 +110,24 @@ function renderPDF(_a) {
84
110
  return [4, loadingTask.promise];
85
111
  case 1:
86
112
  pdf = _b.sent();
113
+ PDFMap.set(pdfUrl, pdf);
114
+ _b.label = 2;
115
+ case 2:
87
116
  container = document.getElementById(containerId);
88
117
  if (!container)
89
118
  return [2];
90
119
  start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
91
120
  end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
92
121
  for (i = start; i <= end; i++) {
93
- render(pdf, container, i);
122
+ renderPage({ pdf: pdf, container: container, num: i, pixelRatio: pixelRatio, scale: scale });
94
123
  }
95
124
  return [2];
96
125
  }
97
126
  });
98
127
  });
99
128
  }
100
- exports.default = renderPDF;
129
+ var RenderPDF = {
130
+ render: render,
131
+ getNumPages: getNumPages
132
+ };
133
+ exports.default = RenderPDF;
package/lib/index.d.ts CHANGED
@@ -5,9 +5,9 @@ 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';
8
+ import RenderPDF from './RenderPDF';
9
9
  import downloadFile from './DownloadFile';
10
10
  import array_is_includes from './ArrayIsIncludes';
11
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, renderPDF, downloadFile, array_is_includes };
11
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes };
12
12
  import { ConsoleData } from './ConsoleTable';
13
13
  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.array_is_includes = exports.downloadFile = exports.renderPDF = exports.loadImage = exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
6
+ exports.array_is_includes = 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"));
@@ -19,7 +19,7 @@ exports.ConvertNumbers = ConvertNumbers_1.default;
19
19
  var LoadImage_1 = __importDefault(require("./LoadImage"));
20
20
  exports.loadImage = LoadImage_1.default;
21
21
  var RenderPDF_1 = __importDefault(require("./RenderPDF"));
22
- exports.renderPDF = RenderPDF_1.default;
22
+ exports.RenderPDF = RenderPDF_1.default;
23
23
  var DownloadFile_1 = __importDefault(require("./DownloadFile"));
24
24
  exports.downloadFile = DownloadFile_1.default;
25
25
  var ArrayIsIncludes_1 = __importDefault(require("./ArrayIsIncludes"));
package/package.json CHANGED
@@ -1,59 +1,59 @@
1
- {
2
- "name": "hsu-utils",
3
- "version": "0.0.18",
4
- "description": "some front-end utils",
5
- "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
- "author": "VitaHsu <vitahsu7@gmail.com>",
7
- "license": "MIT",
8
- "main": "lib/index.js",
9
- "module": "es/index.js",
10
- "unpkg": "dist/hsu-utils.min.js",
11
- "files": [
12
- "es",
13
- "lib",
14
- "dist",
15
- "package.json",
16
- "README.md",
17
- "LICENSE"
18
- ],
19
- "sideEffects": [
20
- "es/*",
21
- "lib/*",
22
- "dist/*"
23
- ],
24
- "scripts": {
25
- "build": "yarn build:es && yarn build:cjs && rimraf dist && yarn build:umd",
26
- "build:es": "rimraf es && tsc -p build/tsconfig.es.json",
27
- "build:cjs": "rimraf lib && tsc -p build/tsconfig.cjs.json",
28
- "build:umd": "rimraf dist && yarn build:dev && yarn build:prod",
29
- "build:dev": "cross-env NODE_ENV=development webpack --config build/webpack.config.js",
30
- "build:prod": "cross-env NODE_ENV=production webpack --config build/webpack.config.js",
31
- "clear": "rimraf lib && rimraf es && rimraf dist",
32
- "test": "jest",
33
- "publish:patch": "yarn build && yarn publish --new-version patch",
34
- "publish:minor": "yarn build && yarn publish --new-version minor",
35
- "publish:major": "yarn build && yarn publish --new-version major",
36
- "publish:alpha": "yarn build && yarn publish --tag alpha"
37
- },
38
- "devDependencies": {
39
- "@jest/globals": "^29.5.0",
40
- "@testing-library/react-hooks": "^8.0.1",
41
- "@types/jest": "^29.5.2",
42
- "cross-env": "^7.0.3",
43
- "jest": "^29.5.0",
44
- "jest-canvas-mock": "^2.5.2",
45
- "jest-environment-jsdom": "^29.5.0",
46
- "jsdom": "^22.1.0",
47
- "rimraf": "^5.0.1",
48
- "terser-webpack-plugin": "^5.3.0",
49
- "ts-jest": "^29.1.0",
50
- "ts-loader": "^9.2.6",
51
- "ts-node": "^10.9.1",
52
- "typescript": "^5.1.3",
53
- "webpack": "^5.65.0",
54
- "webpack-cli": "^4.9.1"
55
- },
56
- "dependencies": {
57
- "pdfjs-dist": "2.13.216"
58
- }
59
- }
1
+ {
2
+ "name": "hsu-utils",
3
+ "version": "0.0.19",
4
+ "description": "some front-end utils",
5
+ "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
+ "author": "VitaHsu <vitahsu7@gmail.com>",
7
+ "license": "MIT",
8
+ "main": "lib/index.js",
9
+ "module": "es/index.js",
10
+ "unpkg": "dist/hsu-utils.min.js",
11
+ "files": [
12
+ "es",
13
+ "lib",
14
+ "dist",
15
+ "package.json",
16
+ "README.md",
17
+ "LICENSE"
18
+ ],
19
+ "sideEffects": [
20
+ "es/*",
21
+ "lib/*",
22
+ "dist/*"
23
+ ],
24
+ "scripts": {
25
+ "build": "yarn build:es && yarn build:cjs && rimraf dist && yarn build:umd",
26
+ "build:es": "rimraf es && tsc -p build/tsconfig.es.json",
27
+ "build:cjs": "rimraf lib && tsc -p build/tsconfig.cjs.json",
28
+ "build:umd": "rimraf dist && yarn build:dev && yarn build:prod",
29
+ "build:dev": "cross-env NODE_ENV=development webpack --config build/webpack.config.js",
30
+ "build:prod": "cross-env NODE_ENV=production webpack --config build/webpack.config.js",
31
+ "clear": "rimraf lib && rimraf es && rimraf dist",
32
+ "test": "jest",
33
+ "publish:patch": "yarn build && yarn publish --new-version patch",
34
+ "publish:minor": "yarn build && yarn publish --new-version minor",
35
+ "publish:major": "yarn build && yarn publish --new-version major",
36
+ "publish:alpha": "yarn build && yarn publish --tag alpha"
37
+ },
38
+ "devDependencies": {
39
+ "@jest/globals": "^29.5.0",
40
+ "@testing-library/react-hooks": "^8.0.1",
41
+ "@types/jest": "^29.5.2",
42
+ "cross-env": "^7.0.3",
43
+ "jest": "^29.5.0",
44
+ "jest-canvas-mock": "^2.5.2",
45
+ "jest-environment-jsdom": "^29.5.0",
46
+ "jsdom": "^22.1.0",
47
+ "rimraf": "^5.0.1",
48
+ "terser-webpack-plugin": "^5.3.0",
49
+ "ts-jest": "^29.1.0",
50
+ "ts-loader": "^9.2.6",
51
+ "ts-node": "^10.9.1",
52
+ "typescript": "^5.1.3",
53
+ "webpack": "^5.65.0",
54
+ "webpack-cli": "^4.9.1"
55
+ },
56
+ "dependencies": {
57
+ "pdfjs-dist": "2.13.216"
58
+ }
59
+ }