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