-
Notifications
You must be signed in to change notification settings - Fork 116
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: Add Annotations bundling changes back #95
Conversation
@@ -255,6 +268,12 @@ class BaseViewer extends EventEmitter { | |||
this.addListener('togglepointannotationmode', () => { | |||
this.annotator.togglePointModeHandler(); | |||
}); | |||
|
|||
this.addListener('load', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this in "destroy"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need to specifically remove this when you have removeAllListeners()
without a param.
- Reverts the commit (176ad13) which reverted the annotations bundling changes - Fixes performance problems from 'load' event
src/lib/annotations/Annotator.js
Outdated
fileId, | ||
token, | ||
canAnnotate: this.canAnnotate | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think breaking this out into a function is super valuable.
src/lib/annotations/Annotator.js
Outdated
@@ -65,8 +65,29 @@ class Annotator extends EventEmitter { | |||
* @return {void} | |||
*/ | |||
init() { | |||
this.annotatedElement = this.getAnnotatedEl(this.container); | |||
this.annotationService = this.initAnnotationService(this.options); | |||
this.notification = new Notification(this.annotatedElement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { apiHost, fileId, token } = this.options;
this.annotatedEl = this.getAnnotatedEl(this.container);
this.notification = new Notification(this.annotatedEl);
this.annotationService = new AnnotationService({
apiHost,
fileId,
token,
canAnnotation: this.canAnnotate
});
src/lib/viewers/BaseViewer.js
Outdated
@@ -83,6 +96,8 @@ class BaseViewer extends EventEmitter { | |||
fullscreen.removeAllListeners(); | |||
document.defaultView.removeEventListener('resize', this.debouncedResizeHandler); | |||
this.removeAllListeners(); | |||
this.removeAllListeners('togglepointannotationmode'); | |||
this.removeAllListeners('load'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need these two - removeAllListeners()
without an argument removes all listeners.
@@ -255,6 +268,12 @@ class BaseViewer extends EventEmitter { | |||
this.addListener('togglepointannotationmode', () => { | |||
this.annotator.togglePointModeHandler(); | |||
}); | |||
|
|||
this.addListener('load', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need to specifically remove this when you have removeAllListeners()
without a param.
src/lib/viewers/BaseViewer.js
Outdated
} | ||
|
||
/** | ||
* Returns whether or not viewer has the option to annotate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returns whether or not annotations are enabled for this viewer.
src/lib/viewers/BaseViewer.js
Outdated
// Construct and init annotator | ||
this.annotator = new this.annotatorLoader.CONSTRUCTOR({ | ||
canAnnotate, | ||
container: this.options.container, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this.options when we destructure above
src/lib/viewers/BaseViewer.js
Outdated
apiHost, | ||
fileId: file.id, | ||
token | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just move these into the annotator constructor options.
src/lib/viewers/BaseViewer.js
Outdated
this.annotator = new this.annotatorLoader.CONSTRUCTOR({ | ||
canAnnotate, | ||
container: this.options.container, | ||
options: annotationOptions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options: {
apiHost,
fileId: file.id,
token,
}
src/lib/viewers/BaseViewer.js
Outdated
|
||
/** | ||
* Returns whether or not viewer is annotatable. If an optional type is | ||
* passed in, we check if that type of annotation is allowed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returns whether or not viewer both supports annotations and has annotations enabled.
src/lib/viewers/BaseViewer.js
Outdated
* @return {boolean} Whether or not viewer is annotatable | ||
*/ | ||
isAnnotatable(type) { | ||
if (type && this.annotationTypes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we pass isAnnotatable with a type anywhere anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getPointModeClickHandler() in DocBaseViewer and ImageViewer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use const { TYPE: annotationTypes } = this.annotatorConf;
after you remove this.annotationTypes
src/lib/viewers/BaseViewer.js
Outdated
// Attempts to load annotations assets and initializes annotations if | ||
// the assets are available, the showAnnotations flag is true, and the | ||
// expiring embed is not a shared link | ||
// TODO(@spramod): Determine the expected behavior on shared links |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has there been any update on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asked by Tony...
src/lib/viewers/BaseViewer.js
Outdated
@@ -83,6 +96,8 @@ class BaseViewer extends EventEmitter { | |||
fullscreen.removeAllListeners(); | |||
document.defaultView.removeEventListener('resize', this.debouncedResizeHandler); | |||
this.removeAllListeners(); | |||
this.removeAllListeners('togglepointannotationmode'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think I was wrong here. It looks like when you call removeAllListeners
with no arguments it removes all listeners bound. https://nodejs.org/api/events.html#events_emitter_removealllisteners_eventname
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asked by Tony...
src/lib/viewers/BaseViewer.js
Outdated
@@ -255,6 +270,12 @@ class BaseViewer extends EventEmitter { | |||
this.addListener('togglepointannotationmode', () => { | |||
this.annotator.togglePointModeHandler(); | |||
}); | |||
|
|||
this.addListener('load', () => { | |||
if (window.BoxAnnotations !== undefined && this.isViewerAnnotatable()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just do (if window.boxAnnotations && this.isViewerAnnotatable())
?
src/lib/viewers/BaseViewer.js
Outdated
const fileVersionID = file.file_version.id; | ||
|
||
// Users can currently only view annotations on mobile | ||
const canAnnotate = checkPermission(file, PERMISSION_ANNOTATE) && !Browser.isMobile(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe extract this out to a class value since we do it twice?
src/lib/viewers/BaseViewer.js
Outdated
isViewerAnnotatable() { | ||
// Respect viewer-specific annotation option if it is set | ||
const viewerAnnotations = this.getViewerOption('annotations'); | ||
if (typeof viewerAnnotations === 'boolean') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What else could viewerAnnotations be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only other option would be undefined, this is really just double checking against if developers set it to anything other than a boolean
src/lib/viewers/BaseViewer.js
Outdated
|
||
annotateButtonEl.title = __('annotation_point_toggle'); | ||
annotateButtonEl.classList.remove(CLASS_HIDDEN); | ||
annotateButtonEl.addEventListener('click', handler); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one should be removed separately since it's a DOM event listener.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't actually remove the event listener unless you also save a reference to the handler. Unlike event emitter event listeners, DOM listeners can't be removed unless you specific the handler as the second param.
I don't think we ever removed this handler in the previous implementation... to do so we'd have to do:
this.annotateClickHandler = handler;
...
annotateButtonEl.addEventListener('click', this.annotateClickHandler);
And in the cleanup, remove with:
annotateButtonEl.removeEventListener('click', this.annotateClickHandler);
Don't have to null check for this.annotateClickHandler - if it doesn't exist, removeEventListener doesn't do anything.
src/lib/viewers/BaseViewer.js
Outdated
* @return {Function|null} Click handler | ||
*/ | ||
/* eslint-disable no-unused-vars */ | ||
getPointModeClickHandler(containerEl) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing the containerEl param in the JSDoc, maybe use @inheritdoc here?
// the assets are available, the showAnnotations flag is true, and the | ||
// expiring embed is not a shared link | ||
if (this.areAnnotationsEnabled() && !this.options.sharedLink) { | ||
this.loadAssets(ANNOTATIONS_JS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's potentially a timing issue if the document loads before annotations JS is ready. Doesn't seem super likely but it's possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave this for now - I think we could hook into each viewer's loadAssets if needed but it gets a bit complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea it happens very rarely but it does happen. I can look into this during the refactor
src/lib/viewers/BaseViewer.js
Outdated
/* global BoxAnnotations */ | ||
this.loader = new BoxAnnotations(); | ||
|
||
this.annotatorLoader = this.loader.determineAnnotator(this.options.viewer.NAME); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming is confusing between this.loader and this.annotatorLoader.
Do you use this.loader anywhere else? If not I'd do:
const boxAnnotations = new BoxAnnotations;
this.annotatorConf = boxAnnotations.determineAnnotator(this.options.viewer.NAME);
if (!this.annotatorConf) {
return;
}
src/lib/viewers/BaseViewer.js
Outdated
return; | ||
} | ||
|
||
this.annotationTypes = this.annotatorLoader.TYPE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's already available on this.annotatorConf.TYPE
, I wouldn't define it again
src/lib/viewers/BaseViewer.js
Outdated
*/ | ||
initAnnotations() { | ||
const { apiHost, container, file, location, token } = this.options; | ||
const fileVersionID = file.file_version.id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { id: fileId, file_version: { id: fileVersionId } } = file;
This creates two const variables, fileId
and fileVersionId
, set to file.id and file.file_version.id respectively. Use camelCase Id
instead of ID
- I know annotations code base uses fileVersionID
and threadID
and annotationID
right now, we can update that later, but the rest of preview uses Id
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh cool! Didn't know I could do that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also made sure to fix fileID and fileVersionID everywhere in BoxAnnotations.js so this shouldn't be anywhere in annotations anymore
src/lib/viewers/BaseViewer.js
Outdated
* @return {boolean} Whether or not viewer is annotatable | ||
*/ | ||
isAnnotatable(type) { | ||
if (type && this.annotationTypes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use const { TYPE: annotationTypes } = this.annotatorConf;
after you remove this.annotationTypes
src/lib/viewers/BaseViewer.js
Outdated
} | ||
} | ||
|
||
// Respect viewer-specific annotation option if it is set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Return whether or not annotations are enabled for this viewer
src/lib/viewers/BaseViewer.js
Outdated
|
||
if (!supportedType) { | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify this to:
if (!annotationTypes.some((annotationType) => type === annotationType)) {
return false;
}
src/lib/viewers/BaseViewer.js
Outdated
|
||
annotateButtonEl.title = __('annotation_point_toggle'); | ||
annotateButtonEl.classList.remove(CLASS_HIDDEN); | ||
annotateButtonEl.addEventListener('click', handler); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't actually remove the event listener unless you also save a reference to the handler. Unlike event emitter event listeners, DOM listeners can't be removed unless you specific the handler as the second param.
I don't think we ever removed this handler in the previous implementation... to do so we'd have to do:
this.annotateClickHandler = handler;
...
annotateButtonEl.addEventListener('click', this.annotateClickHandler);
And in the cleanup, remove with:
annotateButtonEl.removeEventListener('click', this.annotateClickHandler);
Don't have to null check for this.annotateClickHandler - if it doesn't exist, removeEventListener doesn't do anything.
- Replacing fileVersionID with fileVersionId (same with fileID) - Making sure isAnnotatable() is not overridden in Viewers
No description provided.