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.
|
|
|
|
*/
|
|
|
|
|
2017-05-31 10:38:22 +09:00
|
|
|
import { createPromiseCapability } from 'pdfjs-lib';
|
2017-04-15 00:32:36 +09:00
|
|
|
import { scrollIntoView } from './ui_utils';
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2014-09-15 23:49:24 +09:00
|
|
|
var FindStates = {
|
|
|
|
FIND_FOUND: 0,
|
|
|
|
FIND_NOTFOUND: 1,
|
|
|
|
FIND_WRAPPED: 2,
|
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js
index 002dbf29..1de4e530 100644
--- a/web/pdf_thumbnail_view.js
+++ b/web/pdf_thumbnail_view.js
@@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
setPageLabel: function PDFThumbnailView_setPageLabel(label) {
this.pageLabel = (typeof label === 'string' ? label : null);
- this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}').
- then((msg) => {
+ this.l10n.get('thumb_page_title', { page: this.pageId, },
+ 'Page {{page}}').then((msg) => {
this.anchor.title = msg;
});
diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js
index 160e0410..6495fc5e 100644
--- a/web/secondary_toolbar.js
+++ b/web/secondary_toolbar.js
@@ -65,7 +65,8 @@ class SecondaryToolbar {
{ element: options.printButton, eventName: 'print', close: true, },
{ element: options.downloadButton, eventName: 'download', close: true, },
{ element: options.viewBookmarkButton, eventName: null, close: true, },
- { element: options.firstPageButton, eventName: 'firstpage', close: true, },
+ { element: options.firstPageButton, eventName: 'firstpage',
+ close: true, },
{ element: options.lastPageButton, eventName: 'lastpage', close: true, },
{ element: options.pageRotateCwButton, eventName: 'rotatecw',
close: false, },
@@ -76,7 +77,7 @@ class SecondaryToolbar {
{ element: options.cursorHandToolButton, eventName: 'switchcursortool',
eventDetails: { tool: CursorTool.HAND, }, close: true, },
{ element: options.documentPropertiesButton,
- eventName: 'documentproperties', close: true, }
+ eventName: 'documentproperties', close: true, },
];
this.items = {
firstPage: options.firstPageButton,
```
2017-06-01 19:46:12 +09:00
|
|
|
FIND_PENDING: 3,
|
2014-09-15 23:49:24 +09:00
|
|
|
};
|
|
|
|
|
2014-12-18 05:12:51 +09:00
|
|
|
var FIND_SCROLL_OFFSET_TOP = -50;
|
|
|
|
var FIND_SCROLL_OFFSET_LEFT = -400;
|
|
|
|
|
2016-02-27 02:06:59 +09:00
|
|
|
var CHARACTERS_TO_NORMALIZE = {
|
|
|
|
'\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
|
|
|
|
};
|
|
|
|
|
2013-06-19 01:05:55 +09:00
|
|
|
/**
|
2014-06-26 07:15:22 +09:00
|
|
|
* Provides "search" or "find" functionality for the PDF.
|
2013-06-19 01:05:55 +09:00
|
|
|
* This object actually performs the search for a given string.
|
|
|
|
*/
|
2014-06-26 07:15:22 +09:00
|
|
|
var PDFFindController = (function PDFFindControllerClosure() {
|
|
|
|
function PDFFindController(options) {
|
2014-09-15 23:49:24 +09:00
|
|
|
this.pdfViewer = options.pdfViewer || null;
|
2016-04-14 04:39:29 +09:00
|
|
|
|
|
|
|
this.onUpdateResultsCount = null;
|
|
|
|
this.onUpdateState = null;
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2016-02-27 02:06:59 +09:00
|
|
|
this.reset();
|
|
|
|
|
|
|
|
// Compile the regular expression for text normalization once.
|
|
|
|
var replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
|
2014-06-21 04:49:16 +09:00
|
|
|
this.normalizationRegex = new RegExp('[' + replace + ']', 'g');
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
PDFFindController.prototype = {
|
|
|
|
reset: function PDFFindController_reset() {
|
|
|
|
this.startedTextExtraction = false;
|
|
|
|
this.extractTextPromises = [];
|
2016-02-27 02:06:59 +09:00
|
|
|
this.pendingFindMatches = Object.create(null);
|
|
|
|
this.active = false; // If active, find results will be highlighted.
|
|
|
|
this.pageContents = []; // Stores the text for each page.
|
|
|
|
this.pageMatches = [];
|
2016-05-26 22:24:58 +09:00
|
|
|
this.pageMatchesLength = null;
|
2016-02-27 02:06:59 +09:00
|
|
|
this.matchCount = 0;
|
|
|
|
this.selected = { // Currently selected match.
|
|
|
|
pageIdx: -1,
|
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js
index 002dbf29..1de4e530 100644
--- a/web/pdf_thumbnail_view.js
+++ b/web/pdf_thumbnail_view.js
@@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
setPageLabel: function PDFThumbnailView_setPageLabel(label) {
this.pageLabel = (typeof label === 'string' ? label : null);
- this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}').
- then((msg) => {
+ this.l10n.get('thumb_page_title', { page: this.pageId, },
+ 'Page {{page}}').then((msg) => {
this.anchor.title = msg;
});
diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js
index 160e0410..6495fc5e 100644
--- a/web/secondary_toolbar.js
+++ b/web/secondary_toolbar.js
@@ -65,7 +65,8 @@ class SecondaryToolbar {
{ element: options.printButton, eventName: 'print', close: true, },
{ element: options.downloadButton, eventName: 'download', close: true, },
{ element: options.viewBookmarkButton, eventName: null, close: true, },
- { element: options.firstPageButton, eventName: 'firstpage', close: true, },
+ { element: options.firstPageButton, eventName: 'firstpage',
+ close: true, },
{ element: options.lastPageButton, eventName: 'lastpage', close: true, },
{ element: options.pageRotateCwButton, eventName: 'rotatecw',
close: false, },
@@ -76,7 +77,7 @@ class SecondaryToolbar {
{ element: options.cursorHandToolButton, eventName: 'switchcursortool',
eventDetails: { tool: CursorTool.HAND, }, close: true, },
{ element: options.documentPropertiesButton,
- eventName: 'documentproperties', close: true, }
+ eventName: 'documentproperties', close: true, },
];
this.items = {
firstPage: options.firstPageButton,
```
2017-06-01 19:46:12 +09:00
|
|
|
matchIdx: -1,
|
2016-02-27 02:06:59 +09:00
|
|
|
};
|
|
|
|
this.offset = { // Where the find algorithm currently is in the document.
|
|
|
|
pageIdx: null,
|
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js
index 002dbf29..1de4e530 100644
--- a/web/pdf_thumbnail_view.js
+++ b/web/pdf_thumbnail_view.js
@@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
setPageLabel: function PDFThumbnailView_setPageLabel(label) {
this.pageLabel = (typeof label === 'string' ? label : null);
- this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}').
- then((msg) => {
+ this.l10n.get('thumb_page_title', { page: this.pageId, },
+ 'Page {{page}}').then((msg) => {
this.anchor.title = msg;
});
diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js
index 160e0410..6495fc5e 100644
--- a/web/secondary_toolbar.js
+++ b/web/secondary_toolbar.js
@@ -65,7 +65,8 @@ class SecondaryToolbar {
{ element: options.printButton, eventName: 'print', close: true, },
{ element: options.downloadButton, eventName: 'download', close: true, },
{ element: options.viewBookmarkButton, eventName: null, close: true, },
- { element: options.firstPageButton, eventName: 'firstpage', close: true, },
+ { element: options.firstPageButton, eventName: 'firstpage',
+ close: true, },
{ element: options.lastPageButton, eventName: 'lastpage', close: true, },
{ element: options.pageRotateCwButton, eventName: 'rotatecw',
close: false, },
@@ -76,7 +77,7 @@ class SecondaryToolbar {
{ element: options.cursorHandToolButton, eventName: 'switchcursortool',
eventDetails: { tool: CursorTool.HAND, }, close: true, },
{ element: options.documentPropertiesButton,
- eventName: 'documentproperties', close: true, }
+ eventName: 'documentproperties', close: true, },
];
this.items = {
firstPage: options.firstPageButton,
```
2017-06-01 19:46:12 +09:00
|
|
|
matchIdx: null,
|
2016-02-27 02:06:59 +09:00
|
|
|
};
|
|
|
|
this.pagesToSearch = null;
|
|
|
|
this.resumePageIdx = null;
|
|
|
|
this.state = null;
|
|
|
|
this.dirtyMatch = false;
|
|
|
|
this.findTimeout = null;
|
|
|
|
|
2017-04-15 19:57:54 +09:00
|
|
|
this._firstPagePromise = new Promise((resolve) => {
|
2016-02-27 02:06:59 +09:00
|
|
|
this.resolveFirstPage = resolve;
|
2017-04-15 19:57:54 +09:00
|
|
|
});
|
2014-06-26 07:15:22 +09:00
|
|
|
},
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
normalize: function PDFFindController_normalize(text) {
|
|
|
|
return text.replace(this.normalizationRegex, function (ch) {
|
2016-02-27 02:06:59 +09:00
|
|
|
return CHARACTERS_TO_NORMALIZE[ch];
|
2014-06-26 07:15:22 +09:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-05-26 22:24:58 +09:00
|
|
|
// Helper for multiple search - fills matchesWithLength array
|
|
|
|
// and takes into account cases when one search term
|
|
|
|
// include another search term (for example, "tamed tame" or "this is").
|
|
|
|
// Looking for intersecting terms in the 'matches' and
|
|
|
|
// leave elements with a longer match-length.
|
|
|
|
|
|
|
|
_prepareMatches: function PDFFindController_prepareMatches(
|
|
|
|
matchesWithLength, matches, matchesLength) {
|
|
|
|
|
|
|
|
function isSubTerm(matchesWithLength, currentIndex) {
|
|
|
|
var currentElem, prevElem, nextElem;
|
|
|
|
currentElem = matchesWithLength[currentIndex];
|
|
|
|
nextElem = matchesWithLength[currentIndex + 1];
|
|
|
|
// checking for cases like "TAMEd TAME"
|
|
|
|
if (currentIndex < matchesWithLength.length - 1 &&
|
|
|
|
currentElem.match === nextElem.match) {
|
|
|
|
currentElem.skipped = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// checking for cases like "thIS IS"
|
|
|
|
for (var i = currentIndex - 1; i >= 0; i--) {
|
|
|
|
prevElem = matchesWithLength[i];
|
|
|
|
if (prevElem.skipped) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (prevElem.match + prevElem.matchLength < currentElem.match) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (prevElem.match + prevElem.matchLength >=
|
|
|
|
currentElem.match + currentElem.matchLength) {
|
|
|
|
currentElem.skipped = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i, len;
|
|
|
|
// Sorting array of objects { match: <match>, matchLength: <matchLength> }
|
|
|
|
// in increasing index first and then the lengths.
|
|
|
|
matchesWithLength.sort(function(a, b) {
|
|
|
|
return a.match === b.match ?
|
|
|
|
a.matchLength - b.matchLength : a.match - b.match;
|
|
|
|
});
|
|
|
|
for (i = 0, len = matchesWithLength.length; i < len; i++) {
|
|
|
|
if (isSubTerm(matchesWithLength, i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
matches.push(matchesWithLength[i].match);
|
|
|
|
matchesLength.push(matchesWithLength[i].matchLength);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
calcFindPhraseMatch: function PDFFindController_calcFindPhraseMatch(
|
|
|
|
query, pageIndex, pageContent) {
|
|
|
|
var matches = [];
|
|
|
|
var queryLen = query.length;
|
|
|
|
var matchIdx = -queryLen;
|
|
|
|
while (true) {
|
|
|
|
matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
|
|
|
|
if (matchIdx === -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
matches.push(matchIdx);
|
|
|
|
}
|
|
|
|
this.pageMatches[pageIndex] = matches;
|
|
|
|
},
|
|
|
|
|
|
|
|
calcFindWordMatch: function PDFFindController_calcFindWordMatch(
|
|
|
|
query, pageIndex, pageContent) {
|
|
|
|
var matchesWithLength = [];
|
|
|
|
// Divide the query into pieces and search for text on each piece.
|
|
|
|
var queryArray = query.match(/\S+/g);
|
|
|
|
var subquery, subqueryLen, matchIdx;
|
|
|
|
for (var i = 0, len = queryArray.length; i < len; i++) {
|
|
|
|
subquery = queryArray[i];
|
|
|
|
subqueryLen = subquery.length;
|
|
|
|
matchIdx = -subqueryLen;
|
|
|
|
while (true) {
|
|
|
|
matchIdx = pageContent.indexOf(subquery, matchIdx + subqueryLen);
|
|
|
|
if (matchIdx === -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Other searches do not, so we store the length.
|
|
|
|
matchesWithLength.push({
|
|
|
|
match: matchIdx,
|
|
|
|
matchLength: subqueryLen,
|
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js
index 002dbf29..1de4e530 100644
--- a/web/pdf_thumbnail_view.js
+++ b/web/pdf_thumbnail_view.js
@@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
setPageLabel: function PDFThumbnailView_setPageLabel(label) {
this.pageLabel = (typeof label === 'string' ? label : null);
- this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}').
- then((msg) => {
+ this.l10n.get('thumb_page_title', { page: this.pageId, },
+ 'Page {{page}}').then((msg) => {
this.anchor.title = msg;
});
diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js
index 160e0410..6495fc5e 100644
--- a/web/secondary_toolbar.js
+++ b/web/secondary_toolbar.js
@@ -65,7 +65,8 @@ class SecondaryToolbar {
{ element: options.printButton, eventName: 'print', close: true, },
{ element: options.downloadButton, eventName: 'download', close: true, },
{ element: options.viewBookmarkButton, eventName: null, close: true, },
- { element: options.firstPageButton, eventName: 'firstpage', close: true, },
+ { element: options.firstPageButton, eventName: 'firstpage',
+ close: true, },
{ element: options.lastPageButton, eventName: 'lastpage', close: true, },
{ element: options.pageRotateCwButton, eventName: 'rotatecw',
close: false, },
@@ -76,7 +77,7 @@ class SecondaryToolbar {
{ element: options.cursorHandToolButton, eventName: 'switchcursortool',
eventDetails: { tool: CursorTool.HAND, }, close: true, },
{ element: options.documentPropertiesButton,
- eventName: 'documentproperties', close: true, }
+ eventName: 'documentproperties', close: true, },
];
this.items = {
firstPage: options.firstPageButton,
```
2017-06-01 19:46:12 +09:00
|
|
|
skipped: false,
|
2016-05-26 22:24:58 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Prepare arrays for store the matches.
|
|
|
|
if (!this.pageMatchesLength) {
|
|
|
|
this.pageMatchesLength = [];
|
|
|
|
}
|
|
|
|
this.pageMatchesLength[pageIndex] = [];
|
|
|
|
this.pageMatches[pageIndex] = [];
|
|
|
|
// Sort matchesWithLength, clean up intersecting terms
|
|
|
|
// and put the result into the two arrays.
|
|
|
|
this._prepareMatches(matchesWithLength, this.pageMatches[pageIndex],
|
|
|
|
this.pageMatchesLength[pageIndex]);
|
|
|
|
},
|
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
calcFindMatch: function PDFFindController_calcFindMatch(pageIndex) {
|
|
|
|
var pageContent = this.normalize(this.pageContents[pageIndex]);
|
|
|
|
var query = this.normalize(this.state.query);
|
|
|
|
var caseSensitive = this.state.caseSensitive;
|
2016-05-26 22:24:58 +09:00
|
|
|
var phraseSearch = this.state.phraseSearch;
|
2014-06-26 07:15:22 +09:00
|
|
|
var queryLen = query.length;
|
|
|
|
|
|
|
|
if (queryLen === 0) {
|
2014-07-16 23:39:34 +09:00
|
|
|
// Do nothing: the matches should be wiped out already.
|
|
|
|
return;
|
2013-06-19 01:05:55 +09:00
|
|
|
}
|
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
if (!caseSensitive) {
|
|
|
|
pageContent = pageContent.toLowerCase();
|
|
|
|
query = query.toLowerCase();
|
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2016-05-26 22:24:58 +09:00
|
|
|
if (phraseSearch) {
|
|
|
|
this.calcFindPhraseMatch(query, pageIndex, pageContent);
|
|
|
|
} else {
|
|
|
|
this.calcFindWordMatch(query, pageIndex, pageContent);
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
2016-05-26 22:24:58 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
this.updatePage(pageIndex);
|
|
|
|
if (this.resumePageIdx === pageIndex) {
|
|
|
|
this.resumePageIdx = null;
|
|
|
|
this.nextPageMatch();
|
|
|
|
}
|
2014-07-16 23:39:34 +09:00
|
|
|
|
|
|
|
// Update the matches count
|
2016-05-26 22:24:58 +09:00
|
|
|
if (this.pageMatches[pageIndex].length > 0) {
|
|
|
|
this.matchCount += this.pageMatches[pageIndex].length;
|
2015-10-31 01:20:29 +09:00
|
|
|
this.updateUIResultsCount();
|
|
|
|
}
|
2014-06-26 07:15:22 +09:00
|
|
|
},
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2017-05-10 20:33:08 +09:00
|
|
|
extractText() {
|
2014-06-26 07:15:22 +09:00
|
|
|
if (this.startedTextExtraction) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.startedTextExtraction = true;
|
2017-05-10 20:33:08 +09:00
|
|
|
this.pageContents.length = 0;
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2017-05-10 20:33:08 +09:00
|
|
|
let promise = Promise.resolve();
|
|
|
|
for (let i = 0, ii = this.pdfViewer.pagesCount; i < ii; i++) {
|
|
|
|
let extractTextCapability = createPromiseCapability();
|
|
|
|
this.extractTextPromises[i] = extractTextCapability.promise;
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2017-05-10 20:33:08 +09:00
|
|
|
promise = promise.then(() => {
|
|
|
|
return this.pdfViewer.getPageTextContent(i).then((textContent) => {
|
|
|
|
let textItems = textContent.items;
|
|
|
|
let strBuf = [];
|
2014-06-26 07:15:22 +09:00
|
|
|
|
2017-05-10 20:33:08 +09:00
|
|
|
for (let j = 0, jj = textItems.length; j < jj; j++) {
|
|
|
|
strBuf.push(textItems[j].str);
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
|
|
|
// Store the pageContent as a string.
|
2017-05-10 20:33:08 +09:00
|
|
|
this.pageContents[i] = strBuf.join('');
|
|
|
|
extractTextCapability.resolve(i);
|
|
|
|
});
|
|
|
|
});
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
|
|
|
},
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2016-04-14 04:39:29 +09:00
|
|
|
executeCommand: function PDFFindController_executeCommand(cmd, state) {
|
|
|
|
if (this.state === null || cmd !== 'findagain') {
|
2014-06-26 07:15:22 +09:00
|
|
|
this.dirtyMatch = true;
|
|
|
|
}
|
2016-04-14 04:39:29 +09:00
|
|
|
this.state = state;
|
2014-06-26 07:15:22 +09:00
|
|
|
this.updateUIState(FindStates.FIND_PENDING);
|
|
|
|
|
2017-05-05 00:09:50 +09:00
|
|
|
this._firstPagePromise.then(() => {
|
2014-06-26 07:15:22 +09:00
|
|
|
this.extractText();
|
|
|
|
|
|
|
|
clearTimeout(this.findTimeout);
|
2016-04-14 04:39:29 +09:00
|
|
|
if (cmd === 'find') {
|
2014-06-26 07:15:22 +09:00
|
|
|
// Only trigger the find action after 250ms of silence.
|
|
|
|
this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
|
|
|
|
} else {
|
|
|
|
this.nextMatch();
|
|
|
|
}
|
2017-05-05 00:09:50 +09:00
|
|
|
});
|
2014-06-26 07:15:22 +09:00
|
|
|
},
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
updatePage: function PDFFindController_updatePage(index) {
|
|
|
|
if (this.selected.pageIdx === index) {
|
|
|
|
// If the page is selected, scroll the page into view, which triggers
|
|
|
|
// rendering the page, which adds the textLayer. Once the textLayer is
|
|
|
|
// build, it will scroll onto the selected match.
|
Prevent destinations with bad left/top values from scrolling the wrong page into view (bug 874482)
There are PDF generators which create destinations with e.g. too large top values, which cause the wrong page to be scrolled into view because the offset becomes negative.
By ignoring negative offsets, we can prevent this issue, and get a similar behaviour as in Adobe Reader.
However, since we're also using `PDFViewer_scrollPageIntoView` in more cases than just when links (in the document/outline) are clicked, the patch adds a way to allow the caller to opt-out of this behaviour.
In e.g. the following situations, I think that we still want to be able to allow negative offsets: when restoring a position from the `ViewHistory`, when the `viewBookmark` button is used to obtain a link to the current position, or when maintaining the current position on zooming.
Rather than adding another parameter to `PDFViewer_scrollPageIntoView`, I've changed the signature to take an parameter object instead. To maintain backwards compatibility, I've added fallback code enclosed in a `GENERIC` preprocessor tag.
Fixes https://bugzilla.mozilla.org/show_bug.cgi?id=874482.
2016-06-20 19:30:05 +09:00
|
|
|
this.pdfViewer.currentPageNumber = index + 1;
|
2013-09-05 07:25:19 +09:00
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-09-28 03:03:28 +09:00
|
|
|
var page = this.pdfViewer.getPageView(index);
|
2014-06-26 07:15:22 +09:00
|
|
|
if (page.textLayer) {
|
|
|
|
page.textLayer.updateMatches();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
nextMatch: function PDFFindController_nextMatch() {
|
|
|
|
var previous = this.state.findPrevious;
|
2014-09-15 23:49:24 +09:00
|
|
|
var currentPageIndex = this.pdfViewer.currentPageNumber - 1;
|
|
|
|
var numPages = this.pdfViewer.pagesCount;
|
2014-06-26 07:15:22 +09:00
|
|
|
|
|
|
|
this.active = true;
|
|
|
|
|
|
|
|
if (this.dirtyMatch) {
|
|
|
|
// Need to recalculate the matches, reset everything.
|
|
|
|
this.dirtyMatch = false;
|
|
|
|
this.selected.pageIdx = this.selected.matchIdx = -1;
|
|
|
|
this.offset.pageIdx = currentPageIndex;
|
|
|
|
this.offset.matchIdx = null;
|
|
|
|
this.hadMatch = false;
|
|
|
|
this.resumePageIdx = null;
|
|
|
|
this.pageMatches = [];
|
2015-10-31 01:20:29 +09:00
|
|
|
this.matchCount = 0;
|
2016-05-26 22:24:58 +09:00
|
|
|
this.pageMatchesLength = null;
|
2014-06-26 07:15:22 +09:00
|
|
|
|
2017-05-10 20:51:36 +09:00
|
|
|
for (let i = 0; i < numPages; i++) {
|
2014-06-26 07:15:22 +09:00
|
|
|
// Wipe out any previous highlighted matches.
|
|
|
|
this.updatePage(i);
|
|
|
|
|
|
|
|
// As soon as the text is extracted start finding the matches.
|
|
|
|
if (!(i in this.pendingFindMatches)) {
|
|
|
|
this.pendingFindMatches[i] = true;
|
2017-05-10 20:51:36 +09:00
|
|
|
this.extractTextPromises[i].then((pageIdx) => {
|
|
|
|
delete this.pendingFindMatches[pageIdx];
|
|
|
|
this.calcFindMatch(pageIdx);
|
2014-06-26 07:15:22 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
// If there's no query there's no point in searching.
|
|
|
|
if (this.state.query === '') {
|
|
|
|
this.updateUIState(FindStates.FIND_FOUND);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
// If we're waiting on a page, we return since we can't do anything else.
|
|
|
|
if (this.resumePageIdx) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
var offset = this.offset;
|
2014-10-02 08:57:57 +09:00
|
|
|
// Keep track of how many pages we should maximally iterate through.
|
2014-10-02 21:57:01 +09:00
|
|
|
this.pagesToSearch = numPages;
|
2014-06-26 07:15:22 +09:00
|
|
|
// If there's already a matchIdx that means we are iterating through a
|
|
|
|
// page's matches.
|
|
|
|
if (offset.matchIdx !== null) {
|
|
|
|
var numPageMatches = this.pageMatches[offset.pageIdx].length;
|
|
|
|
if ((!previous && offset.matchIdx + 1 < numPageMatches) ||
|
|
|
|
(previous && offset.matchIdx > 0)) {
|
|
|
|
// The simple case; we just have advance the matchIdx to select
|
|
|
|
// the next match on the page.
|
|
|
|
this.hadMatch = true;
|
|
|
|
offset.matchIdx = (previous ? offset.matchIdx - 1 :
|
|
|
|
offset.matchIdx + 1);
|
|
|
|
this.updateMatch(true);
|
|
|
|
return;
|
2013-06-19 01:05:55 +09:00
|
|
|
}
|
2014-06-26 07:15:22 +09:00
|
|
|
// We went beyond the current page's matches, so we advance to
|
|
|
|
// the next page.
|
|
|
|
this.advanceOffsetPage(previous);
|
2013-06-19 01:05:55 +09:00
|
|
|
}
|
2014-06-26 07:15:22 +09:00
|
|
|
// Start searching through the page.
|
|
|
|
this.nextPageMatch();
|
|
|
|
},
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
matchesReady: function PDFFindController_matchesReady(matches) {
|
|
|
|
var offset = this.offset;
|
|
|
|
var numMatches = matches.length;
|
|
|
|
var previous = this.state.findPrevious;
|
2013-06-19 01:05:55 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
if (numMatches) {
|
|
|
|
// There were matches for the page, so initialize the matchIdx.
|
2013-06-19 01:05:55 +09:00
|
|
|
this.hadMatch = true;
|
2014-06-26 07:15:22 +09:00
|
|
|
offset.matchIdx = (previous ? numMatches - 1 : 0);
|
2013-06-19 01:05:55 +09:00
|
|
|
this.updateMatch(true);
|
2014-06-26 07:15:22 +09:00
|
|
|
return true;
|
2016-12-16 21:05:33 +09:00
|
|
|
}
|
|
|
|
// No matches, so attempt to search the next page.
|
|
|
|
this.advanceOffsetPage(previous);
|
|
|
|
if (offset.wrapped) {
|
|
|
|
offset.matchIdx = null;
|
|
|
|
if (this.pagesToSearch < 0) {
|
|
|
|
// No point in wrapping again, there were no matches.
|
|
|
|
this.updateMatch(false);
|
|
|
|
// while matches were not found, searching for a page
|
|
|
|
// with matches should nevertheless halt.
|
|
|
|
return true;
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
|
|
|
}
|
2016-12-16 21:05:33 +09:00
|
|
|
// Matches were not found (and searching is not done).
|
|
|
|
return false;
|
2014-06-26 07:15:22 +09:00
|
|
|
},
|
|
|
|
|
2014-12-18 05:12:51 +09:00
|
|
|
/**
|
|
|
|
* The method is called back from the text layer when match presentation
|
|
|
|
* is updated.
|
|
|
|
* @param {number} pageIndex - page index.
|
|
|
|
* @param {number} index - match index.
|
|
|
|
* @param {Array} elements - text layer div elements array.
|
|
|
|
* @param {number} beginIdx - start index of the div array for the match.
|
|
|
|
*/
|
|
|
|
updateMatchPosition: function PDFFindController_updateMatchPosition(
|
2016-05-11 22:39:01 +09:00
|
|
|
pageIndex, index, elements, beginIdx) {
|
2014-12-18 05:12:51 +09:00
|
|
|
if (this.selected.matchIdx === index &&
|
|
|
|
this.selected.pageIdx === pageIndex) {
|
2015-10-13 19:47:07 +09:00
|
|
|
var spot = {
|
2014-12-18 05:12:51 +09:00
|
|
|
top: FIND_SCROLL_OFFSET_TOP,
|
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js
index 002dbf29..1de4e530 100644
--- a/web/pdf_thumbnail_view.js
+++ b/web/pdf_thumbnail_view.js
@@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
setPageLabel: function PDFThumbnailView_setPageLabel(label) {
this.pageLabel = (typeof label === 'string' ? label : null);
- this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}').
- then((msg) => {
+ this.l10n.get('thumb_page_title', { page: this.pageId, },
+ 'Page {{page}}').then((msg) => {
this.anchor.title = msg;
});
diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js
index 160e0410..6495fc5e 100644
--- a/web/secondary_toolbar.js
+++ b/web/secondary_toolbar.js
@@ -65,7 +65,8 @@ class SecondaryToolbar {
{ element: options.printButton, eventName: 'print', close: true, },
{ element: options.downloadButton, eventName: 'download', close: true, },
{ element: options.viewBookmarkButton, eventName: null, close: true, },
- { element: options.firstPageButton, eventName: 'firstpage', close: true, },
+ { element: options.firstPageButton, eventName: 'firstpage',
+ close: true, },
{ element: options.lastPageButton, eventName: 'lastpage', close: true, },
{ element: options.pageRotateCwButton, eventName: 'rotatecw',
close: false, },
@@ -76,7 +77,7 @@ class SecondaryToolbar {
{ element: options.cursorHandToolButton, eventName: 'switchcursortool',
eventDetails: { tool: CursorTool.HAND, }, close: true, },
{ element: options.documentPropertiesButton,
- eventName: 'documentproperties', close: true, }
+ eventName: 'documentproperties', close: true, },
];
this.items = {
firstPage: options.firstPageButton,
```
2017-06-01 19:46:12 +09:00
|
|
|
left: FIND_SCROLL_OFFSET_LEFT,
|
2015-10-13 19:47:07 +09:00
|
|
|
};
|
|
|
|
scrollIntoView(elements[beginIdx], spot,
|
|
|
|
/* skipOverflowHiddenElements = */ true);
|
2014-12-18 05:12:51 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
nextPageMatch: function PDFFindController_nextPageMatch() {
|
|
|
|
if (this.resumePageIdx !== null) {
|
|
|
|
console.error('There can only be one pending page.');
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
var pageIdx = this.offset.pageIdx;
|
|
|
|
var matches = this.pageMatches[pageIdx];
|
|
|
|
if (!matches) {
|
|
|
|
// The matches don't exist yet for processing by "matchesReady",
|
|
|
|
// so set a resume point for when they do exist.
|
|
|
|
this.resumePageIdx = pageIdx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!this.matchesReady(matches));
|
|
|
|
},
|
|
|
|
|
|
|
|
advanceOffsetPage: function PDFFindController_advanceOffsetPage(previous) {
|
|
|
|
var offset = this.offset;
|
|
|
|
var numPages = this.extractTextPromises.length;
|
|
|
|
offset.pageIdx = (previous ? offset.pageIdx - 1 : offset.pageIdx + 1);
|
|
|
|
offset.matchIdx = null;
|
2014-10-02 08:57:57 +09:00
|
|
|
|
|
|
|
this.pagesToSearch--;
|
2015-02-03 00:12:52 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
|
|
|
|
offset.pageIdx = (previous ? numPages - 1 : 0);
|
|
|
|
offset.wrapped = true;
|
2013-06-19 01:05:55 +09:00
|
|
|
}
|
2014-06-26 07:15:22 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
updateMatch: function PDFFindController_updateMatch(found) {
|
|
|
|
var state = FindStates.FIND_NOTFOUND;
|
|
|
|
var wrapped = this.offset.wrapped;
|
|
|
|
this.offset.wrapped = false;
|
2015-02-03 00:12:52 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
if (found) {
|
|
|
|
var previousPage = this.selected.pageIdx;
|
|
|
|
this.selected.pageIdx = this.offset.pageIdx;
|
|
|
|
this.selected.matchIdx = this.offset.matchIdx;
|
|
|
|
state = (wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND);
|
|
|
|
// Update the currently selected page to wipe out any selected matches.
|
|
|
|
if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
|
|
|
|
this.updatePage(previousPage);
|
2014-01-17 06:58:29 +09:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 00:12:52 +09:00
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
this.updateUIState(state, this.state.findPrevious);
|
|
|
|
if (this.selected.pageIdx !== -1) {
|
2014-09-15 23:49:24 +09:00
|
|
|
this.updatePage(this.selected.pageIdx);
|
2014-06-26 07:15:22 +09:00
|
|
|
}
|
|
|
|
},
|
2014-01-17 06:58:29 +09:00
|
|
|
|
2015-10-31 01:20:29 +09:00
|
|
|
updateUIResultsCount:
|
|
|
|
function PDFFindController_updateUIResultsCount() {
|
2016-04-14 04:39:29 +09:00
|
|
|
if (this.onUpdateResultsCount) {
|
|
|
|
this.onUpdateResultsCount(this.matchCount);
|
2015-10-31 01:20:29 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-26 07:15:22 +09:00
|
|
|
updateUIState: function PDFFindController_updateUIState(state, previous) {
|
2016-04-14 04:39:29 +09:00
|
|
|
if (this.onUpdateState) {
|
|
|
|
this.onUpdateState(state, previous, this.matchCount);
|
2013-06-19 01:05:55 +09:00
|
|
|
}
|
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js
index 002dbf29..1de4e530 100644
--- a/web/pdf_thumbnail_view.js
+++ b/web/pdf_thumbnail_view.js
@@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
setPageLabel: function PDFThumbnailView_setPageLabel(label) {
this.pageLabel = (typeof label === 'string' ? label : null);
- this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}').
- then((msg) => {
+ this.l10n.get('thumb_page_title', { page: this.pageId, },
+ 'Page {{page}}').then((msg) => {
this.anchor.title = msg;
});
diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js
index 160e0410..6495fc5e 100644
--- a/web/secondary_toolbar.js
+++ b/web/secondary_toolbar.js
@@ -65,7 +65,8 @@ class SecondaryToolbar {
{ element: options.printButton, eventName: 'print', close: true, },
{ element: options.downloadButton, eventName: 'download', close: true, },
{ element: options.viewBookmarkButton, eventName: null, close: true, },
- { element: options.firstPageButton, eventName: 'firstpage', close: true, },
+ { element: options.firstPageButton, eventName: 'firstpage',
+ close: true, },
{ element: options.lastPageButton, eventName: 'lastpage', close: true, },
{ element: options.pageRotateCwButton, eventName: 'rotatecw',
close: false, },
@@ -76,7 +77,7 @@ class SecondaryToolbar {
{ element: options.cursorHandToolButton, eventName: 'switchcursortool',
eventDetails: { tool: CursorTool.HAND, }, close: true, },
{ element: options.documentPropertiesButton,
- eventName: 'documentproperties', close: true, }
+ eventName: 'documentproperties', close: true, },
];
this.items = {
firstPage: options.firstPageButton,
```
2017-06-01 19:46:12 +09:00
|
|
|
},
|
2014-06-26 07:15:22 +09:00
|
|
|
};
|
|
|
|
return PDFFindController;
|
|
|
|
})();
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export {
|
|
|
|
FindStates,
|
|
|
|
PDFFindController,
|
|
|
|
};
|