hsu-utils 0.0.19 → 0.0.22

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.18
3
+ * hsu-utils v0.0.21
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -1,3 +1,4 @@
1
+ import { PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api';
1
2
  interface RenderOption {
2
3
  pdfUrl: string;
3
4
  containerId: string;
@@ -6,10 +7,14 @@ interface RenderOption {
6
7
  pixelRatio?: number;
7
8
  scale?: number;
8
9
  }
10
+ declare function load(pdfUrl: string): Promise<PDFDocumentProxy>;
9
11
  declare function getNumPages(pdfUrl: string): Promise<number>;
12
+ declare function clear(containerId: string): void;
10
13
  declare function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }: RenderOption): Promise<void>;
11
14
  declare const RenderPDF: {
12
- render: typeof render;
15
+ load: typeof load;
13
16
  getNumPages: typeof getNumPages;
17
+ clear: typeof clear;
18
+ render: typeof render;
14
19
  };
15
20
  export default RenderPDF;
@@ -10,36 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
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
12
  const PDFMap = new Map();
13
- function clear(containerId) {
14
- const container = document.getElementById(containerId);
15
- const pages = document.querySelectorAll(`[id^="${containerId}-page-"]`);
16
- pages === null || pages === void 0 ? void 0 : pages.forEach((item) => {
17
- container === null || container === void 0 ? void 0 : container.removeChild(item);
18
- });
19
- }
20
- function renderPage({ pdf, container, num, pixelRatio = 2, scale = 1 }) {
21
- pdf.getPage(num).then((page) => {
22
- const pageDiv = document.createElement('div');
23
- pageDiv.setAttribute('id', `${container.id}-page-${num}`);
24
- pageDiv.setAttribute('style', 'position: relative; ');
25
- container.appendChild(pageDiv);
26
- const canvas = document.createElement('canvas');
27
- pageDiv.appendChild(canvas);
28
- const ctx = canvas.getContext('2d');
29
- const devicePixelRatio = window.devicePixelRatio * pixelRatio;
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
- function getNumPages(pdfUrl) {
13
+ function load(pdfUrl) {
43
14
  return __awaiter(this, void 0, void 0, function* () {
44
15
  let pdf = PDFMap.get(pdfUrl);
45
16
  if (!pdf) {
@@ -51,25 +22,29 @@ function getNumPages(pdfUrl) {
51
22
  pdf = yield loadingTask.promise;
52
23
  PDFMap.set(pdfUrl, pdf);
53
24
  }
25
+ return pdf;
26
+ });
27
+ }
28
+ function getNumPages(pdfUrl) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ const pdf = yield load(pdfUrl);
54
31
  return pdf.numPages;
55
32
  });
56
33
  }
34
+ function clear(containerId) {
35
+ const container = document.getElementById(containerId);
36
+ const pages = document.querySelectorAll(`[id^="${containerId}-page-"]`);
37
+ pages === null || pages === void 0 ? void 0 : pages.forEach((item) => {
38
+ container === null || container === void 0 ? void 0 : container.removeChild(item);
39
+ });
40
+ }
57
41
  function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }) {
58
42
  return __awaiter(this, void 0, void 0, function* () {
59
43
  clear(containerId);
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
- }
70
44
  const container = document.getElementById(containerId);
71
45
  if (!container)
72
46
  return;
47
+ const pdf = yield load(pdfUrl);
73
48
  const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
74
49
  const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
75
50
  for (let i = start; i <= end; i++) {
@@ -77,8 +52,32 @@ function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, sca
77
52
  }
78
53
  });
79
54
  }
55
+ function renderPage({ pdf, container, num, pixelRatio = 2, scale = 1 }) {
56
+ pdf.getPage(num).then((page) => {
57
+ const pageDiv = document.createElement('div');
58
+ pageDiv.setAttribute('id', `${container.id}-page-${num}`);
59
+ pageDiv.setAttribute('style', 'position: relative; ');
60
+ container.appendChild(pageDiv);
61
+ const canvas = document.createElement('canvas');
62
+ pageDiv.appendChild(canvas);
63
+ const ctx = canvas.getContext('2d');
64
+ const devicePixelRatio = window.devicePixelRatio * pixelRatio;
65
+ const viewport = page.getViewport({ scale: 1 * devicePixelRatio });
66
+ canvas.style.width = `calc(100% * ${scale})`;
67
+ canvas.style.height = '100%';
68
+ canvas.width = viewport.width;
69
+ canvas.height = viewport.height;
70
+ const renderContext = {
71
+ canvasContext: ctx,
72
+ viewport: viewport
73
+ };
74
+ page.render(renderContext);
75
+ });
76
+ }
80
77
  const RenderPDF = {
81
- render,
82
- getNumPages
78
+ load,
79
+ getNumPages,
80
+ clear,
81
+ render
83
82
  };
84
83
  export default RenderPDF;
@@ -1,3 +1,4 @@
1
+ import { PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api';
1
2
  interface RenderOption {
2
3
  pdfUrl: string;
3
4
  containerId: string;
@@ -6,10 +7,14 @@ interface RenderOption {
6
7
  pixelRatio?: number;
7
8
  scale?: number;
8
9
  }
10
+ declare function load(pdfUrl: string): Promise<PDFDocumentProxy>;
9
11
  declare function getNumPages(pdfUrl: string): Promise<number>;
12
+ declare function clear(containerId: string): void;
10
13
  declare function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }: RenderOption): Promise<void>;
11
14
  declare const RenderPDF: {
12
- render: typeof render;
15
+ load: typeof load;
13
16
  getNumPages: typeof getNumPages;
17
+ clear: typeof clear;
18
+ render: typeof render;
14
19
  };
15
20
  export default RenderPDF;
@@ -39,37 +39,7 @@ 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
41
  var PDFMap = new Map();
42
- function clear(containerId) {
43
- var container = document.getElementById(containerId);
44
- var pages = document.querySelectorAll("[id^=\"".concat(containerId, "-page-\"]"));
45
- pages === null || pages === void 0 ? void 0 : pages.forEach(function (item) {
46
- container === null || container === void 0 ? void 0 : container.removeChild(item);
47
- });
48
- }
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;
51
- pdf.getPage(num).then(function (page) {
52
- var pageDiv = document.createElement('div');
53
- pageDiv.setAttribute('id', "".concat(container.id, "-page-").concat(num));
54
- pageDiv.setAttribute('style', 'position: relative; ');
55
- container.appendChild(pageDiv);
56
- var canvas = document.createElement('canvas');
57
- pageDiv.appendChild(canvas);
58
- var ctx = canvas.getContext('2d');
59
- var devicePixelRatio = window.devicePixelRatio * pixelRatio;
60
- var viewport = page.getViewport({ scale: scale * devicePixelRatio });
61
- canvas.style.width = '100%';
62
- canvas.style.height = '100%';
63
- canvas.width = viewport.width;
64
- canvas.height = viewport.height;
65
- var renderContext = {
66
- canvasContext: ctx,
67
- viewport: viewport
68
- };
69
- page.render(renderContext);
70
- });
71
- }
72
- function getNumPages(pdfUrl) {
42
+ function load(pdfUrl) {
73
43
  return __awaiter(this, void 0, void 0, function () {
74
44
  var pdf, loadingTask;
75
45
  return __generator(this, function (_a) {
@@ -87,35 +57,45 @@ function getNumPages(pdfUrl) {
87
57
  pdf = _a.sent();
88
58
  PDFMap.set(pdfUrl, pdf);
89
59
  _a.label = 2;
90
- case 2: return [2, pdf.numPages];
60
+ case 2: return [2, pdf];
61
+ }
62
+ });
63
+ });
64
+ }
65
+ function getNumPages(pdfUrl) {
66
+ return __awaiter(this, void 0, void 0, function () {
67
+ var pdf;
68
+ return __generator(this, function (_a) {
69
+ switch (_a.label) {
70
+ case 0: return [4, load(pdfUrl)];
71
+ case 1:
72
+ pdf = _a.sent();
73
+ return [2, pdf.numPages];
91
74
  }
92
75
  });
93
76
  });
94
77
  }
78
+ function clear(containerId) {
79
+ var container = document.getElementById(containerId);
80
+ var pages = document.querySelectorAll("[id^=\"".concat(containerId, "-page-\"]"));
81
+ pages === null || pages === void 0 ? void 0 : pages.forEach(function (item) {
82
+ container === null || container === void 0 ? void 0 : container.removeChild(item);
83
+ });
84
+ }
95
85
  function render(_a) {
96
86
  var pdfUrl = _a.pdfUrl, containerId = _a.containerId, startPageNum = _a.startPageNum, endPageNum = _a.endPageNum, pixelRatio = _a.pixelRatio, scale = _a.scale;
97
87
  return __awaiter(this, void 0, void 0, function () {
98
- var pdf, loadingTask, container, start, end, i;
88
+ var container, pdf, start, end, i;
99
89
  return __generator(this, function (_b) {
100
90
  switch (_b.label) {
101
91
  case 0:
102
92
  clear(containerId);
103
- pdf = PDFMap.get(pdfUrl);
104
- if (!!pdf) return [3, 2];
105
- loadingTask = (0, pdf_js_1.getDocument)({
106
- url: pdfUrl,
107
- cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
108
- cMapPacked: true
109
- });
110
- return [4, loadingTask.promise];
111
- case 1:
112
- pdf = _b.sent();
113
- PDFMap.set(pdfUrl, pdf);
114
- _b.label = 2;
115
- case 2:
116
93
  container = document.getElementById(containerId);
117
94
  if (!container)
118
95
  return [2];
96
+ return [4, load(pdfUrl)];
97
+ case 1:
98
+ pdf = _b.sent();
119
99
  start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
120
100
  end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
121
101
  for (i = start; i <= end; i++) {
@@ -126,8 +106,33 @@ function render(_a) {
126
106
  });
127
107
  });
128
108
  }
109
+ function renderPage(_a) {
110
+ 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;
111
+ pdf.getPage(num).then(function (page) {
112
+ var pageDiv = document.createElement('div');
113
+ pageDiv.setAttribute('id', "".concat(container.id, "-page-").concat(num));
114
+ pageDiv.setAttribute('style', 'position: relative; ');
115
+ container.appendChild(pageDiv);
116
+ var canvas = document.createElement('canvas');
117
+ pageDiv.appendChild(canvas);
118
+ var ctx = canvas.getContext('2d');
119
+ var devicePixelRatio = window.devicePixelRatio * pixelRatio;
120
+ var viewport = page.getViewport({ scale: 1 * devicePixelRatio });
121
+ canvas.style.width = "calc(100% * ".concat(scale, ")");
122
+ canvas.style.height = '100%';
123
+ canvas.width = viewport.width;
124
+ canvas.height = viewport.height;
125
+ var renderContext = {
126
+ canvasContext: ctx,
127
+ viewport: viewport
128
+ };
129
+ page.render(renderContext);
130
+ });
131
+ }
129
132
  var RenderPDF = {
130
- render: render,
131
- getNumPages: getNumPages
133
+ load: load,
134
+ getNumPages: getNumPages,
135
+ clear: clear,
136
+ render: render
132
137
  };
133
138
  exports.default = RenderPDF;
package/package.json CHANGED
@@ -1,59 +1,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
- }
1
+ {
2
+ "name": "hsu-utils",
3
+ "version": "0.0.22",
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
+ }