Merge pull request #8397 from Snuffleupagus/extractText-refactor

Simplify `extractText`, and replace unnecessary `var self = this` statements with arrow functions, in `web/pdf_find_controller.js`
This commit is contained in:
Jonas Jenwald 2017-05-10 17:05:37 +02:00 committed by GitHub
commit 52998c5fe1

View File

@ -13,6 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { createPromiseCapability } from './pdfjs';
import { scrollIntoView } from './ui_utils'; import { scrollIntoView } from './ui_utils';
var FindStates = { var FindStates = {
@ -231,43 +232,32 @@ var PDFFindController = (function PDFFindControllerClosure() {
} }
}, },
extractText: function PDFFindController_extractText() { extractText() {
if (this.startedTextExtraction) { if (this.startedTextExtraction) {
return; return;
} }
this.startedTextExtraction = true; this.startedTextExtraction = true;
this.pageContents.length = 0;
this.pageContents = []; let promise = Promise.resolve();
var extractTextPromisesResolves = []; for (let i = 0, ii = this.pdfViewer.pagesCount; i < ii; i++) {
var numPages = this.pdfViewer.pagesCount; let extractTextCapability = createPromiseCapability();
for (var i = 0; i < numPages; i++) { this.extractTextPromises[i] = extractTextCapability.promise;
this.extractTextPromises.push(new Promise(function (resolve) {
extractTextPromisesResolves.push(resolve);
}));
}
var self = this; promise = promise.then(() => {
function extractPageText(pageIndex) { return this.pdfViewer.getPageTextContent(i).then((textContent) => {
self.pdfViewer.getPageTextContent(pageIndex).then( let textItems = textContent.items;
function textContentResolved(textContent) { let strBuf = [];
var textItems = textContent.items;
var str = [];
for (var i = 0, len = textItems.length; i < len; i++) { for (let j = 0, jj = textItems.length; j < jj; j++) {
str.push(textItems[i].str); strBuf.push(textItems[j].str);
} }
// Store the pageContent as a string. // Store the pageContent as a string.
self.pageContents.push(str.join('')); this.pageContents[i] = strBuf.join('');
extractTextCapability.resolve(i);
extractTextPromisesResolves[pageIndex](pageIndex); });
if ((pageIndex + 1) < self.pdfViewer.pagesCount) { });
extractPageText(pageIndex + 1);
}
}
);
} }
extractPageText(0);
}, },
executeCommand: function PDFFindController_executeCommand(cmd, state) { executeCommand: function PDFFindController_executeCommand(cmd, state) {
@ -322,18 +312,17 @@ var PDFFindController = (function PDFFindControllerClosure() {
this.pageMatches = []; this.pageMatches = [];
this.matchCount = 0; this.matchCount = 0;
this.pageMatchesLength = null; this.pageMatchesLength = null;
var self = this;
for (var i = 0; i < numPages; i++) { for (let i = 0; i < numPages; i++) {
// Wipe out any previous highlighted matches. // Wipe out any previous highlighted matches.
this.updatePage(i); this.updatePage(i);
// As soon as the text is extracted start finding the matches. // As soon as the text is extracted start finding the matches.
if (!(i in this.pendingFindMatches)) { if (!(i in this.pendingFindMatches)) {
this.pendingFindMatches[i] = true; this.pendingFindMatches[i] = true;
this.extractTextPromises[i].then(function(pageIdx) { this.extractTextPromises[i].then((pageIdx) => {
delete self.pendingFindMatches[pageIdx]; delete this.pendingFindMatches[pageIdx];
self.calcFindMatch(pageIdx); this.calcFindMatch(pageIdx);
}); });
} }
} }