mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-08 06:43:49 +00:00
Correct handling of pasting tiddlers (#7215)
* First Pass Implementation * Fix Check * Fix Style * Update drag-drop interop example to support copy tiddlers to clipboard --------- Co-authored-by: jeremy@jermolene.com <jeremy@jermolene.com>
This commit is contained in:
parent
474b73bdbe
commit
e66e791944
@ -159,7 +159,7 @@ exports.importDataTransfer = function(dataTransfer,fallbackTitle,callback) {
|
|||||||
if(!$tw.browser.isIE || importDataTypes[t].IECompatible) {
|
if(!$tw.browser.isIE || importDataTypes[t].IECompatible) {
|
||||||
// Get the data
|
// Get the data
|
||||||
var dataType = importDataTypes[t];
|
var dataType = importDataTypes[t];
|
||||||
var data = dataTransfer.getData(dataType.type);
|
var data = dataTransfer.getData(dataType.type);
|
||||||
// Import the tiddlers in the data
|
// Import the tiddlers in the data
|
||||||
if(data !== "" && data !== null) {
|
if(data !== "" && data !== null) {
|
||||||
if($tw.log.IMPORT) {
|
if($tw.log.IMPORT) {
|
||||||
@ -173,6 +173,36 @@ exports.importDataTransfer = function(dataTransfer,fallbackTitle,callback) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.importPaste = function(item,fallbackTitle,callback) {
|
||||||
|
// Try each provided data type in turn
|
||||||
|
for(var t=0; t<importDataTypes.length; t++) {
|
||||||
|
if(item.type === importDataTypes[t].type) {
|
||||||
|
// Get the data
|
||||||
|
var dataType = importDataTypes[t];
|
||||||
|
|
||||||
|
item.getAsString(function(data){
|
||||||
|
if($tw.log.IMPORT) {
|
||||||
|
console.log("Importing data type '" + dataType.type + "', data: '" + data + "'")
|
||||||
|
}
|
||||||
|
var tiddlerFields = dataType.toTiddlerFieldsArray(data,fallbackTitle);
|
||||||
|
callback(tiddlerFields);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.itemHasValidDataType = function(item) {
|
||||||
|
for(var t=0; t<importDataTypes.length; t++) {
|
||||||
|
if(!$tw.browser.isIE || importDataTypes[t].IECompatible) {
|
||||||
|
if(item.type === importDataTypes[t].type) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var importDataTypes = [
|
var importDataTypes = [
|
||||||
{type: "text/vnd.tiddler", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
{type: "text/vnd.tiddler", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
||||||
return parseJSONTiddlers(data,fallbackTitle);
|
return parseJSONTiddlers(data,fallbackTitle);
|
||||||
@ -205,7 +235,13 @@ var importDataTypes = [
|
|||||||
return [{title: fallbackTitle, text: data}];
|
return [{title: fallbackTitle, text: data}];
|
||||||
}},
|
}},
|
||||||
{type: "text/uri-list", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
{type: "text/uri-list", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
||||||
return [{title: fallbackTitle, text: data}];
|
// Check for tiddler data URI
|
||||||
|
var match = $tw.utils.decodeURIComponentSafe(data).match(/^data\:text\/vnd\.tiddler,(.*)/i);
|
||||||
|
if(match) {
|
||||||
|
return parseJSONTiddlers(match[1],fallbackTitle);
|
||||||
|
} else {
|
||||||
|
return [{title: fallbackTitle, text: data}]; // As URL string
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -271,6 +271,20 @@ DropZoneWidget.prototype.handlePasteEvent = function(event) {
|
|||||||
callback: readFileCallback,
|
callback: readFileCallback,
|
||||||
deserializer: this.dropzoneDeserializer
|
deserializer: this.dropzoneDeserializer
|
||||||
});
|
});
|
||||||
|
} else if(item.kind === "string" && !["text/html", "text/plain", "Text"].includes(item.type) && $tw.utils.itemHasValidDataType(item)) {
|
||||||
|
// Try to import the various data types we understand
|
||||||
|
var fallbackTitle = self.wiki.generateNewTitle("Untitled");
|
||||||
|
//Use the deserializer specified if any
|
||||||
|
if(this.dropzoneDeserializer) {
|
||||||
|
item.getAsString(function(str){
|
||||||
|
var tiddlerFields = self.wiki.deserializeTiddlers(null,str,{title: fallbackTitle},{deserializer:self.dropzoneDeserializer});
|
||||||
|
if(tiddlerFields && tiddlerFields.length) {
|
||||||
|
readFileCallback(tiddlerFields);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$tw.utils.importPaste(item,fallbackTitle,readFileCallback);
|
||||||
|
}
|
||||||
} else if(item.kind === "string") {
|
} else if(item.kind === "string") {
|
||||||
// Create tiddlers from string items
|
// Create tiddlers from string items
|
||||||
var tiddlerFields;
|
var tiddlerFields;
|
||||||
|
@ -15,6 +15,10 @@
|
|||||||
<div id="draggable" draggable="true">
|
<div id="draggable" draggable="true">
|
||||||
Drag me to a TiddlyWiki window
|
Drag me to a TiddlyWiki window
|
||||||
</div>
|
</div>
|
||||||
|
<button id="copy">
|
||||||
|
Click to copy two tiddlers to the clipboard
|
||||||
|
</button>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
@ -30,6 +34,18 @@
|
|||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.getElementById("copy").addEventListener("click",function(event) {
|
||||||
|
|
||||||
|
function listener(event) {
|
||||||
|
event.clipboardData.setData("URL","data:text/vnd.tiddler," + encodeURIComponent(JSON.stringify(tiddlerData)));
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("copy",listener);
|
||||||
|
document.execCommand("copy");
|
||||||
|
document.removeEventListener("copy",listener);
|
||||||
|
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
</html>
|
</html>
|
Loading…
x
Reference in New Issue
Block a user