Skip to content

Commit

Permalink
Fix: Load annotator with the correct initial scale (#256)
Browse files Browse the repository at this point in the history
* Fix: annotator is now loaded with the correct initial scale
* Fix: update annotator tests for constructor change
* Update: remove chaining call
* Update: initial scale can be passed in as a parameter in init
  • Loading branch information
Minh-Ng authored Jul 28, 2017
1 parent d115c1c commit fc25534
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/lib/annotations/Annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ class Annotator extends EventEmitter {
/**
* Initializes annotator.
*
* @param {number} [initialScale] - The initial scale factor to render the annotations
* @return {void}
*/
init() {
init(initialScale = 1) {
this.annotatedElement = this.getAnnotatedEl(this.container);
this.notification = new Notification(this.annotatedElement);

Expand All @@ -106,7 +107,8 @@ class Annotator extends EventEmitter {
this.setupMobileDialog();
}

this.setScale(1);
const scale = initialScale;
this.setScale(scale);
this.setupAnnotations();
this.showAnnotations();
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/annotations/__tests__/Annotator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('lib/annotations/Annotator', () => {
beforeEach(() => {
const annotatedEl = document.querySelector('.annotated-element');
sandbox.stub(annotator, 'getAnnotatedEl').returns(annotatedEl);
annotator.annotatedElement = annotatedEl;

stubs.scale = sandbox.stub(annotator, 'setScale');
stubs.setup = sandbox.stub(annotator, 'setupAnnotations');
Expand All @@ -105,8 +106,8 @@ describe('lib/annotations/Annotator', () => {
});

it('should set scale and setup annotations', () => {
annotator.init();
expect(stubs.scale).to.be.called;
annotator.init(5);
expect(stubs.scale).to.be.calledWith(5);
expect(stubs.setup).to.be.called;
expect(stubs.show).to.be.called;
expect(annotator.annotationService).to.not.be.null;
Expand Down Expand Up @@ -161,6 +162,7 @@ describe('lib/annotations/Annotator', () => {
describe('once annotator is initialized', () => {
beforeEach(() => {
const annotatedEl = document.querySelector('.annotated-element');
annotator.annotatedElement = annotatedEl;
sandbox.stub(annotator, 'getAnnotatedEl').returns(annotatedEl);
sandbox.stub(annotator, 'setupAnnotations');
sandbox.stub(annotator, 'showAnnotations');
Expand Down
4 changes: 2 additions & 2 deletions src/lib/annotations/doc/DocAnnotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class DocAnnotator extends Annotator {
} else if (type === TYPES.point) {
thread = new DocPointThread(threadParams);
} else {
throw new Error(`DocAnnotator: Unknown Annotation Type: ${type}`);
throw new Error(`Unhandled document annotation type: ${type}`);
}

this.addThreadToMap(thread);
Expand Down Expand Up @@ -576,7 +576,7 @@ class DocAnnotator extends Annotator {
return;
}

// Determine if mouse is over any highlight dialog currently
// Determine if mouse is over any highlight dialog
// and ignore hover events of any highlights below
const event = this.mouseMoveEvent;
if (docAnnotatorUtil.isDialogDataType(event.target)) {
Expand Down
7 changes: 5 additions & 2 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ class BaseViewer extends EventEmitter {
// Add a resize handler for the window
document.defaultView.addEventListener('resize', this.debouncedResizeHandler);

this.addListener('load', () => {
this.addListener('load', (event) => {
({ scale: this.scale = 1 } = event);

if (this.annotationsPromise) {
this.annotationsPromise.then(this.loadAnnotator);
}
Expand Down Expand Up @@ -653,7 +655,8 @@ class BaseViewer extends EventEmitter {
locale: location.locale,
previewUI: this.previewUI
});
this.annotator.init();

this.annotator.init(this.scale);

// Disables controls during point annotation mode
this.annotator.addListener('annotationmodeenter', this.disableViewerControls);
Expand Down
6 changes: 4 additions & 2 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,17 +744,19 @@ describe('lib/viewers/BaseViewer', () => {
}
};
base.addListener = sandbox.stub();
base.scale = 1.5;
base.annotator = {
init: sandbox.stub(),
addListener: sandbox.stub()
addListener: sandbox.stub(),
setScale: sandbox.stub()
};
base.annotatorConf = {
CONSTRUCTOR: sandbox.stub().returns(base.annotator)
};
base.initAnnotations();
});
it('should initialize the annotator', () => {
expect(base.annotator.init).to.be.called;
expect(base.annotator.init).to.be.calledWith(1.5);
expect(base.annotator.addListener).to.be.calledWith('annotationmodeenter', sinon.match.func);
expect(base.annotator.addListener).to.be.calledWith('annotationmodeexit', sinon.match.func);
expect(base.annotator.addListener).to.be.calledWith('annotationsfetched', sinon.match.func);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ class DocBaseViewer extends BaseViewer {
this.loaded = true;
this.emit('load', {
numPages: this.pdfViewer.pagesCount,
endProgress: false // Indicate that viewer will end progress later
endProgress: false, // Indicate that viewer will end progress later
scale: this.pdfViewer.currentScale
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,8 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
docBase.pagesinitHandler();
expect(stubs.emit).to.be.calledWith('load', {
endProgress: false,
numPages: 5
numPages: 5,
scale: sinon.match.any
});
expect(docBase.loaded).to.be.truthy;
});
Expand Down

0 comments on commit fc25534

Please sign in to comment.