Skip to content

Commit

Permalink
🆕 feat(PopupService): add support for clearing all opended popups (#2098
Browse files Browse the repository at this point in the history
)

* 🆕 feat(PopupService): add support for clearing all opended popups

* 📝 docs: add support for adding api meta for non-components

* missing files
  • Loading branch information
capdiem committed Aug 12, 2024
1 parent f6e8721 commit 17b870a
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 107 deletions.
8 changes: 5 additions & 3 deletions docs/Masa.Blazor.Docs/Pages/Components.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private async Task ReadApisAsync()
foreach (var item in apis)
{
var (dir, componentName) = Resolve(item, Page);
_apiData[componentName] = await getApiGroupAsync(dir, componentName, true);
_apiData[componentName] = await getApiGroupAsync(dir, componentName, true);
}
}
else
Expand All @@ -214,7 +214,9 @@ private async Task ReadApisAsync()

async Task<Dictionary<string, List<ParameterInfo>>> getApiGroupAsync(string dir, string componentName, bool isFullname = false)
{
var otherApis = await BlazorDocService.GetOtherApisAsync();
var componentApiMetas = GetAllComponentApiMetas();
componentApiMetas.AddRange(otherApis);

var component = isFullname
? componentApiMetas.FirstOrDefault(u => u.Name == componentName)
Expand All @@ -224,7 +226,7 @@ async Task<Dictionary<string, List<ParameterInfo>>> getApiGroupAsync(string dir,
if (component is not null)
{
componentName = component.Name;

var parametersCacheValue = component.Parameters;

parametersCacheValue = parametersCacheValue.Where(item => item.Value.Count > 0).ToDictionary(item => item.Key, item => item.Value);
Expand Down Expand Up @@ -265,7 +267,7 @@ async Task<Dictionary<string, List<ParameterInfo>>> getApiGroupAsync(string dir,
return name?.TrimEnd('s').ToPascal();
}

private static IEnumerable<ComponentMeta> GetAllComponentApiMetas()
private static List<ComponentMeta> GetAllComponentApiMetas()
{
var list = ApiGenerator.ComponentMetas.ToList();
list.AddRange(SomethingSkiaApiGenerator.ComponentMetas);
Expand Down
27 changes: 26 additions & 1 deletion docs/Masa.Blazor.Docs/Services/BlazorDocService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http.Json;
using System.Text.Json;

namespace Masa.Blazor.Docs.Services;

Expand All @@ -12,6 +13,7 @@ public class BlazorDocService
private readonly Lazy<Task<Dictionary<string, Dictionary<string, Dictionary<string, string>>>?>> _commonApis;

private Dictionary<string, List<string>>? _apiInPageCache;
private List<ComponentMeta>? _otherApis;

public BlazorDocService(IHttpClientFactory factory, I18n i18n)
{
Expand Down Expand Up @@ -61,6 +63,29 @@ public async Task<Dictionary<string, List<string>>> ReadPageToApiAsync()
return _apiInPageCache ?? new Dictionary<string, List<string>>();
}

public async Task<List<ComponentMeta>> GetOtherApisAsync()
{
if (_otherApis is not null)
{
return _otherApis;
}

try
{
var json = await _httpClient.GetStringAsync("_content/Masa.Blazor.Docs/data/other-apis.json");
_otherApis = JsonSerializer.Deserialize<List<ComponentMeta>>(json, new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
UnmappedMemberHandling = System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip
});
}
catch (Exception)
{
// ignored
}

return _otherApis ?? [];
}

public async Task<Dictionary<string, Dictionary<string, string>>?> ReadApisAsync(string kebabCaseComponent, string? apiName = null)
{
var key = $"{kebabCaseComponent}/{(apiName is null ? "" : apiName + "-")}{_i18n.Culture.Name}";
Expand Down Expand Up @@ -96,7 +121,7 @@ public async Task<Dictionary<string, List<string>>> ReadPageToApiAsync()
api.TryAdd(prop, desc);
}
}
if (commonApiInfo.TryGetValue(category, out var commonApi))
{
foreach (var (prop, desc) in commonApi)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"props": {},
"events": {},
"contents": {},
"methods": {
"openAsync": "Open the specified type of popup component and wait for the result.",
"open": "Open the specified type of popup component.",
"close": "Close the specified type of popup component.",
"clear": "Clear all opened popup components.",
"enqueueSnackbarAsync": "Enqueue a snackbar to show."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"props": {},
"events": {},
"contents": {},
"methods": {
"openAsync": "打开指定类型的弹出组件,可等待结果",
"open": "打开指定类型的弹出组件",
"close": "关闭指定类型的弹出组件",
"clear": "关闭所有弹出组件",
"enqueueSnackbarAsync": "新增消息提示"
}
}
30 changes: 30 additions & 0 deletions docs/Masa.Blazor.Docs/wwwroot/data/other-apis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"name": "IPopupService",
"parameters": {
"methods": [
{
"name": "OpenAsync",
"type": "(Type componentType, Dictionary<string, object?> parameters) => Task<object?>"
},
{
"name": "Open",
"type": "(Type componentType, Dictionary<string, object?> parameters) => void"
},
{
"name": "Close",
"type": "(Type componentType) => void"
},
{
"name": "Clear",
"type": "() => void",
"releasedOn": "v1.7.0"
},
{
"name": "EnqueueSnackbarAsync",
"type": "(SnackbarOptions options) => Task"
}
]
}
}
]
3 changes: 3 additions & 0 deletions docs/Masa.Blazor.Docs/wwwroot/data/page-to-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
"PPageContainer",
"PPageProvider"
],
"popup-service": [
"IPopupService"
],
"radios": [
"MRadio",
"MRadioGroup"
Expand Down
2 changes: 1 addition & 1 deletion src/Masa.Blazor/Components/App/MApp.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@ChildContent
</div>

@foreach (var item in PopupProvider.GetItems())
@foreach (var item in PopupService.GetItems())
{
<CascadingValue @key="item" Value="item">
<DynamicComponent Type="item.ComponentType" Parameters="item.Parameters"></DynamicComponent>
Expand Down
8 changes: 5 additions & 3 deletions src/Masa.Blazor/Components/App/MApp.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ public partial class MApp : MasaComponentBase, IDefaultsProvider
{
[Inject] private MasaBlazor MasaBlazor { get; set; } = null!;

[Inject] private IPopupProvider PopupProvider { get; set; } = null!;
[Inject] private IPopupService InternalPopupService { get; set; } = null!;

[Parameter] public RenderFragment? ChildContent { get; set; }

public IDictionary<string, IDictionary<string, object?>?>? Defaults => MasaBlazor.Defaults;

protected bool IsDark => MasaBlazor?.Theme is { Dark: true };

private PopupService PopupService => (PopupService)InternalPopupService;

protected override void OnInitialized()
{
base.OnInitialized();

PopupProvider.StateChanged += OnStateChanged;
PopupService.StateChanged += OnStateChanged;
MasaBlazor.OnThemeChange += OnThemeChange;
MasaBlazor.RTLChanged += OnRTLChanged;
MasaBlazor.DefaultsChanged += OnDefaultsChanged;
Expand Down Expand Up @@ -87,7 +89,7 @@ protected override IEnumerable<string> BuildComponentClass()

protected override ValueTask DisposeAsyncCore()
{
PopupProvider.StateChanged -= OnStateChanged;
PopupService.StateChanged -= OnStateChanged;
MasaBlazor.OnThemeChange -= OnThemeChange;
MasaBlazor.RTLChanged -= OnRTLChanged;
MasaBlazor.DefaultsChanged -= OnDefaultsChanged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ private static IMasaBlazorBuilder AddMasaBlazorInternal(this IServiceCollection
options.Defaults);
}, masaBlazorServiceLifetime));

services.TryAdd<IPopupProvider, PopupProvider>(masaBlazorServiceLifetime);
services.TryAdd<IPopupService, PopupService>(masaBlazorServiceLifetime);

services.TryAddScoped<IErrorHandler, MErrorHandler>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class EnqueuedSnackbars : ComponentBase, IAsyncDisposable
{
[Inject]
private IPopupService PopupService { get; set; } = null!;
private IPopupService InternalPopupService { get; set; } = null!;

[Inject]
private MasaBlazor MasaBlazor { get; set; } = null!;
Expand Down Expand Up @@ -40,6 +40,8 @@ public partial class EnqueuedSnackbars : ComponentBase, IAsyncDisposable

private PEnqueuedSnackbars? _enqueuedSnackbars;

private PopupService PopupService => (PopupService)InternalPopupService;

protected override async Task OnInitializedAsync()
{
PopupService.SnackbarOpen += OnSnackbarOpenAsync;
Expand Down
12 changes: 0 additions & 12 deletions src/Masa.Blazor/Popup/IPopupProvider.cs

This file was deleted.

34 changes: 27 additions & 7 deletions src/Masa.Blazor/Popup/IPopupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@

public interface IPopupService
{
Task<object?> OpenAsync(Type componentType, IDictionary<string, object?> parameters);

void Open(Type componentType, IDictionary<string, object?>? parameters = null);
/// <summary>
/// Clear all opened popup components.
/// </summary>
void Clear();

/// <summary>
/// Close the specified type of popup component.
/// </summary>
/// <param name="componentType"></param>
void Close(Type componentType);

#region Snackbar

event Func<SnackbarOptions, Task> SnackbarOpen;
/// <summary>
/// Enqueue a snackbar to show.
/// </summary>
/// <param name="options"></param>
/// <returns></returns
Task EnqueueSnackbarAsync(SnackbarOptions options);

#endregion
/// <summary>
/// Open the specified type of popup component and wait for the result.
/// </summary>
/// <param name="componentType"></param>
/// <param name="parameters"></param>
/// <returns></returns>
Task<object?> OpenAsync(Type componentType, IDictionary<string, object?> parameters);

/// <summary>
/// Open the specified type of popup component.
/// </summary>
/// <param name="componentType"></param>
/// <param name="parameters"></param>
void Open(Type componentType, IDictionary<string, object?>? parameters = null);
}
46 changes: 0 additions & 46 deletions src/Masa.Blazor/Popup/PopupProvider.cs

This file was deleted.

Loading

0 comments on commit 17b870a

Please sign in to comment.