Skip to content

Commit

Permalink
fix(annotations): Select newly created annotation (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored Oct 26, 2020
1 parent 741da26 commit 7f11114
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ class DocBaseViewer extends BaseViewer {
initAnnotations() {
super.initAnnotations();

if (this.areNewAnnotationsEnabled() && this.annotationControls) {
if (this.areNewAnnotationsEnabled()) {
this.annotator.addListener('annotations_create', this.handleAnnotationCreateEvent);
this.annotator.addListener('creator_staged_change', this.handleAnnotationCreatorChangeEvent);
this.annotator.addListener('creator_status_change', this.handleAnnotationCreatorChangeEvent);
Expand Down
17 changes: 17 additions & 0 deletions src/lib/viewers/image/ImageViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ImageViewer extends ImageBaseViewer {
this.rotateLeft = this.rotateLeft.bind(this);
this.updatePannability = this.updatePannability.bind(this);
this.handleAnnotationControlsClick = this.handleAnnotationControlsClick.bind(this);
this.handleAnnotationCreateEvent = this.handleAnnotationCreateEvent.bind(this);
this.handleAssetAndRepLoad = this.handleAssetAndRepLoad.bind(this);
this.handleImageDownloadError = this.handleImageDownloadError.bind(this);
this.getViewportDimensions = this.getViewportDimensions.bind(this);
Expand Down Expand Up @@ -507,6 +508,22 @@ class ImageViewer extends ImageBaseViewer {
this.annotator.toggleAnnotationMode(nextMode);
this.processAnnotationModeChange(nextMode);
}

handleAnnotationCreateEvent({ annotation: { id } = {}, meta: { status } = {} }) {
if (status !== 'success') {
return;
}

this.annotator.emit('annotations_active_set', id);
}

initAnnotations() {
super.initAnnotations();

if (this.areNewAnnotationsEnabled()) {
this.annotator.addListener('annotations_create', this.handleAnnotationCreateEvent);
}
}
}

export default ImageViewer;
36 changes: 36 additions & 0 deletions src/lib/viewers/image/__tests__/ImageViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,40 @@ describe('lib/viewers/image/ImageViewer', () => {
expect(image.annotator.toggleAnnotationMode).toHaveBeenCalled();
});
});

describe('handleAnnotationCreateEvent()', () => {
beforeEach(() => {
image.annotator = {
emit: jest.fn(),
};
image.annotationControls = {
destroy: jest.fn(),
setMode: jest.fn(),
};
image.processAnnotationModeChange = jest.fn();
});

const createEvent = status => ({
annotation: { id: '123' },
meta: {
status,
},
});

['error', 'pending'].forEach(status => {
test(`should not do anything if status is ${status}`, () => {
const event = createEvent(status);
image.handleAnnotationCreateEvent(event);

expect(image.annotator.emit).not.toBeCalled();
});
});

test('should reset controls if status is success', () => {
const event = createEvent('success');
image.handleAnnotationCreateEvent(event);

expect(image.annotator.emit).toBeCalledWith('annotations_active_set', '123');
});
});
});

0 comments on commit 7f11114

Please sign in to comment.