Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when pdfFindController.executeCommand has executed

Tags:

pdf.js

I am building a webapp with Angular 7 and I try to implement search functionality like in https://mozilla.github.io/pdf.js/web/viewer.html.

I have it working, but I like to show current match and total matches like in the official viewer. My code looks like this:

this.pdfFindController.executeCommand('find', this.searchCommand);
let { current, total, } = this.pdfFindController._requestMatchesCount();
this.currentMatch = current;
this.numberOfSearchMatches = total;
console.log('Number of matches', this.currentMatch, '/', 
this.numberOfSearchMatches);

When I run this code this.currentMatch is showing the previous match instead of the current match. If I modify the code like this:

this.pdfFindController.executeCommand('find', this.searchCommand);
setTimeout(() => {    
let { current, total, } = 
    this.pdfFindController._requestMatchesCount();
    this.currentMatch = current;
    this.numberOfSearchMatches = total;
    console.log('Number of matches', this.currentMatch, '/', 
    this.numberOfSearchMatches);
}, 1000);

then this.currentMatch contains the correct match number. So I guess I need to wait for something between calling executeCommand and _requestMatcheCount. I have searched the code extensively, but I have not found a way to know when executeCommand has finished so _requestMatchesCount can be safely called. Can anybody tell me how I can know that executeCommand has finished?

like image 887
Jørgen Rasmussen Avatar asked Mar 12 '19 18:03

Jørgen Rasmussen


1 Answers

Here we are, maybe a little late!

this.pdfComponent.pdfViewer.eventBus.on('updatefindmatchescount', data => { this.searchMatchesLength = data.matchesCount.total; });

like image 131
Alberto M. Avatar answered Jan 01 '23 10:01

Alberto M.