mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-2438] fixed thumbnails height (#3407)
* [ADF-2438] calculate thumbnail height * [ADF-2438] send height to parent element * [ADF-2438] add width to css * [ADF-2438] moved height and width logic to parent component * [ADF-2438] added height and width logic to parent component * [ADF-2438] fixed failing test
This commit is contained in:
committed by
Eugenio Romano
parent
2f51b9f2b8
commit
1266acc4ab
@@ -1,4 +1,4 @@
|
|||||||
<ng-container *ngIf="image$ | async as image">
|
<ng-container *ngIf="image$ | async as image">
|
||||||
<img [src]="image"
|
<img [src]="image"
|
||||||
title="{{ 'ADF_VIEWER.SIDEBAR.THUMBNAILS.PAGE' | translate: { pageNum: page.id } }}">
|
title="{{ 'ADF_VIEWER.SIDEBAR.THUMBNAILS.PAGE' | translate: { pageNum: page.id } }}">
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
@@ -28,12 +28,17 @@ describe('PdfThumbComponent', () => {
|
|||||||
const domSanitizer = {
|
const domSanitizer = {
|
||||||
bypassSecurityTrustUrl: () => 'image-data'
|
bypassSecurityTrustUrl: () => 'image-data'
|
||||||
};
|
};
|
||||||
|
const width = 91;
|
||||||
|
const height = 119;
|
||||||
const page = {
|
const page = {
|
||||||
id: 'pageId',
|
id: 'pageId',
|
||||||
getPage: jasmine.createSpy('getPage').and.returnValue(Promise.resolve({
|
getPage: jasmine.createSpy('getPage').and.returnValue(Promise.resolve({
|
||||||
getViewport: () => ({ height: 0, width: 0 }),
|
getViewport: () => ({ height: width, width: height }),
|
||||||
render: jasmine.createSpy('render').and.returnValue(Promise.resolve())
|
render: jasmine.createSpy('render').and.returnValue(Promise.resolve())
|
||||||
}))
|
})),
|
||||||
|
|
||||||
|
getWidth: jasmine.createSpy('getWidth').and.returnValue(width),
|
||||||
|
getHeight: jasmine.createSpy('getHeight').and.returnValue(height)
|
||||||
};
|
};
|
||||||
|
|
||||||
setupTestBed({
|
setupTestBed({
|
||||||
|
@@ -15,7 +15,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component, Input, OnInit, ElementRef, ViewEncapsulation } from '@angular/core';
|
import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core';
|
||||||
import { DomSanitizer } from '@angular/platform-browser';
|
import { DomSanitizer } from '@angular/platform-browser';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -30,16 +30,17 @@ export class PdfThumbComponent implements OnInit {
|
|||||||
|
|
||||||
image$: Promise<string>;
|
image$: Promise<string>;
|
||||||
|
|
||||||
constructor(
|
constructor(private sanitizer: DomSanitizer) {}
|
||||||
private element: ElementRef, private sanitizer: DomSanitizer) {}
|
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.image$ = this.page.getPage().then((page) => this.getThumb(page));
|
this.image$ = this.page.getPage().then((page) => this.getThumb(page));
|
||||||
}
|
}
|
||||||
|
|
||||||
private getThumb(page): Promise<string> {
|
private getThumb(page): Promise<string> {
|
||||||
const canvas = this.getCanvas();
|
|
||||||
const viewport = page.getViewport(1);
|
const viewport = page.getViewport(1);
|
||||||
|
|
||||||
|
const pageRatio = viewport.width / viewport.height;
|
||||||
|
const canvas = this.getCanvas(pageRatio);
|
||||||
const scale = Math.min((canvas.height / viewport.height), (canvas.width / viewport.width));
|
const scale = Math.min((canvas.height / viewport.height), (canvas.width / viewport.width));
|
||||||
|
|
||||||
return page.render({
|
return page.render({
|
||||||
@@ -52,13 +53,10 @@ export class PdfThumbComponent implements OnInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private getCanvas(): HTMLCanvasElement {
|
private getCanvas(pageRatio): HTMLCanvasElement {
|
||||||
const elementRect = this.element.nativeElement.getBoundingClientRect();
|
|
||||||
const canvas = document.createElement('canvas');
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = this.page.getWidth();
|
||||||
canvas.width = elementRect.width;
|
canvas.height = this.page.getHeight();
|
||||||
canvas.height = elementRect.height;
|
|
||||||
|
|
||||||
return canvas;
|
return canvas;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,7 +18,6 @@
|
|||||||
&__thumb {
|
&__thumb {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
display: block;
|
display: block;
|
||||||
height: 114px;
|
|
||||||
width: 91px;
|
width: 91px;
|
||||||
background: mat-color($background, background);
|
background: mat-color($background, background);
|
||||||
margin-bottom: 15px;
|
margin-bottom: 15px;
|
||||||
@@ -32,4 +31,4 @@
|
|||||||
box-shadow: 0px 0px 5px 0px $black-87-opacity;
|
box-shadow: 0px 0px 5px 0px $black-87-opacity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -32,11 +32,7 @@ describe('PdfThumbListComponent', () => {
|
|||||||
const page = (id) => {
|
const page = (id) => {
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
getPage: () => Promise.resolve({
|
getPage: Promise.resolve()
|
||||||
getViewport: () => {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -51,12 +47,8 @@ describe('PdfThumbListComponent', () => {
|
|||||||
},
|
},
|
||||||
pdfDocument: {
|
pdfDocument: {
|
||||||
getPage: (pageNum) => Promise.resolve({
|
getPage: (pageNum) => Promise.resolve({
|
||||||
getViewport: () => {
|
getViewport: () => ({ height: 421, width: 335 }),
|
||||||
return 1;
|
render: jasmine.createSpy('render').and.returnValue(Promise.resolve())
|
||||||
},
|
|
||||||
render: () => {
|
|
||||||
return Promise.resolve({});
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
_pages: [
|
_pages: [
|
||||||
@@ -78,7 +70,6 @@ describe('PdfThumbListComponent', () => {
|
|||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
fixture = TestBed.createComponent(PdfThumbListComponent);
|
fixture = TestBed.createComponent(PdfThumbListComponent);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
|
|
||||||
component.pdfViewer = viewerMock;
|
component.pdfViewer = viewerMock;
|
||||||
|
|
||||||
// provide scrollable container
|
// provide scrollable container
|
||||||
@@ -100,6 +91,7 @@ describe('PdfThumbListComponent', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should render next range on scroll', () => {
|
it('should render next range on scroll', () => {
|
||||||
|
component.currentHeight = 114;
|
||||||
fixture.nativeElement.scrollTop = 700;
|
fixture.nativeElement.scrollTop = 700;
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
@@ -33,6 +33,8 @@ export class PdfThumbListComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
virtualHeight: number = 0;
|
virtualHeight: number = 0;
|
||||||
translateY: number = 0;
|
translateY: number = 0;
|
||||||
renderItems = [];
|
renderItems = [];
|
||||||
|
width: number = 91;
|
||||||
|
currentHeight: number = 0;
|
||||||
|
|
||||||
private items = [];
|
private items = [];
|
||||||
private margin: number = 15;
|
private margin: number = 15;
|
||||||
@@ -52,11 +54,13 @@ export class PdfThumbListComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.element.nativeElement.addEventListener('scroll', this.calculateItems, true);
|
|
||||||
this.pdfViewer.eventBus.on('pagechange', this.onPageChange);
|
this.pdfViewer.eventBus.on('pagechange', this.onPageChange);
|
||||||
|
this.element.nativeElement.addEventListener('scroll', this.calculateItems, true);
|
||||||
|
|
||||||
|
this.setHeight(this.pdfViewer.currentPageNumber);
|
||||||
this.items = this.getPages();
|
this.items = this.getPages();
|
||||||
this.calculateItems();
|
this.calculateItems();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngAfterViewInit() {
|
ngAfterViewInit() {
|
||||||
@@ -97,10 +101,26 @@ export class PdfThumbListComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
getPages() {
|
getPages() {
|
||||||
return this.pdfViewer._pages.map((page) => ({
|
return this.pdfViewer._pages.map((page) => ({
|
||||||
id: page.id,
|
id: page.id,
|
||||||
|
getWidth: () => { return this.width; },
|
||||||
|
getHeight: () => { return this.currentHeight; },
|
||||||
getPage: () => this.pdfViewer.pdfDocument.getPage(page.id)
|
getPage: () => this.pdfViewer.pdfDocument.getPage(page.id)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private setHeight(id): number {
|
||||||
|
const height = this.pdfViewer.pdfDocument.getPage(id).then((page) => this.calculateHeight(page));
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
private calculateHeight(page) {
|
||||||
|
const viewport = page.getViewport(1);
|
||||||
|
const pageRatio = viewport.width / viewport.height;
|
||||||
|
const height = Math.floor(this.width / pageRatio);
|
||||||
|
|
||||||
|
this.currentHeight = height;
|
||||||
|
this.itemHeight = height + this.margin;
|
||||||
|
}
|
||||||
|
|
||||||
private calculateItems() {
|
private calculateItems() {
|
||||||
const { element, viewPort, itemsInView } = this.getContainerSetup();
|
const { element, viewPort, itemsInView } = this.getContainerSetup();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user