Remove special handling for URLs dragged from Firefox (#4649)

And the temporaryPDFHack that we used for them.
This commit is contained in:
Abe Jellinek 2024-08-29 16:02:14 -04:00 committed by Dan Stillman
parent dbce2b33b3
commit da0578fc39
5 changed files with 56 additions and 253 deletions

View file

@ -1749,19 +1749,17 @@ var CollectionTree = class CollectionTree extends LibraryTree {
}
return true;
}
else if (dataType == 'text/x-moz-url' || dataType == 'application/x-moz-file') {
else if (dataType == 'application/x-moz-file') {
if (treeRow.isSearch() || treeRow.isPublications()) {
return false;
}
if (dataType == 'application/x-moz-file') {
// Don't allow folder drag
if (data[0].isDirectory()) {
return false;
}
// Don't allow drop if no permissions
if (!treeRow.filesEditable) {
return false;
}
// Don't allow folder drag
if (data[0].isDirectory()) {
return false;
}
// Don't allow drop if no permissions
if (!treeRow.filesEditable) {
return false;
}
return true;
@ -2388,9 +2386,9 @@ var CollectionTree = class CollectionTree extends LibraryTree {
}.bind(this));
}
}
else if (dataType == 'text/x-moz-url' || dataType == 'application/x-moz-file') {
else if (dataType == 'application/x-moz-file') {
// See note in onDragOver() above
if (dataType == 'application/x-moz-file' && Zotero.isMac) {
if (Zotero.isMac) {
if (event.metaKey) {
if (event.altKey) {
dropEffect = 'link';
@ -2415,20 +2413,7 @@ var CollectionTree = class CollectionTree extends LibraryTree {
for (var i=0; i<data.length; i++) {
var file = data[i];
let item;
if (dataType == 'text/x-moz-url') {
var url = data[i];
// Still string, so remote URL
if (typeof file == 'string') {
window.ZoteroPane.addItemFromURL(url, 'temporaryPDFHack', null, row); // TODO: don't do this
continue;
}
// Otherwise file, so fall through
}
if (dropEffect == 'link') {
item = await Zotero.Attachments.linkFromFile({
file: file,

View file

@ -2403,7 +2403,7 @@ var ItemTree = class ItemTree extends LibraryTree {
}
return false;
}
else if (dataType == "text/x-moz-url" || dataType == 'application/x-moz-file') {
else if (dataType == 'application/x-moz-file') {
// Disallow direct drop on a non-regular item (e.g. note)
if (rowItem) {
if (!rowItem.isRegularItem()) {
@ -2522,7 +2522,7 @@ var ItemTree = class ItemTree extends LibraryTree {
}
}
}
else if (dataType == 'text/x-moz-url' || dataType == 'application/x-moz-file') {
else if (dataType == 'application/x-moz-file') {
// Disallow drop into read-only libraries
if (!collectionTreeRow.editable) {
window.ZoteroPane.displayCannotEditLibraryMessage();
@ -2530,7 +2530,7 @@ var ItemTree = class ItemTree extends LibraryTree {
}
// See note in onDragOver() above
if (dataType == 'application/x-moz-file' && Zotero.isMac) {
if (Zotero.isMac) {
if (event.metaKey) {
if (event.altKey) {
dropEffect = 'link';
@ -2580,42 +2580,7 @@ var ItemTree = class ItemTree extends LibraryTree {
let delaySetAutoAttachmentTitle = data.length > 1;
for (var i=0; i<data.length; i++) {
var file = data[i];
if (dataType == 'text/x-moz-url') {
var url = data[i];
// Still string, so remote URL
if (typeof file == 'string') {
let item;
if (parentItemID) {
if (!collectionTreeRow.filesEditable) {
window.ZoteroPane.displayCannotEditLibraryFilesMessage();
return;
}
item = await Zotero.Attachments.importFromURL({
libraryID: targetLibraryID,
url,
renameIfAllowedType,
parentItemID,
saveOptions: {
notifierQueue
}
});
}
else {
item = await window.ZoteroPane.addItemFromURL(url, 'temporaryPDFHack'); // TODO: don't do this
}
if (item) {
addedItems.push(item);
}
continue;
}
// Otherwise file, so fall through
}
file = file.path;
var file = data[i].path;
// Rename file if it's an allowed type
let fileBaseName = false;
@ -2691,7 +2656,7 @@ var ItemTree = class ItemTree extends LibraryTree {
if (delaySetAutoAttachmentTitle) {
for (let item of addedItems) {
item.setAutoAttachmentTitle();
await item.saveTx();
await item.saveTx({ notifierQueue });
}
}
// Select children created after drag-drop onto a top-level item

View file

@ -2108,7 +2108,7 @@ Zotero.DragDrop = {
if (dt.types.includes('application/x-moz-file')) {
dragData.dataType = 'application/x-moz-file';
var files = [];
for (var i=0; i<len; i++) {
for (var i = 0; i < len; i++) {
var file = dt.mozGetDataAt("application/x-moz-file", i);
if (!file) {
continue;
@ -2133,15 +2133,11 @@ Zotero.DragDrop = {
dragData.data = files;
}
// This isn't an else because on Linux a link drag contains an empty application/x-moz-file too
if (!dragData.data || !dragData.data.length) {
if (dt.types.includes('text/x-moz-url')) {
dragData.dataType = 'text/x-moz-url';
var urls = [];
for (var i=0; i<len; i++) {
var url = dt.getData("text/x-moz-url").split("\n")[0];
urls.push(url);
}
dragData.data = urls;
if ((!dragData.data || !dragData.data.length) && dt.types.includes('text/x-moz-url')) {
let uri = Services.io.newURI(dt.getData('text/x-moz-url').split("\n")[0]);
if (uri.schemeIs('file')) {
dragData.dataType = 'application/x-moz-file';
dragData.data = [uri.QueryInterface(Ci.nsIFileURL).file];
}
}
}

View file

@ -4811,7 +4811,7 @@ var ZoteroPane = new function () {
if (delaySetAutoAttachmentTitle) {
for (let item of addedItems) {
item.setAutoAttachmentTitle();
await item.saveTx();
await item.saveTx({ notifierQueue });
}
}
}
@ -4866,82 +4866,6 @@ var ZoteroPane = new function () {
// Save snapshot if explicitly enabled or automatically pref is set and not explicitly disabled
saveSnapshot = saveSnapshot || (saveSnapshot !== false && Zotero.Prefs.get('automaticSnapshots'));
// TODO: this, needless to say, is a temporary hack
if (itemType == 'temporaryPDFHack') {
itemType = null;
var isPDF = false;
if (doc.title.indexOf('application/pdf') != -1 || Zotero.Attachments.isPDFJSDocument(doc)
|| doc.contentType == 'application/pdf') {
isPDF = true;
}
else {
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
try {
var uri = ios.newURI(doc.location, null, null);
if (uri.fileName && uri.fileName.match(/pdf$/)) {
isPDF = true;
}
}
catch (e) {
Zotero.debug(e);
Components.utils.reportError(e);
}
}
if (isPDF && saveSnapshot) {
//
// Duplicate newItem() checks here
//
if (Zotero.DB.inTransaction()) {
await Zotero.DB.waitForTransaction();
}
// Currently selected row
if (row === undefined && this.collectionsView && this.getCollectionTreeRow()) {
row = this.collectionsView.selection.focused;
}
if (row && !this.canEdit(row)) {
this.displayCannotEditLibraryMessage();
return false;
}
if (row !== undefined) {
var collectionTreeRow = this.collectionsView.getRow(row);
var libraryID = collectionTreeRow.ref.libraryID;
}
else {
var libraryID = Zotero.Libraries.userLibraryID;
var collectionTreeRow = null;
}
//
//
//
if (row && !this.canEditFiles(row)) {
this.displayCannotEditLibraryFilesMessage();
return false;
}
if (collectionTreeRow && collectionTreeRow.isCollection()) {
var collectionID = collectionTreeRow.ref.id;
}
else {
var collectionID = false;
}
let item = await Zotero.Attachments.importFromDocument({
libraryID: libraryID,
document: doc,
collections: collectionID ? [collectionID] : []
});
await this.selectItem(item.id);
return false;
}
}
// Save web page item by default
if (!itemType) {
itemType = 'webpage';
@ -5005,63 +4929,6 @@ var ZoteroPane = new function () {
}
// Otherwise create placeholder item, attach attachment, and update from that
else {
// TODO: this, needless to say, is a temporary hack
if (itemType == 'temporaryPDFHack') {
itemType = null;
if (mimeType == 'application/pdf') {
//
// Duplicate newItem() checks here
//
if (Zotero.DB.inTransaction()) {
await Zotero.DB.waitForTransaction();
}
// Currently selected row
if (row === undefined) {
row = ZoteroPane_Local.collectionsView.selection.focused;
}
if (!ZoteroPane_Local.canEdit(row)) {
ZoteroPane_Local.displayCannotEditLibraryMessage();
return false;
}
if (row !== undefined) {
var collectionTreeRow = ZoteroPane_Local.collectionsView.getRow(row);
var libraryID = collectionTreeRow.ref.libraryID;
}
else {
var libraryID = Zotero.Libraries.userLibraryID;
var collectionTreeRow = null;
}
//
//
//
if (!ZoteroPane_Local.canEditFiles(row)) {
ZoteroPane_Local.displayCannotEditLibraryFilesMessage();
return false;
}
if (collectionTreeRow && collectionTreeRow.isCollection()) {
var collectionID = collectionTreeRow.ref.id;
}
else {
var collectionID = false;
}
let attachmentItem = await Zotero.Attachments.importFromURL({
libraryID,
url,
collections: collectionID ? [collectionID] : undefined,
contentType: mimeType
});
this.selectItem(attachmentItem.id)
return attachmentItem;
}
}
if (!itemType) {
itemType = 'webpage';
}

View file

@ -1099,13 +1099,6 @@ describe("Zotero.ItemTree", function () {
describe("#onDrop()", function () {
var httpd;
var port = 16213;
var baseURL = `http://localhost:${port}/`;
var pdfFilename = "test.pdf";
var pdfURL = baseURL + pdfFilename;
var pdfPath;
function drop(index, orient, dataTransfer) {
Zotero.DragDrop.currentOrientation = orient;
var event = { dataTransfer };
@ -1131,17 +1124,6 @@ describe("Zotero.ItemTree", function () {
return itemsView.onDrop(event, index);
}
// Serve a PDF to test URL dragging
before(function () {
var { HttpServer } = ChromeUtils.importESModule("chrome://remote/content/server/httpd.sys.mjs");;
httpd = new HttpServer();
httpd.start(port);
var file = getTestDataDirectory();
file.append(pdfFilename);
pdfPath = file.path;
httpd.registerFile("/" + pdfFilename, file);
});
beforeEach(() => {
// Don't run recognize on every file
Zotero.Prefs.set('autoRecognizeFiles', false);
@ -1150,10 +1132,6 @@ describe("Zotero.ItemTree", function () {
});
after(function* () {
var defer = Zotero.Promise.defer();
httpd.stop(() => defer.resolve());
yield defer.promise;
Zotero.Prefs.clear('autoRecognizeFiles');
Zotero.Prefs.clear('autoRenameFiles');
Zotero.Prefs.clear('autoRenameFiles.linked');
@ -1256,10 +1234,13 @@ describe("Zotero.ItemTree", function () {
);
});
it("should create a stored top-level attachment when a URL is dragged", async function () {
it("should create a stored top-level attachment when a file URI is dragged", async function () {
var promise = itemsView.waitForSelect();
var pdfFile = getTestDataDirectory();
pdfFile.append('test.pdf');
var pdfURL = Services.io.newFileURI(pdfFile).spec;
drop(0, -1, {
await drop(0, -1, {
dropEffect: 'copy',
effectAllowed: 'copy',
types: ['text/x-moz-url'],
@ -1269,25 +1250,28 @@ describe("Zotero.ItemTree", function () {
}
},
mozItemCount: 1,
})
});
await promise;
var item = itemsView.getSelectedItems()[0];
assert.equal(item.getField('url'), pdfURL);
assert.equal(item.getField('url'), '');
assert.equal(
((await Zotero.File.getBinaryContentsAsync(await item.getFilePathAsync()))),
((await Zotero.File.getBinaryContentsAsync(pdfPath)))
(await Zotero.File.getBinaryContentsAsync(await item.getFilePathAsync())),
(await Zotero.File.getBinaryContentsAsync(pdfFile))
);
});
it("should create a stored child attachment when a URL is dragged", async function () {
it("should create a stored child attachment when a file URI is dragged", async function () {
var view = zp.itemsView;
var parentItem = await createDataObject('item');
var parentRow = view.getRowIndexByID(parentItem.id);
var promise = waitForItemEvent('add');
var pdfFile = getTestDataDirectory();
pdfFile.append('test.pdf');
var pdfURL = Services.io.newFileURI(pdfFile).spec;
drop(parentRow, 0, {
await drop(parentRow, 0, {
dropEffect: 'copy',
effectAllowed: 'copy',
types: ['text/x-moz-url'],
@ -1297,15 +1281,15 @@ describe("Zotero.ItemTree", function () {
}
},
mozItemCount: 1,
})
});
var itemIDs = await promise;
var item = Zotero.Items.get(itemIDs[0]);
assert.equal(item.parentItemID, parentItem.id);
assert.equal(item.getField('url'), pdfURL);
assert.equal(item.getField('url'), '');
assert.equal(
((await Zotero.File.getBinaryContentsAsync(await item.getFilePathAsync()))),
((await Zotero.File.getBinaryContentsAsync(pdfPath)))
(await Zotero.File.getBinaryContentsAsync(await item.getFilePathAsync())),
(await Zotero.File.getBinaryContentsAsync(pdfFile))
);
});
@ -1337,16 +1321,19 @@ describe("Zotero.ItemTree", function () {
}
);
var file = getTestDataDirectory();
file.append('test.pdf');
drop(0, -1, {
dropEffect: 'copy',
effectAllowed: 'copy',
types: ['text/x-moz-url'],
getData: function (type) {
if (type == 'text/x-moz-url') {
return pdfURL;
}
},
types: ['application/x-moz-file'],
mozItemCount: 1,
mozGetDataAt: function (type, i) {
if (type == 'application/x-moz-file' && i == 0) {
return file;
}
}
})
// Wait for attachment item
@ -1390,13 +1377,16 @@ describe("Zotero.ItemTree", function () {
}
);
var file = getTestDataDirectory();
file.append('test.pdf');
drop(0, -1, {
dropEffect: 'copy',
effectAllowed: 'copy',
types: ['text/x-moz-url'],
getData: function (type) {
if (type == 'text/x-moz-url') {
return pdfURL;
types: ['application/x-moz-file'],
mozGetDataAt: function (type) {
if (type == 'application/x-moz-file') {
return file;
}
},
mozItemCount: 2,