如何获取与放置事件的位置相对应的范围对象?
我想将图像拖放到 aloha 可编辑字段中。
我正在查看 at.tapo.aloha.plugins.Image< /a> 插件看起来很棒。
但是,我需要调整这个插件才能使用缩略图。我拖动缩略图,当我将其放入 aloha 可编辑窗口时,html 代码会即时修改,以便使用真实图像。
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableCreated', function(event, editable) {
var the_obj = editable.obj;
jQuery(editable.obj).bind('drop', function(event){
var e = event.originalEvent;
var files = e.dataTransfer.files;
var count = files.length;
if (count < 1) {
var node = e.dataTransfer.mozSourceNode;
if (node.tagName === 'IMG') {
var html = '<img ....>'; //build the real image html code
/// The current selection but I want the drop position
var range = GENTICS.Aloha.Selection.getRangeObject();
if (!jQuery.isEmptyObject(range)) {
GENTICS.Utils.Dom.insertIntoDOM(jQuery(html), range, the_obj);
}
return false;
}
return true;
}
}
当在 Aloha 字段中选择某些内容时,它可以正常工作。我可以获取一个范围并将 html 插入到 DOM 中的选择位置。
但是,我想获得一个与图像放置位置相对应的范围对象。怎么做呢?
预先感谢您的想法。
I want to drag and drop images into an aloha editable field.
I am looking at the at.tapo.aloha.plugins.Image plugin which seems great.
However, i need to adapt this plugin in order to work with thumbnail. I drag the thumbnail and when I drop it into the aloha editable, the html code is modified on the fly in order to use the real image.
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableCreated', function(event, editable) {
var the_obj = editable.obj;
jQuery(editable.obj).bind('drop', function(event){
var e = event.originalEvent;
var files = e.dataTransfer.files;
var count = files.length;
if (count < 1) {
var node = e.dataTransfer.mozSourceNode;
if (node.tagName === 'IMG') {
var html = '<img ....>'; //build the real image html code
/// The current selection but I want the drop position
var range = GENTICS.Aloha.Selection.getRangeObject();
if (!jQuery.isEmptyObject(range)) {
GENTICS.Utils.Dom.insertIntoDOM(jQuery(html), range, the_obj);
}
return false;
}
return true;
}
}
It works ok when something is selected in the aloha field. I can get a range and insert the html into the DOM at the selection position.
However, I would like to get a range object that correspond to the place where my image is dropped. How to do that?
Thanks in advance for ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,一般来说没有一种简单的方法可以做到这一点。您可以获取放置点的像素坐标(可能来自 mousemove 事件),然后尝试获取该点的范围。对于该任务,以下问题的答案很好地总结了它:
从 FF/Webkit 中的像素位置创建折叠范围
There isn't an easy way that I know of to do this in general. You could obtain pixel coordinates for the drop point (possibly from a
mousemove
event) and then attempt to get a range for that point. For that task, the answer to the following question sums it up nicely:Creating a collapsed range from a pixel position in FF/Webkit
Tim Down 向我展示了没有简单的方法,我最终使用了一个解决方法:
Tim Down showed me that there is no easy way and I finally used a workaround: