6
6
public partial class BitMarkdownEditor : BitComponentBase
7
7
{
8
8
private ElementReference _textAreaRef = default ! ;
9
+ private DotNetObjectReference < BitMarkdownEditor > ? _dotnetObj = null ;
9
10
10
11
11
12
12
13
[ Inject ] private IJSRuntime _js { get ; set ; } = default ! ;
13
14
14
15
15
16
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
+
16
35
/// <summary>
17
36
/// Returns the current value of the editor.
18
37
/// </summary>
@@ -23,15 +42,46 @@ public async ValueTask<string> GetValue()
23
42
24
43
25
44
45
+ [ JSInvokable ( "OnChange" ) ]
46
+ public async Task _OnChange ( string ? value )
47
+ {
48
+ await AssignValue ( value ) ;
49
+ await OnChange . InvokeAsync ( value ) ;
50
+ }
51
+
52
+
53
+
26
54
protected override string RootElementClass => "bit-mde" ;
27
55
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 )
29
73
{
30
- if ( firstRender )
74
+ if ( IsDisposed || disposing is false ) return ;
75
+
76
+ _dotnetObj ? . Dispose ( ) ;
77
+
78
+ try
31
79
{
32
- _js . BitMarkdownEditorInit ( _Id , _textAreaRef ) ;
80
+ await _js . BitMarkdownEditorDispose ( _Id ) ;
33
81
}
82
+ catch ( JSDisconnectedException ) { } // we can ignore this exception here
83
+
34
84
35
- return base . OnAfterRenderAsync ( firstRender ) ;
85
+ await base . DisposeAsync ( disposing ) ;
36
86
}
37
87
}
0 commit comments