-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMudHtmlEditor.razor.cs
More file actions
149 lines (113 loc) · 3.97 KB
/
MudHtmlEditor.razor.cs
File metadata and controls
149 lines (113 loc) · 3.97 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
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Tizzani.MudBlazor.HtmlEditor;
public sealed partial class MudHtmlEditor : IAsyncDisposable
{
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; }
[Parameter]
public bool Outlined { get; set; } = true;
[Parameter]
public string Placeholder { get; set; } = "Tell your story...";
[Parameter]
public string Html { get; set; } = "";
[Parameter]
public EventCallback<string> HtmlChanged { get; set; }
[Parameter]
public string Text { get; set; } = "";
[Parameter]
public EventCallback<string> TextChanged { get; set; }
[Parameter]
public Func<string, string, Stream, Task<string>>? ImageUploadHandler { get; set; }
[Parameter]
public string? ImageUploadUrl { get; set; }
[Parameter]
public IEnumerable<string> AllowedImageMimeTypes { get; set; } = new List<string>() { "image/png", "image/jpeg" };
[Parameter]
public bool Resizable { get; set; } = true;
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object?>? UserAttributes { get; set; }
public async Task Reset()
{
await SetHtml(string.Empty);
}
public async Task SetHtml(string html)
{
if (_quill is not null)
await _quill.InvokeVoidAsync("setHtml", html);
HandleHtmlContentChanged(html);
HandleTextContentChanged(await GetText());
}
public async Task<string> GetHtml()
{
if (_quill is not null)
return await _quill.InvokeAsync<string>("getHtml");
return "";
}
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");
var settings = new
{
Placeholder = Placeholder,
BlazorImageUpload = ImageUploadHandler != null,
ImageUploadUrl = ImageUploadUrl ?? "",
AllowedImageMimeTypes = AllowedImageMimeTypes
};
_quill = await module.InvokeAsync<IJSObjectReference>("createQuillInterop", _dotNetRef, _editor, _toolbar, settings);
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);
}
[JSInvokable]
public async Task<string> SaveImage(string imageName, string fileType, long size)
{
if(_quill is not null)
{
var imageJsStream = await _quill.InvokeAsync<IJSStreamReference>("getImageBytes", imageName);
await using var imageStream = await imageJsStream.OpenReadStreamAsync(size);
if (ImageUploadHandler is not null)
return await ImageUploadHandler(imageName, fileType, imageStream);
}
return "";
}
async ValueTask IAsyncDisposable.DisposeAsync()
{
if (_quill is not null)
{
await _quill.DisposeAsync();
_quill = null;
}
_dotNetRef?.Dispose();
_dotNetRef = null;
}
}