Enabled Hover capability on Monaco Editor and fixed JSON serialization issue. #1015
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull request type
Please check the type of change your PR introduces:
What is the current behavior?
In preparation of @btiteux work on
JWT Encoder / Decoder
, I was curious to demonstrate how to useMonaco Editor
API to show some text on a given span when the mouse pass the mouse hover it. Like this:Unfortunately, I quickly faced a bug where the JSON we pass from .NET code to JavaScript code has some properties that aren't expected by Monaco.
To add a message on hover of a span in Monaco, we need to create an instance of MarkdownString.
The minimum needed to make it work is to set a string to its
Value
property. However, that wasn't working.The reason why is that when we send the
MarkdownString
to the JavaScript usingIJsRuntime.InvokeAsync
, we would get a JSON like this:The problem is: in these methods from Monaco, it considers that this JSON isn't good if
supportThemeIcons
is null (instead of undefined, or with a boolean value).As a consequence, the hover feature doesn't work.
What is the new behavior?
As a fix, I created a method
PrepareJsInterop
that takes any object and convert it into a dynamic object, where null properties are excluded.See the documentation on top of
PrepareJsInterop
to understand why I took this approach :-)With this method invoked, the generated JSON is now:
Other information
I considered some other approaches mentioned in dotnet/aspnetcore#12685 but the best in my humble opinion was that
PrepareJsInterop
method.In the future, once this PR is merged, creating a tooltip on hover in Monaco will be as simple as:
Quality check
Before creating this PR: