2
0

pdf_find_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. var _this = this;
  54. this.startedTextExtraction = false;
  55. this.extractTextPromises = [];
  56. this.pendingFindMatches = Object.create(null);
  57. this.active = false;
  58. this.pageContents = [];
  59. this.pageMatches = [];
  60. this.pageMatchesLength = null;
  61. this.matchCount = 0;
  62. this.selected = {
  63. pageIdx: -1,
  64. matchIdx: -1
  65. };
  66. this.offset = {
  67. pageIdx: null,
  68. matchIdx: null
  69. };
  70. this.pagesToSearch = null;
  71. this.resumePageIdx = null;
  72. this.state = null;
  73. this.dirtyMatch = false;
  74. this.findTimeout = null;
  75. this._firstPagePromise = new Promise(function (resolve) {
  76. _this.resolveFirstPage = resolve;
  77. });
  78. },
  79. normalize: function PDFFindController_normalize(text) {
  80. return text.replace(this.normalizationRegex, function (ch) {
  81. return CHARACTERS_TO_NORMALIZE[ch];
  82. });
  83. },
  84. _prepareMatches: function PDFFindController_prepareMatches(matchesWithLength, matches, matchesLength) {
  85. function isSubTerm(matchesWithLength, currentIndex) {
  86. var currentElem, prevElem, nextElem;
  87. currentElem = matchesWithLength[currentIndex];
  88. nextElem = matchesWithLength[currentIndex + 1];
  89. if (currentIndex < matchesWithLength.length - 1 && currentElem.match === nextElem.match) {
  90. currentElem.skipped = true;
  91. return true;
  92. }
  93. for (var i = currentIndex - 1; i >= 0; i--) {
  94. prevElem = matchesWithLength[i];
  95. if (prevElem.skipped) {
  96. continue;
  97. }
  98. if (prevElem.match + prevElem.matchLength < currentElem.match) {
  99. break;
  100. }
  101. if (prevElem.match + prevElem.matchLength >= currentElem.match + currentElem.matchLength) {
  102. currentElem.skipped = true;
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. var i, len;
  109. matchesWithLength.sort(function (a, b) {
  110. return a.match === b.match ? a.matchLength - b.matchLength : a.match - b.match;
  111. });
  112. for (i = 0, len = matchesWithLength.length; i < len; i++) {
  113. if (isSubTerm(matchesWithLength, i)) {
  114. continue;
  115. }
  116. matches.push(matchesWithLength[i].match);
  117. matchesLength.push(matchesWithLength[i].matchLength);
  118. }
  119. },
  120. calcFindPhraseMatch: function PDFFindController_calcFindPhraseMatch(query, pageIndex, pageContent) {
  121. var matches = [];
  122. var queryLen = query.length;
  123. var matchIdx = -queryLen;
  124. while (true) {
  125. matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
  126. if (matchIdx === -1) {
  127. break;
  128. }
  129. matches.push(matchIdx);
  130. }
  131. this.pageMatches[pageIndex] = matches;
  132. },
  133. calcFindWordMatch: function PDFFindController_calcFindWordMatch(query, pageIndex, pageContent) {
  134. var matchesWithLength = [];
  135. var queryArray = query.match(/\S+/g);
  136. var subquery, subqueryLen, matchIdx;
  137. for (var i = 0, len = queryArray.length; i < len; i++) {
  138. subquery = queryArray[i];
  139. subqueryLen = subquery.length;
  140. matchIdx = -subqueryLen;
  141. while (true) {
  142. matchIdx = pageContent.indexOf(subquery, matchIdx + subqueryLen);
  143. if (matchIdx === -1) {
  144. break;
  145. }
  146. matchesWithLength.push({
  147. match: matchIdx,
  148. matchLength: subqueryLen,
  149. skipped: false
  150. });
  151. }
  152. }
  153. if (!this.pageMatchesLength) {
  154. this.pageMatchesLength = [];
  155. }
  156. this.pageMatchesLength[pageIndex] = [];
  157. this.pageMatches[pageIndex] = [];
  158. this._prepareMatches(matchesWithLength, this.pageMatches[pageIndex], this.pageMatchesLength[pageIndex]);
  159. },
  160. calcFindMatch: function PDFFindController_calcFindMatch(pageIndex) {
  161. var pageContent = this.normalize(this.pageContents[pageIndex]);
  162. var query = this.normalize(this.state.query);
  163. var caseSensitive = this.state.caseSensitive;
  164. var phraseSearch = this.state.phraseSearch;
  165. var queryLen = query.length;
  166. if (queryLen === 0) {
  167. return;
  168. }
  169. if (!caseSensitive) {
  170. pageContent = pageContent.toLowerCase();
  171. query = query.toLowerCase();
  172. }
  173. if (phraseSearch) {
  174. this.calcFindPhraseMatch(query, pageIndex, pageContent);
  175. } else {
  176. this.calcFindWordMatch(query, pageIndex, pageContent);
  177. }
  178. this.updatePage(pageIndex);
  179. if (this.resumePageIdx === pageIndex) {
  180. this.resumePageIdx = null;
  181. this.nextPageMatch();
  182. }
  183. if (this.pageMatches[pageIndex].length > 0) {
  184. this.matchCount += this.pageMatches[pageIndex].length;
  185. this.updateUIResultsCount();
  186. }
  187. },
  188. extractText: function PDFFindController_extractText() {
  189. if (this.startedTextExtraction) {
  190. return;
  191. }
  192. this.startedTextExtraction = true;
  193. this.pageContents = [];
  194. var extractTextPromisesResolves = [];
  195. var numPages = this.pdfViewer.pagesCount;
  196. for (var i = 0; i < numPages; i++) {
  197. this.extractTextPromises.push(new Promise(function (resolve) {
  198. extractTextPromisesResolves.push(resolve);
  199. }));
  200. }
  201. var self = this;
  202. function extractPageText(pageIndex) {
  203. self.pdfViewer.getPageTextContent(pageIndex).then(function textContentResolved(textContent) {
  204. var textItems = textContent.items;
  205. var str = [];
  206. for (var i = 0, len = textItems.length; i < len; i++) {
  207. str.push(textItems[i].str);
  208. }
  209. self.pageContents.push(str.join(''));
  210. extractTextPromisesResolves[pageIndex](pageIndex);
  211. if (pageIndex + 1 < self.pdfViewer.pagesCount) {
  212. extractPageText(pageIndex + 1);
  213. }
  214. });
  215. }
  216. extractPageText(0);
  217. },
  218. executeCommand: function PDFFindController_executeCommand(cmd, state) {
  219. if (this.state === null || cmd !== 'findagain') {
  220. this.dirtyMatch = true;
  221. }
  222. this.state = state;
  223. this.updateUIState(FindStates.FIND_PENDING);
  224. this._firstPagePromise.then(function () {
  225. this.extractText();
  226. clearTimeout(this.findTimeout);
  227. if (cmd === 'find') {
  228. this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
  229. } else {
  230. this.nextMatch();
  231. }
  232. }.bind(this));
  233. },
  234. updatePage: function PDFFindController_updatePage(index) {
  235. if (this.selected.pageIdx === index) {
  236. this.pdfViewer.currentPageNumber = index + 1;
  237. }
  238. var page = this.pdfViewer.getPageView(index);
  239. if (page.textLayer) {
  240. page.textLayer.updateMatches();
  241. }
  242. },
  243. nextMatch: function PDFFindController_nextMatch() {
  244. var previous = this.state.findPrevious;
  245. var currentPageIndex = this.pdfViewer.currentPageNumber - 1;
  246. var numPages = this.pdfViewer.pagesCount;
  247. this.active = true;
  248. if (this.dirtyMatch) {
  249. this.dirtyMatch = false;
  250. this.selected.pageIdx = this.selected.matchIdx = -1;
  251. this.offset.pageIdx = currentPageIndex;
  252. this.offset.matchIdx = null;
  253. this.hadMatch = false;
  254. this.resumePageIdx = null;
  255. this.pageMatches = [];
  256. this.matchCount = 0;
  257. this.pageMatchesLength = null;
  258. var self = this;
  259. for (var i = 0; i < numPages; i++) {
  260. this.updatePage(i);
  261. if (!(i in this.pendingFindMatches)) {
  262. this.pendingFindMatches[i] = true;
  263. this.extractTextPromises[i].then(function (pageIdx) {
  264. delete self.pendingFindMatches[pageIdx];
  265. self.calcFindMatch(pageIdx);
  266. });
  267. }
  268. }
  269. }
  270. if (this.state.query === '') {
  271. this.updateUIState(FindStates.FIND_FOUND);
  272. return;
  273. }
  274. if (this.resumePageIdx) {
  275. return;
  276. }
  277. var offset = this.offset;
  278. this.pagesToSearch = numPages;
  279. if (offset.matchIdx !== null) {
  280. var numPageMatches = this.pageMatches[offset.pageIdx].length;
  281. if (!previous && offset.matchIdx + 1 < numPageMatches || previous && offset.matchIdx > 0) {
  282. this.hadMatch = true;
  283. offset.matchIdx = previous ? offset.matchIdx - 1 : offset.matchIdx + 1;
  284. this.updateMatch(true);
  285. return;
  286. }
  287. this.advanceOffsetPage(previous);
  288. }
  289. this.nextPageMatch();
  290. },
  291. matchesReady: function PDFFindController_matchesReady(matches) {
  292. var offset = this.offset;
  293. var numMatches = matches.length;
  294. var previous = this.state.findPrevious;
  295. if (numMatches) {
  296. this.hadMatch = true;
  297. offset.matchIdx = previous ? numMatches - 1 : 0;
  298. this.updateMatch(true);
  299. return true;
  300. }
  301. this.advanceOffsetPage(previous);
  302. if (offset.wrapped) {
  303. offset.matchIdx = null;
  304. if (this.pagesToSearch < 0) {
  305. this.updateMatch(false);
  306. return true;
  307. }
  308. }
  309. return false;
  310. },
  311. updateMatchPosition: function PDFFindController_updateMatchPosition(pageIndex, index, elements, beginIdx) {
  312. if (this.selected.matchIdx === index && this.selected.pageIdx === pageIndex) {
  313. var spot = {
  314. top: FIND_SCROLL_OFFSET_TOP,
  315. left: FIND_SCROLL_OFFSET_LEFT
  316. };
  317. (0, _ui_utils.scrollIntoView)(elements[beginIdx], spot, true);
  318. }
  319. },
  320. nextPageMatch: function PDFFindController_nextPageMatch() {
  321. if (this.resumePageIdx !== null) {
  322. console.error('There can only be one pending page.');
  323. }
  324. do {
  325. var pageIdx = this.offset.pageIdx;
  326. var matches = this.pageMatches[pageIdx];
  327. if (!matches) {
  328. this.resumePageIdx = pageIdx;
  329. break;
  330. }
  331. } while (!this.matchesReady(matches));
  332. },
  333. advanceOffsetPage: function PDFFindController_advanceOffsetPage(previous) {
  334. var offset = this.offset;
  335. var numPages = this.extractTextPromises.length;
  336. offset.pageIdx = previous ? offset.pageIdx - 1 : offset.pageIdx + 1;
  337. offset.matchIdx = null;
  338. this.pagesToSearch--;
  339. if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
  340. offset.pageIdx = previous ? numPages - 1 : 0;
  341. offset.wrapped = true;
  342. }
  343. },
  344. updateMatch: function PDFFindController_updateMatch(found) {
  345. var state = FindStates.FIND_NOTFOUND;
  346. var wrapped = this.offset.wrapped;
  347. this.offset.wrapped = false;
  348. if (found) {
  349. var previousPage = this.selected.pageIdx;
  350. this.selected.pageIdx = this.offset.pageIdx;
  351. this.selected.matchIdx = this.offset.matchIdx;
  352. state = wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND;
  353. if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
  354. this.updatePage(previousPage);
  355. }
  356. }
  357. this.updateUIState(state, this.state.findPrevious);
  358. if (this.selected.pageIdx !== -1) {
  359. this.updatePage(this.selected.pageIdx);
  360. }
  361. },
  362. updateUIResultsCount: function PDFFindController_updateUIResultsCount() {
  363. if (this.onUpdateResultsCount) {
  364. this.onUpdateResultsCount(this.matchCount);
  365. }
  366. },
  367. updateUIState: function PDFFindController_updateUIState(state, previous) {
  368. if (this.onUpdateState) {
  369. this.onUpdateState(state, previous, this.matchCount);
  370. }
  371. }
  372. };
  373. return PDFFindController;
  374. }();
  375. exports.FindStates = FindStates;
  376. exports.PDFFindController = PDFFindController;