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:
parent
34d66381d1
commit
26a5e2900a
2 changed files with 52 additions and 11 deletions
|
@ -1428,18 +1428,27 @@ var ItemTree = class ItemTree extends LibraryTree {
|
||||||
var openItemIDs = this._saveOpenState(true);
|
var openItemIDs = this._saveOpenState(true);
|
||||||
|
|
||||||
// Sort specific items
|
// Sort specific items
|
||||||
if (itemIDs) {
|
try {
|
||||||
let idsToSort = new Set(itemIDs);
|
if (itemIDs) {
|
||||||
this._rows.sort((a, b) => {
|
let idsToSort = new Set(itemIDs);
|
||||||
// Don't re-sort existing items. This assumes a stable sort(), which is the case in Firefox
|
this._rows.sort((a, b) => {
|
||||||
// but not Chrome/v8.
|
// Don't re-sort existing items. This assumes a stable sort(), which is the case in Firefox
|
||||||
if (!idsToSort.has(a.ref.id) && !idsToSort.has(b.ref.id)) return 0;
|
// but not Chrome/v8.
|
||||||
return rowSort(a, b) * order;
|
if (!idsToSort.has(a.ref.id) && !idsToSort.has(b.ref.id)) return 0;
|
||||||
});
|
return rowSort(a, b) * order;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Full sort
|
||||||
|
else {
|
||||||
|
this._rows.sort((a, b) => rowSort(a, b) * order);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Full sort
|
catch (e) {
|
||||||
else {
|
Zotero.logError("Error sorting fields: " + e.message);
|
||||||
this._rows.sort((a, b) => rowSort(a, b) * order);
|
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();
|
||||||
|
|
|
@ -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");
|
||||||
|
|
Loading…
Add table
Reference in a new issue