Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Blazored.TextEditor/Blazored.TextEditor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net9.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<RazorLangVersion>3.0</RazorLangVersion>
<RootNamespace>Blazored.TextEditor</RootNamespace>

Expand Down
81 changes: 79 additions & 2 deletions src/Blazored.TextEditor/BlazoredTextEditor.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@inject IJSRuntime JSRuntime
@using System.ComponentModel
@using System.Text.Json
@inject IJSRuntime JSRuntime

@if (!BottomToolbar)
{
Expand Down Expand Up @@ -44,6 +46,13 @@
public bool ReadOnly { get; set; }
= false;

/// <summary>
/// Pass in initial quill 'delta' content to be rendered.
/// Not required. Can be empty or null.
/// </summary>
//[Parameter]
//public string InitialContent { get; set; } = string.Empty;

[Parameter]
public string Placeholder { get; set; }
= "Compose an epic...";
Expand Down Expand Up @@ -94,8 +103,17 @@
[Parameter]
public bool Syntax { get; set; }

[Parameter]
public string Content { get; set; }

[Parameter]
public EventCallback<string> ContentChanged { get; set; }

private ElementReference QuillElement;
private ElementReference ToolBar;
private DotNetObjectReference<BlazoredTextEditor> _ObjectReference;

private bool IsRendered = false;

protected override async Task
OnAfterRenderAsync(bool firstRender)
Expand All @@ -111,9 +129,42 @@
Theme,
Formats,
DebugLevel,
Syntax
Syntax,
_ObjectReference

);

await LoadContent();
}
if (!IsRendered)
{
await LoadContent();
}
}

private string previousContent = string.Empty;

private async Task LoadContent()
{
if (!string.IsNullOrWhiteSpace(Content))
await LoadContent(Content);
previousContent = Content;
IsRendered = true;
}

protected override void OnParametersSet()
{
if (Content !=previousContent)
{
IsRendered = false;
}

}


protected override void OnInitialized()
{
_ObjectReference = DotNetObjectReference.Create(this);
}

public async Task<string> GetText()
Expand Down Expand Up @@ -222,4 +273,30 @@
JSRuntime, QuillElement, mode, cancellationToken);
}

private async Task GetQuill()
{
if (IsRendered)
{
Content = await GetContent();
await ContentChanged.InvokeAsync(Content);
}

}


[JSInvokable("DeltaChanged")]
[EditorBrowsable(EditorBrowsableState.Never)]
public async Task OnChange(JsonElement delta)
{
if (DebugLevel.Equals("info"))
{
Console.WriteLine($"Quill Editor Element: {EditorId} Invoked Change in Blazor");
Console.WriteLine(" Contents:" + delta.ToString());
}
await ContentChanged.InvokeAsync(delta.ToString());
}




}
5 changes: 3 additions & 2 deletions src/Blazored.TextEditor/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ internal static ValueTask<object> CreateQuill(
string theme,
string[] formats,
string debugLevel,
bool syntax)
bool syntax,
DotNetObjectReference<BlazoredTextEditor> reference)
{
return jsRuntime.InvokeAsync<object>(
"QuillFunctions.createQuill",
quillElement, toolbar, readOnly,
placeholder, theme, formats, debugLevel, syntax);
placeholder, theme, formats, debugLevel, syntax, reference);
}

internal static ValueTask<string> GetText(
Expand Down
20 changes: 19 additions & 1 deletion src/Blazored.TextEditor/wwwroot/Blazored-BlazorQuill.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
(function () {


window.QuillFunctions = {

dotNetRefs: new Map(),

createQuill: function (
quillElement, toolBar, readOnly,
placeholder, theme, formats, debugLevel, syntax) {
placeholder, theme, formats, debugLevel, syntax, dotNetObjectRef) {

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

Expand All @@ -23,6 +28,19 @@
}

quillElement.__quill = new Quill(quillElement, options);

QuillFunctions.dotNetRefs.set(quillElement.id, dotNetObjectRef);

//On Blur - we update the object in dotnet
quillElement.__quill.editor.scroll.domNode.addEventListener('blur',
() => {
if (quillElement.__quill.options.debug === "info") {
console.log('info: Quill Editor blur event for ' + quillElement.id);
}
QuillFunctions.dotNetRefs.get(quillElement.id).invokeMethodAsync('DeltaChanged', QuillFunctions.getQuillContent(quillElement));
}
);

},
getQuillContent: function(quillElement) {
return JSON.stringify(quillElement.__quill.getContents());
Expand Down