Skip to content

Commit

Permalink
Implement UploadComplete event in RadzenHtmlEditor.
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchev committed Sep 12, 2024
1 parent 3ba3610 commit 6a8611c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
36 changes: 36 additions & 0 deletions Radzen.Blazor/RadzenHtmlEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,5 +416,41 @@ public override void Dispose()
JSRuntime.InvokeVoidAsync("Radzen.destroyEditor", ContentEditable);
}
}

/// <summary>
/// Gets or sets the callback which when a file is uploaded.
/// </summary>
/// <value>The complete callback.</value>
[Parameter]
public EventCallback<UploadCompleteEventArgs> UploadComplete { get; set; }


internal async Task RaiseUploadComplete(UploadCompleteEventArgs args)
{
await UploadComplete.InvokeAsync(args);
}

/// <summary>
/// Invoked by interop when the upload is complete.
/// </summary>
[JSInvokable("OnUploadComplete")]
public async Task OnUploadComplete(string response)
{
System.Text.Json.JsonDocument doc = null;

if (!string.IsNullOrEmpty(response))
{
try
{
doc = System.Text.Json.JsonDocument.Parse(response);
}
catch (System.Text.Json.JsonException)
{
//
}
}

await UploadComplete.InvokeAsync(new UploadCompleteEventArgs() { RawResponse = response, JsonResponse = doc });
}
}
}
2 changes: 2 additions & 0 deletions Radzen.Blazor/RadzenHtmlEditorImage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ async Task OnUploadComplete(UploadCompleteEventArgs args)
{
DialogService.Close(true);
}

await Editor.RaiseUploadComplete(args);
}

async Task OnUploadError(UploadErrorEventArgs args)
Expand Down
1 change: 1 addition & 0 deletions Radzen.Blazor/wwwroot/Radzen.Blazor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,7 @@ window.Radzen = {
} else {
document.execCommand("insertHTML", false, '<img src="' + result.url + '">');
}
instance.invokeMethodAsync('OnUploadComplete', xhr.responseText);
} else {
instance.invokeMethodAsync('OnError', xhr.responseText);
}
Expand Down
7 changes: 6 additions & 1 deletion RadzenBlazorDemos/Pages/HtmlEditorGetSetValue.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<RadzenHtmlEditor @bind-Value=@htmlValue style="height: 450px;" Input=@OnInput Change=@OnChange Paste=@OnPaste Execute=@OnExecute UploadUrl="upload/image" />
<RadzenHtmlEditor @bind-Value=@htmlValue style="height: 450px;" Input=@OnInput Change=@OnChange Paste=@OnPaste UploadComplete=@OnUploadComplete Execute=@OnExecute UploadUrl="upload/image" />

<EventConsole @ref=@console />

Expand Down Expand Up @@ -34,4 +34,9 @@
{
console.Log($"Execute: {args.CommandName}");
}

void OnUploadComplete(UploadCompleteEventArgs args)
{
console.Log($"Upload complete: {args.RawResponse}");
}
}

0 comments on commit 6a8611c

Please sign in to comment.