-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathTextEditorGroupDisplay.razor.cs
87 lines (71 loc) · 3.23 KB
/
TextEditorGroupDisplay.razor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Microsoft.AspNetCore.Components;
using System.Collections.Immutable;
using Luthetus.Common.RazorLib.Tabs.Displays;
using Luthetus.Common.RazorLib.Keys.Models;
using Luthetus.TextEditor.RazorLib.Groups.Models;
using Luthetus.TextEditor.RazorLib.TextEditors.Models.Internals;
using Luthetus.Common.RazorLib.Dynamics.Models;
namespace Luthetus.TextEditor.RazorLib.Groups.Displays;
public partial class TextEditorGroupDisplay : ComponentBase, IDisposable
{
[Inject]
private ITextEditorService TextEditorService { get; set; } = null!;
/// <summary>
/// If the provided <see cref="TextEditorGroupKey"/> is registered using the
/// <see cref="ITextEditorService"/>. Then this component will automatically update
/// when the corresponding <see cref="TextEditorGroup"/> is replaced.
/// <br/><br/>
/// A <see cref="TextEditorGroupKey"/> which is NOT registered using the
/// <see cref="ITextEditorService"/> can be passed in. Then if the <see cref="TextEditorGroupKey"/>
/// ever gets registered then this Blazor Component will update accordingly.
/// </summary>
[Parameter, EditorRequired]
public Key<TextEditorGroup> TextEditorGroupKey { get; set; } = Key<TextEditorGroup>.Empty;
[Parameter]
public string CssStyleString { get; set; } = string.Empty;
[Parameter]
public string CssClassString { get; set; } = string.Empty;
/// <summary><see cref="HeaderButtonKindList"/> contains the enum value that represents a button displayed in the optional component: <see cref="TextEditorHeader"/>.</summary>
[Parameter]
public ViewModelDisplayOptions ViewModelDisplayOptions { get; set; } = new();
private TabListDisplay? _tabListDisplay;
private string? _htmlId = null;
private string HtmlId => _htmlId ??= $"luth_te_group_{TextEditorGroupKey.Guid}";
protected override void OnInitialized()
{
TextEditorService.GroupApi.TextEditorGroupStateChanged += TextEditorGroupWrapOnStateChanged;
TextEditorService.TextEditorStateChanged += TextEditorViewModelStateWrapOnStateChanged;
base.OnInitialized();
}
private async void TextEditorGroupWrapOnStateChanged() =>
await InvokeAsync(StateHasChanged);
private async void TextEditorViewModelStateWrapOnStateChanged()
{
var localTabListDisplay = _tabListDisplay;
if (localTabListDisplay is not null)
{
await InvokeAsync(async () => await localTabListDisplay.NotifyStateChangedAsync())
.ConfigureAwait(false);
}
}
private List<ITab> GetTabList(TextEditorGroup textEditorGroup)
{
var textEditorState = TextEditorService.TextEditorState;
var tabList = new List<ITab>();
foreach (var viewModelKey in textEditorGroup.ViewModelKeyList)
{
var viewModel = textEditorState.ViewModelGetOrDefault(viewModelKey);
if (viewModel is not null)
{
viewModel.DynamicViewModelAdapter.TabGroup = textEditorGroup;
tabList.Add(viewModel.DynamicViewModelAdapter);
}
}
return tabList;
}
public void Dispose()
{
TextEditorService.GroupApi.TextEditorGroupStateChanged -= TextEditorGroupWrapOnStateChanged;
TextEditorService.TextEditorStateChanged -= TextEditorViewModelStateWrapOnStateChanged;
}
}