-
-
Notifications
You must be signed in to change notification settings - Fork 364
feat(BootstrapBlazorOutlet): add BootstrapBlazorOutlet component #5482
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
24f025f
feat: 增加 RootOutlet/Content 组件
ArgoZhang fae04df
feat: 增加 RootOutlet 组件
ArgoZhang fdd3902
refactor: Outlet/Content 组件
ArgoZhang e9b1f6f
feat: 增加 BootstrapBlazorRootRegisterService 服务
ArgoZhang 28bf162
test: 增加单元测试
ArgoZhang cb4d4dc
test: 增加单元测试
ArgoZhang 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
96 changes: 96 additions & 0 deletions
96
src/BootstrapBlazor/Components/BootstrapBlazorRootOutlet/BootstrapBlazorRootContent.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,96 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// BootstrapBlazorRootContent Component | ||
| /// </summary> | ||
| public class BootstrapBlazorRootContent : IComponent, IDisposable | ||
| { | ||
| private object? _registeredIdentifier; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the <see cref="string"/> ID that determines which <see cref="BootstrapBlazorRootOutlet"/> instance will render | ||
| /// the content of this instance. | ||
| /// </summary> | ||
| [Parameter] public string? RootName { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the <see cref="object"/> ID that determines which <see cref="BootstrapBlazorRootOutlet"/> instance will render | ||
| /// the content of this instance. | ||
| /// </summary> | ||
| [Parameter] public object? RootId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the content. | ||
| /// </summary> | ||
| [Parameter] | ||
| public RenderFragment? ChildContent { get; set; } | ||
|
|
||
| [Inject] | ||
| private BootstrapBlazorRootRegisterService RootRegisterService { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="renderHandle"></param> | ||
| void IComponent.Attach(RenderHandle renderHandle) | ||
| { | ||
|
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="parameters"></param> | ||
| /// <returns></returns> | ||
| Task IComponent.SetParametersAsync(ParameterView parameters) | ||
| { | ||
| parameters.SetParameterProperties(this); | ||
|
|
||
| object? identifier = null; | ||
|
|
||
| if (RootName is not null && RootId is not null) | ||
| { | ||
| throw new InvalidOperationException($"{nameof(BootstrapBlazorRootContent)} requires that '{nameof(RootName)}' and '{nameof(RootId)}' cannot both have non-null values."); | ||
| } | ||
| else if (RootName is not null) | ||
| { | ||
| identifier = RootName; | ||
| } | ||
| else if (RootId is not null) | ||
| { | ||
| identifier = RootId; | ||
| } | ||
| identifier ??= BootstrapBlazorRootOutlet.DefaultIdentifier; | ||
|
|
||
| if (!Equals(identifier, _registeredIdentifier)) | ||
| { | ||
| if (_registeredIdentifier is not null) | ||
| { | ||
| RootRegisterService.RemoveProvider(_registeredIdentifier, this); | ||
| } | ||
|
|
||
| RootRegisterService.AddProvider(identifier, this); | ||
| _registeredIdentifier = identifier; | ||
| } | ||
|
|
||
| RootRegisterService.NotifyContentProviderChanged(identifier, this); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| if (_registeredIdentifier is not null) | ||
| { | ||
| RootRegisterService.RemoveProvider(_registeredIdentifier, this); | ||
| } | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } |
144 changes: 144 additions & 0 deletions
144
src/BootstrapBlazor/Components/BootstrapBlazorRootOutlet/BootstrapBlazorRootOutlet.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,144 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using Microsoft.AspNetCore.Components.Rendering; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// BootstrapBlazorRootOutlet Component | ||
| /// </summary> | ||
| public class BootstrapBlazorRootOutlet : IComponent, IDisposable | ||
| { | ||
| private static readonly RenderFragment _emptyRenderFragment = _ => { }; | ||
| private object? _subscribedIdentifier; | ||
| private RenderHandle _renderHandle; | ||
|
|
||
| /// <summary> | ||
| /// Gets the default identifier that can be used to subscribe to all <see cref="BootstrapBlazorRootContent"/> instances. | ||
| /// </summary> | ||
| public static readonly object DefaultIdentifier = new(); | ||
|
|
||
| [Inject] | ||
| private BootstrapBlazorRootRegisterService RootRegisterService { get; set; } = default!; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the <see cref="string"/> ID that determines which <see cref="BootstrapBlazorRootContent"/> instances will provide | ||
| /// content to this instance. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string? RootName { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the <see cref="object"/> ID that determines which <see cref="BootstrapBlazorRootContent"/> instances will provide | ||
| /// content to this instance. | ||
| /// </summary> | ||
| [Parameter] | ||
| public object? RootId { get; set; } | ||
|
|
||
| void IComponent.Attach(RenderHandle renderHandle) | ||
| { | ||
| _renderHandle = renderHandle; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="parameters"></param> | ||
| /// <returns></returns> | ||
| Task IComponent.SetParametersAsync(ParameterView parameters) | ||
| { | ||
| parameters.SetParameterProperties(this); | ||
|
|
||
| object? identifier = null; | ||
|
|
||
| if (RootName is not null && RootId is not null) | ||
| { | ||
| throw new InvalidOperationException($"{nameof(BootstrapBlazorRootOutlet)} requires that '{nameof(RootName)}' and '{nameof(RootId)}' cannot both have non-null values."); | ||
| } | ||
| else if (RootName is not null) | ||
| { | ||
| identifier = RootName; | ||
| } | ||
| else if (RootId is not null) | ||
| { | ||
| identifier = RootId; | ||
| } | ||
| identifier ??= DefaultIdentifier; | ||
|
|
||
| if (!Equals(identifier, _subscribedIdentifier)) | ||
| { | ||
| if (_subscribedIdentifier is not null) | ||
| { | ||
| RootRegisterService.Unsubscribe(_subscribedIdentifier); | ||
| } | ||
|
|
||
| RootRegisterService.Subscribe(identifier, this); | ||
| _subscribedIdentifier = identifier; | ||
| } | ||
|
|
||
| RenderContent(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| internal void ContentUpdated(BootstrapBlazorRootContent? provider) | ||
| { | ||
| RenderContent(); | ||
| } | ||
|
|
||
| private void RenderContent() | ||
| { | ||
| _renderHandle.Render(BuildRenderTree); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="builder"></param> | ||
| private void BuildRenderTree(RenderTreeBuilder builder) | ||
| { | ||
| if (_subscribedIdentifier is not null) | ||
| { | ||
| foreach (var content in RootRegisterService.GetProviders(_subscribedIdentifier)) | ||
| { | ||
| builder.OpenComponent<BootstrapBlazorRootOutletContentRenderer>(0); | ||
| builder.SetKey(content); | ||
| builder.AddAttribute(1, BootstrapBlazorRootOutletContentRenderer.ContentParameterName, content.ChildContent ?? _emptyRenderFragment); | ||
| builder.CloseComponent(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| if (_subscribedIdentifier is not null) | ||
| { | ||
| RootRegisterService.Unsubscribe(_subscribedIdentifier); | ||
| } | ||
| GC.SuppressFinalize(this); | ||
| } | ||
|
|
||
| internal sealed class BootstrapBlazorRootOutletContentRenderer : IComponent | ||
| { | ||
| public const string ContentParameterName = "content"; | ||
|
|
||
| private RenderHandle _renderHandle; | ||
|
|
||
| public void Attach(RenderHandle renderHandle) | ||
| { | ||
| _renderHandle = renderHandle; | ||
| } | ||
|
|
||
| public Task SetParametersAsync(ParameterView parameters) | ||
| { | ||
| var fragment = parameters.GetValueOrDefault<RenderFragment>(ContentParameterName)!; | ||
| _renderHandle.Render(fragment); | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } | ||
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
130 changes: 130 additions & 0 deletions
130
src/BootstrapBlazor/Services/BootstrapBlazorRootRegisterService.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,130 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// BootstrapBlazorRootRegisterService | ||
| /// </summary> | ||
| public class BootstrapBlazorRootRegisterService | ||
| { | ||
| private readonly Dictionary<object, BootstrapBlazorRootOutlet> _subscribersByIdentifier = []; | ||
| private readonly Dictionary<object, List<BootstrapBlazorRootContent>> _providersByIdentifier = []; | ||
|
|
||
| /// <summary> | ||
| /// add provider | ||
| /// </summary> | ||
| /// <param name="identifier"></param> | ||
| /// <param name="provider"></param> | ||
| public void AddProvider(object identifier, BootstrapBlazorRootContent provider) | ||
| { | ||
| if (!_providersByIdentifier.TryGetValue(identifier, out var providers)) | ||
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| providers = []; | ||
| _providersByIdentifier.Add(identifier, providers); | ||
| } | ||
|
|
||
| providers.Add(provider); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// remove provider | ||
| /// </summary> | ||
| /// <param name="identifier"></param> | ||
| /// <param name="provider"></param> | ||
| public void RemoveProvider(object identifier, BootstrapBlazorRootContent provider) | ||
| { | ||
| if (!_providersByIdentifier.TryGetValue(identifier, out var providers)) | ||
| { | ||
| throw new InvalidOperationException($"There are no content providers with the given root ID '{identifier}'."); | ||
| } | ||
|
|
||
| var index = providers.LastIndexOf(provider); | ||
| if (index < 0) | ||
| { | ||
| throw new InvalidOperationException($"The provider was not found in the providers list of the given root ID '{identifier}'."); | ||
| } | ||
|
|
||
| providers.RemoveAt(index); | ||
| if (index == providers.Count) | ||
| { | ||
| // We just removed the most recently added provider, meaning we need to change | ||
| // the current content to that of second most recently added provider. | ||
| var contentProvider = GetCurrentProviderContentOrDefault(providers); | ||
| NotifyContentChangedForSubscriber(identifier, contentProvider); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// get all providers by identifier | ||
| /// </summary> | ||
| /// <param name="identifier"></param> | ||
| /// <returns></returns> | ||
| public List<BootstrapBlazorRootContent> GetProviders(object identifier) | ||
| { | ||
| _providersByIdentifier.TryGetValue(identifier, out var providers); | ||
| return providers ?? []; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// subscribe | ||
| /// </summary> | ||
| /// <param name="identifier"></param> | ||
| /// <param name="subscriber"></param> | ||
| public void Subscribe(object identifier, BootstrapBlazorRootOutlet subscriber) | ||
| { | ||
| if (_subscribersByIdentifier.ContainsKey(identifier)) | ||
| { | ||
| throw new InvalidOperationException($"There is already a subscriber to the content with the given root ID '{identifier}'."); | ||
| } | ||
|
|
||
| _subscribersByIdentifier.Add(identifier, subscriber); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 取消订阅 | ||
| /// </summary> | ||
| /// <param name="identifier"></param> | ||
| public void Unsubscribe(object identifier) | ||
| { | ||
| if (!_subscribersByIdentifier.Remove(identifier)) | ||
| { | ||
| throw new InvalidOperationException($"The subscriber with the given root ID '{identifier}' is already unsubscribed."); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Notify content provider changed | ||
| /// </summary> | ||
| /// <param name="identifier"></param> | ||
| /// <param name="provider"></param> | ||
| public void NotifyContentProviderChanged(object identifier, BootstrapBlazorRootContent provider) | ||
| { | ||
| if (!_providersByIdentifier.TryGetValue(identifier, out var providers)) | ||
| { | ||
| throw new InvalidOperationException($"There are no content providers with the given root ID '{identifier}'."); | ||
| } | ||
|
|
||
| // We only notify content changed for subscribers when the content of the | ||
| // most recently added provider changes. | ||
| if (providers.Count != 0 && providers[^1] == provider) | ||
| { | ||
| NotifyContentChangedForSubscriber(identifier, provider); | ||
| } | ||
| } | ||
|
|
||
| private static BootstrapBlazorRootContent? GetCurrentProviderContentOrDefault(List<BootstrapBlazorRootContent> providers) | ||
| => providers.Count != 0 | ||
| ? providers[^1] | ||
| : null; | ||
|
|
||
| private void NotifyContentChangedForSubscriber(object identifier, BootstrapBlazorRootContent? provider) | ||
| { | ||
| if (_subscribersByIdentifier.TryGetValue(identifier, out var subscriber)) | ||
| { | ||
| subscriber.ContentUpdated(provider); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.