93 lines
2.9 KiB
C#
93 lines
2.9 KiB
C#
using Microsoft.JSInterop;
|
|
|
|
namespace WebApp.Services;
|
|
|
|
/// <summary>
|
|
/// Service for initializing paste-markdown functionality in MarkdownEditor components.
|
|
/// Enables automatic conversion of pasted spreadsheet tables (Google Sheets, Excel) to Markdown tables.
|
|
/// </summary>
|
|
public class MarkdownTablePasteService
|
|
{
|
|
private readonly IJSRuntime _jsRuntime;
|
|
private readonly ILogger<MarkdownTablePasteService> _logger;
|
|
|
|
public MarkdownTablePasteService(IJSRuntime jsRuntime, ILogger<MarkdownTablePasteService> logger)
|
|
{
|
|
_jsRuntime = jsRuntime;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes paste-markdown for a MarkdownEditor instance.
|
|
/// Should be called after the editor has been rendered and EasyMDE has initialized.
|
|
/// </summary>
|
|
/// <param name="editorId">Optional ID of the editor wrapper element. If null, will attempt to find the most recent editor.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
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");
|
|
}
|
|
}
|
|
|
|
public async Task<string?> GetValueAsync(string editorId)
|
|
{
|
|
try
|
|
{
|
|
return await _jsRuntime.InvokeAsync<string?>("markdownTablePaste.getValue", editorId);
|
|
}
|
|
catch (JSDisconnectedException)
|
|
{
|
|
return null;
|
|
}
|
|
catch (JSException ex)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to read markdown editor {EditorId}", editorId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> SetValueAsync(string editorId, string text)
|
|
{
|
|
try
|
|
{
|
|
return await _jsRuntime.InvokeAsync<bool>("markdownTablePaste.setValue", editorId, text);
|
|
}
|
|
catch (JSDisconnectedException)
|
|
{
|
|
return false;
|
|
}
|
|
catch (JSException ex)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to set markdown editor {EditorId}", editorId);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> InsertAtCursorAsync(string editorId, string text)
|
|
{
|
|
try
|
|
{
|
|
return await _jsRuntime.InvokeAsync<bool>("markdownTablePaste.insertAtCursor", editorId, text);
|
|
}
|
|
catch (JSDisconnectedException)
|
|
{
|
|
return false;
|
|
}
|
|
catch (JSException ex)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to insert into markdown editor {EditorId}", editorId);
|
|
return false;
|
|
}
|
|
}
|
|
}
|