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

feat(drawing): Add DrawingManager for ImageAnnotator #641

Merged
merged 3 commits into from
Nov 24, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/image/ImageAnnotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PopupManager from '../popup/PopupManager';
import { getAnnotation, getRotation } from '../store';
import { centerRegion, getTransformedShape, isRegion, RegionCreationManager, RegionManager } from '../region';
import { CreatorStatus, getCreatorStatus } from '../store/creator';
import { DrawingManager, getShape, isDrawing } from '../drawing';
import { Manager } from '../common/BaseManager';
import { scrollToLocation } from '../utils/scroll';
import './ImageAnnotator.scss';
Expand Down Expand Up @@ -49,6 +50,9 @@ export default class ImageAnnotator extends BaseAnnotator {

if (this.managers.size === 0) {
this.managers.add(new PopupManager({ referenceEl }));
if (this.isFeatureEnabled('drawing')) {
this.managers.add(new DrawingManager({ referenceEl }));
}
this.managers.add(new RegionManager({ referenceEl }));
this.managers.add(new RegionCreationManager({ referenceEl }));
}
Expand Down Expand Up @@ -112,8 +116,16 @@ export default class ImageAnnotator extends BaseAnnotator {
return;
}

let shape = null;
if (isRegion(annotation)) {
const transformedShape = getTransformedShape(annotation.target.shape, rotation);
// eslint-disable-next-line prefer-destructuring
shape = annotation.target.shape;
} else if (isDrawing(annotation)) {
shape = getShape(annotation.target.path_groups);
}

if (shape) {
const transformedShape = getTransformedShape(shape, rotation);
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved

scrollToLocation(this.annotatedEl, referenceEl, {
offsets: centerRegion(transformedShape),
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
22 changes: 22 additions & 0 deletions src/image/__tests__/ImageAnnotator-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import DrawingManager from '../../drawing/DrawingManager';
import ImageAnnotator, { CSS_IS_DRAWING_CLASS } from '../ImageAnnotator';
import PopupManager from '../../popup/PopupManager';
import RegionCreationManager from '../../region/RegionCreationManager';
import RegionManager from '../../region/RegionManager';
import { Annotation } from '../../@types';
import { CreatorStatus, fetchAnnotationsAction, setStatusAction } from '../../store';
import { annotations as drawings } from '../../drawing/__mocks__/drawingData';
import { annotations as regions } from '../../region/__mocks__/data';
import { scrollToLocation } from '../../utils/scroll';

Expand Down Expand Up @@ -98,6 +100,18 @@ describe('ImageAnnotator', () => {
expect(managerIterator.next().value).toBeInstanceOf(RegionCreationManager);
});

test('should create DrawingManager is feature is enabled', () => {
annotator.destroy();
annotator = getAnnotator({ features: { drawing: true } });
const managers = annotator.getManagers(getParent(), getImage());
const managerIterator = managers.values();

expect(managerIterator.next().value).toBeInstanceOf(PopupManager);
expect(managerIterator.next().value).toBeInstanceOf(DrawingManager);
expect(managerIterator.next().value).toBeInstanceOf(RegionManager);
expect(managerIterator.next().value).toBeInstanceOf(RegionCreationManager);
});

test('should destroy any existing managers if they are not present in the annotated element', () => {
mockManager.exists.mockReturnValue(false);
annotator.managers = new Set([mockManager]);
Expand Down Expand Up @@ -220,13 +234,21 @@ describe('ImageAnnotator', () => {

annotator.annotatedEl = getParent();
annotator.store.dispatch(fetchAnnotationsAction.fulfilled(payload, 'test', undefined));
annotator.store.dispatch(
fetchAnnotationsAction.fulfilled({ ...payload, entries: drawings as Annotation[] }, 'test', undefined),
mxiao6 marked this conversation as resolved.
Show resolved Hide resolved
);
});

test('should call scrollToLocation for region annotations', () => {
annotator.scrollToAnnotation('anno_1');
expect(scrollToLocation).toHaveBeenCalledWith(getParent(), getImage(), { offsets: { x: 15, y: 15 } });
});

test('should call scrollToLocation for drawing anntotations', () => {
annotator.scrollToAnnotation('drawing_anno_1');
expect(scrollToLocation).toHaveBeenCalledWith(getParent(), getImage(), { offsets: { x: 16, y: 16 } });
});

test('should do nothing if the annotation id is undefined or not available in the store', () => {
annotator.scrollToAnnotation('nonsense');
expect(scrollToLocation).not.toHaveBeenCalled();
Expand Down