diff --git a/src/lib/viewers/doc/DocBaseViewer.js b/src/lib/viewers/doc/DocBaseViewer.js index 19465703d..d1416c84d 100644 --- a/src/lib/viewers/doc/DocBaseViewer.js +++ b/src/lib/viewers/doc/DocBaseViewer.js @@ -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); diff --git a/src/lib/viewers/image/ImageViewer.js b/src/lib/viewers/image/ImageViewer.js index 87226194e..a797662b3 100644 --- a/src/lib/viewers/image/ImageViewer.js +++ b/src/lib/viewers/image/ImageViewer.js @@ -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); @@ -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; diff --git a/src/lib/viewers/image/__tests__/ImageViewer-test.js b/src/lib/viewers/image/__tests__/ImageViewer-test.js index 474bb750c..9b908acdc 100644 --- a/src/lib/viewers/image/__tests__/ImageViewer-test.js +++ b/src/lib/viewers/image/__tests__/ImageViewer-test.js @@ -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'); + }); + }); });