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

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

问题描述

我在编辑模式下使用WebView。我从WebUIDelegate Procotol实现此方法:

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

并使用它捕捉元素我的WebView。当我检测到一个文件从我的应用程序外部拖动,并且包含一张图片,我在这个方法中构建img DOM元素并将其添加到我的文档。



好吧,但是正如方法的名字所暗示的,我只是被通知拖拽会发生,我不能控制它。



由于Finder总是做文件拖动操作,在编辑模式下在WebView上放置文件时通常会发生的是webview显示文件的路径。



我最终将文件路径字符串添加到我的webview和图像,但我想防止添加文本。



有没有什么方法配置这个没有子类化webview?



我尝试它,而它的工作原理,它打破了许多其他的东西,像插入移动的下降等。

   - (BOOL)performDragOperation:

(id< NSDraggingInfo>)sender
{

if([sender draggingSource] == nil)
{

NSPasteboard * pboard = 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以更改拖动粘贴板的内容,如果拖动源在我的应用程序之外,并且不包含已有的图像,但只包含一个文件名。


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

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.

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.

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.

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.

解决方案

Answering this myself for a change!

- (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];


}

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天全站免登陆