Change let
to const
where possible in the find controller
Doing so clearly indicates which variables are read-only and may not be mutated, which helps readability and prevents subtle issues.
This commit is contained in:
parent
38c9f5fc24
commit
ede414554e
@ -57,8 +57,8 @@ class PDFFindController {
|
|||||||
this.reset();
|
this.reset();
|
||||||
|
|
||||||
// Compile the regular expression for text normalization once.
|
// Compile the regular expression for text normalization once.
|
||||||
let replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
|
const replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
|
||||||
this._normalizationRegex = new RegExp('[' + replace + ']', 'g');
|
this._normalizationRegex = new RegExp(`[${replace}]`, 'g');
|
||||||
}
|
}
|
||||||
|
|
||||||
get pageMatches() {
|
get pageMatches() {
|
||||||
@ -141,7 +141,7 @@ class PDFFindController {
|
|||||||
updateMatchPosition(pageIndex, matchIndex, elements, beginIdx) {
|
updateMatchPosition(pageIndex, matchIndex, elements, beginIdx) {
|
||||||
if (this.selected.matchIdx === matchIndex &&
|
if (this.selected.matchIdx === matchIndex &&
|
||||||
this.selected.pageIdx === pageIndex) {
|
this.selected.pageIdx === pageIndex) {
|
||||||
let spot = {
|
const spot = {
|
||||||
top: FIND_SCROLL_OFFSET_TOP,
|
top: FIND_SCROLL_OFFSET_TOP,
|
||||||
left: FIND_SCROLL_OFFSET_LEFT,
|
left: FIND_SCROLL_OFFSET_LEFT,
|
||||||
};
|
};
|
||||||
@ -151,7 +151,7 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_normalize(text) {
|
_normalize(text) {
|
||||||
return text.replace(this._normalizationRegex, function (ch) {
|
return text.replace(this._normalizationRegex, function(ch) {
|
||||||
return CHARACTERS_TO_NORMALIZE[ch];
|
return CHARACTERS_TO_NORMALIZE[ch];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -164,8 +164,8 @@ class PDFFindController {
|
|||||||
*/
|
*/
|
||||||
_prepareMatches(matchesWithLength, matches, matchesLength) {
|
_prepareMatches(matchesWithLength, matches, matchesLength) {
|
||||||
function isSubTerm(matchesWithLength, currentIndex) {
|
function isSubTerm(matchesWithLength, currentIndex) {
|
||||||
let currentElem = matchesWithLength[currentIndex];
|
const currentElem = matchesWithLength[currentIndex];
|
||||||
let nextElem = matchesWithLength[currentIndex + 1];
|
const nextElem = matchesWithLength[currentIndex + 1];
|
||||||
|
|
||||||
// Check for cases like "TAMEd TAME".
|
// Check for cases like "TAMEd TAME".
|
||||||
if (currentIndex < matchesWithLength.length - 1 &&
|
if (currentIndex < matchesWithLength.length - 1 &&
|
||||||
@ -176,7 +176,7 @@ class PDFFindController {
|
|||||||
|
|
||||||
// Check for cases like "thIS IS".
|
// Check for cases like "thIS IS".
|
||||||
for (let i = currentIndex - 1; i >= 0; i--) {
|
for (let i = currentIndex - 1; i >= 0; i--) {
|
||||||
let prevElem = matchesWithLength[i];
|
const prevElem = matchesWithLength[i];
|
||||||
if (prevElem.skipped) {
|
if (prevElem.skipped) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -231,8 +231,9 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_calculatePhraseMatch(query, pageIndex, pageContent, entireWord) {
|
_calculatePhraseMatch(query, pageIndex, pageContent, entireWord) {
|
||||||
let matches = [];
|
const matches = [];
|
||||||
let queryLen = query.length;
|
const queryLen = query.length;
|
||||||
|
|
||||||
let matchIdx = -queryLen;
|
let matchIdx = -queryLen;
|
||||||
while (true) {
|
while (true) {
|
||||||
matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
|
matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
|
||||||
@ -248,12 +249,14 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_calculateWordMatch(query, pageIndex, pageContent, entireWord) {
|
_calculateWordMatch(query, pageIndex, pageContent, entireWord) {
|
||||||
let matchesWithLength = [];
|
const matchesWithLength = [];
|
||||||
|
|
||||||
// Divide the query into pieces and search for text in each piece.
|
// Divide the query into pieces and search for text in each piece.
|
||||||
let queryArray = query.match(/\S+/g);
|
const queryArray = query.match(/\S+/g);
|
||||||
for (let i = 0, len = queryArray.length; i < len; i++) {
|
for (let i = 0, len = queryArray.length; i < len; i++) {
|
||||||
let subquery = queryArray[i];
|
const subquery = queryArray[i];
|
||||||
let subqueryLen = subquery.length;
|
const subqueryLen = subquery.length;
|
||||||
|
|
||||||
let matchIdx = -subqueryLen;
|
let matchIdx = -subqueryLen;
|
||||||
while (true) {
|
while (true) {
|
||||||
matchIdx = pageContent.indexOf(subquery, matchIdx + subqueryLen);
|
matchIdx = pageContent.indexOf(subquery, matchIdx + subqueryLen);
|
||||||
@ -289,12 +292,9 @@ class PDFFindController {
|
|||||||
_calculateMatch(pageIndex) {
|
_calculateMatch(pageIndex) {
|
||||||
let pageContent = this._normalize(this._pageContents[pageIndex]);
|
let pageContent = this._normalize(this._pageContents[pageIndex]);
|
||||||
let query = this._normalize(this._state.query);
|
let query = this._normalize(this._state.query);
|
||||||
let caseSensitive = this._state.caseSensitive;
|
const { caseSensitive, entireWord, phraseSearch, } = this._state;
|
||||||
let phraseSearch = this._state.phraseSearch;
|
|
||||||
const entireWord = this._state.entireWord;
|
|
||||||
let queryLen = query.length;
|
|
||||||
|
|
||||||
if (queryLen === 0) {
|
if (query.length === 0) {
|
||||||
// Do nothing: the matches should be wiped out already.
|
// Do nothing: the matches should be wiped out already.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -332,22 +332,23 @@ class PDFFindController {
|
|||||||
|
|
||||||
let promise = Promise.resolve();
|
let promise = Promise.resolve();
|
||||||
for (let i = 0, ii = this._pdfViewer.pagesCount; i < ii; i++) {
|
for (let i = 0, ii = this._pdfViewer.pagesCount; i < ii; i++) {
|
||||||
let extractTextCapability = createPromiseCapability();
|
const extractTextCapability = createPromiseCapability();
|
||||||
this._extractTextPromises[i] = extractTextCapability.promise;
|
this._extractTextPromises[i] = extractTextCapability.promise;
|
||||||
|
|
||||||
promise = promise.then(() => {
|
promise = promise.then(() => {
|
||||||
return this._pdfViewer.getPageTextContent(i).then((textContent) => {
|
return this._pdfViewer.getPageTextContent(i).then((textContent) => {
|
||||||
let textItems = textContent.items;
|
const textItems = textContent.items;
|
||||||
let strBuf = [];
|
const strBuf = [];
|
||||||
|
|
||||||
for (let j = 0, jj = textItems.length; j < jj; j++) {
|
for (let j = 0, jj = textItems.length; j < jj; j++) {
|
||||||
strBuf.push(textItems[j].str);
|
strBuf.push(textItems[j].str);
|
||||||
}
|
}
|
||||||
// Store the pageContent as a string.
|
|
||||||
|
// Store the page content (text items) as one string.
|
||||||
this._pageContents[i] = strBuf.join('');
|
this._pageContents[i] = strBuf.join('');
|
||||||
extractTextCapability.resolve(i);
|
extractTextCapability.resolve(i);
|
||||||
}, (reason) => {
|
}, (reason) => {
|
||||||
console.error(`Unable to get page ${i + 1} text content`, reason);
|
console.error(`Unable to get text content for page ${i + 1}`, reason);
|
||||||
// Page error -- assuming no text content.
|
// Page error -- assuming no text content.
|
||||||
this._pageContents[i] = '';
|
this._pageContents[i] = '';
|
||||||
extractTextCapability.resolve(i);
|
extractTextCapability.resolve(i);
|
||||||
@ -364,16 +365,16 @@ class PDFFindController {
|
|||||||
this._pdfViewer.currentPageNumber = index + 1;
|
this._pdfViewer.currentPageNumber = index + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let page = this._pdfViewer.getPageView(index);
|
const page = this._pdfViewer.getPageView(index);
|
||||||
if (page.textLayer) {
|
if (page.textLayer) {
|
||||||
page.textLayer.updateMatches();
|
page.textLayer.updateMatches();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_nextMatch() {
|
_nextMatch() {
|
||||||
let previous = this._state.findPrevious;
|
const previous = this._state.findPrevious;
|
||||||
let currentPageIndex = this._pdfViewer.currentPageNumber - 1;
|
const currentPageIndex = this._pdfViewer.currentPageNumber - 1;
|
||||||
let numPages = this._pdfViewer.pagesCount;
|
const numPages = this._pdfViewer.pagesCount;
|
||||||
|
|
||||||
this.active = true;
|
this.active = true;
|
||||||
|
|
||||||
@ -384,9 +385,9 @@ class PDFFindController {
|
|||||||
this._offset.pageIdx = currentPageIndex;
|
this._offset.pageIdx = currentPageIndex;
|
||||||
this._offset.matchIdx = null;
|
this._offset.matchIdx = null;
|
||||||
this._resumePageIdx = null;
|
this._resumePageIdx = null;
|
||||||
this._pageMatches = [];
|
this._pageMatches.length = 0;
|
||||||
this._matchesCountTotal = 0;
|
|
||||||
this._pageMatchesLength = null;
|
this._pageMatchesLength = null;
|
||||||
|
this._matchesCountTotal = 0;
|
||||||
|
|
||||||
for (let i = 0; i < numPages; i++) {
|
for (let i = 0; i < numPages; i++) {
|
||||||
// Wipe out any previously highlighted matches.
|
// Wipe out any previously highlighted matches.
|
||||||
@ -414,13 +415,13 @@ class PDFFindController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let offset = this._offset;
|
const offset = this._offset;
|
||||||
// Keep track of how many pages we should maximally iterate through.
|
// Keep track of how many pages we should maximally iterate through.
|
||||||
this._pagesToSearch = numPages;
|
this._pagesToSearch = numPages;
|
||||||
// If there's already a `matchIdx` that means we are iterating through a
|
// If there's already a `matchIdx` that means we are iterating through a
|
||||||
// page's matches.
|
// page's matches.
|
||||||
if (offset.matchIdx !== null) {
|
if (offset.matchIdx !== null) {
|
||||||
let numPageMatches = this._pageMatches[offset.pageIdx].length;
|
const 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 simple case; we just have advance the matchIdx to select
|
||||||
@ -439,9 +440,9 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_matchesReady(matches) {
|
_matchesReady(matches) {
|
||||||
let offset = this._offset;
|
const offset = this._offset;
|
||||||
let numMatches = matches.length;
|
const numMatches = matches.length;
|
||||||
let previous = this._state.findPrevious;
|
const previous = this._state.findPrevious;
|
||||||
|
|
||||||
if (numMatches) {
|
if (numMatches) {
|
||||||
// There were matches for the page, so initialize `matchIdx`.
|
// There were matches for the page, so initialize `matchIdx`.
|
||||||
@ -472,7 +473,7 @@ class PDFFindController {
|
|||||||
|
|
||||||
let matches = null;
|
let matches = null;
|
||||||
do {
|
do {
|
||||||
let pageIdx = this._offset.pageIdx;
|
const pageIdx = this._offset.pageIdx;
|
||||||
matches = this._pageMatches[pageIdx];
|
matches = this._pageMatches[pageIdx];
|
||||||
if (!matches) {
|
if (!matches) {
|
||||||
// The matches don't exist yet for processing by `_matchesReady`,
|
// The matches don't exist yet for processing by `_matchesReady`,
|
||||||
@ -484,8 +485,8 @@ class PDFFindController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_advanceOffsetPage(previous) {
|
_advanceOffsetPage(previous) {
|
||||||
let offset = this._offset;
|
const offset = this._offset;
|
||||||
let numPages = this._extractTextPromises.length;
|
const 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;
|
||||||
|
|
||||||
@ -499,11 +500,11 @@ class PDFFindController {
|
|||||||
|
|
||||||
_updateMatch(found = false) {
|
_updateMatch(found = false) {
|
||||||
let state = FindState.NOT_FOUND;
|
let state = FindState.NOT_FOUND;
|
||||||
let wrapped = this._offset.wrapped;
|
const wrapped = this._offset.wrapped;
|
||||||
this._offset.wrapped = false;
|
this._offset.wrapped = false;
|
||||||
|
|
||||||
if (found) {
|
if (found) {
|
||||||
let previousPage = this._selected.pageIdx;
|
const 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 ? FindState.WRAPPED : FindState.FOUND);
|
state = (wrapped ? FindState.WRAPPED : FindState.FOUND);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user