diff --git a/repo/tabs/createWindow.cjs b/repo/tabs/createWindow.cjs index 3651a5f..bda9a95 100644 --- a/repo/tabs/createWindow.cjs +++ b/repo/tabs/createWindow.cjs @@ -6,16 +6,17 @@ 'use strict'; -module.exports = async function ({ env }, db, __exports, __eval) { - const { BrowserWindow } = require('electron'), - notionCreateWindow = __exports.createWindow; +module.exports = async function (api, db, __exports, __eval) { + const notionCreateWindow = __exports.createWindow; __exports.createWindow = (relativeUrl = '', args) => { - const windows = BrowserWindow.getAllWindows(); - if (relativeUrl && windows.length) { - windows[0].webContents.send('notion-enhancer:open-tab', { + const windows = api.electron.getNotionWindows(); + // '/' is used to create new windows intentionally + if (relativeUrl && relativeUrl !== '/' && windows.length) { + const window = api.electron.getFocusedNotionWindow() || windows[0]; + window.webContents.send('notion-enhancer:open-tab', { notionUrl: `notion://www.notion.so${relativeUrl}`, }); - return windows[0]; + return window; } return notionCreateWindow(relativeUrl, args); }; diff --git a/repo/tabs/main.cjs b/repo/tabs/main.cjs index 472af05..9900eee 100644 --- a/repo/tabs/main.cjs +++ b/repo/tabs/main.cjs @@ -15,16 +15,22 @@ module.exports = async function ({}, db, __exports, __eval) { __eval(` const notionHandleActivate = handleActivate; handleActivate = (relativeUrl) => { - const { BrowserWindow } = require('electron'), - windows = BrowserWindow.getAllWindows(), - focusedWindow = BrowserWindow.getFocusedWindow(); + const api = require('notion-enhancer/api/index.cjs'), + { BrowserWindow } = require('electron'), + windows = api.electron.getNotionWindows(), + electronWindows = BrowserWindow.getAllWindows(); if (relativeUrl && windows.length) { - const win = focusedWindow || windows[0]; + const win = api.electron.getFocusedNotionWindow() || windows[0]; win.webContents.send('notion-enhancer:open-tab', { notionUrl: \`notion://www.notion.so\$\{relativeUrl\}\`, }); win.show(); win.focus(); + } else if (relativeUrl && electronWindows.length && !windows.length) { + // enhancer menu is open: prevent override + const { createWindow } = api.electron.notionRequire('main/createWindow'), + win = createWindow(relativeUrl); + win.focus(); } else notionHandleActivate(relativeUrl); }; `); diff --git a/repo/tabs/tab.cjs b/repo/tabs/tab.cjs index 350f27c..23abd5b 100644 --- a/repo/tabs/tab.cjs +++ b/repo/tabs/tab.cjs @@ -8,10 +8,11 @@ let focusedTab; -module.exports = async function ({ components, env, web, fmt, fs }, db, tabCache = new Map()) { - const electron = require('electron'), +module.exports = async function (api, db, tabCache = new Map()) { + const { components, web, fmt, fs } = api, + electron = require('electron'), electronWindow = electron.remote.getCurrentWindow(), - notionIpc = env.notionRequire('helpers/notionIpc'), + notionIpc = api.electron.notionRequire('helpers/notionIpc'), xIcon = await components.feather('x'); return class Tab { diff --git a/repo/tray/createWindow.cjs b/repo/tray/createWindow.cjs index 53fddad..4589738 100644 --- a/repo/tray/createWindow.cjs +++ b/repo/tray/createWindow.cjs @@ -6,9 +6,9 @@ 'use strict'; -module.exports = async function ({ env }, db, __exports, __eval) { +module.exports = async function (api, db, __exports, __eval) { const electron = require('electron'), - urlHelpers = env.notionRequire('helpers/urlHelpers'), + urlHelpers = api.electron.notionRequire('helpers/urlHelpers'), runInBackground = await db.get(['run_in_background']); if (!runInBackground) return; @@ -19,7 +19,7 @@ module.exports = async function ({ env }, db, __exports, __eval) { const notionCreateWindow = __exports.createWindow; __exports.createWindow = (relativeUrl = '', args) => { - const windows = electron.BrowserWindow.getAllWindows(); + const windows = api.electron.getNotionWindows(); if (windows.length) windows.forEach((win) => win.show()); if (relativeUrl || !windows.length) { @@ -44,14 +44,15 @@ module.exports = async function ({ env }, db, __exports, __eval) { return window; } else { + const window = api.electron.getFocusedNotionWindow() || windows[0]; // prevents duplicate windows on dock/taskbar click - windows[0].focus(); + window.focus(); if (relativeUrl) { // handle requests passed via the notion:// protocol // or ctrl+click - windows[0].loadURL(urlHelpers.getIndexUrl(relativeUrl)); + window.loadURL(urlHelpers.getIndexUrl(relativeUrl)); } - return windows[0]; + return window; } }; }; diff --git a/repo/tray/main.cjs b/repo/tray/main.cjs index 6bf8dbb..057161c 100644 --- a/repo/tray/main.cjs +++ b/repo/tray/main.cjs @@ -8,8 +8,9 @@ let tray; -module.exports = async function ({ env, registry }, db, __exports, __eval) { - const electron = require('electron'), +module.exports = async function (api, db, __exports, __eval) { + const { env, registry } = api, + electron = require('electron'), path = require('path'), enhancerIcon = path.resolve(`${__dirname}/../../media/colour-x16.png`), hotkey = await db.get(['hotkey']), @@ -47,7 +48,7 @@ module.exports = async function ({ env, registry }, db, __exports, __eval) { // since notion's default is broken by // duplicate window prevention const createWindow = () => { - const { createWindow } = env.notionRequire('main/createWindow.js'); + const { createWindow } = api.electron.notionRequire('main/createWindow.js'); createWindow('/'); }; electron.ipcMain.on('notion-enhancer:create-new-window', createWindow);