首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单个网页上嵌入多个RunKit REPL实例

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

Stack Overflow用户
提问于 2017-06-13 05:18:06
回答 1查看 135关注 0票数 2

我正在使用RemarkJS创建一个基于web的演示文稿,这是我首选的演示文稿工具。因为我想演示一小段nodejs代码,所以我想使用RunKitREPL embedding capability。我不太清楚如何在一个网页上嵌入多个实例,但我通过多次运行以下代码成功地搞砸了一些东西

代码语言:javascript
复制
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
})

诸若此类。这实际上是有效的,但效率非常低。当我启动我的演示文稿时,即使整个页面只加载一次,也会多次调用RunKit。不仅如此,每次我更改幻灯片时,都会多次调用RunKit,在RemarkJS中,它简单地隐藏和显示同一个网页的不同部分。因为网页本身只被加载一次,所以RunKit应该只被调用一次。至少,这是我的想法(显然,看起来我错了)。

最后,实际的RunKit REPL帧需要一段时间才能呈现。在开始时,只显示几行截断的代码,但经过一段时间的等待后,整个帧都会呈现出来。

我做错了什么?有没有更好的方法来做这件事?

EN

回答 1

Stack Overflow用户

发布于 2020-09-15 02:48:45

我也有同样的问题,并解决了它。因此,对于未来的用户,这里就是你如何做到这一点的。

代码语言:javascript
复制
<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>

样本取自这里:https://runkit.com/docs/embed向下滚动,你会找到它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44508873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档