monaco-editor使用


monaco-editor是一款非常好用的web代码编辑器,那么如何把他加到自己的项目中呢。

1.下载插件

npm install monaco-editor@0.8.3

2.初始化编辑器值

<!--要绑定的对象-->
<div id="container"></div>

  

var monacoEditor;
//设置插件路径
require.config({ paths: { 'vs': '/Scripts/monaco/min/vs' } });
//绑定对象并赋值
require(['vs/editor/editor.main'], function () {
	//container为要绑定的对象
	monacoEditor = monaco.editor.create(document.getElementById('container'), {
		value: "<div>我是插入的代码</div>",
		language: 'html',
		wrappingColumn: 0,
		wrappingIndent: "indent"
	});
});
//自适应宽度
window.onresize = function () {
	if (monacoEditor) {
		monacoEditor.layout();
	}
};

3.获取编辑器值

monacoEditor.getValue();

4.替换编辑器值

//移除原有对象
$("#container").children().remove();
//重新绑定对象并赋新值
require(['vs/editor/editor.main'], function () {
            monacoEditor = monaco.editor.create(document.getElementById('container'), {
                value: '<span>nenewnew</span>',
                language: 'html',
                wrappingColumn: 0,
                wrappingIndent: "indent"
            });
        });

 

2017.7.17 版本有更新,代码有变动,请以下面的的代码为准

效果预览

代码下载


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM