serdarciplak / BlazorMonaco

Blazor component for Microsoft's Monaco Editor which powers Visual Studio Code.
https://serdarciplak.github.io/BlazorMonaco/
MIT License
441 stars 99 forks source link

How can i add appendText to BlazorMonaco #63

Closed lulianqi closed 1 year ago

lulianqi commented 2 years ago

i find appendText in https://microsoft.github.io/monaco-editor/api/interfaces/monaco.languages.EnterAction.html#appendText and i want use appendText in BlazorMonaco

now i add these code in BlazorMonaco

//add  in MonacoEditor.razor
public async Task AppendText(string newValue)
    {
        if (jsRuntime == null)
            return;
        await jsRuntime.InvokeVoidAsync("blazorMonaco.editor.appendText", Id, newValue);
    }

//add in jsInterop.js
appendText: function (id, value) {
        let editor = this.getEditorById(id);
        editor.appendText(value);
    },

but it is fail that

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot read properties of undefined (reading 'appendText') TypeError: Cannot read properties of undefined (reading 'appendText') at Object.appendText (http://localhost:5000/_content/BlazorMonaco/jsInterop.js:566:22) at http://localhost:5000/_framework/blazor.webassembly.js:1:3942 at new Promise () at Object.beginInvokeJSFromDotNet (http://localhost:5000/_framework/blazor.webassembly.js:1:3908) at Object.w [as invokeJSFromDotNet] (http://localhost:5000/_framework/blazor.webassembly.js:1:64218) at _mono_wasm_invoke_js_blazor (http://localhost:5000/_framework/dotnet.5.0.17.js:1:190800) at do_icall (wasm://wasm/00aba242:wasm-function[10596]:0x194e4e) at do_icall_wrapper (wasm://wasm/00aba242:wasm-function[3305]:0x79df9) at interp_exec_method (wasm://wasm/00aba242:wasm-function[2155]:0x44ad3) at interp_runtime_invoke (wasm://wasm/00aba242:wasm-function[7862]:0x12efff) Microsoft.JSInterop.JSException: Cannot read properties of undefined (reading 'appendText') TypeError: Cannot read properties of undefined (reading 'appendText') at Object.appendText (http://localhost:5000/_content/BlazorMonaco/jsInterop.js:566:22) at http://localhost:5000/_framework/blazor.webassembly.js:1:3942 at new Promise () at Object.beginInvokeJSFromDotNet (http://localhost:5000/_framework/blazor.webassembly.js:1:3908) at Object.w [as invokeJSFromDotNet] (http://localhost:5000/_framework/blazor.webassembly.js:1:64218) at _mono_wasm_invoke_js_blazor (http://localhost:5000/_framework/dotnet.5.0.17.js:1:190800) at do_icall (wasm://wasm/00aba242:wasm-function[10596]:0x194e4e) at do_icall_wrapper (wasm://wasm/00aba242:wasm-function[3305]:0x79df9) at interp_exec_method (wasm://wasm/00aba242:wasm-function[2155]:0x44ad3) at interp_runtime_invoke (wasm://wasm/00aba242:wasm-function[7862]:0x12efff) at Microsoft.JSInterop.JSRuntime.d__15`1[[System.Object, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args) at BlazorMonaco.MonacoEditor.AppendText(String newValue) in C:\Users\hiscl\source\repos\BlazorMonaco-master\BlazorMonaco\MonacoEditor.razor:line 446 at SampleApp.Pages.Index.AppendTextAction() in C:\Users\hiscl\source\repos\BlazorMonaco-master\SampleApp\Pages\Index.razor:line 106 at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

how can i add appendText to BlazorMonaco

serdarciplak commented 1 year ago

If what you want to do is just appending text to the editor, the EnterAction.appendText property is not what you're looking for. The description for the EnterAction class says "Describes what to do when pressing Enter". You can use the GetValue() and SetValue() methods of the editor instance to append text to the editor.

lulianqi commented 1 year ago

If what you want to do is just appending text to the editor, the EnterAction.appendText property is not what you're looking for. The description for the EnterAction class says "Describes what to do when pressing Enter". You can use the GetValue() and SetValue() methods of the editor instance to append text to the editor.

thanks for your reply ! In blazor server SetValue() will receive whole text, and my whole text is so long (mybe 1Mb) ,so ican not use SetValue() ,it make my blazor app slow ,i want send the new text only (just like AppendText in StringBuilder)

image

serdarciplak commented 1 year ago

A Google search listed this. To sum up, you can use the executeEdits method with a range which contains the end of the current text.

lulianqi commented 1 year ago

A Google search listed this. To sum up, you can use the executeEdits method with a range which contains the end of the current text.

thanks a lot , executeEdits is useful for me.

await this.ExecuteEdits("insert-code", new List<IdentifiedSingleEditOperation>() { new IdentifiedSingleEditOperation() { Text = newValue, ForceMoveMarkers = true, Range = new BlazorMonaco.Range(1000000, 0, 1000000, newValue.Length) } }, new List<Selection>() { new Selection() });

it perform well in blazor server .