Blazored / TextEditor

Rich text editor for Blazor applications - Uses Quill JS
MIT License
277 stars 61 forks source link

fixed Cannot read property 'root' of undefined @ OnAfterRenderAsync #26

Closed git-net closed 4 years ago

git-net commented 4 years ago

i found sometimes QuillFunctions.loadQuillHTMLContent called before createQuill,then throw error Cannot read property 'root' of undefined. i try add parameter @ BlazoredTextEditor.razor [Parameter] public string HtmlContent { get; set; }

and change OnAfterRenderAsync @ BlazoredTextEditor.razor

if (firstRender)
        {
            await Interop.CreateQuill(
                JSRuntime,
                QuillElement,
                ToolBar,
                ReadOnly,
                Placeholder,
                Theme,
                DebugLevel);
            if (HtmlContent != null)
            {
               await this.LoadHTMLContent(HtmlContent);
            }
        }

@ myComp.razor

 <Blazored.TextEditor.BlazoredTextEditor
                                         HtmlContent="@xxx.Content"                               
                                                                        @ref="richText">...

its works fine。

ADefWebserver commented 4 years ago

What version of .Net Core are you using? Do you get this error when you run the sample code in this GitHub repo?

Thank You

ADefWebserver commented 4 years ago

I tested this in both .Net Core 3.1 and .Net Core 5.0 and I can't reproduce this error. Can I see the code that sets HtmlContent . From what I can tell this just should not be needed. Thanks!

git-net commented 4 years ago

netcore 3.1. e,my english is poor. i means i resolved "Cannot read property 'root' of undefined." by add HtmlContent ,

before my.razor

....
<BlazoredTextEditor....  @ref="textEditor" />
....
protected override async Task OnAfterRenderAsync(bool firstrender) {
   .....
  await textEditor.LoadHTMLContent(....) // throw 
}

after: add HtmlContent and remove textEditor.LoadHTMLContent(....)

<BlazoredTextEditor.... @ref="textEditor" HtmlContent="sssss" />

git-net commented 4 years ago

when i debug in browser i found 'loadQuillHTMLContent called before createQuill' sometimes

ADefWebserver commented 4 years ago

If you are trying to load HTML content when the page first loads use:

    <EditorContent>
        @((MarkupString)@QuillReadOnlyContent)
    </EditorContent>

See: https://github.com/Blazored/TextEditor/blob/main/samples/BlazorServerSide/Pages/Index.razor

Only use:

await this.QuillHtml.LoadHTMLContent(QuillContent);

On an event like a button click.

git-net commented 4 years ago

Thanks!!