From 0435a151e070264b9a52b51f86affd2815aebcbc Mon Sep 17 00:00:00 2001 From: Noah Eigenfeld Date: Mon, 3 Jun 2024 16:00:00 -0400 Subject: [PATCH] fix: save changes to debug textDivs --- .../components/PdfHighlight/PdfHighlight.tsx | 2 + .../PdfViewer/PdfViewerTextLayer.tsx | 56 +++++++++++-------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/packages/discovery-react-components/src/components/DocumentPreview/components/PdfHighlight/PdfHighlight.tsx b/packages/discovery-react-components/src/components/DocumentPreview/components/PdfHighlight/PdfHighlight.tsx index ed29c4c88..15c858468 100644 --- a/packages/discovery-react-components/src/components/DocumentPreview/components/PdfHighlight/PdfHighlight.tsx +++ b/packages/discovery-react-components/src/components/DocumentPreview/components/PdfHighlight/PdfHighlight.tsx @@ -88,6 +88,8 @@ const PdfHighlight: FC = ({ const { textDivs } = pdfRenderedText || {}; + console.log('textDivs', textDivs); + const highlightShapes = useMemo(() => { if (boxHighlights) { return getShapeFromBboxHighlight(boxHighlights, page); 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 ea6eb9605..4f71e3577 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 @@ -49,7 +49,7 @@ const PdfViewerTextLayer: FC = ({ setRenderedText = () => {} }) => { const textLayerRef = useRef(null); - const textLayerDiv = textLayerRef.current; + const textLayerWrapper = textLayerRef.current; // load text content from the page const loadedText = useAsyncFunctionCall( @@ -67,23 +67,25 @@ const PdfViewerTextLayer: FC = ({ const renderedText = useAsyncFunctionCall( useCallback( async (signal: AbortSignal) => { - if (textLayerDiv && loadedText) { + if (textLayerWrapper && loadedText) { const { textContent, viewport, scale, page } = loadedText; const builder = new TextLayerBuilder({ - textLayerDiv, - viewport, - eventBus: new EventBus(), - pageIndex: page - 1 + pdfPage: loadedPage }); + // trying to find a way to return textDivs + const textItems = textContent.items; + console.log('textItems', textItems); + + textLayerWrapper.append(builder.div); signal.addEventListener('abort', () => builder.cancel()); - await _renderTextLayer(builder, textContent, textLayerDiv, scale); - return { textContent, viewport, page, textDivs: builder.textDivs }; + await _renderTextLayer(builder, textContent, textLayerWrapper, scale, viewport); + return { textContent, viewport, page, textDivs: [] }; } return undefined; }, - [loadedText, textLayerDiv] + [loadedPage, loadedText, textLayerWrapper] ) ); @@ -112,24 +114,32 @@ async function _renderTextLayer( builder: TextLayerBuilder, textContent: TextContent, textLayerDiv: HTMLDivElement, - scale: number + scale: number, + viewport: PageViewport + // loadedPage: PDFPageProxy ) { - builder.setTextContent(textContent); + // TODO: delete if this works without it + // if (!builder.renderingDone) { + // const readableStream = loadedPage.streamTextContent({ + // includeMarkedContent: true + // }) + // builder.(readableStream); + // } // render textLayerDiv.innerHTML = ''; - const deferredRenderEndPromise = new Promise(resolve => { - const listener = () => { - resolve(); - builder?.eventBus.off('textlayerrendered', listener); - }; - builder?.eventBus.on('textlayerrendered', listener); - }); - - builder.render(); - await deferredRenderEndPromise; - - _adjustTextDivs(builder.textDivs, textContent.items as TextItem[], scale); + // const deferredRenderEndPromise = new Promise(resolve => { + // const listener = () => { + // resolve(); + // builder?.eventBus.off('textlayerrendered', listener); + // }; + // builder?.eventBus.on('textlayerrendered', listener); + // }); + + await builder.render(viewport); + // await deferredRenderEndPromise; + + // _adjustTextDivs(builder.textDivs, textContent.items as TextItem[], scale); } /**