使用Monaco Editor运行功能

时间:2019-01-02 05:02:30

标签: visual-studio-code monaco-editor

我想在项目中使用Monaco编辑器,并希望在Microsoft的开源编辑器Monaco编辑器(https://github.com/Microsoft/monaco-editor/)中运行服务器端语言,例如C#或node。

这里有几个例子。

https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-rendering-glyphs-in-the-margin

https://dotnet.microsoft.com/languages

如果您看到上述示例,则可以看到它们正在运行带有我想实现相同功能的运行按钮的c#。

我知道我需要为诸如C#之类的特定语言安装运行时,并且在本地计算机中已经安装了该运行时,但是仍然无法运行它。

我们将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

What you see there is not a Monaco feature and is up to you to implement. How you do that, will depend largely on the language you're trying to run.

The first example (and jsFiddle and CodePen and many others) simply displays an iframe to show the result. That iframe loads a file with a unique name that contains the HTML, CSS and JavaScript code entered in the editor. You can confirm that this is what they're doing, using Chrome Dev Tools.

If you're going to run a language like C#, know that you will need full control of your web server. The flow will be something like this:

  1. The user presses the Run button.
  2. You call a web service (that you must develop), passing it the C# code and anything else needed to build a working project (like dependencies).
  3. The web service creates the project from those inputs, invokes the C# compiler, runs the resulting executable, and finally captures the output (both stdout and stderr) into string variables. Those strings are returned by the web service.
  4. Back in the browser, you display the output from the web service.

This is very doable, but getting it to perform well when your volumes pick up will be a special problem.