从 finder 拖放到 WebView [英] Drag and drop from finder to WebView

查看:13
本文介绍了从 finder 拖放到 WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编辑模式下使用 WebView.我已经从 WebUIDelegate Procotol 实现了这个方法:

I'm using a WebView in edit mode. I have implemented this method from WebUIDelegate Procotol:

- (void)webView:(WebView *)sender willPerformDragDestinationAction:(WebDragDestinationAction)action forDraggingInfo:(id < NSDraggingInfo >)draggingInfo

并用它来捕捉我的 WebView 上的元素滴.当我检测到从我的应用程序外部拖动并包含图片的文件时,我会在此方法中构建 img DOM 元素并将其添加到我的文档中.

and use it to catch the drops of elements on my WebView. When I detect a file being dragged from outside my app, and containing a picture, I build in this method the img DOM element and add it to my document.

这很好用,但正如方法名称所暗示的那样,我只被告知会发生拖动,我无法控制它.

This works fine, but as the method's name implies, I am only informed that the drag will happen, and I have no control over it.

由于Finder总是进行文件拖拽操作,所以在编辑模式下将文件拖放到WebView上通常会发生的是webview显示文件的路径.

As the Finder always does file drag operation, what normally happens when dropping a file on a WebView in editing mode is the webview displays the path of the file.

我最终将文件路径字符串添加到了我的 webview 中,图像也添加了,但我想阻止添加文本.

I end up having the file path string added to my webview, and the image too, but I would like to prevent the text from being added.

有没有办法在不继承 webview 的情况下进行配置?

Is there any way to configure this without subclassing webview?

我试过了,虽然它有效,但它破坏了很多其他的东西,比如插入符号移动等.

I tried it and while it works, it breaks plenty of other things like caret moving for the drop and such.

推荐答案

自己回答这个问题!

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{

    if ([sender draggingSource] == nil)
    {

        NSPasteboard *pboard = [sender draggingPasteboard];

        if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
            NSURL* fileURL;
            fileURL=[NSURL URLFromPasteboard: [sender draggingPasteboard]];

            NSArray *dragTypes = [NSArray arrayWithObject:NSFileContentsPboardType];
            [[sender draggingPasteboard] declareTypes:dragTypes owner:nil];

            NSImage *content = [[NSImage alloc] initWithContentsOfURL:fileURL];
            [[sender draggingPasteboard] setData:[content TIFFRepresentation] forType:NSPasteboardTypeTIFF];
        }
    }

    return [super performDragOperation:sender];


}

实际上,如果拖动源在我的应用程序之外并且不包含图像而仅包含文件名,我所做的确实是对 WebView 进行子类化并拦截 performDragOperation 以更改拖动粘贴板的内容.

Actually, what I did was indeed to subclass the WebView and intercept the performDragOperation to change the content of the dragging pasteboard, if the dragging source is outside of my app and doesn't contain already an image but only a filename.

这篇关于从 finder 拖放到 WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆