user1919163
user1919163

Reputation: 33

I can not see the meaning of this serious piece of js code

These lines are in the Mozilla pdf js viewer. They should be correct. But to me they look like:

a = b;
if (b !== a) {
    // do something
}

Here are the lines:

function webViewerPageNumberChanged(evt) {
    var pdfViewer = PDFViewerApplication.pdfViewer;
    pdfViewer.currentPageLabel = evt.value;
    if (evt.value !== pdfViewer.currentPageNumber.toString() && evt.value !== pdfViewer.currentPageLabel) {
        PDFViewerApplication.toolbar.setPageNumber(pdfViewer.currentPageNumber, pdfViewer.currentPageLabel);
    }
}

Could somebody shed some light?


The above code lines are in viewer.js in pdfjs.1.9.426_dist - line 2194. Judging by the following getters/setters at line 8842 Ivan's answer seems to be correct:

}, {
    key: 'currentPageNumber',
    get: function get() {
        return this._currentPageNumber;
    },
    set: function set(val) {
        if ((val | 0) !== val) {
            throw new Error('Invalid page number.');
        }
        if (!this.pdfDocument) {
            return;
        }
        this._setCurrentPageNumber(val, true);
    }
}, {
    key: 'currentPageLabel',
    get: function get() {
        return this._pageLabels && this._pageLabels[this._currentPageNumber - 1];
    },
    set: function set(val) {
        var pageNumber = val | 0;
        if (this._pageLabels) {
            var i = this._pageLabels.indexOf(val);
            if (i >= 0) {
                pageNumber = i + 1;
            }
        }
        this.currentPageNumber = pageNumber;
    }
}, {

Upvotes: 2

Views: 242

Answers (2)

Ivan
Ivan

Reputation: 40708

On line 1991 of pdf.js/app.js a comment just before the if statement reads:

Ensure that the page number input displays the correct value, even if the value entered by the user was invalid (e.g. a floating point number).

So maybe NaN could be a potential value for evt.value. In this case as Jonas W. said evt.value !== pdfViewer.currentPageLabel would return false.

function webViewerPageNumberChanged(evt) {
  let pdfViewer = PDFViewerApplication.pdfViewer;
  pdfViewer.currentPageLabel = evt.value;

  // Ensure that the page number input displays the correct value, even if the
  // value entered by the user was invalid (e.g. a floating point number).
  if (evt.value !== pdfViewer.currentPageNumber.toString() &&
      evt.value !== pdfViewer.currentPageLabel) {
    PDFViewerApplication.toolbar.setPageNumber(
      pdfViewer.currentPageNumber, pdfViewer.currentPageLabel);
  }
}

I hope this will help.


Edit: Indeed user1919163, you were right.

I got this response after making a pull requestion to change line 1998:

I don't think this fix is correct. While it looks like we're comparing the variable to itself, currentPageNumber is actually a quite elaborate setter; see

set currentPageNumber(val) {
  if (!Number.isInteger(val)) {
    throw new Error('Invalid page number.');
  }
  if (!this.pdfDocument) {
    return;
  }
  // The intent can be to just reset a scroll position and/or scale. 
  this._setCurrentPageNumber(val, /* resetCurrentPageView = */ true);
}

/** 
 * @private 
 */
_setCurrentPageNumber(val, resetCurrentPageView = false) {
  if (this._currentPageNumber === val) {
    if (resetCurrentPageView) {
      this._resetCurrentPageView();
    }
    return;
  }

  if (!(0 < val && val <= this.pagesCount)) {
    console.error(
      `${this._name}._setCurrentPageNumber: "${val}" is out of bounds.`);
    return;
  }

  let arg = {
    source: this,
    pageNumber: val,
    pageLabel: this._pageLabels && this._pageLabels[val - 1],
  };
  this._currentPageNumber = val;
  this.eventBus.dispatch('pagechanging', arg);
  this.eventBus.dispatch('pagechange', arg);

  if (resetCurrentPageView) {
    this._resetCurrentPageView();
  }
}

Therefore, it does more than just an assignment. After the logic is complete, we want to check if the condition holds.

Unless there is a visible bug (and if so, please open an issue for it), I think this is fine.

Upvotes: 1

colxi
colxi

Reputation: 8720

This function is probably binded to a scroll event. When its executed checks if the current page matches the displayed page. If not, it updates the displayed page.

I asume evt.value contains the currentPage number.

if(currentPageNumber != displayedPageNumber){
    /* update page number*/
}

Upvotes: 0

Related Questions