-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(OfficeView): add OfficeView component #478
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/components/BootstrapBlazor.OfficeViewer/BootstrapBlazor.OfficeViewer.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
|
||
| <PropertyGroup> | ||
| <Version>9.0.0</Version> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Office Viewer</PackageTags> | ||
| <Description>Bootstrap UI components extensions of Microsoft Office Documentation Viewer</Description> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="BootstrapBlazor.Components" /> | ||
| <Using Include="Microsoft.JSInterop" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
5 changes: 5 additions & 0 deletions
5
src/components/BootstrapBlazor.OfficeViewer/OfficeViewer.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| @namespace BootstrapBlazor.Components | ||
| @inherits BootstrapModuleComponentBase | ||
| @attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.OfficeViewer/OfficeViewer.razor.js", JSObjectReference = true, AutoInvokeDispose = false)] | ||
|
|
||
| <div @attributes="AdditionalAttributes" id="@Id" class="@ClassString" style="@StyleString"></div> |
109 changes: 109 additions & 0 deletions
109
src/components/BootstrapBlazor.OfficeViewer/OfficeViewer.razor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright (c) Argo Zhang (argo@163.com). All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
| // Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
|
||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// Represents a viewer component for displaying Office documents in a web application. | ||
| /// </summary> | ||
| /// <remarks>The <see cref="OfficeViewer"/> component allows users to display Office files such as Word, | ||
| /// Excel, or PowerPoint documents. It provides configurable options for the document URL, viewer height, and a callback | ||
| /// for when the document is loaded.</remarks> | ||
| public partial class OfficeViewer | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the url for the Office file to be displayed. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string? Url { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the viewer height. Default is null. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string? Height { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the document loaded event callback. | ||
| /// </summary> | ||
| [Parameter] | ||
| public Func<Task>? OnLoaded { get; set; } | ||
|
|
||
| [Inject, NotNull] | ||
| private NavigationManager? NavigationManager { get; set; } | ||
|
|
||
| private string? ClassString => CssBuilder.Default("bb-office-viewer-container") | ||
| .AddClassFromAttributes(AdditionalAttributes) | ||
| .Build(); | ||
|
|
||
| private string? StyleString => CssBuilder.Default() | ||
| .AddClass($"--bb-office-viewer-height: {Height};", !string.IsNullOrEmpty(Height)) | ||
| .Build(); | ||
|
|
||
| private string? _url; | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="firstRender"></param> | ||
| /// <returns></returns> | ||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||
| { | ||
| await base.OnAfterRenderAsync(firstRender); | ||
|
|
||
| if (firstRender) | ||
| { | ||
| _url = Url; | ||
| return; | ||
| } | ||
|
|
||
| var rerender = false; | ||
| if (_url != Url) | ||
| { | ||
| _url = Url; | ||
| rerender = true; | ||
| } | ||
|
|
||
| if (rerender) | ||
| { | ||
| await InvokeVoidAsync("load", Id, GetAbsoluteUri(_url)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new | ||
| { | ||
| LoadedCallaback = nameof(TriggerOnLoaded), | ||
| Url = GetAbsoluteUri(Url) | ||
| }); | ||
|
|
||
| private string GetAbsoluteUri(string? url) | ||
| { | ||
| url ??= string.Empty; | ||
| if (string.IsNullOrEmpty(url)) | ||
| { | ||
| return url; | ||
| } | ||
| var uri = NavigationManager.ToAbsoluteUri(url); | ||
| return uri.AbsoluteUri; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Trigger OnLoaded callback when the PDF document is loaded. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [JSInvokable] | ||
| public async Task TriggerOnLoaded() | ||
| { | ||
| if (OnLoaded != null) | ||
| { | ||
| await OnLoaded(); | ||
| } | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
src/components/BootstrapBlazor.OfficeViewer/OfficeViewer.razor.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { addLink } from "../BootstrapBlazor/modules/utility.js" | ||
| import Data from "../BootstrapBlazor/modules/data.js" | ||
|
|
||
| export async function init(id, invoke, options) { | ||
| await addLink("./_content/BootstrapBlazor.OfficeViewer/office-viewer.css"); | ||
|
|
||
| const el = document.getElementById(id); | ||
| const officeViewer = { el, invoke, options }; | ||
| Data.set(id, officeViewer); | ||
|
|
||
| await load(id, options.url); | ||
| } | ||
|
|
||
| export async function load(id, url) { | ||
| const officeViewer = Data.get(id); | ||
| const { el, invoke, options } = officeViewer; | ||
|
|
||
| el.innerHTML = ''; | ||
|
|
||
| if (url) { | ||
| const { frame } = officeViewer; | ||
| const viewer = frame || createFrame(el); | ||
| if (options.loadedCallaback) { | ||
| viewer.onload = () => { | ||
| invoke.invokeMethodAsync(options.loadedCallaback); | ||
| }; | ||
| } | ||
| viewer.src = `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(url)}`; | ||
| } | ||
| } | ||
|
|
||
| const createFrame = el => { | ||
| const frame = document.createElement('iframe'); | ||
| frame.classList.add('bb-office-viewer'); | ||
| el.appendChild(frame); | ||
| return frame; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| @using BootstrapBlazor.Components; | ||
| @using Microsoft.AspNetCore.Components.Web |
9 changes: 9 additions & 0 deletions
9
src/components/BootstrapBlazor.OfficeViewer/wwwroot/office-viewer.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| .bb-office-viewer-container { | ||
| width: 100%; | ||
| height: var(--bb-office-viewer-height, 500px); | ||
| } | ||
|
|
||
| .bb-office-viewer { | ||
| width: 100%; | ||
| height: 100%; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.