1

I'm trying to figure out how to configure the hover toolkit in monaco editor with clickable actions like in VS Code (see screenshot below) that execute another function. My app is react-based. I'd appreciate any ideas.

Example:

vsc-hover-toolkit.

Dummy code:

const hoverProvider = monaco.languages.registerHoverProvider(mylang, {
      provideHover: (model, position) => {
          return {
            range: new monaco.Range(
              position.lineNumber,
              position.lineNumber,
              model.getWordAtPosition(position).startColumn,
              model.getWordAtPosition(position).endColumn,
            ),
            contents: [
              { supportHtml:true, value: `<>What html element here ?</>` },
            ],
          };
        }
      },
    });

I've tried rendering a component into a string using the ReactDOMServer, but it didn't work as well as other HTML elements, buttons, etc.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
dawids8
  • 19
  • 4

1 Answers1

0

You cannot add HTML in the hover contents, but Markup text, which also supports links.

Create your contents array like this:

contents: [
    { value: "```ts\nlet a: string\n```" },
    { value: "[Hello](http://www.microsoft.com)" },
],

In fact that contents array is defined as:

    export interface Hover {
        /**
         * The contents of this hover.
         */
        contents: IMarkdownString[];
        /**
         * The range to which this hover applies. When missing, the
         * editor will use the range at the current position or the
         * current position itself.
         */
        range?: IRange;
    }
Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • I tried with something like `[Hello](javascript:alert('Hello World'))` and ` – dawids8 Mar 15 '23 at 14:05
  • Alert will not work in VS Code, but see my update. – Mike Lischke Mar 15 '23 at 14:17
  • Apologies, I should've been more precise - the Monaco editor runs as a part of a JSX component. And I'm trying to replicate the VS Code buttons functionality from the hover toolkit (these from the screenshot, e.g. Quick Fix) so that it executes a function. – dawids8 Mar 15 '23 at 19:35