Skip to content

Commit

Permalink
feat(annotations): Emit stroke color
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Dec 14, 2020
1 parent 0f8d7d9 commit 182c3ba
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 3 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',
setStroke: 'annotations_stroke_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 @@ -997,6 +997,7 @@ class BaseViewer extends EventEmitter {
initialMode: this.getInitialAnnotationMode(),
intl: (options && options.intl) || intlUtil.createAnnotatorIntl(),
modeButtons: ANNOTATION_BUTTONS,
stroke: { color: this.annotationModule.getColor() },
});

this.annotator = new this.annotatorConf.CONSTRUCTOR(annotatorOptions);
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, stroke: { color: '#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.setStroke, { 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 @@ -2946,17 +2946,21 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

describe('handleAnnotationColorChange', () => {
beforeEach(() => {
docBase.annotator = {
emit: jest.fn(),
};
docBase.annotationModule = {
setColor: 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('annotations_stroke_set', { color: '#fff' });
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.setStroke, { color });
this.renderUI();
}

Expand Down
6 changes: 5 additions & 1 deletion src/lib/viewers/image/__tests__/ImageViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,17 +709,21 @@ describe('lib/viewers/image/ImageViewer', () => {

describe('handleAnnotationColorChange', () => {
beforeEach(() => {
image.annotator = {
emit: jest.fn(),
};
image.annotationModule = {
setColor: 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('annotations_stroke_set', { color: '#fff' });
expect(image.renderUI).toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 182c3ba

Please sign in to comment.