hsu-utils 0.0.18 → 0.0.20

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.19
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,10 +26,9 @@ 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;
30
- const viewport = page.getViewport({ scale: scale * devicePixelRatio });
31
- canvas.style.width = '100%';
29
+ const devicePixelRatio = window.devicePixelRatio * pixelRatio;
30
+ const viewport = page.getViewport({ scale: 1 * devicePixelRatio });
31
+ canvas.style.width = `calc(100% * ${scale})`;
32
32
  canvas.style.height = '100%';
33
33
  canvas.width = viewport.width;
34
34
  canvas.height = viewport.height;
@@ -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,10 +56,9 @@ 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 viewport = page.getViewport({ scale: scale * devicePixelRatio });
60
- canvas.style.width = '100%';
59
+ var devicePixelRatio = window.devicePixelRatio * pixelRatio;
60
+ var viewport = page.getViewport({ scale: 1 * devicePixelRatio });
61
+ canvas.style.width = "calc(100% * ".concat(scale, ")");
61
62
  canvas.style.height = '100%';
62
63
  canvas.width = viewport.width;
63
64
  canvas.height = viewport.height;
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",