text_layer_builder.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.DefaultTextLayerFactory = exports.TextLayerBuilder = undefined;
  20. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _dom_events = require('./dom_events');
  22. var _pdf = require('../pdf');
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. var EXPAND_DIVS_TIMEOUT = 300;
  25. var TextLayerBuilder = function () {
  26. function TextLayerBuilder(_ref) {
  27. var textLayerDiv = _ref.textLayerDiv,
  28. eventBus = _ref.eventBus,
  29. pageIndex = _ref.pageIndex,
  30. viewport = _ref.viewport,
  31. _ref$findController = _ref.findController,
  32. findController = _ref$findController === undefined ? null : _ref$findController,
  33. _ref$enhanceTextSelec = _ref.enhanceTextSelection,
  34. enhanceTextSelection = _ref$enhanceTextSelec === undefined ? false : _ref$enhanceTextSelec;
  35. _classCallCheck(this, TextLayerBuilder);
  36. this.textLayerDiv = textLayerDiv;
  37. this.eventBus = eventBus || (0, _dom_events.getGlobalEventBus)();
  38. this.textContent = null;
  39. this.textContentItemsStr = [];
  40. this.textContentStream = null;
  41. this.renderingDone = false;
  42. this.pageIdx = pageIndex;
  43. this.pageNumber = this.pageIdx + 1;
  44. this.matches = [];
  45. this.viewport = viewport;
  46. this.textDivs = [];
  47. this.findController = findController;
  48. this.textLayerRenderTask = null;
  49. this.enhanceTextSelection = enhanceTextSelection;
  50. this._bindMouse();
  51. }
  52. _createClass(TextLayerBuilder, [{
  53. key: '_finishRendering',
  54. value: function _finishRendering() {
  55. this.renderingDone = true;
  56. if (!this.enhanceTextSelection) {
  57. var endOfContent = document.createElement('div');
  58. endOfContent.className = 'endOfContent';
  59. this.textLayerDiv.appendChild(endOfContent);
  60. }
  61. this.eventBus.dispatch('textlayerrendered', {
  62. source: this,
  63. pageNumber: this.pageNumber,
  64. numTextDivs: this.textDivs.length
  65. });
  66. }
  67. }, {
  68. key: 'render',
  69. value: function render() {
  70. var _this = this;
  71. var timeout = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  72. if (!(this.textContent || this.textContentStream) || this.renderingDone) {
  73. return;
  74. }
  75. this.cancel();
  76. this.textDivs = [];
  77. var textLayerFrag = document.createDocumentFragment();
  78. this.textLayerRenderTask = (0, _pdf.renderTextLayer)({
  79. textContent: this.textContent,
  80. textContentStream: this.textContentStream,
  81. container: textLayerFrag,
  82. viewport: this.viewport,
  83. textDivs: this.textDivs,
  84. textContentItemsStr: this.textContentItemsStr,
  85. timeout: timeout,
  86. enhanceTextSelection: this.enhanceTextSelection
  87. });
  88. this.textLayerRenderTask.promise.then(function () {
  89. _this.textLayerDiv.appendChild(textLayerFrag);
  90. _this._finishRendering();
  91. _this.updateMatches();
  92. }, function (reason) {});
  93. }
  94. }, {
  95. key: 'cancel',
  96. value: function cancel() {
  97. if (this.textLayerRenderTask) {
  98. this.textLayerRenderTask.cancel();
  99. this.textLayerRenderTask = null;
  100. }
  101. }
  102. }, {
  103. key: 'setTextContentStream',
  104. value: function setTextContentStream(readableStream) {
  105. this.cancel();
  106. this.textContentStream = readableStream;
  107. }
  108. }, {
  109. key: 'setTextContent',
  110. value: function setTextContent(textContent) {
  111. this.cancel();
  112. this.textContent = textContent;
  113. }
  114. }, {
  115. key: 'convertMatches',
  116. value: function convertMatches(matches, matchesLength) {
  117. var i = 0;
  118. var iIndex = 0;
  119. var textContentItemsStr = this.textContentItemsStr;
  120. var end = textContentItemsStr.length - 1;
  121. var queryLen = this.findController === null ? 0 : this.findController.state.query.length;
  122. var ret = [];
  123. if (!matches) {
  124. return ret;
  125. }
  126. for (var m = 0, len = matches.length; m < len; m++) {
  127. var matchIdx = matches[m];
  128. while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
  129. iIndex += textContentItemsStr[i].length;
  130. i++;
  131. }
  132. if (i === textContentItemsStr.length) {
  133. console.error('Could not find a matching mapping');
  134. }
  135. var match = {
  136. begin: {
  137. divIdx: i,
  138. offset: matchIdx - iIndex
  139. }
  140. };
  141. if (matchesLength) {
  142. matchIdx += matchesLength[m];
  143. } else {
  144. matchIdx += queryLen;
  145. }
  146. while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
  147. iIndex += textContentItemsStr[i].length;
  148. i++;
  149. }
  150. match.end = {
  151. divIdx: i,
  152. offset: matchIdx - iIndex
  153. };
  154. ret.push(match);
  155. }
  156. return ret;
  157. }
  158. }, {
  159. key: 'renderMatches',
  160. value: function renderMatches(matches) {
  161. if (matches.length === 0) {
  162. return;
  163. }
  164. var textContentItemsStr = this.textContentItemsStr;
  165. var textDivs = this.textDivs;
  166. var prevEnd = null;
  167. var pageIdx = this.pageIdx;
  168. var isSelectedPage = this.findController === null ? false : pageIdx === this.findController.selected.pageIdx;
  169. var selectedMatchIdx = this.findController === null ? -1 : this.findController.selected.matchIdx;
  170. var highlightAll = this.findController === null ? false : this.findController.state.highlightAll;
  171. var infinity = {
  172. divIdx: -1,
  173. offset: undefined
  174. };
  175. function beginText(begin, className) {
  176. var divIdx = begin.divIdx;
  177. textDivs[divIdx].textContent = '';
  178. appendTextToDiv(divIdx, 0, begin.offset, className);
  179. }
  180. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  181. var div = textDivs[divIdx];
  182. var content = textContentItemsStr[divIdx].substring(fromOffset, toOffset);
  183. var node = document.createTextNode(content);
  184. if (className) {
  185. var span = document.createElement('span');
  186. span.className = className;
  187. span.appendChild(node);
  188. div.appendChild(span);
  189. return;
  190. }
  191. div.appendChild(node);
  192. }
  193. var i0 = selectedMatchIdx,
  194. i1 = i0 + 1;
  195. if (highlightAll) {
  196. i0 = 0;
  197. i1 = matches.length;
  198. } else if (!isSelectedPage) {
  199. return;
  200. }
  201. for (var i = i0; i < i1; i++) {
  202. var match = matches[i];
  203. var begin = match.begin;
  204. var end = match.end;
  205. var isSelected = isSelectedPage && i === selectedMatchIdx;
  206. var highlightSuffix = isSelected ? ' selected' : '';
  207. if (this.findController) {
  208. this.findController.updateMatchPosition(pageIdx, i, textDivs, begin.divIdx);
  209. }
  210. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  211. if (prevEnd !== null) {
  212. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  213. }
  214. beginText(begin);
  215. } else {
  216. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  217. }
  218. if (begin.divIdx === end.divIdx) {
  219. appendTextToDiv(begin.divIdx, begin.offset, end.offset, 'highlight' + highlightSuffix);
  220. } else {
  221. appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, 'highlight begin' + highlightSuffix);
  222. for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  223. textDivs[n0].className = 'highlight middle' + highlightSuffix;
  224. }
  225. beginText(end, 'highlight end' + highlightSuffix);
  226. }
  227. prevEnd = end;
  228. }
  229. if (prevEnd) {
  230. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  231. }
  232. }
  233. }, {
  234. key: 'updateMatches',
  235. value: function updateMatches() {
  236. if (!this.renderingDone) {
  237. return;
  238. }
  239. var matches = this.matches;
  240. var textDivs = this.textDivs;
  241. var textContentItemsStr = this.textContentItemsStr;
  242. var clearedUntilDivIdx = -1;
  243. for (var i = 0, len = matches.length; i < len; i++) {
  244. var match = matches[i];
  245. var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  246. for (var n = begin, end = match.end.divIdx; n <= end; n++) {
  247. var div = textDivs[n];
  248. div.textContent = textContentItemsStr[n];
  249. div.className = '';
  250. }
  251. clearedUntilDivIdx = match.end.divIdx + 1;
  252. }
  253. if (this.findController === null || !this.findController.active) {
  254. return;
  255. }
  256. var pageMatches = void 0,
  257. pageMatchesLength = void 0;
  258. if (this.findController !== null) {
  259. pageMatches = this.findController.pageMatches[this.pageIdx] || null;
  260. pageMatchesLength = this.findController.pageMatchesLength ? this.findController.pageMatchesLength[this.pageIdx] || null : null;
  261. }
  262. this.matches = this.convertMatches(pageMatches, pageMatchesLength);
  263. this.renderMatches(this.matches);
  264. }
  265. }, {
  266. key: '_bindMouse',
  267. value: function _bindMouse() {
  268. var _this2 = this;
  269. var div = this.textLayerDiv;
  270. var expandDivsTimer = null;
  271. div.addEventListener('mousedown', function (evt) {
  272. if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
  273. _this2.textLayerRenderTask.expandTextDivs(true);
  274. if (expandDivsTimer) {
  275. clearTimeout(expandDivsTimer);
  276. expandDivsTimer = null;
  277. }
  278. return;
  279. }
  280. var end = div.querySelector('.endOfContent');
  281. if (!end) {
  282. return;
  283. }
  284. var adjustTop = evt.target !== div;
  285. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue('-moz-user-select') !== 'none';
  286. if (adjustTop) {
  287. var divBounds = div.getBoundingClientRect();
  288. var r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  289. end.style.top = (r * 100).toFixed(2) + '%';
  290. }
  291. end.classList.add('active');
  292. });
  293. div.addEventListener('mouseup', function () {
  294. if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
  295. expandDivsTimer = setTimeout(function () {
  296. if (_this2.textLayerRenderTask) {
  297. _this2.textLayerRenderTask.expandTextDivs(false);
  298. }
  299. expandDivsTimer = null;
  300. }, EXPAND_DIVS_TIMEOUT);
  301. return;
  302. }
  303. var end = div.querySelector('.endOfContent');
  304. if (!end) {
  305. return;
  306. }
  307. end.style.top = '';
  308. end.classList.remove('active');
  309. });
  310. }
  311. }]);
  312. return TextLayerBuilder;
  313. }();
  314. var DefaultTextLayerFactory = function () {
  315. function DefaultTextLayerFactory() {
  316. _classCallCheck(this, DefaultTextLayerFactory);
  317. }
  318. _createClass(DefaultTextLayerFactory, [{
  319. key: 'createTextLayerBuilder',
  320. value: function createTextLayerBuilder(textLayerDiv, pageIndex, viewport) {
  321. var enhanceTextSelection = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  322. return new TextLayerBuilder({
  323. textLayerDiv: textLayerDiv,
  324. pageIndex: pageIndex,
  325. viewport: viewport,
  326. enhanceTextSelection: enhanceTextSelection
  327. });
  328. }
  329. }]);
  330. return DefaultTextLayerFactory;
  331. }();
  332. exports.TextLayerBuilder = TextLayerBuilder;
  333. exports.DefaultTextLayerFactory = DefaultTextLayerFactory;