Fix Quick Copy drag with export format

This commit is contained in:
Dan Stillman 2017-03-06 23:52:24 -05:00
parent 9e59500619
commit dd5ae0f49c
2 changed files with 31 additions and 18 deletions

View File

@ -29,40 +29,43 @@ Zotero.QuickCopy = new function() {
var _initTimeoutID var _initTimeoutID
var _initPromise; var _initPromise;
var _initialized = false; var _initialized = false;
var _initCancelled = false;
var _siteSettings; var _siteSettings;
var _formattedNames; var _formattedNames;
this.init = Zotero.Promise.coroutine(function* () { this.init = Zotero.Promise.coroutine(function* () {
Zotero.debug("Initializing Quick Copy");
yield this.loadSiteSettings(); yield this.loadSiteSettings();
if (!_initialized) {
// Make sure export translator code is loaded whenever the output format changes
Zotero.Prefs.registerObserver("export.quickCopy.setting", _loadOutputFormat);
_initialized = true;
}
// Load code for selected export translator ahead of time // Load code for selected export translator ahead of time
// (in the background, because it requires translator initialization) // (in the background, because it requires translator initialization)
_initTimeoutID = setTimeout(() => { Zotero.Schema.schemaUpdatePromise
.then(function () {
if (_initCancelled) return;
// Avoid random translator initialization during tests, which can result in timeouts, // Avoid random translator initialization during tests, which can result in timeouts,
// if an export format is selected // if an export format is selected
if (Zotero.test) return; if (Zotero.test) return;
_initTimeoutID = null; _initPromise = _loadOutputFormat();
_initPromise = _loadOutputFormat().then(() => _initPromise = null); });
}, 5000);
if (!_initialized) {
Zotero.Prefs.registerObserver("export.quickCopy.setting", () => _loadOutputFormat());
_initialized = true;
}
}); });
this.uninit = function () { this.uninit = function () {
// Cancel load if not yet done _initCancelled = true;
if (_initTimeoutID) {
clearTimeout(_initTimeoutID);
_initTimeoutID = null
}
// Cancel load if in progress // Cancel load if in progress
if (_initPromise) { if (_initPromise) {
_initPromise.cancel(); _initPromise.cancel();
} }
Zotero.Prefs.unregisterObserver("export.quickCopy.setting", _loadOutputFormat);
}; };
@ -451,6 +454,7 @@ Zotero.QuickCopy = new function() {
var format = Zotero.Prefs.get("export.quickCopy.setting"); var format = Zotero.Prefs.get("export.quickCopy.setting");
format = Zotero.QuickCopy.unserializeSetting(format); format = Zotero.QuickCopy.unserializeSetting(format);
if (format.mode == 'export') { if (format.mode == 'export') {
Zotero.debug("Preloading code for Quick Copy export format");
yield Zotero.Translators.init(); yield Zotero.Translators.init();
let translator = Zotero.Translators.get(format.id); let translator = Zotero.Translators.get(format.id);
translator.cacheCode = true; translator.cacheCode = true;

View File

@ -41,16 +41,25 @@ Zotero.Translators = new function() {
* available (e.g., in updateBundledFiles()), to avoid unnecesary file reads * available (e.g., in updateBundledFiles()), to avoid unnecesary file reads
*/ */
this.init = Zotero.Promise.coroutine(function* (options = {}) { this.init = Zotero.Promise.coroutine(function* (options = {}) {
if (_initializationDeferred && !options.reinit) {
return _initializationDeferred.promise;
}
// Wait until bundled files have been updated, except when this is called by the schema update // Wait until bundled files have been updated, except when this is called by the schema update
// code itself // code itself
if (!options.fromSchemaUpdate) { if (!options.fromSchemaUpdate) {
yield Zotero.Schema.schemaUpdatePromise; yield Zotero.Schema.schemaUpdatePromise;
} }
// If an initialization has already started, a regular init() call should return the promise
// for that (which may already be resolved). A reinit should yield on that but then continue
// with reinitialization.
if (_initializationDeferred) {
let promise = _initializationDeferred.promise;
if (options.reinit) {
yield promise;
}
else {
return promise;
}
}
_initializationDeferred = Zotero.Promise.defer(); _initializationDeferred = Zotero.Promise.defer();
Zotero.debug("Initializing translators"); Zotero.debug("Initializing translators");