Skip to content

Commit

Permalink
Merge pull request #3 from dolthub/liuliu/app-open-external-url-in-br…
Browse files Browse the repository at this point in the history
…owser

Liuliu/app: open external url in browser
  • Loading branch information
liuliu-dev authored Sep 23, 2024
2 parents d4aa9b1 + 3d7c212 commit c69760e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
24 changes: 24 additions & 0 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BrowserWindow,
ipcMain,
Menu,
shell,
utilityProcess,
UtilityProcess,
} from "electron";
Expand All @@ -28,6 +29,10 @@ if (isProd) {
let serverProcess: UtilityProcess | null;
let mainWindow: BrowserWindow;

function isExternalUrl(url: string) {
return !url.includes("localhost:") && !url.includes("app://");
}

function createGraphqlSeverProcess() {
const serverPath =
process.env.NODE_ENV === "production"
Expand Down Expand Up @@ -77,6 +82,25 @@ app.on("ready", () => {
await mainWindow.loadURL(`http://localhost:${port}`);
}, 2500);
}

// hit when middle-clicking buttons or <a href/> with a target set to _blank
// always deny, optionally redirect to browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
if (isExternalUrl(url)) {
shell.openExternal(url);
}

return { action: "deny" };
});

// hit when clicking <a href/> with no target
// optionally redirect to browser
mainWindow.webContents.on("will-navigate", (event, url) => {
if (isExternalUrl(url)) {
shell.openExternal(url);
event.preventDefault();
}
});
});

function updateMenu(databaseName?: string) {
Expand Down
46 changes: 46 additions & 0 deletions main/helpers/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BrowserWindow,
Menu,
MenuItemConstructorOptions,
app,
} from "electron";

export function initMenu(
Expand Down Expand Up @@ -139,6 +140,15 @@ export function initMenu(
],
enabled: hasChosenDatabase,
},
{
type: "separator",
},
{
label: "Run Query",
accelerator: "CmdOrCtrl+Q",
click: () => win.webContents.send("menu-clicked", "run-query"),
enabled: hasChosenDatabase,
},
],
},
{
Expand All @@ -161,5 +171,41 @@ export function initMenu(
],
},
];

const name = app.getName();
applicationMenu.unshift({
label: name,
submenu: [
{
role: "about",
},
{
type: "separator",
},
{
role: "services",
submenu: [],
},
{
type: "separator",
},
{
role: "hide",
},
{
role: "hideOthers",
},
{
role: "unhide",
},
{
type: "separator",
},
{
role: "quit",
},
],
});

return Menu.buildFromTemplate(applicationMenu);
}
26 changes: 21 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
"productName": "dolt-sql-workbench",
"version": "1.0.0",
"main": "app/background.js",
"workspaces": [
"graphql-server",
"web"
],
"workspaces": {
"packages": [
"graphql-server",
"web"
],
"nohoist": [
"graphql-server",
"graphql-server/**"
]
},
"packageManager": "[email protected]",
"scripts": {
"dev": "nextron",
Expand All @@ -28,7 +34,17 @@
],
"extraFiles": [
"graphql-server/dist/*",
"graphql-server/dist/**/*"
"graphql-server/dist/**/*",
"graphql-server/node_modules",
"!graphql-server/node_modules/@types",
"!graphql-server/node_modules/eslint",
"!graphql-server/node_modules/prettier",
"!graphql-server/node_modules/jest",
"!graphql-server/node_modules/yalc",
"!graphql-server/node_modules/webpack",
"!graphql-server/node_modules/eslint-*",
"!graphql-server/node_modules/ts-*",
"!graphql-server/node_modules/typescript"
],
"mac": {
"icon": "build/icon.icns",
Expand Down
6 changes: 6 additions & 0 deletions web/hooks/useElectronMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createTable,
newBranch,
newRelease,
query,
schemaDiagram,
upload,
} from "@lib/urls";
Expand Down Expand Up @@ -51,6 +52,11 @@ export default function useElectronMenu(params: DatabasePageParams) {
router.push(href, as).catch(console.error);
break;
}
case "run-query": {
const { href, as } = query(paramsWithRef);
router.push(href, as).catch(console.error);
break;
}
default:
break;
}
Expand Down

0 comments on commit c69760e

Please sign in to comment.