Skip to content

feat(OfficeView): add OfficeView component#478

Merged
ArgoZhang merged 4 commits intomasterfrom
feat-office
Jun 24, 2025
Merged

feat(OfficeView): add OfficeView component#478
ArgoZhang merged 4 commits intomasterfrom
feat-office

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Jun 24, 2025

Link issues

fixes #477

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

Add OfficeViewer component to embed and display Office documents, complete with JS interop, styling, and project integration.

New Features:

  • Introduce OfficeViewer component with Url, Height, and OnLoaded callback to display Office documents via Office Online embed.

Enhancements:

  • Implement corresponding JS interop module and CSS styling for the OfficeViewer component.

Chores:

  • Add new BootstrapBlazor.OfficeViewer project to the solution, including csproj and _Imports.razor.

@bb-auto bb-auto Bot added the enhancement New feature or request label Jun 24, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 24, 2025

Reviewer's Guide

This PR introduces a new OfficeViewer component that embeds Office documents via Office Online Viewer, combining a Blazor Razor component with C# lifecycle logic and JS interop to initialize, load, and style the viewer dynamically.

Sequence diagram for OfficeViewer initialization and document loading

sequenceDiagram
    participant User as actor User
    participant Blazor as OfficeViewer (Blazor)
    participant JS as OfficeViewer.razor.js
    participant OfficeOnline as Office Online Viewer

    User->>Blazor: Navigates to OfficeViewer component
    Blazor->>JS: Invoke init(id, invoke, options)
    JS->>JS: addLink(office-viewer.css)
    JS->>JS: Data.set(id, officeViewer)
    JS->>JS: load(id, options.url)
    JS->>OfficeOnline: iframe src=https://view.officeapps.live.com/op/embed.aspx?src=url
    OfficeOnline-->>JS: Document loaded
    JS->>Blazor: invokeMethodAsync(LoadedCallaback)
    Blazor->>User: OnLoaded callback (if set)
Loading

Class diagram for the new OfficeViewer component

classDiagram
    class OfficeViewer {
        +string? Url
        +string? Height
        +Func<Task>? OnLoaded
        -NavigationManager? NavigationManager
        -string? ClassString
        -string? StyleString
        -string? _url
        +Task OnAfterRenderAsync(bool firstRender)
        +Task InvokeInitAsync()
        -string GetAbsoluteUri(string? url)
        +Task TriggerOnLoaded()
    }
    OfficeViewer --|> BootstrapModuleComponentBase
Loading

File-Level Changes

Change Details Files
Implemented C# logic for the OfficeViewer component
  • Defined Url, Height, OnLoaded parameters
  • Injected NavigationManager for absolute URI resolution
  • Used CssBuilder for dynamic class and style strings
  • Overrode OnAfterRenderAsync to detect URL changes and invoke JS load
  • Implemented InvokeInitAsync to call JS init
  • Added GetAbsoluteUri helper and JSInvokable TriggerOnLoaded
OfficeViewer.razor.cs
Added JavaScript module for initialization and dynamic loading
  • Imported utility and data modules
  • Defined init function to load CSS and register viewer instance
  • Implemented load function to clear container and create iframe
  • Set iframe src to Office Online Viewer URL with encoding
  • Wired onload to invoke C# callback via JS interop
OfficeViewer.razor.js
Created Razor component markup with JS module autoloader
  • Inherited BootstrapModuleComponentBase
  • Applied JSModuleAutoLoader attribute for the JS module
  • Rendered a div with id, class, style, and additional attributes
OfficeViewer.razor
Added CSS for viewer container and iframe
  • Styled container to use full width and configurable height
  • Styled iframe to fill the container
office-viewer.css
Set up project files and imports
  • Updated solution to include new OfficeViewer project
  • Added project file for OfficeViewer component
  • Added _Imports.razor for namespace and web components
BootstrapBlazor.Extensions.sln
BootstrapBlazor.OfficeViewer.csproj
_Imports.razor

Assessment against linked issues

Issue Objective Addressed Explanation
#477 Create an OfficeView component for displaying Office documents in a Blazor application.

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

@bb-auto bb-auto Bot added this to the v9.2.0 milestone Jun 24, 2025
@ArgoZhang ArgoZhang merged commit 5ae8d42 into master Jun 24, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the feat-office branch June 24, 2025 03:09
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:

  • Rather than using Func for the OnLoaded callback, consider switching to an EventCallback (or EventCallback) so it follows standard Blazor event patterns.
  • You’re using CssBuilder to generate inline styles in StyleString; it might be clearer to use a dedicated style builder or just interpolate the --bb-office-viewer-height CSS variable directly into the style attribute.
  • The JS side stores viewer instances in a shared Data map but there’s no cleanup on component disposal—consider implementing DisposeAsync (or OnAfterDispose) to remove the entry and avoid potential memory leaks.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Rather than using Func<Task> for the OnLoaded callback, consider switching to an EventCallback (or EventCallback<T>) so it follows standard Blazor event patterns.
- You’re using CssBuilder to generate inline styles in StyleString; it might be clearer to use a dedicated style builder or just interpolate the `--bb-office-viewer-height` CSS variable directly into the style attribute.
- The JS side stores viewer instances in a shared Data map but there’s no cleanup on component disposal—consider implementing DisposeAsync (or OnAfterDispose) to remove the entry and avoid potential memory leaks.

## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.OfficeViewer/OfficeViewer.razor.cs:42` </location>
<code_context>
+        .AddClassFromAttributes(AdditionalAttributes)
+        .Build();
+
+    private string? StyleString => CssBuilder.Default()
+        .AddClass($"--bb-office-viewer-height: {Height};", !string.IsNullOrEmpty(Height))
+        .Build();
+
</code_context>

<issue_to_address>
Use AddStyle instead of AddClass for inline style construction.

Using AddClass for a CSS variable may prevent the height from being applied correctly. Use AddStyle to set the variable as an inline style.
</issue_to_address>

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 +42 to +43
private string? StyleString => CssBuilder.Default()
.AddClass($"--bb-office-viewer-height: {Height};", !string.IsNullOrEmpty(Height))
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 (bug_risk): Use AddStyle instead of AddClass for inline style construction.

Using AddClass for a CSS variable may prevent the height from being applied correctly. Use AddStyle to set the variable as an inline style.

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(OfficeView): add OfficeView component

1 participant