Skip to content

Commit

Permalink
fix(annotations): handle scrollToAnnotation in Preview
Browse files Browse the repository at this point in the history
* PR Feedback
  • Loading branch information
mickr committed May 28, 2020
1 parent 7d7409a commit 682a10c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
13 changes: 9 additions & 4 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,11 +1021,16 @@ class BaseViewer extends EventEmitter {

/**
* Handles the 'scrolltoannotation' event and calls the annotator scroll method
* @param {Object} data
* @param {string} data.id Annotation id
* @param {Object} data - Annotator event data
* @param {string} [data.id] - Annotation Id
* @param {Object} [data.target] - Annotation target
* @param {Object} [data.target.location] - Target location of the annotation
* @param {number} [data.target.location.value] - location value
*
* @return {void}
*/
handleScrollToAnnotation({ id }) {
this.annotator.scrollToAnnotation(id);
handleScrollToAnnotation(data) {
this.annotator.scrollToAnnotation(data);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ describe('lib/viewers/BaseViewer', () => {

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

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

Expand Down
26 changes: 13 additions & 13 deletions src/lib/viewers/doc/PresentationViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ class PresentationViewer extends DocBaseViewer {
return hasXOverflow || hasYOverflow;
}

/**
* @override
*/
handleScrollToAnnotation(data) {
const { target } = data;

if (target && target.location) {
this.setPage(target.location.value);
}

super.handleScrollToAnnotation(data);
}

//--------------------------------------------------------------------------
// Protected
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -313,19 +326,6 @@ 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;
28 changes: 24 additions & 4 deletions src/lib/viewers/doc/__tests__/PresentationViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,18 +491,38 @@ describe('lib/viewers/doc/PresentationViewer', () => {
});

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

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

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

presentation.annotator = {
scrollToAnnotation: scrollToAnnotationStub,
};

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

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

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

presentation.annotator = {
scrollToAnnotation: scrollToAnnotationStub,
};

presentation.handleScrollToAnnotation(mockPartialAnnotation);

expect(setPageStub).not.to.be.called;
expect(scrollToAnnotationStub).to.be.calledWith(mockPartialAnnotation);
});
});
});

0 comments on commit 682a10c

Please sign in to comment.