-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMudHtmlEditor.razor.cs
More file actions
174 lines (140 loc) · 4.75 KB
/
MudHtmlEditor.razor.cs
File metadata and controls
174 lines (140 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Tizzani.MudBlazor.HtmlEditor;
public sealed partial class MudHtmlEditor : IAsyncDisposable
{
private readonly MudHtmlEditorOptions _options = new();
private DotNetObjectReference<MudHtmlEditor>? _dotNetRef;
private IJSObjectReference? _quill;
private ElementReference _toolbar;
private ElementReference _editor;
[Inject]
private IJSRuntime JS { get; set; } = default!;
[Parameter]
public RenderFragment? ChildContent { get; set; }
/// <summary>
/// Whether or not to ourline the editor. Default value is <see langword="true" />.
/// </summary>
[Parameter]
public bool Outlined { get; set; } = true;
/// <summary>
/// The placeholder text to display when the editor has no content.
/// </summary>
[Parameter]
public string Placeholder { get; set; } = "Tell your story...";
/// <summary>
/// The HTML markup from the editor.
/// </summary>
[Parameter]
public string Html { get; set; } = "";
/// <summary>
/// Raised when the <see cref="Html"/> property changes.
/// </summary>
[Parameter]
public EventCallback<string> HtmlChanged { get; set; }
/// <summary>
/// The plain-text content from the editor.
/// </summary>
[Parameter]
public string Text { get; set; } = "";
/// <summary>
/// Raised when the <see cref="Text"/> property changes.
/// </summary>
[Parameter]
public EventCallback<string> TextChanged { get; set; }
/// <summary>
/// When true, ol elements containing li elements with data-list="bullet" will be replaced with ul elements.
/// Default value is true. Set to false to revert to previous behavior.
/// </summary>
[Obsolete("This parameter was added to preserve backwards compatibility, but will be removed in a future version.")]
[Parameter]
public bool ReplaceOrderedWithUnorderedListTag
{
get => _options.SanitizeHtml;
set => _options.SanitizeHtml = value;
}
/// <summary>
/// Whether or not the user can resize the editor. Default value is <see langword="true" />.
/// </summary>
[Parameter]
public bool Resizable { get; set; } = true;
/// <summary>
/// Captures html attributes and applies them to the editor.
/// </summary>
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object?>? UserAttributes { get; set; }
/// <summary>
/// Clears the content of the editor.
/// </summary>
public async Task Reset()
{
await SetHtml(string.Empty);
}
/// <summary>
/// Sets the HTML content of the editor to the specified <paramref name="html"/>.
/// </summary>
public async Task SetHtml(string html)
{
if (_quill is not null)
await _quill.InvokeVoidAsync("setHtml", html);
HandleHtmlContentChanged(html);
HandleTextContentChanged(await GetText());
}
/// <summary>
/// Gets the current HTML content of the editor.
/// </summary>
public async Task<string> GetHtml()
{
if (_quill is not null)
return await _quill.InvokeAsync<string>("getHtml");
return "";
}
/// <summary>
/// Gets the current plain-text content of the editor.
/// </summary>
public async Task<string> GetText()
{
if (_quill is not null)
return await _quill.InvokeAsync<string>("getText");
return "";
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_dotNetRef = DotNetObjectReference.Create(this);
await using var module = await JS.InvokeAsync<IJSObjectReference>("import", "./_content/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js");
_quill = await module.InvokeAsync<IJSObjectReference>("createQuillInterop", _dotNetRef, _editor, _toolbar, Placeholder, _options);
await SetHtml(Html);
StateHasChanged();
}
}
[JSInvokable]
public async void HandleHtmlContentChanged(string html)
{
if (Html == html) return; // nothing changed
Html = html;
await HtmlChanged.InvokeAsync(html);
}
[JSInvokable]
public async void HandleTextContentChanged(string text)
{
if (Text == text) return; // nothing changed
Text = text;
await TextChanged.InvokeAsync(text);
}
async ValueTask IAsyncDisposable.DisposeAsync()
{
if (_quill is not null)
{
await _quill.DisposeAsync();
_quill = null;
}
_dotNetRef?.Dispose();
_dotNetRef = null;
}
}
public sealed class MudHtmlEditorOptions
{
public bool SanitizeHtml { get; set; } = true;
}