Skip to content

Commit

Permalink
fix(abc:pdf): fix container must be absolutely positioned error (#1448
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cipchk authored Feb 23, 2022
1 parent 475e5cb commit c2ed8a7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
27 changes: 23 additions & 4 deletions packages/abc/pdf/pdf.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SimpleChange,
ViewEncapsulation
} from '@angular/core';
import { fromEvent, Subject } from 'rxjs';
import { fromEvent, Subject, timer } from 'rxjs';
import { debounceTime, filter, takeUntil } from 'rxjs/operators';

import { AlainConfigService } from '@delon/util/config';
Expand Down Expand Up @@ -191,11 +191,20 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
}

private initDelay(): void {
if (!this.win.pdfjsLib) {
throw new Error(
`No window.pdfjsLib found, please make sure that cdn or local path exists, the current referenced path is: ${JSON.stringify(
this.lib
)}`
);
}
this.inited = true;
this.cdr.detectChanges();
this.win.pdfjsLib.GlobalWorkerOptions.workerSrc = `${this.lib}build/pdf.worker.min.js`;

setTimeout(() => this.load(), this.delay);
timer(this.delay ?? 0)
.pipe(takeUntil(this.destroy$))
.subscribe(() => this.load());
}

setLoading(status: boolean): void {
Expand Down Expand Up @@ -277,23 +286,33 @@ export class PdfComponent implements OnChanges, AfterViewInit, OnDestroy {
}

if (this._rotation !== 0 || currentViewer.pagesRotation !== this._rotation) {
setTimeout(() => {
this.timeExec(() => {
currentViewer.pagesRotation = this._rotation;
});
}

if (this.stickToPage) {
setTimeout(() => {
this.timeExec(() => {
currentViewer.currentPageNumber = this._pi;
});
}

this.updateSize();
}

private timeExec(fn: () => void): void {
this.ngZone.runOutsideAngular(() => {
timer(0)
.pipe(takeUntil(this.destroy$))
.subscribe(() => this.ngZone.runOutsideAngular(() => fn()));
});
}

@ZoneOutside()
private updateSize(): void {
const currentViewer = this.pageViewer;
if (!currentViewer) return;

this._pdf.getPage(currentViewer.currentPageNumber).then((page: NzSafeAny) => {
const { _rotation, _zoom } = this;
const rotation = _rotation || page.rotate;
Expand Down
55 changes: 55 additions & 0 deletions packages/abc/pdf/pdf.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Component, ViewChild } from '@angular/core';
import { ComponentFixture, discardPeriodicTasks, fakeAsync, flush, TestBed, tick } from '@angular/core/testing';

import { createTestContext } from '@delon/testing';
import { LazyService } from '@delon/util/other';
import { NzSafeAny } from 'ng-zorro-antd/core/types/any';

import { PdfComponent } from './pdf.component';
import { PdfModule } from './pdf.module';

describe('abc: pdf', () => {
let fixture: ComponentFixture<TestComponent>;
let page: PageObject;
let lazySrv: LazyService;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [PdfModule],
declarations: [TestComponent]
});
({ fixture } = createTestContext(TestComponent));
page = new PageObject();
lazySrv = TestBed.inject(LazyService);
spyOn(lazySrv, 'load').and.returnValue(Promise.resolve([]));
});

it('should be throw error when not found pdfjsViewer in window', fakeAsync(() => {
expect(() => page.cd().end()).toThrow();
}));

class PageObject {
cd(time: number = 0): this {
fixture.detectChanges();
tick(time);
fixture.detectChanges();
return this;
}

end(): void {
discardPeriodicTasks();
flush();
}
}
});

@Component({
template: ` <pdf #comp [src]="src" [options]="options" [delay]="delay" (change)="change()"></pdf> `
})
class TestComponent {
@ViewChild('comp') comp!: PdfComponent;
options: NzSafeAny;
src = '';
delay = 0;
change(): void {}
}
4 changes: 2 additions & 2 deletions packages/abc/pdf/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

@{pdf-prefix} {
&-container {
position: relative;
display: block;
position: absolute;
width: 100%;
height: 100%;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
Expand Down

0 comments on commit c2ed8a7

Please sign in to comment.