From a829b06d206b4af28a09c0b24afff551f9e07026 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Fri, 5 Oct 2012 12:21:04 -0700 Subject: [PATCH] Fixes a minor find bugs. --- web/viewer.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index c651d63a5..9d7724920 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -229,7 +229,7 @@ var cache = new Cache(kCacheSize); var currentPageNumber = 1; var PDFFindController = { - startedTextExtraction: false, + extractTextPromise: null, // If active, find results will be highlighted. active: false, @@ -244,6 +244,8 @@ var PDFFindController = { matchIdx: 0 }, + state: null, + dirtyMatch: false, findTimeout: null, @@ -291,9 +293,11 @@ var PDFFindController = { }, extractText: function() { - if (this.startedTextExtraction) - return; - this.startedTextExtraction = true; + if (this.extractTextPromise) { + return this.extractTextPromise; + } + this.extractTextPromise = new PDFJS.Promise(); + var self = this; function extractPageText(pageIndex) { PDFView.pages[pageIndex].getTextContent().then( @@ -313,24 +317,32 @@ var PDFFindController = { if ((pageIndex + 1) < PDFView.pages.length) extractPageText(pageIndex + 1); + else + self.extractTextPromise.resolve(); } ); } extractPageText(0); + return this.extractTextPromise; }, handleEvent: function(e) { - this.state = e.detail; - if (e.detail.findPrevious === undefined) { + if (this.state === null || e.type !== 'findagain') { this.dirtyMatch = true; } + this.state = e.detail; + this.updateUIState(FindStates.FIND_PENDING); + + var promise = this.extractText(); clearTimeout(this.findTimeout); if (e.type === 'find') { // Only trigger the find action after 250ms of silence. - this.findTimeout = setTimeout(this.performFind.bind(this), 250); + this.findTimeout = setTimeout(function() { + promise.then(this.performFind.bind(this)); + }.bind(this), 250); } else { - this.performFind(); + promise.then(this.performFind.bind(this)); } }, @@ -359,8 +371,6 @@ var PDFFindController = { this.active = true; - this.updateUIState(FindStates.FIND_PENDING); - if (this.dirtyMatch) { // Need to recalculate the matches. this.dirtyMatch = false;