Better fix for errors from invalid sort fields

Just catch the error from `ItemTree::sort()` and clear the
secondary-sort and fallback-sort prefs so that sorting works on the next
attempt.

Replacement for a8ed30ce80

https://groups.google.com/g/zotero-dev/c/kc0-C6-SA74/m/bhHniGceAQAJ
This commit is contained in:
Dan Stillman 2022-07-11 01:28:15 -04:00
parent d747da7c65
commit d3942ad1f0
2 changed files with 52 additions and 11 deletions

View file

@ -1427,6 +1427,7 @@ var ItemTree = class ItemTree extends LibraryTree {
var openItemIDs = this._saveOpenState(true); var openItemIDs = this._saveOpenState(true);
// Sort specific items // Sort specific items
try {
if (itemIDs) { if (itemIDs) {
let idsToSort = new Set(itemIDs); let idsToSort = new Set(itemIDs);
this._rows.sort((a, b) => { this._rows.sort((a, b) => {
@ -1440,6 +1441,14 @@ var ItemTree = class ItemTree extends LibraryTree {
else { else {
this._rows.sort((a, b) => rowSort(a, b) * order); this._rows.sort((a, b) => rowSort(a, b) * order);
} }
}
catch (e) {
Zotero.logError("Error sorting fields: " + e.message);
Zotero.debug(e, 1);
// Clear anything that might be contributing to the error
Zotero.Prefs.clear('secondarySort.' + this.getSortField());
Zotero.Prefs.clear('fallbackSort');
}
this._refreshRowMap(); this._refreshRowMap();

View file

@ -151,6 +151,38 @@ describe("Zotero.ItemTree", function() {
}) })
}) })
describe("#sort()", function () {
it("should ignore invalid secondary-sort field", async function () {
await createDataObject('item', { title: 'A' });
await createDataObject('item', { title: 'A' });
// Set invalid field as secondary sort for title
Zotero.Prefs.set('secondarySort.title', 'invalidField');
// Sort by title
var colIndex = itemsView.tree._getColumns().findIndex(column => column.dataKey == 'title');
await itemsView.tree._columns.toggleSort(colIndex);
var e = await getPromiseError(zp.itemsView.sort());
assert.isFalse(e);
assert.isUndefined(Zotero.Prefs.get('secondarySort.title'));
});
it("should ignore invalid fallback-sort field", async function () {
Zotero.Prefs.clear('fallbackSort');
var originalFallback = Zotero.Prefs.get('fallbackSort');
Zotero.Prefs.set('fallbackSort', 'invalidField,' + originalFallback);
// Sort by title
var colIndex = itemsView.tree._getColumns().findIndex(column => column.dataKey == 'title');
await itemsView.tree._columns.toggleSort(colIndex);
var e = await getPromiseError(zp.itemsView.sort());
assert.isFalse(e);
assert.equal(Zotero.Prefs.get('fallbackSort'), originalFallback);
});
});
describe("#notify()", function () { describe("#notify()", function () {
beforeEach(function () { beforeEach(function () {
sinon.spy(win.ZoteroPane, "itemSelected"); sinon.spy(win.ZoteroPane, "itemSelected");