HTML support in hover (inline HTML in IMarkdownString) #3760
Replies: 18 comments 1 reply
-
@ryanlin1986 I also opened a separate #802 to describe the problem where the hover behaviour doesn't match VS Code's. |
Beta Was this translation helpful? Give feedback.
-
Not sure if it's related, but this feature seems to be broken in general since 0.11.x. One good example is if you go to https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-rendering-glyphs-in-the-margin, switch to the This might be related to the changes noted in #780, not sure. |
Beta Was this translation helpful? Give feedback.
-
@rcjsuen thanks for the help;) |
Beta Was this translation helpful? Give feedback.
-
@mofux That's a bug with monaco-css. I've opened #805 as that's a different problem. |
Beta Was this translation helpful? Give feedback.
-
@ryanlin1986 do you happen to have that code example which you used in describing the issue? I have a problem displaying the hover message, as it cannot find |
Beta Was this translation helpful? Give feedback.
-
@alexdima any update on this bug? we're trying to show a I think we could settle for having a link that opens in the same tab but the current |
Beta Was this translation helpful? Give feedback.
-
@dehoward I am not really familiar with the |
Beta Was this translation helpful? Give feedback.
-
I did some experimenting and was able to achieve this via commands inside the declare var monaco: any
// create the Monaco editor
const editor = monaco.editor.create(document.getElementById("container")!, {
language: "plaintext",
value: "hover here"
});
const cmdId = editor.addCommand(0, () => {
window.location.href = "https://github.com/microsoft/monaco-editor/";
}, "")
monaco.languages.registerHoverProvider("plaintext", {
provideHover() {
return {
contents: [
{
value: `[Run Command](command:${cmdId})`,
isTrusted: true
},
]
}
}
}); |
Beta Was this translation helpful? Give feedback.
-
@dehoward I figured out a workaround for the monaco.languages.register({ id: 'testLanguage' });
const editor = monaco.editor.create(document.getElementById("container"), {
value: 'Hover over this text',
language: 'testLanguage'
});
const cmdId = editor.addCommand(0, () => {
if (window.self !== window.top) {
window.parent.location.href = "https://github.com/microsoft/monaco-editor/";;
} else {
window.location.href = "https://github.com/microsoft/monaco-editor/";;
}
})
monaco.languages.registerHoverProvider('testLanguage', {
provideHover: (model) => {
return {
range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
contents: [
{ value: `[Run Command](command:${cmdId})`, isTrusted: true }
]
};
}
}); |
Beta Was this translation helpful? Give feedback.
-
@alexdima Thanks, monaco.languages.register({ id: 'mySpecialLanguage' });
monaco.languages.registerHoverProvider('mySpecialLanguage', {
provideHover: function (model, position) {
return {
range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
contents: [
{ value: "<a href='https://microsoft.com'>Link!</a>" },
],
supportHtml: true,
isTrusted: true
}
}
});
monaco.editor.create(document.getElementById("container"), {
value: '\n\nHover over this text',
language: 'mySpecialLanguage'
});
@rcjsuen Nice, I think I'll settle for this solution if I'm not able to get |
Beta Was this translation helpful? Give feedback.
-
@dehoward Your code is a little off because your monaco.languages.register({ id: 'mySpecialLanguage' });
monaco.languages.registerHoverProvider('mySpecialLanguage', {
provideHover: function (model) {
return {
range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
contents: [
{
value: "<a href='https://www.microsoft.com'>Link!</a>",
supportHtml: true,
isTrusted: true
},
]
}
}
});
monaco.editor.create(document.getElementById("container"), {
value: '\n\nHover over this text',
language: 'mySpecialLanguage'
}); |
Beta Was this translation helpful? Give feedback.
-
@rcjsuen When I try your snippet, I actually see the correct HTML, so the lack of navigation might be some limitation of the playground's |
Beta Was this translation helpful? Give feedback.
-
@alexdima Yeah, I see the same thing. I copy/pasted it to my personal project and it doesn't work there either. I cut my HTML down and it didn't really help. <!DOCTYPE html>
<html>
<head>
<script src="vs/loader.js"></script>
<script src="bundle.js"></script>
</head>
<style type="text/css">
html,
body {
overflow: hidden;
height: 100%;
margin: 0;
}
#container {
margin: 0;
border: 0;
min-width: 100%;
min-height: 100%;
}
</style>
<body>
<div id="container"></div>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
@rcjsuen thanks for catching my mistake. I see the link now with no navigation as you mentioned. monaco.languages.register({ id: 'mySpecialLanguage' });
monaco.languages.registerHoverProvider('mySpecialLanguage', {
provideHover: function (model) {
return {
range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
contents: [
{
value: "<button onclick='alert(1)'>button!</button>",
supportHtml: true,
isTrusted: true
},
]
}
}
});
monaco.editor.create(document.getElementById("container"), {
value: '\n\nHover over this text',
language: 'mySpecialLanguage'
}); @alexdima @mjbvz it may be helpful to have some documentation around which "subset of HTML elements" are allowed if there isn't some already. |
Beta Was this translation helpful? Give feedback.
-
@dehoward This is not really my area. @mjbvz Can you please help us here? |
Beta Was this translation helpful? Give feedback.
-
@dehoward It's not documented but it's in the code comments. Neither |
Beta Was this translation helpful? Give feedback.
-
Yes that's correct @rcjsuen We only allow a safe subset of html tags and attributes (these are not listed directly in the JSDoc for |
Beta Was this translation helpful? Give feedback.
-
I know this one has been a while, but it looks like the support for rich hover popups is still quite limited? Is there a reason for this limitation? We'd like to switch to using Monaco but it seems like doing the rich popups we do with our existing editor is a nonstarter |
Beta Was this translation helpful? Give feedback.
-
monaco-editor version: latest
Browser:chrome
OS: win10
Steps or JS usage snippet reproducing the issue:
I want my completion item documentation or hover documentation have inline html.
For example, signature should be colorized, such as:
Here is what I've tried:
As the above image shows, the inline html is santinized,.
Beta Was this translation helpful? Give feedback.
All reactions