From 9d474fb26758495f57e3abc6cdba2a6b253ba419 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Tue, 22 Apr 2025 11:41:12 +0200 Subject: [PATCH 1/6] disable search override --- src/scanpydoc/theme/__init__.py | 5 --- .../theme/static/scripts/rtd-sphinx-search.js | 37 ------------------- 2 files changed, 42 deletions(-) delete mode 100644 src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js diff --git a/src/scanpydoc/theme/__init__.py b/src/scanpydoc/theme/__init__.py index 7a37f49..9f44fa8 100644 --- a/src/scanpydoc/theme/__init__.py +++ b/src/scanpydoc/theme/__init__.py @@ -70,7 +70,6 @@ from __future__ import annotations -import os from typing import TYPE_CHECKING from pathlib import Path @@ -89,8 +88,4 @@ def setup(app: Sphinx) -> dict[str, bool]: # pragma: no cover """Set up theme (like an extension).""" app.add_html_theme("scanpydoc", str(HERE)) - # if we’re on ReadTheDocs, hide the pydata-sphinx-theme search popup - if os.environ.get("READTHEDOCS_VERSION", ""): # pragma: no cover - app.add_js_file("scripts/rtd-sphinx-search.js", loading_method="defer") - return dict(parallel_read_safe=True, parallel_write_safe=True) diff --git a/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js b/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js deleted file mode 100644 index e60531b..0000000 --- a/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * This JS is only included when the RTD Sphinx search is active. - */ - -// wire up the search key combination -addEventListener( - "keydown", - ({ key, metaKey, ctrlKey }) => { - if (key === "k" && (metaKey || ctrlKey)) { - if (isModalVisible()) { - removeSearchModal() - } else { - showSearchModal() - } - } - }, - { passive: true }, -) - -// start attempting to override the search popup and to wire up the search button -setTimeout(overrideSearch, 0) - -function overrideSearch() { - /** @type {HTMLDivElement} */ - const theme_popup = document.querySelector(".search-button__wrapper") - /** @type {HTMLButtonElement} */ - const search_button = document.querySelector("button[aria-label='Search']") - if (!theme_popup || !search_button) { - // try again later - setTimeout(overrideSearch, 500) - return - } - // Hide the pydata theme’s search popup. - theme_popup.style.display = "none" - // wire up the search button - search_button.addEventListener("click", () => showSearchModal()) -} From 07c398b0c5879b136b31f704aad79faa50137eb2 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Tue, 22 Apr 2025 11:55:53 +0200 Subject: [PATCH 2/6] Revert "disable search override" This reverts commit 9d474fb26758495f57e3abc6cdba2a6b253ba419. --- src/scanpydoc/theme/__init__.py | 5 +++ .../theme/static/scripts/rtd-sphinx-search.js | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js diff --git a/src/scanpydoc/theme/__init__.py b/src/scanpydoc/theme/__init__.py index 9f44fa8..7a37f49 100644 --- a/src/scanpydoc/theme/__init__.py +++ b/src/scanpydoc/theme/__init__.py @@ -70,6 +70,7 @@ from __future__ import annotations +import os from typing import TYPE_CHECKING from pathlib import Path @@ -88,4 +89,8 @@ def setup(app: Sphinx) -> dict[str, bool]: # pragma: no cover """Set up theme (like an extension).""" app.add_html_theme("scanpydoc", str(HERE)) + # if we’re on ReadTheDocs, hide the pydata-sphinx-theme search popup + if os.environ.get("READTHEDOCS_VERSION", ""): # pragma: no cover + app.add_js_file("scripts/rtd-sphinx-search.js", loading_method="defer") + return dict(parallel_read_safe=True, parallel_write_safe=True) diff --git a/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js b/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js new file mode 100644 index 0000000..e60531b --- /dev/null +++ b/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js @@ -0,0 +1,37 @@ +/** + * This JS is only included when the RTD Sphinx search is active. + */ + +// wire up the search key combination +addEventListener( + "keydown", + ({ key, metaKey, ctrlKey }) => { + if (key === "k" && (metaKey || ctrlKey)) { + if (isModalVisible()) { + removeSearchModal() + } else { + showSearchModal() + } + } + }, + { passive: true }, +) + +// start attempting to override the search popup and to wire up the search button +setTimeout(overrideSearch, 0) + +function overrideSearch() { + /** @type {HTMLDivElement} */ + const theme_popup = document.querySelector(".search-button__wrapper") + /** @type {HTMLButtonElement} */ + const search_button = document.querySelector("button[aria-label='Search']") + if (!theme_popup || !search_button) { + // try again later + setTimeout(overrideSearch, 500) + return + } + // Hide the pydata theme’s search popup. + theme_popup.style.display = "none" + // wire up the search button + search_button.addEventListener("click", () => showSearchModal()) +} From 16615a125a0c2bbe316798f1cb7ef3a8b220d7f5 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Tue, 22 Apr 2025 12:17:18 +0200 Subject: [PATCH 3/6] Try addon API --- src/scanpydoc/theme/__init__.py | 4 +- .../theme/static/scripts/rtd-sphinx-search.js | 70 +++++++++++-------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/scanpydoc/theme/__init__.py b/src/scanpydoc/theme/__init__.py index 7a37f49..b51aad0 100644 --- a/src/scanpydoc/theme/__init__.py +++ b/src/scanpydoc/theme/__init__.py @@ -70,7 +70,6 @@ from __future__ import annotations -import os from typing import TYPE_CHECKING from pathlib import Path @@ -90,7 +89,6 @@ def setup(app: Sphinx) -> dict[str, bool]: # pragma: no cover app.add_html_theme("scanpydoc", str(HERE)) # if we’re on ReadTheDocs, hide the pydata-sphinx-theme search popup - if os.environ.get("READTHEDOCS_VERSION", ""): # pragma: no cover - app.add_js_file("scripts/rtd-sphinx-search.js", loading_method="defer") + app.add_js_file("scripts/rtd-sphinx-search.js", loading_method="defer") return dict(parallel_read_safe=True, parallel_write_safe=True) diff --git a/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js b/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js index e60531b..cad4b60 100644 --- a/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js +++ b/src/scanpydoc/theme/static/scripts/rtd-sphinx-search.js @@ -1,37 +1,47 @@ /** - * This JS is only included when the RTD Sphinx search is active. + * See https://docs.readthedocs.com/platform/stable/addons.html#integrate-with-search-as-you-type */ -// wire up the search key combination -addEventListener( - "keydown", - ({ key, metaKey, ctrlKey }) => { - if (key === "k" && (metaKey || ctrlKey)) { - if (isModalVisible()) { - removeSearchModal() - } else { - showSearchModal() +document.addEventListener("readthedocs-addons-data-ready", (event) => { + const { addons } = event.detail.data() + if (!addons.search?.enabled) { + return + } + + // wire up the search key combination + addEventListener( + "keydown", + ({ key, metaKey, ctrlKey }) => { + if (key === "k" && (metaKey || ctrlKey)) { + const here = document.querySelector("readthedocs-search")?.show + const event = new CustomEvent( + `readthedocs-search-${here ? "hide" : "show"}`, + ) + document.dispatchEvent(event) } - } - }, - { passive: true }, -) + }, + { passive: true }, + ) -// start attempting to override the search popup and to wire up the search button -setTimeout(overrideSearch, 0) + // start attempting to override the search popup and to wire up the search button + setTimeout(overrideSearch, 0) -function overrideSearch() { - /** @type {HTMLDivElement} */ - const theme_popup = document.querySelector(".search-button__wrapper") - /** @type {HTMLButtonElement} */ - const search_button = document.querySelector("button[aria-label='Search']") - if (!theme_popup || !search_button) { - // try again later - setTimeout(overrideSearch, 500) - return + function overrideSearch() { + /** @type {HTMLDivElement} */ + const theme_popup = document.querySelector(".search-button__wrapper") + /** @type {HTMLButtonElement} */ + const search_button = document.querySelector("button[aria-label='Search']") + if (!theme_popup || !search_button) { + // try again later + setTimeout(overrideSearch, 500) + return + } + // Hide the pydata theme’s search popup. + theme_popup.style.display = "none" + // wire up the search button + search_button.addEventListener("click", () => { + const event = new CustomEvent("readthedocs-search-show") + document.dispatchEvent(event) + }) } - // Hide the pydata theme’s search popup. - theme_popup.style.display = "none" - // wire up the search button - search_button.addEventListener("click", () => showSearchModal()) -} +}) From 3b417746e7c09c60299034bcc29b3a3b94137194 Mon Sep 17 00:00:00 2001 From: "Philipp A." Date: Tue, 22 Apr 2025 12:21:48 +0200 Subject: [PATCH 4/6] add meta tag --- src/scanpydoc/theme/layout.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/scanpydoc/theme/layout.html b/src/scanpydoc/theme/layout.html index e0993d6..edbd4d4 100644 --- a/src/scanpydoc/theme/layout.html +++ b/src/scanpydoc/theme/layout.html @@ -10,9 +10,10 @@ {% endif %} {%- endblock -%} - + {%- block extrahead -%} {{ super() }} + {% if theme_accent_color %}