using Microsoft.JSInterop;
namespace WebApp.Services;
///
/// Service for initializing paste-markdown functionality in MarkdownEditor components.
/// Enables automatic conversion of pasted spreadsheet tables (Google Sheets, Excel) to Markdown tables.
///
public class MarkdownTablePasteService
{
private readonly IJSRuntime _jsRuntime;
private readonly ILogger _logger;
public MarkdownTablePasteService(IJSRuntime jsRuntime, ILogger logger)
{
_jsRuntime = jsRuntime;
_logger = logger;
}
///
/// Initializes paste-markdown for a MarkdownEditor instance.
/// Should be called after the editor has been rendered and EasyMDE has initialized.
///
/// Optional ID of the editor wrapper element. If null, will attempt to find the most recent editor.
/// A task that represents the asynchronous operation.
public async Task InitializeAsync(string? editorId = null)
{
try
{
await _jsRuntime.InvokeVoidAsync("markdownTablePaste.initialize", editorId);
}
catch (JSException ex)
{
_logger.LogWarning(ex, "Failed to initialize paste-markdown for editor {EditorId}", editorId ?? "unknown");
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected error initializing paste-markdown for editor {EditorId}", editorId ?? "unknown");
}
}
}