简体   繁体   English

在单个网页上嵌入多个 RunKit REPL 实例

[英]embedding multiple RunKit REPL instances on a single web page

I am creating a web-based presentation using RemarkJS , my preferred tool for presentations.我正在使用RemarkJS创建基于 Web 的演示文稿,这是我首选的演示文稿工具。 Since I want to demo small bits of nodejs code, I want to use RunKit 's REPL embedding capability .由于我想演示一小段nodejs代码,我想使用RunKitREPL 嵌入功能 I couldn't quite figure out how to embed multiple instances on a single web page, but I managed to kludge something up by running the following code multiple times我不太清楚如何在单个网页上嵌入多个实例,但我设法通过多次运行以下代码来拼凑一些东西

var nb1 = RunKit.createNotebook({
    // the parent element for the new notebook
    element: document.getElementById("div1"),

    // specify the source of the notebook
    source: source1
})

var nb2 = RunKit.createNotebook({
    // the parent element for the new notebook
    element: document.getElementById("div2"),

    // specify the source of the notebook
    source: source2
})

and so on.等等。 This actually works, but is super-inefficient.这实际上有效,但效率极低。 When I launch my presentation, multiple calls to RunKit are made even though the entire page is loaded only once.当我启动演示文稿时,即使整个页面只加载一次,也会多次调用 RunKit。 Not only that, multiple calls to RunKit are made every time I change the slides, which, in RemarkJS, simple hides and displays different parts of the same, single web page.不仅如此,每次我更改幻灯片时都会多次调用 RunKit,在 RemarkJS 中,它可以简单地隐藏和显示同一单个网页的不同部分。 Since the web page itself is being loaded only once, RunKit should be called only once.由于网页本身只被加载一次,RunKit 应该只被调用一次。 At least, that is what I think (obviously, it seems I am wrong).至少,这是我的想法(显然,我似乎错了)。

Finally, the actual RunKit REPL frame takes a while before it is rendered.最后,实际的 RunKit REPL 帧在渲染之前需要一段时间。 In the beginning, only a few lines of truncated code shows up, but after a while of waiting, the entire frame gets rendered.一开始,只显示了几行被截断的代码,但经过一段时间的等待,整个帧都被渲染了。

What am I doing wrong?我做错了什么? Is there a better way of doing this?有没有更好的方法来做到这一点?

I had the same problem and figured it out.我有同样的问题并想通了。 So for future users here is how you do it.所以对于未来的用户,这里是你如何做到的。

<script src="https://embed.runkit.com"></script>
<style>.embed { overflow: visible; }</style>
<pre class="embed" data-gutter="inside">console.log("hello inside");
1 + 1</pre>
<pre class="embed" data-gutter="outside">console.log("hello outside");
1 + 1</pre>
<pre class="embed" data-gutter="none">console.log("hello none");
1 + 1</pre>
<script>
const elements = [...document.getElementsByClassName('embed')]
const notebooks = elements.reduce((notebooks, element) => {
    const innerText = element.firstChild
    const currentCell = window.RunKit.createNotebook({
        element,
        gutterStyle: element.getAttribute("data-gutter"),
        source: innerText.textContent,
        // Remove the text content of the pre tag after the embed has loaded
        onLoad: () => innerText.remove()
    })
  return notebooks
}, [])
</script>

The sample is taken from here: https://runkit.com/docs/embed Scroll down and you will find it.示例取自此处: https: //runkit.com/docs/embed 向下滚动,您会找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM