From f9e53ad0b344934332731adb2de3cca18c3e721a Mon Sep 17 00:00:00 2001
From: dragonwocky <thedragonring.bod@gmail.com>
Date: Wed, 2 Sep 2020 22:56:40 +1000
Subject: [PATCH] calendar scroll extension - #74

---
 CHANGELOG.md                    |  4 +-
 mods/calendar-scroll/mod.js     | 79 +++++++++++++++++++++++++++++++++
 mods/calendar-scroll/styles.css | 20 +++++++++
 3 files changed, 101 insertions(+), 2 deletions(-)
 create mode 100644 mods/calendar-scroll/mod.js
 create mode 100644 mods/calendar-scroll/styles.css

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 410ce20..7f7e275 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,7 +22,7 @@ a feature and cleanup update.
 - bugfix: blue select tags are no longer purple.
 - bugfix: page titles now respond to small-text mode.
 - bugfix: weekly calendar view height is now sized correctly according to its contents.
-- bugfix: made the open enhancements menu hotkey configurable and changed the default to `ALT+E`
+- bugfix: made the open enhancements menu hotkey configurable and changed the default to `ALT+E`.
   to remove conflict with the inline code highlight shortcut.
 - bugfix: update property-layout to match notion changes again.
 - themes: "littlepig" (light + dark) = monospaced themes using emojis and colourful text.
@@ -30,13 +30,13 @@ a feature and cleanup update.
   or leave it blank to not change anything.
 - extension: "always on top" = add an arrow/button to show the notion window on top of other windows
   even if it's not focused.
+- extension: "calendar scroll" = add a button to scroll down to the current week for you.
 
 // todo
 
 - improved: added individual text-colour rules for different background colours.
 - improved: added variables for callout colouring.
 - bugfix: block-level text colours are now changed properly.
-- extension: "calendar scroll" = a button to scroll down to the current week for you.
 
 notion-deb-builder has been discovered to not generate an app.asar and so is no longer supported.
 
diff --git a/mods/calendar-scroll/mod.js b/mods/calendar-scroll/mod.js
new file mode 100644
index 0000000..44cf64f
--- /dev/null
+++ b/mods/calendar-scroll/mod.js
@@ -0,0 +1,79 @@
+/*
+ * calendar scroll
+ * (c) 2020 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
+ * under the MIT license
+ */
+
+'use strict';
+
+const helpers = require('../../pkg/helpers.js');
+
+module.exports = {
+  id: 'b1c7db33-dfee-489a-a76c-0dd66f7ed29a',
+  tags: ['extension'],
+  name: 'calendar scroll',
+  desc:
+    'add a button to scroll down to the current week of a calendar for you.',
+  version: '0.1.0',
+  author: 'dragonwocky',
+  hacks: {
+    'renderer/preload.js'(store, __exports) {
+      document.addEventListener('readystatechange', (event) => {
+        if (document.readyState !== 'complete') return false;
+        const attempt_interval = setInterval(enhance, 500);
+        function enhance() {
+          const notion_elem = document.querySelector('.notion-frame');
+          if (!notion_elem) return;
+          clearInterval(attempt_interval);
+
+          const button = helpers.createElement(
+            '<button id="calendar-scroll-to-week">Scroll</button>'
+          );
+          button.addEventListener('click', (event) => {
+            const collection_view = document.querySelector(
+              '.notion-collection-view-select'
+            );
+            if (!collection_view) return;
+            const day = [
+              ...collection_view.parentElement.parentElement.parentElement.parentElement.getElementsByClassName(
+                'notion-calendar-view-day'
+              ),
+            ].find((day) => day.style.background);
+            if (!day) return;
+            const scroller = document.querySelector(
+              '.notion-frame .notion-scroller'
+            );
+            scroller.scroll({
+              top: day.offsetParent.offsetParent.offsetTop + 70,
+            });
+            setTimeout(
+              () =>
+                scroller.scroll({
+                  top: day.offsetParent.offsetParent.offsetTop + 70,
+                }),
+              100
+            );
+          });
+
+          process();
+          const observer = new MutationObserver(process);
+          observer.observe(notion_elem, {
+            childList: true,
+            subtree: true,
+          });
+          function process(list, observer) {
+            if (document.querySelector('#calendar-scroll-to-week')) return;
+            const arrow = document.querySelector(
+              '.notion-selectable.notion-collection_view_page-block .chevronLeft'
+            );
+            if (arrow)
+              arrow.parentElement.parentElement.insertBefore(
+                button,
+                arrow.parentElement
+              );
+          }
+        }
+      });
+    },
+  },
+};
diff --git a/mods/calendar-scroll/styles.css b/mods/calendar-scroll/styles.css
new file mode 100644
index 0000000..ed41f12
--- /dev/null
+++ b/mods/calendar-scroll/styles.css
@@ -0,0 +1,20 @@
+/*
+ * calendar scroll
+ * (c) 2020 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
+ * under the MIT license
+ */
+
+#calendar-scroll-to-week {
+  background: var(--theme--interactive_hover);
+  border: 1px solid transparent;
+  font-size: var(--theme--font_label-size);
+  color: var(--theme--text);
+  height: 24px;
+  border-radius: 3px;
+  line-height: 1.2;
+  padding: 0 0.5em;
+}
+#calendar-scroll-to-week:hover {
+  background: transparent;
+  border: 1px solid var(--theme--interactive_hover);
+}