diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 3b4f91425ecba..2e76ec6aa61fa 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -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) { diff --git a/web/text_layer_builder.js b/web/text_layer_builder.js index da78332918658..d8194b2958d9f 100644 --- a/web/text_layer_builder.js +++ b/web/text_layer_builder.js @@ -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