From bb5853e866fda82cae6b1e4e27db36e1027d3f05 Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Sat, 5 Aug 2023 01:09:01 +1000 Subject: [PATCH] feat(panel): add open/close gui buttons --- src/core/client.mjs | 5 +- src/core/islands/Panel.mjs | 135 +++++++++++++++++++++++++--------- src/{api => shared}/notion.js | 0 3 files changed, 102 insertions(+), 38 deletions(-) rename src/{api => shared}/notion.js (100%) diff --git a/src/core/client.mjs b/src/core/client.mjs index 82a4585..27efa60 100644 --- a/src/core/client.mjs +++ b/src/core/client.mjs @@ -142,9 +142,8 @@ const insertPanel = async (db) => { addKeyListener(togglePanelHotkey, (event) => { event.preventDefault(); event.stopPropagation(); - if ($panel.hasAttribute("open")) { - $panel.close(); - } else $panel.open(); + if ($panel.hasAttribute("open")) $panel.close(); + else $panel.open(); }); }; diff --git a/src/core/islands/Panel.mjs b/src/core/islands/Panel.mjs index e335b79..446b45a 100644 --- a/src/core/islands/Panel.mjs +++ b/src/core/islands/Panel.mjs @@ -17,6 +17,16 @@ import { Tooltip } from "./Tooltip.mjs"; +function PanelView(props) { + const { html } = globalThis.__enhancerApi; + return html``; +} + +function PanelSwitcher(props) { + const { html } = globalThis.__enhancerApi; + return html``; +} + function Panel({ _getWidth, _setWidth, @@ -26,7 +36,8 @@ function Panel({ maxWidth = 640, ...props }) { - const { html, extendProps } = globalThis.__enhancerApi; + const { html, extendProps } = globalThis.__enhancerApi, + { addMutationListener, removeMutationListener } = globalThis.__enhancerApi; extendProps(props, { class: `notion-enhancer--side-panel order-2 shrink-0 transition-[width] open:w-[var(--side\\_panel--width)] @@ -40,11 +51,61 @@ function Panel({ shadow-[var(--theme--fg-border)_-2px_0px_0px_0px_inset]) active:cursor-text group-not-[open]:hidden" >`, + $chevronClose = html``, + appendToDom = () => { + if (document.contains($topbarPanelButton)) return; + const $notionTopbarMore = document.querySelector(notionTopbarMore); + $notionTopbarMore?.before($topbarPanelButton); + }; + $topbarPanelButton.addEventListener("click", () => { + if ($panel.hasAttribute("open")) $panel.close(); + else $panel.open(); + }); + addMutationListener(notionTopbarMore, appendToDom); + appendToDom(); + + $panel.resize = async (width) => { $tooltip.hide(); if (width) { width = Math.max(width, minWidth); width = Math.min(width, maxWidth); - if (!dragActive) _setWidth?.(width); + if (!userDragActive) _setWidth?.(width); } else width = await _getWidth?.(); if (isNaN(width)) width = minWidth; $panel.style.setProperty("--side_panel--width", `${width}px`); @@ -118,6 +181,7 @@ function Panel({ $panel.open = () => { $panel.setAttribute("open", true); $panel.querySelectorAll("[tabindex]").forEach(($el) => ($el.tabIndex = 0)); + $topbarPanelButton.setAttribute("data-active", true); $panel.onopen?.(); _setOpen(true); $panel.resize(); @@ -127,6 +191,7 @@ function Panel({ $panel.onbeforeclose?.(); $panel.removeAttribute("open"); $panel.style.pointerEvents = "auto"; + $topbarPanelButton.removeAttribute("data-active"); $panel.querySelectorAll("[tabindex]").forEach(($el) => ($el.tabIndex = -1)); repositionHelp(); _setOpen(false); diff --git a/src/api/notion.js b/src/shared/notion.js similarity index 100% rename from src/api/notion.js rename to src/shared/notion.js