Merge pull request #10554 from Snuffleupagus/rendering-cancelled
Simplify the `updatetextlayermatches` event handling in `TextLayerBuilder`
This commit is contained in:
		
						commit
						b6f58191ca
					
				| @ -154,6 +154,7 @@ class PDFPageView { | |||||||
| 
 | 
 | ||||||
|   reset(keepZoomLayer = false, keepAnnotations = false) { |   reset(keepZoomLayer = false, keepAnnotations = false) { | ||||||
|     this.cancelRendering(keepAnnotations); |     this.cancelRendering(keepAnnotations); | ||||||
|  |     this.renderingState = RenderingStates.INITIAL; | ||||||
| 
 | 
 | ||||||
|     let div = this.div; |     let div = this.div; | ||||||
|     div.style.width = Math.floor(this.viewport.width) + 'px'; |     div.style.width = Math.floor(this.viewport.width) + 'px'; | ||||||
| @ -258,14 +259,15 @@ class PDFPageView { | |||||||
|     this.reset(/* keepZoomLayer = */ true, /* keepAnnotations = */ true); |     this.reset(/* keepZoomLayer = */ true, /* keepAnnotations = */ true); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** | ||||||
|  |    * PLEASE NOTE: Most likely you want to use the `this.reset()` method, | ||||||
|  |    *              rather than calling this one directly. | ||||||
|  |    */ | ||||||
|   cancelRendering(keepAnnotations = false) { |   cancelRendering(keepAnnotations = false) { | ||||||
|     const renderingState = this.renderingState; |  | ||||||
| 
 |  | ||||||
|     if (this.paintTask) { |     if (this.paintTask) { | ||||||
|       this.paintTask.cancel(); |       this.paintTask.cancel(); | ||||||
|       this.paintTask = null; |       this.paintTask = null; | ||||||
|     } |     } | ||||||
|     this.renderingState = RenderingStates.INITIAL; |  | ||||||
|     this.resume = null; |     this.resume = null; | ||||||
| 
 | 
 | ||||||
|     if (this.textLayer) { |     if (this.textLayer) { | ||||||
| @ -276,14 +278,6 @@ class PDFPageView { | |||||||
|       this.annotationLayer.cancel(); |       this.annotationLayer.cancel(); | ||||||
|       this.annotationLayer = null; |       this.annotationLayer = null; | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     if (renderingState !== RenderingStates.INITIAL) { |  | ||||||
|       this.eventBus.dispatch('pagecancelled', { |  | ||||||
|         source: this, |  | ||||||
|         pageNumber: this.id, |  | ||||||
|         renderingState, |  | ||||||
|       }); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   cssTransform(target, redrawAnnotations = false) { |   cssTransform(target, redrawAnnotations = false) { | ||||||
|  | |||||||
| @ -152,6 +152,7 @@ class PDFThumbnailView { | |||||||
| 
 | 
 | ||||||
|   reset() { |   reset() { | ||||||
|     this.cancelRendering(); |     this.cancelRendering(); | ||||||
|  |     this.renderingState = RenderingStates.INITIAL; | ||||||
| 
 | 
 | ||||||
|     this.pageWidth = this.viewport.width; |     this.pageWidth = this.viewport.width; | ||||||
|     this.pageHeight = this.viewport.height; |     this.pageHeight = this.viewport.height; | ||||||
| @ -195,12 +196,15 @@ class PDFThumbnailView { | |||||||
|     this.reset(); |     this.reset(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** | ||||||
|  |    * PLEASE NOTE: Most likely you want to use the `this.reset()` method, | ||||||
|  |    *              rather than calling this one directly. | ||||||
|  |    */ | ||||||
|   cancelRendering() { |   cancelRendering() { | ||||||
|     if (this.renderTask) { |     if (this.renderTask) { | ||||||
|       this.renderTask.cancel(); |       this.renderTask.cancel(); | ||||||
|       this.renderTask = null; |       this.renderTask = null; | ||||||
|     } |     } | ||||||
|     this.renderingState = RenderingStates.INITIAL; |  | ||||||
|     this.resume = null; |     this.resume = null; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -53,9 +53,7 @@ class TextLayerBuilder { | |||||||
|     this.textLayerRenderTask = null; |     this.textLayerRenderTask = null; | ||||||
|     this.enhanceTextSelection = enhanceTextSelection; |     this.enhanceTextSelection = enhanceTextSelection; | ||||||
| 
 | 
 | ||||||
|     this._boundEvents = Object.create(null); |     this._onUpdateTextLayerMatches = null; | ||||||
|     this._bindEvents(); |  | ||||||
| 
 |  | ||||||
|     this._bindMouse(); |     this._bindMouse(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -109,6 +107,16 @@ class TextLayerBuilder { | |||||||
|     }, function (reason) { |     }, function (reason) { | ||||||
|       // Cancelled or failed to render text layer; skipping errors.
 |       // Cancelled or failed to render text layer; skipping errors.
 | ||||||
|     }); |     }); | ||||||
|  | 
 | ||||||
|  |     if (!this._onUpdateTextLayerMatches) { | ||||||
|  |       this._onUpdateTextLayerMatches = (evt) => { | ||||||
|  |         if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) { | ||||||
|  |           this._updateMatches(); | ||||||
|  |         } | ||||||
|  |       }; | ||||||
|  |       this.eventBus.on('updatetextlayermatches', | ||||||
|  |                        this._onUpdateTextLayerMatches); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
| @ -119,6 +127,11 @@ class TextLayerBuilder { | |||||||
|       this.textLayerRenderTask.cancel(); |       this.textLayerRenderTask.cancel(); | ||||||
|       this.textLayerRenderTask = null; |       this.textLayerRenderTask = null; | ||||||
|     } |     } | ||||||
|  |     if (this._onUpdateTextLayerMatches) { | ||||||
|  |       this.eventBus.off('updatetextlayermatches', | ||||||
|  |                         this._onUpdateTextLayerMatches); | ||||||
|  |       this._onUpdateTextLayerMatches = null; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   setTextContentStream(readableStream) { |   setTextContentStream(readableStream) { | ||||||
| @ -314,40 +327,6 @@ class TextLayerBuilder { | |||||||
|     this._renderMatches(this.matches); |     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]; |  | ||||||
|       } |  | ||||||
|     }; |  | ||||||
|     _boundEvents.updateTextLayerMatches = (evt) => { |  | ||||||
|       if (evt.pageIndex !== this.pageIdx && evt.pageIndex !== -1) { |  | ||||||
|         return; |  | ||||||
|       } |  | ||||||
|       this._updateMatches(); |  | ||||||
|     }; |  | ||||||
| 
 |  | ||||||
|     eventBus.on('pagecancelled', _boundEvents.pageCancelled); |  | ||||||
|     eventBus.on('updatetextlayermatches', _boundEvents.updateTextLayerMatches); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |   /** | ||||||
|    * Improves text selection by adding an additional div where the mouse was |    * Improves text selection by adding an additional div where the mouse was | ||||||
|    * clicked. This reduces flickering of the content if the mouse is slowly |    * clicked. This reduces flickering of the content if the mouse is slowly | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user