1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-02 00:40:47 +00:00

Extend the dropzone widget to cope with dataTransfer objects (as well as files)

This commit is contained in:
Jeremy Ruston 2013-10-26 08:36:31 +01:00
parent 068befb24f
commit 05fc9848bb

View File

@ -88,7 +88,7 @@ DropZoneWidget.prototype.handleDropEvent = function(event) {
// Remove highlighting
$tw.utils.removeClass(this.domNodes[0],"tw-dragover");
// Try to import the various data types we understand
// this.importData(dataTransfer);
this.importData(dataTransfer);
// Import any files in the drop
this.wiki.readFiles(dataTransfer.files,function(tiddlerFields) {
self.dispatchEvent({type: "tw-import-tiddlers", param: JSON.stringify([tiddlerFields])});
@ -99,6 +99,37 @@ DropZoneWidget.prototype.handleDropEvent = function(event) {
event.stopPropagation();
};
DropZoneWidget.prototype.importData = function(dataTransfer) {
for(var t=0; t<this.importDataTypes.length; t++) {
var dataType = this.importDataTypes[t];
var data = dataTransfer.getData(dataType.type);
if(data !== "") {
var tiddlerFields = dataType.convertToFields(data);
if(!tiddlerFields.title) {
tiddlerFields.title = this.generateTitle("Untitled");
}
this.dispatchEvent({type: "tw-import-tiddlers", param: JSON.stringify([tiddlerFields])});
return;
}
};
};
DropZoneWidget.prototype.importDataTypes = [
{type: "text/vnd.tiddler", convertToFields: function(data) {
return JSON.parse(data);
}},
{type: "text/plain", convertToFields: function(data) {
return {
text: data
};
}},
{type: "text/uri-list", convertToFields: function(data) {
return {
text: data
};
}}
];
DropZoneWidget.prototype.handlePasteEvent = function(event) {
// Let the browser handle it if we're in a textarea or input box
if(["TEXTAREA","INPUT"].indexOf(event.target.tagName) == -1) {