【发布时间】: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