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(annotations): Handle mode change events #1244

Merged
merged 4 commits into from
Aug 25, 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
18 changes: 16 additions & 2 deletions src/lib/AnnotationControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,25 @@ export default class AnnotationControls {
};

/**
* Region comment button click handler
* Set the mode. If the mode is different from what is currently saved in state,
* then reset the current controls and apply the active state based on the provided mode.
*/
public setMode(mode: AnnotationMode): void {
// Only update buttons if mode has changed
if (this.currentMode === mode) {
return;
}

this.resetControls();
this.currentMode = mode;
this.updateButton(mode);
}

/**
* Annotation control button click handler
*/
private handleClick = (onClick: ClickHandler, mode: AnnotationMode) => (event: MouseEvent): void => {
const prevMode = this.currentMode;

this.resetControls();

if (prevMode !== mode) {
Expand Down
25 changes: 25 additions & 0 deletions src/lib/__tests__/AnnotationControls-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,29 @@ describe('lib/AnnotationControls', () => {
expect(stubs.buttonElement.setAttribute).to.be.calledWith('data-resin-fileId', '0');
});
});

describe('setMode()', () => {
beforeEach(() => {
annotationControls.resetControls = sandbox.stub();
annotationControls.updateButton = sandbox.stub();
});

it('should do nothing if mode is the same', () => {
annotationControls.currentMode = 'region';

annotationControls.setMode('region');

expect(annotationControls.resetControls).not.to.be.called;
expect(annotationControls.updateButton).not.to.be.called;
});

it('should update controls if mode is not the same', () => {
annotationControls.currentMode = 'region';

annotationControls.setMode('highlight');

expect(annotationControls.resetControls).to.be.called;
expect(annotationControls.updateButton).to.be.calledWith('highlight');
});
});
});
8 changes: 8 additions & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class BaseViewer extends EventEmitter {
this.mobileZoomEndHandler = this.mobileZoomEndHandler.bind(this);
this.handleAnnotatorEvents = this.handleAnnotatorEvents.bind(this);
this.handleAnnotationCreateEvent = this.handleAnnotationCreateEvent.bind(this);
this.handleAnnotationModeChangeEvent = this.handleAnnotationModeChangeEvent.bind(this);
this.handleAnnotationControlsEscape = this.handleAnnotationControlsEscape.bind(this);
this.handleFullscreenEnter = this.handleFullscreenEnter.bind(this);
this.handleFullscreenExit = this.handleFullscreenExit.bind(this);
Expand Down Expand Up @@ -1015,6 +1016,7 @@ class BaseViewer extends EventEmitter {

if (this.areNewAnnotationsEnabled() && this.annotationControls) {
this.annotator.addListener('annotations_create', this.handleAnnotationCreateEvent);
this.annotator.addListener('annotations_mode_change', this.handleAnnotationModeChangeEvent);
}
}

Expand Down Expand Up @@ -1260,6 +1262,12 @@ class BaseViewer extends EventEmitter {
}
}

handleAnnotationModeChangeEvent({ mode }) {
if (this.annotationControls) {
this.annotationControls.setMode(mode);
}
}

/**
* Creates combined options to give to the annotator
*
Expand Down