Last active
April 20, 2022 12:23
-
-
Save cderv/1babac45835a83fb7238d59cc666ac71 to your computer and use it in GitHub Desktop.
[ReveaJS] Set code output content dynamically based on remaining space
This file contains 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
<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> |
This file contains 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
--- | |
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