2
0

text_layer_builder.js 4.9 KB

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