Skip to content

Commit

Permalink
changes to adapt to yihui/servr@2dbf293
Browse files Browse the repository at this point in the history
the handler script should be a JS function now, and dynamic_site() no longer parses the JSON message automatically (so we parse it inside xaringan by ourselves)
  • Loading branch information
yihui committed Feb 9, 2024
1 parent 07b03a7 commit cd81ed6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xaringan
Type: Package
Title: Presentation Ninja
Version: 0.28.1
Version: 0.28.2
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person(given = "Posit Software, PBC", role = c("cph", "fnd")),
Expand Down Expand Up @@ -46,13 +46,14 @@ Depends: R (>= 3.5.0)
Imports:
htmltools,
knitr (>= 1.30),
servr (>= 0.13),
servr (>= 0.28.2),
xfun (>= 0.18),
rmarkdown (>= 2.8)
Suggests: rstudioapi, testit
Suggests: rstudioapi, jsonlite, testit
License: MIT + file LICENSE
URL: https://github.com/yihui/xaringan
BugReports: https://github.com/yihui/xaringan/issues
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Remotes: yihui/servr
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# CHANGES IN xaringan VERSION 0.29

- Internal changes for `xaringan::inf_mr()` according to changes in the **servr** package.

# CHANGES IN xaringan VERSION 0.28

Expand Down
1 change: 1 addition & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ infinite_moon_reader = function(moon, cast_from = '.', ...) {
if (!r || missing(message)) return(FALSE)
ctx = rstudioapi::getSourceEditorContext()
if (identical(normalize_path(as.character(ctx[['path']])), moon)) {
if (is.character(message)) message = jsonlite::fromJSON(message)
if (isTRUE(message[['focused']])) {
# auto-navigate to the slide source corresponding to current HTML
# page only when the slides are on focus
Expand Down
36 changes: 19 additions & 17 deletions inst/rmarkdown/templates/xaringan/resources/js/ws-handler.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
var flag, focused = false;
ws.onmessage = function(event) {
((interval, path) => {
const ws = new WebSocket(location.href.replace(/^http/, 'ws').replace(/\/?$/, '/websocket/'));
let flag, focused = false;

window.addEventListener('focus', e => { focused = true; });
window.addEventListener('blur', e => { focused = false; });

ws.onmessage = event => {
flag = true;
var d = JSON.parse(event.data);
const d = JSON.parse(event.data);
if (d === true) {
// fire a servr:reload event
Event && document.dispatchEvent(new Event('servr:reload'));
return location.reload();
}
if (!window.slideshow || !window.remark || d === false || d === null) return;
var p = d.page; if (p < 1) p = 1;
let p = d.page; if (p < 1) p = 1;
if (!focused) slideshow.gotoSlide(p);
if (d.markdown === false) return;
var el = document.getElementsByClassName('remark-slides-area');
let el = document.getElementsByClassName('remark-slides-area');
el = el[0].children[p - 1].querySelector('.remark-slide-content');
var n = el.querySelector('.remark-slide-number').outerHTML.toString();
const n = el.querySelector('.remark-slide-number').outerHTML.toString();
el.innerHTML = remark.convert(d.markdown) + n;
if (window.MathJax) {
slideshow._releaseMath(el);
MathJax.Hub.Queue(['Typeset', MathJax.Hub, el]);
}
var i, code, codes = el.getElementsByTagName('pre');
for (i = 0; i < codes.length; i++) {
code = codes[i];
[...el.getElementsByTagName('pre')].forEach(code => {
if (code.querySelector('.\\.hidden')) {
code.style.display = 'none'; continue;
code.style.display = 'none'; return;
}
remark.highlighter.engine.highlightBlock(codes[i]);
}
remark.highlighter.engine.highlightBlock(code);
});
};

// send the page number to R, so RStudio can move to the Rmd source of the
// current slide
setInterval(function() {
setInterval(() => {
if (flag === false || ws.readyState !== ws.OPEN) return;
flag = false;
ws.send((window.slideshow && window.remark) ? JSON.stringify({
'n': slideshow.getCurrentSlideIndex() + 1,
'N': slideshow.getSlideCount(),
'focused': focused
}) : '{}');
}, !!SERVR_INTERVAL);

window.addEventListener('focus', function(e) { focused = true; });
window.addEventListener('blur', function(e) { focused = false; });
}, interval);
})

0 comments on commit cd81ed6

Please sign in to comment.