Skip to content

Commit 5cd606a

Browse files
committed
MAINT: Fix the javascript and add a tox file
1 parent e1da2f8 commit 5cd606a

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
"sphinx_togglebutton": ["_static/togglebutton.css_t", "_static/togglebutton.js"]
2828
},
2929
install_requires=["setuptools", "wheel", "sphinx", "docutils"],
30-
extras_require={"sphinx": ["myst_nb", "sphinx_book_theme"]},
30+
extras_require={"sphinx": ["matplotlib", "myst_nb", "sphinx_book_theme", "sphinx_design"]},
3131
classifiers=["License :: OSI Approved :: MIT License"],
3232
)

sphinx_togglebutton/_static/togglebutton.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
var initToggleItems = () => {
22
var itemsToToggle = document.querySelectorAll(togglebuttonSelector);
3-
console.log(itemsToToggle, togglebuttonSelector)
3+
console.log(`[togglebutton]: Adding toggle buttons to ${itemsToToggle.length} items`)
44
// Add the button to each admonition and hook up a callback to toggle visibility
55
itemsToToggle.forEach((item, index) => {
6+
// Generate unique IDs for this item
67
var toggleID = `toggle-${index}`;
78
var buttonID = `button-${toggleID}`;
8-
var collapseButton = `
9-
<button id="${buttonID}" class="toggle-button" data-target="${toggleID}" data-button="${buttonID}">
10-
<div class="bar horizontal" data-button="${buttonID}"></div>
11-
<div class="bar vertical" data-button="${buttonID}"></div>
12-
</button>`;
139

1410
item.setAttribute('id', toggleID);
15-
1611
if (!item.classList.contains("toggle")){
1712
item.classList.add("toggle");
1813
}
1914

20-
// If it's an admonition block, then we'll add the button inside
15+
// This is the button that will be added to each item to trigger the toggle
16+
var collapseButton = `
17+
<button id="${buttonID}" class="toggle-button" data-target="${toggleID}" data-button="${buttonID}">
18+
<div class="bar horizontal" data-button="${buttonID}"></div>
19+
<div class="bar vertical" data-button="${buttonID}"></div>
20+
</button>`;
21+
22+
// Add the button HTML to this element and assign it as a variable to use later
2123
if (item.classList.contains("admonition")) {
24+
// If it's an admonition block, then we'll add the button inside
2225
item.insertAdjacentHTML("afterbegin", collapseButton);
2326
} else {
2427
item.insertAdjacentHTML('beforebegin', collapseButton);
2528
}
26-
2729
thisButton = document.getElementById(buttonID);
28-
thisButton.on('click', toggleClickHandler);
30+
31+
// Add click handlers for the button + admonition title (if admonition)
32+
thisButton.addEventListener('click', toggleClickHandler);
33+
2934
// If admonition has a single direct-child title make it clickable.
3035
admonitionTitle = document.querySelector(`#${toggleID} > .admonition-title`)
3136
if (admonitionTitle) {
32-
admonitionTitle.on('click', toggleClickHandler);
37+
admonitionTitle.addEventListener('click', toggleClickHandler);
3338
admonitionTitle.dataset.target = toggleID
3439
admonitionTitle.dataset.button = buttonID
3540
}
41+
42+
// Now hide the item for this toggle button unless explicitly noted to show
3643
if (!item.classList.contains("toggle-shown")) {
37-
toggleHidden(thisButton[0]);
44+
toggleHidden(thisButton);
3845
}
3946
})
4047
};

tox.ini

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# To use tox, see https://tox.readthedocs.io
2+
3+
[tox]
4+
envlist = py39-sphinx3
5+
6+
[testenv]
7+
# only recreate the environment when we use `tox -r`
8+
recreate = false
9+
10+
[testenv:docs]
11+
description = Build the documentation
12+
extras =
13+
sphinx
14+
commands =
15+
sphinx-build \
16+
-n -b {posargs:html} docs/ docs/_build/{posargs:html}
17+
18+
[testenv:docs-live]
19+
description = Auto-build and preview the documentation in the browser
20+
deps =
21+
sphinx-autobuild
22+
extras =
23+
sphinx
24+
commands =
25+
sphinx-autobuild \
26+
--re-ignore _build/.* \
27+
--watch sphinx_togglebutton \
28+
--port 0 --open-browser \
29+
-n -b {posargs:html} docs/ docs/_build/{posargs:html}

0 commit comments

Comments
 (0)