cozy-ui 135.5.0 → 135.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/assets/icons/ui/expand.svg +1 -0
- package/assets/icons/ui/export.svg +1 -0
- package/assets/icons/ui/narrow.svg +1 -0
- package/package.json +1 -1
- package/react/ActionsMenu/Actions/exportToText.js +54 -0
- package/react/ActionsMenu/Actions/helpers.js +20 -0
- package/react/ActionsMenu/Actions/index.js +1 -0
- package/react/ActionsMenu/Actions/locales/en.json +2 -1
- package/react/ActionsMenu/Actions/locales/fr.json +2 -1
- package/react/ActionsMenu/Actions/locales/ru.json +3 -2
- package/react/ActionsMenu/Actions/locales/vi.json +3 -2
- package/react/ActionsMenu/Actions/pdfHelpers.js +187 -0
- package/react/Icon/Readme.md +7 -1
- package/react/Icons/Expand.jsx +12 -0
- package/react/Icons/Export.jsx +13 -0
- package/react/Icons/Narrow.jsx +16 -0
- package/react/Table/Readme.md +2 -1
- package/react/Table/Virtualized/FixedHeaderContent.jsx +14 -12
- package/react/Table/Virtualized/RowContent.jsx +16 -14
- package/react/Table/Virtualized/TableRow.jsx +8 -0
- package/react/Table/Virtualized/index.jsx +9 -3
- package/transpiled/react/ActionsMenu/Actions/exportToText.d.ts +13 -0
- package/transpiled/react/ActionsMenu/Actions/exportToText.js +84 -0
- package/transpiled/react/ActionsMenu/Actions/helpers.d.ts +1 -0
- package/transpiled/react/ActionsMenu/Actions/helpers.js +20 -1
- package/transpiled/react/ActionsMenu/Actions/index.d.ts +1 -0
- package/transpiled/react/ActionsMenu/Actions/index.js +2 -1
- package/transpiled/react/ActionsMenu/Actions/locales/withActionsLocales.js +8 -4
- package/transpiled/react/ActionsMenu/Actions/pdfHelpers.d.ts +34 -0
- package/transpiled/react/ActionsMenu/Actions/pdfHelpers.js +242 -0
- package/transpiled/react/Icon/icons-sprite.d.ts +1 -1
- package/transpiled/react/Icon/icons-sprite.js +1 -1
- package/transpiled/react/Icons/Expand.d.ts +2 -0
- package/transpiled/react/Icons/Expand.js +14 -0
- package/transpiled/react/Icons/Export.d.ts +2 -0
- package/transpiled/react/Icons/Export.js +16 -0
- package/transpiled/react/Icons/Narrow.d.ts +2 -0
- package/transpiled/react/Icons/Narrow.js +16 -0
- package/transpiled/react/Table/Virtualized/FixedHeaderContent.js +1 -1
- package/transpiled/react/Table/Virtualized/RowContent.js +1 -1
- package/transpiled/react/Table/Virtualized/TableRow.js +12 -1
- package/transpiled/react/Table/Virtualized/index.js +11 -4
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
|
+
import React, { forwardRef } from 'react';
|
|
5
|
+
import logger from 'cozy-logger';
|
|
6
|
+
import { downloadBlob } from "cozy-ui/transpiled/react/ActionsMenu/Actions/helpers";
|
|
7
|
+
import { getActionsI18n } from "cozy-ui/transpiled/react/ActionsMenu/Actions/locales/withActionsLocales";
|
|
8
|
+
import { makePdfBlobFromText } from "cozy-ui/transpiled/react/ActionsMenu/Actions/pdfHelpers";
|
|
9
|
+
import Icon from "cozy-ui/transpiled/react/Icon";
|
|
10
|
+
import ExportIcon from "cozy-ui/transpiled/react/Icons/Export";
|
|
11
|
+
import ListItemIcon from "cozy-ui/transpiled/react/ListItemIcon";
|
|
12
|
+
import ListItemText from "cozy-ui/transpiled/react/ListItemText";
|
|
13
|
+
import ActionsMenuItem from "cozy-ui/transpiled/react/ActionsMenu/ActionsMenuItem";
|
|
14
|
+
|
|
15
|
+
var makeComponent = function makeComponent(label, icon) {
|
|
16
|
+
var Component = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
17
|
+
return /*#__PURE__*/React.createElement(ActionsMenuItem, _extends({}, props, {
|
|
18
|
+
ref: ref
|
|
19
|
+
}), /*#__PURE__*/React.createElement(ListItemIcon, null, /*#__PURE__*/React.createElement(Icon, {
|
|
20
|
+
icon: icon
|
|
21
|
+
})), /*#__PURE__*/React.createElement(ListItemText, {
|
|
22
|
+
primary: label
|
|
23
|
+
}));
|
|
24
|
+
});
|
|
25
|
+
Component.displayName = 'exportToText';
|
|
26
|
+
return Component;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export var exportToText = function exportToText(_ref) {
|
|
30
|
+
var exportedText = _ref.exportedText,
|
|
31
|
+
file = _ref.file;
|
|
32
|
+
|
|
33
|
+
var _getActionsI18n = getActionsI18n(),
|
|
34
|
+
t = _getActionsI18n.t;
|
|
35
|
+
|
|
36
|
+
var icon = ExportIcon;
|
|
37
|
+
var label = t('exportToText');
|
|
38
|
+
return {
|
|
39
|
+
name: 'exportToText',
|
|
40
|
+
icon: icon,
|
|
41
|
+
label: label,
|
|
42
|
+
displayCondition: function displayCondition() {
|
|
43
|
+
return !!exportedText;
|
|
44
|
+
},
|
|
45
|
+
Component: makeComponent(label, icon),
|
|
46
|
+
action: function () {
|
|
47
|
+
var _action = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
48
|
+
var blob, baseName;
|
|
49
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
50
|
+
while (1) {
|
|
51
|
+
switch (_context.prev = _context.next) {
|
|
52
|
+
case 0:
|
|
53
|
+
_context.prev = 0;
|
|
54
|
+
_context.next = 3;
|
|
55
|
+
return makePdfBlobFromText(exportedText);
|
|
56
|
+
|
|
57
|
+
case 3:
|
|
58
|
+
blob = _context.sent;
|
|
59
|
+
baseName = file !== null && file !== void 0 && file.name ? "".concat(file.name.replace(/\.[^/.]+$/, ''), "_export") : 'export';
|
|
60
|
+
downloadBlob(blob, "".concat(baseName, ".pdf"));
|
|
61
|
+
_context.next = 11;
|
|
62
|
+
break;
|
|
63
|
+
|
|
64
|
+
case 8:
|
|
65
|
+
_context.prev = 8;
|
|
66
|
+
_context.t0 = _context["catch"](0);
|
|
67
|
+
logger.error(_context.t0);
|
|
68
|
+
|
|
69
|
+
case 11:
|
|
70
|
+
case "end":
|
|
71
|
+
return _context.stop();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}, _callee, null, [[0, 8]]);
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
function action() {
|
|
78
|
+
return _action.apply(this, arguments);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return action;
|
|
82
|
+
}()
|
|
83
|
+
};
|
|
84
|
+
};
|
|
@@ -5,4 +5,5 @@ export function makeBase64FromFile(file: File): Promise<string | null>;
|
|
|
5
5
|
export function fileToArrayBuffer(file: File): Promise<ArrayBuffer>;
|
|
6
6
|
export function addFileToPdf(pdfDoc: PDFDocument, file: File): Promise<ArrayBuffer>;
|
|
7
7
|
export function makePdfBlob({ client, docs, fetchBlobFileById }: object): Promise<object>;
|
|
8
|
+
export function downloadBlob(blob: Blob, filename: string): void;
|
|
8
9
|
import { PDFDocument } from "pdf-lib/cjs/api";
|
|
@@ -475,4 +475,23 @@ export var makePdfBlob = /*#__PURE__*/function () {
|
|
|
475
475
|
return function makePdfBlob(_x11) {
|
|
476
476
|
return _ref11.apply(this, arguments);
|
|
477
477
|
};
|
|
478
|
-
}();
|
|
478
|
+
}();
|
|
479
|
+
/**
|
|
480
|
+
* Trigger a browser download for a given blob.
|
|
481
|
+
*
|
|
482
|
+
* @param {Blob} blob - File content
|
|
483
|
+
* @param {string} filename - Desired filename
|
|
484
|
+
* @returns {void}
|
|
485
|
+
*/
|
|
486
|
+
|
|
487
|
+
export var downloadBlob = function downloadBlob(blob, filename) {
|
|
488
|
+
if (!blob || !filename) return;
|
|
489
|
+
var url = URL.createObjectURL(blob);
|
|
490
|
+
var a = document.createElement('a');
|
|
491
|
+
a.href = url;
|
|
492
|
+
a.download = filename;
|
|
493
|
+
a.style.display = 'none';
|
|
494
|
+
document.body.appendChild(a);
|
|
495
|
+
a.click();
|
|
496
|
+
a.remove();
|
|
497
|
+
};
|
|
@@ -12,4 +12,5 @@ export { viewInContacts } from './viewInContacts';
|
|
|
12
12
|
export { viewInDrive } from './viewInDrive';
|
|
13
13
|
export { copyToClipboard } from './copyToClipboard';
|
|
14
14
|
export { editAttribute } from './editAttribute';
|
|
15
|
-
export { others } from './others';
|
|
15
|
+
export { others } from './others';
|
|
16
|
+
export { exportToText } from './exportToText';
|
|
@@ -26,7 +26,8 @@ var en = {
|
|
|
26
26
|
success: "Copied to clipboard",
|
|
27
27
|
error: "Cannot copy to clipboard"
|
|
28
28
|
},
|
|
29
|
-
call: "Call"
|
|
29
|
+
call: "Call",
|
|
30
|
+
exportToText: "Export as text"
|
|
30
31
|
};
|
|
31
32
|
var fr = {
|
|
32
33
|
menu: "Menu",
|
|
@@ -55,7 +56,8 @@ var fr = {
|
|
|
55
56
|
success: "Copi\xE9 dans le presse-papier",
|
|
56
57
|
error: "Impossible de copier dans le presse-papier"
|
|
57
58
|
},
|
|
58
|
-
call: "Appeler"
|
|
59
|
+
call: "Appeler",
|
|
60
|
+
exportToText: "Exporter en texte"
|
|
59
61
|
};
|
|
60
62
|
var ru = {
|
|
61
63
|
menu: "\u041C\u0435\u043D\u044E",
|
|
@@ -84,7 +86,8 @@ var ru = {
|
|
|
84
86
|
success: "\u0421\u043A\u043E\u043F\u0438\u0440\u043E\u0432\u0430\u043D\u043E \u0432 \u0431\u0443\u0444\u0435\u0440 \u043E\u0431\u043C\u0435\u043D\u0430",
|
|
85
87
|
error: "\u041D\u0435 \u0443\u0434\u0430\u0435\u0442\u0441\u044F \u0441\u043A\u043E\u043F\u0438\u0440\u043E\u0432\u0430\u0442\u044C \u0432 \u0431\u0443\u0444\u0435\u0440 \u043E\u0431\u043C\u0435\u043D\u0430"
|
|
86
88
|
},
|
|
87
|
-
call: "\u0417\u0432\u043E\u043D\u0438\u0442\u044C"
|
|
89
|
+
call: "\u0417\u0432\u043E\u043D\u0438\u0442\u044C",
|
|
90
|
+
exportToText: "\u042D\u043A\u0441\u043F\u043E\u0440\u0442\u0438\u0440\u043E\u0432\u0430\u0442\u044C \u043A\u0430\u043A \u0442\u0435\u043A\u0441\u0442"
|
|
88
91
|
};
|
|
89
92
|
var vi = {
|
|
90
93
|
menu: "Menu",
|
|
@@ -113,7 +116,8 @@ var vi = {
|
|
|
113
116
|
success: "\u0110\xE3 sao ch\xE9p v\xE0o clipboard",
|
|
114
117
|
error: "Kh\xF4ng th\u1EC3 sao ch\xE9p v\xE0o clipboard"
|
|
115
118
|
},
|
|
116
|
-
call: "G\u1ECDi"
|
|
119
|
+
call: "G\u1ECDi",
|
|
120
|
+
exportToText: "Xu\u1EA5t d\u01B0\u1EDBi d\u1EA1ng v\u0103n b\u1EA3n"
|
|
117
121
|
};
|
|
118
122
|
export var locales = {
|
|
119
123
|
en: en,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function wrapTextToLines({ font, fontSize, maxLineWidth, line }: {
|
|
2
|
+
font: import('pdf-lib').PDFFont;
|
|
3
|
+
fontSize: number;
|
|
4
|
+
maxLineWidth: number;
|
|
5
|
+
line: string;
|
|
6
|
+
}): string[];
|
|
7
|
+
export function pushNewPageIfNeeded({ pdfDoc, page, y, requiredHeight, marginTop, marginBottom }: {
|
|
8
|
+
pdfDoc: PDFDocument;
|
|
9
|
+
page: import('pdf-lib').PDFPage;
|
|
10
|
+
y: number;
|
|
11
|
+
requiredHeight: number;
|
|
12
|
+
marginTop: number;
|
|
13
|
+
marginBottom: number;
|
|
14
|
+
}): {
|
|
15
|
+
page: import('pdf-lib').PDFPage;
|
|
16
|
+
y: number;
|
|
17
|
+
};
|
|
18
|
+
export function drawWrappedText({ pdfDoc, page, y, text, font, fontSize, lineHeight, marginX, marginY, maxLineWidth }: {
|
|
19
|
+
pdfDoc: PDFDocument;
|
|
20
|
+
page: import('pdf-lib').PDFPage;
|
|
21
|
+
y: number;
|
|
22
|
+
text: string;
|
|
23
|
+
font: import('pdf-lib').PDFFont;
|
|
24
|
+
fontSize: number;
|
|
25
|
+
lineHeight: number;
|
|
26
|
+
marginX: number;
|
|
27
|
+
marginY: number;
|
|
28
|
+
maxLineWidth: number;
|
|
29
|
+
}): {
|
|
30
|
+
page: import('pdf-lib').PDFPage;
|
|
31
|
+
y: number;
|
|
32
|
+
};
|
|
33
|
+
export function makePdfBlobFromText(text: string): Promise<Blob>;
|
|
34
|
+
import { PDFDocument } from "pdf-lib/cjs/api";
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
|
+
|
|
5
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
6
|
+
|
|
7
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
+
|
|
9
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
10
|
+
|
|
11
|
+
import { PDFDocument, StandardFonts } from 'pdf-lib';
|
|
12
|
+
/**
|
|
13
|
+
* Wrap a string into multiple lines based on available width.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} opts
|
|
16
|
+
* @param {import('pdf-lib').PDFFont} opts.font - Embedded pdf-lib font
|
|
17
|
+
* @param {number} opts.fontSize - Font size
|
|
18
|
+
* @param {number} opts.maxLineWidth - Max line width in PDF units
|
|
19
|
+
* @param {string} opts.line - Text to wrap
|
|
20
|
+
* @returns {string[]} Wrapped lines
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export var wrapTextToLines = function wrapTextToLines(_ref) {
|
|
24
|
+
var font = _ref.font,
|
|
25
|
+
fontSize = _ref.fontSize,
|
|
26
|
+
maxLineWidth = _ref.maxLineWidth,
|
|
27
|
+
line = _ref.line;
|
|
28
|
+
if (!line) return [];
|
|
29
|
+
if (fontSize <= 0 || maxLineWidth <= 0) return [line];
|
|
30
|
+
var words = line.split(' ');
|
|
31
|
+
return words.reduce(function (lines, word) {
|
|
32
|
+
var currentLine = lines[lines.length - 1];
|
|
33
|
+
|
|
34
|
+
if (!currentLine) {
|
|
35
|
+
return [word];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var testLine = "".concat(currentLine, " ").concat(word);
|
|
39
|
+
var testWidth = font.widthOfTextAtSize(testLine, fontSize);
|
|
40
|
+
|
|
41
|
+
if (testWidth <= maxLineWidth) {
|
|
42
|
+
lines[lines.length - 1] = testLine;
|
|
43
|
+
} else {
|
|
44
|
+
lines.push(word);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return lines;
|
|
48
|
+
}, []);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Add a new page if there is not enough space to draw a new line.
|
|
52
|
+
*
|
|
53
|
+
* @param {object} opts
|
|
54
|
+
* @param {PDFDocument} opts.pdfDoc - pdf-lib document
|
|
55
|
+
* @param {import('pdf-lib').PDFPage} opts.page - Current page
|
|
56
|
+
* @param {number} opts.y - Current y cursor position
|
|
57
|
+
* @param {number} opts.requiredHeight - Height required before adding a new page
|
|
58
|
+
* @param {number} opts.marginTop - Top margin
|
|
59
|
+
* @param {number} opts.marginBottom - Bottom margin
|
|
60
|
+
* @returns {{ page: import('pdf-lib').PDFPage, y: number }} Updated page and y
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
export var pushNewPageIfNeeded = function pushNewPageIfNeeded(_ref2) {
|
|
64
|
+
var pdfDoc = _ref2.pdfDoc,
|
|
65
|
+
page = _ref2.page,
|
|
66
|
+
y = _ref2.y,
|
|
67
|
+
requiredHeight = _ref2.requiredHeight,
|
|
68
|
+
marginTop = _ref2.marginTop,
|
|
69
|
+
marginBottom = _ref2.marginBottom;
|
|
70
|
+
|
|
71
|
+
if (!pdfDoc || !page) {
|
|
72
|
+
throw new Error('pdfDoc and page are required');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
var _page$getSize = page.getSize(),
|
|
76
|
+
width = _page$getSize.width,
|
|
77
|
+
height = _page$getSize.height;
|
|
78
|
+
|
|
79
|
+
if (y - requiredHeight < marginBottom) {
|
|
80
|
+
var newPage = pdfDoc.addPage([width, height]);
|
|
81
|
+
return {
|
|
82
|
+
page: newPage,
|
|
83
|
+
y: height - marginTop
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
page: page,
|
|
89
|
+
y: y
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Draws a plain text string onto a PDF, wrapping lines to max width and handling page breaks.
|
|
94
|
+
*
|
|
95
|
+
* @param {object} opts
|
|
96
|
+
* @param {PDFDocument} opts.pdfDoc - pdf-lib document
|
|
97
|
+
* @param {import('pdf-lib').PDFPage} opts.page - Current page
|
|
98
|
+
* @param {number} opts.y - Current y cursor position
|
|
99
|
+
* @param {string} opts.text - Text to draw
|
|
100
|
+
* @param {import('pdf-lib').PDFFont} opts.font - Embedded pdf-lib font
|
|
101
|
+
* @param {number} opts.fontSize - Font size
|
|
102
|
+
* @param {number} opts.lineHeight - Line height
|
|
103
|
+
* @param {number} opts.marginX - Left margin
|
|
104
|
+
* @param {number} opts.marginY - Top/bottom margin
|
|
105
|
+
* @param {number} opts.maxLineWidth - Max line width in PDF units
|
|
106
|
+
* @returns {{ page: import('pdf-lib').PDFPage, y: number }} Updated page and y
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
export var drawWrappedText = function drawWrappedText(_ref3) {
|
|
110
|
+
var pdfDoc = _ref3.pdfDoc,
|
|
111
|
+
page = _ref3.page,
|
|
112
|
+
y = _ref3.y,
|
|
113
|
+
text = _ref3.text,
|
|
114
|
+
font = _ref3.font,
|
|
115
|
+
fontSize = _ref3.fontSize,
|
|
116
|
+
lineHeight = _ref3.lineHeight,
|
|
117
|
+
marginX = _ref3.marginX,
|
|
118
|
+
marginY = _ref3.marginY,
|
|
119
|
+
maxLineWidth = _ref3.maxLineWidth;
|
|
120
|
+
var currentPage = page;
|
|
121
|
+
var currentY = y;
|
|
122
|
+
var paragraphs = String(text !== null && text !== void 0 ? text : '').split(/\r?\n/);
|
|
123
|
+
var renderItems = paragraphs.flatMap(function (paragraph) {
|
|
124
|
+
var wrappedLines = wrapTextToLines({
|
|
125
|
+
font: font,
|
|
126
|
+
fontSize: fontSize,
|
|
127
|
+
maxLineWidth: maxLineWidth,
|
|
128
|
+
line: paragraph
|
|
129
|
+
});
|
|
130
|
+
return [].concat(_toConsumableArray(wrappedLines), [{
|
|
131
|
+
type: 'paragraphGap'
|
|
132
|
+
}]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
var _iterator = _createForOfIteratorHelper(renderItems),
|
|
136
|
+
_step;
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
140
|
+
var item = _step.value;
|
|
141
|
+
|
|
142
|
+
if (typeof item === 'object' && item.type === 'paragraphGap') {
|
|
143
|
+
currentY -= lineHeight / 2;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
var nextState = pushNewPageIfNeeded({
|
|
148
|
+
pdfDoc: pdfDoc,
|
|
149
|
+
page: currentPage,
|
|
150
|
+
y: currentY,
|
|
151
|
+
requiredHeight: lineHeight,
|
|
152
|
+
marginTop: marginY,
|
|
153
|
+
marginBottom: marginY
|
|
154
|
+
});
|
|
155
|
+
currentPage = nextState.page;
|
|
156
|
+
currentY = nextState.y;
|
|
157
|
+
currentPage.drawText(item, {
|
|
158
|
+
x: marginX,
|
|
159
|
+
y: currentY - fontSize,
|
|
160
|
+
size: fontSize,
|
|
161
|
+
font: font
|
|
162
|
+
});
|
|
163
|
+
currentY -= lineHeight;
|
|
164
|
+
}
|
|
165
|
+
} catch (err) {
|
|
166
|
+
_iterator.e(err);
|
|
167
|
+
} finally {
|
|
168
|
+
_iterator.f();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
page: currentPage,
|
|
173
|
+
y: currentY
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* Creates a PDF Blob from a plain text string.
|
|
178
|
+
*
|
|
179
|
+
* @param {string} text - Text content to put into the PDF
|
|
180
|
+
* @returns {Promise<Blob>} PDF file as a Blob
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
export var makePdfBlobFromText = /*#__PURE__*/function () {
|
|
184
|
+
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(text) {
|
|
185
|
+
var pdfDoc, font, CONFIG, maxLineWidth, initialPage, pdfBytes;
|
|
186
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
187
|
+
while (1) {
|
|
188
|
+
switch (_context.prev = _context.next) {
|
|
189
|
+
case 0:
|
|
190
|
+
_context.next = 2;
|
|
191
|
+
return PDFDocument.create();
|
|
192
|
+
|
|
193
|
+
case 2:
|
|
194
|
+
pdfDoc = _context.sent;
|
|
195
|
+
_context.next = 5;
|
|
196
|
+
return pdfDoc.embedFont(StandardFonts.Helvetica);
|
|
197
|
+
|
|
198
|
+
case 5:
|
|
199
|
+
font = _context.sent;
|
|
200
|
+
CONFIG = {
|
|
201
|
+
fontSize: 12,
|
|
202
|
+
lineHeight: 16,
|
|
203
|
+
marginX: 40,
|
|
204
|
+
marginY: 40,
|
|
205
|
+
pageSize: [595.28, 841.89] // A4
|
|
206
|
+
|
|
207
|
+
};
|
|
208
|
+
maxLineWidth = CONFIG.pageSize[0] - CONFIG.marginX * 2;
|
|
209
|
+
initialPage = pdfDoc.addPage(CONFIG.pageSize);
|
|
210
|
+
drawWrappedText({
|
|
211
|
+
pdfDoc: pdfDoc,
|
|
212
|
+
page: initialPage,
|
|
213
|
+
y: CONFIG.pageSize[1] - CONFIG.marginY,
|
|
214
|
+
text: text,
|
|
215
|
+
font: font,
|
|
216
|
+
fontSize: CONFIG.fontSize,
|
|
217
|
+
lineHeight: CONFIG.lineHeight,
|
|
218
|
+
marginX: CONFIG.marginX,
|
|
219
|
+
marginY: CONFIG.marginY,
|
|
220
|
+
maxLineWidth: maxLineWidth
|
|
221
|
+
});
|
|
222
|
+
_context.next = 12;
|
|
223
|
+
return pdfDoc.save();
|
|
224
|
+
|
|
225
|
+
case 12:
|
|
226
|
+
pdfBytes = _context.sent;
|
|
227
|
+
return _context.abrupt("return", new Blob([pdfBytes], {
|
|
228
|
+
type: 'application/pdf'
|
|
229
|
+
}));
|
|
230
|
+
|
|
231
|
+
case 14:
|
|
232
|
+
case "end":
|
|
233
|
+
return _context.stop();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}, _callee);
|
|
237
|
+
}));
|
|
238
|
+
|
|
239
|
+
return function makePdfBlobFromText(_x) {
|
|
240
|
+
return _ref4.apply(this, arguments);
|
|
241
|
+
};
|
|
242
|
+
}();
|