Skip to content

Commit 6fd6a33

Browse files
authored
Fix Quill init/disposal race (#31)
approach missing dom elements with exception add another safeguard
1 parent 7a4986c commit 6fd6a33

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,23 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
114114
_dotNetRef = DotNetObjectReference.Create(this);
115115

116116
await using var module = await JS.InvokeAsync<IJSObjectReference>("import", "./_content/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js");
117-
_quill = await module.InvokeAsync<IJSObjectReference>("createQuillInterop", _dotNetRef, _editor, _toolbar, Placeholder);
118-
119-
await SetHtml(Html);
120-
121-
StateHasChanged();
117+
try
118+
{
119+
_quill = await module.InvokeAsync<IJSObjectReference>("createQuillInterop", _dotNetRef, _editor, _toolbar, Placeholder);
120+
await SetHtml(Html);
121+
StateHasChanged();
122+
}
123+
catch (JSException)
124+
{
125+
// Creation failed due to missing DOM elements
126+
}
127+
catch (ObjectDisposedException)
128+
{
129+
// Component disposed before JS interop completed
130+
}
122131
}
123132
}
124133

125-
126134
[JSInvokable]
127135
public async void HandleHtmlContentChanged(string html)
128136
{

src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ if (typeof QuillBlotFormatter !== 'undefined') {
1717
}
1818

1919
export function createQuillInterop(dotNetRef, editorRef, toolbarRef, placeholder) {
20+
if (!editorRef || !editorRef.isConnected || !toolbarRef || !toolbarRef.isConnected) {
21+
throw new Error('DOM elements disconnected.');
22+
}
23+
2024
const modulesConfig = {
2125
toolbar: {
2226
container: toolbarRef

0 commit comments

Comments
 (0)