FileHandlers: Fall back when _getSystemHandler() fails

Continue to Zotero.launchFile() as intended.

https://forums.zotero.org/discussion/113179/pdfs-will-not-open-in-system-reader-when-double-clicked
This commit is contained in:
Abe Jellinek 2024-03-27 13:23:54 -04:00
parent 54b9ff6e8a
commit 63f54d3184
2 changed files with 45 additions and 9 deletions

View file

@ -90,17 +90,19 @@ Zotero.FileHandlers = {
// If there are handlers for this platform and this reader type...
if (handlers) {
// First try to open with the custom handler
try {
for (let [i, { name, open }] of handlers.entries()) {
if (name.test(handler)) {
Zotero.debug('Opening with handler ' + i);
await open(handler, { filePath: path, location, page });
return true;
if (handler) {
try {
for (let [i, { name, open }] of handlers.entries()) {
if (name.test(handler)) {
Zotero.debug('Opening with handler ' + i);
await open(handler, { filePath: path, location, page });
return true;
}
}
}
}
catch (e) {
Zotero.logError(e);
catch (e) {
Zotero.logError(e);
}
}
// If we get here, we don't have special handling for the custom

View file

@ -109,5 +109,39 @@ describe("Zotero.FileHandlers", () => {
readerOpenSpy.restore();
getSystemHandlerStub.restore();
});
it("should fall back when handler is set to system and we can't retrieve the system handler", async function () {
let pdf = await importFileAttachment('wonderland_short.pdf');
let wasRun = false;
let readerOpenSpy = sinon.spy(Zotero.Reader, 'open');
let launchFileStub = sinon.stub(Zotero, 'launchFile');
Zotero.FileHandlers._mockHandlers = {
pdf: [
{
name: new RegExp(''),
async open() {
wasRun = true;
}
}
]
};
// Set our custom handler to something nonexistent,
// and stub the system handler to something nonexistent as well
Zotero.Prefs.set('fileHandler.pdf', 'system');
let getSystemHandlerStub = sinon.stub(Zotero.FileHandlers, '_getSystemHandler');
getSystemHandlerStub.returns(false);
await Zotero.FileHandlers.open(pdf, { location: {} });
assert.isFalse(wasRun);
assert.isFalse(readerOpenSpy.called);
assert.isTrue(launchFileStub.called);
assert.notOk(Zotero.Reader.getByTabID(win.Zotero_Tabs.selectedID));
assert.isEmpty(Zotero.Reader.getWindowStates());
readerOpenSpy.restore();
launchFileStub.restore();
getSystemHandlerStub.restore();
});
});
});