Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADF-5377] Viewer: Image Crop #6977

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/core/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@
"ZOOM_OUT": "Zoom out",
"FIT_PAGE": "Fit page",
"ROTATE": "Rotate",
"CROP": "Crop",
"SAVE": "Save",
"CANCEL": "Cancel",
"RESET": "Reset",
Expand Down
8 changes: 8 additions & 0 deletions lib/core/viewer/components/img-viewer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
(click)="rotateImage()">
<mat-icon>rotate_left</mat-icon>
</button>
<button
*ngIf="!readOnly" id="viewer-crop-button"
title="{{ 'ADF_VIEWER.ARIA.CROP' | translate }}"
attr.aria-label="{{ 'ADF_VIEWER.ARIA.CROP' | translate }}"
mat-icon-button
(click)="cropImage()">
<mat-icon>crop</mat-icon>
</button>

<button
id="viewer-reset-button"
Expand Down
29 changes: 28 additions & 1 deletion lib/core/viewer/components/img-viewer.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,26 @@ describe('Test Img viewer component ', () => {
expect(rotateButtonElement).toEqual(null);
});

it('should not show crop button by default', () => {
const rotateButtonElement = element.querySelector('#viewer-crop-button');
expect(rotateButtonElement).toEqual(null);
});

it('should start cropping when clicking the crop button', fakeAsync(() => {
component.readOnly = false;
spyOn(component, 'cropImage').and.callThrough();
spyOn(component.cropper, 'crop');
spyOn(component.cropper, 'setDragMode');
fixture.detectChanges();
const cropButtonElement = fixture.debugElement.query(By.css('#viewer-crop-button'));
cropButtonElement.triggerEventHandler('click', null);
tick();

expect(component.cropImage).toHaveBeenCalled();
expect(component.cropper.crop).toHaveBeenCalled();
expect(component.cropper.setDragMode).toHaveBeenCalledWith('crop');
}));

it('should rotate image by -90 degrees on button click', fakeAsync(() => {
component.readOnly = false;
spyOn(component, 'rotateImage').and.callThrough();
Expand Down Expand Up @@ -260,7 +280,7 @@ describe('Test Img viewer component ', () => {
expect(secondaryToolbar).toEqual(null);
});

it('should display second toolbar in rotate mode', fakeAsync(() => {
it('should display second toolbar in edit mode', fakeAsync(() => {
component.readOnly = false;
component.isEditing = true;

Expand All @@ -287,6 +307,13 @@ describe('Test Img viewer component ', () => {
expect(component.isEditing).toEqual(true);
});

it('should get in editing mode when the image gets cropped', () => {
component.readOnly = false;
component.cropImage();

expect(component.isEditing).toEqual(true);
});

it('should reset the scale and hide second toolbar', fakeAsync(() => {
component.readOnly = false;
component.isEditing = true;
Expand Down
11 changes: 11 additions & 0 deletions lib/core/viewer/components/img-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class ImgViewerComponent implements AfterViewInit, OnChanges {
dragMode: 'move',
background: false,
scalable: true,
modal: false,
zoomOnWheel: false,
toggleDragModeOnDblclick: false,
viewMode: 1,
Expand Down Expand Up @@ -175,16 +176,26 @@ export class ImgViewerComponent implements AfterViewInit, OnChanges {
this.cropper.rotate( -90);
}

cropImage() {
this.isEditing = true;
this.cropper.setDragMode( 'crop');
ursedaniel marked this conversation as resolved.
Show resolved Hide resolved
this.cropper.crop();
}

save() {
this.isEditing = false;
this.cropper.setDragMode('move');
this.cropper.getCroppedCanvas().toBlob((blob) => {
this.submit.emit(blob);
this.cropper.clear();
});
}

reset() {
this.isEditing = false;
this.cropper.clear();
this.cropper.reset();
this.cropper.setDragMode('move');
this.scale = 1.0;
this.cropper.zoomTo(this.scale);
}
Expand Down