Dispatch a 'pagecancelled' event, in PDFPageView.cancelRendering, when rendering is cancelled

Also, the patch updates `TextLayerBuilder` to use the new 'pagecancelled' event for (future) event removal purposes.
This commit is contained in:
Jonas Jenwald 2018-09-23 13:47:33 +02:00
parent 54d6c2436c
commit 1eaa3b8a08
2 changed files with 40 additions and 0 deletions

View File

@ -260,6 +260,8 @@ class PDFPageView {
}
cancelRendering(keepAnnotations = false) {
const renderingState = this.renderingState;
if (this.paintTask) {
this.paintTask.cancel();
this.paintTask = null;
@ -275,6 +277,14 @@ class PDFPageView {
this.annotationLayer.cancel();
this.annotationLayer = null;
}
if (renderingState !== RenderingStates.INITIAL) {
this.eventBus.dispatch('pagecancelled', {
source: this,
pageNumber: this.id,
renderingState,
});
}
}
cssTransform(target, redrawAnnotations = false) {

View File

@ -56,6 +56,9 @@ class TextLayerBuilder {
this.textLayerRenderTask = null;
this.enhanceTextSelection = enhanceTextSelection;
this._boundEvents = Object.create(null);
this._bindEvents();
this._bindMouse();
}
@ -331,6 +334,33 @@ class TextLayerBuilder {
this.renderMatches(this.matches);
}
/**
* @private
*/
_bindEvents() {
const { eventBus, _boundEvents, } = this;
_boundEvents.pageCancelled = (evt) => {
if (evt.pageNumber !== this.pageNumber) {
return;
}
if (this.textLayerRenderTask) {
console.error('TextLayerBuilder._bindEvents: `this.cancel()` should ' +
'have been called when the page was reset, or rendering cancelled.');
return;
}
// Ensure that all event listeners are cleaned up when the page is reset,
// since re-rendering will create new `TextLayerBuilder` instances and the
// number of (stale) event listeners would otherwise grow without bound.
for (const name in _boundEvents) {
eventBus.off(name.toLowerCase(), _boundEvents[name]);
delete _boundEvents[name];
}
};
eventBus.on('pagecancelled', _boundEvents.pageCancelled);
}
/**
* Improves text selection by adding an additional div where the mouse was
* clicked. This reduces flickering of the content if the mouse is slowly