Skip to content

Commit 9d4c674

Browse files
committed
Expose delta contents
1 parent 6fd6a33 commit 9d4c674

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

samples/BlazorServer/Pages/EditorView.razor

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
<MudStack Justify="Justify.FlexEnd" Row="true">
1+
@using System.Text.Json.Nodes
2+
<MudStack Justify="Justify.FlexEnd" Row="true">
23
<MudButton OnClick="AddParagraph" Size="Size.Small">Add Paragraph</MudButton>
4+
<MudButton OnClick="ShowContents" Size="Size.Small">Show Contents</MudButton>
35
<MudButton OnClick="OnCancel" Size="Size.Small">Cancel</MudButton>
46
<MudButton OnClick="Reset" Size="Size.Small">Reset</MudButton>
57
<MudButton Color="Color.Primary" OnClick="SaveChanges" Size="Size.Small" StartIcon="@Icons.Material.Filled.Save" Variant="Variant.Filled">Save Changes</MudButton>
68
</MudStack>
79

810
<MudHtmlEditor @ref="_editor" @bind-Html="@_html" />
11+
<MudText>@(deltaContentsString)</MudText>
912

1013
@code {
1114
private MudHtmlEditor _editor = default!;
1215
private string _html = "";
16+
private string deltaContentsString = "";
1317

1418
[Parameter]
1519
public string InitialHtml { get; set; } = "";
@@ -32,11 +36,16 @@
3236

3337
private async Task SaveChanges()
3438
{
39+
deltaContentsString = _editor.Contents?.ToString() ?? string.Empty;
3540
await OnSave.InvokeAsync(_html);
3641
}
3742

3843
private async Task AddParagraph()
3944
{
4045
await _editor.SetHtml(_editor.Html + Environment.NewLine + "<p>New Paragraph</p>");
4146
}
47+
private void ShowContents()
48+
{
49+
deltaContentsString = _editor.Contents?.ToString() ?? string.Empty;
50+
}
4251
}

src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json.Nodes;
12
using Microsoft.AspNetCore.Components;
23
using Microsoft.JSInterop;
34

@@ -40,6 +41,18 @@ public sealed partial class MudHtmlEditor : IAsyncDisposable
4041
[Parameter]
4142
public EventCallback<string> HtmlChanged { get; set; }
4243

44+
/// <summary>
45+
/// The delta contents from the editor.
46+
/// </summary>
47+
[Parameter]
48+
public JsonObject Contents { get; set; } = new JsonObject();
49+
50+
/// <summary>
51+
/// Raised when the <see cref="Contents"/> property changes.
52+
/// </summary>
53+
[Parameter]
54+
public EventCallback<JsonObject> ContentsChanged { get; set; }
55+
4356
/// <summary>
4457
/// The plain-text content from the editor.
4558
/// </summary>
@@ -83,6 +96,7 @@ public async Task SetHtml(string html)
8396

8497
HandleHtmlContentChanged(html);
8598
HandleTextContentChanged(await GetText());
99+
HandleContentsChanged(await GetContents());
86100
}
87101

88102
/// <summary>
@@ -96,6 +110,17 @@ public async Task<string> GetHtml()
96110
return "";
97111
}
98112

113+
/// <summary>
114+
/// Gets the current delta contents of the editor.
115+
/// </summary>
116+
public async Task<JsonObject> GetContents()
117+
{
118+
if (_quill is not null)
119+
return await _quill.InvokeAsync<JsonObject>("getContents");
120+
121+
return new JsonObject();
122+
}
123+
99124
/// <summary>
100125
/// Gets the current plain-text content of the editor.
101126
/// </summary>
@@ -149,6 +174,15 @@ public async void HandleTextContentChanged(string text)
149174
await TextChanged.InvokeAsync(text);
150175
}
151176

177+
[JSInvokable]
178+
public async void HandleContentsChanged(JsonObject contents)
179+
{
180+
if (JsonObject.Equals(Contents, contents)) return; // nothing changed
181+
182+
Contents = contents;
183+
await ContentsChanged.InvokeAsync(contents);
184+
}
185+
152186
async ValueTask IAsyncDisposable.DisposeAsync()
153187
{
154188
if (_quill is not null)

src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export class MudQuillInterop {
5858
return this.quill.getText();
5959
};
6060

61+
getContents = () => {
62+
return this.quill.getContents();
63+
};
64+
6165
getHtml = () => {
6266
return this.quill.root.innerHTML;
6367
};
@@ -75,13 +79,14 @@ export class MudQuillInterop {
7579
};
7680

7781
/**
78-
*
82+
*
7983
* @param {Delta} delta
8084
* @param {Delta} oldDelta
8185
* @param {any} source
8286
*/
8387
textChangedHandler = (delta, oldDelta, source) => {
8488
this.dotNetRef.invokeMethodAsync('HandleHtmlContentChanged', this.getHtml());
8589
this.dotNetRef.invokeMethodAsync('HandleTextContentChanged', this.getText());
90+
this.dotNetRef.invokeMethodAsync('HandleContentsChanged', this.getContents());
8691
};
8792
}

src/Tizzani.MudBlazor.HtmlEditor/Tizzani.MudBlazor.HtmlEditor.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Title>$(AssemblyName)</Title>
8-
<Authors>erinnmclaughlin</Authors>
8+
<Authors>erinnmclaughlin, FeniXb3</Authors>
99
<RepositoryUrl>https://github.com/erinnmclaughlin/MudBlazor.HtmlEditor</RepositoryUrl>
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
12-
<Version>2.3.0</Version>
12+
<Version>2.3.1</Version>
1313
<Description>A customizable HTML editor component for MudBlazor, powered by QuillJS.</Description>
1414
<Copyright>2024 Erin McLaughlin</Copyright>
1515
<PackageProjectUrl>https://github.com/erinnmclaughlin/MudBlazor.HtmlEditor</PackageProjectUrl>

0 commit comments

Comments
 (0)