Implement the setDocument method for the find controller

Now it follows the same pattern as e.g., the document properties
component, which allows us to have one instance of the find controller
and set a new document to search upon switching documents.

Moreover, this allows us to get rid of the dependency on `pdfViewer` in
order to fetch the text content for a page. This is working towards
getting rid of the `pdfViewer` dependency upon initializing the
component entirely in future commits.

Finally, we make the `reset` method private since it's not supposed to
be used from the outside anymore now that `setDocument` takes care of
this, similar to other components.
This commit is contained in:
Tim van der Meij 2018-09-21 17:19:33 +02:00
parent b14c1fbc28
commit e293c12afc
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
7 changed files with 59 additions and 38 deletions

View File

@ -55,7 +55,7 @@ container.addEventListener('pagesinit', function () {
pdfViewer.currentScaleValue = 'page-width';
if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
pdfFindController.executeCommand('find', { query: SEARCH_FOR, });
}
});
@ -70,4 +70,5 @@ pdfjsLib.getDocument({
pdfViewer.setDocument(pdfDocument);
pdfLinkService.setDocument(pdfDocument, null);
pdfFindController.setDocument(pdfDocument);
});

View File

@ -55,7 +55,7 @@ container.addEventListener('pagesinit', function () {
pdfSinglePageViewer.currentScaleValue = 'page-width';
if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
pdfFindController.executeCommand('find', { query: SEARCH_FOR, });
}
});
@ -70,4 +70,5 @@ pdfjsLib.getDocument({
pdfSinglePageViewer.setDocument(pdfDocument);
pdfLinkService.setDocument(pdfDocument, null);
pdfFindController.setDocument(pdfDocument);
});

View File

@ -57,7 +57,7 @@ container.addEventListener('pagesinit', function () {
pdfViewer.currentScaleValue = 'page-width';
if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
pdfFindController.executeCommand('find', { query: SEARCH_FOR, });
}
});
@ -72,4 +72,5 @@ pdfjsLib.getDocument({
pdfViewer.setDocument(pdfDocument);
pdfLinkService.setDocument(pdfDocument, null);
pdfFindController.setDocument(pdfDocument);
});

View File

@ -592,6 +592,7 @@ let PDFViewerApplication = {
if (this.pdfDocument) {
this.pdfDocument = null;
this.findController.setDocument(null);
this.pdfThumbnailViewer.setDocument(null);
this.pdfViewer.setDocument(null);
this.pdfLinkService.setDocument(null);
@ -608,7 +609,6 @@ let PDFViewerApplication = {
this.pdfOutlineViewer.reset();
this.pdfAttachmentViewer.reset();
this.findController.reset();
this.findBar.reset();
this.toolbar.reset();
this.secondaryToolbar.reset();
@ -916,6 +916,7 @@ let PDFViewerApplication = {
} else if (PDFJSDev.test('CHROME')) {
baseDocumentUrl = location.href.split('#')[0];
}
this.findController.setDocument(pdfDocument);
this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl);
this.pdfDocumentProperties.setDocument(pdfDocument, this.url);

View File

@ -913,14 +913,6 @@ class BaseViewer {
return false;
}
getPageTextContent(pageIndex) {
return this.pdfDocument.getPage(pageIndex + 1).then(function(page) {
return page.getTextContent({
normalizeWhitespace: true,
});
});
}
/**
* @param {HTMLDivElement} textLayerDiv
* @param {number} pageIndex

View File

@ -184,7 +184,7 @@ class PDFDocumentProperties {
* Note that the overlay will contain no information if this method
* is not called.
*
* @param {Object} pdfDocument - A reference to the PDF document.
* @param {PDFDocumentProxy} pdfDocument - A reference to the PDF document.
* @param {string} url - The URL of the document.
*/
setDocument(pdfDocument, url = null) {

View File

@ -51,7 +51,7 @@ class PDFFindController {
this.onUpdateResultsCount = null;
this.onUpdateState = null;
this.reset();
this._reset();
eventBus.on('findbarclose', () => {
this._highlightMatches = false;
@ -87,8 +87,51 @@ class PDFFindController {
return this._state;
}
reset() {
/**
* Set a reference to the PDF document in order to search it.
* Note that searching is not possible if this method is not called.
*
* @param {PDFDocumentProxy} pdfDocument - The PDF document to search.
*/
setDocument(pdfDocument) {
if (this._pdfDocument) {
this._reset();
}
if (!pdfDocument) {
return;
}
this._pdfDocument = pdfDocument;
}
executeCommand(cmd, state) {
if (!this._pdfDocument) {
return;
}
if (this._state === null || cmd !== 'findagain') {
this._dirtyMatch = true;
}
this._state = state;
this._updateUIState(FindState.PENDING);
this._firstPagePromise.then(() => {
this._extractText();
clearTimeout(this._findTimeout);
if (cmd === 'find') {
// Trigger the find action with a small delay to avoid starting the
// search when the user is still typing (saving resources).
this._findTimeout =
setTimeout(this._nextMatch.bind(this), FIND_TIMEOUT);
} else {
this._nextMatch();
}
});
}
_reset() {
this._highlightMatches = false;
this._pdfDocument = null;
this._pageMatches = [];
this._pageMatchesLength = null;
this._state = null;
@ -118,28 +161,6 @@ class PDFFindController {
});
}
executeCommand(cmd, state) {
if (this._state === null || cmd !== 'findagain') {
this._dirtyMatch = true;
}
this._state = state;
this._updateUIState(FindState.PENDING);
this._firstPagePromise.then(() => {
this._extractText();
clearTimeout(this._findTimeout);
if (cmd === 'find') {
// Trigger the find action with a small delay to avoid starting the
// search when the user is still typing (saving resources).
this._findTimeout =
setTimeout(this._nextMatch.bind(this), FIND_TIMEOUT);
} else {
this._nextMatch();
}
});
}
_normalize(text) {
return text.replace(this._normalizationRegex, function(ch) {
return CHARACTERS_TO_NORMALIZE[ch];
@ -326,7 +347,11 @@ class PDFFindController {
this._extractTextPromises[i] = extractTextCapability.promise;
promise = promise.then(() => {
return this._pdfViewer.getPageTextContent(i).then((textContent) => {
return this._pdfDocument.getPage(i + 1).then((pdfPage) => {
return pdfPage.getTextContent({
normalizeWhitespace: true,
});
}).then((textContent) => {
const textItems = textContent.items;
const strBuf = [];