@paprize/puppeteer 0.0.12 → 0.2.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.
@@ -290,6 +290,12 @@ var sectionClassName = "pz-section";
290
290
  var previewClassName = "pz-preview";
291
291
  var attributePrefix = "data-pz-";
292
292
 
293
+ // ../core/src/logger.ts
294
+ var import_loglevel = __toESM(require_loglevel(), 1);
295
+ var logger = import_loglevel.default.getLogger(loggerName);
296
+ logger.setDefaultLevel("info");
297
+ var logger_default = logger;
298
+
293
299
  // ../core/src/debugUtilities/debugMode.ts
294
300
  var debugMode = false;
295
301
  var isDebugMode = () => debugMode;
@@ -333,7 +339,18 @@ var layoutOptionsAttributes = {
333
339
  },
334
340
  keepOnSamePage: {
335
341
  key: layoutOptionKeepOnSamePageAttribute,
336
- reader: (value) => value === "true"
342
+ reader: (value) => {
343
+ switch (value) {
344
+ case "true":
345
+ return true;
346
+ case "false":
347
+ return false;
348
+ case "prefer":
349
+ return "prefer";
350
+ default:
351
+ return defaultLayoutOptions.keepOnSamePage;
352
+ }
353
+ }
337
354
  },
338
355
  hyphenationDisabled: {
339
356
  key: layoutOptionHyphenationDisabledAttribute,
@@ -346,9 +363,7 @@ function assignAttribute(key, reader, value, attributes) {
346
363
  return;
347
364
  }
348
365
  const parsedValue = reader(value);
349
- if (parsedValue !== void 0) {
350
- attributes[key] = parsedValue;
351
- }
366
+ attributes[key] = parsedValue;
352
367
  }
353
368
  function getNodeLayoutOptionsFromAttribute(node) {
354
369
  if (!node) {
@@ -381,12 +396,6 @@ function resolvePaginationOptions(node, globalConfig) {
381
396
  return { ...defaultPaginationOptions, ...globalConfig, ...attributes };
382
397
  }
383
398
 
384
- // ../core/src/logger.ts
385
- var import_loglevel = __toESM(require_loglevel(), 1);
386
- var logger = import_loglevel.default.getLogger(loggerName);
387
- logger.setDefaultLevel("info");
388
- var logger_default = logger;
389
-
390
399
  // ../core/src/paginate/PaginationPlugin.ts
391
400
  var logPrefix = "\x1B[46mPLUGIN\x1B[0m";
392
401
  function callPluginHook(plugins, hookName, ...args) {
@@ -427,21 +436,25 @@ var PageElement = class _PageElement {
427
436
  config;
428
437
  type = PageNodeType.Element;
429
438
  transaction;
430
- clonedFrom;
431
- cloneCount;
439
+ clonedData;
432
440
  constructor(element, transaction, config, clonedFrom) {
433
441
  this._node = element;
434
442
  this.transaction = transaction;
435
443
  this.config = config;
436
- this.clonedFrom = clonedFrom;
437
- this.cloneCount = clonedFrom ? clonedFrom.cloneCount + 1 : 0;
444
+ this.clonedData = clonedFrom ? {
445
+ clonedFrom,
446
+ cloneCount: clonedFrom.clonedData.cloneCount + 1
447
+ } : {
448
+ clonedFrom: void 0,
449
+ cloneCount: 0
450
+ };
438
451
  }
439
452
  getOriginalNode() {
440
- let current = this.clonedFrom;
441
- while (current?.clonedFrom) {
442
- current = current.clonedFrom;
453
+ let current = this.clonedData.clonedFrom;
454
+ while (current?.clonedData.clonedFrom) {
455
+ current = current.clonedData.clonedFrom;
443
456
  }
444
- return current?._node;
457
+ return current?._node ?? this._node;
445
458
  }
446
459
  appendChild(node) {
447
460
  if (this.transaction.isActive) {
@@ -678,7 +691,9 @@ var logPrefix3 = "\x1B[102mPAGE\x1B[0m";
678
691
  var PageState = class _PageState {
679
692
  currentPage;
680
693
  activeElement;
694
+ // most recently added/handled node
681
695
  currentElement;
696
+ // the current parent container/context
682
697
  parentStack;
683
698
  pageIsFull;
684
699
  pageIndex;
@@ -819,9 +834,14 @@ var PageManager = class _PageManager {
819
834
  return !this._pageState.pageIsFull && this._pageState.currentPage.getHeight() + (elementHeight || 1e-4) <= this._pageState.pageHeight;
820
835
  }
821
836
  isOverFlow() {
822
- return this._pageState.currentPage.getHeight() > this._pageState.pageHeight;
837
+ return this._pageState.pageIsFull || this._pageState.currentPage.getHeight() > this._pageState.pageHeight;
823
838
  }
824
839
  markPageAsFull() {
840
+ logger_default.debug(
841
+ logPrefix3,
842
+ "marking page as full",
843
+ this._pageState.activeElement
844
+ );
825
845
  this._pageState.pageIsFull = true;
826
846
  }
827
847
  appendChild(node, withChildren) {
@@ -874,19 +894,28 @@ function paginateElementAcrossPages(currentNode, pageManager) {
874
894
  return nextPageResult;
875
895
  }
876
896
  rollback();
877
- logger_default.debug("Element is too big to fit on a page", currentNode);
897
+ if (currentNode.config.keepOnSamePage === "prefer") {
898
+ return tryPlaceElementWithoutChildren(currentNode, pageManager);
899
+ }
900
+ logger_default.error("Element is too big to fit on a page", currentNode);
878
901
  return SplitResult.None;
879
902
  }
880
903
  function tryPlaceElement(currentNode, pageManager) {
881
904
  if (pageManager.hasEmptySpace(currentNode.getHeight())) {
882
- const clonedNode2 = pageManager.appendChild(currentNode, true);
905
+ const clonedNode = pageManager.appendChild(currentNode, true);
883
906
  if (pageManager.isOverFlow()) {
884
- clonedNode2.remove();
907
+ clonedNode.remove();
885
908
  } else {
886
909
  return SplitResult.FullNodePlaced;
887
910
  }
888
911
  }
889
- if (currentNode.config.keepOnSamePage || currentNode.getChildrenCount() === 0) {
912
+ if (currentNode.config.keepOnSamePage === false) {
913
+ return tryPlaceElementWithoutChildren(currentNode, pageManager);
914
+ }
915
+ return SplitResult.None;
916
+ }
917
+ function tryPlaceElementWithoutChildren(currentNode, pageManager) {
918
+ if (currentNode.getChildrenCount() === 0) {
890
919
  return SplitResult.None;
891
920
  }
892
921
  const clonedNode = pageManager.appendChild(currentNode, false);
@@ -1027,6 +1056,13 @@ var Paginator = class _Paginator {
1027
1056
  return result;
1028
1057
  }
1029
1058
  processAllNodes() {
1059
+ callPluginHook(
1060
+ this._config.plugins,
1061
+ "beforePagination",
1062
+ this._config.id,
1063
+ this._domState,
1064
+ this._pageManager
1065
+ );
1030
1066
  this._domState.goToNextNode();
1031
1067
  do {
1032
1068
  import_loglevel2.default.debug(
@@ -1067,7 +1103,7 @@ var Paginator = class _Paginator {
1067
1103
  handleNodeSkipped() {
1068
1104
  import_loglevel2.default.debug(logPrefix4, "node skipped - couldn't paginate");
1069
1105
  DEV: markIgnoredNode(this._domState.currentNode);
1070
- this._domState.goToNextNode();
1106
+ this._domState.goToNextSiblingOrParentSibling();
1071
1107
  }
1072
1108
  handleFullNodePlaced() {
1073
1109
  import_loglevel2.default.debug(logPrefix4, "node fully paginated");
@@ -1357,11 +1393,11 @@ var overlay = {
1357
1393
  left: 0,
1358
1394
  top: 0
1359
1395
  };
1360
- var section = (sectionId2) => ({
1361
- page: `section-${sectionId2}`
1396
+ var section = (sectionId) => ({
1397
+ page: `section-${sectionId}`
1362
1398
  });
1363
- function sectionPageMedia(sectionId2, size) {
1364
- return `@page section-${sectionId2} {
1399
+ function sectionPageMedia(sectionId, size) {
1400
+ return `@page section-${sectionId} {
1365
1401
  margin: none;
1366
1402
  size:${size.width} ${size.height};
1367
1403
  width:${size.width};
@@ -1393,13 +1429,74 @@ function cloneComponents(components) {
1393
1429
  };
1394
1430
  }
1395
1431
 
1432
+ // ../core/src/plugins/OrderedListPlugin.ts
1433
+ var OrderedListPlugin = class {
1434
+ // original list -> (clone list -> count of items rendered in the clone)
1435
+ _originalItemCount = /* @__PURE__ */ new Map();
1436
+ name = "orderedList";
1437
+ order = 1;
1438
+ onClone = (_id, source, cloned) => {
1439
+ if (!this._isListItem(source)) return;
1440
+ const isSpreadAcrossPages = cloned.clonedData.cloneCount > 1;
1441
+ if (!isSpreadAcrossPages) return;
1442
+ const node = cloned.getNode();
1443
+ node.style.listStyleType = "none";
1444
+ const sourceParent = cloned.clonedData.clonedFrom?.getNode().parentElement;
1445
+ const originalParent = cloned.getOriginalNode().parentElement;
1446
+ if (!sourceParent || !originalParent || !this._isOrderedList(sourceParent) || !this._isOrderedList(originalParent)) {
1447
+ return;
1448
+ }
1449
+ const currentCount = this._readRenderedItemCount(
1450
+ originalParent,
1451
+ (list) => list === sourceParent
1452
+ );
1453
+ this._setListItemCount(originalParent, sourceParent, currentCount - 1);
1454
+ };
1455
+ afterVisitNode = (_id, _result, _domState, pageManager) => {
1456
+ const currentElement = pageManager.getPageState().currentElement;
1457
+ const node = currentElement.getNode();
1458
+ if (!this._isOrderedList(node)) return;
1459
+ const originalList = currentElement.getOriginalNode();
1460
+ const renderedItemCount = this._readRenderedItemCount(
1461
+ originalList,
1462
+ (list) => list !== node
1463
+ );
1464
+ node.start = originalList.start + renderedItemCount;
1465
+ const itemCount = this._getListItemCount(node);
1466
+ this._setListItemCount(originalList, node, itemCount);
1467
+ };
1468
+ beforePagination = () => {
1469
+ this._originalItemCount.clear();
1470
+ };
1471
+ _isListItem(element) {
1472
+ return element.tagName === "LI";
1473
+ }
1474
+ _isOrderedList(element) {
1475
+ return element.tagName === "OL";
1476
+ }
1477
+ _setListItemCount(originalList, list, count) {
1478
+ const cloneCatalog = this._originalItemCount.get(originalList) ?? /* @__PURE__ */ new Map();
1479
+ cloneCatalog.set(list, count);
1480
+ this._originalItemCount.set(originalList, cloneCatalog);
1481
+ }
1482
+ _readRenderedItemCount(originalList, filter) {
1483
+ const cloneCatalog = this._originalItemCount.get(originalList);
1484
+ if (!cloneCatalog) return 0;
1485
+ return [...cloneCatalog.entries()].filter((i) => filter(i[0])).reduce((a, b) => a + b[1], 0);
1486
+ }
1487
+ _getListItemCount(list) {
1488
+ return [...list.children].filter((child) => this._isListItem(child)).length;
1489
+ }
1490
+ };
1491
+
1396
1492
  // ../core/src/plugins/PageBreakPlugin.ts
1397
- var pageBreakAttributeName = "data-pz-page-break";
1493
+ var pageBreakAttributeName = `${attributePrefix}page-break`;
1398
1494
  var PageBreakPlugin = class {
1399
1495
  name = "pageBreak";
1400
1496
  order = 1;
1401
1497
  onVisitElement = (_id, domState, pageManager, context) => {
1402
- if (domState.currentNode.getNode().getAttribute(pageBreakAttributeName) === "true") {
1498
+ const attr = domState.currentNode.getNode().getAttribute(pageBreakAttributeName);
1499
+ if (attr === "true" || attr === "") {
1403
1500
  pageManager.markPageAsFull();
1404
1501
  context.result = SplitResult.FullNodePlaced;
1405
1502
  }
@@ -1422,10 +1519,10 @@ var TablePlugin = class {
1422
1519
  const tableElement = pageState.parentStack.find(
1423
1520
  (node) => this._isTable(node.getNode())
1424
1521
  );
1425
- if (!tableElement || !tableElement.clonedFrom) {
1522
+ if (!tableElement || !tableElement.clonedData.clonedFrom) {
1426
1523
  return;
1427
1524
  }
1428
- const openTable = tableElement.clonedFrom;
1525
+ const openTable = tableElement.clonedData.clonedFrom;
1429
1526
  const openTableNode = openTable.getNode();
1430
1527
  const header = openTableNode.tHead;
1431
1528
  const hasHeader = header !== null;
@@ -1450,7 +1547,7 @@ var TablePlugin = class {
1450
1547
  onVisitElement = (_id, domState, _pageManager, context) => {
1451
1548
  const node = domState.currentNode.getNode();
1452
1549
  if (node.tagName === "TR") {
1453
- domState.currentNode.config.keepOnSamePage = true;
1550
+ domState.currentNode.config.keepOnSamePage = "prefer";
1454
1551
  } else if (node.tagName === "TFOOT" && this._options.cloneFooter) {
1455
1552
  context.result = SplitResult.FullNodePlaced;
1456
1553
  } else if (node.tagName === "THEAD" && this._options.cloneHeader) {
@@ -1458,7 +1555,7 @@ var TablePlugin = class {
1458
1555
  }
1459
1556
  };
1460
1557
  onClone = (_id, source, cloned) => {
1461
- if (!this._isTable(source) || !cloned.clonedFrom) {
1558
+ if (!this._isTable(source) || !cloned.clonedData.clonedFrom) {
1462
1559
  return;
1463
1560
  }
1464
1561
  const node = cloned.getNode();
@@ -1504,7 +1601,8 @@ var TablePlugin = class {
1504
1601
  // ../core/src/plugins/defaultPlugins.ts
1505
1602
  var defaultPlugins = [
1506
1603
  new PageBreakPlugin(),
1507
- new TablePlugin()
1604
+ new TablePlugin(),
1605
+ new OrderedListPlugin()
1508
1606
  ];
1509
1607
 
1510
1608
  // ../core/src/plugins/debugPlugin.ts
@@ -1585,8 +1683,8 @@ var PromiseTracker = class _PromiseTracker {
1585
1683
  };
1586
1684
 
1587
1685
  // ../core/src/utils.ts
1588
- function buildPageId(sectionId2, pageIndex) {
1589
- return `${sectionId2}-${pageIndex + 1}`;
1686
+ function buildPageId(sectionId, pageIndex) {
1687
+ return `${sectionId}-${pageIndex + 1}`;
1590
1688
  }
1591
1689
  function adjustPageSize(size, orientation) {
1592
1690
  if (orientation === "landscape") {
@@ -1622,8 +1720,8 @@ var ReportBuilder = class {
1622
1720
  /**
1623
1721
  * Removes a section from the registered sections, if it has already been registered in the report.
1624
1722
  */
1625
- removeSection(sectionId2) {
1626
- this._sections.delete(sectionId2);
1723
+ removeSection(sectionId) {
1724
+ this._sections.delete(sectionId);
1627
1725
  }
1628
1726
  /**
1629
1727
  * Registers a section by its ID, specifying the page size, margins, and other options.
@@ -2064,12 +2162,12 @@ var PaprizeReport = class {
2064
2162
  }
2065
2163
  return state;
2066
2164
  }
2067
- _createSectionInDom(sectionId2) {
2165
+ _createSectionInDom(sectionId) {
2068
2166
  const section2 = document.createElement("div");
2069
- section2.setAttribute(sectionIdMetadataAttribute, sectionId2);
2167
+ section2.setAttribute(sectionIdMetadataAttribute, sectionId);
2070
2168
  section2.classList.add(sectionClassName);
2071
- section2.id = sectionId2;
2072
- Object.assign(section2.style, reportStyles.section(sectionId2));
2169
+ section2.id = sectionId;
2170
+ Object.assign(section2.style, reportStyles.section(sectionId));
2073
2171
  this._root.appendChild(section2);
2074
2172
  return section2;
2075
2173
  }
@@ -2196,16 +2294,36 @@ var PaprizeReport = class {
2196
2294
  }
2197
2295
  };
2198
2296
 
2199
- // src/zeroReport.ts
2297
+ // src/zeroReportUtils.ts
2200
2298
  var sectionPageSizeAttribute = `${attributePrefix}section-page-size`;
2299
+ var sectionPageWidthAttribute = `${attributePrefix}section-page-width`;
2300
+ var sectionPageHeightAttribute = `${attributePrefix}section-page-height`;
2301
+ var sectionPageOrientationAttribute = `${attributePrefix}section-page-orientation`;
2201
2302
  var sectionPageMarginAttribute = `${attributePrefix}section-page-margin`;
2202
2303
  function parsePageSizeAttribute(section2) {
2203
2304
  const sizeAttributeValue = section2.getAttribute(sectionPageSizeAttribute);
2305
+ const widthAttributeValue = section2.getAttribute(sectionPageWidthAttribute);
2306
+ const heightAttributeValue = section2.getAttribute(
2307
+ sectionPageHeightAttribute
2308
+ );
2309
+ if (widthAttributeValue && heightAttributeValue) {
2310
+ return {
2311
+ width: widthAttributeValue,
2312
+ height: heightAttributeValue
2313
+ };
2314
+ }
2204
2315
  const size = Object.entries(pageSize).find(
2205
2316
  ([key]) => key.toLowerCase() === sizeAttributeValue?.toLowerCase()
2206
2317
  )?.[1];
2207
2318
  return size ?? pageSize.A4;
2208
2319
  }
2320
+ function parsePageOrientation(section2) {
2321
+ const orientationAttributeValue = section2.getAttribute(sectionPageOrientationAttribute)?.toLocaleLowerCase();
2322
+ if (orientationAttributeValue === "landscape" || orientationAttributeValue === "portrait") {
2323
+ return orientationAttributeValue;
2324
+ }
2325
+ return void 0;
2326
+ }
2209
2327
  function parsePageMarginAttribute(section2) {
2210
2328
  const size = section2.getAttribute(sectionPageMarginAttribute);
2211
2329
  const margin = Object.entries(pageMargin).find(
@@ -2213,23 +2331,26 @@ function parsePageMarginAttribute(section2) {
2213
2331
  )?.[1];
2214
2332
  return margin ?? pageMargin.Narrow;
2215
2333
  }
2216
- var sectionId = 1;
2217
- var report = new PaprizeReport();
2218
- var elements = document.querySelectorAll(`[${sectionAttribute}]`);
2219
- for (const el of elements) {
2220
- const id = el.id || `section-${sectionId++}`;
2221
- if (!el.id) {
2222
- el.id = id;
2223
- }
2224
- await report.addSection({
2225
- size: parsePageSizeAttribute(el),
2226
- margin: parsePageMarginAttribute(el),
2227
- id
2228
- });
2334
+ async function runZeroReport() {
2335
+ let sectionId = 1;
2336
+ const report = new PaprizeReport();
2337
+ const elements = document.querySelectorAll(`[${sectionAttribute}]`);
2338
+ for (const el of elements) {
2339
+ const id = el.id || `section-${sectionId++}`;
2340
+ if (!el.id) {
2341
+ el.id = id;
2342
+ }
2343
+ await report.addSection({
2344
+ size: parsePageSizeAttribute(el),
2345
+ orientation: parsePageOrientation(el),
2346
+ margin: parsePageMarginAttribute(el),
2347
+ id
2348
+ });
2349
+ }
2350
+ await report.schedulePagination();
2229
2351
  }
2230
- await report.schedulePagination();
2231
- export {
2232
- sectionPageMarginAttribute,
2233
- sectionPageSizeAttribute
2234
- };
2352
+
2353
+ // src/zeroReport.ts
2354
+ await runZeroReport();
2355
+ /* v8 ignore if -- @preserve */
2235
2356
  //# sourceMappingURL=paprize-zero.js.map