2013-06-19 01:05:55 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 20:00:16 +09:00
|
|
|
import { getGlobalEventBus, scrollIntoView } from "./ui_utils.js";
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
import { createPromiseCapability } from "pdfjs-lib";
|
2020-01-02 20:00:16 +09:00
|
|
|
import { getCharacterType } from "./pdf_find_utils.js";
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
const FindState = {
|
|
|
|
FOUND: 0,
|
|
|
|
NOT_FOUND: 1,
|
|
|
|
WRAPPED: 2,
|
|
|
|
PENDING: 3,
|
2014-09-15 23:49:24 +09:00
|
|
|
};
|
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
const FIND_TIMEOUT = 250; // ms
|
Only scroll search results into view as a result of an actual find operation, and not when the user scrolls/zooms/rotates the document (bug 1237076, issue 6746)
Currently searching, and particularily highlighting of search results, may interfere with subsequent user-interactions such as scrolling/zooming/rotating which can result in a somewhat jarring UX where the document suddenly "jumps" to a previous position.
This is especially annoying in cases where the highlighted search result isn't even visible when a user initiated scrolling/zooming/rotating happens, and there exists a couple of bugs/issues about this behaviour.
It seems reasonable, as far as I'm concerned, to treat searching as one operation and any subsequent non-search user interactions with the viewer as separate and thus not scroll the current search result into view *unless* the user is actually doing another search.
This also seems consistent with general searching in e.g. Firefox and Adobe Reader:
- Compare with "regular" searching of e.g. HTML files in Firefox, where the user scrolling and/or zooming the document will not force a currently highlighted search result to become re-scrolled into view.
- Compare also with Adobe Reader, where the user scrolling, zooming, and/or rotating the document will not force the currently highlighted search result to become re-scrolled into view.
The question is then why search highlighting was implemented this way in PDF.js to begin with. It might be that this wasn't really intended behaviour, but more a consequence of the asynchronous nature of the API. Considering that most operations, such as fetching the page, rendering it and extracting its text-content are all asynchronous; searching and highlighting of matches thus becomes asynchronous too.
However, it should be possible to track when search results have been scrolled into view and highlighted, and thus prevent these wierd "jumps" when the user interacts with the document.
*Please note:* Unfortunately this required moving the scrolling of matches back into `PDFFindController`, since I simply couldn't see any other (reasonable) way of implementing the functionality without tracking the `_shouldScroll` property in only *one* spot.
However, given that the new `PDFFindController.scrollMatchIntoView` method follows a similar pattern as `BaseViewer.scrollPageIntoView` and `PDFThumbnailViewer.scrollThumbnailIntoView`, this is hopefully deemed OK.
2018-10-30 19:08:09 +09:00
|
|
|
const MATCH_SCROLL_OFFSET_TOP = -50; // px
|
|
|
|
const MATCH_SCROLL_OFFSET_LEFT = -400; // px
|
2014-12-18 05:12:51 +09:00
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
const CHARACTERS_TO_NORMALIZE = {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
"\u2018": "'", // Left single quotation mark
|
|
|
|
"\u2019": "'", // Right single quotation mark
|
|
|
|
"\u201A": "'", // Single low-9 quotation mark
|
|
|
|
"\u201B": "'", // Single high-reversed-9 quotation mark
|
|
|
|
"\u201C": '"', // Left double quotation mark
|
|
|
|
"\u201D": '"', // Right double quotation mark
|
|
|
|
"\u201E": '"', // Double low-9 quotation mark
|
|
|
|
"\u201F": '"', // Double high-reversed-9 quotation mark
|
|
|
|
"\u00BC": "1/4", // Vulgar fraction one quarter
|
|
|
|
"\u00BD": "1/2", // Vulgar fraction one half
|
|
|
|
"\u00BE": "3/4", // Vulgar fraction three quarters
|
2016-02-27 02:06:59 +09:00
|
|
|
};
|
|
|
|
|
2018-10-27 01:22:32 +09:00
|
|
|
let normalizationRegex = null;
|
|
|
|
function normalize(text) {
|
|
|
|
if (!normalizationRegex) {
|
|
|
|
// Compile the regular expression for text normalization once.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const replace = Object.keys(CHARACTERS_TO_NORMALIZE).join("");
|
|
|
|
normalizationRegex = new RegExp(`[${replace}]`, "g");
|
2018-10-27 01:22:32 +09:00
|
|
|
}
|
|
|
|
return text.replace(normalizationRegex, function(ch) {
|
|
|
|
return CHARACTERS_TO_NORMALIZE[ch];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-22 02:40:11 +09:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PDFFindControllerOptions
|
|
|
|
* @property {IPDFLinkService} linkService - The navigation/linking service.
|
|
|
|
* @property {EventBus} eventBus - The application event bus.
|
|
|
|
*/
|
|
|
|
|
2013-06-19 01:05:55 +09:00
|
|
|
/**
|
2017-06-05 03:35:21 +09:00
|
|
|
* Provides search functionality to find a given string in a PDF document.
|
2013-06-19 01:05:55 +09:00
|
|
|
*/
|
2017-06-05 03:35:21 +09:00
|
|
|
class PDFFindController {
|
2018-09-22 02:40:11 +09:00
|
|
|
/**
|
|
|
|
* @param {PDFFindControllerOptions} options
|
|
|
|
*/
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
constructor({ linkService, eventBus = getGlobalEventBus() }) {
|
2018-09-22 02:40:11 +09:00
|
|
|
this._linkService = linkService;
|
2018-09-12 22:10:45 +09:00
|
|
|
this._eventBus = eventBus;
|
2016-04-14 04:39:29 +09:00
|
|
|
|
2018-09-22 00:19:33 +09:00
|
|
|
this._reset();
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
eventBus.on("findbarclose", this._onFindBarClose.bind(this));
|
2018-09-12 22:10:45 +09:00
|
|
|
}
|
|
|
|
|
2018-09-22 22:31:33 +09:00
|
|
|
get highlightMatches() {
|
|
|
|
return this._highlightMatches;
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:10:45 +09:00
|
|
|
get pageMatches() {
|
|
|
|
return this._pageMatches;
|
|
|
|
}
|
|
|
|
|
|
|
|
get pageMatchesLength() {
|
|
|
|
return this._pageMatchesLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
get selected() {
|
|
|
|
return this._selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
get state() {
|
|
|
|
return this._state;
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-09-22 00:19:33 +09:00
|
|
|
/**
|
|
|
|
* 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;
|
Make `PDFFindController` less confusing to use, by allowing searching to start when `setDocument` is called
*This patch is based on something that I noticed while working on PR 10126.*
The recent re-factoring of `PDFFindController` brought many improvements, among those the fact that access to `BaseViewer` is no longer required. However, with these changes there's one thing which now strikes me as not particularly user-friendly[1]: The fact that in order for searching to actually work, `PDFFindController.setDocument` must be called *and* a 'pagesinit' event must be dispatched (from somewhere).
For all other viewer components, calling the `setDocument` method[2] is enough in order for the component to actually be usable.
The `PDFFindController` thus stands out quite a bit, and it also becomes difficult to work with in any sort of custom implementation. For example: Imagine someone trying to use `PDFFindController` separately from the viewer[3], which *should* now be relatively simple given the re-factoring, and thus having to (somehow) figure out that they'll also need to manually dispatch a 'pagesinit' event for searching to work.
Note that the above even affects the unit-tests, where an out-of-place 'pagesinit' event is being used.
To attempt to address these problems, I'm thus suggesting that *only* `setDocument` should be used to indicate that searching may start. For the default viewer and/or the viewer components, `BaseViewer.setDocument` will now call `PDFFindController.setDocument` when the document is ready, thus requiring no outside configuration anymore[4]. For custom implementation, and the unit-tests, it's now as simple as just calling `PDFFindController.setDocument` to allow searching to start.
---
[1] I should have caught this during review of PR 10099, but unfortunately it's sometimes not until you actually work with the code in question that things like these become clear.
[2] Assuming, obviously, that the viewer component in question actually implements such a method :-)
[3] There's even a very recent issue, filed by someone trying to do just that.
[4] Short of providing a `PDFFindController` instance when creating a `BaseViewer` instance, of course.
2018-10-03 19:42:41 +09:00
|
|
|
this._firstPageCapability.resolve();
|
2018-09-22 00:19:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
executeCommand(cmd, state) {
|
2018-11-05 19:10:31 +09:00
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-02 00:41:57 +09:00
|
|
|
const pdfDocument = this._pdfDocument;
|
2018-09-22 00:19:33 +09:00
|
|
|
|
2018-11-05 19:05:42 +09:00
|
|
|
if (this._state === null || this._shouldDirtyMatch(cmd, state)) {
|
2018-09-22 00:19:33 +09:00
|
|
|
this._dirtyMatch = true;
|
|
|
|
}
|
|
|
|
this._state = state;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (cmd !== "findhighlightallchange") {
|
2018-10-30 19:08:16 +09:00
|
|
|
this._updateUIState(FindState.PENDING);
|
|
|
|
}
|
2018-09-22 00:19:33 +09:00
|
|
|
|
Make `PDFFindController` less confusing to use, by allowing searching to start when `setDocument` is called
*This patch is based on something that I noticed while working on PR 10126.*
The recent re-factoring of `PDFFindController` brought many improvements, among those the fact that access to `BaseViewer` is no longer required. However, with these changes there's one thing which now strikes me as not particularly user-friendly[1]: The fact that in order for searching to actually work, `PDFFindController.setDocument` must be called *and* a 'pagesinit' event must be dispatched (from somewhere).
For all other viewer components, calling the `setDocument` method[2] is enough in order for the component to actually be usable.
The `PDFFindController` thus stands out quite a bit, and it also becomes difficult to work with in any sort of custom implementation. For example: Imagine someone trying to use `PDFFindController` separately from the viewer[3], which *should* now be relatively simple given the re-factoring, and thus having to (somehow) figure out that they'll also need to manually dispatch a 'pagesinit' event for searching to work.
Note that the above even affects the unit-tests, where an out-of-place 'pagesinit' event is being used.
To attempt to address these problems, I'm thus suggesting that *only* `setDocument` should be used to indicate that searching may start. For the default viewer and/or the viewer components, `BaseViewer.setDocument` will now call `PDFFindController.setDocument` when the document is ready, thus requiring no outside configuration anymore[4]. For custom implementation, and the unit-tests, it's now as simple as just calling `PDFFindController.setDocument` to allow searching to start.
---
[1] I should have caught this during review of PR 10099, but unfortunately it's sometimes not until you actually work with the code in question that things like these become clear.
[2] Assuming, obviously, that the viewer component in question actually implements such a method :-)
[3] There's even a very recent issue, filed by someone trying to do just that.
[4] Short of providing a `PDFFindController` instance when creating a `BaseViewer` instance, of course.
2018-10-03 19:42:41 +09:00
|
|
|
this._firstPageCapability.promise.then(() => {
|
2018-10-26 18:56:25 +09:00
|
|
|
// If the document was closed before searching began, or if the search
|
|
|
|
// operation was relevant for a previously opened document, do nothing.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
|
|
|
!this._pdfDocument ||
|
|
|
|
(pdfDocument && this._pdfDocument !== pdfDocument)
|
|
|
|
) {
|
2018-10-02 00:41:57 +09:00
|
|
|
return;
|
|
|
|
}
|
2018-09-22 00:19:33 +09:00
|
|
|
this._extractText();
|
|
|
|
|
2018-10-30 19:08:03 +09:00
|
|
|
const findbarClosed = !this._highlightMatches;
|
2018-10-30 19:08:16 +09:00
|
|
|
const pendingTimeout = !!this._findTimeout;
|
2018-10-30 19:08:03 +09:00
|
|
|
|
2018-10-02 00:41:57 +09:00
|
|
|
if (this._findTimeout) {
|
|
|
|
clearTimeout(this._findTimeout);
|
|
|
|
this._findTimeout = null;
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (cmd === "find") {
|
2018-09-22 00:19:33 +09:00
|
|
|
// Trigger the find action with a small delay to avoid starting the
|
|
|
|
// search when the user is still typing (saving resources).
|
2018-10-02 00:41:57 +09:00
|
|
|
this._findTimeout = setTimeout(() => {
|
|
|
|
this._nextMatch();
|
|
|
|
this._findTimeout = null;
|
|
|
|
}, FIND_TIMEOUT);
|
2018-10-30 19:08:03 +09:00
|
|
|
} else if (this._dirtyMatch) {
|
|
|
|
// Immediately trigger searching for non-'find' operations, when the
|
|
|
|
// current state needs to be reset and matches re-calculated.
|
|
|
|
this._nextMatch();
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
} else if (cmd === "findagain") {
|
2018-10-30 00:15:28 +09:00
|
|
|
this._nextMatch();
|
|
|
|
|
|
|
|
// When the findbar was previously closed, and `highlightAll` is set,
|
|
|
|
// ensure that the matches on all active pages are highlighted again.
|
2018-10-30 19:08:03 +09:00
|
|
|
if (findbarClosed && this._state.highlightAll) {
|
2018-10-30 00:15:28 +09:00
|
|
|
this._updateAllPages();
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
} else if (cmd === "findhighlightallchange") {
|
2018-10-30 19:08:16 +09:00
|
|
|
// If there was a pending search operation, synchronously trigger a new
|
|
|
|
// search *first* to ensure that the correct matches are highlighted.
|
|
|
|
if (pendingTimeout) {
|
|
|
|
this._nextMatch();
|
|
|
|
} else {
|
|
|
|
this._highlightMatches = true;
|
|
|
|
}
|
|
|
|
this._updateAllPages(); // Update the highlighting on all active pages.
|
2018-09-22 00:19:33 +09:00
|
|
|
} else {
|
|
|
|
this._nextMatch();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
scrollMatchIntoView({ element = null, pageIndex = -1, matchIndex = -1 }) {
|
Only scroll search results into view as a result of an actual find operation, and not when the user scrolls/zooms/rotates the document (bug 1237076, issue 6746)
Currently searching, and particularily highlighting of search results, may interfere with subsequent user-interactions such as scrolling/zooming/rotating which can result in a somewhat jarring UX where the document suddenly "jumps" to a previous position.
This is especially annoying in cases where the highlighted search result isn't even visible when a user initiated scrolling/zooming/rotating happens, and there exists a couple of bugs/issues about this behaviour.
It seems reasonable, as far as I'm concerned, to treat searching as one operation and any subsequent non-search user interactions with the viewer as separate and thus not scroll the current search result into view *unless* the user is actually doing another search.
This also seems consistent with general searching in e.g. Firefox and Adobe Reader:
- Compare with "regular" searching of e.g. HTML files in Firefox, where the user scrolling and/or zooming the document will not force a currently highlighted search result to become re-scrolled into view.
- Compare also with Adobe Reader, where the user scrolling, zooming, and/or rotating the document will not force the currently highlighted search result to become re-scrolled into view.
The question is then why search highlighting was implemented this way in PDF.js to begin with. It might be that this wasn't really intended behaviour, but more a consequence of the asynchronous nature of the API. Considering that most operations, such as fetching the page, rendering it and extracting its text-content are all asynchronous; searching and highlighting of matches thus becomes asynchronous too.
However, it should be possible to track when search results have been scrolled into view and highlighted, and thus prevent these wierd "jumps" when the user interacts with the document.
*Please note:* Unfortunately this required moving the scrolling of matches back into `PDFFindController`, since I simply couldn't see any other (reasonable) way of implementing the functionality without tracking the `_shouldScroll` property in only *one* spot.
However, given that the new `PDFFindController.scrollMatchIntoView` method follows a similar pattern as `BaseViewer.scrollPageIntoView` and `PDFThumbnailViewer.scrollThumbnailIntoView`, this is hopefully deemed OK.
2018-10-30 19:08:09 +09:00
|
|
|
if (!this._scrollMatches || !element) {
|
|
|
|
return;
|
|
|
|
} else if (matchIndex === -1 || matchIndex !== this._selected.matchIdx) {
|
|
|
|
return;
|
|
|
|
} else if (pageIndex === -1 || pageIndex !== this._selected.pageIdx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._scrollMatches = false; // Ensure that scrolling only happens once.
|
|
|
|
|
|
|
|
const spot = {
|
|
|
|
top: MATCH_SCROLL_OFFSET_TOP,
|
|
|
|
left: MATCH_SCROLL_OFFSET_LEFT,
|
|
|
|
};
|
|
|
|
scrollIntoView(element, spot, /* skipOverflowHiddenElements = */ true);
|
|
|
|
}
|
|
|
|
|
2018-09-22 00:19:33 +09:00
|
|
|
_reset() {
|
2018-09-22 22:31:33 +09:00
|
|
|
this._highlightMatches = false;
|
Only scroll search results into view as a result of an actual find operation, and not when the user scrolls/zooms/rotates the document (bug 1237076, issue 6746)
Currently searching, and particularily highlighting of search results, may interfere with subsequent user-interactions such as scrolling/zooming/rotating which can result in a somewhat jarring UX where the document suddenly "jumps" to a previous position.
This is especially annoying in cases where the highlighted search result isn't even visible when a user initiated scrolling/zooming/rotating happens, and there exists a couple of bugs/issues about this behaviour.
It seems reasonable, as far as I'm concerned, to treat searching as one operation and any subsequent non-search user interactions with the viewer as separate and thus not scroll the current search result into view *unless* the user is actually doing another search.
This also seems consistent with general searching in e.g. Firefox and Adobe Reader:
- Compare with "regular" searching of e.g. HTML files in Firefox, where the user scrolling and/or zooming the document will not force a currently highlighted search result to become re-scrolled into view.
- Compare also with Adobe Reader, where the user scrolling, zooming, and/or rotating the document will not force the currently highlighted search result to become re-scrolled into view.
The question is then why search highlighting was implemented this way in PDF.js to begin with. It might be that this wasn't really intended behaviour, but more a consequence of the asynchronous nature of the API. Considering that most operations, such as fetching the page, rendering it and extracting its text-content are all asynchronous; searching and highlighting of matches thus becomes asynchronous too.
However, it should be possible to track when search results have been scrolled into view and highlighted, and thus prevent these wierd "jumps" when the user interacts with the document.
*Please note:* Unfortunately this required moving the scrolling of matches back into `PDFFindController`, since I simply couldn't see any other (reasonable) way of implementing the functionality without tracking the `_shouldScroll` property in only *one* spot.
However, given that the new `PDFFindController.scrollMatchIntoView` method follows a similar pattern as `BaseViewer.scrollPageIntoView` and `PDFThumbnailViewer.scrollThumbnailIntoView`, this is hopefully deemed OK.
2018-10-30 19:08:09 +09:00
|
|
|
this._scrollMatches = false;
|
2018-09-22 00:19:33 +09:00
|
|
|
this._pdfDocument = null;
|
2018-09-12 22:10:45 +09:00
|
|
|
this._pageMatches = [];
|
2018-10-25 23:21:55 +09:00
|
|
|
this._pageMatchesLength = [];
|
2018-09-12 22:10:45 +09:00
|
|
|
this._state = null;
|
2019-12-26 04:03:46 +09:00
|
|
|
// Currently selected match.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._selected = {
|
2017-06-05 03:35:21 +09:00
|
|
|
pageIdx: -1,
|
|
|
|
matchIdx: -1,
|
|
|
|
};
|
2019-12-26 04:03:46 +09:00
|
|
|
// Where the find algorithm currently is in the document.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._offset = {
|
2017-06-05 03:35:21 +09:00
|
|
|
pageIdx: null,
|
|
|
|
matchIdx: null,
|
2018-10-26 18:56:25 +09:00
|
|
|
wrapped: false,
|
2017-06-05 03:35:21 +09:00
|
|
|
};
|
2018-09-12 22:10:45 +09:00
|
|
|
this._extractTextPromises = [];
|
2018-10-27 03:23:32 +09:00
|
|
|
this._pageContents = []; // Stores the normalized text for each page.
|
2018-09-12 22:10:45 +09:00
|
|
|
this._matchesCountTotal = 0;
|
|
|
|
this._pagesToSearch = null;
|
|
|
|
this._pendingFindMatches = Object.create(null);
|
|
|
|
this._resumePageIdx = null;
|
|
|
|
this._dirtyMatch = false;
|
2018-10-02 00:41:57 +09:00
|
|
|
clearTimeout(this._findTimeout);
|
2018-09-12 22:10:45 +09:00
|
|
|
this._findTimeout = null;
|
2017-06-05 03:35:21 +09:00
|
|
|
|
Make `PDFFindController` less confusing to use, by allowing searching to start when `setDocument` is called
*This patch is based on something that I noticed while working on PR 10126.*
The recent re-factoring of `PDFFindController` brought many improvements, among those the fact that access to `BaseViewer` is no longer required. However, with these changes there's one thing which now strikes me as not particularly user-friendly[1]: The fact that in order for searching to actually work, `PDFFindController.setDocument` must be called *and* a 'pagesinit' event must be dispatched (from somewhere).
For all other viewer components, calling the `setDocument` method[2] is enough in order for the component to actually be usable.
The `PDFFindController` thus stands out quite a bit, and it also becomes difficult to work with in any sort of custom implementation. For example: Imagine someone trying to use `PDFFindController` separately from the viewer[3], which *should* now be relatively simple given the re-factoring, and thus having to (somehow) figure out that they'll also need to manually dispatch a 'pagesinit' event for searching to work.
Note that the above even affects the unit-tests, where an out-of-place 'pagesinit' event is being used.
To attempt to address these problems, I'm thus suggesting that *only* `setDocument` should be used to indicate that searching may start. For the default viewer and/or the viewer components, `BaseViewer.setDocument` will now call `PDFFindController.setDocument` when the document is ready, thus requiring no outside configuration anymore[4]. For custom implementation, and the unit-tests, it's now as simple as just calling `PDFFindController.setDocument` to allow searching to start.
---
[1] I should have caught this during review of PR 10099, but unfortunately it's sometimes not until you actually work with the code in question that things like these become clear.
[2] Assuming, obviously, that the viewer component in question actually implements such a method :-)
[3] There's even a very recent issue, filed by someone trying to do just that.
[4] Short of providing a `PDFFindController` instance when creating a `BaseViewer` instance, of course.
2018-10-03 19:42:41 +09:00
|
|
|
this._firstPageCapability = createPromiseCapability();
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
2016-02-27 02:06:59 +09:00
|
|
|
|
2018-10-27 03:53:42 +09:00
|
|
|
/**
|
2019-10-13 00:02:54 +09:00
|
|
|
* @type {string} The (current) normalized search query.
|
2018-10-27 03:53:42 +09:00
|
|
|
*/
|
|
|
|
get _query() {
|
|
|
|
if (this._state.query !== this._rawQuery) {
|
|
|
|
this._rawQuery = this._state.query;
|
|
|
|
this._normalizedQuery = normalize(this._state.query);
|
|
|
|
}
|
|
|
|
return this._normalizedQuery;
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:05:42 +09:00
|
|
|
_shouldDirtyMatch(cmd, state) {
|
|
|
|
// When the search query changes, regardless of the actual search command
|
|
|
|
// used, always re-calculate matches to avoid errors (fixes bug 1030622).
|
|
|
|
if (state.query !== this._state.query) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-03 19:52:48 +09:00
|
|
|
switch (cmd) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
case "findagain":
|
2018-10-30 19:08:26 +09:00
|
|
|
const pageNumber = this._selected.pageIdx + 1;
|
|
|
|
const linkService = this._linkService;
|
|
|
|
// Only treat a 'findagain' event as a new search operation when it's
|
|
|
|
// *absolutely* certain that the currently selected match is no longer
|
|
|
|
// visible, e.g. as a result of the user scrolling in the document.
|
|
|
|
//
|
|
|
|
// NOTE: If only a simple `this._linkService.page` check was used here,
|
|
|
|
// there's a risk that consecutive 'findagain' operations could "skip"
|
|
|
|
// over matches at the top/bottom of pages thus making them completely
|
|
|
|
// inaccessible when there's multiple pages visible in the viewer.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
|
|
|
pageNumber >= 1 &&
|
|
|
|
pageNumber <= linkService.pagesCount &&
|
|
|
|
pageNumber !== linkService.page &&
|
|
|
|
!linkService.isPageVisible(pageNumber)
|
|
|
|
) {
|
2019-06-11 00:11:21 +09:00
|
|
|
return true;
|
2018-10-30 19:08:26 +09:00
|
|
|
}
|
2018-11-03 19:52:48 +09:00
|
|
|
return false;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
case "findhighlightallchange":
|
2018-10-30 19:08:16 +09:00
|
|
|
return false;
|
2018-11-03 19:52:48 +09:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
/**
|
|
|
|
* Helper for multi-term search that fills the `matchesWithLength` array
|
|
|
|
* and handles cases where one search term includes another search term (for
|
|
|
|
* example, "tamed tame" or "this is"). It looks for intersecting terms in
|
|
|
|
* the `matches` and keeps elements with a longer match length.
|
|
|
|
*/
|
|
|
|
_prepareMatches(matchesWithLength, matches, matchesLength) {
|
|
|
|
function isSubTerm(matchesWithLength, currentIndex) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const currentElem = matchesWithLength[currentIndex];
|
|
|
|
const nextElem = matchesWithLength[currentIndex + 1];
|
2017-06-05 03:35:21 +09:00
|
|
|
|
|
|
|
// Check for cases like "TAMEd TAME".
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
|
|
|
currentIndex < matchesWithLength.length - 1 &&
|
|
|
|
currentElem.match === nextElem.match
|
|
|
|
) {
|
2017-06-05 03:35:21 +09:00
|
|
|
currentElem.skipped = true;
|
|
|
|
return true;
|
2016-05-26 22:24:58 +09:00
|
|
|
}
|
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
// Check for cases like "thIS IS".
|
|
|
|
for (let i = currentIndex - 1; i >= 0; i--) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const prevElem = matchesWithLength[i];
|
2017-06-05 03:35:21 +09:00
|
|
|
if (prevElem.skipped) {
|
2016-05-26 22:24:58 +09:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
if (prevElem.match + prevElem.matchLength < currentElem.match) {
|
2016-05-26 22:24:58 +09:00
|
|
|
break;
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
|
|
|
prevElem.match + prevElem.matchLength >=
|
|
|
|
currentElem.match + currentElem.matchLength
|
|
|
|
) {
|
2017-06-05 03:35:21 +09:00
|
|
|
currentElem.skipped = true;
|
|
|
|
return true;
|
2016-05-26 22:24:58 +09:00
|
|
|
}
|
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the array of `{ match: <match>, matchLength: <matchLength> }`
|
|
|
|
// objects on increasing index first and on the length otherwise.
|
|
|
|
matchesWithLength.sort(function(a, b) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
return a.match === b.match
|
|
|
|
? a.matchLength - b.matchLength
|
|
|
|
: a.match - b.match;
|
2017-06-05 03:35:21 +09:00
|
|
|
});
|
|
|
|
for (let i = 0, len = matchesWithLength.length; i < len; i++) {
|
|
|
|
if (isSubTerm(matchesWithLength, i)) {
|
|
|
|
continue;
|
2013-06-19 01:05:55 +09:00
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
matches.push(matchesWithLength[i].match);
|
|
|
|
matchesLength.push(matchesWithLength[i].matchLength);
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-09-01 08:28:19 +09:00
|
|
|
/**
|
|
|
|
* Determine if the search query constitutes a "whole word", by comparing the
|
|
|
|
* first/last character type with the preceding/following character type.
|
|
|
|
*/
|
|
|
|
_isEntireWord(content, startIdx, length) {
|
|
|
|
if (startIdx > 0) {
|
|
|
|
const first = content.charCodeAt(startIdx);
|
|
|
|
const limit = content.charCodeAt(startIdx - 1);
|
|
|
|
if (getCharacterType(first) === getCharacterType(limit)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const endIdx = startIdx + length - 1;
|
|
|
|
if (endIdx < content.length - 1) {
|
2018-09-01 08:28:19 +09:00
|
|
|
const last = content.charCodeAt(endIdx);
|
|
|
|
const limit = content.charCodeAt(endIdx + 1);
|
|
|
|
if (getCharacterType(last) === getCharacterType(limit)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_calculatePhraseMatch(query, pageIndex, pageContent, entireWord) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const matches = [];
|
|
|
|
const queryLen = query.length;
|
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
let matchIdx = -queryLen;
|
|
|
|
while (true) {
|
|
|
|
matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
|
|
|
|
if (matchIdx === -1) {
|
|
|
|
break;
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
2018-09-01 08:28:19 +09:00
|
|
|
if (entireWord && !this._isEntireWord(pageContent, matchIdx, queryLen)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
matches.push(matchIdx);
|
|
|
|
}
|
2018-09-12 22:10:45 +09:00
|
|
|
this._pageMatches[pageIndex] = matches;
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-09-01 08:28:19 +09:00
|
|
|
_calculateWordMatch(query, pageIndex, pageContent, entireWord) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const matchesWithLength = [];
|
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
// Divide the query into pieces and search for text in each piece.
|
2018-09-12 22:25:46 +09:00
|
|
|
const queryArray = query.match(/\S+/g);
|
2017-06-05 03:35:21 +09:00
|
|
|
for (let i = 0, len = queryArray.length; i < len; i++) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const subquery = queryArray[i];
|
|
|
|
const subqueryLen = subquery.length;
|
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
let matchIdx = -subqueryLen;
|
|
|
|
while (true) {
|
|
|
|
matchIdx = pageContent.indexOf(subquery, matchIdx + subqueryLen);
|
|
|
|
if (matchIdx === -1) {
|
|
|
|
break;
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
|
|
|
entireWord &&
|
|
|
|
!this._isEntireWord(pageContent, matchIdx, subqueryLen)
|
|
|
|
) {
|
2018-09-01 08:28:19 +09:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
// Other searches do not, so we store the length.
|
|
|
|
matchesWithLength.push({
|
|
|
|
match: matchIdx,
|
|
|
|
matchLength: subqueryLen,
|
|
|
|
skipped: false,
|
|
|
|
});
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare arrays for storing the matches.
|
2018-09-12 22:10:45 +09:00
|
|
|
this._pageMatchesLength[pageIndex] = [];
|
|
|
|
this._pageMatches[pageIndex] = [];
|
2017-06-05 03:35:21 +09:00
|
|
|
|
|
|
|
// Sort `matchesWithLength`, remove intersecting terms and put the result
|
|
|
|
// into the two arrays.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._prepareMatches(
|
|
|
|
matchesWithLength,
|
|
|
|
this._pageMatches[pageIndex],
|
|
|
|
this._pageMatchesLength[pageIndex]
|
|
|
|
);
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
2016-05-26 22:24:58 +09:00
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_calculateMatch(pageIndex) {
|
2018-10-27 03:23:32 +09:00
|
|
|
let pageContent = this._pageContents[pageIndex];
|
2018-10-27 03:53:42 +09:00
|
|
|
let query = this._query;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const { caseSensitive, entireWord, phraseSearch } = this._state;
|
2017-06-05 03:35:21 +09:00
|
|
|
|
2018-09-12 22:25:46 +09:00
|
|
|
if (query.length === 0) {
|
2017-06-05 03:35:21 +09:00
|
|
|
// Do nothing: the matches should be wiped out already.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!caseSensitive) {
|
|
|
|
pageContent = pageContent.toLowerCase();
|
|
|
|
query = query.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (phraseSearch) {
|
2018-09-01 08:28:19 +09:00
|
|
|
this._calculatePhraseMatch(query, pageIndex, pageContent, entireWord);
|
2017-06-05 03:35:21 +09:00
|
|
|
} else {
|
2018-09-01 08:28:19 +09:00
|
|
|
this._calculateWordMatch(query, pageIndex, pageContent, entireWord);
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
|
2018-10-31 23:54:10 +09:00
|
|
|
// When `highlightAll` is set, ensure that the matches on previously
|
|
|
|
// rendered (and still active) pages are correctly highlighted.
|
|
|
|
if (this._state.highlightAll) {
|
|
|
|
this._updatePage(pageIndex);
|
|
|
|
}
|
2018-09-12 22:10:45 +09:00
|
|
|
if (this._resumePageIdx === pageIndex) {
|
|
|
|
this._resumePageIdx = null;
|
2018-02-12 04:28:34 +09:00
|
|
|
this._nextPageMatch();
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
2014-07-16 23:39:34 +09:00
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
// Update the match count.
|
2018-09-12 22:10:45 +09:00
|
|
|
const pageMatchesCount = this._pageMatches[pageIndex].length;
|
2018-09-08 21:19:34 +09:00
|
|
|
if (pageMatchesCount > 0) {
|
2018-09-12 22:10:45 +09:00
|
|
|
this._matchesCountTotal += pageMatchesCount;
|
2018-02-12 04:28:34 +09:00
|
|
|
this._updateUIResultsCount();
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_extractText() {
|
2018-09-12 03:14:57 +09:00
|
|
|
// Perform text extraction once if this method is called multiple times.
|
2018-09-12 22:10:45 +09:00
|
|
|
if (this._extractTextPromises.length > 0) {
|
2017-06-05 03:35:21 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let promise = Promise.resolve();
|
2018-09-22 02:40:11 +09:00
|
|
|
for (let i = 0, ii = this._linkService.pagesCount; i < ii; i++) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const extractTextCapability = createPromiseCapability();
|
2018-09-12 22:10:45 +09:00
|
|
|
this._extractTextPromises[i] = extractTextCapability.promise;
|
2017-06-05 03:35:21 +09:00
|
|
|
|
|
|
|
promise = promise.then(() => {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
return this._pdfDocument
|
|
|
|
.getPage(i + 1)
|
|
|
|
.then(pdfPage => {
|
|
|
|
return pdfPage.getTextContent({
|
|
|
|
normalizeWhitespace: true,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
textContent => {
|
|
|
|
const textItems = textContent.items;
|
|
|
|
const strBuf = [];
|
|
|
|
|
|
|
|
for (let j = 0, jj = textItems.length; j < jj; j++) {
|
|
|
|
strBuf.push(textItems[j].str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the normalized page content (text items) as one string.
|
|
|
|
this._pageContents[i] = normalize(strBuf.join(""));
|
|
|
|
extractTextCapability.resolve(i);
|
|
|
|
},
|
|
|
|
reason => {
|
|
|
|
console.error(
|
|
|
|
`Unable to get text content for page ${i + 1}`,
|
|
|
|
reason
|
|
|
|
);
|
|
|
|
// Page error -- assuming no text content.
|
|
|
|
this._pageContents[i] = "";
|
|
|
|
extractTextCapability.resolve(i);
|
|
|
|
}
|
|
|
|
);
|
2017-05-05 00:09:50 +09:00
|
|
|
});
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_updatePage(index) {
|
Only scroll search results into view as a result of an actual find operation, and not when the user scrolls/zooms/rotates the document (bug 1237076, issue 6746)
Currently searching, and particularily highlighting of search results, may interfere with subsequent user-interactions such as scrolling/zooming/rotating which can result in a somewhat jarring UX where the document suddenly "jumps" to a previous position.
This is especially annoying in cases where the highlighted search result isn't even visible when a user initiated scrolling/zooming/rotating happens, and there exists a couple of bugs/issues about this behaviour.
It seems reasonable, as far as I'm concerned, to treat searching as one operation and any subsequent non-search user interactions with the viewer as separate and thus not scroll the current search result into view *unless* the user is actually doing another search.
This also seems consistent with general searching in e.g. Firefox and Adobe Reader:
- Compare with "regular" searching of e.g. HTML files in Firefox, where the user scrolling and/or zooming the document will not force a currently highlighted search result to become re-scrolled into view.
- Compare also with Adobe Reader, where the user scrolling, zooming, and/or rotating the document will not force the currently highlighted search result to become re-scrolled into view.
The question is then why search highlighting was implemented this way in PDF.js to begin with. It might be that this wasn't really intended behaviour, but more a consequence of the asynchronous nature of the API. Considering that most operations, such as fetching the page, rendering it and extracting its text-content are all asynchronous; searching and highlighting of matches thus becomes asynchronous too.
However, it should be possible to track when search results have been scrolled into view and highlighted, and thus prevent these wierd "jumps" when the user interacts with the document.
*Please note:* Unfortunately this required moving the scrolling of matches back into `PDFFindController`, since I simply couldn't see any other (reasonable) way of implementing the functionality without tracking the `_shouldScroll` property in only *one* spot.
However, given that the new `PDFFindController.scrollMatchIntoView` method follows a similar pattern as `BaseViewer.scrollPageIntoView` and `PDFThumbnailViewer.scrollThumbnailIntoView`, this is hopefully deemed OK.
2018-10-30 19:08:09 +09:00
|
|
|
if (this._scrollMatches && this._selected.pageIdx === index) {
|
2017-06-05 03:35:21 +09:00
|
|
|
// If the page is selected, scroll the page into view, which triggers
|
2018-09-22 02:40:11 +09:00
|
|
|
// rendering the page, which adds the text layer. Once the text layer
|
Only scroll search results into view as a result of an actual find operation, and not when the user scrolls/zooms/rotates the document (bug 1237076, issue 6746)
Currently searching, and particularily highlighting of search results, may interfere with subsequent user-interactions such as scrolling/zooming/rotating which can result in a somewhat jarring UX where the document suddenly "jumps" to a previous position.
This is especially annoying in cases where the highlighted search result isn't even visible when a user initiated scrolling/zooming/rotating happens, and there exists a couple of bugs/issues about this behaviour.
It seems reasonable, as far as I'm concerned, to treat searching as one operation and any subsequent non-search user interactions with the viewer as separate and thus not scroll the current search result into view *unless* the user is actually doing another search.
This also seems consistent with general searching in e.g. Firefox and Adobe Reader:
- Compare with "regular" searching of e.g. HTML files in Firefox, where the user scrolling and/or zooming the document will not force a currently highlighted search result to become re-scrolled into view.
- Compare also with Adobe Reader, where the user scrolling, zooming, and/or rotating the document will not force the currently highlighted search result to become re-scrolled into view.
The question is then why search highlighting was implemented this way in PDF.js to begin with. It might be that this wasn't really intended behaviour, but more a consequence of the asynchronous nature of the API. Considering that most operations, such as fetching the page, rendering it and extracting its text-content are all asynchronous; searching and highlighting of matches thus becomes asynchronous too.
However, it should be possible to track when search results have been scrolled into view and highlighted, and thus prevent these wierd "jumps" when the user interacts with the document.
*Please note:* Unfortunately this required moving the scrolling of matches back into `PDFFindController`, since I simply couldn't see any other (reasonable) way of implementing the functionality without tracking the `_shouldScroll` property in only *one* spot.
However, given that the new `PDFFindController.scrollMatchIntoView` method follows a similar pattern as `BaseViewer.scrollPageIntoView` and `PDFThumbnailViewer.scrollThumbnailIntoView`, this is hopefully deemed OK.
2018-10-30 19:08:09 +09:00
|
|
|
// is built, it will attempt to scroll the selected match into view.
|
2018-09-22 02:40:11 +09:00
|
|
|
this._linkService.page = index + 1;
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._eventBus.dispatch("updatetextlayermatches", {
|
2018-09-21 23:53:52 +09:00
|
|
|
source: this,
|
|
|
|
pageIndex: index,
|
|
|
|
});
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-10-23 22:57:10 +09:00
|
|
|
_updateAllPages() {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._eventBus.dispatch("updatetextlayermatches", {
|
2018-10-23 22:57:10 +09:00
|
|
|
source: this,
|
|
|
|
pageIndex: -1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_nextMatch() {
|
2018-09-12 22:25:46 +09:00
|
|
|
const previous = this._state.findPrevious;
|
2018-09-22 02:40:11 +09:00
|
|
|
const currentPageIndex = this._linkService.page - 1;
|
|
|
|
const numPages = this._linkService.pagesCount;
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-09-22 22:31:33 +09:00
|
|
|
this._highlightMatches = true;
|
2017-06-05 03:35:21 +09:00
|
|
|
|
2018-09-12 22:10:45 +09:00
|
|
|
if (this._dirtyMatch) {
|
2017-06-05 03:35:21 +09:00
|
|
|
// Need to recalculate the matches, reset everything.
|
2018-09-12 22:10:45 +09:00
|
|
|
this._dirtyMatch = false;
|
|
|
|
this._selected.pageIdx = this._selected.matchIdx = -1;
|
|
|
|
this._offset.pageIdx = currentPageIndex;
|
|
|
|
this._offset.matchIdx = null;
|
2018-10-26 18:56:25 +09:00
|
|
|
this._offset.wrapped = false;
|
2018-09-12 22:10:45 +09:00
|
|
|
this._resumePageIdx = null;
|
2018-09-12 22:25:46 +09:00
|
|
|
this._pageMatches.length = 0;
|
2018-10-25 23:21:55 +09:00
|
|
|
this._pageMatchesLength.length = 0;
|
2018-09-12 22:25:46 +09:00
|
|
|
this._matchesCountTotal = 0;
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-10-23 22:57:10 +09:00
|
|
|
this._updateAllPages(); // Wipe out any previously highlighted matches.
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2018-10-23 22:57:10 +09:00
|
|
|
for (let i = 0; i < numPages; i++) {
|
2017-06-05 03:35:21 +09:00
|
|
|
// Start finding the matches as soon as the text is extracted.
|
2018-10-23 22:57:10 +09:00
|
|
|
if (this._pendingFindMatches[i] === true) {
|
|
|
|
continue;
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
2018-10-23 22:57:10 +09:00
|
|
|
this._pendingFindMatches[i] = true;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._extractTextPromises[i].then(pageIdx => {
|
2018-10-23 22:57:10 +09:00
|
|
|
delete this._pendingFindMatches[pageIdx];
|
|
|
|
this._calculateMatch(pageIdx);
|
|
|
|
});
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no query there's no point in searching.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (this._query === "") {
|
2018-02-12 04:28:34 +09:00
|
|
|
this._updateUIState(FindState.FOUND);
|
2017-06-05 03:35:21 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If we're waiting on a page, we return since we can't do anything else.
|
2018-09-12 22:10:45 +09:00
|
|
|
if (this._resumePageIdx) {
|
2017-06-05 03:35:21 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-12 22:25:46 +09:00
|
|
|
const offset = this._offset;
|
2017-06-05 03:35:21 +09:00
|
|
|
// Keep track of how many pages we should maximally iterate through.
|
2018-09-12 22:10:45 +09:00
|
|
|
this._pagesToSearch = numPages;
|
2017-06-05 03:35:21 +09:00
|
|
|
// If there's already a `matchIdx` that means we are iterating through a
|
|
|
|
// page's matches.
|
|
|
|
if (offset.matchIdx !== null) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const numPageMatches = this._pageMatches[offset.pageIdx].length;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
|
|
|
(!previous && offset.matchIdx + 1 < numPageMatches) ||
|
|
|
|
(previous && offset.matchIdx > 0)
|
|
|
|
) {
|
2017-06-05 03:35:21 +09:00
|
|
|
// The simple case; we just have advance the matchIdx to select
|
|
|
|
// the next match on the page.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
offset.matchIdx = previous ? offset.matchIdx - 1 : offset.matchIdx + 1;
|
2018-02-12 04:28:34 +09:00
|
|
|
this._updateMatch(/* found = */ true);
|
2017-06-05 03:35:21 +09:00
|
|
|
return;
|
2016-12-16 21:05:33 +09:00
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
// We went beyond the current page's matches, so we advance to
|
|
|
|
// the next page.
|
2018-02-12 04:28:34 +09:00
|
|
|
this._advanceOffsetPage(previous);
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
// Start searching through the page.
|
2018-02-12 04:28:34 +09:00
|
|
|
this._nextPageMatch();
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_matchesReady(matches) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const offset = this._offset;
|
|
|
|
const numMatches = matches.length;
|
|
|
|
const previous = this._state.findPrevious;
|
2017-06-05 03:35:21 +09:00
|
|
|
|
|
|
|
if (numMatches) {
|
|
|
|
// There were matches for the page, so initialize `matchIdx`.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
offset.matchIdx = previous ? numMatches - 1 : 0;
|
2018-02-12 04:28:34 +09:00
|
|
|
this._updateMatch(/* found = */ true);
|
2017-06-05 03:35:21 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// No matches, so attempt to search the next page.
|
2018-02-12 04:28:34 +09:00
|
|
|
this._advanceOffsetPage(previous);
|
2017-06-05 03:35:21 +09:00
|
|
|
if (offset.wrapped) {
|
|
|
|
offset.matchIdx = null;
|
2018-09-12 22:10:45 +09:00
|
|
|
if (this._pagesToSearch < 0) {
|
2017-06-05 03:35:21 +09:00
|
|
|
// No point in wrapping again, there were no matches.
|
2018-02-12 04:28:34 +09:00
|
|
|
this._updateMatch(/* found = */ false);
|
2017-06-05 03:35:21 +09:00
|
|
|
// While matches were not found, searching for a page
|
|
|
|
// with matches should nevertheless halt.
|
|
|
|
return true;
|
2014-12-18 05:12:51 +09:00
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
// Matches were not found (and searching is not done).
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_nextPageMatch() {
|
2018-09-12 22:10:45 +09:00
|
|
|
if (this._resumePageIdx !== null) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
console.error("There can only be one pending page.");
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
let matches = null;
|
|
|
|
do {
|
2018-09-12 22:25:46 +09:00
|
|
|
const pageIdx = this._offset.pageIdx;
|
2018-09-12 22:10:45 +09:00
|
|
|
matches = this._pageMatches[pageIdx];
|
2017-06-05 03:35:21 +09:00
|
|
|
if (!matches) {
|
2018-02-12 04:28:34 +09:00
|
|
|
// The matches don't exist yet for processing by `_matchesReady`,
|
2017-06-05 03:35:21 +09:00
|
|
|
// so set a resume point for when they do exist.
|
2018-09-12 22:10:45 +09:00
|
|
|
this._resumePageIdx = pageIdx;
|
2017-06-05 03:35:21 +09:00
|
|
|
break;
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
2018-02-12 04:28:34 +09:00
|
|
|
} while (!this._matchesReady(matches));
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
2014-06-26 07:15:22 +09:00
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_advanceOffsetPage(previous) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const offset = this._offset;
|
2018-09-22 02:40:11 +09:00
|
|
|
const numPages = this._linkService.pagesCount;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
offset.pageIdx = previous ? offset.pageIdx - 1 : offset.pageIdx + 1;
|
2017-06-05 03:35:21 +09:00
|
|
|
offset.matchIdx = null;
|
2014-10-02 08:57:57 +09:00
|
|
|
|
2018-09-12 22:10:45 +09:00
|
|
|
this._pagesToSearch--;
|
2015-02-03 00:12:52 +09:00
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
offset.pageIdx = previous ? numPages - 1 : 0;
|
2017-06-05 03:35:21 +09:00
|
|
|
offset.wrapped = true;
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 00:12:52 +09:00
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_updateMatch(found = false) {
|
2017-06-05 03:35:21 +09:00
|
|
|
let state = FindState.NOT_FOUND;
|
2018-09-12 22:25:46 +09:00
|
|
|
const wrapped = this._offset.wrapped;
|
2018-09-12 22:10:45 +09:00
|
|
|
this._offset.wrapped = false;
|
2014-01-17 06:58:29 +09:00
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
if (found) {
|
2018-09-12 22:25:46 +09:00
|
|
|
const previousPage = this._selected.pageIdx;
|
2018-09-22 05:40:15 +09:00
|
|
|
this._selected.pageIdx = this._offset.pageIdx;
|
|
|
|
this._selected.matchIdx = this._offset.matchIdx;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
state = wrapped ? FindState.WRAPPED : FindState.FOUND;
|
2015-10-31 01:20:29 +09:00
|
|
|
|
2017-06-05 03:35:21 +09:00
|
|
|
// Update the currently selected page to wipe out any selected matches.
|
2018-09-12 22:10:45 +09:00
|
|
|
if (previousPage !== -1 && previousPage !== this._selected.pageIdx) {
|
2018-02-12 04:28:34 +09:00
|
|
|
this._updatePage(previousPage);
|
2013-06-19 01:05:55 +09:00
|
|
|
}
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
|
2018-09-12 22:10:45 +09:00
|
|
|
this._updateUIState(state, this._state.findPrevious);
|
|
|
|
if (this._selected.pageIdx !== -1) {
|
Only scroll search results into view as a result of an actual find operation, and not when the user scrolls/zooms/rotates the document (bug 1237076, issue 6746)
Currently searching, and particularily highlighting of search results, may interfere with subsequent user-interactions such as scrolling/zooming/rotating which can result in a somewhat jarring UX where the document suddenly "jumps" to a previous position.
This is especially annoying in cases where the highlighted search result isn't even visible when a user initiated scrolling/zooming/rotating happens, and there exists a couple of bugs/issues about this behaviour.
It seems reasonable, as far as I'm concerned, to treat searching as one operation and any subsequent non-search user interactions with the viewer as separate and thus not scroll the current search result into view *unless* the user is actually doing another search.
This also seems consistent with general searching in e.g. Firefox and Adobe Reader:
- Compare with "regular" searching of e.g. HTML files in Firefox, where the user scrolling and/or zooming the document will not force a currently highlighted search result to become re-scrolled into view.
- Compare also with Adobe Reader, where the user scrolling, zooming, and/or rotating the document will not force the currently highlighted search result to become re-scrolled into view.
The question is then why search highlighting was implemented this way in PDF.js to begin with. It might be that this wasn't really intended behaviour, but more a consequence of the asynchronous nature of the API. Considering that most operations, such as fetching the page, rendering it and extracting its text-content are all asynchronous; searching and highlighting of matches thus becomes asynchronous too.
However, it should be possible to track when search results have been scrolled into view and highlighted, and thus prevent these wierd "jumps" when the user interacts with the document.
*Please note:* Unfortunately this required moving the scrolling of matches back into `PDFFindController`, since I simply couldn't see any other (reasonable) way of implementing the functionality without tracking the `_shouldScroll` property in only *one* spot.
However, given that the new `PDFFindController.scrollMatchIntoView` method follows a similar pattern as `BaseViewer.scrollPageIntoView` and `PDFThumbnailViewer.scrollThumbnailIntoView`, this is hopefully deemed OK.
2018-10-30 19:08:09 +09:00
|
|
|
// Ensure that the match will be scrolled into view.
|
|
|
|
this._scrollMatches = true;
|
|
|
|
|
2018-09-12 22:10:45 +09:00
|
|
|
this._updatePage(this._selected.pageIdx);
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:31:27 +09:00
|
|
|
_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.
|
Make `PDFFindController` less confusing to use, by allowing searching to start when `setDocument` is called
*This patch is based on something that I noticed while working on PR 10126.*
The recent re-factoring of `PDFFindController` brought many improvements, among those the fact that access to `BaseViewer` is no longer required. However, with these changes there's one thing which now strikes me as not particularly user-friendly[1]: The fact that in order for searching to actually work, `PDFFindController.setDocument` must be called *and* a 'pagesinit' event must be dispatched (from somewhere).
For all other viewer components, calling the `setDocument` method[2] is enough in order for the component to actually be usable.
The `PDFFindController` thus stands out quite a bit, and it also becomes difficult to work with in any sort of custom implementation. For example: Imagine someone trying to use `PDFFindController` separately from the viewer[3], which *should* now be relatively simple given the re-factoring, and thus having to (somehow) figure out that they'll also need to manually dispatch a 'pagesinit' event for searching to work.
Note that the above even affects the unit-tests, where an out-of-place 'pagesinit' event is being used.
To attempt to address these problems, I'm thus suggesting that *only* `setDocument` should be used to indicate that searching may start. For the default viewer and/or the viewer components, `BaseViewer.setDocument` will now call `PDFFindController.setDocument` when the document is ready, thus requiring no outside configuration anymore[4]. For custom implementation, and the unit-tests, it's now as simple as just calling `PDFFindController.setDocument` to allow searching to start.
---
[1] I should have caught this during review of PR 10099, but unfortunately it's sometimes not until you actually work with the code in question that things like these become clear.
[2] Assuming, obviously, that the viewer component in question actually implements such a method :-)
[3] There's even a very recent issue, filed by someone trying to do just that.
[4] Short of providing a `PDFFindController` instance when creating a `BaseViewer` instance, of course.
2018-10-03 19:42:41 +09:00
|
|
|
this._firstPageCapability.promise.then(() => {
|
2018-10-26 18:56:25 +09:00
|
|
|
// Only update the UI if the document is open, and is the current one.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
|
|
|
!this._pdfDocument ||
|
|
|
|
(pdfDocument && this._pdfDocument !== pdfDocument)
|
|
|
|
) {
|
2018-10-02 01:31:27 +09:00
|
|
|
return;
|
|
|
|
}
|
2018-10-26 18:56:25 +09:00
|
|
|
// Ensure that a pending, not yet started, search operation is aborted.
|
2018-10-02 01:31:27 +09:00
|
|
|
if (this._findTimeout) {
|
|
|
|
clearTimeout(this._findTimeout);
|
|
|
|
this._findTimeout = null;
|
|
|
|
}
|
2018-10-26 18:56:25 +09:00
|
|
|
// Abort any long running searches, to avoid a match being scrolled into
|
|
|
|
// view *after* the findbar has been closed. In this case `this._offset`
|
|
|
|
// will most likely differ from `this._selected`, hence we also ensure
|
|
|
|
// that any new search operation will always start with a clean slate.
|
|
|
|
if (this._resumePageIdx) {
|
|
|
|
this._resumePageIdx = null;
|
|
|
|
this._dirtyMatch = true;
|
|
|
|
}
|
|
|
|
// Avoid the UI being in a pending state when the findbar is re-opened.
|
|
|
|
this._updateUIState(FindState.FOUND);
|
2018-10-02 01:31:27 +09:00
|
|
|
|
|
|
|
this._highlightMatches = false;
|
2018-10-23 22:57:10 +09:00
|
|
|
this._updateAllPages(); // Wipe out any previously highlighted matches.
|
2018-10-02 01:31:27 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-08 21:19:34 +09:00
|
|
|
_requestMatchesCount() {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const { pageIdx, matchIdx } = this._selected;
|
|
|
|
let current = 0,
|
|
|
|
total = this._matchesCountTotal;
|
2018-09-08 21:19:34 +09:00
|
|
|
if (matchIdx !== -1) {
|
|
|
|
for (let i = 0; i < pageIdx; i++) {
|
2018-09-12 22:10:45 +09:00
|
|
|
current += (this._pageMatches[i] && this._pageMatches[i].length) || 0;
|
2018-09-08 21:19:34 +09:00
|
|
|
}
|
|
|
|
current += matchIdx + 1;
|
|
|
|
}
|
2018-09-11 21:34:20 +09:00
|
|
|
// When searching starts, this method may be called before the `pageMatches`
|
|
|
|
// have been counted (in `_calculateMatch`). Ensure that the UI won't show
|
|
|
|
// temporarily broken state when the active find result doesn't make sense.
|
2018-09-13 18:40:12 +09:00
|
|
|
if (current < 1 || current > total) {
|
2018-09-11 21:34:20 +09:00
|
|
|
current = total = 0;
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
return { current, total };
|
2018-09-08 21:19:34 +09:00
|
|
|
}
|
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_updateUIResultsCount() {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._eventBus.dispatch("updatefindmatchescount", {
|
2018-09-22 03:00:58 +09:00
|
|
|
source: this,
|
|
|
|
matchesCount: this._requestMatchesCount(),
|
|
|
|
});
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
|
2018-02-12 04:28:34 +09:00
|
|
|
_updateUIState(state, previous) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._eventBus.dispatch("updatefindcontrolstate", {
|
2018-09-22 03:00:58 +09:00
|
|
|
source: this,
|
|
|
|
state,
|
|
|
|
previous,
|
|
|
|
matchesCount: this._requestMatchesCount(),
|
|
|
|
});
|
2017-06-05 03:35:21 +09:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 02:34:27 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
export { FindState, PDFFindController };
|