Skip to content

Commit 6897a70

Browse files
authored
feat(blazorui): improve BitMarkdownEditor bitfoundation#10271 (bitfoundation#10272)
1 parent 76e1aeb commit 6897a70

File tree

6 files changed

+334
-149
lines changed

6 files changed

+334
-149
lines changed

src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownEditor/BitMarkdownEditor.razor.cs

+54-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,32 @@
66
public partial class BitMarkdownEditor : BitComponentBase
77
{
88
private ElementReference _textAreaRef = default!;
9+
private DotNetObjectReference<BitMarkdownEditor>? _dotnetObj = null;
910

1011

1112

1213
[Inject] private IJSRuntime _js { get; set; } = default!;
1314

1415

1516

17+
/// <summary>
18+
/// The default text value of the editor to use at initialization.
19+
/// </summary>
20+
[Parameter] public string? DefaultValue { get; set; }
21+
22+
/// <summary>
23+
/// Callback for when the editor value changes.
24+
/// </summary>
25+
[Parameter] public EventCallback<string?> OnChange { get; set; }
26+
27+
/// <summary>
28+
/// The two-way bound text value of the editor.
29+
/// </summary>
30+
[Parameter, TwoWayBound]
31+
public string? Value { get; set; }
32+
33+
34+
1635
/// <summary>
1736
/// Returns the current value of the editor.
1837
/// </summary>
@@ -23,15 +42,46 @@ public async ValueTask<string> GetValue()
2342

2443

2544

45+
[JSInvokable("OnChange")]
46+
public async Task _OnChange(string? value)
47+
{
48+
await AssignValue(value);
49+
await OnChange.InvokeAsync(value);
50+
}
51+
52+
53+
2654
protected override string RootElementClass => "bit-mde";
2755

28-
protected override Task OnAfterRenderAsync(bool firstRender)
56+
protected override async Task OnAfterRenderAsync(bool firstRender)
57+
{
58+
await base.OnAfterRenderAsync(firstRender);
59+
60+
if (firstRender is false) return;
61+
62+
if ((ValueHasBeenSet && ValueChanged.HasDelegate) || OnChange.HasDelegate)
63+
{
64+
_dotnetObj = DotNetObjectReference.Create(this);
65+
}
66+
67+
await _js.BitMarkdownEditorInit(_Id, _textAreaRef, _dotnetObj, DefaultValue);
68+
}
69+
70+
71+
72+
protected override async ValueTask DisposeAsync(bool disposing)
2973
{
30-
if (firstRender)
74+
if (IsDisposed || disposing is false) return;
75+
76+
_dotnetObj?.Dispose();
77+
78+
try
3179
{
32-
_js.BitMarkdownEditorInit(_Id, _textAreaRef);
80+
await _js.BitMarkdownEditorDispose(_Id);
3381
}
82+
catch (JSDisconnectedException) { } // we can ignore this exception here
83+
3484

35-
return base.OnAfterRenderAsync(firstRender);
85+
await base.DisposeAsync(disposing);
3686
}
3787
}

src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownEditor/BitMarkdownEditor.scss

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
.bit-mde {
44
width: 100%;
5+
height: 100%;
56
box-sizing: border-box;
67
}
78

0 commit comments

Comments
 (0)