Skip to content

Commit

Permalink
fix: save changes to debug textDivs
Browse files Browse the repository at this point in the history
  • Loading branch information
noah-eigenfeld committed Jun 3, 2024
1 parent 29f8d23 commit 0435a15
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const PdfHighlight: FC<Props> = ({

const { textDivs } = pdfRenderedText || {};

console.log('textDivs', textDivs);

const highlightShapes = useMemo(() => {
if (boxHighlights) {
return getShapeFromBboxHighlight(boxHighlights, page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const PdfViewerTextLayer: FC<PdfViewerTextLayerProps> = ({
setRenderedText = () => {}
}) => {
const textLayerRef = useRef<HTMLDivElement>(null);
const textLayerDiv = textLayerRef.current;
const textLayerWrapper = textLayerRef.current;

// load text content from the page
const loadedText = useAsyncFunctionCall(
Expand All @@ -67,23 +67,25 @@ const PdfViewerTextLayer: FC<PdfViewerTextLayerProps> = ({
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]
)
);

Expand Down Expand Up @@ -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<void>(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<void>(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);
}

/**
Expand Down

0 comments on commit 0435a15

Please sign in to comment.