Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[pdfHighlight]: scale pdf down and using scaling to highlight text… #24

Merged
merged 4 commits into from
Oct 15, 2024

Conversation

ArslanSaleem
Copy link
Collaborator

@ArslanSaleem ArslanSaleem commented Oct 15, 2024

… in pdf

Summary by CodeRabbit

  • New Features

    • Enhanced PDF highlighting functionality with improved scaling based on viewport dimensions.
    • Adjusted rendering of PDF pages for better responsiveness and layout.
  • Bug Fixes

    • Improved stability by adding checks to prevent division by zero during highlight calculations.
  • Style

    • Removed minimum height restriction on the page container for a more flexible layout.

@ArslanSaleem ArslanSaleem requested a review from gventuri October 15, 2024 12:01
Copy link
Contributor

coderabbitai bot commented Oct 15, 2024

Walkthrough

The changes in this pull request involve modifications to the CSS and TypeScript files related to the HighlightPdfViewer component. In the CSS file, the min-height: 100vh; property was removed from the .pageContainer class, affecting the layout behavior. In the TypeScript file, a new state variable for viewer width was introduced, and the constructCoordinates function was updated to include new parameters for scaling. The logic for highlight rendering was refined, including adjustments to prevent division by zero and enhance overlap detection.

Changes

File Change Summary
frontend/src/ee/components/HighlightPdfViewer.module.css Removed min-height: 100vh; from .pageContainer class.
frontend/src/ee/components/HighlightPdfViewer.tsx Introduced viewerWidth state variable. Updated constructCoordinates with new scaling parameters. Enhanced highlightTextInPdf for scaling calculations and refined highlight rendering logic.

Possibly related PRs

  • Release v0.2.0 #21: The changes in the HighlightPdfViewer.tsx component regarding the highlighting logic and rendering adjustments are directly related to the modifications made in the same file in the main PR, specifically concerning how highlights are calculated and rendered.

Poem

🐰 In the viewer where highlights gleam,
A change was made, a coder's dream.
No more the height of the sky,
Just perfect scales that catch the eye.
With every text, a brighter hue,
Oh, what a joy, this change is true! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fbda153 and dd647df.

📒 Files selected for processing (1)
  • frontend/src/ee/components/HighlightPdfViewer.tsx (12 hunks)
🧰 Additional context used
🔇 Additional comments (8)
frontend/src/ee/components/HighlightPdfViewer.tsx (8)

Line range hint 31-55: Improved responsiveness with dynamic width handling

The addition of the viewerWidth state and the useEffect hook for updating it on mount and resize is a great improvement. This change addresses the previous concern about ensuring consistent width between the container and the Page component, and enhances the overall responsiveness of the PDF viewer.


158-174: Enhanced coordinate calculation with proper scaling

The updates to the constructCoordinates function, including the addition of scaleFactorHeight and scaleFactorWidth parameters, significantly improve the accuracy of highlight positioning and sizing. This change correctly addresses the previous issue of using an incorrect scaling factor for width calculation.


178-202: Improved text matching with new helper functions

The addition of isStringApproximatelyAtStart and replaceStringAtStart functions enhances the text highlighting logic. These helper functions provide more robust handling of edge cases in text matching and replacement, which should lead to more accurate highlighting results.


Line range hint 212-232: Improved scaling factor calculation with error prevention

The updates to the highlightTextInPdf function, particularly the addition of checks to prevent division by zero when calculating scale factors, address the previous concern about potential errors. The new viewport and scaling factor calculations enhance the accuracy of highlight rendering.


Line range hint 239-331: Refined text processing and overlap detection

The updates to the text processing and overlap detection logic significantly improve the accuracy and efficiency of the highlighting process. The new conditions for handling different overlap scenarios and adding highlight coordinates provide a more robust solution for text highlighting.


356-360: Improved handling of multi-line text highlights

The update to the highlightAllSources function to split and process each line of multi-line sources separately is a valuable improvement. This change ensures that each line of a multi-line text source is properly highlighted, enhancing the overall accuracy of the highlighting feature.


410-417: Enhanced PDF page rendering and layout

The updates to the Page component rendering, including the use of viewerWidth for the width prop and the addition of centering margin, significantly improve the responsiveness and layout of the PDF viewer. These changes effectively address the previous concerns about hardcoded width and centering of PDF pages.


Line range hint 1-427: Excellent improvements to the HighlightPdfViewer component

The changes made to the HighlightPdfViewer component significantly enhance its functionality, responsiveness, and accuracy. Key improvements include:

  1. Dynamic width handling for better responsiveness
  2. Enhanced coordinate calculation with proper scaling
  3. Improved text matching and overlap detection
  4. Refined highlight rendering process
  5. Better handling of multi-line text highlights
  6. Enhanced PDF page rendering and layout

These updates address all major concerns from previous reviews and collectively provide a more robust and user-friendly PDF viewing experience with accurate text highlighting.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov-commenter
Copy link

codecov-commenter commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (release/v0.2.1@e61e1b9). Learn more about missing BASE report.

Additional details and impacted files
@@                Coverage Diff                @@
##             release/v0.2.1      #24   +/-   ##
=================================================
  Coverage                  ?   53.11%           
=================================================
  Files                     ?       36           
  Lines                     ?     1751           
  Branches                  ?        0           
=================================================
  Hits                      ?      930           
  Misses                    ?      821           
  Partials                  ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e61e1b9 and 52a6e95.

📒 Files selected for processing (2)
  • frontend/src/ee/components/HighlightPdfViewer.module.css (0 hunks)
  • frontend/src/ee/components/HighlightPdfViewer.tsx (9 hunks)
💤 Files with no reviewable changes (1)
  • frontend/src/ee/components/HighlightPdfViewer.module.css
🧰 Additional context used
🔇 Additional comments (1)
frontend/src/ee/components/HighlightPdfViewer.tsx (1)

145-147: Addition of scaling factors to constructCoordinates

The inclusion of scaleFactorHeight and scaleFactorWidth parameters in the constructCoordinates function enhances the accuracy of coordinate scaling based on viewport dimensions.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
frontend/src/ee/components/HighlightPdfViewer.tsx (3)

151-155: Correct implementation of scaled coordinate calculations

The updated calculations for x and y coordinates using the scaling factors are correct and align with the function's new parameters. The adjustment for viewHeight in the y-coordinate calculation ensures proper vertical positioning on the scaled PDF.

For improved readability, consider extracting the y-coordinate calculation into a separate variable:

const scaledHeight = height / scaleFactorHeight;
const y = viewHeight - (transform[5] / scaleFactorHeight) - scaledHeight;

This change would make the calculation easier to understand at a glance.


185-194: Improved error handling and correct scaling factor calculation

The addition of a check for zero values in viewWidth and viewHeight effectively prevents division by zero errors, addressing a previous concern. The calculation of scaleFactorWidth and scaleFactorHeight is correct and essential for accurate highlight positioning.

To further improve the code, consider using a guard clause:

if (viewWidth === 0 || viewHeight === 0) {
  console.error("viewWidth or viewHeight is zero, cannot calculate scaling factors.");
  return;
}

const scaleFactorWidth = viewPort.width / viewWidth;
const scaleFactorHeight = viewPort.height / viewHeight;

This change reduces nesting and makes the code more readable.


359-359: Improve responsiveness and maintain consistent sizing

The use of a fixed width of "600px" for the page container may limit responsiveness on different screen sizes. Consider using a more flexible approach:

- style={{ width: "600px", margin: "0 auto" }}
+ style={{ maxWidth: "600px", width: "100%", margin: "0 auto" }}

This change allows the container to adjust to smaller screen sizes while maintaining a maximum width of 600px on larger screens.

The update to use viewerRef.current?.offsetWidth for the Page component's width is a good approach to maintain consistency between the container and the PDF page. However, ensure that this value is reliably set and corresponds to the container's width to avoid potential sizing discrepancies.

Also applies to: 366-366

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 52a6e95 and 08a74ef.

📒 Files selected for processing (1)
  • frontend/src/ee/components/HighlightPdfViewer.tsx (9 hunks)
🧰 Additional context used
🔇 Additional comments (4)
frontend/src/ee/components/HighlightPdfViewer.tsx (4)

145-147: Improved coordinate calculation with scaling factors

The addition of scaleFactorHeight and scaleFactorWidth parameters to the constructCoordinates function is a positive change. This modification allows for more accurate coordinate calculations, which is crucial for correctly positioning highlights on scaled PDFs.


173-174: Correct viewport initialization and improved readability

The initialization of the viewport with a scale of 1 is correct when implementing custom scaling. This ensures that the original dimensions are used as a base for further calculations. The added blank line also improves code readability.


231-233: Consistent use of scaling factors in coordinate calculations

The constructCoordinates function is now correctly called with the new scaling factor parameters throughout the highlightTextInPdf function. This consistent use of scaling factors ensures that highlight coordinates are accurately calculated and positioned on the scaled PDF.

Also applies to: 245-247, 260-262, 275-277


160-161: ⚠️ Potential issue

Incorrect scaling factor used for width calculation

The width is currently being divided by scaleFactorHeight, which is inconsistent with the x-coordinate calculation and may lead to distorted highlights. To maintain correct aspect ratios, the width should be divided by scaleFactorWidth.

Please update the width calculation as follows:

return {
  x: x,
  y: y,
- width: width / scaleFactorHeight,
+ width: width / scaleFactorWidth,
  height: height / scaleFactorHeight,
};

This change ensures that both horizontal dimensions (x and width) use the same scaling factor.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 08a74ef and fbda153.

📒 Files selected for processing (1)
  • frontend/src/ee/components/HighlightPdfViewer.tsx (9 hunks)
🧰 Additional context used
🔇 Additional comments (4)
frontend/src/ee/components/HighlightPdfViewer.tsx (4)

145-161: LGTM: Improved highlight positioning with scaling factors

The changes to the constructCoordinates function look good. The addition of scaleFactorHeight and scaleFactorWidth parameters and their usage in the calculations improve the accuracy of highlight positioning. This change also addresses the previous issue where an incorrect scaling factor was used for the width calculation.


Line range hint 173-194: LGTM: Improved robustness with scaling factor calculations

The changes to the highlightTextInPdf function are well-implemented. The addition of scaling factor calculations and the check for zero values in viewWidth and viewHeight address the potential division by zero issue raised in a previous review. This improves the overall robustness of the function.


Line range hint 213-233: LGTM: Enhanced overlap detection and consistent use of scaling factors

The changes to the overlap detection logic are well-implemented. The additional overlap check improves the accuracy of text matching. The constructCoordinates function call has been correctly updated to include the new scaling factor parameters, maintaining consistency with the earlier changes.


Line range hint 245-277: LGTM: Consistent application of scaling factors in highlight calculations

The changes to the highlight coordinate calculations are well-implemented. All instances of the constructCoordinates function calls have been correctly updated to include the new scaling factor parameters. This ensures consistent and accurate highlight positioning across different scenarios.

@gventuri gventuri merged commit e507506 into release/v0.2.1 Oct 15, 2024
5 checks passed
@gventuri gventuri deleted the fix/pdf_scaling_issues branch October 15, 2024 16:20
@gventuri gventuri restored the fix/pdf_scaling_issues branch October 15, 2024 16:20
@gventuri gventuri deleted the fix/pdf_scaling_issues branch October 15, 2024 16:20
gventuri pushed a commit that referenced this pull request Oct 15, 2024
#24)

* fix[pdfHighlight]: scale pdf down and using scaling to highlight text in pdf

* fix[pdfViewer]: requested code refactory

* fix[pdfHighlight]: extras leftovers

* fix[pdfHighlight]: refactor and add highlights in pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants