如何知道 pdfFindController.executeCommand 何时执行

How to know when pdfFindController.executeCommand has executed

我正在使用 Angular 7 构建一个网络应用程序,我尝试实现像 https://mozilla.github.io/pdf.js/web/viewer.html 中那样的搜索功能。

我可以正常使用,但我喜欢像在官方查看器中一样显示当前匹配和总匹配。我的代码如下所示:

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);

当我 运行 此代码 this.currentMatch 显示上一场比赛而不是当前比赛。如果我这样修改代码:

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);

然后 this.currentMatch 包含正确的比赛号码。所以我想我需要在调用 executeCommand 和 _requestMatcheCount 之间等待一些时间。我已经广泛搜索了代码,但我还没有找到一种方法来知道 executeCommand 何时完成以便可以安全地调用 _requestMatchesCount。谁能告诉我如何知道 executeCommand 已经完成?

到了,可能有点晚了!

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

如果您想知道在 pdf 上搜索是否没有任何结果,您必须检查状态。您还可以检查事件 'updatefindcontrolstate'。 状态如下:

const FindState = {
   FOUND: 0,
   NOT_FOUND: 1,
   WRAPPED: 2,
   PENDING: 3
};

因此,如果您使用 NOT_FOUND(1) 的值检查状态,那么您将能够知道搜索后是否找不到结果。

this.pdfComponent.pdfViewer.eventBus.on('updatefindcontrolstate', data => {
    if (data.state === FindState.NOT_FOUND) {
         // you have no results here
    }
});