Asp.Net?Core?使用Monaco?Editor?實(shí)現(xiàn)代碼編輯器功能
在項(xiàng)目中經(jīng)常有代碼在線編輯的需求,比如修改基于Xml的配置文件,編輯Json格式的測(cè)試數(shù)據(jù)等。我們可以使用微軟開(kāi)源的在線代碼編輯器Monaco Editor實(shí)現(xiàn)這些功能。Monaco Editor是著名的VSCode的前身,項(xiàng)目地址:https://microsoft.github.io/monaco-editor/。本文介紹在Asp.Net Core項(xiàng)目中使用Monaco Editor實(shí)現(xiàn)代碼編輯器功能。
安裝
可以使用npm下載moaco-editor:
npm install monaco-editor@0.31.1
我們需要的文件在node_modules/monaco-editor/min/vs目錄中,將整個(gè)vs目錄拷貝到Asp.Net Core Web項(xiàng)目的wwwroot/lib目錄下:
在Program.cs中需要啟動(dòng)靜態(tài)文件:
app.UseStaticFiles();
在布局頁(yè)面中引入如下css和js:
<link data-name="vs/editor/editor.main" rel="stylesheet" href="~/lib/vs/editor/editor.main.css" rel="external nofollow" /> <script src="~/lib/vs/loader.js"></script> <script src="~/lib/vs/editor/editor.main.nls.js"></script> <script src="~/lib/vs/editor/editor.main.js"></script>
基本的環(huán)境設(shè)置就完成了。
基本功能
現(xiàn)在可以在頁(yè)面中實(shí)現(xiàn)編輯器的基本功能,在頁(yè)面中增加一個(gè)div,作為編輯器容器:
<div id="container" style="width:800px;height:600px;border:1px solid grey"></div>
然后增加編輯器的js代碼:
<script> $(document).ready(function () { require.config({ paths: { 'vs': '/lib/vs' } }); monaco.editor.create(document.getElementById('container'), { value: "function sayHello(){\n console.write('Hello World');\n}", language: 'javascript' }); }) </script>
設(shè)置
編輯器有多種設(shè)置,比如是否顯示代碼行、顯示樣式等等,設(shè)置完成后,可以使用updateOptions修改設(shè)置,示例代碼如下:
var editor = monaco.editor.create(document.getElementById('container'), { value: "function sayHello(){\n console.write('Hello World');\n}", language: 'javascript', lineNumbers: 'on', roundedSelection: false, scrollBeyondLastLine: false, readOnly: false, theme: 'vs-dark' });
代碼比較
monaco editor的一個(gè)強(qiáng)大的功能是文字比較功能,可以將兩段文字進(jìn)行比較:
<script> require.config({ paths: { 'vs': '/lib/vs' } }); var originalModel = monaco.editor.createModel( '刪除行\(zhòng)n行一\n行二\n行三內(nèi)容\n更多內(nèi)容', 'text/plain' ); var modifiedModel = monaco.editor.createModel( '\n行一\n行二\n行三\n更多內(nèi)容\n增加行', 'text/plain' ); var diffEditor = monaco.editor.createDiffEditor(document.getElementById('container'), { // You can optionally disable the resizing enableSplitViewResizing: false }); diffEditor.setModel({ original: originalModel, modified: modifiedModel }); </script>
效果如下:
自定義語(yǔ)言
monaco editor 支持常見(jiàn)的幾乎所有編程語(yǔ)言,在編輯這些語(yǔ)言的代碼時(shí)可以高亮顯示關(guān)鍵字,同時(shí)也支持對(duì)自定義語(yǔ)言的擴(kuò)展。其功能非常強(qiáng)大,同時(shí)配置起來(lái)也比較復(fù)雜,這里只舉個(gè)簡(jiǎn)單的例子,說(shuō)明如何使用。
這里使用的“語(yǔ)言”很簡(jiǎn)單,目的是記錄中國(guó)象棋的棋譜,關(guān)鍵字只有代表“車(chē)馬炮”等棋子的大寫(xiě)漢語(yǔ)拼音,運(yùn)算符只有代表向前、向后和平行移動(dòng)的“++”、“--”和“==”,還有就是注釋。
使用自定義語(yǔ)言,首先要注冊(cè)這個(gè)語(yǔ)言的id:
monaco.languages.register({ id: 'mylang' });
然后設(shè)置語(yǔ)言的Token Provider:
monaco.languages.setMonarchTokensProvider('mylang', getlang());
這樣就可以在編輯器中使用這種自定義語(yǔ)言了,下面是完整的代碼:
@page @model CustomLanguageModel @{ ViewData["Title"] = "自定義語(yǔ)言"; } <h1>@ViewData["Title"]</h1> <div id="container" style="height: 800px"></div> <script> var require = { paths: { vs: '/lib/vs' } }; </script> @section Scripts { <script> $(document).ready(function () { monaco.languages.register({ id: 'mylang' }); monaco.languages.setMonarchTokensProvider('mylang', getlang()); var sampleEditor=monaco.editor.create(document.getElementById('container'), { model:null } ); setTimeout(function(){ var model=monaco.editor.createModel('// 炮二平五 \nPAO 2 == 5 \n//馬八進(jìn)七 \nMA 8 ++ 7', 'mylang'); sampleEditor.setModel(model); },1); }); function getlang(){ return { //車(chē)馬炮相士將帥兵卒 keywords: [ 'JU', 'MA', 'PAO', 'XIANG', 'SHI', 'JIANG', 'SHUAI', 'BING', 'ZU' ], //++ 進(jìn) --退 ==平 operators: [ '++', '--', '==' ], symbols: /[=><!~?:&|+\-*\/\^%]+/, // The main tokenizer for our languages tokenizer: { root: [ [/[A-Z][\w\$]*/, {cases: { '@@keywords': 'keyword' }}], { include: '@@whitespace' }, [/@@symbols/, { cases: { '@@operators': 'operator'} } ], [/\d./, 'number'], ], comment: [ [/[^\/*]+/, 'comment' ], [/\/\*/, 'comment', '@@push' ], ["\\*/", 'comment', '@@pop' ], [/[\/*]/, 'comment' ] ], whitespace: [ [/[ \t\r\n]+/, 'white'], [/\/\*/, 'comment', '@@comment' ], [/\/\/.*$/, 'comment'], ], }, }; } </script> }}
效果如下:
本文的示例項(xiàng)目已經(jīng)上傳到github: https://github.com/zhenl/monacoEditorDotNet
到此這篇關(guān)于Asp.Net Core 使用Monaco Editor 實(shí)現(xiàn)代碼編輯器的文章就介紹到這了,更多相關(guān)Asp.Net Core代碼編輯器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
asp.net(C#)跨域及跨域?qū)慍ookie問(wèn)題
在網(wǎng)站www.A.com下通過(guò)iframe或ajax調(diào)用www.B.com下的內(nèi)容時(shí),默認(rèn)情況下IE會(huì)阻止www.B.com寫(xiě)任何Cookie2011-10-10.Net中導(dǎo)出數(shù)據(jù)到Excel(asp.net和winform程序中)
.Net中導(dǎo)出數(shù)據(jù)到Excel包括以下兩種情況:asp.net中導(dǎo)出Excel的方法/winForm中導(dǎo)出Excel的方法,針對(duì)以上兩種情況做了下詳細(xì)的實(shí)現(xiàn)代碼,感興趣的朋友可不要錯(cuò)過(guò)了哈,希望本文對(duì)你有所幫助2013-02-02ASP.NET中的DataGridView綁定數(shù)據(jù)和選中行刪除功能具體實(shí)例
廢話就不多說(shuō)了,都說(shuō).NET是托控件的,我就托給你們看,這個(gè)博文主要講 DataGridView 的數(shù)據(jù)綁定,和選中行刪除功能2013-12-12.NetCore手動(dòng)封裝日志組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了.NetCore手動(dòng)封裝日志組件的實(shí)現(xiàn)代碼,封裝的目的是便于在項(xiàng)目里更加簡(jiǎn)單方便使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03asp.net DataTable導(dǎo)出Excel自定義列名的方法
本文分享了asp.net DataTable導(dǎo)出Excel 自定義列名的具體實(shí)現(xiàn)方法,步驟清晰,代碼詳細(xì),需要的朋友可以參考借鑒,下面就跟小編一起來(lái)看看吧2016-12-12C# .Net動(dòng)態(tài)調(diào)用webService實(shí)現(xiàn)思路及代碼
動(dòng)態(tài)調(diào)用web服務(wù)將執(zhí)行以下步驟:獲取WSDL/生成客戶(hù)端代理類(lèi)代碼/設(shè)定編譯參數(shù)/編譯代理類(lèi)/生成代理實(shí)例,并調(diào)用方法,很詳細(xì)的,感興趣的你可不要錯(cuò)過(guò)了哈2013-02-02