From 82769f0b9cb3cc0b43a696cb68a669383f1565cd Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 3 Sep 2013 04:25:35 -0400 Subject: [PATCH] Allow moving (instead of copying) files into Zotero Cmd-drag on OS X, Shift on Windows and Linux As requested in https://forums.zotero.org/discussion/31717/delete-source-file-on-import/ Unfortunately due to https://bugzilla.mozilla.org/show_bug.cgi?id=911918 (and another issue on Windows) we can't show proper cursor feedback for external file drags. --- .../zotero/xpcom/collectionTreeView.js | 43 +++++++++++++++++-- chrome/content/zotero/xpcom/itemTreeView.js | 42 ++++++++++++++++-- chrome/content/zotero/zoteroPane.xul | 2 +- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/chrome/content/zotero/xpcom/collectionTreeView.js b/chrome/content/zotero/xpcom/collectionTreeView.js index 23864d2e0..e4271b975 100644 --- a/chrome/content/zotero/xpcom/collectionTreeView.js +++ b/chrome/content/zotero/xpcom/collectionTreeView.js @@ -1729,10 +1729,16 @@ Zotero.CollectionTreeView.prototype.drop = function(row, orient) var itemID = Zotero.Attachments.linkFromFile(file); } else { - if (dragData.dropEffect != 'copy') { - Components.utils.reportError("Invalid dropEffect '" + dragData.dropEffect + "' dropping file"); - } var itemID = Zotero.Attachments.importFromFile(file, false, targetLibraryID); + // If moving, delete original file + if (dragData.dropEffect == 'move') { + try { + file.remove(false); + } + catch (e) { + Components.utils.reportError("Error deleting original file " + file.path + " after drag"); + } + } } if (parentCollectionID) { @@ -1802,7 +1808,36 @@ Zotero.CollectionTreeView.prototype.onDragOver = function (event) { * Called by HTML 5 Drag and Drop when dropping onto the tree */ Zotero.CollectionTreeView.prototype.onDrop = function (event) { - Zotero.DragDrop.currentDataTransfer = event.dataTransfer; + if (event.dataTransfer.types.contains("application/x-moz-file")) { + Zotero.DragDrop.currentDataTransfer = event.dataTransfer; + Zotero.safeDebug(event); + if (Zotero.isMac) { + if (event.metaKey) { + if (event.altKey) { + event.dataTransfer.dropEffect = 'link'; + } + else { + event.dataTransfer.dropEffect = 'move'; + } + } + else { + event.dataTransfer.dropEffect = 'copy'; + } + } + else { + if (event.shiftKey) { + if (event.ctrlKey) { + event.dataTransfer.dropEffect = "link"; + } + else { + event.dataTransfer.dropEffect = "move"; + } + } + else { + event.dataTransfer.dropEffect = "copy"; + } + } + } return false; } diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index 9656ed579..e36e15601 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -3015,10 +3015,16 @@ Zotero.ItemTreeView.prototype.drop = function(row, orient) var itemID = Zotero.Attachments.linkFromFile(file, sourceItemID); } else { - if (dragData.dropEffect != 'copy') { - Components.utils.reportError("Invalid dropEffect '" + dragData.dropEffect + "' dropping file"); - } var itemID = Zotero.Attachments.importFromFile(file, sourceItemID, targetLibraryID); + // If moving, delete original file + if (dragData.dropEffect == 'move') { + try { + file.remove(false); + } + catch (e) { + Components.utils.reportError("Error deleting original file " + file.path + " after drag"); + } + } } if (parentCollectionID) { var col = Zotero.Collections.get(parentCollectionID); @@ -3077,10 +3083,40 @@ Zotero.ItemTreeView.prototype.onDragOver = function (event) { return false; } + /* * Called by HTML 5 Drag and Drop when dropping onto the tree */ Zotero.ItemTreeView.prototype.onDrop = function (event) { + if (event.dataTransfer.types.contains("application/x-moz-file")) { + Zotero.DragDrop.currentDataTransfer = event.dataTransfer; + if (Zotero.isMac) { + if (event.metaKey) { + if (event.altKey) { + event.dataTransfer.dropEffect = 'link'; + } + else { + event.dataTransfer.dropEffect = 'move'; + } + } + else { + event.dataTransfer.dropEffect = 'copy'; + } + } + else { + if (event.shiftKey) { + if (event.ctrlKey) { + event.dataTransfer.dropEffect = "link"; + } + else { + event.dataTransfer.dropEffect = "move"; + } + } + else { + event.dataTransfer.dropEffect = "copy"; + } + } + } return false; } diff --git a/chrome/content/zotero/zoteroPane.xul b/chrome/content/zotero/zoteroPane.xul index f14e5204a..9952a8dc3 100644 --- a/chrome/content/zotero/zoteroPane.xul +++ b/chrome/content/zotero/zoteroPane.xul @@ -341,7 +341,7 @@ ondragstart="if (event.target.localName == 'treechildren') { ZoteroPane_Local.itemsView.onDragStart(event); }" ondragenter="return ZoteroPane_Local.itemsView.onDragEnter(event)" ondragover="return ZoteroPane_Local.itemsView.onDragOver(event)" - ondragdrop="return ZoteroPane_Local.itemsView.onDrop(event)" + ondrop="return ZoteroPane_Local.itemsView.onDrop(event)" oncommand="ZoteroPane_Local.serializePersist()" flex="1">