From c4b83a83dce4f2d6f87312b948bcc41b960925be Mon Sep 17 00:00:00 2001 From: Steven Luiten Date: Fri, 3 Jan 2025 10:59:39 +0100 Subject: [PATCH] Add GetHTMLAsStream method. --- README.md | 1 + .../BlazorServerSide/BlazorServerSide.csproj | 2 +- samples/BlazorServerSide/Pages/Index.razor | 20 ++++++++++++++++++- samples/BlazorServerSide/_Imports.razor | 3 ++- .../Blazored.TextEditor.csproj | 6 +++++- .../BlazoredTextEditor.razor | 14 +++++++++++++ src/Blazored.TextEditor/Interop.cs | 13 ++++++++++++ .../wwwroot/Blazored-BlazorQuill.js | 3 +++ 8 files changed, 58 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4cf5f50..45fc5a4 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Below is a list of all the options available on the Text Editor. - `GetText` - Gets the content of the editor as Text. - `GetHTML` - Gets the content of the editor as HTML. +- `GetHTMLAsStream` - Gets the content of the editor as HTML stream (requires .NET 6.0 or later). - `GetContent` - Gets the content of the editor in the native Quill JSON Delta format. - `LoadContent` (`json`) - Allows the content of the editor to be programmatically set. - `LoadHTMLContent` (`string`) - Allows the content of the editor to be programmatically set. diff --git a/samples/BlazorServerSide/BlazorServerSide.csproj b/samples/BlazorServerSide/BlazorServerSide.csproj index aef4ac3..86eb6c5 100644 --- a/samples/BlazorServerSide/BlazorServerSide.csproj +++ b/samples/BlazorServerSide/BlazorServerSide.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + netcoreapp3.1;net6.0 diff --git a/samples/BlazorServerSide/Pages/Index.razor b/samples/BlazorServerSide/Pages/Index.razor index 25a8f74..fc9b0c1 100644 --- a/samples/BlazorServerSide/Pages/Index.razor +++ b/samples/BlazorServerSide/Pages/Index.razor @@ -27,6 +27,7 @@ + @@ -224,9 +225,26 @@ console.log("I love " ERROR: Maximum allowed size exceeded ({streamReference.Length} / {MaxAllowedSize})

"; + } + else + { + using var stream = await streamReference.OpenReadStreamAsync(MaxAllowedSize, cts.Token); + using var streamReader = new StreamReader(stream); + QuillHTMLContent = await streamReader.ReadToEndAsync().WaitAsync(cts.Token); + } +#else QuillHTMLContent = await this.QuillHtml.GetHTML(cts.Token); +#endif } - catch (TaskCanceledException e) + catch (TaskCanceledException) { QuillHTMLContent = "

ERROR: Timeout

"; } diff --git a/samples/BlazorServerSide/_Imports.razor b/samples/BlazorServerSide/_Imports.razor index 10faf57..90d56db 100644 --- a/samples/BlazorServerSide/_Imports.razor +++ b/samples/BlazorServerSide/_Imports.razor @@ -1,4 +1,5 @@ -@using System.Net.Http +@using System.IO +@using System.Net.Http @using System.Threading @using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Components.Authorization diff --git a/src/Blazored.TextEditor/Blazored.TextEditor.csproj b/src/Blazored.TextEditor/Blazored.TextEditor.csproj index 40eb716..7de083a 100644 --- a/src/Blazored.TextEditor/Blazored.TextEditor.csproj +++ b/src/Blazored.TextEditor/Blazored.TextEditor.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netstandard2.0;net6.0 3.0 Blazored.TextEditor @@ -34,6 +34,10 @@
+ + + + diff --git a/src/Blazored.TextEditor/BlazoredTextEditor.razor b/src/Blazored.TextEditor/BlazoredTextEditor.razor index 28c160d..94c6d6f 100644 --- a/src/Blazored.TextEditor/BlazoredTextEditor.razor +++ b/src/Blazored.TextEditor/BlazoredTextEditor.razor @@ -124,6 +124,20 @@ JSRuntime, QuillElement, cancellationToken); } +#if NET6_0_OR_GREATER + public async Task GetHTMLAsStream() + { + return await Interop.GetHTMLAsStream( + JSRuntime, QuillElement); + } + + public async Task GetHTMLAsStream(CancellationToken cancellationToken) + { + return await Interop.GetHTMLAsStream( + JSRuntime, QuillElement, cancellationToken); + } +#endif + public async Task GetContent() { return await Interop.GetContent( diff --git a/src/Blazored.TextEditor/Interop.cs b/src/Blazored.TextEditor/Interop.cs index dee8053..4bb3515 100644 --- a/src/Blazored.TextEditor/Interop.cs +++ b/src/Blazored.TextEditor/Interop.cs @@ -46,6 +46,19 @@ internal static ValueTask GetHTML( quillElement); } +#if NET6_0_OR_GREATER + internal static ValueTask GetHTMLAsStream( + IJSRuntime jsRuntime, + ElementReference quillElement, + CancellationToken cancellationToken = default) + { + return jsRuntime.InvokeAsync( + "QuillFunctions.getQuillEncodedHTML", + cancellationToken, + quillElement); + } +#endif + internal static ValueTask GetContent( IJSRuntime jsRuntime, ElementReference quillElement, diff --git a/src/Blazored.TextEditor/wwwroot/Blazored-BlazorQuill.js b/src/Blazored.TextEditor/wwwroot/Blazored-BlazorQuill.js index 7f36e7c..0a2a345 100644 --- a/src/Blazored.TextEditor/wwwroot/Blazored-BlazorQuill.js +++ b/src/Blazored.TextEditor/wwwroot/Blazored-BlazorQuill.js @@ -33,6 +33,9 @@ getQuillHTML: function(quillElement) { return quillElement.__quill.root.innerHTML; }, + getQuillEncodedHTML: function(quillElement) { + return new TextEncoder().encode(quillElement.__quill.root.innerHTML); + }, loadQuillContent: function(quillElement, quillContent) { content = JSON.parse(quillContent); return quillElement.__quill.setContents(content, 'api');