简体   繁体   中英

Display an image in Monaco-Editor HoverProvider

According to the docs , monaco-editor's HoverProvider supports Markdown. Is there a way for it to display an image? I tried this in the playground (based on the HoverProvider example ) and it doesn't work:


monaco.languages.register({ id: 'mySpecialLanguage' });

monaco.languages.registerHoverProvider('mySpecialLanguage', {
    provideHover: function (model, position) {
        return xhr('../playground.html').then(function (res) {
            return {
                range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
                contents: [
                    { value: '<img src="https://www.sciencemag.org/sites/default/files/styles/inline__450w__no_aspect/public/dogs_1280p_0.jpg?itok=4t_1_fSJ"/>', isTrusted: true }
                ]
            }
        });
    }
});

monaco.editor.create(document.getElementById("container"), {
    value: '\n\nHover over this text',
    language: 'mySpecialLanguage'
});

function xhr(url) {
    var req = null;
    return new Promise(function (c, e) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function () {
            if (req._canceled) { return; }

            if (req.readyState === 4) {
                if ((req.status >= 200 && req.status < 300) || req.status === 1223) {
                    c(req);
                } else {
                    e(req);
                }
                req.onreadystatechange = function () { };
            }
        };

        req.open("GET", url, true);
        req.responseType = "";

        req.send(null);
    }, function () {
        req._canceled = true;
        req.abort();
    });
}

It just displays the content as a string

You should provide your image path in Markdown syntax:

{ value: '![my image](https://www.sciencemag.org/sites/default/files/styles/inline__450w__no_aspect/public/dogs_1280p_0.jpg?itok=4t_1_fSJ)' }

In that case the full playground code would look like below (modified version of the example ):

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: '**SOURCE**' },
                { value: '![my image](https://www.sciencemag.org/sites/default/files/styles/inline__450w__no_aspect/public/dogs_1280p_0.jpg?itok=4t_1_fSJ)' }
            ]
        }
    }
});

monaco.editor.create(document.getElementById("container"), {
    value: '\n\nHover over this text',
    language: 'mySpecialLanguage'
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM