Skip to content

Commit

Permalink
provide a custom clipboard service (#224)
Browse files Browse the repository at this point in the history
Co-authored-by: kuzmins-85 <[email protected]>
  • Loading branch information
MihailsKuzmins and kuzmins-85 authored Dec 11, 2023
1 parent a2b89c9 commit ecd3d8d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddMudServices();
services.AddMudMarkdownServices();
// Optionally if default clipboard functionality fails it is possible to add a custom service
// NB! MauiClipboardService is just an example
builder.Services.AddMudMarkdownClipboardService<MauiClipboardService>();
}
```
For the Blazor WebAssembly in the `Program.cs` add this method.
```cs
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddMudServices();
builder.Services.AddMudMarkdownServices();
// Optionally if default clipboard functionality fails it is possible to add a custom service
// NB! MauiClipboardService is just an example
builder.Services.AddMudMarkdownClipboardService<MauiClipboardService>();
```
## Using the component
```razor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion samples/Server/MudBlazor.Markdown.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ReactiveUI.Blazor" Version="19.4.1" />
<PackageReference Include="ReactiveUI.Blazor" Version="19.5.1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions samples/WebAssembly/MudBlazor.Markdown.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.10" PrivateAssets="all" />
<PackageReference Include="ReactiveUI.Blazor" Version="19.4.1" />
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="ReactiveUI.Blazor" Version="19.5.1" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 12 additions & 1 deletion src/MudBlazor.Markdown/Components/MudCodeHighlight.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,18 @@ await Js.InvokeVoidAsync("setHighlightStylesheet", stylesheetPath)

private async Task CopyTextToClipboardAsync(MouseEventArgs args)
{
await Js.InvokeVoidAsync("navigator.clipboard.writeText", Text)
var ok = await Js.InvokeAsync<bool>("copyTextToClipboard", Text)
.ConfigureAwait(false);

if (ok)
return;

var clipboardService = ServiceProvider?.GetService<IMudMarkdownClipboardService>();

if (clipboardService != null)
{
await clipboardService.CopyToClipboardAsync(Text)
.ConfigureAwait(false);
}
}
}
2 changes: 1 addition & 1 deletion src/MudBlazor.Markdown/MudBlazor.Markdown.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MudBlazor</RootNamespace>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<Authors>MyNihongo</Authors>
<Description>Markdown component for MudBlazor (https://mudblazor.com/)</Description>
<Copyright>Copyright © 2023 MyNihongo</Copyright>
Expand Down
9 changes: 9 additions & 0 deletions src/MudBlazor.Markdown/Resources/MudBlazor.Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@ window.refreshMathJaxScript = function () {
// swallow since in some cases MathJax might not be initialized
}
}

window.copyTextToClipboard = async function (text) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (e) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MudBlazor;

public interface IMudMarkdownClipboardService
{
ValueTask CopyToClipboardAsync(string text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ public static class ServiceCollectionEx
{
public static IServiceCollection AddMudMarkdownServices(this IServiceCollection @this)
{
@this.AddScoped<IMudMarkdownThemeService, MudMarkdownThemeService>();
return @this.AddScoped<IMudMarkdownThemeService, MudMarkdownThemeService>();
}

return @this;
public static IServiceCollection AddMudMarkdownClipboardService<T>(this IServiceCollection @this)
where T : class, IMudMarkdownClipboardService
{
return @this.AddScoped<IMudMarkdownClipboardService, T>();
}
}
}

0 comments on commit ecd3d8d

Please sign in to comment.