Skip to content

Commit 4e2a223

Browse files
committed
refactor: Abstract clipboard operations into a dedicated service and integrate it into relevant tools.
1 parent 02f6914 commit 4e2a223

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/BlazorApps/Pages/Tools/Base64Converter.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@page "/tools/base64"
22
@using System.Text
3-
@inject IJSRuntime JSRuntime
3+
@using BlazorApps.Services
4+
@inject IClipboardService ClipboardService
45

56
<PageTitle>Base64 Encoder and Decoder</PageTitle>
67

@@ -84,7 +85,7 @@
8485
{
8586
if (!string.IsNullOrEmpty(outputText))
8687
{
87-
await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", outputText);
88+
await ClipboardService.WriteTextAsync(outputText);
8889
}
8990
}
9091
}

src/BlazorApps/Pages/Tools/GuidGenerator.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@page "/tools/guid"
22
@using System.Text
3-
@inject IJSRuntime JSRuntime
3+
@using BlazorApps.Services
4+
@inject IClipboardService ClipboardService
45

56
<PageTitle>GUID Generator</PageTitle>
67

@@ -92,7 +93,7 @@
9293
{
9394
if (!string.IsNullOrEmpty(FormattedOutput))
9495
{
95-
await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", FormattedOutput);
96+
await ClipboardService.WriteTextAsync(FormattedOutput);
9697
// Optional: Provide feedback to the user (e.g., toast notification) if desired,
9798
// but requirements just asked for the button.
9899
}

src/BlazorApps/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
33
using Microsoft.FluentUI.AspNetCore.Components;
44
using BlazorApps;
5+
using BlazorApps.Services;
56

67
var builder = WebAssemblyHostBuilder.CreateDefault(args);
78
builder.RootComponents.Add<App>("#app");
@@ -15,4 +16,5 @@ static void ConfigureServices(IServiceCollection services, string baseAddress)
1516
{
1617
services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseAddress) });
1718
services.AddFluentUIComponents();
19+
services.AddScoped<IClipboardService, ClipboardService>();
1820
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.JSInterop;
2+
3+
namespace BlazorApps.Services;
4+
5+
public class ClipboardService : IClipboardService
6+
{
7+
private readonly IJSRuntime _jsRuntime;
8+
9+
public ClipboardService(IJSRuntime jsRuntime)
10+
{
11+
_jsRuntime = jsRuntime;
12+
}
13+
14+
public async Task WriteTextAsync(string text)
15+
{
16+
await _jsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", text);
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BlazorApps.Services;
2+
3+
public interface IClipboardService
4+
{
5+
Task WriteTextAsync(string text);
6+
}

0 commit comments

Comments
 (0)