cozy-ui 135.5.0 → 135.6.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.
Files changed (34) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/assets/icons/ui/expand.svg +1 -0
  3. package/assets/icons/ui/export.svg +1 -0
  4. package/assets/icons/ui/narrow.svg +1 -0
  5. package/package.json +1 -1
  6. package/react/ActionsMenu/Actions/exportToText.js +54 -0
  7. package/react/ActionsMenu/Actions/helpers.js +20 -0
  8. package/react/ActionsMenu/Actions/index.js +1 -0
  9. package/react/ActionsMenu/Actions/locales/en.json +2 -1
  10. package/react/ActionsMenu/Actions/locales/fr.json +2 -1
  11. package/react/ActionsMenu/Actions/locales/ru.json +3 -2
  12. package/react/ActionsMenu/Actions/locales/vi.json +3 -2
  13. package/react/ActionsMenu/Actions/pdfHelpers.js +187 -0
  14. package/react/Icon/Readme.md +7 -1
  15. package/react/Icons/Expand.jsx +12 -0
  16. package/react/Icons/Export.jsx +13 -0
  17. package/react/Icons/Narrow.jsx +16 -0
  18. package/transpiled/react/ActionsMenu/Actions/exportToText.d.ts +13 -0
  19. package/transpiled/react/ActionsMenu/Actions/exportToText.js +84 -0
  20. package/transpiled/react/ActionsMenu/Actions/helpers.d.ts +1 -0
  21. package/transpiled/react/ActionsMenu/Actions/helpers.js +20 -1
  22. package/transpiled/react/ActionsMenu/Actions/index.d.ts +1 -0
  23. package/transpiled/react/ActionsMenu/Actions/index.js +2 -1
  24. package/transpiled/react/ActionsMenu/Actions/locales/withActionsLocales.js +8 -4
  25. package/transpiled/react/ActionsMenu/Actions/pdfHelpers.d.ts +34 -0
  26. package/transpiled/react/ActionsMenu/Actions/pdfHelpers.js +242 -0
  27. package/transpiled/react/Icon/icons-sprite.d.ts +1 -1
  28. package/transpiled/react/Icon/icons-sprite.js +1 -1
  29. package/transpiled/react/Icons/Expand.d.ts +2 -0
  30. package/transpiled/react/Icons/Expand.js +14 -0
  31. package/transpiled/react/Icons/Export.d.ts +2 -0
  32. package/transpiled/react/Icons/Export.js +16 -0
  33. package/transpiled/react/Icons/Narrow.d.ts +2 -0
  34. package/transpiled/react/Icons/Narrow.js +16 -0
@@ -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
+ }();