Skip to content

Commit 81165c0

Browse files
committed
Don't let Blazor Server do the clipboard management (Safari Fix)
1 parent c41e18e commit 81165c0

File tree

2 files changed

+6
-35
lines changed

2 files changed

+6
-35
lines changed
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
@inject NavigationManager NavigationManager
2-
@inject IJSRuntime JsRuntime
3-
@inject IToastService ToastService
42

53
<div class="flex-row" style="font-size: 1.5em">
64
<a class="text-decoration-none" id="share-linkedin" href="@LinkedInShare" target="_blank" rel="noreferrer"><i class="linkedin"></i></a>
7-
<a class="text-decoration-none" id="share-clipboard" href="javascript:void(0)" @onclick="CopyToClipboard"><i class="copy"></i></a>
5+
<a class="text-decoration-none" id="share-clipboard" href="javascript:void(0)" onclick="navigator.clipboard.writeText('@LinkedInShare')"><i class="copy"></i></a>
86
</div>
97
@code {
108
private string LinkedInShare => $"https://www.linkedin.com/shareArticle?mini=true&url={NavigationManager.Uri}";
11-
12-
private async Task CopyToClipboard()
13-
{
14-
try
15-
{
16-
await JsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", NavigationManager.Uri);
17-
ToastService.ShowSuccess("Copied link to clipboard");
18-
}
19-
catch
20-
{
21-
ToastService.ShowError("There was an error copying the link. Please copy the link from your address bar instead.");
22-
}
23-
}
249
}
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Linq;
33
using AngleSharp.Html.Dom;
4-
using Blazored.Toast.Services;
54
using LinkDotNet.Blog.Web.Features.ShowBlogPost.Components;
65
using Microsoft.Extensions.DependencyInjection;
76

@@ -12,38 +11,25 @@ public class ShareBlogPostTests : TestContext
1211
[Fact]
1312
public void ShouldCopyLinkToClipboard()
1413
{
15-
JSInterop.Mode = JSRuntimeMode.Loose;
16-
Services.AddScoped(_ => Substitute.For<IToastService>());
1714
Services.GetRequiredService<FakeNavigationManager>().NavigateTo("blogPost/1");
1815
var cut = RenderComponent<ShareBlogPost>();
1916

20-
cut.Find("#share-clipboard").Click();
17+
var element = cut.Find("#share-clipboard") as IHtmlAnchorElement;
2118

22-
var copyToClipboardInvocation = JSInterop.Invocations.SingleOrDefault(i => i.Identifier == "navigator.clipboard.writeText");
23-
copyToClipboardInvocation.Arguments[0].Should().Be("http://localhost/blogPost/1");
19+
element.Should().NotBeNull();
20+
var onclick = element!.Attributes.FirstOrDefault(a => a.Name.Equals("onclick", StringComparison.InvariantCultureIgnoreCase));
21+
onclick.Should().NotBeNull();
22+
onclick!.Value.Should().Contain("blogPost/1");
2423
}
2524

2625
[Fact]
2726
public void ShouldShareToLinkedIn()
2827
{
29-
Services.AddScoped(_ => Substitute.For<IToastService>());
3028
Services.GetRequiredService<FakeNavigationManager>().NavigateTo("blogPost/1");
3129

3230
var cut = RenderComponent<ShareBlogPost>();
3331

3432
var linkedInShare = (IHtmlAnchorElement)cut.Find("#share-linkedin");
3533
linkedInShare.Href.Should().Be("https://www.linkedin.com/shareArticle?mini=true&url=http://localhost/blogPost/1");
3634
}
37-
38-
[Fact]
39-
public void ShouldNotCrashWhenCopyingLinkNotWorking()
40-
{
41-
Services.AddScoped(_ => Substitute.For<IToastService>());
42-
JSInterop.SetupVoid(s => s.InvocationMethodName == "navigator.clipboard.writeText").SetException(new Exception());
43-
var cut = RenderComponent<ShareBlogPost>();
44-
45-
var act = () => cut.Find("#share-clipboard").Click();
46-
47-
act.Should().NotThrow<Exception>();
48-
}
4935
}

0 commit comments

Comments
 (0)