Skip to content

Commit

Permalink
fix(annotations): handle scrollToAnnotation in Preview
Browse files Browse the repository at this point in the history
* Add handler to handle the scrolltoannotation event.
* Add override to PresentationViewer to setPage since we only show 1 page at a time.
  • Loading branch information
mickr committed May 28, 2020
1 parent 699e859 commit 7d7409a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ class BaseViewer extends EventEmitter {
});

// Add a custom listener to scroll to the specified annotation
this.addListener('scrolltoannotation', data => this.annotator.scrollToAnnotation(data));
this.addListener('scrolltoannotation', this.handleScrollToAnnotation);

// Add a custom listener for events emmited by the annotator
this.annotator.addListener('annotatorevent', this.handleAnnotatorEvents);
Expand Down Expand Up @@ -1019,6 +1019,15 @@ class BaseViewer extends EventEmitter {
return permissions.can_annotate || permissions.can_create_annotations;
}

/**
* Handles the 'scrolltoannotation' event and calls the annotator scroll method
* @param {Object} data
* @param {string} data.id Annotation id
*/
handleScrollToAnnotation({ id }) {
this.annotator.scrollToAnnotation(id);
}

/**
* Returns whether or not annotations are enabled for this viewer.
*
Expand Down
18 changes: 17 additions & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ describe('lib/viewers/BaseViewer', () => {
expect(base.annotator.init).to.be.calledWith(1.5);
expect(base.addListener).to.be.calledWith('toggleannotationmode', sinon.match.func);
expect(base.addListener).to.be.calledWith('scale', sinon.match.func);
expect(base.addListener).to.be.calledWith('scrolltoannotation', sinon.match.func);
expect(base.addListener).to.be.calledWith('scrolltoannotation', base.handleScrollToAnnotation);
expect(base.annotator.addListener).to.be.calledWith('annotations_create', base.handleAnnotationCreateEvent);
expect(base.annotator.addListener).to.be.calledWith('annotatorevent', sinon.match.func);
expect(base.emit).to.be.calledWith('annotator', base.annotator);
Expand Down Expand Up @@ -1258,6 +1258,22 @@ describe('lib/viewers/BaseViewer', () => {
});
});

describe('handleScrollToAnnotation', () => {
it('should call the annotators scrollToAnnotation method', () => {
const scrollToAnnotationStub = sandbox.stub();

base.annotator = {
addListener: sandbox.stub(),
init: sandbox.stub(),
scrollToAnnotation: scrollToAnnotationStub,
};

base.handleScrollToAnnotation({ id: '123' });

expect(scrollToAnnotationStub).to.be.calledWith('123');
});
});

describe('areAnnotationsEnabled()', () => {
beforeEach(() => {
stubs.getViewerOption = sandbox
Expand Down
13 changes: 13 additions & 0 deletions src/lib/viewers/doc/PresentationViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,19 @@ class PresentationViewer extends DocBaseViewer {
};
};
}

/**
* @override
* @param {Object} data
* @param {string} data.id - location value of the annotation
* @param {Object} data.location - location value of the annotation
* @param {number} data.location.value - location value of the annotation
*/
handleScrollToAnnotation({ id, location: { value = 1 } }) {
this.setPage(value);

this.annotator.scrollToAnnotation(id);
}
}

export default PresentationViewer;
16 changes: 16 additions & 0 deletions src/lib/viewers/doc/__tests__/PresentationViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,20 @@ describe('lib/viewers/doc/PresentationViewer', () => {
expect(result.last.id).to.equal(1);
});
});

describe('handleScrollToAnnotation', () => {
it('should call setPage is location value provided', () => {
const setPageStub = sandbox.stub(presentation, 'setPage');
const scrollToAnnotationStub = sandbox.stub();

presentation.annotator = {
scrollToAnnotation: scrollToAnnotationStub,
};

presentation.handleScrollToAnnotation({ id: '123', location: { value: 5 } });

expect(setPageStub).to.be.calledWith(5);
expect(scrollToAnnotationStub).to.be.calledWith('123');
});
});
});

0 comments on commit 7d7409a

Please sign in to comment.