gpt4 book ai didi

javascript - 获取 Monaco Editor 的值(value)

转载 作者:行者123 更新时间:2023-12-03 03:15:38 38 4
gpt4 key购买 nike

微软最近开源了他们的 Monaco Editor (类似于 ace/codemirror)。

https://github.com/Microsoft/monaco-editor

我已经在浏览器中启动并运行它,但仍然不知道如何获取编辑器的当前文本,就像我想保存它一样。

示例:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>

<div id="container" style="width:800px;height:600px;border:1px solid grey"></div>

<script src="monaco-editor/min/vs/loader.js"></script>
<script>
require.config({ paths: { 'vs': 'monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function() {
var editor = monaco.editor.create(document.getElementById('container'), {
value: [
'function x() {',
'\tconsole.log("Hello world!");',
'}'
].join('\n'),
language: 'javascript'
});
});

function save() {
// how do I get the value/code inside the editor?
var value = "";
saveValueSomewhere(value);
}
</script>
</body>
</html>

最佳答案

require.config({ paths: { 'vs': 'monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function() {
window.editor = monaco.editor.create(document.getElementById('container'), {
value: [
'function x() {',
'\tconsole.log("Hello world!");',
'}'
].join('\n'),
language: 'javascript'
});
});

function save() {
// get the value of the data
var value = window.editor.getValue()
saveValueSomewhere(value);
}

关于javascript - 获取 Monaco Editor 的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086013/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com