Merge pull request #10182 from Snuffleupagus/TextLayerBuilder-rm-findController-checks

Small clean-up of the search related methods in `TextLayerBuilder`
This commit is contained in:
Tim van der Meij 2018-10-28 13:45:01 +01:00 committed by GitHub
commit 04ce2afd4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 62 deletions

View File

@ -149,7 +149,7 @@ class PDFFindController {
this._highlightMatches = false; this._highlightMatches = false;
this._pdfDocument = null; this._pdfDocument = null;
this._pageMatches = []; this._pageMatches = [];
this._pageMatchesLength = null; this._pageMatchesLength = [];
this._state = null; this._state = null;
this._selected = { // Currently selected match. this._selected = { // Currently selected match.
pageIdx: -1, pageIdx: -1,
@ -304,9 +304,6 @@ class PDFFindController {
} }
// Prepare arrays for storing the matches. // Prepare arrays for storing the matches.
if (!this._pageMatchesLength) {
this._pageMatchesLength = [];
}
this._pageMatchesLength[pageIndex] = []; this._pageMatchesLength[pageIndex] = [];
this._pageMatches[pageIndex] = []; this._pageMatches[pageIndex] = [];
@ -417,7 +414,7 @@ class PDFFindController {
this._offset.matchIdx = null; this._offset.matchIdx = null;
this._resumePageIdx = null; this._resumePageIdx = null;
this._pageMatches.length = 0; this._pageMatches.length = 0;
this._pageMatchesLength = null; this._pageMatchesLength.length = 0;
this._matchesCountTotal = 0; this._matchesCountTotal = 0;
for (let i = 0; i < numPages; i++) { for (let i = 0; i < numPages; i++) {

View File

@ -108,7 +108,7 @@ class TextLayerBuilder {
this.textLayerRenderTask.promise.then(() => { this.textLayerRenderTask.promise.then(() => {
this.textLayerDiv.appendChild(textLayerFrag); this.textLayerDiv.appendChild(textLayerFrag);
this._finishRendering(); this._finishRendering();
this.updateMatches(); this._updateMatches();
}, function (reason) { }, function (reason) {
// Cancelled or failed to render text layer; skipping errors. // Cancelled or failed to render text layer; skipping errors.
}); });
@ -134,24 +134,25 @@ class TextLayerBuilder {
this.textContent = textContent; this.textContent = textContent;
} }
convertMatches(matches, matchesLength) { _convertMatches(matches, matchesLength) {
let i = 0; // Early exit if there is nothing to convert.
let iIndex = 0;
let textContentItemsStr = this.textContentItemsStr;
let end = textContentItemsStr.length - 1;
let queryLen = (this.findController === null ?
0 : this.findController.state.query.length);
let ret = [];
if (!matches) { if (!matches) {
return ret; return [];
} }
for (let m = 0, len = matches.length; m < len; m++) { const { findController, textContentItemsStr, } = this;
let i = 0, iIndex = 0;
const end = textContentItemsStr.length - 1;
const queryLen = findController.state.query.length;
const result = [];
for (let m = 0, mm = matches.length; m < mm; m++) {
// Calculate the start position. // Calculate the start position.
let matchIdx = matches[m]; let matchIdx = matches[m];
// Loop over the divIdxs. // Loop over the divIdxs.
while (i !== end && matchIdx >= while (i !== end &&
(iIndex + textContentItemsStr[i].length)) { matchIdx >= (iIndex + textContentItemsStr[i].length)) {
iIndex += textContentItemsStr[i].length; iIndex += textContentItemsStr[i].length;
i++; i++;
} }
@ -176,8 +177,8 @@ class TextLayerBuilder {
// Somewhat the same array as above, but use > instead of >= to get // Somewhat the same array as above, but use > instead of >= to get
// the end position right. // the end position right.
while (i !== end && matchIdx > while (i !== end &&
(iIndex + textContentItemsStr[i].length)) { matchIdx > (iIndex + textContentItemsStr[i].length)) {
iIndex += textContentItemsStr[i].length; iIndex += textContentItemsStr[i].length;
i++; i++;
} }
@ -186,28 +187,22 @@ class TextLayerBuilder {
divIdx: i, divIdx: i,
offset: matchIdx - iIndex, offset: matchIdx - iIndex,
}; };
ret.push(match); result.push(match);
} }
return result;
return ret;
} }
renderMatches(matches) { _renderMatches(matches) {
// Early exit if there is nothing to render. // Early exit if there is nothing to render.
if (matches.length === 0) { if (matches.length === 0) {
return; return;
} }
const { findController, pageIdx, textContentItemsStr, textDivs, } = this;
let textContentItemsStr = this.textContentItemsStr; const isSelectedPage = (pageIdx === findController.selected.pageIdx);
let textDivs = this.textDivs; const selectedMatchIdx = findController.selected.matchIdx;
const highlightAll = findController.state.highlightAll;
let prevEnd = null; let prevEnd = null;
let pageIdx = this.pageIdx;
let isSelectedPage = (this.findController === null ?
false : (pageIdx === this.findController.selected.pageIdx));
let selectedMatchIdx = (this.findController === null ?
-1 : this.findController.selected.matchIdx);
let highlightAll = (this.findController === null ?
false : this.findController.state.highlightAll);
let infinity = { let infinity = {
divIdx: -1, divIdx: -1,
offset: undefined, offset: undefined,
@ -250,16 +245,14 @@ class TextLayerBuilder {
let highlightSuffix = (isSelected ? ' selected' : ''); let highlightSuffix = (isSelected ? ' selected' : '');
// Scroll the selected match into view. // Scroll the selected match into view.
if (this.findController) { if (findController.selected.matchIdx === i &&
if (this.findController.selected.matchIdx === i && findController.selected.pageIdx === pageIdx) {
this.findController.selected.pageIdx === pageIdx) { const spot = {
const spot = { top: MATCH_SCROLL_OFFSET_TOP,
top: MATCH_SCROLL_OFFSET_TOP, left: MATCH_SCROLL_OFFSET_LEFT,
left: MATCH_SCROLL_OFFSET_LEFT, };
}; scrollIntoView(textDivs[begin.divIdx], spot,
scrollIntoView(textDivs[begin.divIdx], spot, /* skipOverflowHiddenElements = */ true);
/* skipOverflowHiddenElements = */ true);
}
} }
// Match inside new div. // Match inside new div.
@ -293,20 +286,18 @@ class TextLayerBuilder {
} }
} }
updateMatches() { _updateMatches() {
// Only show matches when all rendering is done. // Only show matches when all rendering is done.
if (!this.renderingDone) { if (!this.renderingDone) {
return; return;
} }
const {
// Clear all matches. findController, matches, pageIdx, textContentItemsStr, textDivs,
let matches = this.matches; } = this;
let textDivs = this.textDivs;
let textContentItemsStr = this.textContentItemsStr;
let clearedUntilDivIdx = -1; let clearedUntilDivIdx = -1;
// Clear all current matches. // Clear all current matches.
for (let i = 0, len = matches.length; i < len; i++) { for (let i = 0, ii = matches.length; i < ii; i++) {
let match = matches[i]; let match = matches[i];
let begin = Math.max(clearedUntilDivIdx, match.begin.divIdx); let begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
for (let n = begin, end = match.end.divIdx; n <= end; n++) { for (let n = begin, end = match.end.divIdx; n <= end; n++) {
@ -317,21 +308,16 @@ class TextLayerBuilder {
clearedUntilDivIdx = match.end.divIdx + 1; clearedUntilDivIdx = match.end.divIdx + 1;
} }
if (!this.findController || !this.findController.highlightMatches) { if (!findController || !findController.highlightMatches) {
return; return;
} }
// Convert the matches on the `findController` into the match format
// Convert the matches on the page controller into the match format
// used for the textLayer. // used for the textLayer.
let pageMatches, pageMatchesLength; const pageMatches = findController.pageMatches[pageIdx] || null;
if (this.findController !== null) { const pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
pageMatches = this.findController.pageMatches[this.pageIdx] || null;
pageMatchesLength = (this.findController.pageMatchesLength) ?
this.findController.pageMatchesLength[this.pageIdx] || null : null;
}
this.matches = this.convertMatches(pageMatches, pageMatchesLength); this.matches = this._convertMatches(pageMatches, pageMatchesLength);
this.renderMatches(this.matches); this._renderMatches(this.matches);
} }
/** /**
@ -361,7 +347,7 @@ class TextLayerBuilder {
if (evt.pageIndex !== this.pageIdx && evt.pageIndex !== -1) { if (evt.pageIndex !== this.pageIdx && evt.pageIndex !== -1) {
return; return;
} }
this.updateMatches(); this._updateMatches();
}; };
eventBus.on('pagecancelled', _boundEvents.pageCancelled); eventBus.on('pagecancelled', _boundEvents.pageCancelled);