Monaco-editor: Add an image to the editor

Created on 5 Aug 2020  路  6Comments  路  Source: microsoft/monaco-editor


Add an image to the editor. This image can be added, deleted or checked

feature-request

All 6 comments

Please further describe what you mean by this

just add an image to the editor.

How about make your editor theme's backgroundColor transparent.
And add an image by using <div></div>

Go to monaco playground
https://microsoft.github.io/monaco-editor/playground.html#creating-the-editor-hello-world

And copy & paste this code.
You can add an image to the editor

var jsCode = [
    '"use strict";',
    'function Person(age) {',
    '   if (age) {',
    '       this.age = age;',
    '   }',
    '}',
    'Person.prototype.getAge = function () {',
    '   return this.age;',
    '};'
].join('\n');

var editor = monaco.editor.create(document.getElementById("container"), {
    value: jsCode,
    language: "javascript",
    glyphMargin: true,
    contextmenu: false
});




// Add a content widget (scrolls inline with text)
var contentWidget = {
    domNode: null,
    getId: function() {
        return 'my.content.widget';
    },
    getDomNode: function() {
        if (!this.domNode) {
            this.domNode = document.createElement('div');
            this.domNode.innerHTML = '<img width="100px" height="100px" src="https://placeimg.com/100/100/any" />';
        }
        return this.domNode;
    },
    getPosition: function() {
        return {
            position: {
                lineNumber: 7,
                column: 8
            },
            preference: [monaco.editor.ContentWidgetPositionPreference.ABOVE, monaco.editor.ContentWidgetPositionPreference.BELOW]
        };
    }
};
editor.addContentWidget(contentWidget);



var output = document.getElementById('output');
function showEvent(str) {
    while(output.childNodes.length > 6) {
        output.removeChild(output.firstChild.nextSibling.nextSibling);
    }
    output.appendChild(document.createTextNode(str));
    output.appendChild(document.createElement('br'));
}


@ry7791 You are using images as widgets, which I have already implemented, but it is not what I want.
What I want is something similar to the markText https://codemirror.net/doc/manual.html#api_constructor in codemirror, where I can add an image instead of an image name by putting an image into view-lines, which I can edit as a value.

@ry7791 You are using images as widgets, which I have already implemented, but it is not what I want.
What I want is something similar to the markText https://codemirror.net/doc/manual.html#api_constructor in codemirror, where I can add an image instead of an image name by putting an image into view-lines, which I can edit as a value.

@qiufeihong2018 Your original issue was one sentence, and when the maintainer asked you to clarify you send one message back. You are clearly capable of explaining the issue clearly, why not doing it from the beginning?

Please be respectful of the maintainer's time and try to be more thorough with your writing so that the maintainers do not have to try to guess what you mean and waste time responding to the wrong issue.

Was this page helpful?
0 / 5 - 0 ratings