Skip to content

Instantly share code, notes, and snippets.

@cderv
Last active April 20, 2022 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cderv/1babac45835a83fb7238d59cc666ac71 to your computer and use it in GitHub Desktop.
Save cderv/1babac45835a83fb7238d59cc666ac71 to your computer and use it in GitHub Desktop.
[ReveaJS] Set code output content dynamically based on remaining space
<script type="text/javascript">
Reveal.on('slidechanged', event => {
getNodes = function(slide) {
var level1 = slide.querySelectorAll(":scope > *")
childs = []
for (let e of level1) {
if (e.nodeName === "DIV" && e.className.includes('cell')) {
var level2 = e.querySelectorAll(":scope > *:not(div.cell-output)")
childs.push(...level2)
} else {
childs.push(e)
}
}
return childs
}
allHeight = function(nodes) {
var sum = 0;
all.forEach((x) => {
size = x.offsetHeight;
marginTop = parseInt(window.getComputedStyle(x).getPropertyValue('margin-top'));
marginBottom = parseInt(window.getComputedStyle(x).getPropertyValue('margin-bottom'));
sum+=size+marginTop+marginBottom
})
return sum
}
all = getNodes(event.currentSlide)
height = allHeight(all)
vp = document.querySelector('body.reveal-viewport')
slideHeight = parseInt(window.getComputedStyle(vp).getPropertyValue('--slide-height'));
codeCellOutput = event.currentSlide.querySelector('div.cell-output > pre > code')
if (codeCellOutput) {
codeCellOutput.style.maxHeight = slideHeight-allHeight(all)+'px'
}
})
</script>
---
title: "Slideshow"
author: "author"
format:
revealjs:
width: 1920
height: 1080
include-after-body: custom-reveal.html
---
## First slide
```{r}
#| echo: true
#| code-overflow: wrap
#| tidy: true
data("mtcars")
rbind(mtcars, mtcars, mtcars)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment