Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetHTMLAsStream method. #85

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorServerSide/BlazorServerSide.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
20 changes: 19 additions & 1 deletion samples/BlazorServerSide/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</span>
<span class="ql-formats">
<button class="ql-link"></button>
<button class="ql-image"></button>
</span>
</ToolbarContent>
<EditorContent>
Expand Down Expand Up @@ -224,9 +225,26 @@ console.<span class="hljs-built_in">log</span>("I love " <span class="hljs-opera

try
{
#if NET6_0_OR_GREATER
await using var streamReference = await this.QuillHtml.GetHTMLAsStream(cts.Token);

const long MaxAllowedSize = 5_000_000; // 5MB

if (streamReference.Length > MaxAllowedSize)
{
QuillHTMLContent = $"<p>ERROR: Maximum allowed size exceeded ({streamReference.Length} / {MaxAllowedSize})</p>";
}
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 = "<p>ERROR: Timeout</p>";
}
Expand Down
3 changes: 2 additions & 1 deletion samples/BlazorServerSide/_Imports.razor
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Blazored.TextEditor/Blazored.TextEditor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<RazorLangVersion>3.0</RazorLangVersion>
<RootNamespace>Blazored.TextEditor</RootNamespace>

Expand Down Expand Up @@ -34,6 +34,10 @@
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.JSInterop" Version="6.0.36" />
</ItemGroup>

<ItemGroup>
<None Include="wwwroot\Blazored-BlazorQuill.js" />
Expand Down
14 changes: 14 additions & 0 deletions src/Blazored.TextEditor/BlazoredTextEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@
JSRuntime, QuillElement, cancellationToken);
}

#if NET6_0_OR_GREATER
public async Task<IJSStreamReference> GetHTMLAsStream()
{
return await Interop.GetHTMLAsStream(
JSRuntime, QuillElement);
}

public async Task<IJSStreamReference> GetHTMLAsStream(CancellationToken cancellationToken)
{
return await Interop.GetHTMLAsStream(
JSRuntime, QuillElement, cancellationToken);
}
#endif

public async Task<string> GetContent()
{
return await Interop.GetContent(
Expand Down
13 changes: 13 additions & 0 deletions src/Blazored.TextEditor/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ internal static ValueTask<string> GetHTML(
quillElement);
}

#if NET6_0_OR_GREATER
internal static ValueTask<IJSStreamReference> GetHTMLAsStream(
IJSRuntime jsRuntime,
ElementReference quillElement,
CancellationToken cancellationToken = default)
{
return jsRuntime.InvokeAsync<IJSStreamReference>(
"QuillFunctions.getQuillEncodedHTML",
cancellationToken,
quillElement);
}
#endif

internal static ValueTask<string> GetContent(
IJSRuntime jsRuntime,
ElementReference quillElement,
Expand Down
3 changes: 3 additions & 0 deletions src/Blazored.TextEditor/wwwroot/Blazored-BlazorQuill.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading