Skip to content

Commit 263d128

Browse files
authored
feat: get text from quill (#16)
1 parent 83dba24 commit 263d128

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public sealed partial class MudHtmlEditor : IAsyncDisposable
2828
[Parameter]
2929
public EventCallback<string> HtmlChanged { get; set; }
3030

31+
[Parameter]
32+
public string Text { get; set; } = "";
33+
34+
[Parameter]
35+
public EventCallback<string> TextChanged { get; set; }
36+
3137
[Parameter]
3238
public bool Resizable { get; set; } = true;
3339

@@ -44,10 +50,22 @@ public async Task SetHtml(string html)
4450
if (_quill is not null)
4551
await _quill.InvokeVoidAsync("setHtml", html);
4652

47-
if (HtmlChanged.HasDelegate)
48-
await HtmlChanged.InvokeAsync(html);
53+
HandleHtmlContentChanged(html);
54+
HandleTextContentChanged(await GetText());
55+
}
4956

50-
Html = html;
57+
public async Task<string> GetHtml()
58+
{
59+
if (_quill is not null)
60+
return await _quill.InvokeAsync<string>("getHtml");
61+
return "";
62+
}
63+
64+
public async Task<string> GetText()
65+
{
66+
if (_quill is not null)
67+
return await _quill.InvokeAsync<string>("getText");
68+
return "";
5169
}
5270

5371
protected override async Task OnAfterRenderAsync(bool firstRender)
@@ -74,6 +92,15 @@ public async void HandleHtmlContentChanged(string html)
7492
await HtmlChanged.InvokeAsync(html);
7593
}
7694

95+
[JSInvokable]
96+
public async void HandleTextContentChanged(string text)
97+
{
98+
if (Text == text) return; // nothing changed
99+
100+
Text = text;
101+
await TextChanged.InvokeAsync(text);
102+
}
103+
77104
async ValueTask IAsyncDisposable.DisposeAsync()
78105
{
79106
if (_quill is not null)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export class MudQuillInterop {
4545
this.toolbarRef = toolbarRef;
4646
}
4747

48+
getText = () => {
49+
return this.quill.getText();
50+
};
51+
4852
getHtml = () => {
4953
return this.quill.root.innerHTML;
5054
};
@@ -69,5 +73,6 @@ export class MudQuillInterop {
6973
*/
7074
textChangedHandler = (delta, oldDelta, source) => {
7175
this.dotNetRef.invokeMethodAsync('HandleHtmlContentChanged', this.getHtml());
76+
this.dotNetRef.invokeMethodAsync('HandleTextContentChanged', this.getText());
7277
};
7378
}

0 commit comments

Comments
 (0)