From fc4cd900cf1be1b3de82b9db8a81ff22222882dc Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Sun, 12 Dec 2021 17:10:11 +1100 Subject: [PATCH] tabs: intercept ctrl+click & 'open in desktop app' create a new tab instead of a new window --- repo/tabs/createWindow.cjs | 22 ++++++++++++++++++++++ repo/tabs/main.cjs | 17 +++++++++++++++++ repo/tabs/mod.json | 1 + repo/tray/createWindow.cjs | 1 + 4 files changed, 41 insertions(+) create mode 100644 repo/tabs/createWindow.cjs diff --git a/repo/tabs/createWindow.cjs b/repo/tabs/createWindow.cjs new file mode 100644 index 0000000..3651a5f --- /dev/null +++ b/repo/tabs/createWindow.cjs @@ -0,0 +1,22 @@ +/** + * notion-enhancer: tabs + * (c) 2021 dragonwocky (https://dragonwocky.me/) + * (https://notion-enhancer.github.io/) under the MIT license + */ + +'use strict'; + +module.exports = async function ({ env }, db, __exports, __eval) { + const { BrowserWindow } = require('electron'), + notionCreateWindow = __exports.createWindow; + __exports.createWindow = (relativeUrl = '', args) => { + const windows = BrowserWindow.getAllWindows(); + if (relativeUrl && windows.length) { + windows[0].webContents.send('notion-enhancer:open-tab', { + notionUrl: `notion://www.notion.so${relativeUrl}`, + }); + return windows[0]; + } + return notionCreateWindow(relativeUrl, args); + }; +}; diff --git a/repo/tabs/main.cjs b/repo/tabs/main.cjs index 395cf8e..472af05 100644 --- a/repo/tabs/main.cjs +++ b/repo/tabs/main.cjs @@ -11,4 +11,21 @@ module.exports = async function ({}, db, __exports, __eval) { electron.ipcMain.on('notion-enhancer:close-tab', (event, { window, id }) => { electron.webContents.fromId(window).send('notion-enhancer:close-tab', id); }); + + __eval(` + const notionHandleActivate = handleActivate; + handleActivate = (relativeUrl) => { + const { BrowserWindow } = require('electron'), + windows = BrowserWindow.getAllWindows(), + focusedWindow = BrowserWindow.getFocusedWindow(); + if (relativeUrl && windows.length) { + const win = focusedWindow || windows[0]; + win.webContents.send('notion-enhancer:open-tab', { + notionUrl: \`notion://www.notion.so\$\{relativeUrl\}\`, + }); + win.show(); + win.focus(); + } else notionHandleActivate(relativeUrl); + }; + `); }; diff --git a/repo/tabs/mod.json b/repo/tabs/mod.json index f58fb62..7a33105 100644 --- a/repo/tabs/mod.json +++ b/repo/tabs/mod.json @@ -21,6 +21,7 @@ "electron": [ { "source": "main.cjs", "target": "main/main.js" }, { "source": "systemMenu.cjs", "target": "main/systemMenu.js" }, + { "source": "createWindow.cjs", "target": "main/createWindow.js" }, { "source": "rendererIndex.cjs", "target": "renderer/index.js" } ] }, diff --git a/repo/tray/createWindow.cjs b/repo/tray/createWindow.cjs index 9b21c16..53fddad 100644 --- a/repo/tray/createWindow.cjs +++ b/repo/tray/createWindow.cjs @@ -48,6 +48,7 @@ module.exports = async function ({ env }, db, __exports, __eval) { windows[0].focus(); if (relativeUrl) { // handle requests passed via the notion:// protocol + // or ctrl+click windows[0].loadURL(urlHelpers.getIndexUrl(relativeUrl)); } return windows[0];