Files
2024-06-01 11:35:32 +02:00

89 lines
2.4 KiB
Plaintext

@using System
@using Microsoft.JSInterop;
@inject IJSRuntime JSRuntime
<div class="@mainDivClass">
@if (ShowText)
{
<input class="form-control" readonly type="text" value="@Text" />
}
else
{
<span class="form-control text-center fas fa-eye-slash fs-5" readonly></span>
}
<button type="button" class="btn @btnStyle" @onclick="CopyTextToClipboard" title="Copy to Clipboard">
@if (copyDone)
{
<span><i class="far fa-thumbs-up"></i></span>
}
else
{
<span><i class="far fa-copy"></i></span>
}
</button>
</div>
@code {
/// <summary>
/// Metodi per copia verso clipboard:
/// link:
/// - https://chrissainty.com/copy-to-clipboard-in-blazor/
/// - https://www.daveabrock.com/2021/02/18/copy-to-clipboard-markdown-editor/
/// - https://www.meziantou.net/
/// </summary>
[Parameter]
public string Text { get; set; } = "Text-To-Copy";
[Parameter]
public bool ShowText { get; set; } = true;
[Parameter]
public bool AddSlash { get; set; } = false;
[Parameter]
public bool IsSmall { get; set; } = true;
private string mainDivClass
{
get => IsSmall ? "input-group input-group-sm" : "input-group";
}
protected string btnStyle = "btn-primary";
protected bool copyDone = false;
private IJSObjectReference module { get; set; } = null!;
/// <summary>
/// Import modulo JS post rendering
/// </summary>
/// <param name="firstRender"></param>
/// <returns></returns>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
string jsPath = "./_content/EgwCoreLib.Razor/CopyToClipboard.razor.js";
if (AddSlash)
{
jsPath = "/." + jsPath;
}
module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", jsPath);
}
/// <summary>
/// Esecuzione copia a clipboard via JavaScript!
/// </summary>
/// <returns></returns>
private async Task CopyTextToClipboard()
{
// cambio colore..
btnStyle = "btn-success";
copyDone = true;
await JSRuntime.InvokeVoidAsync("clipboardCopy.copyText", Text);
await Task.Delay(1100);
// fix cambio colore..
btnStyle = "btn-primary";
copyDone = false;
await Task.Delay(10);
}
}