如何获取与放置事件的位置相对应的范围对象?

发布于 2024-12-07 13:04:23 字数 1381 浏览 1 评论 0原文

我想将图像拖放到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-12-14 13:04:23

据我所知,一般来说没有一种简单的方法可以做到这一点。您可以获取放置点的像素坐标(可能来自 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

骄兵必败 2024-12-14 13:04:23

Tim Down 向我展示了没有简单的方法,我最终使用了一个解决方法:

GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableCreated', function(event, editable) {
    var the_obj = editable.obj;
    jQuery(editable.obj).bind('drop', function(event){
        setTimeout(function () {
            //at this point the html is updated and can be postprocessed 
            //in order to turn thumbnails into the real image

            //force the focus in order to make sure that the editable is activated
            //this will cause the deactivated event to be triggered, and the content to be saved
            the_obj.focus(); 
        }, 0);
    });
});

Tim Down showed me that there is no easy way and I finally used a workaround:

GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableCreated', function(event, editable) {
    var the_obj = editable.obj;
    jQuery(editable.obj).bind('drop', function(event){
        setTimeout(function () {
            //at this point the html is updated and can be postprocessed 
            //in order to turn thumbnails into the real image

            //force the focus in order to make sure that the editable is activated
            //this will cause the deactivated event to be triggered, and the content to be saved
            the_obj.focus(); 
        }, 0);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文