PDFViewerSearch.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <template>
  2. <div style="width: 100%; height: 100%; position: relative; overflow: hidden">
  3. <iframe
  4. :src="src"
  5. width="100%"
  6. scrolling="no"
  7. height="100%"
  8. style="position: relative; border: medium none;"
  9. ref="iframeRef"
  10. @load="onLoad"
  11. >
  12. </iframe>
  13. <div class="close-icon" @click="emits('close')" v-if="false">
  14. <CloseOutlined />
  15. </div>
  16. </div>
  17. </template>
  18. <script setup>
  19. import { CloseOutlined } from '@ant-design/icons-vue';
  20. import { message } from 'ant-design-vue';
  21. import { nextTick } from 'vue';
  22. const iframeRef = ref(null);
  23. const emits = defineEmits(['close', 'outline', 'search', 'load']);
  24. const searchList = ref([]);
  25. const pageContent = ref([]);
  26. const pageText = ref([]);
  27. const props = defineProps({
  28. src: String
  29. });
  30. const type = ref('pdf');
  31. watch(
  32. () => props.src,
  33. (v) => {
  34. if (v.endsWith('.pdf')) {
  35. type.value = 'pdf';
  36. } else if (v.endsWith('.txt')) {
  37. type.value = 'txt';
  38. }
  39. },
  40. { immediate: true }
  41. );
  42. const onLoad = () => {
  43. const doc = iframeRef.value.contentDocument || iframeRef.value.contentWindow.document;
  44. //待定
  45. // var toolbar = doc.getElementsByClassName('toolbar');
  46. // if (toolbar && toolbar.length > 0) {
  47. // toolbar[0].style.display = 'none';
  48. // }
  49. setTimeout(() => {
  50. const script = doc.createElement('script');
  51. script.textContent = `
  52. window.addEventListener('message', (event) => {
  53. const message = event.data;
  54. if (message.action === 'locateElement') {
  55. const element= document.getElementById('hightlight-'+message.index);
  56. debugger;
  57. if(element!=null&&element.offsetParent){
  58. setTimeout(() => {
  59. element.scrollIntoView({ behavior: 'smooth', block: 'center' })
  60. }, 10);
  61. }
  62. }
  63. if (message.action === 'mlLocateElement') {
  64. const elements= document.getElementsByClassName(message.className);
  65. if(elements!=null){
  66. setTimeout(() => {
  67. elements[message.index].scrollIntoView({ behavior: 'smooth', block: 'center' })
  68. }, 50);
  69. }
  70. }
  71. });`;
  72. doc.body.appendChild(script);
  73. const style = doc.createElement('style');
  74. style.textContent = `
  75. #outerContainer{
  76. overflow: hidden;
  77. }
  78. #page-container{
  79. overflow: hidden;
  80. }
  81. #viewerContainer{
  82. overflow: hidden;
  83. }
  84. .canvasWrapper {
  85. position: relative;
  86. /* display: flex;
  87. justify-content: center; */
  88. }
  89. .textLayer {
  90. text-align: initial;
  91. position: absolute;
  92. top: 0;
  93. left: 0;
  94. right: 0;
  95. bottom: 0;
  96. overflow: hidden;
  97. opacity: 1;
  98. line-height: 1;
  99. text-size-adjust: none;
  100. forced-color-adjust: none;
  101. }
  102. .textLayer span,
  103. .textLayer br {
  104. color: transparent;
  105. position: absolute;
  106. white-space: pre;
  107. cursor: text;
  108. transform-origin: 0% 0%;
  109. }
  110. .highlight-layer {
  111. position: absolute;
  112. top: 0;
  113. left: 0;
  114. right: 0;
  115. bottom: 0;
  116. pointer-events: none;
  117. opacity: 0.15;
  118. overflow: hidden;
  119. }
  120. .flash {
  121. animation: flashing 0.6s;
  122. animation-iteration-count: 2;
  123. }
  124. @keyframes flashing {
  125. 0% {
  126. opacity: 0;
  127. }
  128. 100% {
  129. opacity: 0.15;
  130. }
  131. }
  132. .highlight {
  133. background-color: #0076fa;
  134. position: absolute;
  135. }
  136. `;
  137. doc.head.appendChild(style);
  138. }, 400);
  139. setTimeout(() => {
  140. var el = doc.getElementsByClassName('page');
  141. // var singleHeight = 1263;
  142. var singleHeight = 1000;
  143. if (iframeRef.value.height != singleHeight * el.length) {
  144. iframeRef.value.height = singleHeight * el.length;
  145. }
  146. setTimeout(() => {
  147. pageText.value = [];
  148. pageContent.value = [];
  149. //获取每页数据
  150. var textDivs = doc.getElementsByClassName('textLayer');
  151. if (textDivs) {
  152. for (var i = 0; i < textDivs.length; i++) {
  153. var item = { page: i + 1, txt: textDivs[i].innerText };
  154. var items = [];
  155. for (var j = 0; j < textDivs[i].children.length; j++) {
  156. var citem = {
  157. str: textDivs[i].children[j].innerText,
  158. width: textDivs[i].children[j].getBoundingClientRect().width,
  159. top: textDivs[i].children[j].getBoundingClientRect().top,
  160. height: textDivs[i].children[j].getBoundingClientRect().height
  161. };
  162. items.push(citem);
  163. }
  164. pageContent.value.push({ page: i + 1, items: items });
  165. pageText.value.push(item);
  166. }
  167. }
  168. }, 1000);
  169. }, 1000);
  170. };
  171. const src = computed(() => {
  172. // return `/lib/pdfjs/web/viewer.html?file=${props.src}&t=` + new Date().getTime();
  173. return `/aisearch/lib/pdfjs/web/viewer.html?file=${props.src}`;
  174. // return `/aisearch/lib/pdfjs/web/viewer.html?file=${props.src}&t=` + new Date().getTime();
  175. // return `/lib/pdfjs/web/viewer.html?file=http://121.40.148.47:8530/doc/knowledge_base/download_doc/国土资源部 国家发展和改革委员会+财政部+住房和城乡建设部农业部+中国人民银行+国家林业局+中国银行业监督管理委员会关于扩大国有土地有偿使用范围的意见%28279-283%29.pdf`
  176. });
  177. const scrollTo = (item) => {
  178. nextTick(() => {
  179. setTimeout(() => {
  180. if (item.top) {
  181. locateElement(item.cs, item.index, 1);
  182. } else {
  183. //搜索
  184. locateElement(item.cs, item.index);
  185. //改变选中颜色 其他黄色
  186. searchList.value.forEach((citem, cindex) => {
  187. const iframeDocument =
  188. iframeRef.value.contentDocument || iframeRef.value.contentWindow.document;
  189. const highlightedSpan = iframeDocument.getElementById('hightlight-' + cindex);
  190. if (cindex == item.index) {
  191. highlightedSpan.parentNode.innerHTML = highlightedSpan.parentNode.innerHTML.replace(
  192. 'lightblue',
  193. 'yellow'
  194. );
  195. } else {
  196. highlightedSpan.parentNode.innerHTML = highlightedSpan.parentNode.innerHTML.replace(
  197. 'yellow',
  198. 'lightblue'
  199. );
  200. }
  201. });
  202. }
  203. }, 10);
  204. });
  205. };
  206. const locateElement = (className, index, type) => {
  207. const message = {
  208. action: type ? 'mlLocateElement' : 'locateElement',
  209. className: className,
  210. index: index
  211. };
  212. iframeRef.value.contentWindow.postMessage(message, '*');
  213. };
  214. const gotoElement = (element) => {
  215. nextTick(() => {
  216. element.scrollIntoView({ behavior: 'smooth', block: 'center' });
  217. });
  218. };
  219. const highlightText = (str) => {
  220. setTimeout(() => {
  221. removeHighlight();
  222. heightSingle(str);
  223. }, 100);
  224. };
  225. const goLocation = (txt, flag) => {
  226. removeHighlight();
  227. searchList.value = [];
  228. heightSingle(txt, flag);
  229. };
  230. //ai 问答
  231. const goSourceLocation = (content, type) => {
  232. //获取页面
  233. removeSourceHightLight();
  234. const doc = iframeRef.value.contentDocument || iframeRef.value.contentWindow.document;
  235. var pageNumber = 0;
  236. if (content.indexOf('-') > -1 && content.indexOf('-') < 3) {
  237. content = content.substring(content.indexOf('-') + 1);
  238. }
  239. var text = content;
  240. var ptText = text.replaceAll('\n', '');
  241. ptText = ptText.replaceAll(' ', '');
  242. console.log(ptText);
  243. pageText.value.forEach((item) => {
  244. item.txt = item.txt.replaceAll('\n', '');
  245. item.txt = item.txt.replaceAll(' ', '');
  246. console.log('==页=' + item.txt);
  247. console.log('==t=' + ptText);
  248. if (ptText.length > 30) {
  249. if (item.txt.indexOf(ptText.substring(30, 48)) > -1) {
  250. pageNumber = item.page;
  251. }
  252. } else {
  253. if (item.txt.indexOf(ptText.substring(0, 18)) > -1) {
  254. pageNumber = item.page;
  255. }
  256. }
  257. });
  258. text = text.replaceAll('\n', '');
  259. text = text.replaceAll(' ', '');
  260. if (text.endsWith('。') || text.endsWith(',') || text.endsWith('、')) {
  261. text = text.substring(0, text.length - 1);
  262. }
  263. if (pageNumber == 0) {
  264. message.info('未匹配到相关内容!');
  265. return;
  266. }
  267. //'left: 133.17px; top: 152.408px; font-size: 31.3597px; font-family: sans-serif; transform: scaleX(1.01878);'
  268. var arr = doc.getElementsByClassName('textLayer')[pageNumber - 1].getElementsByTagName('span');
  269. if (arr) {
  270. var hightList = [];
  271. var startIndex = 0;
  272. var endIndex = 0;
  273. var thirdIndex = 0;
  274. startIndex = getStartIndex(arr, text, text.length);
  275. endIndex = getEndIndex(arr, text, text.length, startIndex);
  276. if (startIndex && endIndex && endIndex < startIndex) {
  277. endIndex = startIndex + text.length;
  278. }
  279. var currentPageIndex = 0;
  280. var nextArr = [];
  281. var thirdArr = [];
  282. if (endIndex == undefined) {
  283. currentPageIndex = arr.length;
  284. if (doc.getElementsByClassName('textLayer')[pageNumber]) {
  285. nextArr = doc.getElementsByClassName('textLayer')[pageNumber].getElementsByTagName('span');
  286. endIndex = getEndIndex(nextArr, text, text.length, startIndex);
  287. if (endIndex == undefined && doc.getElementsByClassName('textLayer')[pageNumber + 1]) {
  288. thirdArr = doc
  289. .getElementsByClassName('textLayer')
  290. [pageNumber + 1].getElementsByTagName('span');
  291. thirdIndex = getEndIndex(thirdArr, text, text.length, 0);
  292. }
  293. } else {
  294. endIndex = undefined;
  295. }
  296. } else {
  297. currentPageIndex = endIndex;
  298. if (type == 1) {
  299. if (endIndex && startIndex && endIndex > startIndex + content.length) {
  300. currentPageIndex = startIndex + content.length;
  301. }
  302. //中间标点匹配问题
  303. // var len = startIndex + content.length + 5;
  304. // if (startIndex && endIndex && endIndex > (startIndex + content.length + 5)) {
  305. // currentPageIndex = len;
  306. // }
  307. }
  308. }
  309. if (!startIndex) {
  310. startIndex = 0;
  311. }
  312. for (var i = startIndex; i < currentPageIndex + 1; i++) {
  313. var element = arr[i];
  314. if (!element) {
  315. break;
  316. }
  317. var hItem = {};
  318. var cssText = element.style.cssText;
  319. var spanText = element.innerText;
  320. var s = spanText.replaceAll(' ', '');
  321. var d = spanText.endsWith('。') ? spanText.replaceAll('。', '') : spanText;
  322. console.log(spanText);
  323. if (
  324. text.includes(spanText) ||
  325. (s && text.includes(s)) ||
  326. (d && text.includes(d)) ||
  327. (spanText.substring(2) && text.includes(spanText.substring(2)))
  328. ) {
  329. var topLabel = cssText.split('top: ')[1].split(';')[0];
  330. var leftLabel = cssText.split('left: ')[1].split(';')[0];
  331. var currentItems = [];
  332. pageContent.value.forEach((item) => {
  333. if (item.page == pageNumber) {
  334. currentItems = item.items;
  335. }
  336. });
  337. for (var m = 0; m < currentItems.length; m++) {
  338. // citem.str=citem.str.replaceAll(' ','');
  339. var citem = currentItems[m];
  340. if (citem.str == spanText) {
  341. hItem.width = parseFloat(citem.width);
  342. hItem.height = parseFloat(citem.height);
  343. hItem.text = spanText;
  344. hItem.left = leftLabel;
  345. hItem.top = topLabel;
  346. hightList.push(hItem);
  347. if (hightList.length == 1) {
  348. gotoElement(element);
  349. }
  350. if (text.endsWith(citem.str)) {
  351. break;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. if (type == 1 && hightList.length == 0) {
  358. message.info('未匹配到相关内容!');
  359. }
  360. // if (type == 2) {
  361. var nextHightList = [];
  362. //跨页 目前只考虑跨1页
  363. if (endIndex != undefined) {
  364. for (var i = 0; i < nextArr.length; i++) {
  365. var element = nextArr[i];
  366. var hItem = {};
  367. var cssText = element.style.cssText;
  368. var spanText = element.innerText;
  369. console.log(spanText);
  370. if (text.includes(spanText)) {
  371. var topLabel = cssText.split('top: ')[1].split(';')[0];
  372. var leftLabel = cssText.split('left: ')[1].split(';')[0];
  373. var currentItems = [];
  374. pageContent.value.forEach((item) => {
  375. if (item.page == pageNumber + 1) {
  376. currentItems = item.items;
  377. }
  378. });
  379. for (var l = 0; l < currentItems.length; l++) {
  380. var citem = currentItems[l];
  381. if (citem.str == spanText) {
  382. hItem.width = parseFloat(citem.width);
  383. hItem.height = parseFloat(citem.height);
  384. hItem.text = spanText;
  385. hItem.left = leftLabel;
  386. hItem.top = topLabel;
  387. nextHightList.push(hItem);
  388. if (text.endsWith(citem.str)) {
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. if (text.endsWith(spanText)) {
  395. break;
  396. }
  397. }
  398. } else {
  399. //跨页 目前只考虑跨2页
  400. for (var i = 0; i < nextArr.length; i++) {
  401. var element = nextArr[i];
  402. var hItem = {};
  403. var cssText = element.style.cssText;
  404. var spanText = element.innerText;
  405. console.log(spanText);
  406. var topLabel = cssText.split('top: ')[1].split(';')[0];
  407. var leftLabel = cssText.split('left: ')[1].split(';')[0];
  408. var currentItems = [];
  409. pageContent.value.forEach((item) => {
  410. if (item.page == pageNumber + 1) {
  411. currentItems = item.items;
  412. }
  413. });
  414. for (var l = 0; l < currentItems.length; l++) {
  415. var citem = currentItems[l];
  416. if (citem.str == spanText) {
  417. hItem.width = parseFloat(citem.width);
  418. hItem.height = parseFloat(citem.height);
  419. hItem.text = spanText;
  420. hItem.left = leftLabel;
  421. hItem.top = topLabel;
  422. nextHightList.push(hItem);
  423. if (text.endsWith(citem.str)) {
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. var thirdHightList = [];
  430. if (thirdIndex != 0 && thirdIndex != undefined) {
  431. for (var z = 0; z < thirdIndex + 1; z++) {
  432. var element = thirdArr[z];
  433. var hItem = {};
  434. var cssText = element.style.cssText;
  435. var spanText = element.innerText;
  436. console.log(spanText);
  437. if (text.includes(spanText)) {
  438. var topLabel = cssText.split('top: ')[1].split(';')[0];
  439. var leftLabel = cssText.split('left: ')[1].split(';')[0];
  440. var currentItems = [];
  441. pageContent.value.forEach((item) => {
  442. if (item.page == pageNumber + 2) {
  443. currentItems = item.items;
  444. }
  445. });
  446. for (var l = 0; l < currentItems.length; l++) {
  447. var citem = currentItems[l];
  448. if (citem.str == spanText) {
  449. hItem.width = parseFloat(citem.width);
  450. hItem.height = parseFloat(citem.height);
  451. hItem.text = spanText;
  452. hItem.left = leftLabel;
  453. hItem.top = topLabel;
  454. thirdHightList.push(hItem);
  455. if (text.endsWith(citem.str)) {
  456. break;
  457. }
  458. }
  459. }
  460. }
  461. if (text.endsWith(spanText)) {
  462. break;
  463. }
  464. }
  465. }
  466. }
  467. //渲染
  468. var canvas = doc.getElementsByClassName(`page`)[pageNumber - 1];
  469. const hightDiv = document.createElement('div');
  470. hightDiv.className = 'highlight-layer flash';
  471. hightList.forEach((it) => {
  472. const hDiv = document.createElement('div');
  473. hDiv.className = 'highlight';
  474. // 设置内联样式
  475. hDiv.style.width = it.width + 'px';
  476. hDiv.style.height = it.height * 1.4 + 'px';
  477. hDiv.style.left = it.left;
  478. hDiv.style.top = parseFloat(it.top.split('%')[0]) - 0.5 + '%';
  479. hightDiv.appendChild(hDiv);
  480. });
  481. canvas.appendChild(hightDiv);
  482. if (nextHightList && nextHightList.length > 0) {
  483. //渲染
  484. var nextCanvas = doc.getElementsByClassName(`page`)[pageNumber];
  485. const hightDiv = document.createElement('div');
  486. hightDiv.className = 'highlight-layer flash';
  487. nextHightList.forEach((it) => {
  488. const hDiv = document.createElement('div');
  489. hDiv.className = 'highlight';
  490. // 设置内联样式
  491. hDiv.style.width = it.width + 'px';
  492. hDiv.style.height = it.height * 1.4 + 'px';
  493. hDiv.style.left = it.left;
  494. hDiv.style.top = parseFloat(it.top.split('%')[0]) - 0.5 + '%';
  495. hightDiv.appendChild(hDiv);
  496. });
  497. nextCanvas.appendChild(hightDiv);
  498. }
  499. if (thirdHightList && thirdHightList.length > 0) {
  500. //渲染
  501. var thirdCanvas = doc.getElementsByClassName(`page`)[pageNumber + 1];
  502. const hightDiv = document.createElement('div');
  503. hightDiv.className = 'highlight-layer flash';
  504. thirdHightList.forEach((it) => {
  505. const hDiv = document.createElement('div');
  506. hDiv.className = 'highlight';
  507. // 设置内联样式
  508. hDiv.style.width = it.width + 'px';
  509. hDiv.style.height = it.height * 1.4 + 'px';
  510. hDiv.style.left = it.left;
  511. hDiv.style.top = parseFloat(it.top.split('%')[0]) - 0.5 + '%';
  512. hightDiv.appendChild(hDiv);
  513. });
  514. thirdCanvas.appendChild(hightDiv);
  515. }
  516. // }
  517. }
  518. };
  519. //交集
  520. function commonContent(str1, str2) {
  521. let result = '';
  522. for (let i = 0; i < str1.length; i++) {
  523. if (str2.includes(str1[i])) {
  524. result += str1[i];
  525. }
  526. }
  527. return result;
  528. }
  529. const getStartIndex = (arr, text, total) => {
  530. var indexs = [];
  531. for (var i = 0; i < arr.length; i++) {
  532. var element = arr[i];
  533. var spanText = element.innerText;
  534. console.log('===' + spanText);
  535. if (
  536. (spanText && text.startsWith(spanText)) ||
  537. (spanText && spanText.substring(2) && spanText && text.startsWith(spanText.substring(2))) ||
  538. (spanText.indexOf('。') > -1 &&
  539. spanText.split('。')[1] &&
  540. text.startsWith(spanText.split('。')[1])) ||
  541. (spanText.endsWith('。') &&
  542. spanText.split('。')[0] &&
  543. text.startsWith(spanText.split('。')[0]))
  544. ) {
  545. indexs.push(i);
  546. // return i;
  547. }
  548. }
  549. if (indexs.length == 0) {
  550. return 0;
  551. } else if (indexs.length == 2) {
  552. console.log(JSON.stringify(indexs));
  553. return indexs[0] == 0 ? indexs[1] : indexs[0];
  554. } else {
  555. return indexs[0];
  556. }
  557. //todo 有问题
  558. // if (count == arr.length) {
  559. // start.value = start.value + 1;
  560. // text = text.substring(start.value, total);
  561. // getStartIndex(arr, text, total);
  562. // }
  563. };
  564. const getEndIndex = (arr, text, total, startIndex) => {
  565. for (var i = 0; i < arr.length; i++) {
  566. var element = arr[i];
  567. var spanText = element.innerText;
  568. var e = '';
  569. if (spanText.endsWith('。')) {
  570. spanText = spanText.substring(0, spanText.lastIndexOf('。'));
  571. }
  572. if (spanText.indexOf('。') > -1) {
  573. e = spanText.substring(0, spanText.lastIndexOf('。'));
  574. }
  575. console.log('===' + spanText);
  576. if (
  577. (spanText && text.endsWith(spanText) && spanText.length > 0) ||
  578. (spanText && spanText.indexOf(text) > -1) ||
  579. (e && text.endsWith(e))
  580. ) {
  581. return i;
  582. }
  583. }
  584. };
  585. const heightSingle = (str, flag) => {
  586. const iframeDocument = iframeRef.value.contentDocument || iframeRef.value.contentWindow.document;
  587. const searchText = str;
  588. var highlightColor = 'lightblue';
  589. // 使用DOM操作在iframe中搜索并高亮文本
  590. const textNodes = iframeDocument.evaluate(
  591. ".//text()[contains(., '" + searchText + "')]",
  592. iframeDocument,
  593. null,
  594. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  595. null
  596. );
  597. var firstFlag = false;
  598. for (let i = 0, length = textNodes.snapshotLength; i < length; i++) {
  599. const content = textNodes.snapshotItem(i);
  600. const textNode = content.textContent;
  601. const regex = new RegExp(searchText, 'gi');
  602. var newContent = textNode.replace(regex, (match) => {
  603. if (match == searchText) {
  604. if (!firstFlag) {
  605. firstFlag = true;
  606. highlightColor = 'yellow';
  607. } else {
  608. highlightColor = 'lightblue';
  609. }
  610. return `<span id='hightlight-${searchList.value.length}' style="background-color: ${highlightColor};color: #333;margin-top:-3px;border-radius: 3px;">${match}</span>`;
  611. }
  612. });
  613. if (content.parentNode) {
  614. var n = content.parentNode;
  615. console.log('====' + n.offsetTop);
  616. newContent = '<' + newContent.split('<')[1] + '</span>';
  617. var h = content.parentNode.innerHTML;
  618. if (!flag) {
  619. content.parentNode.innerHTML = h.replace(searchText, newContent);
  620. }
  621. if (n) {
  622. searchList.value.push({
  623. cs: n.parentNode.className,
  624. index: searchList.value.length,
  625. n: n.parentNode
  626. });
  627. if (searchList.value.length == 1) {
  628. // if (!flag) {
  629. // locateElement(searchList.value[0].cs, 0);
  630. // }
  631. gotoElement(n);
  632. }
  633. //大纲只定位一次
  634. if (flag) {
  635. break;
  636. }
  637. }
  638. }
  639. if (!flag) {
  640. if (i == textNodes.snapshotLength - 1) {
  641. //查询结束
  642. emits('search', searchList.value);
  643. }
  644. }
  645. }
  646. };
  647. const removeSourceHightLight = () => {
  648. //获取页面 ai定位移除
  649. const doc = iframeRef.value.contentDocument || iframeRef.value.contentWindow.document;
  650. const arr = doc.getElementsByClassName('highlight-layer');
  651. const l = arr.length;
  652. for (let i = l - 1; i >= 0; i--) {
  653. if (arr[i] != null) {
  654. arr[i].parentNode.removeChild(arr[i]);
  655. }
  656. }
  657. };
  658. const removeHighlight = () => {
  659. removeSourceHightLight();
  660. if (searchList.value) {
  661. searchList.value.forEach((item, index) => {
  662. const iframeDocument =
  663. iframeRef.value.contentDocument || iframeRef.value.contentWindow.document;
  664. const highlightedSpan = iframeDocument.getElementById('hightlight-' + index);
  665. if (highlightedSpan != null) {
  666. const span = highlightedSpan;
  667. console.log(span.innerHTML);
  668. setTimeout(() => {
  669. span.parentNode.replaceChild(document.createTextNode(span.textContent), span);
  670. }, 10);
  671. }
  672. });
  673. }
  674. searchList.value = [];
  675. emits('search', searchList.value);
  676. };
  677. defineExpose({ highlightText, removeHighlight, scrollTo, goLocation, goSourceLocation, location });
  678. </script>
  679. <style scoped lang="scss">
  680. .close-icon {
  681. position: absolute;
  682. top: 8px;
  683. right: 12px;
  684. cursor: pointer;
  685. }
  686. </style>
  687. <style>
  688. /* 不能用scoped,因为动态创建的元素不会被编译带上hash */
  689. /* 不加上这些css修饰,元素会错位,样式来自vue-pdf-embed这个库 */
  690. .canvasWrapper {
  691. position: relative;
  692. /* display: flex;
  693. justify-content: center; */
  694. }
  695. .page {
  696. position: relative;
  697. box-shadow: 0 4px 10px 0 rgba(0, 0, 0, 0.08);
  698. margin: 30px auto;
  699. }
  700. .textLayer {
  701. text-align: initial;
  702. position: absolute;
  703. top: 0;
  704. left: 0;
  705. right: 0;
  706. bottom: 0;
  707. overflow: hidden;
  708. opacity: 0.2;
  709. line-height: 1;
  710. text-size-adjust: none;
  711. forced-color-adjust: none;
  712. }
  713. .textLayer span,
  714. .textLayer br {
  715. color: transparent;
  716. position: absolute;
  717. white-space: pre;
  718. cursor: text;
  719. transform-origin: 0% 0%;
  720. }
  721. .highlight-layer {
  722. position: absolute;
  723. top: 0;
  724. left: 0;
  725. right: 0;
  726. bottom: 0;
  727. pointer-events: none;
  728. opacity: 0.15;
  729. overflow: hidden;
  730. }
  731. .flash {
  732. animation: flashing 0.6s;
  733. animation-iteration-count: 2;
  734. }
  735. @keyframes flashing {
  736. 0% {
  737. opacity: 0;
  738. }
  739. 100% {
  740. opacity: 0.15;
  741. }
  742. }
  743. .highlight {
  744. background-color: #0076fa;
  745. position: absolute;
  746. }
  747. </style>