From adf7a016b90291104d317f2eea3801b1ed5b7004 Mon Sep 17 00:00:00 2001 From: Noah Eigenfeld Date: Tue, 4 Jun 2024 10:14:52 -0400 Subject: [PATCH] fix: import TextLayer to get textDivs --- .../PdfViewer/PdfViewerTextLayer.tsx | 22 +++++++++++++++---- .../components/PdfViewer/typings.d.ts | 8 +++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewerTextLayer.tsx b/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewerTextLayer.tsx index 4f71e3577..1154e4557 100644 --- a/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewerTextLayer.tsx +++ b/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/PdfViewerTextLayer.tsx @@ -2,7 +2,8 @@ import { FC, useEffect, useRef, useCallback } from 'react'; import cx from 'classnames'; import { TextContent, TextItem, PDFPageProxy } from 'pdfjs-dist/types/src/display/api'; import { PageViewport } from 'pdfjs-dist/types/src/display/display_utils'; -import { EventBus, TextLayerBuilder } from 'pdfjs-dist/web/pdf_viewer.mjs'; +import { TextLayerBuilder } from 'pdfjs-dist/web/pdf_viewer.mjs'; +import { TextLayer } from 'pdfjs-dist/build/pdf.mjs'; import useAsyncFunctionCall from 'utils/useAsyncFunctionCall'; import { PdfDisplayProps } from './types'; @@ -74,14 +75,20 @@ const PdfViewerTextLayer: FC = ({ pdfPage: loadedPage }); // trying to find a way to return textDivs - const textItems = textContent.items; - console.log('textItems', textItems); + // const textItems = textContent.items; + // const textDivs = processItemsToDivs(textItems); textLayerWrapper.append(builder.div); signal.addEventListener('abort', () => builder.cancel()); await _renderTextLayer(builder, textContent, textLayerWrapper, scale, viewport); - return { textContent, viewport, page, textDivs: [] }; + const textLayer = new TextLayer({ + textContentSource: textContent, + container: textLayerWrapper, + viewport + }); + await textLayer.render(); + return { textContent, viewport, page, textDivs: textLayer.textDivs }; } return undefined; }, @@ -142,6 +149,13 @@ async function _renderTextLayer( // _adjustTextDivs(builder.textDivs, textContent.items as TextItem[], scale); } +function processItemsToDivs(textItems: TextItem[] | null): HTMLElement[] | null { + console.log('items to process', textItems); + let textDivs = []; + + return textDivs; +} + /** * Adjust text span width based on scale * @param textDivs diff --git a/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/typings.d.ts b/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/typings.d.ts index 7ca8b7f2e..706f44ebe 100644 --- a/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/typings.d.ts +++ b/packages/discovery-react-components/src/components/DocumentPreview/components/PdfViewer/typings.d.ts @@ -1,5 +1,13 @@ // declare module 'pdfjs-dist/build/pdf.worker.min.mjs'; +declare module 'pdfjs-dist/build/pdf.mjs' { + export class TextLayer { + constructor({ textContentSource: TextContent, container: any, viewport: PageViewport }); + render(): Promise; + textDivs: HTMLElement[]; + } +} + // // // // Declare modules and their types that is referred from PDF text layer rendering. // // Unused properties are commented out