From cd81ed6f8a53e628775c821e328521eb4f88af26 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Thu, 8 Feb 2024 23:34:20 -0600 Subject: [PATCH] changes to adapt to https://github.com/yihui/servr/commit/2dbf2932d152703072c9fe2e271ba13c21520061 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) --- DESCRIPTION | 9 ++--- NEWS.md | 1 + R/render.R | 1 + .../xaringan/resources/js/ws-handler.js | 36 ++++++++++--------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 72621381..c9d5eb7a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person(given = "Posit Software, PBC", role = c("cph", "fnd")), @@ -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 diff --git a/NEWS.md b/NEWS.md index 58ee5f05..f5af46f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/render.R b/R/render.R index 50c9ea9b..92682df3 100644 --- a/R/render.R +++ b/R/render.R @@ -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 diff --git a/inst/rmarkdown/templates/xaringan/resources/js/ws-handler.js b/inst/rmarkdown/templates/xaringan/resources/js/ws-handler.js index b9072411..fe1e4bc7 100644 --- a/inst/rmarkdown/templates/xaringan/resources/js/ws-handler.js +++ b/inst/rmarkdown/templates/xaringan/resources/js/ws-handler.js @@ -1,37 +1,41 @@ -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({ @@ -39,7 +43,5 @@ setInterval(function() { 'N': slideshow.getSlideCount(), 'focused': focused }) : '{}'); -}, !!SERVR_INTERVAL); - -window.addEventListener('focus', function(e) { focused = true; }); -window.addEventListener('blur', function(e) { focused = false; }); +}, interval); +})