-
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
Chore: Store annotation threads by threadID in the thread map #316
Changes from 12 commits
0967d5b
43ebc0c
4252e64
e8ad909
b6c7218
cba44e4
810b673
45f931f
89c8610
7d62852
ae12fa4
b6f9751
b1e8d12
57c919d
4a7ff2f
100674a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,8 +76,8 @@ class Annotator extends EventEmitter { | |
this.unbindModeListeners(); | ||
|
||
if (this.threads) { | ||
Object.keys(this.threads).forEach((page) => { | ||
this.threads[page].forEach((thread) => { | ||
Object.values(this.threads).forEach((pageThreads) => { | ||
Object.values(pageThreads).forEach((thread) => { | ||
this.unbindCustomListenersOnThread(thread); | ||
}); | ||
}); | ||
|
@@ -244,8 +244,8 @@ class Annotator extends EventEmitter { | |
* @return {void} | ||
*/ | ||
hideAnnotations() { | ||
Object.keys(this.threads).forEach((page) => { | ||
this.threads[page].forEach((thread) => { | ||
Object.values(this.threads).forEach((pageThreads) => { | ||
Object.values(pageThreads).forEach((thread) => { | ||
thread.hide(); | ||
}); | ||
}); | ||
|
@@ -258,11 +258,14 @@ class Annotator extends EventEmitter { | |
* @return {void} | ||
*/ | ||
hideAnnotationsOnPage(pageNum) { | ||
if (this.threads[pageNum]) { | ||
this.threads[pageNum].forEach((thread) => { | ||
thread.hide(); | ||
}); | ||
if (!this.threads) { | ||
return; | ||
} | ||
|
||
const pageThreads = this.getThreadsOnPage(pageNum); | ||
Object.values(pageThreads).forEach((thread) => { | ||
thread.hide(); | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -272,8 +275,8 @@ class Annotator extends EventEmitter { | |
* @return {void} | ||
*/ | ||
renderAnnotations() { | ||
Object.keys(this.threads).forEach((page) => { | ||
this.threads[page].forEach((thread) => { | ||
Object.values(this.threads).forEach((pageThreads) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
Object.values(pageThreads).forEach((thread) => { | ||
thread.show(); | ||
}); | ||
}); | ||
|
@@ -287,11 +290,14 @@ class Annotator extends EventEmitter { | |
* @return {void} | ||
*/ | ||
renderAnnotationsOnPage(pageNum) { | ||
if (this.threads && this.threads[pageNum]) { | ||
this.threads[pageNum].forEach((thread) => { | ||
thread.show(); | ||
}); | ||
if (!this.threads) { | ||
return; | ||
} | ||
|
||
const pageThreads = this.getThreadsOnPage(pageNum); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be setup so that if it is given a certain value, it renders ALL annotations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean? We are currently rendering all annotations on that page There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, nvm |
||
Object.values(pageThreads).forEach((thread) => { | ||
thread.show(); | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -517,7 +523,7 @@ class Annotator extends EventEmitter { | |
* @return {void} | ||
*/ | ||
setupAnnotations() { | ||
// Map of page => [threads on page] | ||
// Map of page => {threads on page} | ||
this.threads = {}; | ||
this.bindDOMListeners(); | ||
this.bindCustomListenersOnService(this.annotationService); | ||
|
@@ -660,14 +666,7 @@ class Annotator extends EventEmitter { | |
|
||
// Thread was deleted, remove from thread map | ||
thread.addListener('threaddeleted', () => { | ||
const page = thread.location.page || 1; | ||
|
||
// Remove from map | ||
if (this.threads[page] instanceof Array) { | ||
this.threads[page] = this.threads[page].filter( | ||
(searchThread) => searchThread.threadID !== thread.threadID | ||
); | ||
} | ||
this.removeThreadFromMap(thread); | ||
}); | ||
|
||
// Thread should be cleaned up, unbind listeners - we don't do this | ||
|
@@ -794,9 +793,21 @@ class Annotator extends EventEmitter { | |
*/ | ||
addThreadToMap(thread) { | ||
// Add thread to in-memory map | ||
const page = thread.location.page || 1; // Defaults to page 1 if thread has no page | ||
this.threads[page] = this.threads[page] || []; | ||
this.threads[page].push(thread); | ||
const page = thread.location.page || 1; // Defaults to page 1 if thread has no page' | ||
const pageThreads = this.getThreadsOnPage(page); | ||
pageThreads[thread.threadID] = thread; | ||
} | ||
|
||
/** | ||
* Removes thread to in-memory map. | ||
* | ||
* @protected | ||
* @param {AnnotationThread} thread - Thread to bind events to | ||
* @return {void} | ||
*/ | ||
removeThreadFromMap(thread) { | ||
const page = thread.location.page || 1; | ||
delete this.threads[page][thread.threadID]; | ||
} | ||
|
||
/** | ||
|
@@ -825,6 +836,19 @@ class Annotator extends EventEmitter { | |
this.setScale(data.scale); | ||
this.rotateAnnotations(data.rotationAngle, data.pageNum); | ||
} | ||
/** | ||
* Gets threads on page | ||
* | ||
* @private | ||
* @param {number} page - Current page number | ||
* @return {Map|[]} Threads on page | ||
*/ | ||
getThreadsOnPage(page) { | ||
if (!(page in this.threads)) { | ||
this.threads[page] = {}; | ||
} | ||
return this.threads[page]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline after the |
||
} | ||
|
||
/** | ||
* Destroys pending threads. | ||
|
@@ -835,11 +859,12 @@ class Annotator extends EventEmitter { | |
*/ | ||
destroyPendingThreads() { | ||
let hasPendingThreads = false; | ||
Object.keys(this.threads).forEach((page) => { | ||
this.threads[page].forEach((pendingThread) => { | ||
if (annotatorUtil.isPending(pendingThread.state)) { | ||
|
||
Object.values(this.threads).forEach((pageThreads) => { | ||
Object.values(pageThreads).forEach((thread) => { | ||
if (annotatorUtil.isPending(thread.state)) { | ||
hasPendingThreads = true; | ||
pendingThread.destroy(); | ||
thread.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.
Can you follow the same idea for what I mention below, about
showAnnotationsOnPage()