Skip to content

Commit

Permalink
refactor: use a for of instead of a foreach in the highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
gventuri committed Oct 14, 2024
1 parent f03f6c2 commit b149916
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ repos:
entry: bash -c 'cd backend && poetry run pytest'
language: system
pass_filenames: false
always_run: true
always_run: false
files: ^(backend/|tests/)
types: [python]

- id: coverage-check
name: coverage-check
entry: bash -c 'cd backend && poetry run coverage erase && poetry run coverage run -m pytest && poetry run coverage report -m && poetry run coverage xml'
language: system
pass_filenames: false
always_run: true
always_run: false
files: ^(backend/|tests/)
types: [python]
34 changes: 18 additions & 16 deletions frontend/src/ee/components/HighlightPdfViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
// Highlighting logic for both custom coordinates and text
const highlightText = (
pageNumber: number,
sources: HighlightCoordinate[],
sources: HighlightCoordinate[]
) => {
const pageContainer = document.querySelector<HTMLDivElement>(
`#page_${pageNumber}`,
`#page_${pageNumber}`
);

if (!pageContainer) {
Expand Down Expand Up @@ -143,7 +143,7 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
const constructCoordinates = (
item: any,
viewHeight: number,
viewWidth: number,
viewWidth: number
) => {
const { transform, width, height } = item;
const x = 2 * transform[4];
Expand All @@ -166,7 +166,7 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
const page = await pdfDocument.getPage(pageNumber);
const textContent = await page.getTextContent();
const pageCanvas = document.querySelector<HTMLCanvasElement>(
`#page_${pageNumber} canvas`,
`#page_${pageNumber} canvas`
);

if (!pageCanvas) return;
Expand Down Expand Up @@ -210,7 +210,7 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
const highlightCoord = constructCoordinates(
item,
viewHeight,
viewWidth,
viewWidth
);
highlightCoordinates.push(highlightCoord);
copyText = copyText.replace(overlap.overlap, "").trim();
Expand All @@ -222,7 +222,7 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
const highlightCoord = constructCoordinates(
item,
viewHeight,
viewWidth,
viewWidth
);
highlightCoordinates.push(highlightCoord);
copyText = copyText.replace(overlap.overlap, "").trim();
Expand All @@ -235,7 +235,7 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
const highlightCoord = constructCoordinates(
item,
viewHeight,
viewWidth,
viewWidth
);
highlightCoordinates.push(highlightCoord);
copyText = copyText.replace(overlap.overlap, "").trim();
Expand All @@ -248,7 +248,7 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
const highlightCoord = constructCoordinates(
item,
viewHeight,
viewWidth,
viewWidth
);
highlightCoordinates.push(highlightCoord);
copyText = copyText.replace(overlap.overlap, "").trim();
Expand All @@ -275,21 +275,23 @@ const HighlightPdfViewer: React.FC<PdfViewerProps> = ({
}, [numPages]);

useEffect(() => {
console.log("Starting to highlight sources:", highlightSources); // Add this line
highlightSources.forEach(async (highlightSource) => {
await highlightTextInPdf(
highlightSource.page_number,
highlightSource.source,
);
});
const highlightAllSources = async () => {
for (const highlightSource of highlightSources) {
await highlightTextInPdf(
highlightSource.page_number,
highlightSource.source
);
}
};
highlightAllSources();
}, [onScrolled]);

const handleIntersection = (entries: IntersectionObserverEntry[]) => {
entries.forEach((entry) => {
const pageNumber = parseInt(entry.target.id.split("_")[1]);
if (entry.isIntersecting) {
setVisiblePages((prev) =>
prev.includes(pageNumber) ? prev : [...prev, pageNumber],
prev.includes(pageNumber) ? prev : [...prev, pageNumber]
);
}
});
Expand Down

0 comments on commit b149916

Please sign in to comment.