Skip to content

Commit

Permalink
feat(annotations): Emit annotations color
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Dec 15, 2020
1 parent 0f8d7d9 commit b198112
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const ANNOTATOR_EVENT = {
fetch: 'annotationsfetched',
error: 'annotationerror',
scale: 'scaleannotations',
setColor: 'annotations_color_set',
setVisibility: 'annotations_visible_set',
};

Expand Down
1 change: 1 addition & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ class BaseViewer extends EventEmitter {
const annotatorOptions = this.createAnnotatorOptions({
annotator: this.annotatorConf,
features: options && options.features,
initialColor: this.annotationModule.getColor(),
initialMode: this.getInitialAnnotationMode(),
intl: (options && options.intl) || intlUtil.createAnnotatorIntl(),
modeButtons: ANNOTATION_BUTTONS,
Expand Down
7 changes: 6 additions & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,9 @@ describe('lib/viewers/BaseViewer', () => {
};

beforeEach(() => {
base.annotationModule.cache = {
get: jest.fn().mockReturnValue('#000'),
};
base.options.viewer = { NAME: 'viewerName' };
base.options.location = { locale: 'en-US' };
base.options.showAnnotations = true;
Expand Down Expand Up @@ -1189,7 +1192,9 @@ describe('lib/viewers/BaseViewer', () => {
base.createAnnotator();

expect(base.options.boxAnnotations.getOptions).toBeCalled();
expect(base.createAnnotatorOptions).toBeCalledWith(expect.objectContaining(createOptionsArg));
expect(base.createAnnotatorOptions).toBeCalledWith(
expect.objectContaining({ ...createOptionsArg, initialColor: '#000' }),
);
});

test('should use default intl lib if annotator options not present ', () => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,7 @@ class DocBaseViewer extends BaseViewer {

handleAnnotationColorChange(color) {
this.annotationModule.setColor(color);
this.annotator.emit(ANNOTATOR_EVENT.setColor, color);
this.renderUI();
}

Expand Down
6 changes: 5 additions & 1 deletion src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2949,14 +2949,18 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
docBase.annotationModule = {
setColor: jest.fn(),
};
docBase.annotator = {
emit: jest.fn(),
};
docBase.renderUI = jest.fn();
});

test('should call setColor and renderUI', () => {
test('should call setColor and renderUI, and emit color', () => {
const color = '#fff';
docBase.handleAnnotationColorChange(color);

expect(docBase.annotationModule.setColor).toBeCalledWith(color);
expect(docBase.annotator.emit).toBeCalledWith(ANNOTATOR_EVENT.setColor, color);
expect(docBase.renderUI).toHaveBeenCalled();
});
});
Expand Down
2 changes: 2 additions & 0 deletions src/lib/viewers/image/ImageViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AnnotationControlsFSM, { AnnotationInput, AnnotationState, stateModeMap }
import ImageBaseViewer from './ImageBaseViewer';
import ImageControls from './ImageControls';
import {
ANNOTATOR_EVENT,
CLASS_ANNOTATIONS_IMAGE_FTUX_CURSOR_SEEN,
CLASS_INVISIBLE,
DISCOVERABILITY_ATTRIBUTE,
Expand Down Expand Up @@ -554,6 +555,7 @@ class ImageViewer extends ImageBaseViewer {

handleAnnotationColorChange(color) {
this.annotationModule.setColor(color);
this.annotator.emit(ANNOTATOR_EVENT.setColor, color);
this.renderUI();
}

Expand Down
12 changes: 10 additions & 2 deletions src/lib/viewers/image/__tests__/ImageViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import Browser from '../../../Browser';
import ControlsRoot from '../../controls/controls-root';
import ImageControls from '../ImageControls';
import ImageViewer from '../ImageViewer';
import { CLASS_ANNOTATIONS_IMAGE_FTUX_CURSOR_SEEN, IMAGE_FTUX_CURSOR_SEEN_KEY } from '../../../constants';
import {
ANNOTATOR_EVENT,
CLASS_ANNOTATIONS_IMAGE_FTUX_CURSOR_SEEN,
IMAGE_FTUX_CURSOR_SEEN_KEY,
} from '../../../constants';

jest.mock('../../controls/controls-root');

Expand Down Expand Up @@ -712,14 +716,18 @@ describe('lib/viewers/image/ImageViewer', () => {
image.annotationModule = {
setColor: jest.fn(),
};
image.annotator = {
emit: jest.fn(),
};
image.renderUI = jest.fn();
});

test('should call setColor and renderUI', () => {
test('should call setColor and renderUI, and emit color', () => {
const color = '#fff';
image.handleAnnotationColorChange(color);

expect(image.annotationModule.setColor).toBeCalledWith(color);
expect(image.annotator.emit).toBeCalledWith(ANNOTATOR_EVENT.setColor, color);
expect(image.renderUI).toHaveBeenCalled();
});
});
Expand Down

0 comments on commit b198112

Please sign in to comment.