pdf_find_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFFindController = exports.FindStates = undefined;
  20. var _ui_utils = require('./ui_utils');
  21. var FindStates = {
  22. FIND_FOUND: 0,
  23. FIND_NOTFOUND: 1,
  24. FIND_WRAPPED: 2,
  25. FIND_PENDING: 3
  26. };
  27. var FIND_SCROLL_OFFSET_TOP = -50;
  28. var FIND_SCROLL_OFFSET_LEFT = -400;
  29. var CHARACTERS_TO_NORMALIZE = {
  30. '\u2018': '\'',
  31. '\u2019': '\'',
  32. '\u201A': '\'',
  33. '\u201B': '\'',
  34. '\u201C': '"',
  35. '\u201D': '"',
  36. '\u201E': '"',
  37. '\u201F': '"',
  38. '\xBC': '1/4',
  39. '\xBD': '1/2',
  40. '\xBE': '3/4'
  41. };
  42. var PDFFindController = function PDFFindControllerClosure() {
  43. function PDFFindController(options) {
  44. this.pdfViewer = options.pdfViewer || null;
  45. this.onUpdateResultsCount = null;
  46. this.onUpdateState = null;
  47. this.reset();
  48. var replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
  49. this.normalizationRegex = new RegExp('[' + replace + ']', 'g');
  50. }
  51. PDFFindController.prototype = {
  52. reset: function PDFFindController_reset() {
  53. this.startedTextExtraction = false;
  54. this.extractTextPromises = [];
  55. this.pendingFindMatches = Object.create(null);
  56. this.active = false;
  57. this.pageContents = [];
  58. this.pageMatches = [];
  59. this.pageMatchesLength = null;
  60. this.matchCount = 0;
  61. this.selected = {
  62. pageIdx: -1,
  63. matchIdx: -1
  64. };
  65. this.offset = {
  66. pageIdx: null,
  67. matchIdx: null
  68. };
  69. this.pagesToSearch = null;
  70. this.resumePageIdx = null;
  71. this.state = null;
  72. this.dirtyMatch = false;
  73. this.findTimeout = null;
  74. this.firstPagePromise = new Promise(function (resolve) {
  75. this.resolveFirstPage = resolve;
  76. }.bind(this));
  77. },
  78. normalize: function PDFFindController_normalize(text) {
  79. return text.replace(this.normalizationRegex, function (ch) {
  80. return CHARACTERS_TO_NORMALIZE[ch];
  81. });
  82. },
  83. _prepareMatches: function PDFFindController_prepareMatches(matchesWithLength, matches, matchesLength) {
  84. function isSubTerm(matchesWithLength, currentIndex) {
  85. var currentElem, prevElem, nextElem;
  86. currentElem = matchesWithLength[currentIndex];
  87. nextElem = matchesWithLength[currentIndex + 1];
  88. if (currentIndex < matchesWithLength.length - 1 && currentElem.match === nextElem.match) {
  89. currentElem.skipped = true;
  90. return true;
  91. }
  92. for (var i = currentIndex - 1; i >= 0; i--) {
  93. prevElem = matchesWithLength[i];
  94. if (prevElem.skipped) {
  95. continue;
  96. }
  97. if (prevElem.match + prevElem.matchLength < currentElem.match) {
  98. break;
  99. }
  100. if (prevElem.match + prevElem.matchLength >= currentElem.match + currentElem.matchLength) {
  101. currentElem.skipped = true;
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. var i, len;
  108. matchesWithLength.sort(function (a, b) {
  109. return a.match === b.match ? a.matchLength - b.matchLength : a.match - b.match;
  110. });
  111. for (i = 0, len = matchesWithLength.length; i < len; i++) {
  112. if (isSubTerm(matchesWithLength, i)) {
  113. continue;
  114. }
  115. matches.push(matchesWithLength[i].match);
  116. matchesLength.push(matchesWithLength[i].matchLength);
  117. }
  118. },
  119. calcFindPhraseMatch: function PDFFindController_calcFindPhraseMatch(query, pageIndex, pageContent) {
  120. var matches = [];
  121. var queryLen = query.length;
  122. var matchIdx = -queryLen;
  123. while (true) {
  124. matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
  125. if (matchIdx === -1) {
  126. break;
  127. }
  128. matches.push(matchIdx);
  129. }
  130. this.pageMatches[pageIndex] = matches;
  131. },
  132. calcFindWordMatch: function PDFFindController_calcFindWordMatch(query, pageIndex, pageContent) {
  133. var matchesWithLength = [];
  134. var queryArray = query.match(/\S+/g);
  135. var subquery, subqueryLen, matchIdx;
  136. for (var i = 0, len = queryArray.length; i < len; i++) {
  137. subquery = queryArray[i];
  138. subqueryLen = subquery.length;
  139. matchIdx = -subqueryLen;
  140. while (true) {
  141. matchIdx = pageContent.indexOf(subquery, matchIdx + subqueryLen);
  142. if (matchIdx === -1) {
  143. break;
  144. }
  145. matchesWithLength.push({
  146. match: matchIdx,
  147. matchLength: subqueryLen,
  148. skipped: false
  149. });
  150. }
  151. }
  152. if (!this.pageMatchesLength) {
  153. this.pageMatchesLength = [];
  154. }
  155. this.pageMatchesLength[pageIndex] = [];
  156. this.pageMatches[pageIndex] = [];
  157. this._prepareMatches(matchesWithLength, this.pageMatches[pageIndex], this.pageMatchesLength[pageIndex]);
  158. },
  159. calcFindMatch: function PDFFindController_calcFindMatch(pageIndex) {
  160. var pageContent = this.normalize(this.pageContents[pageIndex]);
  161. var query = this.normalize(this.state.query);
  162. var caseSensitive = this.state.caseSensitive;
  163. var phraseSearch = this.state.phraseSearch;
  164. var queryLen = query.length;
  165. if (queryLen === 0) {
  166. return;
  167. }
  168. if (!caseSensitive) {
  169. pageContent = pageContent.toLowerCase();
  170. query = query.toLowerCase();
  171. }
  172. if (phraseSearch) {
  173. this.calcFindPhraseMatch(query, pageIndex, pageContent);
  174. } else {
  175. this.calcFindWordMatch(query, pageIndex, pageContent);
  176. }
  177. this.updatePage(pageIndex);
  178. if (this.resumePageIdx === pageIndex) {
  179. this.resumePageIdx = null;
  180. this.nextPageMatch();
  181. }
  182. if (this.pageMatches[pageIndex].length > 0) {
  183. this.matchCount += this.pageMatches[pageIndex].length;
  184. this.updateUIResultsCount();
  185. }
  186. },
  187. extractText: function PDFFindController_extractText() {
  188. if (this.startedTextExtraction) {
  189. return;
  190. }
  191. this.startedTextExtraction = true;
  192. this.pageContents = [];
  193. var extractTextPromisesResolves = [];
  194. var numPages = this.pdfViewer.pagesCount;
  195. for (var i = 0; i < numPages; i++) {
  196. this.extractTextPromises.push(new Promise(function (resolve) {
  197. extractTextPromisesResolves.push(resolve);
  198. }));
  199. }
  200. var self = this;
  201. function extractPageText(pageIndex) {
  202. self.pdfViewer.getPageTextContent(pageIndex).then(function textContentResolved(textContent) {
  203. var textItems = textContent.items;
  204. var str = [];
  205. for (var i = 0, len = textItems.length; i < len; i++) {
  206. str.push(textItems[i].str);
  207. }
  208. self.pageContents.push(str.join(''));
  209. extractTextPromisesResolves[pageIndex](pageIndex);
  210. if (pageIndex + 1 < self.pdfViewer.pagesCount) {
  211. extractPageText(pageIndex + 1);
  212. }
  213. });
  214. }
  215. extractPageText(0);
  216. },
  217. executeCommand: function PDFFindController_executeCommand(cmd, state) {
  218. if (this.state === null || cmd !== 'findagain') {
  219. this.dirtyMatch = true;
  220. }
  221. this.state = state;
  222. this.updateUIState(FindStates.FIND_PENDING);
  223. this.firstPagePromise.then(function () {
  224. this.extractText();
  225. clearTimeout(this.findTimeout);
  226. if (cmd === 'find') {
  227. this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
  228. } else {
  229. this.nextMatch();
  230. }
  231. }.bind(this));
  232. },
  233. updatePage: function PDFFindController_updatePage(index) {
  234. if (this.selected.pageIdx === index) {
  235. this.pdfViewer.currentPageNumber = index + 1;
  236. }
  237. var page = this.pdfViewer.getPageView(index);
  238. if (page.textLayer) {
  239. page.textLayer.updateMatches();
  240. }
  241. },
  242. nextMatch: function PDFFindController_nextMatch() {
  243. var previous = this.state.findPrevious;
  244. var currentPageIndex = this.pdfViewer.currentPageNumber - 1;
  245. var numPages = this.pdfViewer.pagesCount;
  246. this.active = true;
  247. if (this.dirtyMatch) {
  248. this.dirtyMatch = false;
  249. this.selected.pageIdx = this.selected.matchIdx = -1;
  250. this.offset.pageIdx = currentPageIndex;
  251. this.offset.matchIdx = null;
  252. this.hadMatch = false;
  253. this.resumePageIdx = null;
  254. this.pageMatches = [];
  255. this.matchCount = 0;
  256. this.pageMatchesLength = null;
  257. var self = this;
  258. for (var i = 0; i < numPages; i++) {
  259. this.updatePage(i);
  260. if (!(i in this.pendingFindMatches)) {
  261. this.pendingFindMatches[i] = true;
  262. this.extractTextPromises[i].then(function (pageIdx) {
  263. delete self.pendingFindMatches[pageIdx];
  264. self.calcFindMatch(pageIdx);
  265. });
  266. }
  267. }
  268. }
  269. if (this.state.query === '') {
  270. this.updateUIState(FindStates.FIND_FOUND);
  271. return;
  272. }
  273. if (this.resumePageIdx) {
  274. return;
  275. }
  276. var offset = this.offset;
  277. this.pagesToSearch = numPages;
  278. if (offset.matchIdx !== null) {
  279. var numPageMatches = this.pageMatches[offset.pageIdx].length;
  280. if (!previous && offset.matchIdx + 1 < numPageMatches || previous && offset.matchIdx > 0) {
  281. this.hadMatch = true;
  282. offset.matchIdx = previous ? offset.matchIdx - 1 : offset.matchIdx + 1;
  283. this.updateMatch(true);
  284. return;
  285. }
  286. this.advanceOffsetPage(previous);
  287. }
  288. this.nextPageMatch();
  289. },
  290. matchesReady: function PDFFindController_matchesReady(matches) {
  291. var offset = this.offset;
  292. var numMatches = matches.length;
  293. var previous = this.state.findPrevious;
  294. if (numMatches) {
  295. this.hadMatch = true;
  296. offset.matchIdx = previous ? numMatches - 1 : 0;
  297. this.updateMatch(true);
  298. return true;
  299. }
  300. this.advanceOffsetPage(previous);
  301. if (offset.wrapped) {
  302. offset.matchIdx = null;
  303. if (this.pagesToSearch < 0) {
  304. this.updateMatch(false);
  305. return true;
  306. }
  307. }
  308. return false;
  309. },
  310. updateMatchPosition: function PDFFindController_updateMatchPosition(pageIndex, index, elements, beginIdx) {
  311. if (this.selected.matchIdx === index && this.selected.pageIdx === pageIndex) {
  312. var spot = {
  313. top: FIND_SCROLL_OFFSET_TOP,
  314. left: FIND_SCROLL_OFFSET_LEFT
  315. };
  316. (0, _ui_utils.scrollIntoView)(elements[beginIdx], spot, true);
  317. }
  318. },
  319. nextPageMatch: function PDFFindController_nextPageMatch() {
  320. if (this.resumePageIdx !== null) {
  321. console.error('There can only be one pending page.');
  322. }
  323. do {
  324. var pageIdx = this.offset.pageIdx;
  325. var matches = this.pageMatches[pageIdx];
  326. if (!matches) {
  327. this.resumePageIdx = pageIdx;
  328. break;
  329. }
  330. } while (!this.matchesReady(matches));
  331. },
  332. advanceOffsetPage: function PDFFindController_advanceOffsetPage(previous) {
  333. var offset = this.offset;
  334. var numPages = this.extractTextPromises.length;
  335. offset.pageIdx = previous ? offset.pageIdx - 1 : offset.pageIdx + 1;
  336. offset.matchIdx = null;
  337. this.pagesToSearch--;
  338. if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
  339. offset.pageIdx = previous ? numPages - 1 : 0;
  340. offset.wrapped = true;
  341. }
  342. },
  343. updateMatch: function PDFFindController_updateMatch(found) {
  344. var state = FindStates.FIND_NOTFOUND;
  345. var wrapped = this.offset.wrapped;
  346. this.offset.wrapped = false;
  347. if (found) {
  348. var previousPage = this.selected.pageIdx;
  349. this.selected.pageIdx = this.offset.pageIdx;
  350. this.selected.matchIdx = this.offset.matchIdx;
  351. state = wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND;
  352. if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
  353. this.updatePage(previousPage);
  354. }
  355. }
  356. this.updateUIState(state, this.state.findPrevious);
  357. if (this.selected.pageIdx !== -1) {
  358. this.updatePage(this.selected.pageIdx);
  359. }
  360. },
  361. updateUIResultsCount: function PDFFindController_updateUIResultsCount() {
  362. if (this.onUpdateResultsCount) {
  363. this.onUpdateResultsCount(this.matchCount);
  364. }
  365. },
  366. updateUIState: function PDFFindController_updateUIState(state, previous) {
  367. if (this.onUpdateState) {
  368. this.onUpdateState(state, previous, this.matchCount);
  369. }
  370. }
  371. };
  372. return PDFFindController;
  373. }();
  374. exports.FindStates = FindStates;
  375. exports.PDFFindController = PDFFindController;