Add support for opening web links in the default browser (#221)

* Add support for opening web links in the default browser

* require modifier key to click

* undo emain change

* remove newline
This commit is contained in:
Evan Simkowitz 2024-01-11 20:49:13 -05:00 committed by GitHub
parent ea3be8115a
commit ed2a345555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 1 deletions

View File

@ -36,7 +36,8 @@
"tsx-control-statements": "^4.1.1", "tsx-control-statements": "^4.1.1",
"uuid": "^9.0.0", "uuid": "^9.0.0",
"winston": "^3.8.2", "winston": "^3.8.2",
"xterm": "^5.0.0" "xterm": "^5.0.0",
"xterm-addon-web-links": "^0.9.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.17.10", "@babel/cli": "^7.17.10",

View File

@ -485,6 +485,14 @@ electron.ipcMain.on("reload-window", (event) => {
return; return;
}); });
electron.ipcMain.on("open-external-link", async (_, url) => {
try {
await electron.shell.openExternal(url);
} catch (err) {
console.warn("error opening external link", err);
}
});
electron.ipcMain.on("get-last-logs", async (event, numberOfLines) => { electron.ipcMain.on("get-last-logs", async (event, numberOfLines) => {
try { try {
const logPath = path.join(getWaveHomeDir(), "wavesrv.log"); const logPath = path.join(getWaveHomeDir(), "wavesrv.log");

View File

@ -12,6 +12,7 @@ contextBridge.exposeInMainWorld("api", {
}, },
restartWaveSrv: () => ipcRenderer.sendSync("restart-server"), restartWaveSrv: () => ipcRenderer.sendSync("restart-server"),
reloadWindow: () => ipcRenderer.sendSync("reload-window"), reloadWindow: () => ipcRenderer.sendSync("reload-window"),
openExternalLink: (url) => ipcRenderer.send("open-external-link", url),
onTCmd: (callback) => ipcRenderer.on("t-cmd", callback), onTCmd: (callback) => ipcRenderer.on("t-cmd", callback),
onICmd: (callback) => ipcRenderer.on("i-cmd", callback), onICmd: (callback) => ipcRenderer.on("i-cmd", callback),
onLCmd: (callback) => ipcRenderer.on("l-cmd", callback), onLCmd: (callback) => ipcRenderer.on("l-cmd", callback),

View File

@ -196,6 +196,7 @@ type ElectronApi = {
getWaveSrvStatus: () => boolean; getWaveSrvStatus: () => boolean;
restartWaveSrv: () => boolean; restartWaveSrv: () => boolean;
reloadWindow: () => void; reloadWindow: () => void;
openExternalLink: (url: string) => void;
onTCmd: (callback: (mods: KeyModsType) => void) => void; onTCmd: (callback: (mods: KeyModsType) => void) => void;
onICmd: (callback: (mods: KeyModsType) => void) => void; onICmd: (callback: (mods: KeyModsType) => void) => void;
onLCmd: (callback: (mods: KeyModsType) => void) => void; onLCmd: (callback: (mods: KeyModsType) => void) => void;
@ -3424,6 +3425,16 @@ class Model {
getApi().reloadWindow(); getApi().reloadWindow();
} }
/**
* Opens a new default browser window to the given url
* @param {string} url The url to open
*/
openExternalLink(url: string): void {
console.log("opening external link: " + url);
getApi().openExternalLink(url);
console.log("finished opening external link");
}
refocus() { refocus() {
// givefocus() give back focus to cmd or input // givefocus() give back focus to cmd or input
let activeScreen = this.getActiveScreen(); let activeScreen = this.getActiveScreen();

View File

@ -3,10 +3,13 @@
import * as mobx from "mobx"; import * as mobx from "mobx";
import { Terminal } from "xterm"; import { Terminal } from "xterm";
//TODO: replace with `@xterm/addon-web-links` when it's available as stable
import { WebLinksAddon } from "xterm-addon-web-links";
import { sprintf } from "sprintf-js"; import { sprintf } from "sprintf-js";
import { boundMethod } from "autobind-decorator"; import { boundMethod } from "autobind-decorator";
import { windowWidthToCols, windowHeightToRows } from "../../util/textmeasure"; import { windowWidthToCols, windowHeightToRows } from "../../util/textmeasure";
import { boundInt } from "../../util/util"; import { boundInt } from "../../util/util";
import { GlobalModel } from "../../model/model"
import type { import type {
TermContextUnion, TermContextUnion,
TermOptsType, TermOptsType,
@ -96,6 +99,21 @@ class TermWrap {
fontFamily: "JetBrains Mono", fontFamily: "JetBrains Mono",
theme: { foreground: terminal.foreground, background: terminal.background }, theme: { foreground: terminal.foreground, background: terminal.background },
}); });
this.terminal.loadAddon(new WebLinksAddon((e, uri) => {
e.preventDefault();
switch (GlobalModel.platform) {
case "darwin":
if (e.metaKey) {
GlobalModel.openExternalLink(uri);
}
break;
default:
if (e.ctrlKey) {
GlobalModel.openExternalLink(uri);
}
break;
}
}));
this.terminal._core._inputHandler._parser.setErrorHandler((state) => { this.terminal._core._inputHandler._parser.setErrorHandler((state) => {
this.numParseErrors++; this.numParseErrors++;
return state; return state;

View File

@ -8466,6 +8466,11 @@ xmlbuilder@^15.1.1:
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-15.1.1.tgz#9dcdce49eea66d8d10b42cae94a79c3c8d0c2ec5" resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-15.1.1.tgz#9dcdce49eea66d8d10b42cae94a79c3c8d0c2ec5"
integrity sha512-yMqGBqtXyeN1e3TGYvgNgDVZ3j84W4cwkOXQswghol6APgZWaff9lnbvN7MHYJOiXsvGPXtjTYJEiC9J2wv9Eg== integrity sha512-yMqGBqtXyeN1e3TGYvgNgDVZ3j84W4cwkOXQswghol6APgZWaff9lnbvN7MHYJOiXsvGPXtjTYJEiC9J2wv9Eg==
xterm-addon-web-links@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/xterm-addon-web-links/-/xterm-addon-web-links-0.9.0.tgz#c65b18588d1f613e703eb6feb7f129e7ff1c63e7"
integrity sha512-LIzi4jBbPlrKMZF3ihoyqayWyTXAwGfu4yprz1aK2p71e9UKXN6RRzVONR0L+Zd+Ik5tPVI9bwp9e8fDTQh49Q==
xterm@^5.0.0: xterm@^5.0.0:
version "5.3.0" version "5.3.0"
resolved "https://registry.yarnpkg.com/xterm/-/xterm-5.3.0.tgz#867daf9cc826f3d45b5377320aabd996cb0fce46" resolved "https://registry.yarnpkg.com/xterm/-/xterm-5.3.0.tgz#867daf9cc826f3d45b5377320aabd996cb0fce46"