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: Load annotator with the correct initial scale #256

Merged
merged 7 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions src/lib/annotations/Annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Annotator extends EventEmitter {
this.isMobile = data.isMobile;
this.previewUI = data.previewUI;
this.annotationModeHandlers = [];
this.annotatedElement = this.getAnnotatedEl(this.container);
}

/**
Expand Down Expand Up @@ -89,7 +90,6 @@ class Annotator extends EventEmitter {
* @return {void}
*/
init() {
this.annotatedElement = this.getAnnotatedEl(this.container);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe throw an error here if a developer forgets to override getAnnotatotedEl() in their viewer specific annotator

this.notification = new Notification(this.annotatedElement);

const { apiHost, fileId, token } = this.options;
Expand All @@ -106,7 +106,8 @@ class Annotator extends EventEmitter {
this.setupMobileDialog();
}

this.setScale(1);
const scale = annotatorUtil.getScale(this.annotatedElement);
this.setScale(scale);
this.setupAnnotations();
this.showAnnotations();
}
Expand Down Expand Up @@ -236,10 +237,11 @@ class Annotator extends EventEmitter {
* Sets the zoom scale.
*
* @param {number} scale - current zoom scale
* @return {void}
* @return {Annotator} Annotator instance returned for chaining
*/
setScale(scale) {
this.annotatedElement.setAttribute('data-scale', scale);
return this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the benefits of chaining, but since we already have access to the annotator instance, what is the benefit here over just calling instance.whatever() after calling instance.setScale()?

My thought process is that it goes against any coding paradigms we currently follow, in Preview

Copy link
Contributor Author

@Minh-Ng Minh-Ng Jul 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anytime you can chain you will have access to the instance. Its just for readability so you can chain calls in a clear readable fashion.

NumberArray.setFirstFiveElements(5)
           .setMultiplier(3)
           .transform()
           .collect()

As opposed to

NumberArray.setFirstFiveElements(5)
NumberArray.setMultiplier(3)
NumberArray.transform()
NumberArray.collect()

Very minute semantic idea, although I understand it is probably not a good idea to throw it into the mix now. Essentially your functions will build properties of your instance and a few getter methods will actually display the transformed properties.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When in Rome :) Good discussion around patterns here but since we don't utilize that sort of chaining in Preview, please add it in a feature change.

}

/**
Expand Down
2 changes: 2 additions & 0 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 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
7 changes: 6 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ class BaseViewer extends EventEmitter {
// Add a resize handler for the window
document.defaultView.addEventListener('resize', this.debouncedResizeHandler);

this.addListener('load', () => {
this.addListener('load', (event) => {
// this.scale set to 1 if event.scale does not exist
({ scale: this.scale = 1 } = event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that's not how it works now, but wouldn't it make more sense if set scale set this.scale as well as set the attribute on the annotated element?

If so, then move to

const { scale } = event.scale;
//...
this.annotator.setScale(scale);

and setScale() can be

setScale(scale = 1) { 
  this.scale = scale;
  // ... set attribute and stuff
}

Copy link
Contributor Author

@Minh-Ng Minh-Ng Jul 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally misread what you meant.

Problem here is that setScale is in Annotator and the eventListener is in BaseViewer. Annotator does not have a Annotator.scale value. It gets its scale from data-scale. In addition, BaseViewer.initAnnotations() both sets up the scale listener and the annotator. At this point the Annotator needs to know the scale value which cannot be known through any method other than the 'load' event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make this more clear by doing

if(event && event.scale) {
        this.scale = event.scale;
}

since this.scale is set to 1 by the property default.


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

this.annotator.setScale(this.scale);
this.annotator.init();

// Disables controls during point annotation mode
Expand Down
6 changes: 5 additions & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,17 +744,21 @@ describe('lib/viewers/BaseViewer', () => {
}
};
base.addListener = sandbox.stub();
base.scale = 1;
base.annotator = {
init: sandbox.stub(),
addListener: sandbox.stub()
addListener: sandbox.stub(),
setScale: sandbox.stub()
};
base.annotator.setScale.returns(base.annotator);
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.setScale).to.be.called;
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