2
0

text_highlighter.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.TextHighlighter = void 0;
  27. class TextHighlighter {
  28. constructor({
  29. findController,
  30. eventBus,
  31. pageIndex
  32. }) {
  33. this.findController = findController;
  34. this.matches = [];
  35. this.eventBus = eventBus;
  36. this.pageIdx = pageIndex;
  37. this._onUpdateTextLayerMatches = null;
  38. this.textDivs = null;
  39. this.textContentItemsStr = null;
  40. this.enabled = false;
  41. }
  42. setTextMapping(divs, texts) {
  43. this.textDivs = divs;
  44. this.textContentItemsStr = texts;
  45. }
  46. enable() {
  47. if (!this.textDivs || !this.textContentItemsStr) {
  48. throw new Error("Text divs and strings have not been set.");
  49. }
  50. if (this.enabled) {
  51. throw new Error("TextHighlighter is already enabled.");
  52. }
  53. this.enabled = true;
  54. if (!this._onUpdateTextLayerMatches) {
  55. this._onUpdateTextLayerMatches = evt => {
  56. if (evt.pageIndex === this.pageIdx || evt.pageIndex === -1) {
  57. this._updateMatches();
  58. }
  59. };
  60. this.eventBus._on("updatetextlayermatches", this._onUpdateTextLayerMatches);
  61. }
  62. this._updateMatches();
  63. }
  64. disable() {
  65. if (!this.enabled) {
  66. return;
  67. }
  68. this.enabled = false;
  69. if (this._onUpdateTextLayerMatches) {
  70. this.eventBus._off("updatetextlayermatches", this._onUpdateTextLayerMatches);
  71. this._onUpdateTextLayerMatches = null;
  72. }
  73. }
  74. _convertMatches(matches, matchesLength) {
  75. if (!matches) {
  76. return [];
  77. }
  78. const {
  79. textContentItemsStr
  80. } = this;
  81. let i = 0,
  82. iIndex = 0;
  83. const end = textContentItemsStr.length - 1;
  84. const result = [];
  85. for (let m = 0, mm = matches.length; m < mm; m++) {
  86. let matchIdx = matches[m];
  87. while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
  88. iIndex += textContentItemsStr[i].length;
  89. i++;
  90. }
  91. if (i === textContentItemsStr.length) {
  92. console.error("Could not find a matching mapping");
  93. }
  94. const match = {
  95. begin: {
  96. divIdx: i,
  97. offset: matchIdx - iIndex
  98. }
  99. };
  100. matchIdx += matchesLength[m];
  101. while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
  102. iIndex += textContentItemsStr[i].length;
  103. i++;
  104. }
  105. match.end = {
  106. divIdx: i,
  107. offset: matchIdx - iIndex
  108. };
  109. result.push(match);
  110. }
  111. return result;
  112. }
  113. _renderMatches(matches) {
  114. if (matches.length === 0) {
  115. return;
  116. }
  117. const {
  118. findController,
  119. pageIdx
  120. } = this;
  121. const {
  122. textContentItemsStr,
  123. textDivs
  124. } = this;
  125. const isSelectedPage = pageIdx === findController.selected.pageIdx;
  126. const selectedMatchIdx = findController.selected.matchIdx;
  127. const highlightAll = findController.state.highlightAll;
  128. let prevEnd = null;
  129. const infinity = {
  130. divIdx: -1,
  131. offset: undefined
  132. };
  133. function beginText(begin, className) {
  134. const divIdx = begin.divIdx;
  135. textDivs[divIdx].textContent = "";
  136. return appendTextToDiv(divIdx, 0, begin.offset, className);
  137. }
  138. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  139. let div = textDivs[divIdx];
  140. if (div.nodeType === Node.TEXT_NODE) {
  141. const span = document.createElement("span");
  142. div.before(span);
  143. span.append(div);
  144. textDivs[divIdx] = span;
  145. div = span;
  146. }
  147. const content = textContentItemsStr[divIdx].substring(fromOffset, toOffset);
  148. const node = document.createTextNode(content);
  149. if (className) {
  150. const span = document.createElement("span");
  151. span.className = `${className} appended`;
  152. span.append(node);
  153. div.append(span);
  154. return className.includes("selected") ? span.offsetLeft : 0;
  155. }
  156. div.append(node);
  157. return 0;
  158. }
  159. let i0 = selectedMatchIdx,
  160. i1 = i0 + 1;
  161. if (highlightAll) {
  162. i0 = 0;
  163. i1 = matches.length;
  164. } else if (!isSelectedPage) {
  165. return;
  166. }
  167. for (let i = i0; i < i1; i++) {
  168. const match = matches[i];
  169. const begin = match.begin;
  170. const end = match.end;
  171. const isSelected = isSelectedPage && i === selectedMatchIdx;
  172. const highlightSuffix = isSelected ? " selected" : "";
  173. let selectedLeft = 0;
  174. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  175. if (prevEnd !== null) {
  176. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  177. }
  178. beginText(begin);
  179. } else {
  180. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  181. }
  182. if (begin.divIdx === end.divIdx) {
  183. selectedLeft = appendTextToDiv(begin.divIdx, begin.offset, end.offset, "highlight" + highlightSuffix);
  184. } else {
  185. selectedLeft = appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, "highlight begin" + highlightSuffix);
  186. for (let n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  187. textDivs[n0].className = "highlight middle" + highlightSuffix;
  188. }
  189. beginText(end, "highlight end" + highlightSuffix);
  190. }
  191. prevEnd = end;
  192. if (isSelected) {
  193. findController.scrollMatchIntoView({
  194. element: textDivs[begin.divIdx],
  195. selectedLeft,
  196. pageIndex: pageIdx,
  197. matchIndex: selectedMatchIdx
  198. });
  199. }
  200. }
  201. if (prevEnd) {
  202. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  203. }
  204. }
  205. _updateMatches() {
  206. if (!this.enabled) {
  207. return;
  208. }
  209. const {
  210. findController,
  211. matches,
  212. pageIdx
  213. } = this;
  214. const {
  215. textContentItemsStr,
  216. textDivs
  217. } = this;
  218. let clearedUntilDivIdx = -1;
  219. for (const match of matches) {
  220. const begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  221. for (let n = begin, end = match.end.divIdx; n <= end; n++) {
  222. const div = textDivs[n];
  223. div.textContent = textContentItemsStr[n];
  224. div.className = "";
  225. }
  226. clearedUntilDivIdx = match.end.divIdx + 1;
  227. }
  228. if (!findController?.highlightMatches) {
  229. return;
  230. }
  231. const pageMatches = findController.pageMatches[pageIdx] || null;
  232. const pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
  233. this.matches = this._convertMatches(pageMatches, pageMatchesLength);
  234. this._renderMatches(this.matches);
  235. }
  236. }
  237. exports.TextHighlighter = TextHighlighter;