ESMify/asyncify preference panes

This commit is contained in:
Abe Jellinek 2025-07-01 12:05:04 -04:00
parent e748e705be
commit aafad2a880
4 changed files with 71 additions and 71 deletions

View file

@ -23,6 +23,7 @@
***** END LICENSE BLOCK *****
*/
const { ZOTERO_CONFIG } = ChromeUtils.importESModule('resource://zotero/config.mjs');
var { FilePicker } = ChromeUtils.importESModule('chrome://zotero/content/modules/filePicker.mjs');
Zotero_Preferences.Advanced = {
@ -66,8 +67,8 @@ Zotero_Preferences.Advanced = {
},
updateTranslators: Zotero.Promise.coroutine(function* () {
var updated = yield Zotero.Schema.updateFromRepository(Zotero.Schema.REPO_UPDATE_MANUAL);
updateTranslators: async function () {
var updated = await Zotero.Schema.updateFromRepository(Zotero.Schema.REPO_UPDATE_MANUAL);
var button = document.getElementById('updateButton');
if (button) {
if (updated===-1) {
@ -82,13 +83,13 @@ Zotero_Preferences.Advanced = {
button.setAttribute('label', label);
if (updated && Zotero_Preferences.Cite) {
yield Zotero_Preferences.Cite.refreshStylesList();
await Zotero_Preferences.Cite.refreshStylesList();
}
}
}),
},
migrateDataDirectory: Zotero.Promise.coroutine(function* () {
migrateDataDirectory: async function () {
var currentDir = Zotero.DataDirectory.dir;
var defaultDir = Zotero.DataDirectory.defaultDir;
if (currentDir == defaultDir) {
@ -96,24 +97,23 @@ Zotero_Preferences.Advanced = {
return;
}
Cu.import("resource://zotero/config.js")
var ps = Services.prompt;
// If there's a migration marker, point data directory back to the current location and remove
// it to trigger the migration again
var marker = PathUtils.join(defaultDir, Zotero.DataDirectory.MIGRATION_MARKER);
if (yield IOUtils.exists(marker)) {
if (await IOUtils.exists(marker)) {
Zotero.Prefs.clear('dataDir');
Zotero.Prefs.clear('useDataDir');
yield IOUtils.remove(marker);
await IOUtils.remove(marker);
try {
yield IOUtils.remove(PathUtils.join(defaultDir, '.DS_Store'));
await IOUtils.remove(PathUtils.join(defaultDir, '.DS_Store'));
}
catch (e) {}
}
// ~/Zotero exists and is non-empty
if ((yield IOUtils.exists(defaultDir)) && !(yield Zotero.File.directoryIsEmpty(defaultDir))) {
if (((await IOUtils.exists(defaultDir))) && !((await Zotero.File.directoryIsEmpty(defaultDir)))) {
let buttonFlags = (ps.BUTTON_POS_0) * (ps.BUTTON_TITLE_IS_STRING)
+ (ps.BUTTON_POS_1) * (ps.BUTTON_TITLE_CANCEL);
let index = ps.confirmEx(
@ -127,7 +127,7 @@ Zotero_Preferences.Advanced = {
null, null, null, {}
);
if (index == 0) {
yield Zotero.File.reveal(
await Zotero.File.reveal(
// Windows opens the directory, which might be confusing here, so open parent instead
Zotero.isWin ? PathUtils.parent(defaultDir) : defaultDir
);
@ -138,7 +138,7 @@ Zotero_Preferences.Advanced = {
var additionalText = '';
if (Zotero.isWin) {
try {
let numItems = yield Zotero.DB.valueQueryAsync(
let numItems = await Zotero.DB.valueQueryAsync(
"SELECT COUNT(*) FROM itemAttachments WHERE linkMode IN (?, ?)",
[Zotero.Attachments.LINK_MODE_IMPORTED_FILE, Zotero.Attachments.LINK_MODE_IMPORTED_URL]
);
@ -172,10 +172,10 @@ Zotero_Preferences.Advanced = {
);
if (index == 0) {
yield Zotero.DataDirectory.markForMigration(currentDir);
await Zotero.DataDirectory.markForMigration(currentDir);
Zotero.Utilities.Internal.quitZotero(true);
}
}),
},
runIntegrityCheck: async function (button) {
@ -367,23 +367,23 @@ Zotero_Preferences.Advanced = {
},
dataDirUpdate: Zotero.Promise.coroutine(function* (isCustomSelection) {
dataDirUpdate: async function (isCustomSelection) {
if (!isCustomSelection && this._usingDefaultDataDir()) return;
// This call shows a filepicker if needed, forces a restart if required, and does nothing if
// cancel was pressed or value hasn't changed
yield Zotero.DataDirectory.choose(
await Zotero.DataDirectory.choose(
true,
!isCustomSelection,
() => Zotero.launchURL('https://www.zotero.org/support/zotero_data')
);
}),
},
setDataDirInput: async function () {
var filefield = document.getElementById('data-dir-path');
var path = Zotero.Prefs.get('dataDir');
if (path && await IOUtils.exists(path)) {
if (path && (await IOUtils.exists(path))) {
filefield.style.backgroundImage = 'url(moz-icon://' + Zotero.File.pathToFileURI(path) + '?size=16)';
filefield.value = path;
}
@ -422,8 +422,8 @@ Zotero_Preferences.Advanced = {
return false;
},
updateIndexStats: Zotero.Promise.coroutine(function* () {
var stats = yield Zotero.Fulltext.getIndexStats();
updateIndexStats: async function () {
var stats = await Zotero.Fulltext.getIndexStats();
document.getElementById('fulltext-stats-indexed')
.setAttribute('value', stats.indexed);
document.getElementById('fulltext-stats-partial')
@ -432,7 +432,7 @@ Zotero_Preferences.Advanced = {
.setAttribute('value', stats.unindexed);
document.getElementById('fulltext-stats-words')
.setAttribute('value', stats.words);
}),
},
rebuildIndexPrompt: async function () {
@ -572,7 +572,7 @@ Zotero_Preferences.Attachment_Base_Directory = {
}
fp.init(window, Zotero.getString('attachmentBasePath.selectDir'), fp.modeGetFolder);
fp.appendFilters(fp.filterAll);
if (await fp.show() != fp.returnOK) {
if ((await fp.show()) != fp.returnOK) {
return false;
}
var newPath = PathUtils.normalize(fp.file);
@ -743,14 +743,14 @@ Zotero_Preferences.Attachment_Base_Directory = {
},
clearPath: Zotero.Promise.coroutine(function* () {
clearPath: async function () {
// Find all current attachments with relative paths
var sql = "SELECT itemID FROM itemAttachments WHERE linkMode=? AND path LIKE ?";
var params = [
Zotero.Attachments.LINK_MODE_LINKED_FILE,
Zotero.Attachments.BASE_PATH_PLACEHOLDER + "%"
];
var relativeAttachmentIDs = yield Zotero.DB.columnQueryAsync(sql, params);
var relativeAttachmentIDs = await Zotero.DB.columnQueryAsync(sql, params);
// Prompt for confirmation
var ps = Services.prompt;
@ -794,7 +794,7 @@ Zotero_Preferences.Attachment_Base_Directory = {
Zotero.debug('Clearing base directory');
Zotero.Prefs.set('saveRelativeAttachmentPath', false);
yield Zotero.Utilities.Internal.forEachChunkAsync(
await Zotero.Utilities.Internal.forEachChunkAsync(
relativeAttachmentIDs,
100,
function (chunk) {
@ -811,13 +811,13 @@ Zotero_Preferences.Attachment_Base_Directory = {
);
Zotero.Prefs.set('baseAttachmentPath', '');
}),
},
updateUI: async function () {
var filefield = document.getElementById('baseAttachmentPath');
var path = Zotero.Prefs.get('baseAttachmentPath');
if (path && await IOUtils.exists(path)) {
if (path && (await IOUtils.exists(path))) {
filefield.style.backgroundImage = 'url(moz-icon://' + Zotero.File.pathToFileURI(path) + '?size=16)';
filefield.value = path;
}

View file

@ -182,7 +182,7 @@ Zotero_Preferences.Cite = {
/**
* Deletes selected styles from the styles pane
**/
deleteStyle: Zotero.Promise.coroutine(function* () {
deleteStyle: async function() {
// get selected cslIDs
var styles = Zotero.Styles.getVisible();
var cslIDs = [];
@ -203,16 +203,16 @@ Zotero_Preferences.Cite = {
if(ps.confirm(null, '', text)) {
// delete if requested
if(cslIDs.length == 1) {
yield selectedStyle.remove();
await selectedStyle.remove();
} else {
for(var i=0; i<cslIDs.length; i++) {
yield Zotero.Styles.get(cslIDs[i]).remove();
await Zotero.Styles.get(cslIDs[i]).remove();
}
}
yield this.refreshStylesList();
await this.refreshStylesList();
}
}),
},
/**

View file

@ -54,13 +54,13 @@ Zotero_Preferences.Export = {
/*
* Builds the main Quick Copy drop-down from the current global pref
*/
populateQuickCopyList: Zotero.Promise.coroutine(function* () {
populateQuickCopyList: async function() {
// Initialize default format drop-down
var format = Zotero.Prefs.get("export.quickCopy.setting");
format = Zotero.QuickCopy.unserializeSetting(format);
var menulist = document.getElementById("zotero-quickCopy-menu");
yield Zotero.Styles.init();
var translators = yield this.getQuickCopyTranslators();
await Zotero.Styles.init();
var translators = await this.getQuickCopyTranslators();
this.buildQuickCopyFormatDropDown(
menulist, format.contentType, format, translators
);
@ -75,8 +75,8 @@ Zotero_Preferences.Export = {
});
localeMenulist.setAttribute('preference', "extensions.zotero.export.quickCopy.locale");
yield this.refreshQuickCopySiteList();
}),
await this.refreshQuickCopySiteList();
},
/*
@ -517,13 +517,13 @@ Zotero_Preferences.Export = {
},
deleteSelectedQuickCopySite: Zotero.Promise.coroutine(function* () {
deleteSelectedQuickCopySite: async function() {
var domainPath = this._rows[this._tree.selection.focused].domain;
yield Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domainPath]);
yield Zotero.QuickCopy.loadSiteSettings();
yield this.refreshQuickCopySiteList();
await Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domainPath]);
await Zotero.QuickCopy.loadSiteSettings();
await this.refreshQuickCopySiteList();
this.updateQuickCopySiteButtons();
}),
},
updateQuickCopyInstructions: function () {

View file

@ -24,8 +24,8 @@
*/
"use strict";
Components.utils.import("resource://zotero/config.js");
const { ZOTERO_CONFIG } = ChromeUtils.importESModule('resource://zotero/config.mjs');
var React = require('react');
var ReactDOM = require('react-dom');
var VirtualizedTable = require('components/virtualized-table');
@ -120,7 +120,7 @@ Zotero_Preferences.Sync = {
_secmodDeleted: false,
linkAccount: Zotero.Promise.coroutine(function* (event) {
linkAccount: async function(event) {
this.trimUsername();
var username = document.getElementById('sync-username-textbox').value;
var password = document.getElementById('sync-password').value;
@ -133,7 +133,7 @@ Zotero_Preferences.Sync = {
// Try to acquire API key with current credentials
this.updateSyncIndicator('animated');
try {
var json = yield Zotero.Sync.Runner.createAPIKeyFromCredentials(username, password);
var json = await Zotero.Sync.Runner.createAPIKeyFromCredentials(username, password);
}
catch (e) {
// On "User canceled primary password entry", delete secmod.db and restart
@ -144,7 +144,7 @@ Zotero_Preferences.Sync = {
Zotero.logError(e);
let profileDir = Zotero.Profile.dir;
let secmodPath = PathUtils.join(profileDir, 'secmod.db');
if (!this._secmodDeleted && !(yield IOUtils.exists(secmodPath))) {
if (!this._secmodDeleted && !((await IOUtils.exists(secmodPath)))) {
Zotero.debug("secmod.db doesn't exist");
setTimeout(function () {
Zotero.Sync.Runner.alert(e);
@ -152,7 +152,7 @@ Zotero_Preferences.Sync = {
throw e;
}
Zotero.debug("Deleting secmod.db", 2);
yield IOUtils.remove(secmodPath);
await IOUtils.remove(secmodPath);
// Once we've deleted, keep showing the restart message
this._secmodDeleted = true;
@ -189,7 +189,7 @@ Zotero_Preferences.Sync = {
return;
}
var ok = yield Zotero.Sync.Data.Local.checkUser(
var ok = await Zotero.Sync.Data.Local.checkUser(
window,
json.userID,
json.username,
@ -215,7 +215,7 @@ Zotero_Preferences.Sync = {
});
this.displayFields(json.username);
}),
},
/**
* Updates the auth indicator icon, depending on status
@ -230,7 +230,7 @@ Zotero_Preferences.Sync = {
}
},
unlinkAccount: Zotero.Promise.coroutine(function* (showAlert=true) {
unlinkAccount: async function(showAlert=true) {
if (showAlert) {
var check = {value: false};
var ps = Services.prompt;
@ -248,9 +248,9 @@ Zotero_Preferences.Sync = {
if (index == 0) {
if (check.value) {
var resetDataDirFile = PathUtils.join(Zotero.DataDirectory.dir, 'reset-data-directory');
yield Zotero.File.putContentsAsync(resetDataDirFile, '');
await Zotero.File.putContentsAsync(resetDataDirFile, '');
yield Zotero.Sync.Runner.deleteAPIKey();
await Zotero.Sync.Runner.deleteAPIKey();
Zotero.Prefs.clear('sync.server.username');
return Zotero.Utilities.Internal.quitZotero(true);
}
@ -261,8 +261,8 @@ Zotero_Preferences.Sync = {
this.displayFields();
Zotero.Prefs.clear('sync.librariesToSync');
yield Zotero.Sync.Runner.deleteAPIKey();
}),
await Zotero.Sync.Runner.deleteAPIKey();
},
showLibrariesToSyncDialog: function() {
@ -437,7 +437,7 @@ Zotero_Preferences.Sync = {
},
updateStorageSettingsUI: Zotero.Promise.coroutine(function* () {
updateStorageSettingsUI: async function() {
this.unverifyStorageServer();
var protocol = Zotero.Prefs.get('sync.storage.protocol');
@ -459,7 +459,7 @@ Zotero_Preferences.Sync = {
document.getElementById('storage-user-download-mode').disabled = !enabled;
this.updateStorageTerms();
}),
},
updateStorageSettingsGroupsUI: function () {
@ -482,19 +482,19 @@ Zotero_Preferences.Sync = {
},
onStorageSettingsKeyPress: Zotero.Promise.coroutine(function* (event) {
onStorageSettingsKeyPress: async function(event) {
if (event.keyCode == 13) {
yield this.verifyStorageServer();
await this.verifyStorageServer();
}
}),
},
onStorageSettingsChange: Zotero.Promise.coroutine(function* () {
onStorageSettingsChange: async function() {
var oldProtocol = this._lastStorageProtocol;
var oldURL = this._lastStorageURL;
// Necessary for pref to update
yield Zotero.Promise.delay(1);
await Zotero.Promise.delay(1);
var newProtocol = Zotero.Prefs.get('sync.storage.protocol');
var newURL = Zotero.Prefs.get('sync.storage.url').trim()
@ -503,7 +503,7 @@ Zotero_Preferences.Sync = {
Zotero.Prefs.set('sync.storage.url', newURL);
if (oldProtocol != newProtocol || oldURL != newURL) {
yield Zotero.Sync.Storage.Local.resetAllSyncStates(Zotero.Libraries.userLibraryID);
await Zotero.Sync.Storage.Local.resetAllSyncStates(Zotero.Libraries.userLibraryID);
}
if (oldProtocol == 'webdav') {
@ -517,7 +517,7 @@ Zotero_Preferences.Sync = {
var password = document.getElementById('storage-password').value;
if (username) {
// Get a new controller
yield Zotero.Sync.Runner.getStorageController('webdav').setPassword(password);
await Zotero.Sync.Runner.getStorageController('webdav').setPassword(password);
}
}
@ -536,10 +536,10 @@ Zotero_Preferences.Sync = {
if (index == 0) {
var sql = "INSERT OR IGNORE INTO settings VALUES (?,?,?)";
yield Zotero.DB.queryAsync(sql, ['storage', 'zfsPurge', 'user']);
await Zotero.DB.queryAsync(sql, ['storage', 'zfsPurge', 'user']);
try {
yield Zotero.Sync.Storage.ZFS.purgeDeletedStorageFiles();
await Zotero.Sync.Storage.ZFS.purgeDeletedStorageFiles();
Services.prompt.alert(
null,
Zotero.getString("general.success"),
@ -560,14 +560,14 @@ Zotero_Preferences.Sync = {
this.updateStorageSettingsUI();
this.storeLastStorageSettings();
}),
},
verifyStorageServer: Zotero.Promise.coroutine(function* () {
verifyStorageServer: async function() {
// onchange weirdly isn't triggered when clicking straight from a field to the button,
// so we have to trigger this here (and we don't trigger it for Enter in
// onStorageSettingsKeyPress()).
yield this.onStorageSettingsChange();
await this.onStorageSettingsChange();
Zotero.debug("Verifying storage");
@ -593,7 +593,7 @@ Zotero_Preferences.Sync = {
var controller = Zotero.Sync.Runner.getStorageController('webdav');
try {
yield controller.checkServer({
await controller.checkServer({
// Get the XMLHttpRequest for possible cancelling
onRequest: r => request = r
})
@ -617,7 +617,7 @@ Zotero_Preferences.Sync = {
break;
}
}
success = yield controller.handleVerificationError(e);
success = await controller.handleVerificationError(e);
}
finally {
verifyButton.hidden = false;
@ -648,7 +648,7 @@ Zotero_Preferences.Sync = {
progressMeter.hidden = true;
}
}
}),
},
unverifyStorageServer: function () {