Merge pull request #10126 from Snuffleupagus/find-fixes

[Regression] Restore the ability to start searching before a document has loaded, and ignore searches for previously opened documents (PR 10099 follow-up)
This commit is contained in:
Tim van der Meij 2018-10-04 00:09:26 +02:00 committed by GitHub
commit 8411a7da28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,15 +58,7 @@ class PDFFindController {
this._eventBus = eventBus; this._eventBus = eventBus;
this._reset(); this._reset();
eventBus.on('findbarclose', this._onFindBarClose.bind(this));
eventBus.on('findbarclose', () => {
this._highlightMatches = false;
eventBus.dispatch('updatetextlayermatches', {
source: this,
pageIndex: -1,
});
});
// Compile the regular expression for text normalization once. // Compile the regular expression for text normalization once.
const replace = Object.keys(CHARACTERS_TO_NORMALIZE).join(''); const replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
@ -110,9 +102,7 @@ class PDFFindController {
} }
executeCommand(cmd, state) { executeCommand(cmd, state) {
if (!this._pdfDocument) { const pdfDocument = this._pdfDocument;
return;
}
if (this._state === null || cmd !== 'findagain') { if (this._state === null || cmd !== 'findagain') {
this._dirtyMatch = true; this._dirtyMatch = true;
@ -121,14 +111,25 @@ class PDFFindController {
this._updateUIState(FindState.PENDING); this._updateUIState(FindState.PENDING);
this._firstPagePromise.then(() => { this._firstPagePromise.then(() => {
if (!this._pdfDocument ||
(pdfDocument && this._pdfDocument !== pdfDocument)) {
// If the document was closed before searching began, or if the search
// operation was relevant for a previously opened document, do nothing.
return;
}
this._extractText(); this._extractText();
if (this._findTimeout) {
clearTimeout(this._findTimeout); clearTimeout(this._findTimeout);
this._findTimeout = null;
}
if (cmd === 'find') { if (cmd === 'find') {
// Trigger the find action with a small delay to avoid starting the // Trigger the find action with a small delay to avoid starting the
// search when the user is still typing (saving resources). // search when the user is still typing (saving resources).
this._findTimeout = this._findTimeout = setTimeout(() => {
setTimeout(this._nextMatch.bind(this), FIND_TIMEOUT); this._nextMatch();
this._findTimeout = null;
}, FIND_TIMEOUT);
} else { } else {
this._nextMatch(); this._nextMatch();
} }
@ -156,14 +157,19 @@ class PDFFindController {
this._pendingFindMatches = Object.create(null); this._pendingFindMatches = Object.create(null);
this._resumePageIdx = null; this._resumePageIdx = null;
this._dirtyMatch = false; this._dirtyMatch = false;
clearTimeout(this._findTimeout);
this._findTimeout = null; this._findTimeout = null;
this._firstPagePromise = new Promise((resolve) => { this._firstPagePromise = new Promise((resolve) => {
const eventBus = this._eventBus; const onPagesInit = () => {
eventBus.on('pagesinit', function onPagesInit() { if (!this._pdfDocument) {
eventBus.off('pagesinit', onPagesInit); throw new Error(
'PDFFindController: `setDocument()` should have been called.');
}
this._eventBus.off('pagesinit', onPagesInit);
resolve(); resolve();
}); };
this._eventBus.on('pagesinit', onPagesInit);
}); });
} }
@ -542,6 +548,32 @@ class PDFFindController {
} }
} }
_onFindBarClose(evt) {
const pdfDocument = this._pdfDocument;
// Since searching is asynchronous, ensure that the removal of highlighted
// matches (from the UI) is async too such that the 'updatetextlayermatches'
// events will always be dispatched in the expected order.
this._firstPagePromise.then(() => {
if (!this._pdfDocument ||
(pdfDocument && this._pdfDocument !== pdfDocument)) {
// Only update the UI if the document is open, and is the current one.
return;
}
if (this._findTimeout) {
clearTimeout(this._findTimeout);
this._findTimeout = null;
// Avoid the UI being in a pending state if the findbar is re-opened.
this._updateUIState(FindState.FOUND);
}
this._highlightMatches = false;
this._eventBus.dispatch('updatetextlayermatches', {
source: this,
pageIndex: -1,
});
});
}
_requestMatchesCount() { _requestMatchesCount() {
const { pageIdx, matchIdx, } = this._selected; const { pageIdx, matchIdx, } = this._selected;
let current = 0, total = this._matchesCountTotal; let current = 0, total = this._matchesCountTotal;