Fix code style issues in pdf_find_controller.js and build strings more efficiently
This commit is contained in:
parent
456d219f2a
commit
ec1b58a30a
@ -24,45 +24,28 @@
|
|||||||
|
|
||||||
var PDFFindController = {
|
var PDFFindController = {
|
||||||
startedTextExtraction: false,
|
startedTextExtraction: false,
|
||||||
|
|
||||||
extractTextPromises: [],
|
extractTextPromises: [],
|
||||||
|
|
||||||
pendingFindMatches: {},
|
pendingFindMatches: {},
|
||||||
|
active: false, // If active, find results will be highlighted.
|
||||||
// If active, find results will be highlighted.
|
pageContents: [], // Stores the text for each page.
|
||||||
active: false,
|
|
||||||
|
|
||||||
// Stores the text for each page.
|
|
||||||
pageContents: [],
|
|
||||||
|
|
||||||
pageMatches: [],
|
pageMatches: [],
|
||||||
|
selected: { // Currently selected match.
|
||||||
// Currently selected match.
|
|
||||||
selected: {
|
|
||||||
pageIdx: -1,
|
pageIdx: -1,
|
||||||
matchIdx: -1
|
matchIdx: -1
|
||||||
},
|
},
|
||||||
|
offset: { // Where the find algorithm currently is in the document.
|
||||||
// Where find algorithm currently is in the document.
|
|
||||||
offset: {
|
|
||||||
pageIdx: null,
|
pageIdx: null,
|
||||||
matchIdx: null
|
matchIdx: null
|
||||||
},
|
},
|
||||||
|
|
||||||
resumePageIdx: null,
|
resumePageIdx: null,
|
||||||
|
|
||||||
state: null,
|
state: null,
|
||||||
|
|
||||||
dirtyMatch: false,
|
dirtyMatch: false,
|
||||||
|
|
||||||
findTimeout: null,
|
findTimeout: null,
|
||||||
|
|
||||||
pdfPageSource: null,
|
pdfPageSource: null,
|
||||||
|
|
||||||
integratedFind: false,
|
integratedFind: false,
|
||||||
|
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
if(typeof PDFFindBar === 'undefined' || PDFFindBar === null) {
|
if (typeof PDFFindBar === 'undefined' || PDFFindBar === null) {
|
||||||
throw 'PDFFindController cannot be initialized ' +
|
throw 'PDFFindController cannot be initialized ' +
|
||||||
'without a PDFFindBar instance';
|
'without a PDFFindBar instance';
|
||||||
}
|
}
|
||||||
@ -82,7 +65,7 @@ var PDFFindController = {
|
|||||||
}.bind(this));
|
}.bind(this));
|
||||||
this.handleEvent = this.handleEvent.bind(this);
|
this.handleEvent = this.handleEvent.bind(this);
|
||||||
|
|
||||||
for (var i = 0; i < events.length; i++) {
|
for (var i = 0, len = events.length; i < len; i++) {
|
||||||
window.addEventListener(events[i], this.handleEvent);
|
window.addEventListener(events[i], this.handleEvent);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -100,7 +83,7 @@ var PDFFindController = {
|
|||||||
var queryLen = query.length;
|
var queryLen = query.length;
|
||||||
|
|
||||||
if (queryLen === 0) {
|
if (queryLen === 0) {
|
||||||
// Do nothing the matches should be wiped out already.
|
// Do nothing: the matches should be wiped out already.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,14 +93,12 @@ var PDFFindController = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var matches = [];
|
var matches = [];
|
||||||
|
|
||||||
var matchIdx = -queryLen;
|
var matchIdx = -queryLen;
|
||||||
while (true) {
|
while (true) {
|
||||||
matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
|
matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
|
||||||
if (matchIdx === -1) {
|
if (matchIdx === -1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
matches.push(matchIdx);
|
matches.push(matchIdx);
|
||||||
}
|
}
|
||||||
this.pageMatches[pageIndex] = matches;
|
this.pageMatches[pageIndex] = matches;
|
||||||
@ -136,7 +117,8 @@ var PDFFindController = {
|
|||||||
|
|
||||||
this.pageContents = [];
|
this.pageContents = [];
|
||||||
var extractTextPromisesResolves = [];
|
var extractTextPromisesResolves = [];
|
||||||
for (var i = 0, ii = this.pdfPageSource.pdfDocument.numPages; i < ii; i++) {
|
var numPages = this.pdfPageSource.pdfDocument.numPages;
|
||||||
|
for (var i = 0; i < numPages; i++) {
|
||||||
this.extractTextPromises.push(new Promise(function (resolve) {
|
this.extractTextPromises.push(new Promise(function (resolve) {
|
||||||
extractTextPromisesResolves.push(resolve);
|
extractTextPromisesResolves.push(resolve);
|
||||||
}));
|
}));
|
||||||
@ -147,14 +129,14 @@ var PDFFindController = {
|
|||||||
self.pdfPageSource.pages[pageIndex].getTextContent().then(
|
self.pdfPageSource.pages[pageIndex].getTextContent().then(
|
||||||
function textContentResolved(textContent) {
|
function textContentResolved(textContent) {
|
||||||
var textItems = textContent.items;
|
var textItems = textContent.items;
|
||||||
var str = '';
|
var str = [];
|
||||||
|
|
||||||
for (var i = 0; i < textItems.length; i++) {
|
for (var i = 0, len = textItems.length; i < len; i++) {
|
||||||
str += textItems[i].str;
|
str.push(textItems[i].str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the pageContent as a string.
|
// Store the pageContent as a string.
|
||||||
self.pageContents.push(str);
|
self.pageContents.push(str.join(''));
|
||||||
|
|
||||||
extractTextPromisesResolves[pageIndex](pageIndex);
|
extractTextPromisesResolves[pageIndex](pageIndex);
|
||||||
if ((pageIndex + 1) < self.pdfPageSource.pages.length) {
|
if ((pageIndex + 1) < self.pdfPageSource.pages.length) {
|
||||||
@ -252,15 +234,16 @@ var PDFFindController = {
|
|||||||
var numPageMatches = this.pageMatches[offset.pageIdx].length;
|
var numPageMatches = this.pageMatches[offset.pageIdx].length;
|
||||||
if ((!previous && offset.matchIdx + 1 < numPageMatches) ||
|
if ((!previous && offset.matchIdx + 1 < numPageMatches) ||
|
||||||
(previous && offset.matchIdx > 0)) {
|
(previous && offset.matchIdx > 0)) {
|
||||||
// The simple case, we just have advance the matchIdx to select the next
|
// The simple case; we just have advance the matchIdx to select
|
||||||
// match on the page.
|
// the next match on the page.
|
||||||
this.hadMatch = true;
|
this.hadMatch = true;
|
||||||
offset.matchIdx = previous ? offset.matchIdx - 1 : offset.matchIdx + 1;
|
offset.matchIdx = (previous ? offset.matchIdx - 1 :
|
||||||
|
offset.matchIdx + 1);
|
||||||
this.updateMatch(true);
|
this.updateMatch(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// We went beyond the current page's matches, so we advance to the next
|
// We went beyond the current page's matches, so we advance to
|
||||||
// page.
|
// the next page.
|
||||||
this.advanceOffsetPage(previous);
|
this.advanceOffsetPage(previous);
|
||||||
}
|
}
|
||||||
// Start searching through the page.
|
// Start searching through the page.
|
||||||
@ -274,24 +257,23 @@ var PDFFindController = {
|
|||||||
if (numMatches) {
|
if (numMatches) {
|
||||||
// There were matches for the page, so initialize the matchIdx.
|
// There were matches for the page, so initialize the matchIdx.
|
||||||
this.hadMatch = true;
|
this.hadMatch = true;
|
||||||
offset.matchIdx = previous ? numMatches - 1 : 0;
|
offset.matchIdx = (previous ? numMatches - 1 : 0);
|
||||||
this.updateMatch(true);
|
this.updateMatch(true);
|
||||||
// matches were found
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// No matches attempt to search the next page.
|
// No matches, so attempt to search the next page.
|
||||||
this.advanceOffsetPage(previous);
|
this.advanceOffsetPage(previous);
|
||||||
if (offset.wrapped) {
|
if (offset.wrapped) {
|
||||||
offset.matchIdx = null;
|
offset.matchIdx = null;
|
||||||
if (!this.hadMatch) {
|
if (!this.hadMatch) {
|
||||||
// No point in wrapping there were no matches.
|
// No point in wrapping, there were no matches.
|
||||||
this.updateMatch(false);
|
this.updateMatch(false);
|
||||||
// while matches were not found, searching for a page
|
// while matches were not found, searching for a page
|
||||||
// with matches should nevertheless halt.
|
// with matches should nevertheless halt.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// matches were not found (and searching is not done)
|
// Matches were not found (and searching is not done).
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -315,10 +297,10 @@ var PDFFindController = {
|
|||||||
advanceOffsetPage: function(previous) {
|
advanceOffsetPage: function(previous) {
|
||||||
var offset = this.offset;
|
var offset = this.offset;
|
||||||
var numPages = this.extractTextPromises.length;
|
var numPages = this.extractTextPromises.length;
|
||||||
offset.pageIdx = previous ? offset.pageIdx - 1 : offset.pageIdx + 1;
|
offset.pageIdx = (previous ? offset.pageIdx - 1 : offset.pageIdx + 1);
|
||||||
offset.matchIdx = null;
|
offset.matchIdx = null;
|
||||||
if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
|
if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
|
||||||
offset.pageIdx = previous ? numPages - 1 : 0;
|
offset.pageIdx = (previous ? numPages - 1 : 0);
|
||||||
offset.wrapped = true;
|
offset.wrapped = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -332,7 +314,7 @@ var PDFFindController = {
|
|||||||
var previousPage = this.selected.pageIdx;
|
var previousPage = this.selected.pageIdx;
|
||||||
this.selected.pageIdx = this.offset.pageIdx;
|
this.selected.pageIdx = this.offset.pageIdx;
|
||||||
this.selected.matchIdx = this.offset.matchIdx;
|
this.selected.matchIdx = this.offset.matchIdx;
|
||||||
state = wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND;
|
state = (wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND);
|
||||||
// Update the currently selected page to wipe out any selected matches.
|
// Update the currently selected page to wipe out any selected matches.
|
||||||
if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
|
if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
|
||||||
this.updatePage(previousPage);
|
this.updatePage(previousPage);
|
||||||
@ -347,7 +329,7 @@ var PDFFindController = {
|
|||||||
updateUIState: function(state, previous) {
|
updateUIState: function(state, previous) {
|
||||||
if (this.integratedFind) {
|
if (this.integratedFind) {
|
||||||
FirefoxCom.request('updateFindControlState',
|
FirefoxCom.request('updateFindControlState',
|
||||||
{result: state, findPrevious: previous});
|
{ result: state, findPrevious: previous });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PDFFindBar.updateUIState(state, previous);
|
PDFFindBar.updateUIState(state, previous);
|
||||||
|
Loading…
Reference in New Issue
Block a user