Skip to content

Commit 369a57d

Browse files
Merge pull request #57 from MarvinKlein1508/change-namepsace
Change namespace to SigPad (Fixes #56)
2 parents ab1b5fd + 3dcacb5 commit 369a57d

File tree

11 files changed

+159
-163
lines changed

11 files changed

+159
-163
lines changed

BlazorServerDemo/_Imports.razor

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
@using Microsoft.JSInterop
99
@using BlazorServerDemo
1010
@using BlazorServerDemo.Shared
11-
@using SignaturePad
1211
@using Demos.Core.Models
1312
@using Demos.Core.Services
1413
@using Demos.Core

BlazorWebAssemblyDemo/Properties/launchSettings.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dotnetRunMessages": true,
1515
"launchBrowser": true,
1616
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
17-
"applicationUrl": "http://localhost:5243",
17+
"applicationUrl": "http://localhost:5000",
1818
"environmentVariables": {
1919
"ASPNETCORE_ENVIRONMENT": "Development"
2020
}
@@ -24,7 +24,7 @@
2424
"dotnetRunMessages": true,
2525
"launchBrowser": true,
2626
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
27-
"applicationUrl": "https://localhost:7279;http://localhost:5243",
27+
"applicationUrl": "https://localhost:5100;http://localhost:5000",
2828
"environmentVariables": {
2929
"ASPNETCORE_ENVIRONMENT": "Development"
3030
}

Demos.Core/SignaturePadDemos/HomeDemo.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@code {
2222
public MyInput Input { get; set; } = new();
23-
23+
private SignaturePad signaturePad = default!;
2424

2525
private void SaveSignature()
2626
{

Demos.Core/SignaturePadDemos/_Imports.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
@using Demos.Core
33
@using Demos.Core.Models
44
@using Demos.Core.Services
5-
@using SignaturePad
5+
@using SigPad

MudBlazorDemo/Components/_Imports.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@using MudBlazor.Services
1111
@using MudBlazorDemo
1212
@using MudBlazorDemo.Components
13-
@using SignaturePad
13+
@using SigPad
1414
@using Demos.Core.Models
1515
@using Demos.Core.Services
1616
@using Demos.Core.SignaturePadDemos

SignaturePad/LineCap.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
namespace SignaturePad
1+
namespace SigPad;
2+
3+
public enum LineCap
24
{
3-
public enum LineCap
4-
{
5-
Butt,
6-
Round,
7-
Square
8-
}
5+
Butt,
6+
Round,
7+
Square
98
}

SignaturePad/LineJoin.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
namespace SignaturePad
1+
namespace SigPad;
2+
public enum LineJoin
23
{
3-
public enum LineJoin
4-
{
5-
Miter,
6-
Round,
7-
Bevel
8-
}
4+
Miter,
5+
Round,
6+
Bevel
97
}

SignaturePad/SignaturePad.csproj

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
<PackageId>Blazor.$(AssemblyName)</PackageId>
1717
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
19+
<RootNamespace>SigPad</RootNamespace>
1920
</PropertyGroup>
2021

2122
<ItemGroup>
22-
<None Include="..\LICENSE">
23-
<Pack>True</Pack>
24-
<PackagePath>\</PackagePath>
25-
</None>
26-
<None Include="..\README.md">
27-
<Pack>True</Pack>
28-
<PackagePath>\</PackagePath>
29-
</None>
23+
<None Include="..\LICENSE">
24+
<Pack>True</Pack>
25+
<PackagePath>\</PackagePath>
26+
</None>
27+
<None Include="..\README.md">
28+
<Pack>True</Pack>
29+
<PackagePath>\</PackagePath>
30+
</None>
3031
</ItemGroup>
3132

3233

SignaturePad/SignaturePad.razor

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@inject Microsoft.JSInterop.IJSRuntime jsRuntime
2+
@namespace SigPad
23

34
<canvas class="@Class @(Disabled ? "disabled" : string.Empty)" id="signature-@_id" @attributes="@Attributes">
45
This application requires JavaScript to be enabled.

SignaturePad/SignaturePad.razor.cs

+113-114
Original file line numberDiff line numberDiff line change
@@ -3,152 +3,151 @@
33
using System.Text;
44

55

6-
namespace SignaturePad
6+
namespace SigPad;
7+
8+
public partial class SignaturePad
79
{
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()
951
{
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
4560
{
46-
_reference = DotNetObjectReference.Create(this);
61+
Value = Convert.FromBase64String(base64);
62+
_prevValue = Value;
4763
}
48-
49-
[JSInvokable]
50-
public async Task SignatureDataChangedAsync()
64+
catch (Exception)
5165
{
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+
}
5868

59-
try
60-
{
61-
Value = Convert.FromBase64String(base64);
62-
_prevValue = Value;
63-
}
64-
catch (Exception)
65-
{
66-
Value = [];
67-
}
6869

70+
await ValueChanged.InvokeAsync(Value);
6971

70-
await ValueChanged.InvokeAsync(Value);
72+
}
7173

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();
7282
}
7383

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+
}
8386

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+
}
8697

87-
protected override async Task OnParametersSetAsync()
98+
private async Task Setup()
99+
{
100+
if (_jsModule is not null)
88101
{
89-
await Update();
90-
await UpdateImage();
102+
await _jsModule.InvokeVoidAsync("setup", [_id, _reference, Options.ToJSON(), Value is null ? string.Empty : ByteToData(Value)]);
91103
}
104+
}
92105

93-
private static string ByteToData(byte[] data)
106+
private async Task Update()
107+
{
108+
if (_jsModule is not null)
94109
{
95-
return $"{Encoding.UTF8.GetString(data)}";
110+
await _jsModule.InvokeVoidAsync("update", [_id, Options.ToJSON()]);
96111
}
112+
}
97113

98-
private async Task Setup()
114+
[JSInvokable]
115+
public async Task UpdateImage()
116+
{
117+
if (_jsModule is not null && !(_prevValue?.SequenceEqual(Value) ?? false))
99118
{
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)]);
104120
}
121+
}
105122

106-
private async Task Update()
123+
public async ValueTask DisposeAsync()
124+
{
125+
if (_jsModule is not null)
107126
{
108-
if (_jsModule is not null)
127+
try
109128
{
110-
await _jsModule.InvokeVoidAsync("update", [_id, Options.ToJSON()]);
129+
await _jsModule.InvokeVoidAsync("destroy", [_id]);
130+
await _jsModule.DisposeAsync();
111131
}
112-
}
113-
114-
[JSInvokable]
115-
public async Task UpdateImage()
116-
{
117-
if (_jsModule is not null && !(_prevValue?.SequenceEqual(Value) ?? false))
132+
catch (TaskCanceledException)
118133
{
119-
await _jsModule.InvokeVoidAsync("updateImage", [_id, Value is null ? string.Empty : ByteToData(Value)]);
120134
}
121-
}
122-
123-
public async ValueTask DisposeAsync()
124-
{
125-
if (_jsModule is not null)
135+
catch (JSDisconnectedException)
126136
{
127-
try
128-
{
129-
await _jsModule.InvokeVoidAsync("destroy", [_id]);
130-
await _jsModule.DisposeAsync();
131-
}
132-
catch (TaskCanceledException)
133-
{
134-
}
135-
catch (JSDisconnectedException)
136-
{
137-
}
138137
}
139-
140138
}
141139

142-
public async Task Clear()
140+
}
141+
142+
public async Task Clear()
143+
{
144+
if (_jsModule is not null)
143145
{
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();
152151
}
153152
}
154153
}

0 commit comments

Comments
 (0)