mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-12 17:04:57 +00:00
* updates including cropperJS * update on rotation + unit tests * small fix * hide toolbar on save * remove unused & duplicate method * added readonly & prettier code * include readOnly mode to hide/show media management actions * updated dependencies * fix emit spy * ADF-5378: Fix failing e2es * Fix comments for unit tests * ADF-5378: Removed obsolete buttons from e2e Co-authored-by: kristian <kristian.dimitrov@alfresco.com> Co-authored-by: adomi <ardit.domi@alfresco.com>
196 lines
5.3 KiB
TypeScript
196 lines
5.3 KiB
TypeScript
/*!
|
|
* @license
|
|
* Copyright 2019 Alfresco Software, Ltd.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import {
|
|
Component,
|
|
Input,
|
|
OnChanges,
|
|
SimpleChanges,
|
|
ViewEncapsulation,
|
|
ElementRef,
|
|
Output,
|
|
EventEmitter, AfterViewInit, ViewChild, HostListener
|
|
} from '@angular/core';
|
|
import { ContentService } from '../../services/content.service';
|
|
import { AppConfigService } from './../../app-config/app-config.service';
|
|
import Cropper from 'cropperjs';
|
|
|
|
@Component({
|
|
selector: 'adf-img-viewer',
|
|
templateUrl: './img-viewer.component.html',
|
|
styleUrls: ['./img-viewer.component.scss'],
|
|
host: { 'class': 'adf-image-viewer' },
|
|
encapsulation: ViewEncapsulation.None
|
|
})
|
|
export class ImgViewerComponent implements AfterViewInit, OnChanges {
|
|
|
|
@Input()
|
|
showToolbar = true;
|
|
|
|
@Input()
|
|
readOnly = true;
|
|
|
|
@Input()
|
|
urlFile: string;
|
|
|
|
@Input()
|
|
blobFile: Blob;
|
|
|
|
@Input()
|
|
nameFile: string;
|
|
|
|
@Output()
|
|
error = new EventEmitter<any>();
|
|
|
|
@Output()
|
|
submit = new EventEmitter<any>();
|
|
|
|
@ViewChild('image', { static: false})
|
|
public imageElement: ElementRef;
|
|
|
|
public scale: number = 1.0;
|
|
public cropper: Cropper;
|
|
public isEditing: boolean = false;
|
|
|
|
get currentScaleText(): string {
|
|
return Math.round(this.scale * 100) + '%';
|
|
}
|
|
|
|
constructor(
|
|
private appConfigService: AppConfigService,
|
|
private contentService: ContentService) {
|
|
this.initializeScaling();
|
|
}
|
|
|
|
initializeScaling() {
|
|
const scaling = this.appConfigService.get<number>('adf-viewer.image-viewer-scaling', undefined) / 100;
|
|
if (scaling) {
|
|
this.scale = scaling;
|
|
}
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
this.cropper = new Cropper(this.imageElement.nativeElement, {
|
|
autoCrop: false,
|
|
dragMode: 'move',
|
|
background: false,
|
|
scalable: true,
|
|
zoomOnWheel: false,
|
|
toggleDragModeOnDblclick: false,
|
|
viewMode: 1,
|
|
checkCrossOrigin: false,
|
|
ready: () => {
|
|
if (this.imageElement.nativeElement.width < this.cropper.getContainerData().width) {
|
|
const width = this.imageElement.nativeElement.width;
|
|
const height = this.imageElement.nativeElement.height;
|
|
const top = (this.cropper.getContainerData().height - this.imageElement.nativeElement.height) / 2;
|
|
const left = (this.cropper.getContainerData().width - this.imageElement.nativeElement.width) / 2;
|
|
|
|
this.cropper.setCanvasData({
|
|
width,
|
|
height,
|
|
top,
|
|
left
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
this.cropper.destroy();
|
|
}
|
|
|
|
@HostListener('document:keydown', ['$event'])
|
|
onKeyDown(event: KeyboardEvent) {
|
|
switch (event.key) {
|
|
case 'ArrowLeft':
|
|
event.preventDefault();
|
|
this.cropper.move(-3, 0);
|
|
break;
|
|
case 'ArrowUp':
|
|
event.preventDefault();
|
|
this.cropper.move(0, -3);
|
|
break;
|
|
case 'ArrowRight':
|
|
event.preventDefault();
|
|
this.cropper.move(3, 0);
|
|
break;
|
|
case 'ArrowDown':
|
|
event.preventDefault();
|
|
this.cropper.move(0, 3);
|
|
break;
|
|
case 'i':
|
|
this.zoomIn();
|
|
break;
|
|
case 'o':
|
|
this.zoomOut();
|
|
break;
|
|
case 'r':
|
|
this.rotateImage();
|
|
break;
|
|
default:
|
|
}
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
const blobFile = changes['blobFile'];
|
|
if (blobFile && blobFile.currentValue) {
|
|
this.urlFile = this.contentService.createTrustedUrl(this.blobFile);
|
|
return;
|
|
}
|
|
if (!this.urlFile && !this.blobFile) {
|
|
throw new Error('Attribute urlFile or blobFile is required');
|
|
}
|
|
}
|
|
|
|
zoomIn() {
|
|
this.cropper.zoom( 0.2);
|
|
this.scale = +((this.scale + 0.2).toFixed(1));
|
|
}
|
|
|
|
zoomOut() {
|
|
if (this.scale > 0.2) {
|
|
this.cropper.zoom( -0.2 );
|
|
this.scale = +((this.scale - 0.2).toFixed(1));
|
|
}
|
|
}
|
|
|
|
rotateImage() {
|
|
this.isEditing = true;
|
|
this.cropper.rotate( -90);
|
|
}
|
|
|
|
save() {
|
|
this.isEditing = false;
|
|
this.cropper.getCroppedCanvas().toBlob((blob) => {
|
|
this.submit.emit(blob);
|
|
});
|
|
}
|
|
|
|
reset() {
|
|
this.isEditing = false;
|
|
this.cropper.reset();
|
|
this.scale = 1.0;
|
|
this.cropper.zoomTo(this.scale);
|
|
}
|
|
|
|
onImageError() {
|
|
this.error.emit();
|
|
}
|
|
}
|