让 Monaco 与 Vuejs 和 Electron 一起工作
Getting Monaco to work with Vuejs and electron
我有兴趣使用 Monaco editor in a Vue.js backed Electron 项目。
到目前为止:
Microsoft 提供了一个 Electron Sample(我已经 运行 并且工作正常)
摩纳哥有多种 vue.js
npm repos - 但其中 none 似乎 完全支持 Electron盒子。
看起来最有前途的是 vue-monaco,但我 运行 遇到了正确集成它的问题。
AMD 要求?
这是用于 Electron 的 Microsoft 示例代码
<!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 with the "offending" component being in ./src/renderer/components/Monaco.vue
上放了一个示例项目
总结
我怎样才能让这个摩纳哥编辑器正确加载到 Vue.js 组件中,该组件将 运行 放入电子中?
感谢您提供的任何帮助。
我做的几乎一样,只是没有额外的 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)
}
我刚刚拿了 electron-amd 样本并稍微调整了一下。我在组件的创建函数中调用 loadMonacoEditor
函数。
为了不出现Not allowed to load local resource: file:///C:/.../node_modules/monaco-editor/min/vs/editor/editor.main.css
的问题,还得设置
webPreferences: {
webSecurity: false
}
在您的 BrowserWindow
实例中。
我有兴趣使用 Monaco editor in a Vue.js backed Electron 项目。
到目前为止:
Microsoft 提供了一个 Electron Sample(我已经 运行 并且工作正常)
摩纳哥有多种 vue.js
npm repos - 但其中 none 似乎 完全支持 Electron盒子。
看起来最有前途的是 vue-monaco,但我 运行 遇到了正确集成它的问题。
AMD 要求?
这是用于 Electron 的 Microsoft 示例代码
<!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 with the "offending" component being in ./src/renderer/components/Monaco.vue
上放了一个示例项目总结
我怎样才能让这个摩纳哥编辑器正确加载到 Vue.js 组件中,该组件将 运行 放入电子中?
感谢您提供的任何帮助。
我做的几乎一样,只是没有额外的 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)
}
我刚刚拿了 electron-amd 样本并稍微调整了一下。我在组件的创建函数中调用 loadMonacoEditor
函数。
为了不出现Not allowed to load local resource: file:///C:/.../node_modules/monaco-editor/min/vs/editor/editor.main.css
的问题,还得设置
webPreferences: {
webSecurity: false
}
在您的 BrowserWindow
实例中。