【问题标题】:Getting Monaco to work with Vuejs and electron让 Monaco 使用 Vuejs 和 electron
【发布时间】:2018-09-21 07:04:56
【问题描述】:

我有兴趣在Vue.js 支持的Electron 项目中使用Monaco 编辑器。

到目前为止:

微软提供了一个Electron Sample(我已经运行并且工作正常)

对于 monaco,有多种 vue.js npm repos - 但它们中没有一个似乎完全支持开箱即用的 Electron。

看起来最有希望的是vue-monaco,但我遇到了正确集成它的问题。

AMD 要求?

这是 Microsoft 示例中用于 Electron 的代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Monaco Editor!</title>
    </head>
    <body>
        <h1>Monaco Editor in Electron!</h1>
        <div id="container" style="width:500px;height:300px;border:1px solid #ccc"></div>
    </body>

    <script>
        // Monaco uses a custom amd loader that overrides node's require.
        // Keep a reference to node's require so we can restore it after executing the amd loader file.
        var nodeRequire = global.require;
    </script>
    <script src="../node_modules/monaco-editor/min/vs/loader.js"></script>
    <script>
        // Save Monaco's amd require and restore Node's require
        var amdRequire = global.require;
        global.require = nodeRequire;
    </script>

    <script>
        // require node modules before loader.js comes in
        var path = require('path');

        function uriFromPath(_path) {
            var pathName = path.resolve(_path).replace(/\\/g, '/');
            if (pathName.length > 0 && pathName.charAt(0) !== '/') {
                pathName = '/' + pathName;
            }
            return encodeURI('file://' + pathName);
        }

        amdRequire.config({
            baseUrl: uriFromPath(path.join(__dirname, '../node_modules/monaco-editor/min'))
        });

        // workaround monaco-css not understanding the environment
        self.module = undefined;

        // workaround monaco-typescript not understanding the environment
        self.process.browser = true;

        amdRequire(['vs/editor/editor.main'], function() {
            var editor = monaco.editor.create(document.getElementById('container'), {
                value: [
                    'function x() {',
                    '\tconsole.log("Hello world!");',
                    '}'
                ].join('\n'),
                language: 'javascript'
            });
        });
    </script>
</html>

我正在使用的模块允许这样的事情:

<template>
    <monaco-editor :require="amdRequire" />
</template>

<script>
export default {
    methods: {
        amdRequire: window.amdRequire
        // Or put this in `data`, doesn't really matter I guess
    }
}
</script>

我似乎无法弄清楚如何获得在 Electon + vue 中定义的正确 amdRequire 变量。我相信如果我能战胜它,其他一切都会变得简单。

Electron 常见问题解答中提到了一些关于此的内容(我认为):I can not sue jQuery/RequireJS/Meteor/AngularJS in Electron

示例代码

我在 GitHub https://github.com/jeeftor/Vue-Monaco-Electron 上放了一个示例项目,其中“违规”组件位于 ./src/renderer/components/Monaco.vue

总结

如何让这个 Monaco Editor 正确加载到将在 electron 中运行的 Vue.js 组件中?

感谢您提供的任何帮助。

【问题讨论】:

    标签: javascript vue.js electron amd monaco-editor


    【解决方案1】:

    我做的几乎一样,只是没有额外的 vue-monaco 组件。经过一番挣扎,我可以解决问题:

    function loadMonacoEditor () {
      const nodeRequire = global.require
    
      const loaderScript = document.createElement('script')
    
      loaderScript.onload = () => {
        const amdRequire = global.require
        global.require = nodeRequire
    
        var path = require('path')
    
        function uriFromPath (_path) {
          var pathName = path.resolve(_path).replace(/\\/g, '/')
    
          if (pathName.length > 0 && pathName.charAt(0) !== '/') {
            pathName = '/' + pathName
          }
    
          return encodeURI('file://' + pathName)
        }
    
        amdRequire.config({
          baseUrl: uriFromPath(path.join(__dirname, '../../../node_modules/monaco-editor/min'))
        })
    
        // workaround monaco-css not understanding the environment
        self.module = undefined
    
        // workaround monaco-typescript not understanding the environment
        self.process.browser = true
    
        amdRequire(['vs/editor/editor.main'], function () {
          this.monaco.editor.create(document.getElementById('container'), {
            value: [
              'function x() {',
              '\tconsole.log("Hello world!");',
              '}'
            ].join('\n'),
            language: 'javascript'
          })
        })
      }
    
      loaderScript.setAttribute('src', '../node_modules/monaco-editor/min/vs/loader.js')
      document.body.appendChild(loaderScript)
    }
    

    我刚刚获取了电子 AMD 样本并对其进行了一些调整。我在组件的created函数中调用loadMonacoEditor函数。

    为了不得到Not allowed to load local resource: file:///C:/.../node_modules/monaco-editor/min/vs/editor/editor.main.css的问题,你还得设置

    webPreferences: {
      webSecurity: false
    }
    

    在您的 BrowserWindow 实例中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-15
    • 2020-05-27
    • 2016-12-08
    • 2020-12-07
    • 2023-01-05
    • 1970-01-01
    • 2021-11-15
    • 2022-01-07
    相关资源
    最近更新 更多