hsu-utils 0.0.20 → 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.19
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,6 +10,27 @@ 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 load(pdfUrl) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ let pdf = PDFMap.get(pdfUrl);
16
+ if (!pdf) {
17
+ const loadingTask = getDocument({
18
+ url: pdfUrl,
19
+ cMapUrl: 'https://unpkg.com/browse/pdfjs-dist@2.13.216/cmaps/',
20
+ cMapPacked: true
21
+ });
22
+ pdf = yield loadingTask.promise;
23
+ PDFMap.set(pdfUrl, pdf);
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);
31
+ return pdf.numPages;
32
+ });
33
+ }
13
34
  function clear(containerId) {
14
35
  const container = document.getElementById(containerId);
15
36
  const pages = document.querySelectorAll(`[id^="${containerId}-page-"]`);
@@ -17,6 +38,20 @@ function clear(containerId) {
17
38
  container === null || container === void 0 ? void 0 : container.removeChild(item);
18
39
  });
19
40
  }
41
+ function render({ pdfUrl, containerId, startPageNum, endPageNum, pixelRatio, scale }) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ clear(containerId);
44
+ const container = document.getElementById(containerId);
45
+ if (!container)
46
+ return;
47
+ const pdf = yield load(pdfUrl);
48
+ const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
49
+ const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
50
+ for (let i = start; i <= end; i++) {
51
+ renderPage({ pdf, container: container, num: i, pixelRatio, scale });
52
+ }
53
+ });
54
+ }
20
55
  function renderPage({ pdf, container, num, pixelRatio = 2, scale = 1 }) {
21
56
  pdf.getPage(num).then((page) => {
22
57
  const pageDiv = document.createElement('div');
@@ -39,46 +74,10 @@ function renderPage({ pdf, container, num, pixelRatio = 2, scale = 1 }) {
39
74
  page.render(renderContext);
40
75
  });
41
76
  }
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 }) {
58
- return __awaiter(this, void 0, void 0, function* () {
59
- 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
- const container = document.getElementById(containerId);
71
- if (!container)
72
- return;
73
- const start = startPageNum !== null && startPageNum !== void 0 ? startPageNum : 1;
74
- const end = endPageNum !== null && endPageNum !== void 0 ? endPageNum : pdf.numPages;
75
- for (let i = start; i <= end; i++) {
76
- renderPage({ pdf, container: container, num: i, pixelRatio, scale });
77
- }
78
- });
79
- }
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: 1 * devicePixelRatio });
61
- canvas.style.width = "calc(100% * ".concat(scale, ")");
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];
91
61
  }
92
62
  });
93
63
  });
94
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];
74
+ }
75
+ });
76
+ });
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,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",