/** * notion-enhancer * (c) 2023 dragonwocky (https://dragonwocky.me/) * (https://notion-enhancer.github.io/) under the MIT license */ const notionSidebar = `.notion-sidebar-container .notion-sidebar > :nth-child(3) > div > :nth-child(2)`; export default async (api, db) => { const { html, platform, getThemes, isEnabled, enhancerUrl, onMessage, sendMessage, addMutationListener, addKeyListener, } = api, openMenuHotkey = await db.get("openMenuHotkey"), menuButtonIconStyle = await db.get("menuButtonIconStyle"), loadThemeOverrides = await db.get("loadThemeOverrides"), customStyles = await db.get("customStyles"); // appearance const enabledThemes = (await getThemes()).map((theme) => isEnabled(theme.id)), forceLoadOverrides = loadThemeOverrides === "Enabled", autoLoadOverrides = loadThemeOverrides === "Auto" && (await Promise.all(enabledThemes)).some((enabled) => enabled); if (forceLoadOverrides || autoLoadOverrides) { document.head.append(html``); } if (customStyles) { document.head.append(html``); } // menu let $menuModal, $menuFrame, _notionTheme; const updateTheme = (force = false) => { const darkMode = document.body.classList.contains("dark"), notionTheme = darkMode ? "dark" : "light"; if (notionTheme !== _notionTheme || force) { _notionTheme = notionTheme; const msg = { namespace: "notion-enhancer", iconStyle: menuButtonIconStyle, mode: notionTheme, }; $menuFrame?.contentWindow.postMessage(msg, "*"); } }; const openMenu = () => { updateTheme(true); $menuModal?.setAttribute("open", true); }, closeMenu = () => $menuModal?.removeAttribute("open"); $menuFrame = html``; $menuModal = html`
${$menuFrame}
`; document.body.append($menuModal); const $menuButton = html`
notion-enhancer
`; addMutationListener(notionSidebar, () => { if (document.contains($menuButton)) return; document.querySelector(notionSidebar)?.append($menuButton); }); document.querySelector(notionSidebar)?.append($menuButton); addMutationListener("body", () => { if ($menuModal?.hasAttribute("open")) updateTheme(); }); onMessage("notion-enhancer", (message) => { if (message === "open-menu") openMenu(); }); addKeyListener(openMenuHotkey, (event) => { event.preventDefault(); openMenu(); }); addKeyListener("Escape", () => { if (document.activeElement?.nodeName === "INPUT") return; closeMenu(); }); sendMessage("notion-enhancer", "load-complete"); };