From 8ee12ea57efd46ac87a4b01d0545f291c7e3d525 Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Mon, 17 Aug 2020 23:43:43 +1000 Subject: [PATCH] remove loop from core client.js --- mods/core/client.js | 105 ++++++++++++++++++++++++-------------------- mods/core/menu.js | 3 +- mods/core/render.js | 2 - mods/core/tray.js | 9 +++- 4 files changed, 66 insertions(+), 53 deletions(-) diff --git a/mods/core/client.js b/mods/core/client.js index 43d9137..d8fd243 100644 --- a/mods/core/client.js +++ b/mods/core/client.js @@ -25,7 +25,11 @@ module.exports = (store, __exports) => { const attempt_interval = setInterval(enhance, 500); async function enhance() { - if (!document.querySelector('.notion-frame')) return; + if ( + !document.querySelector('.notion-frame') || + !document.querySelector('.notion-sidebar') + ) + return; clearInterval(attempt_interval); // scrollbars @@ -72,60 +76,65 @@ module.exports = (store, __exports) => { .querySelector('.notion-topbar-share-menu') .parentElement.classList.add('notion-topbar-actions'); - let sidebar_width; - function communicationLoop() { - const getStyle = (prop) => - getComputedStyle( - document.querySelector('.notion-app-inner') - ).getPropertyValue(prop), - mode = JSON.parse(localStorage.theme).mode; + const getStyle = (prop) => + getComputedStyle( + document.querySelector('.notion-app-inner') + ).getPropertyValue(prop); - // ctrl+f theming - notionIpc.sendNotionToIndex('search:set-theme', { - 'mode': mode, - 'colors': { - 'white': getStyle(`--theme--option_active-color`), - 'blue': getStyle(`--theme--option_active-background`), - }, - 'borderRadius': 3, - 'textColor': getStyle(`--theme--text`), - 'popoverBackgroundColor': getStyle(`--theme--card`), - 'popoverBoxShadow': `0 0 0 1px ${getStyle( - `--theme--overlay` - )}, 0 3px 6px ${getStyle(`--theme--overlay`)}`, - 'inputBoxShadow': `box-shadow: ${getStyle( - `--theme--primary` - )} 0px 0px 0px 1px inset, ${getStyle( - `--theme--primary_hover` - )} 0px 0px 0px 2px !important`, - 'inputBackgroundColor': getStyle(`--theme--main`), - 'dividerColor': getStyle(`--theme--table-border`), - 'shadowOpacity': 0.2, - }); + // ctrl+f theming + document.defaultView.addEventListener('keydown', (event) => { + if (event.key === 'f' && (event.ctrlKey || event.metaKey)) { + notionIpc.sendNotionToIndex('search:set-theme', { + 'mode': document.querySelector('.notion-dark-theme') + ? 'dark' + : 'light', + 'colors': { + 'white': getStyle(`--theme--option_active-color`), + 'blue': getStyle(`--theme--option_active-background`), + }, + 'borderRadius': 3, + 'textColor': getStyle(`--theme--text`), + 'popoverBackgroundColor': getStyle(`--theme--card`), + 'popoverBoxShadow': `0 0 0 1px ${getStyle( + `--theme--overlay` + )}, 0 3px 6px ${getStyle(`--theme--overlay`)}`, + 'inputBoxShadow': `box-shadow: ${getStyle( + `--theme--primary` + )} 0px 0px 0px 1px inset, ${getStyle( + `--theme--primary_hover` + )} 0px 0px 0px 2px !important`, + 'inputBackgroundColor': getStyle(`--theme--main`), + 'dividerColor': getStyle(`--theme--table-border`), + 'shadowOpacity': 0.2, + }); + } + }); - // enhancer menu - electron.ipcRenderer.send('enhancer:set-theme', { - mode, + // enhancer menu + function setMenuTheme() { + electron.ipcRenderer.send('enhancer:set-menu-theme', { + mode: document.querySelector('.notion-dark-theme') ? 'dark' : 'light', rules: require('./css/variables.json').map((rule) => [ rule, getStyle(rule), ]), }); - - // draggable area resizing - const sidebar = document.querySelector('.notion-sidebar'); - if (store().frameless && sidebar) { - let new_sidebar_width = - sidebar.style.height === 'auto' ? '0px' : sidebar.style.width; - if (sidebar_width !== new_sidebar_width) { - sidebar_width = new_sidebar_width; - electron.ipcRenderer.sendToHost( - 'enhancer:sidebar-width', - sidebar_width - ); - } - } } - setInterval(communicationLoop, 500); + setMenuTheme(); + electron.ipcRenderer.on('enhancer:get-menu-theme', setMenuTheme); + + const observer = new MutationObserver(setSidebarWidth); + observer.observe(document.querySelector('.notion-sidebar'), { + attributes: true, + }); + function setSidebarWidth(list, observer) { + if (!store().frameless) return; + electron.ipcRenderer.sendToHost( + 'enhancer:sidebar-width', + list[0].target.style.height === 'auto' + ? '0px' + : list[0].target.style.width + ); + } } }; diff --git a/mods/core/menu.js b/mods/core/menu.js index eb963ff..f1c4ce8 100644 --- a/mods/core/menu.js +++ b/mods/core/menu.js @@ -41,7 +41,8 @@ window['__start'] = async () => { } }); - electron.ipcRenderer.on('enhancer:set-theme', (event, theme) => { + electron.ipcRenderer.send('enhancer:get-menu-theme'); + electron.ipcRenderer.on('enhancer:set-menu-theme', (event, theme) => { document.body.className = `notion-${theme.mode}-theme`; for (const style of theme.rules) document.body.style.setProperty(style[0], style[1]); diff --git a/mods/core/render.js b/mods/core/render.js index c7b0a83..a6c7d58 100644 --- a/mods/core/render.js +++ b/mods/core/render.js @@ -16,8 +16,6 @@ module.exports = (store, __exports) => { ), default_styles = dragarea.getAttribute('style'); - // document.body.innerText = document.body.innerHTML; - document .getElementById('notion') .addEventListener('ipc-message', (event) => { diff --git a/mods/core/tray.js b/mods/core/tray.js index 66830d1..280bb50 100644 --- a/mods/core/tray.js +++ b/mods/core/tray.js @@ -29,9 +29,14 @@ module.exports = (store, __exports) => { }) ); - electron.ipcMain.on('enhancer:set-theme', (event, arg) => { + electron.ipcMain.on('enhancer:set-menu-theme', (event, arg) => { if (!enhancer_menu) return; - enhancer_menu.webContents.send('enhancer:set-theme', arg); + enhancer_menu.webContents.send('enhancer:set-menu-theme', arg); + }); + electron.ipcMain.on('enhancer:get-menu-theme', (event, arg) => { + electron.webContents + .getAllWebContents() + .forEach((webContents) => webContents.send('enhancer:get-menu-theme')); }); electron.ipcMain.on('enhancer:open-extension-menu', openExtensionMenu);