This answer is partial, which refers to the part of the Ace editor of the question, but not how to do it using raw DOM operations. I hope someone else has the solution to do this with the raw DOM.
As for the Ace Editor solution, it has supporting APIs that allow you to map positions PageXboth PageYfor matching with physical rows and columns in an editor document using:
var pos = editor.renderer.screenToTextCoordinates(e.pageX, e.pageY)
Using this logic, you can drop files at the drop position using the following code:
var $el = $("pre[lang]")
.on("dragover", function(e) {
$(this).addClass("hover");
return false;
})
.on("dragleave", function() {
$(this).removeClass("hover");
return false;
})
.on("drop", function(e) {
e = e.originalEvent;
e.preventDefault();
var file = e.dataTransfer.files[0];
var imageUrl = "somelinktoimage";
var pos = editor.renderer.screenToTextCoordinates(e.pageX, e.pageY);
var sel = te.editor.getSelection();
sel.selectToPosition(pos);
var range = sel.getRange();
range.setStart(pos.row,pos.column);
range.setEnd(pos.row, pos.column);
sel.setSelectionRange(range);
te.editor.getSession().replace(range, "");
return false;
});
source
share