3
3
using System . Text ;
4
4
5
5
6
- namespace SignaturePad
6
+ namespace SigPad ;
7
+
8
+ public partial class SignaturePad
7
9
{
8
- public partial class SignaturePad
10
+ [ Parameter ]
11
+ public byte [ ] Value { get ; set ; } = [ ] ;
12
+
13
+ private byte [ ] ? _prevValue = null ;
14
+
15
+ [ Parameter ]
16
+ public EventCallback < byte [ ] > ValueChanged { get ; set ; }
17
+ [ Parameter ]
18
+ public SignaturePadOptions Options { get ; set ; } = new SignaturePadOptions ( ) ;
19
+
20
+ [ Parameter ]
21
+ public bool ShowClearButton { get ; set ; } = true ;
22
+ [ Parameter ]
23
+ public string ClearButtonClass { get ; set ; } = "btn btn-default" ;
24
+ [ Parameter ]
25
+ public string Class { get ; set ; } = "signature" ;
26
+ [ Parameter ]
27
+ public string ClearButtonText { get ; set ; } = "Clear Signature" ;
28
+ [ Parameter ]
29
+ public bool Disabled { get ; set ; }
30
+
31
+
32
+
33
+ /// <summary>
34
+ /// Captures all the custom attributes that are not part of BlazorBootstrap component.
35
+ /// </summary>
36
+ [ Parameter ( CaptureUnmatchedValues = true ) ]
37
+ public Dictionary < string , object > Attributes { get ; set ; } = [ ] ;
38
+
39
+ private readonly string _id = Guid . NewGuid ( ) . ToString ( ) ;
40
+ private readonly DotNetObjectReference < SignaturePad > _reference ;
41
+ private IJSObjectReference ? _jsModule ;
42
+
43
+
44
+ public SignaturePad ( )
45
+ {
46
+ _reference = DotNetObjectReference . Create ( this ) ;
47
+ }
48
+
49
+ [ JSInvokable ]
50
+ public async Task SignatureDataChangedAsync ( )
9
51
{
10
- [ Parameter ]
11
- public byte [ ] Value { get ; set ; } = [ ] ;
12
-
13
- private byte [ ] ? _prevValue = null ;
14
-
15
- [ Parameter ]
16
- public EventCallback < byte [ ] > ValueChanged { get ; set ; }
17
- [ Parameter ]
18
- public SignaturePadOptions Options { get ; set ; } = new SignaturePadOptions ( ) ;
19
-
20
- [ Parameter ]
21
- public bool ShowClearButton { get ; set ; } = true ;
22
- [ Parameter ]
23
- public string ClearButtonClass { get ; set ; } = "btn btn-default" ;
24
- [ Parameter ]
25
- public string Class { get ; set ; } = "signature" ;
26
- [ Parameter ]
27
- public string ClearButtonText { get ; set ; } = "Clear Signature" ;
28
- [ Parameter ]
29
- public bool Disabled { get ; set ; }
30
-
31
-
32
-
33
- /// <summary>
34
- /// Captures all the custom attributes that are not part of BlazorBootstrap component.
35
- /// </summary>
36
- [ Parameter ( CaptureUnmatchedValues = true ) ]
37
- public Dictionary < string , object > Attributes { get ; set ; } = [ ] ;
38
-
39
- private readonly string _id = Guid . NewGuid ( ) . ToString ( ) ;
40
- private readonly DotNetObjectReference < SignaturePad > _reference ;
41
- private IJSObjectReference ? _jsModule ;
42
-
43
-
44
- public SignaturePad ( )
52
+ using MemoryStream memoryStream = new ( ) ;
53
+ var dataReference = await _jsModule ! . InvokeAsync < IJSStreamReference > ( "getBase64" , _id ) ;
54
+ using var dataReferenceStream = await dataReference . OpenReadStreamAsync ( maxAllowedSize : 10_000_000 ) ;
55
+ await dataReferenceStream . CopyToAsync ( memoryStream ) ;
56
+
57
+ string base64 = Encoding . UTF8 . GetString ( memoryStream . ToArray ( ) ) ;
58
+
59
+ try
45
60
{
46
- _reference = DotNetObjectReference . Create ( this ) ;
61
+ Value = Convert . FromBase64String ( base64 ) ;
62
+ _prevValue = Value ;
47
63
}
48
-
49
- [ JSInvokable ]
50
- public async Task SignatureDataChangedAsync ( )
64
+ catch ( Exception )
51
65
{
52
- using MemoryStream memoryStream = new ( ) ;
53
- var dataReference = await _jsModule ! . InvokeAsync < IJSStreamReference > ( "getBase64" , _id ) ;
54
- using var dataReferenceStream = await dataReference . OpenReadStreamAsync ( maxAllowedSize : 10_000_000 ) ;
55
- await dataReferenceStream . CopyToAsync ( memoryStream ) ;
56
-
57
- string base64 = Encoding . UTF8 . GetString ( memoryStream . ToArray ( ) ) ;
66
+ Value = [ ] ;
67
+ }
58
68
59
- try
60
- {
61
- Value = Convert . FromBase64String ( base64 ) ;
62
- _prevValue = Value ;
63
- }
64
- catch ( Exception )
65
- {
66
- Value = [ ] ;
67
- }
68
69
70
+ await ValueChanged . InvokeAsync ( Value ) ;
69
71
70
- await ValueChanged . InvokeAsync ( Value ) ;
72
+ }
71
73
74
+ protected async override Task OnAfterRenderAsync ( bool firstRender )
75
+ {
76
+ if ( firstRender )
77
+ {
78
+ _jsModule = await jsRuntime . InvokeAsync < IJSObjectReference > ( "import" , "./_content/Blazor.SignaturePad/sigpad.interop.js?ver=8.1.5" ) ;
79
+ await Setup ( ) ;
80
+ await Update ( ) ;
81
+ await UpdateImage ( ) ;
72
82
}
73
83
74
- protected async override Task OnAfterRenderAsync ( bool firstRender )
75
- {
76
- if ( firstRender )
77
- {
78
- _jsModule = await jsRuntime . InvokeAsync < IJSObjectReference > ( "import" , "./_content/Blazor.SignaturePad/sigpad.interop.js?ver=8.1.5" ) ;
79
- await Setup ( ) ;
80
- await Update ( ) ;
81
- await UpdateImage ( ) ;
82
- }
84
+ await base . OnAfterRenderAsync ( firstRender ) ;
85
+ }
83
86
84
- await base . OnAfterRenderAsync ( firstRender ) ;
85
- }
87
+ protected override async Task OnParametersSetAsync ( )
88
+ {
89
+ await Update ( ) ;
90
+ await UpdateImage ( ) ;
91
+ }
92
+
93
+ private static string ByteToData ( byte [ ] data )
94
+ {
95
+ return $ "{ Encoding . UTF8 . GetString ( data ) } ";
96
+ }
86
97
87
- protected override async Task OnParametersSetAsync ( )
98
+ private async Task Setup ( )
99
+ {
100
+ if ( _jsModule is not null )
88
101
{
89
- await Update ( ) ;
90
- await UpdateImage ( ) ;
102
+ await _jsModule . InvokeVoidAsync ( "setup" , [ _id , _reference , Options . ToJSON ( ) , Value is null ? string . Empty : ByteToData ( Value ) ] ) ;
91
103
}
104
+ }
92
105
93
- private static string ByteToData ( byte [ ] data )
106
+ private async Task Update ( )
107
+ {
108
+ if ( _jsModule is not null )
94
109
{
95
- return $ " { Encoding . UTF8 . GetString ( data ) } " ;
110
+ await _jsModule . InvokeVoidAsync ( "update" , [ _id , Options . ToJSON ( ) ] ) ;
96
111
}
112
+ }
97
113
98
- private async Task Setup ( )
114
+ [ JSInvokable ]
115
+ public async Task UpdateImage ( )
116
+ {
117
+ if ( _jsModule is not null && ! ( _prevValue ? . SequenceEqual ( Value ) ?? false ) )
99
118
{
100
- if ( _jsModule is not null )
101
- {
102
- await _jsModule . InvokeVoidAsync ( "setup" , [ _id , _reference , Options . ToJSON ( ) , Value is null ? string . Empty : ByteToData ( Value ) ] ) ;
103
- }
119
+ await _jsModule . InvokeVoidAsync ( "updateImage" , [ _id , Value is null ? string . Empty : ByteToData ( Value ) ] ) ;
104
120
}
121
+ }
105
122
106
- private async Task Update ( )
123
+ public async ValueTask DisposeAsync ( )
124
+ {
125
+ if ( _jsModule is not null )
107
126
{
108
- if ( _jsModule is not null )
127
+ try
109
128
{
110
- await _jsModule . InvokeVoidAsync ( "update" , [ _id , Options . ToJSON ( ) ] ) ;
129
+ await _jsModule . InvokeVoidAsync ( "destroy" , [ _id ] ) ;
130
+ await _jsModule . DisposeAsync ( ) ;
111
131
}
112
- }
113
-
114
- [ JSInvokable ]
115
- public async Task UpdateImage ( )
116
- {
117
- if ( _jsModule is not null && ! ( _prevValue ? . SequenceEqual ( Value ) ?? false ) )
132
+ catch ( TaskCanceledException )
118
133
{
119
- await _jsModule . InvokeVoidAsync ( "updateImage" , [ _id , Value is null ? string . Empty : ByteToData ( Value ) ] ) ;
120
134
}
121
- }
122
-
123
- public async ValueTask DisposeAsync ( )
124
- {
125
- if ( _jsModule is not null )
135
+ catch ( JSDisconnectedException )
126
136
{
127
- try
128
- {
129
- await _jsModule . InvokeVoidAsync ( "destroy" , [ _id ] ) ;
130
- await _jsModule . DisposeAsync ( ) ;
131
- }
132
- catch ( TaskCanceledException )
133
- {
134
- }
135
- catch ( JSDisconnectedException )
136
- {
137
- }
138
137
}
139
-
140
138
}
141
139
142
- public async Task Clear ( )
140
+ }
141
+
142
+ public async Task Clear ( )
143
+ {
144
+ if ( _jsModule is not null )
143
145
{
144
- if ( _jsModule is not null )
145
- {
146
- await _jsModule . InvokeVoidAsync ( "clear" , [ _id , Value is null ? String . Empty : ByteToData ( Value ) ] ) ;
147
- _prevValue = null ;
148
- Value = [ ] ;
149
- await ValueChanged . InvokeAsync ( Value ) ;
150
- await UpdateImage ( ) ;
151
- }
146
+ await _jsModule . InvokeVoidAsync ( "clear" , [ _id , Value is null ? String . Empty : ByteToData ( Value ) ] ) ;
147
+ _prevValue = null ;
148
+ Value = [ ] ;
149
+ await ValueChanged . InvokeAsync ( Value ) ;
150
+ await UpdateImage ( ) ;
152
151
}
153
152
}
154
153
}
0 commit comments