Home

Awesome

<p align="center"><img src="https://raw.githubusercontent.com/serdarciplak/BlazorMonaco/master/BlazorMonaco/icon.png" width="150" height="150" /></p> <p align="center"> <a href="https://www.nuget.org/packages/BlazorMonaco/"><img src="https://buildstats.info/nuget/BlazorMonaco" /></a> </p> <h1 align="center">BlazorMonaco</h1>

Blazor component for Microsoft's Monaco Editor which powers Visual Studio Code.

Some less-frequently used Monaco Editor features are currently missing, but a good amount of the basic feature set is supported. The package is updated regularly to cover more features and use cases. Any contributions, comments or suggestions are greatly welcome. Please feel free to contact me at twitter/serdarciplak or via GitHub.

Current version of BlazorMonaco :

Demo

You can see a working sample WebAssembly app here.

Get Started

dotnet add package BlazorMonaco
// or
Install-Package BlazorMonaco
<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>
@using BlazorMonaco
@using BlazorMonaco.Editor
@using BlazorMonaco.Languages

Code Editor

Adding an editor instance

<StandaloneCodeEditor Id="my-editor-instance-id" />

Providing custom options

<StandaloneCodeEditor Id="my-editor-instance-id" ConstructionOptions="EditorConstructionOptions" />
private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
{
	return new StandaloneEditorConstructionOptions
	{
		AutomaticLayout = true,
		Language = "javascript",
		Value = "function xyz() {\n" +
				"   console.log(\"Hello world!\");\n" +
				"}"
	};
}

Editor events

<StandaloneCodeEditor Id="my-editor-instance-id" OnDidChangeCursorPosition="EditorDidChangeCursorPosition" />
private void EditorDidChangeCursorPosition(CursorPositionChangedEvent eventArgs)
{
	Console.WriteLine("EditorDidChangeCursorPosition");
}

Diff Editor

Adding a diff editor instance

<StandaloneDiffEditor Id="my-editor-instance-id" />

Access to inner editor instances and events

<StandaloneDiffEditor Id="my-editor-instance-id" OnKeyUpOriginal="OnKeyUpOriginal" OnKeyUpModified="OnKeyUpModified" />

Notes

Css styling

<StandaloneCodeEditor Id="my-editor-id" CssClass="my-editor-class" />
.monaco-editor-container { /* for all editor instances */
	height: 100px;
}

/* or */

#my-editor-id { /* for a specific editor instance */
	height: 100px;
}

/* or */

.my-editor-class { /* for a specific editor instance */
	height: 100px;
}

⚠️ Note : As an html element cannot set its height disregarding where it's placed in, BlazorMonaco cannot manage editor instance heights. So, if you don't do that yourself, editor instances may have a height of 0px and be invisible. Please don't forget to set your editor instance heights as you need.

Global Methods

Monaco Editor JavaScript library defines some methods in the global scope. As C# does not allow global methods, BlazorMonaco groups those methods in two static classes named BlazorMonaco.Editor.Global and BlazorMonaco.Language.Global. If you need to use a Monaco Editor method in the global scope, check where in the JavaScript library that method is, and search for it in the corresponding Global class. They're defined as static methods.

For example, you can use the SetTheme method as below.

await BlazorMonaco.Editor.Global.SetTheme(jsRuntime, "my-custom-theme");

Using a custom Monaco Editor setup

<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>

Documentation

As BlazorMonaco is just a bridge between JavaScript and Blazor, you can use Monaco Editor's documentation.

Migration Guide

After a major version update (like from v2.x.x to v3.x.x), you may need to make some changes in your integration. Please see the MIGRATE.md for details.

Change Log

You can view the history and the changes in the CHANGELOG.md

License

MIT, see the LICENSE file for detail.