Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify extractText, and replace unnecessary var self = this statements with arrow functions, in web/pdf_find_controller.js #8397

Merged
merged 2 commits into from
May 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 21 additions & 32 deletions web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

import { createPromiseCapability } from './pdfjs';
import { scrollIntoView } from './ui_utils';

var FindStates = {
Expand Down Expand Up @@ -231,43 +232,32 @@ var PDFFindController = (function PDFFindControllerClosure() {
}
},

extractText: function PDFFindController_extractText() {
extractText() {
if (this.startedTextExtraction) {
return;
}
this.startedTextExtraction = true;
this.pageContents.length = 0;

this.pageContents = [];
var extractTextPromisesResolves = [];
var numPages = this.pdfViewer.pagesCount;
for (var i = 0; i < numPages; i++) {
this.extractTextPromises.push(new Promise(function (resolve) {
extractTextPromisesResolves.push(resolve);
}));
}
let promise = Promise.resolve();
for (let i = 0, ii = this.pdfViewer.pagesCount; i < ii; i++) {
let extractTextCapability = createPromiseCapability();
this.extractTextPromises[i] = extractTextCapability.promise;

var self = this;
function extractPageText(pageIndex) {
self.pdfViewer.getPageTextContent(pageIndex).then(
function textContentResolved(textContent) {
var textItems = textContent.items;
var str = [];
promise = promise.then(() => {
return this.pdfViewer.getPageTextContent(i).then((textContent) => {
let textItems = textContent.items;
let strBuf = [];

for (var i = 0, len = textItems.length; i < len; i++) {
str.push(textItems[i].str);
for (let j = 0, jj = textItems.length; j < jj; j++) {
strBuf.push(textItems[j].str);
}

// Store the pageContent as a string.
self.pageContents.push(str.join(''));

extractTextPromisesResolves[pageIndex](pageIndex);
if ((pageIndex + 1) < self.pdfViewer.pagesCount) {
extractPageText(pageIndex + 1);
}
}
);
this.pageContents[i] = strBuf.join('');
extractTextCapability.resolve(i);
});
});
}
extractPageText(0);
},

executeCommand: function PDFFindController_executeCommand(cmd, state) {
Expand Down Expand Up @@ -322,18 +312,17 @@ var PDFFindController = (function PDFFindControllerClosure() {
this.pageMatches = [];
this.matchCount = 0;
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.
this.updatePage(i);

// As soon as the text is extracted start finding the matches.
if (!(i in this.pendingFindMatches)) {
this.pendingFindMatches[i] = true;
this.extractTextPromises[i].then(function(pageIdx) {
delete self.pendingFindMatches[pageIdx];
self.calcFindMatch(pageIdx);
this.extractTextPromises[i].then((pageIdx) => {
delete this.pendingFindMatches[pageIdx];
this.calcFindMatch(pageIdx);
});
}
}
Expand Down