text_layer_builder.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DefaultTextLayerFactory = exports.TextLayerBuilder = void 0;
  27. var _ui_utils = require("./ui_utils");
  28. var _pdf = require("../pdf");
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. 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); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. var EXPAND_DIVS_TIMEOUT = 300;
  33. var TextLayerBuilder =
  34. /*#__PURE__*/
  35. function () {
  36. function TextLayerBuilder(_ref) {
  37. var textLayerDiv = _ref.textLayerDiv,
  38. eventBus = _ref.eventBus,
  39. pageIndex = _ref.pageIndex,
  40. viewport = _ref.viewport,
  41. _ref$findController = _ref.findController,
  42. findController = _ref$findController === void 0 ? null : _ref$findController,
  43. _ref$enhanceTextSelec = _ref.enhanceTextSelection,
  44. enhanceTextSelection = _ref$enhanceTextSelec === void 0 ? false : _ref$enhanceTextSelec;
  45. _classCallCheck(this, TextLayerBuilder);
  46. this.textLayerDiv = textLayerDiv;
  47. this.eventBus = eventBus || (0, _ui_utils.getGlobalEventBus)();
  48. this.textContent = null;
  49. this.textContentItemsStr = [];
  50. this.textContentStream = null;
  51. this.renderingDone = false;
  52. this.pageIdx = pageIndex;
  53. this.pageNumber = this.pageIdx + 1;
  54. this.matches = [];
  55. this.viewport = viewport;
  56. this.textDivs = [];
  57. this.findController = findController;
  58. this.textLayerRenderTask = null;
  59. this.enhanceTextSelection = enhanceTextSelection;
  60. this._onUpdateTextLayerMatches = null;
  61. this._bindMouse();
  62. }
  63. _createClass(TextLayerBuilder, [{
  64. key: "_finishRendering",
  65. value: function _finishRendering() {
  66. this.renderingDone = true;
  67. if (!this.enhanceTextSelection) {
  68. var endOfContent = document.createElement('div');
  69. endOfContent.className = 'endOfContent';
  70. this.textLayerDiv.appendChild(endOfContent);
  71. }
  72. this.eventBus.dispatch('textlayerrendered', {
  73. source: this,
  74. pageNumber: this.pageNumber,
  75. numTextDivs: this.textDivs.length
  76. });
  77. }
  78. }, {
  79. key: "render",
  80. value: function render() {
  81. var _this = this;
  82. var timeout = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  83. if (!(this.textContent || this.textContentStream) || this.renderingDone) {
  84. return;
  85. }
  86. this.cancel();
  87. this.textDivs = [];
  88. var textLayerFrag = document.createDocumentFragment();
  89. this.textLayerRenderTask = (0, _pdf.renderTextLayer)({
  90. textContent: this.textContent,
  91. textContentStream: this.textContentStream,
  92. container: textLayerFrag,
  93. viewport: this.viewport,
  94. textDivs: this.textDivs,
  95. textContentItemsStr: this.textContentItemsStr,
  96. timeout: timeout,
  97. enhanceTextSelection: this.enhanceTextSelection
  98. });
  99. this.textLayerRenderTask.promise.then(function () {
  100. _this.textLayerDiv.appendChild(textLayerFrag);
  101. _this._finishRendering();
  102. _this._updateMatches();
  103. }, function (reason) {});
  104. if (!this._onUpdateTextLayerMatches) {
  105. this._onUpdateTextLayerMatches = function (evt) {
  106. if (evt.pageIndex === _this.pageIdx || evt.pageIndex === -1) {
  107. _this._updateMatches();
  108. }
  109. };
  110. this.eventBus.on('updatetextlayermatches', this._onUpdateTextLayerMatches);
  111. }
  112. }
  113. }, {
  114. key: "cancel",
  115. value: function cancel() {
  116. if (this.textLayerRenderTask) {
  117. this.textLayerRenderTask.cancel();
  118. this.textLayerRenderTask = null;
  119. }
  120. if (this._onUpdateTextLayerMatches) {
  121. this.eventBus.off('updatetextlayermatches', this._onUpdateTextLayerMatches);
  122. this._onUpdateTextLayerMatches = null;
  123. }
  124. }
  125. }, {
  126. key: "setTextContentStream",
  127. value: function setTextContentStream(readableStream) {
  128. this.cancel();
  129. this.textContentStream = readableStream;
  130. }
  131. }, {
  132. key: "setTextContent",
  133. value: function setTextContent(textContent) {
  134. this.cancel();
  135. this.textContent = textContent;
  136. }
  137. }, {
  138. key: "_convertMatches",
  139. value: function _convertMatches(matches, matchesLength) {
  140. if (!matches) {
  141. return [];
  142. }
  143. var findController = this.findController,
  144. textContentItemsStr = this.textContentItemsStr;
  145. var i = 0,
  146. iIndex = 0;
  147. var end = textContentItemsStr.length - 1;
  148. var queryLen = findController.state.query.length;
  149. var result = [];
  150. for (var m = 0, mm = matches.length; m < mm; m++) {
  151. var matchIdx = matches[m];
  152. while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
  153. iIndex += textContentItemsStr[i].length;
  154. i++;
  155. }
  156. if (i === textContentItemsStr.length) {
  157. console.error('Could not find a matching mapping');
  158. }
  159. var match = {
  160. begin: {
  161. divIdx: i,
  162. offset: matchIdx - iIndex
  163. }
  164. };
  165. if (matchesLength) {
  166. matchIdx += matchesLength[m];
  167. } else {
  168. matchIdx += queryLen;
  169. }
  170. while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
  171. iIndex += textContentItemsStr[i].length;
  172. i++;
  173. }
  174. match.end = {
  175. divIdx: i,
  176. offset: matchIdx - iIndex
  177. };
  178. result.push(match);
  179. }
  180. return result;
  181. }
  182. }, {
  183. key: "_renderMatches",
  184. value: function _renderMatches(matches) {
  185. if (matches.length === 0) {
  186. return;
  187. }
  188. var findController = this.findController,
  189. pageIdx = this.pageIdx,
  190. textContentItemsStr = this.textContentItemsStr,
  191. textDivs = this.textDivs;
  192. var isSelectedPage = pageIdx === findController.selected.pageIdx;
  193. var selectedMatchIdx = findController.selected.matchIdx;
  194. var highlightAll = findController.state.highlightAll;
  195. var prevEnd = null;
  196. var infinity = {
  197. divIdx: -1,
  198. offset: undefined
  199. };
  200. function beginText(begin, className) {
  201. var divIdx = begin.divIdx;
  202. textDivs[divIdx].textContent = '';
  203. appendTextToDiv(divIdx, 0, begin.offset, className);
  204. }
  205. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  206. var div = textDivs[divIdx];
  207. var content = textContentItemsStr[divIdx].substring(fromOffset, toOffset);
  208. var node = document.createTextNode(content);
  209. if (className) {
  210. var span = document.createElement('span');
  211. span.className = className;
  212. span.appendChild(node);
  213. div.appendChild(span);
  214. return;
  215. }
  216. div.appendChild(node);
  217. }
  218. var i0 = selectedMatchIdx,
  219. i1 = i0 + 1;
  220. if (highlightAll) {
  221. i0 = 0;
  222. i1 = matches.length;
  223. } else if (!isSelectedPage) {
  224. return;
  225. }
  226. for (var i = i0; i < i1; i++) {
  227. var match = matches[i];
  228. var begin = match.begin;
  229. var end = match.end;
  230. var isSelected = isSelectedPage && i === selectedMatchIdx;
  231. var highlightSuffix = isSelected ? ' selected' : '';
  232. if (isSelected) {
  233. findController.scrollMatchIntoView({
  234. element: textDivs[begin.divIdx],
  235. pageIndex: pageIdx,
  236. matchIndex: selectedMatchIdx
  237. });
  238. }
  239. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  240. if (prevEnd !== null) {
  241. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  242. }
  243. beginText(begin);
  244. } else {
  245. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  246. }
  247. if (begin.divIdx === end.divIdx) {
  248. appendTextToDiv(begin.divIdx, begin.offset, end.offset, 'highlight' + highlightSuffix);
  249. } else {
  250. appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, 'highlight begin' + highlightSuffix);
  251. for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  252. textDivs[n0].className = 'highlight middle' + highlightSuffix;
  253. }
  254. beginText(end, 'highlight end' + highlightSuffix);
  255. }
  256. prevEnd = end;
  257. }
  258. if (prevEnd) {
  259. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  260. }
  261. }
  262. }, {
  263. key: "_updateMatches",
  264. value: function _updateMatches() {
  265. if (!this.renderingDone) {
  266. return;
  267. }
  268. var findController = this.findController,
  269. matches = this.matches,
  270. pageIdx = this.pageIdx,
  271. textContentItemsStr = this.textContentItemsStr,
  272. textDivs = this.textDivs;
  273. var clearedUntilDivIdx = -1;
  274. for (var i = 0, ii = matches.length; i < ii; i++) {
  275. var match = matches[i];
  276. var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  277. for (var n = begin, end = match.end.divIdx; n <= end; n++) {
  278. var div = textDivs[n];
  279. div.textContent = textContentItemsStr[n];
  280. div.className = '';
  281. }
  282. clearedUntilDivIdx = match.end.divIdx + 1;
  283. }
  284. if (!findController || !findController.highlightMatches) {
  285. return;
  286. }
  287. var pageMatches = findController.pageMatches[pageIdx] || null;
  288. var pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
  289. this.matches = this._convertMatches(pageMatches, pageMatchesLength);
  290. this._renderMatches(this.matches);
  291. }
  292. }, {
  293. key: "_bindMouse",
  294. value: function _bindMouse() {
  295. var _this2 = this;
  296. var div = this.textLayerDiv;
  297. var expandDivsTimer = null;
  298. div.addEventListener('mousedown', function (evt) {
  299. if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
  300. _this2.textLayerRenderTask.expandTextDivs(true);
  301. if (expandDivsTimer) {
  302. clearTimeout(expandDivsTimer);
  303. expandDivsTimer = null;
  304. }
  305. return;
  306. }
  307. var end = div.querySelector('.endOfContent');
  308. if (!end) {
  309. return;
  310. }
  311. var adjustTop = evt.target !== div;
  312. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue('-moz-user-select') !== 'none';
  313. if (adjustTop) {
  314. var divBounds = div.getBoundingClientRect();
  315. var r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  316. end.style.top = (r * 100).toFixed(2) + '%';
  317. }
  318. end.classList.add('active');
  319. });
  320. div.addEventListener('mouseup', function () {
  321. if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
  322. expandDivsTimer = setTimeout(function () {
  323. if (_this2.textLayerRenderTask) {
  324. _this2.textLayerRenderTask.expandTextDivs(false);
  325. }
  326. expandDivsTimer = null;
  327. }, EXPAND_DIVS_TIMEOUT);
  328. return;
  329. }
  330. var end = div.querySelector('.endOfContent');
  331. if (!end) {
  332. return;
  333. }
  334. end.style.top = '';
  335. end.classList.remove('active');
  336. });
  337. }
  338. }]);
  339. return TextLayerBuilder;
  340. }();
  341. exports.TextLayerBuilder = TextLayerBuilder;
  342. var DefaultTextLayerFactory =
  343. /*#__PURE__*/
  344. function () {
  345. function DefaultTextLayerFactory() {
  346. _classCallCheck(this, DefaultTextLayerFactory);
  347. }
  348. _createClass(DefaultTextLayerFactory, [{
  349. key: "createTextLayerBuilder",
  350. value: function createTextLayerBuilder(textLayerDiv, pageIndex, viewport) {
  351. var enhanceTextSelection = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  352. return new TextLayerBuilder({
  353. textLayerDiv: textLayerDiv,
  354. pageIndex: pageIndex,
  355. viewport: viewport,
  356. enhanceTextSelection: enhanceTextSelection
  357. });
  358. }
  359. }]);
  360. return DefaultTextLayerFactory;
  361. }();
  362. exports.DefaultTextLayerFactory = DefaultTextLayerFactory;