@paprize/core 0.0.5 → 0.0.6

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;
@@ -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,69 @@ 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;
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
+ */
362
408
  tryAddSection(options: SectionOptions, components: SectionComponents, onPaginationCompleted: (pages: PageContext[]) => void): boolean;
363
- schedulePaginate(): Promise<ScheduleResult>;
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>;
364
425
  private _executePagination;
365
426
  private _processPendingPagination;
366
427
  private _injectStyle;
367
428
  private _paginateSection;
368
429
  }
369
430
 
431
+ /**
432
+ * Available events that can be subscribed to, during the pagination process.
433
+ */
370
434
  export declare interface ReportBuilderEvents {
435
+ /**
436
+ * Triggered when a page has been fully paginated.
437
+ * event: {@link PageContext}
438
+ */
371
439
  pageCompleted: (event: PageContext) => void;
440
+ /**
441
+ * Triggered when a section has been fully paginated.
442
+ * event: {@link SectionContext}
443
+ */
372
444
  sectionCompleted: (event: SectionContext) => void;
445
+ /**
446
+ * Triggered when a new section is created.
447
+ * event: {@link SectionContext}
448
+ */
373
449
  sectionCreated: (event: SectionContext) => void;
450
+ /**
451
+ * Triggered when an entire pagination cycle is completed.
452
+ * event: {@link PaginationCycleCompleted}
453
+ */
374
454
  paginationCycleCompleted: (event: PaginationCycleCompleted) => void;
375
455
  }
376
456
 
@@ -378,7 +458,7 @@ export declare const reportStyles: {
378
458
  globalStyle: string;
379
459
  component: CSSProperties;
380
460
  outOfScreen: CSSProperties;
381
- page: (pageDimension: PageDimension, pageMargin: PageMargin) => CSSProperties;
461
+ page: (pageSize: PageSize, pageMargin: PageMargin) => CSSProperties;
382
462
  overlay: CSSProperties;
383
463
  pageContent: CSSProperties;
384
464
  sectionPageMedia: typeof sectionPageMedia;
@@ -389,37 +469,113 @@ declare type SafeElement = Omit<Element, 'removeChild' | 'appendChild' | 'replac
389
469
 
390
470
  declare type SafeText = Omit<Text, 'remove'>;
391
471
 
472
+ /**
473
+ * Represents the result of a scheduled pagination process.
474
+ */
392
475
  export declare interface ScheduleResult {
476
+ /**
477
+ * List of all registered sections.
478
+ */
393
479
  sections: SectionContext[];
480
+ /**
481
+ * If there are any suspended sections, this Promise tracks their state
482
+ * and resolves only after all suspended sections have been resumed
483
+ * and paginated.
484
+ */
394
485
  suspension: Promise<void>;
395
486
  }
396
487
 
397
488
  export declare const sectionClassName = "pz-section";
398
489
 
490
+ /**
491
+ * Represents the collection of DOM elements generated by
492
+ * the pagination engine from the report components on the current page.
493
+ */
399
494
  export declare interface SectionComponents {
495
+ /**
496
+ * The HTML element that serves as the header for the entire section.
497
+ * `null` if the section has no section header.
498
+ */
400
499
  sectionHeader: HTMLElement | null;
500
+ /**
501
+ * The HTML element that serves as the footer for the entire section.
502
+ * `null` if the section has no section footer.
503
+ */
401
504
  sectionFooter: HTMLElement | null;
505
+ /**
506
+ * The HTML element that serves as the header for this page.
507
+ * `null` if the page has no header.
508
+ */
402
509
  pageHeader: HTMLElement | null;
510
+ /**
511
+ * The HTML element that serves as the footer for this page.
512
+ * `null` if the page has no footer.
513
+ */
403
514
  pageFooter: HTMLElement | null;
515
+ /**
516
+ * The main HTML element for the this page content
517
+ * This property is always defined.
518
+ */
404
519
  pageContent: HTMLElement;
405
520
  }
406
521
 
522
+ /**
523
+ * Context information for a paginated section.
524
+ */
407
525
  export declare interface SectionContext {
408
- index: number;
526
+ /**
527
+ * Index of the section within the report.
528
+ */
529
+ sectionIndex: number;
530
+ /**
531
+ * Unique identifier of the section.
532
+ */
409
533
  sectionId: string;
534
+ /**
535
+ * Indicates whether pagination for this section is suspended
536
+ * and waiting for the suspension to be resolved.
537
+ */
410
538
  isSuspended: boolean;
539
+ /**
540
+ * Indicates whether pagination for this section has completed.
541
+ */
411
542
  isPaginated: boolean;
543
+ /**
544
+ * All paginated pages that belong to this section.
545
+ */
412
546
  pages: PageContext[];
413
547
  }
414
548
 
415
- export declare interface SectionOptions extends Partial<PaginationConfig> {
549
+ /**
550
+ * Configuration options for a section.
551
+ * @inlineType PaginationConfig
552
+ */
553
+ export declare interface SectionOptions extends Partial<Omit<PaginationOptions, 'id'>> {
554
+ /**
555
+ * Unique id of the section within the report.
556
+ */
416
557
  readonly id: string;
417
- readonly dimension: PageDimension;
558
+ /**
559
+ * Page size used for this section.
560
+ */
561
+ readonly size: PageSize;
562
+ /**
563
+ * Page orientation used for this section.
564
+ * @inlineType PageOrientation
565
+ * @default portrait
566
+ */
567
+ readonly orientation?: PageOrientation;
568
+ /**
569
+ * Page margins for this section.
570
+ */
418
571
  readonly margin?: PageMargin;
572
+ /**
573
+ * A list of promises that must be resolved before the section can be paginated.
574
+ */
419
575
  readonly suspense?: Promise<unknown>[];
420
576
  }
421
577
 
422
- declare function sectionPageMedia(sectionId: string, dimension: PageDimension): string;
578
+ declare function sectionPageMedia(sectionId: string, size: PageSize): string;
423
579
 
424
580
  export declare interface SectionState {
425
581
  options: SectionOptions;
@@ -467,6 +623,9 @@ export declare class TablePlugin implements PaginationPlugin {
467
623
  private _isTableBodyEmpty;
468
624
  }
469
625
 
626
+ /**
627
+ * Table plugin options
628
+ */
470
629
  export declare interface TablePluginOptions {
471
630
  /**
472
631
  * If true, the table header (thead) will be cloned on each page.