Restore drag-and-drop Quick Copy
This commit is contained in:
parent
8a0081c84e
commit
3de40256bf
|
@ -2532,8 +2532,6 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Quick Copy format for current URL
|
// Get Quick Copy format for current URL
|
||||||
// TODO: Fix this
|
|
||||||
/** Currently broken
|
|
||||||
var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ?
|
var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ?
|
||||||
this._ownerDocument.defaultView.content.location.href : null;
|
this._ownerDocument.defaultView.content.location.href : null;
|
||||||
var format = Zotero.QuickCopy.getFormatFromURL(url);
|
var format = Zotero.QuickCopy.getFormatFromURL(url);
|
||||||
|
@ -2572,7 +2570,6 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
|
||||||
Zotero.debug(e);
|
Zotero.debug(e);
|
||||||
Components.utils.reportError(e + " with '" + format.id + "'");
|
Components.utils.reportError(e + " with '" + format.id + "'");
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,18 @@
|
||||||
Zotero.QuickCopy = new function() {
|
Zotero.QuickCopy = new function() {
|
||||||
var _siteSettings;
|
var _siteSettings;
|
||||||
var _formattedNames;
|
var _formattedNames;
|
||||||
|
var _initialized = false;
|
||||||
|
|
||||||
this.init = Zotero.Promise.coroutine(function* () {
|
this.init = Zotero.Promise.coroutine(function* () {
|
||||||
yield this.loadSiteSettings();
|
yield this.loadSiteSettings();
|
||||||
|
|
||||||
|
// Load code for selected export translator ahead of time
|
||||||
|
// (in the background, because it requires translator initialization)
|
||||||
|
setTimeout(_loadOutputFormat, 5000);
|
||||||
|
if (!_initialized) {
|
||||||
|
Zotero.Prefs.registerObserver("export.quickCopy.setting", () => _loadOutputFormat());
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -405,6 +414,22 @@ Zotero.QuickCopy = new function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If an export translator is the selected output format, load its code (which must be done
|
||||||
|
* asynchronously) ahead of time, since drag-and-drop requires synchronous operation
|
||||||
|
*/
|
||||||
|
var _loadOutputFormat = Zotero.Promise.coroutine(function* () {
|
||||||
|
var format = Zotero.Prefs.get("export.quickCopy.setting");
|
||||||
|
format = Zotero.QuickCopy.unserializeSetting(format);
|
||||||
|
if (format.mode == 'export') {
|
||||||
|
yield Zotero.Translators.init();
|
||||||
|
let translator = Zotero.Translators.get(format.id);
|
||||||
|
translator.cacheCode = true;
|
||||||
|
yield translator.getCode();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
var _loadFormattedNames = Zotero.Promise.coroutine(function* () {
|
var _loadFormattedNames = Zotero.Promise.coroutine(function* () {
|
||||||
var t = new Date;
|
var t = new Date;
|
||||||
Zotero.debug("Loading formatted names for Quick Copy");
|
Zotero.debug("Loading formatted names for Quick Copy");
|
||||||
|
|
|
@ -1245,15 +1245,22 @@ Zotero.Translate.Base.prototype = {
|
||||||
this.setHandler("done", doneHandler);
|
this.setHandler("done", doneHandler);
|
||||||
this.setHandler("error", errorHandler);
|
this.setHandler("error", errorHandler);
|
||||||
|
|
||||||
if(typeof this.translator[0] === "object") {
|
|
||||||
// already have a translator object, so use it
|
|
||||||
this._loadTranslator(this.translator[0]).then(function() { me._translateTranslatorLoaded() });
|
|
||||||
} else {
|
|
||||||
// need to get translator first
|
// need to get translator first
|
||||||
let translator = Zotero.Translators.get(this.translator[0]);
|
if (typeof this.translator[0] !== "object") {
|
||||||
this.translator[0] = translator;
|
this.translator[0] = Zotero.Translators.get(this.translator[0]);
|
||||||
this._loadTranslator(translator).then(function() { me._translateTranslatorLoaded() });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var loadPromise = this._loadTranslator(this.translator[0]);
|
||||||
|
if (this.noWait) {
|
||||||
|
if (!loadPromise.isResolved()) {
|
||||||
|
return Zotero.Promise.reject(new Error("Load promise is not resolved in noWait mode"));
|
||||||
|
}
|
||||||
|
this._translateTranslatorLoaded();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
loadPromise.then(() => this._translateTranslatorLoaded());
|
||||||
|
}
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1633,7 +1640,7 @@ Zotero.Translate.Base.prototype = {
|
||||||
* @param {Zotero.Translator} translator
|
* @param {Zotero.Translator} translator
|
||||||
* @return {Boolean} Whether the translator could be successfully loaded
|
* @return {Boolean} Whether the translator could be successfully loaded
|
||||||
*/
|
*/
|
||||||
"_loadTranslator":function(translator) {
|
"_loadTranslator": Zotero.Promise.method(function (translator) {
|
||||||
var sandboxLocation = this._getSandboxLocation();
|
var sandboxLocation = this._getSandboxLocation();
|
||||||
if(!this._sandboxLocation || sandboxLocation !== this._sandboxLocation) {
|
if(!this._sandboxLocation || sandboxLocation !== this._sandboxLocation) {
|
||||||
this._sandboxLocation = sandboxLocation;
|
this._sandboxLocation = sandboxLocation;
|
||||||
|
@ -1646,19 +1653,42 @@ Zotero.Translate.Base.prototype = {
|
||||||
this._aborted = false;
|
this._aborted = false;
|
||||||
this.saveQueue = [];
|
this.saveQueue = [];
|
||||||
|
|
||||||
var me = this;
|
var parse = function(code) {
|
||||||
return translator.getCode().then(function(code) {
|
|
||||||
Zotero.debug("Translate: Parsing code for " + translator.label + " "
|
Zotero.debug("Translate: Parsing code for " + translator.label + " "
|
||||||
+ "(" + translator.translatorID + ", " + translator.lastUpdated + ")", 4);
|
+ "(" + translator.translatorID + ", " + translator.lastUpdated + ")", 4);
|
||||||
me._sandboxManager.eval("var exports = {}, ZOTERO_TRANSLATOR_INFO = "+code,
|
this._sandboxManager.eval(
|
||||||
["detect"+me._entryFunctionSuffix, "do"+me._entryFunctionSuffix, "exports",
|
"var exports = {}, ZOTERO_TRANSLATOR_INFO = " + code,
|
||||||
"ZOTERO_TRANSLATOR_INFO"],
|
[
|
||||||
(translator.file ? translator.file.path : translator.label));
|
"detect" + this._entryFunctionSuffix,
|
||||||
me._translatorInfo = me._sandboxManager.sandbox.ZOTERO_TRANSLATOR_INFO;
|
"do" + this._entryFunctionSuffix,
|
||||||
}).catch(function(e) {
|
"exports",
|
||||||
me.complete(false, e);
|
"ZOTERO_TRANSLATOR_INFO"
|
||||||
});
|
],
|
||||||
},
|
(translator.file ? translator.file.path : translator.label)
|
||||||
|
);
|
||||||
|
this._translatorInfo = this._sandboxManager.sandbox.ZOTERO_TRANSLATOR_INFO;
|
||||||
|
}.bind(this);
|
||||||
|
|
||||||
|
if (this.noWait) {
|
||||||
|
try {
|
||||||
|
let codePromise = translator.getCode();
|
||||||
|
if (!codePromise.isResolved()) {
|
||||||
|
throw new Error("Code promise is not resolved in noWait mode");
|
||||||
|
}
|
||||||
|
parse(codePromise.value());
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
this.complete(false, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return translator.getCode()
|
||||||
|
.then(parse)
|
||||||
|
.catch(function(e) {
|
||||||
|
this.complete(false, e);
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a sandbox for scraping/scraper detection
|
* Generates a sandbox for scraping/scraper detection
|
||||||
|
|
|
@ -33,4 +33,28 @@ describe("Zotero.QuickCopy", function() {
|
||||||
assert.deepEqual(Zotero.QuickCopy.getFormatFromURL('chrome://zotero/content/tab.xul'), quickCopyPref);
|
assert.deepEqual(Zotero.QuickCopy.getFormatFromURL('chrome://zotero/content/tab.xul'), quickCopyPref);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("#getContentFromItems()", function () {
|
||||||
|
it("should generate BibTeX", function* () {
|
||||||
|
var item = yield createDataObject('item');
|
||||||
|
var content = "";
|
||||||
|
var worked = false;
|
||||||
|
|
||||||
|
var format = 'export=9cb70025-a888-4a29-a210-93ec52da40d4'; // BibTeX
|
||||||
|
Zotero.Prefs.set('export.quickCopy.setting', format);
|
||||||
|
// The translator code is loaded automatically on pref change, but let's not wait for it
|
||||||
|
yield Zotero.QuickCopy.init();
|
||||||
|
|
||||||
|
Zotero.QuickCopy.getContentFromItems(
|
||||||
|
[item],
|
||||||
|
format,
|
||||||
|
(obj, w) => {
|
||||||
|
content = obj.string;
|
||||||
|
worked = w;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert.isTrue(worked);
|
||||||
|
assert.isTrue(content.trim().startsWith('@'));
|
||||||
|
});
|
||||||
|
});
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user