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

fix(annotations): Select newly created annotation #1276

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);
jstoffan marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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');
});
});
});