Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code editor is not disposed properly #136

Open
FLAMESpl opened this issue Jul 14, 2024 · 1 comment
Open

Code editor is not disposed properly #136

FLAMESpl opened this issue Jul 14, 2024 · 1 comment

Comments

@FLAMESpl
Copy link

FLAMESpl commented Jul 14, 2024

Consider this file:

@page "/"
@using BlazorMonaco.Editor

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

<input @bind="content" />

<button @onclick="() => visible = !visible">Toggle</button>

@if (visible)
{
    <p>Visible!</p>
    <StandaloneCodeEditor Id="main-editor" CssClass="editor" ConstructionOptions="GetConstructionOptions" />
}

Welcome to your new app.

@code
{
    private string content = "{}";
    private bool visible = false;

    public StandaloneEditorConstructionOptions GetConstructionOptions(StandaloneCodeEditor editor) => new()
    {
        Language = "json",
        Value = content
    };

When button is pressed it toggles visibility of StandaloneCodeEditor. It either is added or removed from the DOM. When displayed for the first time, it correctly obtains value from input, but after every consecutive toggle value is not updated and remains the same in the editor.

EDIT:

I have made some digging and discovered this snippet when editor is created:

        if (oldEditor !== null) {
            options.value = oldEditor.getValue();
            window.blazorMonaco.editors.splice(window.blazorMonaco.editors.findIndex(item => item.id === id), 1);
            oldEditor.dispose();
        }

I have no idea why it is overriding new value in options with value from previous instance, I don't think of any scenario where it would be a desired behaviour.

I have managed to fix this issue by inserting this code to my App.razor:

<script>
    const blazorMonaco = window.blazorMonaco;
    const innerFunction = blazorMonaco.editor.create;

    blazorMonaco.editor.create = function(id, options, override, dotnetRef) {

        const oldEditor = blazorMonaco.editor.getEditor(id, true);

        if (oldEditor !== null) {
            blazorMonaco.editors.splice(blazorMonaco.editors.findIndex(item => item.id === id), 1);
            oldEditor.dispose();
        }

        innerFunction.call(this, id, options, override, dotnetRef);
    };
</script>
@FLAMESpl
Copy link
Author

@serdarciplak I have created a PR to address this, can you take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant