@@ -19,6 +19,12 @@ import {
19
19
} from '@angular/core' ;
20
20
import { fromEvent , Subject , timer , debounceTime , filter , takeUntil } from 'rxjs' ;
21
21
22
+ import type { PDFDocumentLoadingTask , PDFDocumentProxy } from 'pdfjs-dist' ;
23
+ import type { EventBus } from 'pdfjs-dist/types/web/interfaces' ;
24
+ import type { PDFFindController } from 'pdfjs-dist/types/web/pdf_find_controller' ;
25
+ import type { PDFLinkService } from 'pdfjs-dist/types/web/pdf_link_service' ;
26
+ import type { PDFViewer } from 'pdfjs-dist/types/web/pdf_viewer' ;
27
+
22
28
import { AlainConfigService } from '@delon/util/config' ;
23
29
import { BooleanInput , InputBoolean , InputNumber , NumberInput , ZoneOutside } from '@delon/util/decorator' ;
24
30
import { LazyService } from '@delon/util/other' ;
@@ -59,10 +65,10 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
59
65
inited = false ;
60
66
private destroy$ = new Subject < void > ( ) ;
61
67
private lib : string = '' ;
62
- private _pdf : NzSafeAny ;
63
- private loadingTask : NzSafeAny ;
68
+ private _pdf ?: PDFDocumentProxy | null ;
69
+ private loadingTask ?: PDFDocumentLoadingTask ;
64
70
private _src : NzSafeAny ;
65
- private lastSrc ?: string ;
71
+ private lastSrc ?: NzSafeAny ;
66
72
private _pi = 1 ;
67
73
private _total = 0 ;
68
74
private _showAll = true ;
@@ -71,12 +77,13 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
71
77
private _renderText = true ;
72
78
private _loading = false ;
73
79
74
- private multiPageViewer : NzSafeAny ;
75
- private multiPageLinkService : NzSafeAny ;
76
- private multiPageFindController : NzSafeAny ;
77
- private singlePageViewer : NzSafeAny ;
78
- private singlePageLinkService : NzSafeAny ;
79
- private singlePageFindController : NzSafeAny ;
80
+ private multiPageViewer ?: PDFViewer ;
81
+ private multiPageLinkService ?: PDFLinkService ;
82
+ private multiPageFindController ?: PDFFindController ;
83
+ private singlePageViewer ?: PDFViewer ;
84
+ private singlePageLinkService ?: PDFLinkService ;
85
+ private singlePageFindController ?: PDFFindController ;
86
+ private _eventBus ?: EventBus ;
80
87
81
88
@Input ( ) set src ( dataOrBuffer : NzSafeAny ) {
82
89
this . _src = dataOrBuffer ;
@@ -86,7 +93,7 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
86
93
@InputNumber ( )
87
94
set pi ( val : number ) {
88
95
this . _pi = this . getValidPi ( val ) ;
89
- if ( this . _pdf ) {
96
+ if ( this . pageViewer ) {
90
97
this . pageViewer . scrollPageIntoView ( { pageNumber : this . _pi } ) ;
91
98
}
92
99
}
@@ -96,12 +103,12 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
96
103
}
97
104
@Input ( ) @InputBoolean ( ) set renderText ( val : boolean ) {
98
105
this . _renderText = val ;
99
- if ( this . _pdf ) {
106
+ if ( this . pageViewer ) {
100
107
this . pageViewer . textLayerMode = this . _textLayerMode ;
101
108
this . resetDoc ( ) ;
102
109
}
103
110
}
104
- @Input ( ) textLayerMode : PdfTextLayerMode = PdfTextLayerMode . ENABLE ;
111
+ @Input ( ) textLayerMode = PdfTextLayerMode . ENABLE ;
105
112
@Input ( ) @InputBoolean ( ) showBorders = false ;
106
113
@Input ( ) @InputBoolean ( ) stickToPage = false ;
107
114
@Input ( ) @InputBoolean ( ) originalSize = true ;
@@ -119,30 +126,34 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
119
126
this . _rotation = val ;
120
127
}
121
128
@Input ( ) @InputBoolean ( ) autoReSize = true ;
122
- @Input ( ) externalLinkTarget : PdfExternalLinkTarget = PdfExternalLinkTarget . BLANK ;
129
+ @Input ( ) externalLinkTarget = PdfExternalLinkTarget . BLANK ;
123
130
@Input ( ) @InputNumber ( ) delay ?: number ;
124
131
@Output ( ) readonly change = new EventEmitter < PdfChangeEvent > ( ) ;
125
132
126
133
get loading ( ) : boolean {
127
134
return this . _loading ;
128
135
}
129
136
130
- get pdf ( ) : NzSafeAny {
137
+ get pdf ( ) : PDFDocumentProxy | undefined | null {
131
138
return this . _pdf ;
132
139
}
133
140
134
- get findController ( ) : NzSafeAny {
141
+ get findController ( ) : PDFFindController | undefined {
135
142
return this . _showAll ? this . multiPageFindController : this . singlePageFindController ;
136
143
}
137
144
138
- get pageViewer ( ) : NzSafeAny {
145
+ get pageViewer ( ) : PDFViewer | undefined {
139
146
return this . _showAll ? this . multiPageViewer : this . singlePageViewer ;
140
147
}
141
148
142
- get linkService ( ) : NzSafeAny {
149
+ get linkService ( ) : PDFLinkService | undefined {
143
150
return this . _showAll ? this . multiPageLinkService : this . singlePageLinkService ;
144
151
}
145
152
153
+ get eventBus ( ) : EventBus | undefined {
154
+ return this . _eventBus ;
155
+ }
156
+
146
157
private get _textLayerMode ( ) : PdfTextLayerMode {
147
158
return this . _renderText ? this . textLayerMode : PdfTextLayerMode . DISABLE ;
148
159
}
@@ -231,11 +242,11 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
231
242
this . cdr . detectChanges ( ) ;
232
243
} ) ;
233
244
this . setLoading ( true ) ;
234
- const loadingTask = ( this . loadingTask = this . win . pdfjsLib . getDocument ( _src ) ) ;
245
+ const loadingTask : PDFDocumentLoadingTask = ( this . loadingTask = this . win . pdfjsLib . getDocument ( _src ) ) ;
235
246
loadingTask . onProgress = ( progress : { loaded : number ; total : number } ) => this . emit ( 'load-progress' , { progress } ) ;
236
- ( loadingTask . promise as PromiseLike < void > )
247
+ ( loadingTask . promise as PromiseLike < PDFDocumentProxy > )
237
248
. then (
238
- ( pdf : NzSafeAny ) => {
249
+ pdf => {
239
250
this . _pdf = pdf ;
240
251
this . lastSrc = _src ;
241
252
this . _total = pdf . numPages ;
@@ -249,7 +260,7 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
249
260
this . resetDoc ( ) ;
250
261
this . render ( ) ;
251
262
} ,
252
- ( error : NzSafeAny ) => this . emit ( 'error' , { error } )
263
+ error => this . emit ( 'error' , { error } )
253
264
)
254
265
. then ( ( ) => this . setLoading ( false ) ) ;
255
266
}
@@ -262,20 +273,20 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
262
273
}
263
274
this . cleanDoc ( ) ;
264
275
265
- this . findController . setDocument ( pdf ) ;
266
- this . pageViewer . setDocument ( pdf ) ;
267
- this . linkService . setDocument ( pdf , null ) ;
276
+ this . findController ! . setDocument ( pdf ) ;
277
+ this . pageViewer ! . setDocument ( pdf ) ;
278
+ this . linkService ! . setDocument ( pdf , null ) ;
268
279
}
269
280
270
281
private cleanDoc ( ) : void {
271
- this . multiPageViewer . setDocument ( null ) ;
272
- this . singlePageViewer . setDocument ( null ) ;
282
+ this . multiPageViewer ! . setDocument ( null as NzSafeAny ) ;
283
+ this . singlePageViewer ! . setDocument ( null as NzSafeAny ) ;
273
284
274
- this . multiPageLinkService . setDocument ( null , null ) ;
275
- this . singlePageLinkService . setDocument ( null , null ) ;
285
+ this . multiPageLinkService ! . setDocument ( null , null ) ;
286
+ this . singlePageLinkService ! . setDocument ( null , null ) ;
276
287
277
- this . multiPageFindController . setDocument ( null ) ;
278
- this . singlePageFindController . setDocument ( null ) ;
288
+ this . multiPageFindController ! . setDocument ( null as NzSafeAny ) ;
289
+ this . singlePageFindController ! . setDocument ( null as NzSafeAny ) ;
279
290
}
280
291
281
292
private render ( ) : void {
@@ -312,7 +323,7 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
312
323
const currentViewer = this . pageViewer ;
313
324
if ( ! currentViewer ) return ;
314
325
315
- this . _pdf . getPage ( currentViewer . currentPageNumber ) . then ( ( page : NzSafeAny ) => {
326
+ this . _pdf ! . getPage ( currentViewer . currentPageNumber ) . then ( page => {
316
327
const { _rotation, _zoom } = this ;
317
328
const rotation = _rotation || page . rotate ;
318
329
const viewportWidth =
@@ -382,8 +393,8 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
382
393
this . setupSinglePageViewer ( ) ;
383
394
}
384
395
385
- private createEventBus ( ) : NzSafeAny {
386
- const eventBus = new this . win . pdfjsViewer . EventBus ( ) ;
396
+ private createEventBus ( ) : EventBus {
397
+ const eventBus : EventBus = new this . win . pdfjsViewer . EventBus ( ) ;
387
398
eventBus . on ( `pagesinit` , ( ev : NzSafeAny ) => {
388
399
this . emit ( 'pages-init' , { ev } ) ;
389
400
} ) ;
@@ -406,16 +417,16 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
406
417
private setupMultiPageViewer ( ) : void {
407
418
const VIEWER = this . win . pdfjsViewer ;
408
419
409
- const eventBus = this . createEventBus ( ) ;
410
- const linkService = ( this . multiPageLinkService = new VIEWER . PDFLinkService ( {
420
+ const eventBus = ( this . _eventBus = this . createEventBus ( ) ) ;
421
+ const linkService : PDFLinkService = ( this . multiPageLinkService = new VIEWER . PDFLinkService ( {
411
422
eventBus
412
423
} ) ) ;
413
- const findController = ( this . multiPageFindController = new VIEWER . PDFFindController ( {
424
+ const findController : PDFFindController = ( this . multiPageFindController = new VIEWER . PDFFindController ( {
414
425
eventBus,
415
426
linkService
416
427
} ) ) ;
417
428
418
- const viewer = ( this . multiPageViewer = new VIEWER . PDFViewer ( {
429
+ const viewer : PDFViewer = ( this . multiPageViewer = new VIEWER . PDFViewer ( {
419
430
eventBus,
420
431
container : this . el ,
421
432
removePageBorders : ! this . showBorders ,
@@ -430,10 +441,10 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
430
441
const VIEWER = this . win . pdfjsViewer ;
431
442
432
443
const eventBus = this . createEventBus ( ) ;
433
- const linkService = ( this . singlePageLinkService = new VIEWER . PDFLinkService ( {
444
+ const linkService : PDFLinkService = ( this . singlePageLinkService = new VIEWER . PDFLinkService ( {
434
445
eventBus
435
446
} ) ) ;
436
- const findController = ( this . singlePageFindController = new VIEWER . PDFFindController ( {
447
+ const findController : PDFFindController = ( this . singlePageFindController = new VIEWER . PDFFindController ( {
437
448
eventBus,
438
449
linkService
439
450
} ) ) ;
@@ -471,7 +482,7 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
471
482
fromEvent ( this . win , 'resize' )
472
483
. pipe (
473
484
debounceTime ( 100 ) ,
474
- filter ( ( ) => this . autoReSize && this . _pdf ) ,
485
+ filter ( ( ) => this . autoReSize && this . _pdf != null ) ,
475
486
takeUntil ( this . destroy$ )
476
487
)
477
488
. subscribe ( ( ) => this . updateSize ( ) ) ;
0 commit comments