Skip to content

Commit

Permalink
replace electron openExternal with hacky DOM workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrynes7 committed Feb 2, 2024
1 parent 0e0c480 commit 2761862
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed all icons to be part of the Lucide set to align with Obsidian.
- The `title` part of the query is now optional.

### 🐛 Bug Fixes

- Opening tasks via the context menu should now work properly on mobile devices.

## [1.11.1] - 2023-04-09

### 🐛 Bug Fixes
Expand Down
42 changes: 17 additions & 25 deletions src/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,20 @@ export function showTaskContext(
.showAtPosition(position);
}

const openExternal: (url: string) => Promise<void> = async (url: string) => {
try {
await getElectronOpenExternal()(url);
} catch {
new Notice("Failed to open in external application.");
}
};

type OpenExternal = (url: string) => Promise<void>;

let electronOpenExternal: OpenExternal | undefined;

function getElectronOpenExternal(): OpenExternal {
if (electronOpenExternal) {
return electronOpenExternal;
}

try {
electronOpenExternal = require("electron").shell.openExternal;
} catch (e) {
electronOpenExternal = (url) => Promise.resolve();
}

return electronOpenExternal;
}
// A bit hacky, but in order to simulate clicking a link
// we create a unparented DOM element, dispatch an event,
// then remove the link. Using electron's openExternal doesn't
// work on mobile unfortunately.
function openExternal(url: string): void {
const link = document.createElement("a");
link.href = url;

const clickEvent = new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window
});

link.dispatchEvent(clickEvent);
link.remove();
}

0 comments on commit 2761862

Please sign in to comment.