AI-Studio/app/MindWork AI Studio/Components/MudCopyClipboardButton.razor.cs
Thorsten Sommer 65a1683b95
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Fixed exports and the clipboard losing the sources of an answer (#966)
2026-09-13 19:37:15 +02:00

88 lines
2.8 KiB
C#

using AIStudio.Chat;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.Services;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
public partial class MudCopyClipboardButton : ComponentBase
{
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(MudCopyClipboardButton).Namespace, nameof(MudCopyClipboardButton));
/// <summary>
/// The string, if you want to copy a string.
/// </summary>
[Parameter]
public string StringContent { get; set; } = string.Empty;
/// <summary>
/// The content, if you want to copy content.
/// </summary>
[Parameter]
public IContent? Content { get; init; }
/// <summary>
/// The content type, if you want to copy Content.
/// </summary>
[Parameter]
public ContentType Type { get; init; } = ContentType.NONE;
/// <summary>
/// The tooltip that should be shown to the user.
/// </summary>
[Parameter]
public string TooltipMessage { get; set; } = TB("Copies the content to the clipboard");
/// <summary>
/// The size of the button. The default size is small.
/// </summary>
[Parameter]
public Size Size { get; set; } = Size.Small;
[Inject]
private RustService RustService { get; init; } = null!;
private async Task HandleCopyClick()
{
if (this.Type is ContentType.NONE)
await this.CopyToClipboard(this.StringContent);
else
await this.CopyToClipboard(this.Content);
}
/// <summary>
/// Copy this the string to the clipboard.
/// </summary>
private async Task CopyToClipboard(string textContent)
{
await this.RustService.CopyText2Clipboard(textContent);
}
/// <summary>
/// Copy this block's content to the clipboard.
/// </summary>
/// <remarks>
/// The user copies what the card shows, and the card shows the answer together with the sources
/// AI Studio collected for it. Pasting the answer into a mail without them would leave the
/// reader with claims nobody is able to check.
/// </remarks>
private async Task CopyToClipboard(IContent? contentToCopy)
{
if (contentToCopy is null)
return;
switch (this.Type)
{
case ContentType.TEXT when contentToCopy.TryGetExportMarkdown(out var markdown):
await this.RustService.CopyText2Clipboard(markdown);
break;
default:
// This component is no MSGComponentBase, so it uses the shared bus instance the same
// way FileExtensionValidation does:
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.ContentCopy, TB("Cannot copy this content type to clipboard.")));
break;
}
}
}