Last active
June 6, 2023 17:03
-
-
Save Pangoraw/cc905dae6f31e79f49785eebdb131620 to your computer and use it in GitHub Desktop.
Fix the broken slide controls in Pluto (0.19.? <= x <= 0.19.9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ╔═╡ 9e5d37c8-c45c-4eb0-bc38-fd42bb408508 | |
html""" | |
<script> | |
const calculate_slide_positions = (/** @type {Event} */ e) => { | |
const notebook_node = /** @type {HTMLElement?} */ (e.target)?.closest("pluto-editor")?.querySelector("pluto-notebook") | |
console.log(e.target) | |
if (!notebook_node) return [] | |
const height = window.innerHeight | |
const headers = Array.from(notebook_node.querySelectorAll("pluto-output h1, pluto-output h2")) | |
const pos = headers.map((el) => el.getBoundingClientRect()) | |
const edges = pos.map((rect) => rect.top + window.pageYOffset) | |
edges.push(notebook_node.getBoundingClientRect().bottom + window.pageYOffset) | |
const scrollPositions = headers.map((el, i) => { | |
if (el.tagName == "H1") { | |
// center vertically | |
const slideHeight = edges[i + 1] - edges[i] - height | |
return edges[i] - Math.max(0, (height - slideHeight) / 2) | |
} else { | |
// align to top | |
return edges[i] - 20 | |
} | |
}) | |
return scrollPositions | |
} | |
const go_previous_slide = (/** @type {Event} */ e) => { | |
const positions = calculate_slide_positions(e) | |
const pos = positions.reverse().find((y) => y < window.pageYOffset - 10) | |
if (pos) window.scrollTo(window.pageXOffset, pos) | |
} | |
const go_next_slide = (/** @type {Event} */ e) => { | |
const positions = calculate_slide_positions(e) | |
const pos = positions.find((y) => y - 10 > window.pageYOffset) | |
if (pos) window.scrollTo(window.pageXOffset, pos) | |
} | |
const left_button = document.querySelector(".changeslide.prev") | |
const right_button = document.querySelector(".changeslide.next") | |
left_button.addEventListener("click", go_previous_slide) | |
right_button.addEventListener("click", go_next_slide) | |
</script> | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment