mirror of
https://github.com/notion-enhancer/notion-enhancer.git
synced 2025-04-05 13:19:03 +00:00
strict hotkey modifiers + electron window helpers
This commit is contained in:
parent
8cc057a5f8
commit
2428a42103
@ -15,12 +15,7 @@
|
||||
* access to the electron BrowserWindow instance for the current window
|
||||
* see https://www.electronjs.org/docs/latest/api/browser-window
|
||||
* @type {BrowserWindow}
|
||||
*
|
||||
* @env win32
|
||||
* @env linux
|
||||
* @env darwin
|
||||
* @runtime client
|
||||
* @runtime menu
|
||||
* @runtime electron (renderer process)
|
||||
*/
|
||||
export const browser = globalThis.__enhancerElectronApi?.browser;
|
||||
|
||||
@ -28,12 +23,7 @@ export const browser = globalThis.__enhancerElectronApi?.browser;
|
||||
* access to the electron webFrame instance for the current page
|
||||
* see https://www.electronjs.org/docs/latest/api/web-frame
|
||||
* @type {webFrame}
|
||||
*
|
||||
* @env win32
|
||||
* @env linux
|
||||
* @env darwin
|
||||
* @runtime client
|
||||
* @runtime menu
|
||||
* @runtime electron (renderer process)
|
||||
*/
|
||||
export const webFrame = globalThis.__enhancerElectronApi?.webFrame;
|
||||
|
||||
@ -44,12 +34,7 @@ export const webFrame = globalThis.__enhancerElectronApi?.webFrame;
|
||||
* @param {string=} namespace - a prefix for the message to categorise
|
||||
* it as e.g. enhancer-related. this should not be changed unless replicating
|
||||
* builtin ipc events.
|
||||
*
|
||||
* @env win32
|
||||
* @env linux
|
||||
* @env darwin
|
||||
* @runtime client
|
||||
* @runtime menu
|
||||
* @runtime electron (renderer process)
|
||||
*/
|
||||
export const sendMessage = (channel, data, namespace = 'notion-enhancer') => {
|
||||
if (globalThis.__enhancerElectronApi) {
|
||||
@ -64,12 +49,7 @@ export const sendMessage = (channel, data, namespace = 'notion-enhancer') => {
|
||||
* @param {string=} namespace - a prefix for the message to categorise
|
||||
* it as e.g. enhancer-related. this should not be changed unless replicating
|
||||
* builtin ipc events.
|
||||
*
|
||||
* @env win32
|
||||
* @env linux
|
||||
* @env darwin
|
||||
* @runtime client
|
||||
* @runtime menu
|
||||
* @runtime electron (renderer process)
|
||||
*/
|
||||
export const sendMessageToHost = (channel, data, namespace = 'notion-enhancer') => {
|
||||
if (globalThis.__enhancerElectronApi) {
|
||||
@ -85,15 +65,41 @@ export const sendMessageToHost = (channel, data, namespace = 'notion-enhancer')
|
||||
* @param {string=} namespace - a prefix for the message to categorise
|
||||
* it as e.g. enhancer-related. this should not be changed unless replicating
|
||||
* builtin ipc events.
|
||||
*
|
||||
* @env win32
|
||||
* @env linux
|
||||
* @env darwin
|
||||
* @runtime client
|
||||
* @runtime menu
|
||||
* @runtime electron (renderer process)
|
||||
*/
|
||||
export const onMessage = (channel, callback, namespace = 'notion-enhancer') => {
|
||||
if (globalThis.__enhancerElectronApi) {
|
||||
globalThis.__enhancerElectronApi.ipcRenderer.onMessage(channel, callback, namespace);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* require() notion app files
|
||||
* @param {string} path - within notion/resources/app/ e.g. main/createWindow.js
|
||||
* @runtime electron (main process)
|
||||
*/
|
||||
export const notionRequire = (path) => {
|
||||
return globalThis.__enhancerElectronApi
|
||||
? globalThis.__enhancerElectronApi.notionRequire(path)
|
||||
: null;
|
||||
};
|
||||
|
||||
/**
|
||||
* get all available app windows excluding the menu
|
||||
* @runtime electron (main process)
|
||||
*/
|
||||
export const getNotionWindows = () => {
|
||||
return globalThis.__enhancerElectronApi
|
||||
? globalThis.__enhancerElectronApi.getNotionWindows()
|
||||
: null;
|
||||
};
|
||||
|
||||
/**
|
||||
* get the currently focused notion window
|
||||
* @runtime electron (main process)
|
||||
*/
|
||||
export const getFocusedNotionWindow = () => {
|
||||
return globalThis.__enhancerElectronApi
|
||||
? globalThis.__enhancerElectronApi.getFocusedNotionWindow()
|
||||
: null;
|
||||
};
|
||||
|
11
api/env.mjs
11
api/env.mjs
@ -44,14 +44,3 @@ export const focusNotion = env.focusNotion;
|
||||
* @type {function}
|
||||
*/
|
||||
export const reload = env.reload;
|
||||
|
||||
/**
|
||||
* require() notion app files
|
||||
* @param {string} path - within notion/resources/app/ e.g. main/createWindow.js
|
||||
*
|
||||
* @env win32
|
||||
* @env linux
|
||||
* @env darwin
|
||||
* @runtime electron
|
||||
*/
|
||||
export const notionRequire = env.notionRequire;
|
||||
|
@ -50,10 +50,6 @@ export const isFile = fs.isFile;
|
||||
/**
|
||||
* get an absolute path to files within notion
|
||||
* @param {string} path - relative to the root notion/resources/app/ e.g. renderer/search.js
|
||||
*
|
||||
* @env win32
|
||||
* @env linux
|
||||
* @env darwin
|
||||
* @runtime electron
|
||||
*/
|
||||
export const notionPath = fs.notionPath;
|
||||
|
File diff suppressed because one or more lines are too long
38
api/web.mjs
38
api/web.mjs
@ -169,23 +169,35 @@ export const readFromClipboard = () => {
|
||||
const triggerHotkeyListener = (event, hotkey) => {
|
||||
const inInput = document.activeElement.nodeName === 'INPUT' && !hotkey.listenInInput;
|
||||
if (inInput) return;
|
||||
const pressed = hotkey.keys.every((key) => {
|
||||
key = key.toLowerCase();
|
||||
const modifiers = {
|
||||
const modifiers = {
|
||||
metaKey: ['meta', 'os', 'win', 'cmd', 'command'],
|
||||
ctrlKey: ['ctrl', 'control'],
|
||||
shiftKey: ['shift'],
|
||||
altKey: ['alt'],
|
||||
};
|
||||
for (const modifier in modifiers) {
|
||||
const pressed = modifiers[modifier].includes(key) && event[modifier];
|
||||
if (pressed) return true;
|
||||
}
|
||||
if (key === 'space') key = ' ';
|
||||
if (key === 'plus') key = '+';
|
||||
if (key === event.key.toLowerCase()) return true;
|
||||
});
|
||||
if (pressed) hotkey.callback(event);
|
||||
},
|
||||
pressed = hotkey.keys.every((key) => {
|
||||
key = key.toLowerCase();
|
||||
for (const modifier in modifiers) {
|
||||
const pressed = modifiers[modifier].includes(key) && event[modifier];
|
||||
if (pressed) {
|
||||
// mark modifier as part of hotkey
|
||||
modifiers[modifier] = [];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (key === 'space') key = ' ';
|
||||
if (key === 'plus') key = '+';
|
||||
if (key === event.key.toLowerCase()) return true;
|
||||
});
|
||||
if (!pressed) return;
|
||||
// test for modifiers not in hotkey
|
||||
// e.g. to differentiate ctrl+x from ctrl+shift+x
|
||||
for (const modifier in modifiers) {
|
||||
const modifierPressed = event[modifier],
|
||||
modifierNotInHotkey = modifiers[modifier].length > 0;
|
||||
if (modifierPressed && modifierNotInHotkey) return;
|
||||
}
|
||||
hotkey.callback(event);
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user