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

fix(annotations): handle scrollToAnnotation in Preview #1214

Merged
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
14 changes: 13 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,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);
jstoffan marked this conversation as resolved.
Show resolved Hide resolved
mickr marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* Handles the 'scrolltoannotation' event and calls the annotator scroll method
* @param {string | Object} event - Annotation Event
* @param {string} event.id - Annotation Id
* @return {void}
*/
handleScrollToAnnotation(event) {
const data = event && event.id ? event.id : event;

this.annotator.scrollToAnnotation(data);
}

/**
* Returns whether or not annotations are enabled for this viewer.
*
Expand Down
32 changes: 31 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,36 @@ describe('lib/viewers/BaseViewer', () => {
});
});

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

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

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

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

it('should call the annotators scrollToAnnotation if string provided', () => {
const scrollToAnnotationStub = sandbox.stub();

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

base.handleScrollToAnnotation('123');

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

describe('areAnnotationsEnabled()', () => {
beforeEach(() => {
stubs.getViewerOption = sandbox
Expand Down
10 changes: 10 additions & 0 deletions src/lib/viewers/doc/PresentationViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import throttle from 'lodash/throttle';
import DocBaseViewer from './DocBaseViewer';
import PresentationPreloader from './PresentationPreloader';
import { CLASS_INVISIBLE } from '../../constants';
import { getProp } from '../../util';
import './Presentation.scss';

const WHEEL_THROTTLE = 200;
Expand Down Expand Up @@ -124,6 +125,15 @@ class PresentationViewer extends DocBaseViewer {
return hasXOverflow || hasYOverflow;
}

/**
* @override
*/
handleScrollToAnnotation(data) {
this.setPage(getProp(data, 'target.location.value', 1));

super.handleScrollToAnnotation(data);
}

//--------------------------------------------------------------------------
// Protected
//--------------------------------------------------------------------------
Expand Down
36 changes: 36 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,40 @@ describe('lib/viewers/doc/PresentationViewer', () => {
expect(result.last.id).to.equal(1);
});
});

describe('handleScrollToAnnotation', () => {
let setPageStub;
let scrollToAnnotationStub;

beforeEach(() => {
setPageStub = sandbox.stub(presentation, 'setPage');
scrollToAnnotationStub = sandbox.stub();
});

it('should call setPage is location value provided', () => {
const mockPartialAnnotation = { id: '123', target: { location: { value: 5 } } };

presentation.annotator = {
scrollToAnnotation: scrollToAnnotationStub,
};

presentation.handleScrollToAnnotation(mockPartialAnnotation);

expect(setPageStub).to.be.calledWith(5);
expect(scrollToAnnotationStub).to.be.calledWith(mockPartialAnnotation.id);
});

it('should call setPage with 1 if location not provided', () => {
const mockPartialAnnotation = { id: '123' };

presentation.annotator = {
scrollToAnnotation: scrollToAnnotationStub,
};

presentation.handleScrollToAnnotation(mockPartialAnnotation);

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