BlazorMonaco
repository·master·Indexed 20 days ago
https://github.com/serdarciplak/blazormonacoA Blazor component wrapper for the Microsoft Monaco Editor, providing rich code editing capabilities. It includes components such as StandaloneCodeEditor and StandaloneDiffEditor, support for editor events, and access to global Monaco methods via C#. The library requires an interactive render mode and explicit CSS height configuration to function correctly.
What's inside BlazorMonaco
- Event callback methods no longer include a parameter for the source editor instance that raised the event. This change was made to match the original Monaco Editor implementation.
Use StandaloneCodeEditor and StandaloneDiffEditor
masterBlazorMonaco provides two primary components for rendering editors:
<StandaloneCodeEditor />: A standard code editor.<StandaloneDiffEditor />: An editor used for comparing two versions of code.
By default, these components render with default options.
<StandaloneCodeEditor /> <!-- or --> <StandaloneDiffEditor />Migrate from v2.x to v3.x
masterWhen upgrading from version 2.x to 3.x, you must update your HTML configuration, namespaces, and component usage to align with the updated Monaco Editor version and the library's new structure.
<!-- Update script tags in your index.html body --> <script src="_content/BlazorMonaco/jsInterop.js"></script> <script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/loader.js"></script> <script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.js"></script>Migrate from v1.x to v2.x
masterWhen upgrading from version 1.x to 2.x, note the following breaking changes:
- The
BlazorMonaco.Bridgenamespace was merged intoBlazorMonaco. - Several options classes were renamed with an
Editorprefix:CommentOptions$\rightarrow$EditorCommentsOptionsFindOptions$\rightarrow$EditorFindOptionsHoverOptions$\rightarrow$EditorHoverOptionsLightbulbOptions$\rightarrow$EditorLightbulbOptionsMinimapOptions$\rightarrow$EditorMinimapOptionsParameterHintOptions$\rightarrow$EditorParameterHintOptionsScrollbarOptions$\rightarrow$EditorScrollbarOptions
EditorLayoutInfohas been restructured.- Certain options and properties were removed if they were no longer supported by the underlying Monaco Editor.
- The
Install BlazorMonaco
masterTo use BlazorMonaco in your Blazor project, follow these three steps:
Add the NuGet package:
dotnet add package BlazorMonacoAdd required script tags: Add these tags to the end of your
htmlbody tag. They must be placed before your Blazor script tag (blazor.webassembly.js,blazor.server.js, orblazor.web.js).Add using directives: Add the following to your
_Imports.razorfile to access the necessary namespaces:@using BlazorMonaco @using BlazorMonaco.Editor @using BlazorMonaco.Languages
<script src="_content/BlazorMonaco/jsInterop.js"></script> <script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/loader.js"></script> <script src="_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.js"></script>Customize editor styling with CSS
masterBlazorMonaco wraps editor instances in a
divwith specific classes and IDs that you can target with CSS:- Global styling: Use
.monaco-editor-containerto style all editor instances. - Class-based styling: Set the
CssClassproperty on the component to add a custom class to the container. - Instance-specific styling: Use the
Idproperty to target a specific editor instance via its ID.
Note: You must explicitly set a
heightfor the editor in CSS, otherwise it may default to0pxand be invisible.<StandaloneCodeEditor Id="my-editor-id" CssClass="my-editor-class" />/* Target specific instance */ #my-editor-id { height: 100px; } /* Target all instances with this class */ .my-editor-class { height: 100px; } /* Target all BlazorMonaco editors */ .monaco-editor-container { height: 100px; }- Global styling: Use
Update namespaces for v3.x
masterIn version 3.x, all functionality is consolidated into two primary namespaces. Update your
_Imports.razoror individual component files with the following directives:@using BlazorMonaco @using BlazorMonaco.Editor @using BlazorMonaco.LanguageUse a custom Monaco Editor installation
masterIf you have modified the Monaco Editor JS library and want to use your own version instead of the one bundled with BlazorMonaco, you can override the script loading logic. You must define a custom
requirepath for thevsdirectory before loading the Monaco scripts.<script src="_content/BlazorMonaco/jsInterop.js"></script> <script>var require = { paths: { vs: 'my-path/monaco-editor/min/vs' } };</script> <script src="my-path/monaco-editor/min/vs/loader.js"></script> <script src="my-path/monaco-editor/min/vs/editor/editor.main.js"></script>Troubleshoot editor visibility and initialization
masterEditor is invisible
Most likely, the editor has a height of
0px. BlazorMonaco does not manage the height internally. You must set a height via CSS using the editor'sIdorCssClass.Editor fails to initialize
BlazorMonaco requires an interactive render mode. If you are using Static Server-Side Rendering (Static SSR), the editor will not work. Ensure your page or component uses an interactive mode:
@rendermode InteractiveServerEditor breaks during navigation
If you are using Blazor's enhanced navigation, it may undo dynamic DOM changes made by Monaco. To prevent this, disable enhanced navigation for the target page:
- For links: Add
data-enhance-nav="false"to the<a>tag. - For programmatic navigation: Use
Navigation.NavigateTo("url", true)to force a full reload.
- For links: Add
Update Editor component names in v3.x
masterClass, property, and object names have been renamed to better align with the original Monaco Editor JavaScript library. Key renames include:
MonacoEditoris nowStandaloneCodeEditorMonacoDiffEditoris nowStandaloneDiffEditor
Check the namespace and class definitions for other renamed members. If a feature was not removed by Microsoft in the underlying Monaco Editor update, it will exist under a name similar to its previous version.
Access inner editors in StandaloneDiffEditor
masterA
StandaloneDiffEditorcontains two inner editors. You can access them via theOriginalEditorandModifiedEditorproperties. To listen to events on these inner editors, use the helper event parameters provided by theStandaloneDiffEditor(e.g.,OnKeyUpOriginalandOnKeyUpModified).<StandaloneDiffEditor @ref="_diffEditor" Id="my-diff-editor" OnKeyUpOriginal="OnKeyUpOriginal" OnKeyUpModified="OnKeyUpModified" /> @code { private StandaloneDiffEditor _diffEditor; private void OnKeyUpOriginal(KeyboardEvent keyboardEvent) { StandaloneCodeEditor originalEditor = _diffEditor.OriginalEditor; Console.WriteLine("OnKeyUpOriginal : " + keyboardEvent.Code); } private void OnKeyUpModified(KeyboardEvent keyboardEvent) { StandaloneCodeEditor modifiedEditor = _diffEditor.ModifiedEditor; Console.WriteLine("OnKeyUpModified : " + keyboardEvent.Code); } }Access Global methods in v3.x
masterStatic methods that were previously located inMonacoEditorandMonacoEditorBase(such asSetThemeandColorize) have been moved. They are now grouped under theBlazorMonaco.Editor.Globalclass.