1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.DefaultTextLayerFactory = exports.TextLayerBuilder = void 0;
|
28 |
|
29 | var _ui_utils = require("./ui_utils");
|
30 |
|
31 | var _pdf = require("../pdf");
|
32 |
|
33 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
34 |
|
35 | 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); } }
|
36 |
|
37 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
38 |
|
39 | var EXPAND_DIVS_TIMEOUT = 300;
|
40 |
|
41 | var TextLayerBuilder =
|
42 |
|
43 | function () {
|
44 | function TextLayerBuilder(_ref) {
|
45 | var textLayerDiv = _ref.textLayerDiv,
|
46 | eventBus = _ref.eventBus,
|
47 | pageIndex = _ref.pageIndex,
|
48 | viewport = _ref.viewport,
|
49 | _ref$findController = _ref.findController,
|
50 | findController = _ref$findController === void 0 ? null : _ref$findController,
|
51 | _ref$enhanceTextSelec = _ref.enhanceTextSelection,
|
52 | enhanceTextSelection = _ref$enhanceTextSelec === void 0 ? false : _ref$enhanceTextSelec;
|
53 |
|
54 | _classCallCheck(this, TextLayerBuilder);
|
55 |
|
56 | this.textLayerDiv = textLayerDiv;
|
57 | this.eventBus = eventBus || (0, _ui_utils.getGlobalEventBus)();
|
58 | this.textContent = null;
|
59 | this.textContentItemsStr = [];
|
60 | this.textContentStream = null;
|
61 | this.renderingDone = false;
|
62 | this.pageIdx = pageIndex;
|
63 | this.pageNumber = this.pageIdx + 1;
|
64 | this.matches = [];
|
65 | this.viewport = viewport;
|
66 | this.textDivs = [];
|
67 | this.findController = findController;
|
68 | this.textLayerRenderTask = null;
|
69 | this.enhanceTextSelection = enhanceTextSelection;
|
70 | this._onUpdateTextLayerMatches = null;
|
71 |
|
72 | this._bindMouse();
|
73 | }
|
74 |
|
75 | _createClass(TextLayerBuilder, [{
|
76 | key: "_finishRendering",
|
77 | value: function _finishRendering() {
|
78 | this.renderingDone = true;
|
79 |
|
80 | if (!this.enhanceTextSelection) {
|
81 | var endOfContent = document.createElement('div');
|
82 | endOfContent.className = 'endOfContent';
|
83 | this.textLayerDiv.appendChild(endOfContent);
|
84 | }
|
85 |
|
86 | this.eventBus.dispatch('textlayerrendered', {
|
87 | source: this,
|
88 | pageNumber: this.pageNumber,
|
89 | numTextDivs: this.textDivs.length
|
90 | });
|
91 | }
|
92 | }, {
|
93 | key: "render",
|
94 | value: function render() {
|
95 | var _this = this;
|
96 |
|
97 | var timeout = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
98 |
|
99 | if (!(this.textContent || this.textContentStream) || this.renderingDone) {
|
100 | return;
|
101 | }
|
102 |
|
103 | this.cancel();
|
104 | this.textDivs = [];
|
105 | var textLayerFrag = document.createDocumentFragment();
|
106 | this.textLayerRenderTask = (0, _pdf.renderTextLayer)({
|
107 | textContent: this.textContent,
|
108 | textContentStream: this.textContentStream,
|
109 | container: textLayerFrag,
|
110 | viewport: this.viewport,
|
111 | textDivs: this.textDivs,
|
112 | textContentItemsStr: this.textContentItemsStr,
|
113 | timeout: timeout,
|
114 | enhanceTextSelection: this.enhanceTextSelection
|
115 | });
|
116 | this.textLayerRenderTask.promise.then(function () {
|
117 | _this.textLayerDiv.appendChild(textLayerFrag);
|
118 |
|
119 | _this._finishRendering();
|
120 |
|
121 | _this._updateMatches();
|
122 | }, function (reason) {});
|
123 |
|
124 | if (!this._onUpdateTextLayerMatches) {
|
125 | this._onUpdateTextLayerMatches = function (evt) {
|
126 | if (evt.pageIndex === _this.pageIdx || evt.pageIndex === -1) {
|
127 | _this._updateMatches();
|
128 | }
|
129 | };
|
130 |
|
131 | this.eventBus.on('updatetextlayermatches', this._onUpdateTextLayerMatches);
|
132 | }
|
133 | }
|
134 | }, {
|
135 | key: "cancel",
|
136 | value: function cancel() {
|
137 | if (this.textLayerRenderTask) {
|
138 | this.textLayerRenderTask.cancel();
|
139 | this.textLayerRenderTask = null;
|
140 | }
|
141 |
|
142 | if (this._onUpdateTextLayerMatches) {
|
143 | this.eventBus.off('updatetextlayermatches', this._onUpdateTextLayerMatches);
|
144 | this._onUpdateTextLayerMatches = null;
|
145 | }
|
146 | }
|
147 | }, {
|
148 | key: "setTextContentStream",
|
149 | value: function setTextContentStream(readableStream) {
|
150 | this.cancel();
|
151 | this.textContentStream = readableStream;
|
152 | }
|
153 | }, {
|
154 | key: "setTextContent",
|
155 | value: function setTextContent(textContent) {
|
156 | this.cancel();
|
157 | this.textContent = textContent;
|
158 | }
|
159 | }, {
|
160 | key: "_convertMatches",
|
161 | value: function _convertMatches(matches, matchesLength) {
|
162 | if (!matches) {
|
163 | return [];
|
164 | }
|
165 |
|
166 | var findController = this.findController,
|
167 | textContentItemsStr = this.textContentItemsStr;
|
168 | var i = 0,
|
169 | iIndex = 0;
|
170 | var end = textContentItemsStr.length - 1;
|
171 | var queryLen = findController.state.query.length;
|
172 | var result = [];
|
173 |
|
174 | for (var m = 0, mm = matches.length; m < mm; m++) {
|
175 | var matchIdx = matches[m];
|
176 |
|
177 | while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
|
178 | iIndex += textContentItemsStr[i].length;
|
179 | i++;
|
180 | }
|
181 |
|
182 | if (i === textContentItemsStr.length) {
|
183 | console.error('Could not find a matching mapping');
|
184 | }
|
185 |
|
186 | var match = {
|
187 | begin: {
|
188 | divIdx: i,
|
189 | offset: matchIdx - iIndex
|
190 | }
|
191 | };
|
192 |
|
193 | if (matchesLength) {
|
194 | matchIdx += matchesLength[m];
|
195 | } else {
|
196 | matchIdx += queryLen;
|
197 | }
|
198 |
|
199 | while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
|
200 | iIndex += textContentItemsStr[i].length;
|
201 | i++;
|
202 | }
|
203 |
|
204 | match.end = {
|
205 | divIdx: i,
|
206 | offset: matchIdx - iIndex
|
207 | };
|
208 | result.push(match);
|
209 | }
|
210 |
|
211 | return result;
|
212 | }
|
213 | }, {
|
214 | key: "_renderMatches",
|
215 | value: function _renderMatches(matches) {
|
216 | if (matches.length === 0) {
|
217 | return;
|
218 | }
|
219 |
|
220 | var findController = this.findController,
|
221 | pageIdx = this.pageIdx,
|
222 | textContentItemsStr = this.textContentItemsStr,
|
223 | textDivs = this.textDivs;
|
224 | var isSelectedPage = pageIdx === findController.selected.pageIdx;
|
225 | var selectedMatchIdx = findController.selected.matchIdx;
|
226 | var highlightAll = findController.state.highlightAll;
|
227 | var prevEnd = null;
|
228 | var infinity = {
|
229 | divIdx: -1,
|
230 | offset: undefined
|
231 | };
|
232 |
|
233 | function beginText(begin, className) {
|
234 | var divIdx = begin.divIdx;
|
235 | textDivs[divIdx].textContent = '';
|
236 | appendTextToDiv(divIdx, 0, begin.offset, className);
|
237 | }
|
238 |
|
239 | function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
|
240 | var div = textDivs[divIdx];
|
241 | var content = textContentItemsStr[divIdx].substring(fromOffset, toOffset);
|
242 | var node = document.createTextNode(content);
|
243 |
|
244 | if (className) {
|
245 | var span = document.createElement('span');
|
246 | span.className = className;
|
247 | span.appendChild(node);
|
248 | div.appendChild(span);
|
249 | return;
|
250 | }
|
251 |
|
252 | div.appendChild(node);
|
253 | }
|
254 |
|
255 | var i0 = selectedMatchIdx,
|
256 | i1 = i0 + 1;
|
257 |
|
258 | if (highlightAll) {
|
259 | i0 = 0;
|
260 | i1 = matches.length;
|
261 | } else if (!isSelectedPage) {
|
262 | return;
|
263 | }
|
264 |
|
265 | for (var i = i0; i < i1; i++) {
|
266 | var match = matches[i];
|
267 | var begin = match.begin;
|
268 | var end = match.end;
|
269 | var isSelected = isSelectedPage && i === selectedMatchIdx;
|
270 | var highlightSuffix = isSelected ? ' selected' : '';
|
271 |
|
272 | if (isSelected) {
|
273 | findController.scrollMatchIntoView({
|
274 | element: textDivs[begin.divIdx],
|
275 | pageIndex: pageIdx,
|
276 | matchIndex: selectedMatchIdx
|
277 | });
|
278 | }
|
279 |
|
280 | if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
|
281 | if (prevEnd !== null) {
|
282 | appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
|
283 | }
|
284 |
|
285 | beginText(begin);
|
286 | } else {
|
287 | appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
|
288 | }
|
289 |
|
290 | if (begin.divIdx === end.divIdx) {
|
291 | appendTextToDiv(begin.divIdx, begin.offset, end.offset, 'highlight' + highlightSuffix);
|
292 | } else {
|
293 | appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, 'highlight begin' + highlightSuffix);
|
294 |
|
295 | for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
|
296 | textDivs[n0].className = 'highlight middle' + highlightSuffix;
|
297 | }
|
298 |
|
299 | beginText(end, 'highlight end' + highlightSuffix);
|
300 | }
|
301 |
|
302 | prevEnd = end;
|
303 | }
|
304 |
|
305 | if (prevEnd) {
|
306 | appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
|
307 | }
|
308 | }
|
309 | }, {
|
310 | key: "_updateMatches",
|
311 | value: function _updateMatches() {
|
312 | if (!this.renderingDone) {
|
313 | return;
|
314 | }
|
315 |
|
316 | var findController = this.findController,
|
317 | matches = this.matches,
|
318 | pageIdx = this.pageIdx,
|
319 | textContentItemsStr = this.textContentItemsStr,
|
320 | textDivs = this.textDivs;
|
321 | var clearedUntilDivIdx = -1;
|
322 |
|
323 | for (var i = 0, ii = matches.length; i < ii; i++) {
|
324 | var match = matches[i];
|
325 | var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
|
326 |
|
327 | for (var n = begin, end = match.end.divIdx; n <= end; n++) {
|
328 | var div = textDivs[n];
|
329 | div.textContent = textContentItemsStr[n];
|
330 | div.className = '';
|
331 | }
|
332 |
|
333 | clearedUntilDivIdx = match.end.divIdx + 1;
|
334 | }
|
335 |
|
336 | if (!findController || !findController.highlightMatches) {
|
337 | return;
|
338 | }
|
339 |
|
340 | var pageMatches = findController.pageMatches[pageIdx] || null;
|
341 | var pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
|
342 | this.matches = this._convertMatches(pageMatches, pageMatchesLength);
|
343 |
|
344 | this._renderMatches(this.matches);
|
345 | }
|
346 | }, {
|
347 | key: "_bindMouse",
|
348 | value: function _bindMouse() {
|
349 | var _this2 = this;
|
350 |
|
351 | var div = this.textLayerDiv;
|
352 | var expandDivsTimer = null;
|
353 | div.addEventListener('mousedown', function (evt) {
|
354 | if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
|
355 | _this2.textLayerRenderTask.expandTextDivs(true);
|
356 |
|
357 | if (expandDivsTimer) {
|
358 | clearTimeout(expandDivsTimer);
|
359 | expandDivsTimer = null;
|
360 | }
|
361 |
|
362 | return;
|
363 | }
|
364 |
|
365 | var end = div.querySelector('.endOfContent');
|
366 |
|
367 | if (!end) {
|
368 | return;
|
369 | }
|
370 |
|
371 | var adjustTop = evt.target !== div;
|
372 | adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue('-moz-user-select') !== 'none';
|
373 |
|
374 | if (adjustTop) {
|
375 | var divBounds = div.getBoundingClientRect();
|
376 | var r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
|
377 | end.style.top = (r * 100).toFixed(2) + '%';
|
378 | }
|
379 |
|
380 | end.classList.add('active');
|
381 | });
|
382 | div.addEventListener('mouseup', function () {
|
383 | if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
|
384 | expandDivsTimer = setTimeout(function () {
|
385 | if (_this2.textLayerRenderTask) {
|
386 | _this2.textLayerRenderTask.expandTextDivs(false);
|
387 | }
|
388 |
|
389 | expandDivsTimer = null;
|
390 | }, EXPAND_DIVS_TIMEOUT);
|
391 | return;
|
392 | }
|
393 |
|
394 | var end = div.querySelector('.endOfContent');
|
395 |
|
396 | if (!end) {
|
397 | return;
|
398 | }
|
399 |
|
400 | end.style.top = '';
|
401 | end.classList.remove('active');
|
402 | });
|
403 | }
|
404 | }]);
|
405 |
|
406 | return TextLayerBuilder;
|
407 | }();
|
408 |
|
409 | exports.TextLayerBuilder = TextLayerBuilder;
|
410 |
|
411 | var DefaultTextLayerFactory =
|
412 |
|
413 | function () {
|
414 | function DefaultTextLayerFactory() {
|
415 | _classCallCheck(this, DefaultTextLayerFactory);
|
416 | }
|
417 |
|
418 | _createClass(DefaultTextLayerFactory, [{
|
419 | key: "createTextLayerBuilder",
|
420 | value: function createTextLayerBuilder(textLayerDiv, pageIndex, viewport) {
|
421 | var enhanceTextSelection = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
422 | return new TextLayerBuilder({
|
423 | textLayerDiv: textLayerDiv,
|
424 | pageIndex: pageIndex,
|
425 | viewport: viewport,
|
426 | enhanceTextSelection: enhanceTextSelection
|
427 | });
|
428 | }
|
429 | }]);
|
430 |
|
431 | return DefaultTextLayerFactory;
|
432 | }();
|
433 |
|
434 | exports.DefaultTextLayerFactory = DefaultTextLayerFactory; |
\ | No newline at end of file |