text_layer_builder.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.TextLayerBuilder = void 0;
  27. var _pdf = require("../pdf");
  28. const EXPAND_DIVS_TIMEOUT = 300;
  29. class TextLayerBuilder {
  30. constructor({
  31. textLayerDiv,
  32. eventBus,
  33. pageIndex,
  34. viewport,
  35. highlighter = null,
  36. enhanceTextSelection = false
  37. }) {
  38. this.textLayerDiv = textLayerDiv;
  39. this.eventBus = eventBus;
  40. this.textContent = null;
  41. this.textContentItemsStr = [];
  42. this.textContentStream = null;
  43. this.renderingDone = false;
  44. this.pageNumber = pageIndex + 1;
  45. this.viewport = viewport;
  46. this.textDivs = [];
  47. this.textLayerRenderTask = null;
  48. this.highlighter = highlighter;
  49. this.enhanceTextSelection = enhanceTextSelection;
  50. this._bindMouse();
  51. }
  52. _finishRendering() {
  53. this.renderingDone = true;
  54. if (!this.enhanceTextSelection) {
  55. const endOfContent = document.createElement("div");
  56. endOfContent.className = "endOfContent";
  57. this.textLayerDiv.appendChild(endOfContent);
  58. }
  59. this.eventBus.dispatch("textlayerrendered", {
  60. source: this,
  61. pageNumber: this.pageNumber,
  62. numTextDivs: this.textDivs.length
  63. });
  64. }
  65. render(timeout = 0) {
  66. if (!(this.textContent || this.textContentStream) || this.renderingDone) {
  67. return;
  68. }
  69. this.cancel();
  70. this.textDivs.length = 0;
  71. this.highlighter?.setTextMapping(this.textDivs, this.textContentItemsStr);
  72. const textLayerFrag = document.createDocumentFragment();
  73. this.textLayerRenderTask = (0, _pdf.renderTextLayer)({
  74. textContent: this.textContent,
  75. textContentStream: this.textContentStream,
  76. container: textLayerFrag,
  77. viewport: this.viewport,
  78. textDivs: this.textDivs,
  79. textContentItemsStr: this.textContentItemsStr,
  80. timeout,
  81. enhanceTextSelection: this.enhanceTextSelection
  82. });
  83. this.textLayerRenderTask.promise.then(() => {
  84. this.textLayerDiv.appendChild(textLayerFrag);
  85. this._finishRendering();
  86. this.highlighter?.enable();
  87. }, function (reason) {});
  88. }
  89. cancel() {
  90. if (this.textLayerRenderTask) {
  91. this.textLayerRenderTask.cancel();
  92. this.textLayerRenderTask = null;
  93. }
  94. this.highlighter?.disable();
  95. }
  96. setTextContentStream(readableStream) {
  97. this.cancel();
  98. this.textContentStream = readableStream;
  99. }
  100. setTextContent(textContent) {
  101. this.cancel();
  102. this.textContent = textContent;
  103. }
  104. _bindMouse() {
  105. const div = this.textLayerDiv;
  106. let expandDivsTimer = null;
  107. div.addEventListener("mousedown", evt => {
  108. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  109. this.textLayerRenderTask.expandTextDivs(true);
  110. if (expandDivsTimer) {
  111. clearTimeout(expandDivsTimer);
  112. expandDivsTimer = null;
  113. }
  114. return;
  115. }
  116. const end = div.querySelector(".endOfContent");
  117. if (!end) {
  118. return;
  119. }
  120. let adjustTop = evt.target !== div;
  121. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue("-moz-user-select") !== "none";
  122. if (adjustTop) {
  123. const divBounds = div.getBoundingClientRect();
  124. const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  125. end.style.top = (r * 100).toFixed(2) + "%";
  126. }
  127. end.classList.add("active");
  128. });
  129. div.addEventListener("mouseup", () => {
  130. if (this.enhanceTextSelection && this.textLayerRenderTask) {
  131. expandDivsTimer = setTimeout(() => {
  132. if (this.textLayerRenderTask) {
  133. this.textLayerRenderTask.expandTextDivs(false);
  134. }
  135. expandDivsTimer = null;
  136. }, EXPAND_DIVS_TIMEOUT);
  137. return;
  138. }
  139. const end = div.querySelector(".endOfContent");
  140. if (!end) {
  141. return;
  142. }
  143. end.style.top = "";
  144. end.classList.remove("active");
  145. });
  146. }
  147. }
  148. exports.TextLayerBuilder = TextLayerBuilder;