簡體   English   中英

將 Monaco Editor 集成到 LitElement

[英]Integrate Monaco Editor into LitElement

我嘗試在 open-wc 中創建一個 Code Playground,並想使用 Monaco Editor。 問題是,我不能簡單地整合它。 這就是我嘗試過的方法,它可以工作,但性能很糟糕。

updated(changedProperties) {
    let container = this.shadowRoot.getElementById('container');
s
    function loadScript(filename, callback) {
      var fileref = document.createElement('script');
      fileref.setAttribute("type", "text/javascript");
      fileref.onload = callback;
      fileref.setAttribute("src", filename);
      if (typeof fileref != "undefined") {
        document.getElementsByTagName("head")[0].appendChild(fileref)
      }
    }

    loadScript('../node_modules/monaco-editor/min/vs/loader.js', function () {


      require.config({paths: {'vs': '../node_modules/monaco-editor/min/vs'}});

      require(['vs/editor/editor.main'], function () {

        var editor = monaco.editor.create(container, {
          value: [
            'function x() {',
            '\tconsole.log("Hello world!");',
            '}'
          ].join('\n'),
          language: 'javascript'
        });
      });

    });

  }


  render() {
    return html`
              <h2> Monaco Editor Sample</h2>
      <div id="container" style="width:100%;height:100%;border:1px solid grey"></div>

    `;
  }

您需要使用Light Dom才能使用它。

您必須將此添加到所有父母

createRenderRoot() {
    return this;
}

您必須考慮到 styles 將停止工作,您必須在render()中用<style>替換它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM