Skip to content

feat(PdfViewer): add UseGoogleDocs parameter#454

Merged
ArgoZhang merged 2 commits intomasterfrom
refactor-pdf
Jun 6, 2025
Merged

feat(PdfViewer): add UseGoogleDocs parameter#454
ArgoZhang merged 2 commits intomasterfrom
refactor-pdf

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Jun 6, 2025

Link issues

fixes #453

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Introduce a new UseGoogleDocs parameter and update the interop logic to load PDFs via Google Docs when enabled, while ensuring the viewer is properly reinitialized on configuration changes.

New Features:

  • Add UseGoogleDocs parameter to PdfViewer to enable rendering PDFs through the Google Docs viewer.

Enhancements:

  • Reload the PDF viewer when the UseGoogleDocs flag or URL changes to reflect updated settings.

@bb-auto bb-auto Bot added the enhancement New feature or request label Jun 6, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Jun 6, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 6, 2025

Reviewer's Guide

Introduces a new UseGoogleDocs toggle to the PdfViewer component, with component-level state tracking and updates to both Razor and JS interop to optionally render PDFs via the Google Docs viewer and to reinitialize the viewer when this flag or the URL changes.

File-Level Changes

Change Details Files
Add UseGoogleDocs parameter and integrate into component rendering logic
  • Define UseGoogleDocs parameter with default value
  • Expose UseGoogleDocsString for attribute binding
  • Track previous UseGoogleDocs in private field and compare in OnAfterRenderAsync
  • Extend OnAfterRenderAsync to trigger rerender when URL or UseGoogleDocs changes
  • Include Url in InvokeInitAsync payload alongside callbacks
PdfViewer.razor.cs
Update JS interop to support Google Docs viewer
  • Switch init to use options.url instead of attribute lookup
  • Clear existing frame and container content before each load
  • Check data-bb-google-docs attribute and prepend Google Docs viewer URL
PdfViewer.razor.js
Expose Google Docs flag in Razor markup
  • Add data-bb-google-docs attribute bound to UseGoogleDocsString on the container div
PdfViewer.razor

Assessment against linked issues

Issue Objective Addressed Explanation

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes - here's some feedback:

  • Use HTTPS and encode the PDF URL when constructing the Google Docs viewer link to avoid mixed-content issues and ensure valid URLs.
  • The manual state-tracking in OnAfterRenderAsync for _url and _useGoogleDocs could be simplified by overriding ShouldRender or leveraging Blazor’s built-in change detection.
  • Re-add clearing logic for when Url is null or empty, as removing the else branch in loadPdf may leave old frames in place when no URL is provided.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +36 to +38
if (useGoogleDocs) {
url = `http://docs.google.com/viewer?url=${url}`
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (bug_risk): Use HTTPS and encode URL when loading Google Docs viewer

Update the protocol to https:// and use encodeURIComponent(url) to prevent mixed-content errors and handle special characters in the URL.

Suggested change
if (useGoogleDocs) {
url = `http://docs.google.com/viewer?url=${url}`
}
if (useGoogleDocs) {
url = `https://docs.google.com/viewer?url=${encodeURIComponent(url)}`
}

private string? _url;
private bool _useGoogleDocs;

private string? UseGoogleDocsString => UseGoogleDocs ? "true" : null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (complexity): Consider removing the unused property and simplifying the rerender logic in OnAfterRenderAsync with a single if/else block.

// 1) Remove the unused property
//    private string? UseGoogleDocsString => UseGoogleDocs ? "true" : null;

// 2) Collapse OnAfterRenderAsync into a single if/else
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    await base.OnAfterRenderAsync(firstRender);

    if (firstRender)
    {
        _url = Url;
        _useGoogleDocs = UseGoogleDocs;
    }
    else if (_url != Url || _useGoogleDocs != UseGoogleDocs)
    {
        _url = Url;
        _useGoogleDocs = UseGoogleDocs;
        await InvokeVoidAsync("loadPdf", Id, _url);
    }
}

These two changes preserve all behavior while removing dead code and flattening the rerender logic.


const useGoogleDocs = el.getAttribute('data-bb-google-docs') === 'true';
if (useGoogleDocs) {
url = `http://docs.google.com/viewer?url=${url}`
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (code-quality): Don't reassign parameter - url (dont-reassign-parameters)

ExplanationReassigning parameters can lead to unexpected behavior, especially when accessing the arguments object. It can also cause optimization issues, especially in V8.

From the Airbnb JavaScript Style Guide

@ArgoZhang ArgoZhang merged commit e9b1fd2 into master Jun 6, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the refactor-pdf branch June 6, 2025 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(PdfViewer): add UseGoogleDocs parameter

1 participant