Skip to content

Commit

Permalink
chore(resin): Add annotations resin tags (#1230)
Browse files Browse the repository at this point in the history
* chore(resin): Add annotations resin tags

* chore(resin): Address comments

* chore(resin): Fix tests
  • Loading branch information
Mingze authored Jun 30, 2020
1 parent 70747eb commit 6312e28
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/lib/AnnotationControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum AnnotationMode {
}
export type ClickHandler = ({ event }: { event: MouseEvent }) => void;
export type Options = {
fileId: string;
onEscape?: () => void;
onRegionClick?: ClickHandler;
};
Expand Down Expand Up @@ -171,7 +172,7 @@ export default class AnnotationControls {
/**
* Initialize the annotation controls with options.
*/
public init({ onEscape = noop, onRegionClick = noop }: Options = {}): void {
public init({ fileId, onEscape = noop, onRegionClick = noop }: Options): void {
if (this.hasInit) {
return;
}
Expand All @@ -185,6 +186,9 @@ export default class AnnotationControls {
groupElement,
);

groupElement.setAttribute('data-resin-feature', 'annotations');
regionButton.setAttribute('data-resin-target', 'highlightRegion');
regionButton.setAttribute('data-resin-fileId', fileId);
regionButton.setAttribute('data-testid', 'bp-AnnotationsControls-regionBtn');

this.onEscape = onEscape;
Expand Down
1 change: 1 addition & 0 deletions src/lib/Controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Controls {
this.controlsEl = this.containerEl.appendChild(document.createElement('div'));
this.controlsEl.className = 'bp-controls';
this.controlsEl.setAttribute('data-testid', 'bp-controls');
this.controlsEl.setAttribute('data-resin-component', 'toolbar');

this.containerEl.addEventListener('mousemove', this.mousemoveHandler);
this.controlsEl.addEventListener('mouseenter', this.mouseenterHandler);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/__tests__/AnnotationControls-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('lib/AnnotationControls', () => {
});

it('should add the controls', () => {
annotationControls.init({ onRegionClick: stubs.onRegionClick });
annotationControls.init({ fileId: '0', onRegionClick: stubs.onRegionClick });

expect(annotationControls.controls.add).to.be.calledWith(
__('region_comment'),
Expand All @@ -113,15 +113,15 @@ describe('lib/AnnotationControls', () => {
it('should add keydown event listener', () => {
sandbox.spy(document, 'addEventListener');

annotationControls.init();
annotationControls.init({ fileId: '0' });

expect(document.addEventListener).to.be.calledWith('keydown', annotationControls.handleKeyDown);
});

it('should set onRest and hasInit', () => {
const onEscapeMock = sandbox.stub();

annotationControls.init({ onEscape: onEscapeMock });
annotationControls.init({ fileId: '0', onEscape: onEscapeMock });

expect(annotationControls.onEscape).to.equal(onEscapeMock);
expect(annotationControls.hasInit).to.equal(true);
Expand All @@ -132,7 +132,7 @@ describe('lib/AnnotationControls', () => {

sandbox.spy(document, 'addEventListener');

annotationControls.init();
annotationControls.init({ fileId: '0' });

expect(annotationControls.controls.add).not.to.be.called;
expect(document.addEventListener).not.to.be.called;
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 @@ -1099,6 +1099,7 @@ class DocBaseViewer extends BaseViewer {

if (this.areNewAnnotationsEnabled() && this.hasAnnotationCreatePermission()) {
this.annotationControls.init({
fileId: this.options.file.id,
onEscape: this.handleAnnotationControlsEscape,
onRegionClick: this.handleRegionClick,
});
Expand Down
1 change: 1 addition & 0 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
ICON_FULLSCREEN_OUT,
);
expect(docBase.annotationControls.init).to.be.calledWith({
fileId: docBase.options.file.id,
onEscape: docBase.handleAnnotationControlsEscape,
onRegionClick: docBase.handleRegionClick,
});
Expand Down
1 change: 1 addition & 0 deletions src/lib/viewers/image/ImageViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class ImageViewer extends ImageBaseViewer {
if (this.areNewAnnotationsEnabled() && this.hasAnnotationCreatePermission()) {
this.annotationControls = new AnnotationControls(this.controls);
this.annotationControls.init({
fileId: this.options.file.id,
onEscape: this.handleAnnotationControlsEscape,
onRegionClick: this.handleRegionClick,
});
Expand Down
1 change: 1 addition & 0 deletions src/lib/viewers/image/__tests__/ImageViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ describe('lib/viewers/image/ImageViewer', () => {
image.loadUI();

expect(AnnotationControls.prototype.init).to.be.calledWith({
fileId: image.options.file.id,
onEscape: image.handleAnnotationControlsEscape,
onRegionClick: image.handleRegionClick,
});
Expand Down

0 comments on commit 6312e28

Please sign in to comment.