Skip to content

Commit 4daffc7

Browse files
Merge pull request #97 from gismofx/main
Allow initial loading of quill "delta" content via component parameter.
2 parents 0b8f189 + 69151b1 commit 4daffc7

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

src/Blazored.TextEditor/Blazored.TextEditor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net9.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0</TargetFrameworks>
55
<RazorLangVersion>3.0</RazorLangVersion>
66
<RootNamespace>Blazored.TextEditor</RootNamespace>
77

src/Blazored.TextEditor/BlazoredTextEditor.razor

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
@inject IJSRuntime JSRuntime
1+
@using System.ComponentModel
2+
@using System.Text.Json
3+
@inject IJSRuntime JSRuntime
24

35
@if (!BottomToolbar)
46
{
@@ -44,6 +46,13 @@
4446
public bool ReadOnly { get; set; }
4547
= false;
4648

49+
/// <summary>
50+
/// Pass in initial quill 'delta' content to be rendered.
51+
/// Not required. Can be empty or null.
52+
/// </summary>
53+
//[Parameter]
54+
//public string InitialContent { get; set; } = string.Empty;
55+
4756
[Parameter]
4857
public string Placeholder { get; set; }
4958
= "Compose an epic...";
@@ -94,8 +103,17 @@
94103
[Parameter]
95104
public bool Syntax { get; set; }
96105

106+
[Parameter]
107+
public string Content { get; set; }
108+
109+
[Parameter]
110+
public EventCallback<string> ContentChanged { get; set; }
111+
97112
private ElementReference QuillElement;
98113
private ElementReference ToolBar;
114+
private DotNetObjectReference<BlazoredTextEditor> _ObjectReference;
115+
116+
private bool IsRendered = false;
99117

100118
protected override async Task
101119
OnAfterRenderAsync(bool firstRender)
@@ -111,9 +129,42 @@
111129
Theme,
112130
Formats,
113131
DebugLevel,
114-
Syntax
132+
Syntax,
133+
_ObjectReference
134+
115135
);
136+
137+
await LoadContent();
116138
}
139+
if (!IsRendered)
140+
{
141+
await LoadContent();
142+
}
143+
}
144+
145+
private string previousContent = string.Empty;
146+
147+
private async Task LoadContent()
148+
{
149+
if (!string.IsNullOrWhiteSpace(Content))
150+
await LoadContent(Content);
151+
previousContent = Content;
152+
IsRendered = true;
153+
}
154+
155+
protected override void OnParametersSet()
156+
{
157+
if (Content !=previousContent)
158+
{
159+
IsRendered = false;
160+
}
161+
162+
}
163+
164+
165+
protected override void OnInitialized()
166+
{
167+
_ObjectReference = DotNetObjectReference.Create(this);
117168
}
118169

119170
public async Task<string> GetText()
@@ -222,4 +273,30 @@
222273
JSRuntime, QuillElement, mode, cancellationToken);
223274
}
224275

276+
private async Task GetQuill()
277+
{
278+
if (IsRendered)
279+
{
280+
Content = await GetContent();
281+
await ContentChanged.InvokeAsync(Content);
282+
}
283+
284+
}
285+
286+
287+
[JSInvokable("DeltaChanged")]
288+
[EditorBrowsable(EditorBrowsableState.Never)]
289+
public async Task OnChange(JsonElement delta)
290+
{
291+
if (DebugLevel.Equals("info"))
292+
{
293+
Console.WriteLine($"Quill Editor Element: {EditorId} Invoked Change in Blazor");
294+
Console.WriteLine(" Contents:" + delta.ToString());
295+
}
296+
await ContentChanged.InvokeAsync(delta.ToString());
297+
}
298+
299+
300+
301+
225302
}

src/Blazored.TextEditor/Interop.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ internal static ValueTask<object> CreateQuill(
1616
string theme,
1717
string[] formats,
1818
string debugLevel,
19-
bool syntax)
19+
bool syntax,
20+
DotNetObjectReference<BlazoredTextEditor> reference)
2021
{
2122
return jsRuntime.InvokeAsync<object>(
2223
"QuillFunctions.createQuill",
2324
quillElement, toolbar, readOnly,
24-
placeholder, theme, formats, debugLevel, syntax);
25+
placeholder, theme, formats, debugLevel, syntax, reference);
2526
}
2627

2728
internal static ValueTask<string> GetText(

src/Blazored.TextEditor/wwwroot/Blazored-BlazorQuill.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
(function () {
2+
3+
24
window.QuillFunctions = {
5+
6+
dotNetRefs: new Map(),
7+
38
createQuill: function (
49
quillElement, toolBar, readOnly,
5-
placeholder, theme, formats, debugLevel, syntax) {
10+
placeholder, theme, formats, debugLevel, syntax, dotNetObjectRef) {
611

712
Quill.register('modules/blotFormatter', QuillBlotFormatter.default);
813

@@ -23,6 +28,19 @@
2328
}
2429

2530
quillElement.__quill = new Quill(quillElement, options);
31+
32+
QuillFunctions.dotNetRefs.set(quillElement.id, dotNetObjectRef);
33+
34+
//On Blur - we update the object in dotnet
35+
quillElement.__quill.editor.scroll.domNode.addEventListener('blur',
36+
() => {
37+
if (quillElement.__quill.options.debug === "info") {
38+
console.log('info: Quill Editor blur event for ' + quillElement.id);
39+
}
40+
QuillFunctions.dotNetRefs.get(quillElement.id).invokeMethodAsync('DeltaChanged', QuillFunctions.getQuillContent(quillElement));
41+
}
42+
);
43+
2644
},
2745
getQuillContent: function(quillElement) {
2846
return JSON.stringify(quillElement.__quill.getContents());

0 commit comments

Comments
 (0)