@paprize/core 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.
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  Design your report using the full power of JavaScript and CSS, mark the report section by paprize components and then paprize pagination engine will transforming it into a beautiful, professional, print-ready pages.
4
4
 
5
- ![Components](https://raw.githubusercontent.com/PejmanNik/paprize/refs/heads/bootstrap/packages/website/static/img/components.svg)
5
+ ![Components](https://raw.githubusercontent.com/PejmanNik/paprize/refs/heads/main/packages/website/static/img/components.svg)
@@ -1,42 +1,16 @@
1
1
  import type * as CSS_2 from 'csstype';
2
2
  import { default as default_2 } from 'loglevel';
3
3
 
4
- export declare function adjustDimension(dimension: PageDimension, orientation: PageOrientation): PageDimension;
5
-
6
- declare const AttributeDef: {
7
- hyphen: AttributeValueDef<string>;
8
- keepOnSamePage: AttributeValueDef<boolean>;
9
- hyphenationEnabled: AttributeValueDef<boolean>;
10
- };
11
-
12
- declare type AttributeKey = keyof typeof AttributeDef;
4
+ export declare function adjustPageSize(size: PageSize, orientation?: PageOrientation): PageSize;
13
5
 
14
6
  export declare const attributePrefix = "data-pz-";
15
7
 
16
- declare type AttributeValue<K extends AttributeKey> = (typeof AttributeDef)[K] extends AttributeValueDef<infer R> ? R : never;
17
-
18
- declare class AttributeValueDef<T> {
19
- name: string;
20
- defaultValue: T;
21
- private _reader;
22
- constructor(name: string, reader: (value: string) => T, defaultValue: T);
23
- read(value: string): T;
24
- static createStr(name: string, defaultValue: string): AttributeValueDef<string>;
25
- static createBool(name: string, defaultValue: boolean): AttributeValueDef<boolean>;
26
- }
27
-
28
8
  export declare function buildPageId(sectionId: string, pageIndex: number): string;
29
9
 
30
10
  export declare function callPluginHook<T extends PluginHookNames>(plugins: PaginationPlugin[], hookName: T, ...args: Parameters<NonNullable<PaginationPlugin[T]>>): void;
31
11
 
32
12
  export declare function cloneComponents(components: SectionComponents): SectionComponents;
33
13
 
34
- export declare type ConfigAttribute = {
35
- [K in AttributeKey]?: AttributeValue<K>;
36
- };
37
-
38
- export declare function configToAttributeMap(config: ConfigAttribute): Record<string, string>;
39
-
40
14
  export declare function createLoremIpsumParagraph(wordCount: number, seed: number): string;
41
15
 
42
16
  export declare type CSSProperties = CSS_2.Properties<string | number>;
@@ -56,7 +30,7 @@ export declare class DomState {
56
30
  private _completed;
57
31
  private _currentNode;
58
32
  private _previousNode;
59
- constructor(root: Element, transaction: Transaction, config: PaginationConfig);
33
+ constructor(root: Element, transaction: Transaction, config: PaginationOptions);
60
34
  get completed(): boolean;
61
35
  get currentNode(): PageNode | null;
62
36
  get previousNode(): PageNode | null;
@@ -76,7 +50,7 @@ export declare class EventDispatcher<TEvents> {
76
50
  private registry;
77
51
  addEventListener<T extends keyof TEvents>(name: T, handler: EventHandler<T, TEvents>): () => void;
78
52
  removeEventListener<T extends keyof TEvents>(name: T, handler: EventHandler<T, TEvents>): void;
79
- dispatch<T extends keyof TEvents>(name: T, ...args: Parameters<EventHandler<T, TEvents>>): void;
53
+ dispatch<T extends keyof TEvents>(name: T, ...args: Parameters<EventHandler<T, TEvents>>): Promise<void>;
80
54
  }
81
55
 
82
56
  declare type EventHandler<TKey extends keyof TEvents, TEvents> = TEvents[TKey] extends (...args: infer TArgs) => infer TReturn ? (...args: TArgs) => TReturn : never;
@@ -97,6 +71,35 @@ export declare function isElement(node: Node): node is Element;
97
71
 
98
72
  export declare function isTextNode(node: Node): node is Text;
99
73
 
74
+ /**
75
+ * Layout options for the pagination engine
76
+ */
77
+ export declare interface LayoutOptions {
78
+ /**
79
+ * Specifies the character used for hyphenation when a word is broken across lines.
80
+ * @defaultValue "-"
81
+ */
82
+ hyphen?: string;
83
+ /**
84
+ * Prevents an element from being split across pages.
85
+ * If an element does not fit in the available space on the current page,
86
+ * it will be moved entirely to the next page. If it still does not fit on an empty page,
87
+ * it will be skipped and not rendered.
88
+ * @defaultValue false
89
+ */
90
+ keepOnSamePage?: boolean;
91
+ /**
92
+ * Disables automatic word hyphenation.
93
+ * When disabled, if a word (a sequence of text without whitespace) does not fit
94
+ * on the current page, it will move to the next page instead of being split
95
+ * with a hyphen character.
96
+ * @defaultValue false
97
+ */
98
+ hyphenationDisabled?: boolean;
99
+ }
100
+
101
+ export declare function layoutOptionsToAttributes(config: LayoutOptions): Record<string, string>;
102
+
100
103
  export declare const logger: default_2.Logger;
101
104
 
102
105
  export declare const loggerName = "paprize";
@@ -117,26 +120,36 @@ export declare class PageBreakPlugin implements PaginationPlugin {
117
120
 
118
121
  export declare const pageClassName = "pz-page";
119
122
 
123
+ /**
124
+ * Context information for a paginated page.
125
+ */
120
126
  export declare interface PageContext {
127
+ /**
128
+ * Index of the section to which this page belongs
129
+ */
121
130
  sectionId: string;
122
- index: number;
131
+ /**
132
+ * Index of this page within its section.
133
+ */
134
+ pageIndex: number;
135
+ /**
136
+ * Total number of pages in the section that contains this page.
137
+ */
123
138
  totalPages: number;
139
+ /**
140
+ * HTML string representing the paginated content of this page.
141
+ */
124
142
  pageContentHtml: string;
125
143
  }
126
144
 
127
- export declare interface PageDimension {
128
- height: string;
129
- width: string;
130
- }
131
-
132
145
  declare class PageElement {
133
146
  private readonly _node;
134
- config: PaginationConfig;
147
+ config: PaginationOptions;
135
148
  readonly type: 'element';
136
149
  readonly transaction: Transaction;
137
150
  readonly clonedFrom?: PageElement;
138
151
  readonly cloneCount: number;
139
- constructor(element: Element, transaction: Transaction, config: PaginationConfig, clonedFrom?: PageElement);
152
+ constructor(element: Element, transaction: Transaction, config: PaginationOptions, clonedFrom?: PageElement);
140
153
  getOriginalNode(): Node | undefined;
141
154
  appendChild(node: PageNode): void;
142
155
  clone(withChildren?: boolean): PageElement;
@@ -152,7 +165,7 @@ declare class PageManager {
152
165
  private readonly _transaction;
153
166
  private readonly _tempContainer;
154
167
  private readonly _config;
155
- constructor(tempContainer: Element, pageSize: PageSize, transaction: Transaction, config: PaginationConfig);
168
+ constructor(tempContainer: Element, pageSize: RealizedPageSize, transaction: Transaction, config: PaginationOptions);
156
169
  nextPage(): void;
157
170
  private cloneParentStackToNewPage;
158
171
  private cleanupEmptyParent;
@@ -172,102 +185,94 @@ declare class PageManager {
172
185
  getPageState(): PageState;
173
186
  }
174
187
 
188
+ /**
189
+ * Represents the margin sizes for a page.
190
+ * All values should be valid CSS size strings (e.g., '10mm', '1in').
191
+ *
192
+ * Common presets are available in {@link pageMargin}
193
+ */
175
194
  export declare interface PageMargin {
195
+ /** @public */
176
196
  top: string;
197
+ /** @public */
177
198
  right: string;
199
+ /** @public */
178
200
  bottom: string;
201
+ /** @public */
179
202
  left: string;
180
203
  }
181
204
 
182
- export declare const pageMargin: Record<keyof typeof _pageMargin, PageMargin>;
183
-
184
- declare const _pageMargin: {
185
- Normal: {
186
- top: string;
187
- right: string;
188
- bottom: string;
189
- left: string;
190
- };
191
- Narrow: {
192
- top: string;
193
- right: string;
194
- bottom: string;
195
- left: string;
196
- };
197
- Wide: {
198
- top: string;
199
- right: string;
200
- bottom: string;
201
- left: string;
202
- };
203
- None: {
204
- top: string;
205
- right: string;
206
- bottom: string;
207
- left: string;
208
- };
205
+ /**
206
+ * Predefined values for commonly used {@link PageMargin}
207
+ * @privateRemarks
208
+ * Type casting is only for cleaner TypeDoc output
209
+ */
210
+ export declare const pageMargin: {
211
+ /** Top, Right, Bottom, Left: 1in */
212
+ readonly Normal: PageMargin;
213
+ /** Top: 0.4in, Right, Bottom, Left: 0.6in */
214
+ readonly Narrow: PageMargin;
215
+ /** Top, Bottom: 0.5in, Right, Left: 2in */
216
+ readonly Wide: PageMargin;
217
+ /** Top, Right, Bottom, Left: 0 */
218
+ readonly None: PageMargin;
209
219
  };
210
220
 
211
221
  declare type PageNode = PageElement | PageText;
212
222
 
223
+ /**
224
+ * Describes the page orientation.
225
+ *
226
+ * - 'portrait' (default): the page is taller than it is wide. Use the provided
227
+ * width and height as-is.
228
+ * - 'landscape': the page is wider than it is tall. When applying landscape,
229
+ * swap the width and height.
230
+ */
213
231
  export declare type PageOrientation = 'portrait' | 'landscape';
214
232
 
233
+ /**
234
+ * Represents the dimensions of a page.
235
+ * All values should be valid CSS size strings (e.g., '210mm', '8.5in').
236
+ *
237
+ * Common presets are available in {@link pageSize}
238
+ */
215
239
  export declare interface PageSize {
216
- width: number;
217
- height: number;
240
+ /** @public */
241
+ height: string;
242
+ /** @public */
243
+ width: string;
218
244
  }
219
245
 
220
- export declare const pageSize: Record<keyof typeof _pageSize, PageDimension>;
221
-
222
- declare const _pageSize: {
223
- A1: {
224
- height: string;
225
- width: string;
226
- };
227
- A2: {
228
- height: string;
229
- width: string;
230
- };
231
- A3: {
232
- height: string;
233
- width: string;
234
- };
235
- A4: {
236
- height: string;
237
- width: string;
238
- };
239
- A5: {
240
- height: string;
241
- width: string;
242
- };
243
- A6: {
244
- height: string;
245
- width: string;
246
- };
247
- B3: {
248
- height: string;
249
- width: string;
250
- };
251
- B4: {
252
- height: string;
253
- width: string;
254
- };
255
- B5: {
256
- height: string;
257
- width: string;
258
- };
259
- Letter: {
260
- height: string;
261
- width: string;
262
- };
263
- Legal: {
264
- height: string;
265
- width: string;
266
- };
267
- Tabloid: {
268
- height: string;
269
- width: string;
270
- };
246
+ /**
247
+ * Predefined values for commonly used {@link PageSize}
248
+ * @privateRemarks
249
+ * Type casting is only for cleaner TypeDoc output
250
+ */
251
+ export declare const pageSize: {
252
+ /** 841mm x 594mm */
253
+ readonly A1: PageSize;
254
+ /** 594mm x 420mm */
255
+ readonly A2: PageSize;
256
+ /** 420mm x 297mm */
257
+ readonly A3: PageSize;
258
+ /** 297mm x 210mm */
259
+ readonly A4: PageSize;
260
+ /** 210mm x 148mm */
261
+ readonly A5: PageSize;
262
+ /** 148mm x 105mm */
263
+ readonly A6: PageSize;
264
+ /** 500mm x 353mm */
265
+ readonly B3: PageSize;
266
+ /** 353mm x 250mm */
267
+ readonly B4: PageSize;
268
+ /** 250mm x 176mm */
269
+ readonly B5: PageSize;
270
+ /** 8.5in x 11in */
271
+ readonly Letter: PageSize;
272
+ /** 11in x 8.5in */
273
+ readonly Legal: PageSize;
274
+ /** 11in x 17in */
275
+ readonly Tabloid: PageSize;
271
276
  };
272
277
 
273
278
  declare class PageState {
@@ -287,8 +292,8 @@ declare class PageText {
287
292
  private readonly _node;
288
293
  readonly type: 'text';
289
294
  readonly transaction: Transaction;
290
- config: PaginationConfig;
291
- constructor(text: Text, transaction: Transaction, config: PaginationConfig);
295
+ config: PaginationOptions;
296
+ constructor(text: Text, transaction: Transaction, config: PaginationOptions);
292
297
  get textContent(): string;
293
298
  set textContent(value: string);
294
299
  remove(): void;
@@ -297,15 +302,32 @@ declare class PageText {
297
302
 
298
303
  export declare type PaginateResult = string[];
299
304
 
300
- export declare type PaginationConfig = Required<ConfigAttribute> & {
301
- id: string;
302
- plugins: PaginationPlugin[];
303
- };
304
-
305
+ /**
306
+ * Context information for pagination cycle.
307
+ */
305
308
  export declare interface PaginationCycleCompleted {
309
+ /**
310
+ * All paginated section within the report.
311
+ */
306
312
  sections: SectionContext[];
307
313
  }
308
314
 
315
+ /**
316
+ * Pagination options
317
+ * @interface
318
+ * @inlineType LayoutOptions
319
+ */
320
+ export declare type PaginationOptions = Required<LayoutOptions> & {
321
+ /**
322
+ * Unique id of the pagination.
323
+ */
324
+ id: string;
325
+ /**
326
+ * List of plugins to use during pagination.
327
+ */
328
+ plugins: PaginationPlugin[];
329
+ };
330
+
309
331
  export declare interface PaginationPlugin {
310
332
  readonly name: string;
311
333
  readonly order: number;
@@ -328,7 +350,7 @@ export declare class Paginator {
328
350
  private readonly _config;
329
351
  private constructor();
330
352
  private static createTempContainer;
331
- static paginate(root: Element, pageSize: PageSize, config?: Partial<PaginationConfig>): PaginateResult;
353
+ static paginate(root: Element, pageSize: RealizedPageSize, config?: Partial<PaginationOptions>): PaginateResult;
332
354
  private processAllNodes;
333
355
  private handleNodeSkipped;
334
356
  private handleFullNodePlaced;
@@ -350,6 +372,15 @@ declare type PluginKeys = {
350
372
 
351
373
  export declare const previewClassName = "pz-preview";
352
374
 
375
+ export declare interface RealizedPageSize {
376
+ width: number;
377
+ height: number;
378
+ }
379
+
380
+ /**
381
+ * The report builder class that contains the logic for handling pagination
382
+ * and managing the report layout.
383
+ */
353
384
  export declare class ReportBuilder {
354
385
  private readonly _sections;
355
386
  private readonly _monitor;
@@ -357,20 +388,85 @@ export declare class ReportBuilder {
357
388
  private _pendingPaginateResolvers;
358
389
  private _currentAbortController;
359
390
  constructor();
391
+ /**
392
+ * Monitor instance used to subscribe to pagination events.
393
+ * See {@link ReportBuilderEvents} for available event types.
394
+ */
360
395
  get monitor(): Monitor<ReportBuilderEvents>;
396
+ /**
397
+ * Removes a section from the registered sections, if it has already been registered in the report.
398
+ */
361
399
  removeSection(sectionId: string): void;
362
- tryAddSection(options: SectionOptions, components: SectionComponents, onPaginationCompleted: (pages: PageContext[]) => void): boolean;
363
- schedulePaginate(): Promise<ScheduleResult>;
400
+ /**
401
+ * Registers a section by its ID, specifying the page size, margins, and other options.
402
+ *
403
+ * @param options - Configuration options for the section.
404
+ * @param components - The DOM components associated with the section.
405
+ * @param onPaginationCompleted - Callback invoked when pagination for the section is completed.
406
+ * @returns `true` if the section was added to the report’s section list, or `false` if it already exists.
407
+ */
408
+ tryAddSection(options: SectionOptions, components: SectionComponents, onPaginationCompleted: (pages: PageContext[]) => void): Promise<boolean>;
409
+ /**
410
+ * Schedules a pagination operation.
411
+ *
412
+ * It is not possible to schedule multiple pagination operations in parallel,
413
+ * as the process involves DOM manipulation, and concurrent modifications
414
+ * could cause conflicts and unexpected results.
415
+ * Each newly scheduled operation is queued and executed sequentially.
416
+ * When a new pagination is scheduled, any ongoing or pending operations
417
+ * will be aborted, and the new pagination will start immediately afterward.
418
+ *
419
+ * @returns A promise that resolves when the first pagination cycle is completed.
420
+ * It does not wait for suspended sections to resolve and be paginated.
421
+ * To wait for all sections to complete pagination, use the
422
+ * `suspension` property of the returned result object.
423
+ */
424
+ schedulePagination(): Promise<ScheduleResult>;
425
+ /**
426
+ * Retrieves JSON data injected by **@paprize/puppeteer** during server-side rendering (SSR).
427
+ *
428
+ * If no injected data is available, the function returns the provided `defaultData`, or `null` if none is given.
429
+ *
430
+ * ⚠️ **Important Notes:**
431
+ * - This function is **not type-safe** — it performs **no runtime type validation** on the returned data.
432
+ * - It is available **only during server-side rendering** when using **@paprize/puppeteer**.
433
+ * - When used in **client-side rendering** or **development** mode, you should provide a `defaultData` value for testing purposes.
434
+ *
435
+ * @template T - The expected type of the injected JSON data.
436
+ * @param defaultData - Optional fallback value to return if no injected data is found.
437
+ * @returns A promise resolving to the injected JSON data if available, otherwise the provided default value or `null`.
438
+ */
439
+ getJsonData<T>(defaultData?: T): Promise<T | null>;
440
+ private _lazyJsonDataReader;
364
441
  private _executePagination;
365
442
  private _processPendingPagination;
366
443
  private _injectStyle;
367
444
  private _paginateSection;
368
445
  }
369
446
 
447
+ /**
448
+ * Available events that can be subscribed to, during the pagination process.
449
+ */
370
450
  export declare interface ReportBuilderEvents {
451
+ /**
452
+ * Triggered when a page has been fully paginated.
453
+ * event: {@link PageContext}
454
+ */
371
455
  pageCompleted: (event: PageContext) => void;
456
+ /**
457
+ * Triggered when a section has been fully paginated.
458
+ * event: {@link SectionContext}
459
+ */
372
460
  sectionCompleted: (event: SectionContext) => void;
461
+ /**
462
+ * Triggered when a new section is created.
463
+ * event: {@link SectionContext}
464
+ */
373
465
  sectionCreated: (event: SectionContext) => void;
466
+ /**
467
+ * Triggered when an entire pagination cycle is completed.
468
+ * event: {@link PaginationCycleCompleted}
469
+ */
374
470
  paginationCycleCompleted: (event: PaginationCycleCompleted) => void;
375
471
  }
376
472
 
@@ -378,7 +474,7 @@ export declare const reportStyles: {
378
474
  globalStyle: string;
379
475
  component: CSSProperties;
380
476
  outOfScreen: CSSProperties;
381
- page: (pageDimension: PageDimension, pageMargin: PageMargin) => CSSProperties;
477
+ page: (pageSize: PageSize, pageMargin: PageMargin) => CSSProperties;
382
478
  overlay: CSSProperties;
383
479
  pageContent: CSSProperties;
384
480
  sectionPageMedia: typeof sectionPageMedia;
@@ -389,37 +485,113 @@ declare type SafeElement = Omit<Element, 'removeChild' | 'appendChild' | 'replac
389
485
 
390
486
  declare type SafeText = Omit<Text, 'remove'>;
391
487
 
488
+ /**
489
+ * Represents the result of a scheduled pagination process.
490
+ */
392
491
  export declare interface ScheduleResult {
492
+ /**
493
+ * List of all registered sections.
494
+ */
393
495
  sections: SectionContext[];
496
+ /**
497
+ * If there are any suspended sections, this Promise tracks their state
498
+ * and resolves only after all suspended sections have been resumed
499
+ * and paginated.
500
+ */
394
501
  suspension: Promise<void>;
395
502
  }
396
503
 
397
504
  export declare const sectionClassName = "pz-section";
398
505
 
506
+ /**
507
+ * Represents the collection of DOM elements generated by
508
+ * the pagination engine from the report components on the current page.
509
+ */
399
510
  export declare interface SectionComponents {
511
+ /**
512
+ * The HTML element that serves as the header for the entire section.
513
+ * `null` if the section has no section header.
514
+ */
400
515
  sectionHeader: HTMLElement | null;
516
+ /**
517
+ * The HTML element that serves as the footer for the entire section.
518
+ * `null` if the section has no section footer.
519
+ */
401
520
  sectionFooter: HTMLElement | null;
521
+ /**
522
+ * The HTML element that serves as the header for this page.
523
+ * `null` if the page has no header.
524
+ */
402
525
  pageHeader: HTMLElement | null;
526
+ /**
527
+ * The HTML element that serves as the footer for this page.
528
+ * `null` if the page has no footer.
529
+ */
403
530
  pageFooter: HTMLElement | null;
531
+ /**
532
+ * The main HTML element for the this page content
533
+ * This property is always defined.
534
+ */
404
535
  pageContent: HTMLElement;
405
536
  }
406
537
 
538
+ /**
539
+ * Context information for a paginated section.
540
+ */
407
541
  export declare interface SectionContext {
408
- index: number;
542
+ /**
543
+ * Index of the section within the report.
544
+ */
545
+ sectionIndex: number;
546
+ /**
547
+ * Unique identifier of the section.
548
+ */
409
549
  sectionId: string;
550
+ /**
551
+ * Indicates whether pagination for this section is suspended
552
+ * and waiting for the suspension to be resolved.
553
+ */
410
554
  isSuspended: boolean;
555
+ /**
556
+ * Indicates whether pagination for this section has completed.
557
+ */
411
558
  isPaginated: boolean;
559
+ /**
560
+ * All paginated pages that belong to this section.
561
+ */
412
562
  pages: PageContext[];
413
563
  }
414
564
 
415
- export declare interface SectionOptions extends Partial<PaginationConfig> {
565
+ /**
566
+ * Configuration options for a section.
567
+ * @inlineType PaginationConfig
568
+ */
569
+ export declare interface SectionOptions extends Partial<Omit<PaginationOptions, 'id'>> {
570
+ /**
571
+ * Unique id of the section within the report.
572
+ */
416
573
  readonly id: string;
417
- readonly dimension: PageDimension;
574
+ /**
575
+ * Page size used for this section.
576
+ */
577
+ readonly size: PageSize;
578
+ /**
579
+ * Page orientation used for this section.
580
+ * @inlineType PageOrientation
581
+ * @default portrait
582
+ */
583
+ readonly orientation?: PageOrientation;
584
+ /**
585
+ * Page margins for this section.
586
+ */
418
587
  readonly margin?: PageMargin;
588
+ /**
589
+ * A list of promises that must be resolved before the section can be paginated.
590
+ */
419
591
  readonly suspense?: Promise<unknown>[];
420
592
  }
421
593
 
422
- declare function sectionPageMedia(sectionId: string, dimension: PageDimension): string;
594
+ declare function sectionPageMedia(sectionId: string, size: PageSize): string;
423
595
 
424
596
  export declare interface SectionState {
425
597
  options: SectionOptions;
@@ -467,6 +639,9 @@ export declare class TablePlugin implements PaginationPlugin {
467
639
  private _isTableBodyEmpty;
468
640
  }
469
641
 
642
+ /**
643
+ * Table plugin options
644
+ */
470
645
  export declare interface TablePluginOptions {
471
646
  /**
472
647
  * If true, the table header (thead) will be cloned on each page.