简体   繁体   English

将文件拖放到鼠标下的光标位置

[英]Drag and Drop File into Cursor Position Under Mouse

Is there a way that you can do a file drag and drop operation into the DOM and detect the cursor position/range in the DOM where the mouse pointer is during the drop operation? 有没有办法可以在DOM中执行文件拖放操作并检测拖放操作期间鼠标指针所在的DOM中的光标位置/范围?

I can easily capture the Drop and get the file and content, but I can't seem to figure out how to drop custom HTML markup into the page at the drop location. 我可以轻松捕获Drop并获取文件和内容,但我似乎无法弄清楚如何将自定义HTML标记放入放置位置的页面中。 The drop provides a mouse position, but the question is how to convert that (or anything else) into a cursor position and range into which I can paste a new link for the file (after I've saved it dynamically). drop提供了一个鼠标位置,但问题是如何将其(或其他任何东西)转换为光标位置和范围,我可以粘贴文件的新链接(在我动态保存之后)。

My use case is that I'm using Ace Editor (or any contentEditable area) and am trying to drag a file into the document at the location the mouse cursor is dropped. 我的用例是我正在使用Ace Editor(或任何contentEditable区域)并尝试将文件拖放到鼠标光标掉落位置的文档中。 I then want to capture the file, and insert a link that references the capture file data which in this case is captured using the file API and stored on disk using a Web Browser control. 然后,我想要捕获该文件,并插入一个引用捕获文件数据的链接,在这种情况下,使用文件API捕获该数据并使用Web浏览器控件存储在磁盘上。 I can get everything to works except for detecting the drop location in my text document . 除了检测文本文档中的放置位置之外,我可以使一切工作正常。

So, any ideas how to acquire a selection range from a drop operation? 那么,任何想法如何从丢弃操作中获取选择范围? I have the mouse coordinates, but I'm not sure how to correlate those to a text position from which a range can be created to insert my link into. 我有鼠标坐标,但我不确定如何将这些与文本位置相关联,从中可以创建范围以插入我的链接。

This answer is a partial that addresses the Ace Editor portion of the question, but not how to do this using raw DOM operations. 这个答案部分解决了问题的Ace编辑器部分,而不是如何使用原始DOM操作来解决这个问题。 I'm hoping somebody else has a solution to doing this with raw DOM. 我希望其他人有一个解决方案,用原始DOM做到这一点。

As to the Ace Editor solution, it has supporting APIs that allow mapping PageX and PageY positions to be mapped to physical row and column positions in the editor document using: 对于Ace Editor解决方案,它具有支持API,允许将PageXPageY位置映射到编辑器文档中的物理行和列位置,方法是:

var pos = editor.renderer.screenToTextCoordinates(e.pageX, e.pageY)

Using that logic makes it possible to drop files at the drop position using the following code: 使用该逻辑可以使用以下代码删除放置位置的文件:

var $el = $("pre[lang]") // editor element
.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();            

    // grab the file
    var file = e.dataTransfer.files[0];

    // do something to save the file data to disk or server
    var imageUrl = "somelinktoimage";

    // Ace Editor logic
    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, "![" + file.name + "](" + imageUrl + ")");

    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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