@paprize/puppeteer 0.0.5 → 0.0.7

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.
@@ -315,60 +315,51 @@ function moveOffscreen(element) {
315
315
  element.style.top = "-9999px";
316
316
  }
317
317
 
318
+ // ../core/src/paginate/LayoutOptions.ts
319
+ var defaultLayoutOptions = {
320
+ hyphen: "-",
321
+ keepOnSamePage: false,
322
+ hyphenationDisabled: false
323
+ };
324
+
318
325
  // ../core/src/paginate/attributes.ts
319
- var AttributeValueDef = class _AttributeValueDef {
320
- name;
321
- defaultValue;
322
- _reader;
323
- constructor(name, reader, defaultValue) {
324
- this.name = name;
325
- this._reader = reader;
326
- this.defaultValue = defaultValue;
327
- }
328
- read(value) {
329
- return this._reader(value);
330
- }
331
- static createStr(name, defaultValue) {
332
- return new _AttributeValueDef(name, (value) => value, defaultValue);
333
- }
334
- static createBool(name, defaultValue) {
335
- return new _AttributeValueDef(
336
- name,
337
- (value) => value === "true",
338
- defaultValue
339
- );
326
+ var layoutOptionsAttributes = {
327
+ hyphen: {
328
+ key: `${attributePrefix}hyphen`,
329
+ reader: (value) => String(value)
330
+ },
331
+ keepOnSamePage: {
332
+ key: `${attributePrefix}keep-on-same-page`,
333
+ reader: (value) => value === "true"
334
+ },
335
+ hyphenationDisabled: {
336
+ key: `${attributePrefix}hyphenation-disabled`,
337
+ reader: (value) => value === "true"
340
338
  }
341
339
  };
342
- var AttributeDef = {
343
- hyphen: AttributeValueDef.createStr("hyphen", "-"),
344
- keepOnSamePage: AttributeValueDef.createBool("keep-on-same-page", false),
345
- hyphenationEnabled: AttributeValueDef.createBool(
346
- "hyphenation-enabled",
347
- true
348
- )
349
- };
350
- var defaultConfigAttribute = Object.fromEntries(
351
- Object.entries(AttributeDef).map(([key, configValue]) => [
352
- key,
353
- configValue.defaultValue
354
- ])
355
- );
356
340
  var attributeCache = /* @__PURE__ */ new WeakMap();
357
- function getNodeConfigAttribute(node) {
341
+ function assignAttribute(key, reader, value, attributes) {
342
+ if (value == null) {
343
+ return;
344
+ }
345
+ const parsedValue = reader(value);
346
+ if (parsedValue !== void 0) {
347
+ attributes[key] = parsedValue;
348
+ }
349
+ }
350
+ function getNodeLayoutOptionsFromAttribute(node) {
358
351
  if (!node) {
359
352
  return {};
360
353
  }
361
354
  if (!(node instanceof Element)) {
362
- return getNodeConfigAttribute(node?.parentNode);
355
+ return getNodeLayoutOptionsFromAttribute(node?.parentNode);
363
356
  }
364
357
  const attributes = {};
365
- for (const key in AttributeDef) {
366
- const valueDef = AttributeDef[key];
367
- const attrName = `${attributePrefix}${valueDef.name}`;
368
- const value = node.getAttribute(attrName);
369
- if (value !== null) {
370
- attributes[key] = valueDef.read(value);
371
- }
358
+ for (const key of Object.keys(defaultLayoutOptions)) {
359
+ const typedKey = key;
360
+ const attribute = layoutOptionsAttributes[typedKey];
361
+ const value = node.getAttribute(attribute.key);
362
+ assignAttribute(typedKey, attribute.reader, value, attributes);
372
363
  }
373
364
  const parentAttributes = node.parentNode ? attributeCache.get(node.parentNode) : void 0;
374
365
  const result = { ...parentAttributes, ...attributes };
@@ -376,15 +367,15 @@ function getNodeConfigAttribute(node) {
376
367
  return result;
377
368
  }
378
369
 
379
- // ../core/src/paginate/PaginationConfig.ts
380
- var defaultConfig = {
370
+ // ../core/src/paginate/PaginationOptions.ts
371
+ var defaultPaginationOptions = {
381
372
  id: "default",
382
373
  plugins: [],
383
- ...defaultConfigAttribute
374
+ ...defaultLayoutOptions
384
375
  };
385
- function getConfigFromAttributes(node, globalConfig) {
386
- const attributes = getNodeConfigAttribute(node);
387
- return { ...defaultConfig, ...globalConfig, ...attributes };
376
+ function resolvePaginationOptions(node, globalConfig) {
377
+ const attributes = getNodeLayoutOptionsFromAttribute(node);
378
+ return { ...defaultPaginationOptions, ...globalConfig, ...attributes };
388
379
  }
389
380
 
390
381
  // ../core/src/logger.ts
@@ -628,7 +619,7 @@ var DomState = class {
628
619
  this._currentNode = createPageNode(
629
620
  this._treeWalker.currentNode,
630
621
  this._transaction,
631
- getConfigFromAttributes(this._treeWalker.currentNode, this._config)
622
+ resolvePaginationOptions(this._treeWalker.currentNode, this._config)
632
623
  );
633
624
  DEV: markCurrentNode(this._currentNode);
634
625
  DEV: unmarkCurrentNode(this._previousNode);
@@ -952,7 +943,7 @@ function handleTokenOverflow(token, pageManager, config) {
952
943
  };
953
944
  }
954
945
  rollback();
955
- if (!config.hyphenationEnabled) {
946
+ if (config.hyphenationDisabled) {
956
947
  logger_default.warn("Hyphenation disabled, skipping oversized token:", token);
957
948
  return {
958
949
  completed: false
@@ -991,7 +982,7 @@ var Paginator = class _Paginator {
991
982
  _tempContainer;
992
983
  _config;
993
984
  constructor(root, pageSize2, config) {
994
- this._config = { ...defaultConfig, ...config };
985
+ this._config = { ...defaultPaginationOptions, ...config };
995
986
  this._tempContainer = _Paginator.createTempContainer(this._config.id);
996
987
  this._transaction = new Transaction();
997
988
  this._domState = new DomState(root, this._transaction, this._config);
@@ -1134,41 +1125,80 @@ var EventDispatcher = class {
1134
1125
  listeners.delete(handler);
1135
1126
  registry.set(name, listeners);
1136
1127
  }
1137
- dispatch(name, ...args) {
1128
+ async dispatch(name, ...args) {
1138
1129
  const { registry } = this;
1139
1130
  const listeners = registry.get(name);
1140
1131
  if (!listeners) {
1141
1132
  return;
1142
1133
  }
1143
1134
  for (const listener of listeners) {
1144
- listener(...args);
1135
+ await listener(...args);
1145
1136
  }
1146
1137
  }
1147
1138
  };
1148
1139
 
1149
1140
  // ../core/src/report/pageConst.ts
1150
- var _pageSize = {
1141
+ var pageSize = {
1142
+ /** 841mm x 594mm */
1151
1143
  A1: { height: "841mm", width: "594mm" },
1144
+ /** 594mm x 420mm */
1152
1145
  A2: { height: "594mm", width: "420mm" },
1146
+ /** 420mm x 297mm */
1153
1147
  A3: { height: "420mm", width: "297mm" },
1148
+ /** 297mm x 210mm */
1154
1149
  A4: { height: "297mm", width: "210mm" },
1150
+ /** 210mm x 148mm */
1155
1151
  A5: { height: "210mm", width: "148mm" },
1152
+ /** 148mm x 105mm */
1156
1153
  A6: { height: "148mm", width: "105mm" },
1154
+ /** 500mm x 353mm */
1157
1155
  B3: { height: "500mm", width: "353mm" },
1156
+ /** 353mm x 250mm */
1158
1157
  B4: { height: "353mm", width: "250mm" },
1158
+ /** 250mm x 176mm */
1159
1159
  B5: { height: "250mm", width: "176mm" },
1160
+ /** 8.5in x 11in */
1160
1161
  Letter: { height: "8.5in", width: "11in" },
1162
+ /** 11in x 8.5in */
1161
1163
  Legal: { height: "11in", width: "8.5in" },
1164
+ /** 11in x 17in */
1162
1165
  Tabloid: { height: "11in", width: "17in" }
1163
1166
  };
1164
- var _pageMargin = {
1165
- Normal: { top: "1in", right: "1in", bottom: "1in", left: "1in" },
1166
- Narrow: { top: "0.4in", right: "0.6in", bottom: "0.6in", left: "0.6in" },
1167
- Wide: { top: "0.5in", right: "2in", bottom: "0.5in", left: "2in" },
1168
- None: { top: "0in", right: "0in", bottom: "0in", left: "0in" }
1167
+ var pageMargin = {
1168
+ /** Top, Right, Bottom, Left: 1in */
1169
+ Normal: {
1170
+ top: "1in",
1171
+ right: "1in",
1172
+ bottom: "1in",
1173
+ left: "1in"
1174
+ },
1175
+ /** Top: 0.4in, Right, Bottom, Left: 0.6in */
1176
+ Narrow: {
1177
+ top: "0.4in",
1178
+ right: "0.6in",
1179
+ bottom: "0.6in",
1180
+ left: "0.6in"
1181
+ },
1182
+ /** Top, Bottom: 0.5in, Right, Left: 2in */
1183
+ Wide: {
1184
+ top: "0.5in",
1185
+ right: "2in",
1186
+ bottom: "0.5in",
1187
+ left: "2in"
1188
+ },
1189
+ /** Top, Right, Bottom, Left: 0 */
1190
+ None: {
1191
+ top: "0in",
1192
+ right: "0in",
1193
+ bottom: "0in",
1194
+ left: "0in"
1195
+ }
1169
1196
  };
1170
- var pageSize = _pageSize;
1171
- var pageMargin = _pageMargin;
1197
+
1198
+ // ../core/src/window.ts
1199
+ var paprize_isInitialized = "__PAPRIZE_IS_INITIALIZED";
1200
+ var paprize_isReady = "__PAPRIZE_IS_READY";
1201
+ var paprize_readJsonDataFile = "__PAPRIZE_READ_JSON_DATA_FILE";
1172
1202
 
1173
1203
  // ../core/src/report/utils.ts
1174
1204
  function getVisibleSize(element) {
@@ -1181,7 +1211,7 @@ function getVisibleSize(element) {
1181
1211
  width: rect.width + marginLeft + marginRight
1182
1212
  };
1183
1213
  }
1184
- function calculatePageDimensions(current, sectionHeader, sectionFooter) {
1214
+ function calculatePageSizes(current, sectionHeader, sectionFooter) {
1185
1215
  const { height, width } = getVisibleSize(current);
1186
1216
  const sectionHeaderHeight = sectionHeader ? getVisibleHeight(sectionHeader) : 0;
1187
1217
  const sectionFooterHeight = sectionFooter ? getVisibleHeight(sectionFooter) : 0;
@@ -1211,6 +1241,22 @@ function createSectionPageHeightPlugin(height, sectionHeaderHeight, sectionFoote
1211
1241
  function shorthand(margin) {
1212
1242
  return margin ? `${margin.top} ${margin.right} ${margin.bottom} ${margin.left}` : "0";
1213
1243
  }
1244
+ function lazyPromise(factory) {
1245
+ let promise = null;
1246
+ return () => {
1247
+ if (!promise) {
1248
+ promise = factory();
1249
+ }
1250
+ return promise;
1251
+ };
1252
+ }
1253
+ async function jsonDataReader() {
1254
+ if (!(paprize_readJsonDataFile in window)) {
1255
+ return null;
1256
+ }
1257
+ const strData = await window[paprize_readJsonDataFile]?.();
1258
+ return strData ? JSON.parse(strData) : null;
1259
+ }
1214
1260
 
1215
1261
  // ../core/src/report/reportStyles.ts
1216
1262
  var globalStyle = `
@@ -1274,12 +1320,12 @@ var outOfScreen = {
1274
1320
  top: "-9999px",
1275
1321
  visibility: "hidden"
1276
1322
  };
1277
- var page = (pageDimension, pageMargin2) => ({
1323
+ var page = (pageSize2, pageMargin2) => ({
1278
1324
  display: "flex",
1279
1325
  flexDirection: "column",
1280
- width: pageDimension.width,
1281
- height: pageDimension.height,
1282
- maxHeight: pageDimension.height,
1326
+ width: pageSize2.width,
1327
+ height: pageSize2.height,
1328
+ maxHeight: pageSize2.height,
1283
1329
  position: "relative",
1284
1330
  padding: shorthand(pageMargin2),
1285
1331
  zIndex: "1"
@@ -1299,12 +1345,12 @@ var overlay = {
1299
1345
  var section = (sectionId2) => ({
1300
1346
  page: `section-${sectionId2}`
1301
1347
  });
1302
- function sectionPageMedia(sectionId2, dimension) {
1348
+ function sectionPageMedia(sectionId2, size) {
1303
1349
  return `@page section-${sectionId2} {
1304
1350
  margin: none;
1305
- size:${dimension.width} ${dimension.height};
1306
- width:${dimension.width};
1307
- height:${dimension.height};
1351
+ size:${size.width} ${size.height};
1352
+ width:${size.width};
1353
+ height:${size.height};
1308
1354
  }`;
1309
1355
  }
1310
1356
  var reportStyles = {
@@ -1451,10 +1497,6 @@ var debugPlugin = {
1451
1497
  }
1452
1498
  };
1453
1499
 
1454
- // ../core/src/window.ts
1455
- var paprize_isInitialized = "__PAPRIZE_IS_INITIALIZED";
1456
- var paprize_isReady = "__PAPRIZE_IS_READY";
1457
-
1458
1500
  // ../core/src/report/PromiseTracker.ts
1459
1501
  var PromiseTracker = class _PromiseTracker {
1460
1502
  _promises;
@@ -1501,6 +1543,17 @@ var PromiseTracker = class _PromiseTracker {
1501
1543
  }
1502
1544
  };
1503
1545
 
1546
+ // ../core/src/utils.ts
1547
+ function buildPageId(sectionId2, pageIndex) {
1548
+ return `${sectionId2}-${pageIndex + 1}`;
1549
+ }
1550
+ function adjustPageSize(size, orientation) {
1551
+ if (orientation === "landscape") {
1552
+ return { height: size.width, width: size.height };
1553
+ }
1554
+ return size;
1555
+ }
1556
+
1504
1557
  // ../core/src/report/ReportBuilder.ts
1505
1558
  var logPrefix5 = "\x1B[43mREPORT\x1B[0m";
1506
1559
  var ReportBuilder = class {
@@ -1518,18 +1571,33 @@ var ReportBuilder = class {
1518
1571
  window[paprize_isInitialized] = true;
1519
1572
  this._injectStyle(reportStyles.globalStyle);
1520
1573
  }
1574
+ /**
1575
+ * Monitor instance used to subscribe to pagination events.
1576
+ * See {@link ReportBuilderEvents} for available event types.
1577
+ */
1521
1578
  get monitor() {
1522
1579
  return this._monitor;
1523
1580
  }
1581
+ /**
1582
+ * Removes a section from the registered sections, if it has already been registered in the report.
1583
+ */
1524
1584
  removeSection(sectionId2) {
1525
1585
  this._sections.delete(sectionId2);
1526
1586
  }
1527
- tryAddSection(options, components, onPaginationCompleted) {
1587
+ /**
1588
+ * Registers a section by its ID, specifying the page size, margins, and other options.
1589
+ *
1590
+ * @param options - Configuration options for the section.
1591
+ * @param components - The DOM components associated with the section.
1592
+ * @param onPaginationCompleted - Callback invoked when pagination for the section is completed.
1593
+ * @returns `true` if the section was added to the report’s section list, or `false` if it already exists.
1594
+ */
1595
+ async tryAddSection(options, components, onPaginationCompleted) {
1528
1596
  if (this._sections.has(options.id)) {
1529
1597
  return false;
1530
1598
  }
1531
1599
  const context = {
1532
- index: this._sections.size,
1600
+ sectionIndex: this._sections.size,
1533
1601
  sectionId: options.id,
1534
1602
  isPaginated: false,
1535
1603
  isSuspended: !!options.suspense?.length,
@@ -1537,17 +1605,38 @@ var ReportBuilder = class {
1537
1605
  };
1538
1606
  this._sections.set(options.id, {
1539
1607
  context,
1540
- options,
1608
+ options: {
1609
+ ...options,
1610
+ size: adjustPageSize(
1611
+ options.size,
1612
+ options.orientation ?? "portrait"
1613
+ )
1614
+ },
1541
1615
  components,
1542
1616
  onPaginationCompleted
1543
1617
  });
1544
1618
  this._injectStyle(
1545
- reportStyles.sectionPageMedia(options.id, options.dimension)
1619
+ reportStyles.sectionPageMedia(options.id, options.size)
1546
1620
  );
1547
- this._monitor.dispatch("sectionCreated", context);
1621
+ await this._monitor.dispatch("sectionCreated", context);
1548
1622
  return true;
1549
1623
  }
1550
- async schedulePaginate() {
1624
+ /**
1625
+ * Schedules a pagination operation.
1626
+ *
1627
+ * It is not possible to schedule multiple pagination operations in parallel,
1628
+ * as the process involves DOM manipulation, and concurrent modifications
1629
+ * could cause conflicts and unexpected results.
1630
+ * Each newly scheduled operation is queued and executed sequentially.
1631
+ * When a new pagination is scheduled, any ongoing or pending operations
1632
+ * will be aborted, and the new pagination will start immediately afterward.
1633
+ *
1634
+ * @returns A promise that resolves when the first pagination cycle is completed.
1635
+ * It does not wait for suspended sections to resolve and be paginated.
1636
+ * To wait for all sections to complete pagination, use the
1637
+ * `suspension` property of the returned result object.
1638
+ */
1639
+ async schedulePagination() {
1551
1640
  if (this._sections.size === 0) {
1552
1641
  window[paprize_isReady] = true;
1553
1642
  return {
@@ -1571,6 +1660,25 @@ var ReportBuilder = class {
1571
1660
  }
1572
1661
  return this._executePagination();
1573
1662
  }
1663
+ /**
1664
+ * Retrieves JSON data injected by **@paprize/puppeteer** during server-side rendering (SSR).
1665
+ *
1666
+ * If no injected data is available, the function returns the provided `defaultData`, or `null` if none is given.
1667
+ *
1668
+ * ⚠️ **Important Notes:**
1669
+ * - This function is **not type-safe** — it performs **no runtime type validation** on the returned data.
1670
+ * - It is available **only during server-side rendering** when using **@paprize/puppeteer**.
1671
+ * - When used in **client-side rendering** or **development** mode, you should provide a `defaultData` value for testing purposes.
1672
+ *
1673
+ * @template T - The expected type of the injected JSON data.
1674
+ * @param defaultData - Optional fallback value to return if no injected data is found.
1675
+ * @returns A promise resolving to the injected JSON data if available, otherwise the provided default value or `null`.
1676
+ */
1677
+ async getJsonData(defaultData) {
1678
+ const json = await this._lazyJsonDataReader().catch(() => defaultData);
1679
+ return json ?? defaultData ?? null;
1680
+ }
1681
+ _lazyJsonDataReader = lazyPromise(jsonDataReader);
1574
1682
  async _executePagination() {
1575
1683
  this._paginationInProgress = true;
1576
1684
  this._currentAbortController = new AbortController();
@@ -1608,9 +1716,9 @@ var ReportBuilder = class {
1608
1716
  trackers.push(tracker);
1609
1717
  }
1610
1718
  const reportTracker = new PromiseTracker();
1611
- reportTracker.monitor.addEventListener("onChange", () => {
1719
+ reportTracker.monitor.addEventListener("onChange", async () => {
1612
1720
  logger_default.debug(logPrefix5, "Report pagination completed.");
1613
- this._monitor.dispatch("paginationCycleCompleted", {
1721
+ await this._monitor.dispatch("paginationCycleCompleted", {
1614
1722
  sections: [...this._sections.values()].map(
1615
1723
  (s) => s.context
1616
1724
  )
@@ -1666,7 +1774,7 @@ var ReportBuilder = class {
1666
1774
  Object.assign(
1667
1775
  temporarilyContainer.style,
1668
1776
  reportStyles.page(
1669
- state.options.dimension,
1777
+ state.options.size,
1670
1778
  state.options.margin ?? pageMargin.None
1671
1779
  )
1672
1780
  );
@@ -1699,7 +1807,7 @@ var ReportBuilder = class {
1699
1807
  temporarilyContainer.appendChild(components.sectionFooter);
1700
1808
  }
1701
1809
  document.body.appendChild(temporarilyContainer);
1702
- const { height, width, sectionHeaderHeight, sectionFooterHeight } = calculatePageDimensions(
1810
+ const { height, width, sectionHeaderHeight, sectionFooterHeight } = calculatePageSizes(
1703
1811
  components.pageContent,
1704
1812
  components.sectionHeader,
1705
1813
  components.sectionFooter
@@ -1721,14 +1829,14 @@ var ReportBuilder = class {
1721
1829
  );
1722
1830
  temporarilyContainer.remove();
1723
1831
  const pageContexts = paginatorResult.map((content, index) => ({
1724
- index,
1832
+ pageIndex: index,
1725
1833
  totalPages: paginatorResult.length,
1726
1834
  sectionId: state.options.id,
1727
1835
  pageContentHtml: content
1728
1836
  }));
1729
1837
  state.onPaginationCompleted(pageContexts);
1730
1838
  for (const pageContext of pageContexts) {
1731
- this._monitor.dispatch("pageCompleted", pageContext);
1839
+ await this._monitor.dispatch("pageCompleted", pageContext);
1732
1840
  }
1733
1841
  const sectionContext = {
1734
1842
  ...state.context,
@@ -1740,17 +1848,13 @@ var ReportBuilder = class {
1740
1848
  ...state,
1741
1849
  context: sectionContext
1742
1850
  });
1743
- this._monitor.dispatch("sectionCompleted", sectionContext);
1851
+ await this._monitor.dispatch("sectionCompleted", sectionContext);
1744
1852
  }
1745
1853
  };
1746
1854
 
1747
- // ../core/src/utils.ts
1748
- function buildPageId(sectionId2, pageIndex) {
1749
- return `${sectionId2}-${pageIndex + 1}`;
1750
- }
1751
-
1752
1855
  // ../vanilla/src/attributes.ts
1753
1856
  var sectionAttribute = `${attributePrefix}section`;
1857
+ var previewAttribute = `${attributePrefix}preview`;
1754
1858
  var sectionHeaderAttribute = `${attributePrefix}section-header`;
1755
1859
  var sectionFooterAttribute = `${attributePrefix}section-footer`;
1756
1860
  var pageHeaderAttribute = `${attributePrefix}page-header`;
@@ -1763,6 +1867,23 @@ var pageNumberValueAttribute = `${attributePrefix}v-page-number`;
1763
1867
  var totalPagesValueAttribute = `${attributePrefix}v-total-pages`;
1764
1868
  var sectionNumberValueAttribute = `${attributePrefix}v-section-number`;
1765
1869
  var totalSectionsValueAttribute = `${attributePrefix}v-total-sections`;
1870
+ var jsonDataValueAttribute = `${attributePrefix}v-json-data-key`;
1871
+
1872
+ // ../vanilla/src/utils.ts
1873
+ function get(obj, path) {
1874
+ if (!obj || !path) {
1875
+ return;
1876
+ }
1877
+ const keys = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
1878
+ let result = obj;
1879
+ for (const key of keys) {
1880
+ if (result == null) {
1881
+ return;
1882
+ }
1883
+ result = result[key];
1884
+ }
1885
+ return result?.toString();
1886
+ }
1766
1887
 
1767
1888
  // ../vanilla/src/PaprizeReport.ts
1768
1889
  var globalStyles = `
@@ -1776,6 +1897,7 @@ var PaprizeReport = class {
1776
1897
  _monitor;
1777
1898
  _root;
1778
1899
  _options;
1900
+ /** @public */
1779
1901
  constructor(options) {
1780
1902
  const style = document.createElement("style");
1781
1903
  style.textContent = globalStyles;
@@ -1807,7 +1929,7 @@ var PaprizeReport = class {
1807
1929
  "pageCompleted",
1808
1930
  (event) => {
1809
1931
  const pages = this._getSectionState(event.sectionId).pages;
1810
- this._monitor.dispatch("pageCompleted", pages[event.index]);
1932
+ this._monitor.dispatch("pageCompleted", pages[event.pageIndex]);
1811
1933
  }
1812
1934
  );
1813
1935
  this._reportManager.monitor.addEventListener(
@@ -1824,19 +1946,38 @@ var PaprizeReport = class {
1824
1946
  }
1825
1947
  _createRootElement() {
1826
1948
  const wrapper = document.createElement("div");
1827
- if (this._options.usePreviewMode) {
1949
+ if (document.querySelector(`[${previewAttribute}]`)) {
1828
1950
  wrapper.classList.add(previewClassName);
1829
1951
  }
1830
1952
  (this._options.root ?? document.body).appendChild(wrapper);
1831
1953
  return wrapper;
1832
1954
  }
1833
- async schedulePaginate() {
1834
- return this._reportManager.schedulePaginate();
1955
+ /**
1956
+ * {@inheritDoc @paprize/core!ReportBuilder.schedulePagination}
1957
+ */
1958
+ async schedulePagination() {
1959
+ const result = await this._reportManager.schedulePagination();
1960
+ return {
1961
+ suspension: result.suspension,
1962
+ sections: result.sections.map((s) => ({
1963
+ ...s,
1964
+ pages: this._sections.get(s.sectionId)?.pages ?? []
1965
+ }))
1966
+ };
1835
1967
  }
1968
+ /**
1969
+ * Monitor instance used to subscribe to pagination events.
1970
+ * See {@link PaprizeReportEvents} for available event types.
1971
+ */
1836
1972
  get monitor() {
1837
1973
  return this._monitor;
1838
1974
  }
1839
- addSection(options) {
1975
+ /**
1976
+ * Registers a section by its id, specifying the page size, margins, and other options.
1977
+ * If a section with the same id already exists, the operation will be ignored.
1978
+ * @param options - Configuration options for the section.
1979
+ */
1980
+ async addSection(options) {
1840
1981
  if (this._sections.has(options.id)) {
1841
1982
  return this;
1842
1983
  }
@@ -1848,7 +1989,7 @@ var PaprizeReport = class {
1848
1989
  }
1849
1990
  const components = this._getSectionComponents(section2);
1850
1991
  const sectionElement = this._createSectionInDom(options.id);
1851
- const result = this._reportManager.tryAddSection(
1992
+ const result = await this._reportManager.tryAddSection(
1852
1993
  options,
1853
1994
  components,
1854
1995
  (pageContexts) => {
@@ -1920,20 +2061,20 @@ var PaprizeReport = class {
1920
2061
  for (const pageContext of pageContexts) {
1921
2062
  const page2 = document.createElement("div");
1922
2063
  page2.classList.add(pageClassName);
1923
- page2.id = buildPageId(options.id, pageContext.index);
2064
+ page2.id = buildPageId(options.id, pageContext.pageIndex);
1924
2065
  page2.setAttribute(
1925
2066
  pageIndexMetadataAttribute,
1926
- pageContext.index.toString()
2067
+ pageContext.pageIndex.toString()
1927
2068
  );
1928
2069
  Object.assign(
1929
2070
  page2.style,
1930
2071
  reportStyles.page(
1931
- options.dimension,
2072
+ options.size,
1932
2073
  options.margin ?? pageMargin.None
1933
2074
  )
1934
2075
  );
1935
2076
  const components = cloneComponents(state.components);
1936
- if (pageContext.index === 0 && components.sectionHeader) {
2077
+ if (pageContext.pageIndex === 0 && components.sectionHeader) {
1937
2078
  page2.appendChild(components.sectionHeader);
1938
2079
  }
1939
2080
  if (components.pageHeader) {
@@ -1947,7 +2088,7 @@ var PaprizeReport = class {
1947
2088
  if (components.pageFooter) {
1948
2089
  pageFooterContainer.appendChild(components.pageFooter);
1949
2090
  }
1950
- if (pageContext.index === pageContexts.length - 1 && components.sectionFooter) {
2091
+ if (pageContext.pageIndex === pageContexts.length - 1 && components.sectionFooter) {
1951
2092
  pageFooterContainer.appendChild(
1952
2093
  components.sectionFooter.cloneNode(true)
1953
2094
  );
@@ -1955,18 +2096,17 @@ var PaprizeReport = class {
1955
2096
  if (pageFooterContainer.children.length > 0) {
1956
2097
  page2.appendChild(pageFooterContainer);
1957
2098
  }
1958
- this._replaceContentInfoValues(
2099
+ await this._replaceContentInfoValues(
1959
2100
  { ...components, pageContent: pageContent2 },
1960
2101
  pageContext
1961
2102
  );
1962
2103
  state.sectionElement.appendChild(page2);
1963
2104
  const domPageContext = {
1964
2105
  sectionId: options.id,
1965
- index: pageContext.index,
2106
+ pageIndex: pageContext.pageIndex,
1966
2107
  totalPages: pageContexts.length,
1967
- ...components,
1968
2108
  page: page2,
1969
- pageContent: pageContent2
2109
+ components: { ...components, pageContent: pageContent2 }
1970
2110
  };
1971
2111
  pages.push(domPageContext);
1972
2112
  }
@@ -1975,11 +2115,12 @@ var PaprizeReport = class {
1975
2115
  pages
1976
2116
  });
1977
2117
  }
1978
- _replaceContentInfoValues(components, pageContext) {
2118
+ async _replaceContentInfoValues(components, pageContext) {
2119
+ const jsonData = await this._reportManager.getJsonData();
1979
2120
  for (const component2 of Object.values(components)) {
1980
2121
  if (!component2) continue;
1981
2122
  component2.querySelectorAll(`[${pageNumberValueAttribute}]`).forEach((el) => {
1982
- el.textContent = (pageContext.index + 1).toString();
2123
+ el.textContent = (pageContext.pageIndex + 1).toString();
1983
2124
  });
1984
2125
  component2.querySelectorAll(`[${totalPagesValueAttribute}]`).forEach((el) => {
1985
2126
  el.textContent = pageContext.totalPages.toString();
@@ -1992,6 +2133,11 @@ var PaprizeReport = class {
1992
2133
  component2.querySelectorAll(`[${totalSectionsValueAttribute}]`).forEach((el) => {
1993
2134
  el.textContent = this._sections.size.toString();
1994
2135
  });
2136
+ component2.querySelectorAll(`[${jsonDataValueAttribute}]`).forEach((el) => {
2137
+ const key = el.getAttribute(jsonDataValueAttribute);
2138
+ const value = get(jsonData, key);
2139
+ el.textContent = value ?? "";
2140
+ });
1995
2141
  }
1996
2142
  }
1997
2143
  };
@@ -2000,18 +2146,11 @@ var PaprizeReport = class {
2000
2146
  var sectionPageSizeAttribute = `${attributePrefix}section-page-size`;
2001
2147
  var sectionPageMarginAttribute = `${attributePrefix}section-page-margin`;
2002
2148
  function parsePageSizeAttribute(section2) {
2003
- const size = section2.getAttribute(sectionPageSizeAttribute);
2004
- console.warn(
2005
- "ss",
2006
- size,
2007
- section2.getAttributeNames().join(","),
2008
- sectionPageSizeAttribute,
2009
- section2.getAttribute("data-pz-section-page-size")
2010
- );
2011
- const dimension = Object.entries(pageSize).find(
2012
- ([key]) => key.toLowerCase() === size?.toLowerCase()
2149
+ const sizeAttributeValue = section2.getAttribute(sectionPageSizeAttribute);
2150
+ const size = Object.entries(pageSize).find(
2151
+ ([key]) => key.toLowerCase() === sizeAttributeValue?.toLowerCase()
2013
2152
  )?.[1];
2014
- return dimension ?? pageSize.A4;
2153
+ return size ?? pageSize.A4;
2015
2154
  }
2016
2155
  function parsePageMarginAttribute(section2) {
2017
2156
  const size = section2.getAttribute(sectionPageMarginAttribute);
@@ -2022,18 +2161,19 @@ function parsePageMarginAttribute(section2) {
2022
2161
  }
2023
2162
  var sectionId = 1;
2024
2163
  var report = new PaprizeReport();
2025
- document.querySelectorAll(`[${sectionAttribute}]`).forEach((el) => {
2164
+ var elements = document.querySelectorAll(`[${sectionAttribute}]`);
2165
+ for (const el of elements) {
2026
2166
  const id = el.id || `section-${sectionId++}`;
2027
2167
  if (!el.id) {
2028
2168
  el.id = id;
2029
2169
  }
2030
- report.addSection({
2031
- dimension: parsePageSizeAttribute(el),
2170
+ await report.addSection({
2171
+ size: parsePageSizeAttribute(el),
2032
2172
  margin: parsePageMarginAttribute(el),
2033
2173
  id
2034
2174
  });
2035
- });
2036
- report.schedulePaginate();
2175
+ }
2176
+ await report.schedulePagination();
2037
2177
  export {
2038
2178
  sectionPageMarginAttribute,
2039
2179
  sectionPageSizeAttribute