代码编辑插件使用


你是否需要高逼格的代码编辑功能

前言

最近项目需要在系统中增加代码编辑,为客户提供二次开发功能,让系统更容易扩展。为了实现这个功能,可没少折腾,在百度上一通翻箱倒柜,发现用的比较多的是codemirror,像开发人员踏足的w3school就是用的codemirror

但是codemirror只是基本的代码编辑和着色功能,没有我想要的自带自动完成功能,即编辑时自动触发自动完成(我测试时,是需要按快捷键才能出现自动完成,为了自动完成,还必须每次输入后还有按快捷键,所以放弃了)。又是一番找寻。众里寻她千百度,蓦然回首,那她却在微软处。原来微软开源了一款代码编辑神器monaco-editor,Visual Studio Code强大的代码编辑功能就是使用的她,她的家https://github.com/microsoft/monaco-editor, 看她第一眼就深深的吸引了我,对就她了,非她不"娶"了。

插件选择

功能codemirrormonaco-editor
官网https://codemirror.nethttps://github.com/microsoft/monaco-editor
代码编辑和着色支持支持
代码自动完成自动完成关键字需要先行维护自带
支持语言html,javascript,css等常用语言都支持codemirror支持的都支持,还支持一些codemirror不支持的她也支持,比如本人需要的csharp
大小小,几百kb,可按需引用语言包原始大小80m,忍受不了这么大,一番精简到22m

monaco-editor支持的语言见下图。image-20211209214505225

monaco-editor引用即用的代码自动完成功能。

如何使用

​ 经过一番权衡后,还是选择了monaco-editor,现在我们开始把她"娶"回家。要娶媳妇回家,怎能没有房子呢,打开vs,选择ASP.NET core web 应用,我们来为她建一栋。新家建好后,我来把她迎进来,看看新家怎么样。

image-20211213215230596

把媳妇安排到_CodeLayout.cshtml房间住下。为了媳妇能找得到房间,需指定一个路径。

//具体路径路径根据类库放置位置进行调整
<script src="~/lib/monaco-editor/min/vs/loader.js"></script> 

image-20211213215826381

媳妇一身好才艺,可别浪费了,发挥发挥她的特长。帮我们做个趁手的在线代码编辑器吧。

首先建一个首页Index.cshtml,前端建立一个按钮,一个代码编辑框和一个运行结果展示。前端代码如下

<div class="text-center">
    <button id='bt_run' onclick="addIframe()">运行(R)</button>
    <fieldset class="ui-grid-a">
        <!--编辑框-->
        <div class="ui-block-a">
            <div id="editcode" style="width: 800px; height: 800px; border: 1px solid grey"></div>
        </div>
        <div class="ui-block-b">
            <div id="iframe">
                 <!--显示控件-->
                <div id="iframewrapper">
                </div>
            </div>
        </div>
    </fieldset>
</div>

JS初始化配置

  window.onload=function () 
    {
        //**代码编辑器初始化 ,初始化很简单,就下面几行代码就实现了代码编辑*/
        code= window.localStorage["editcode"]
        if(code==null) code= '<HTML>this is test </HTML>';
        //指定配置路径,路径必须指定正确,不然无法正常显示
        require.config({ paths: { vs: '../lib/monaco-editor/min/vs' } });
        require(['vs/editor/editor.main'], function () {
            //**创建编辑器对象 */
            editor = monaco.editor.create(document.getElementById('editcode'), {
                //value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
                value: code,                          //初始化代码
                // language: 'javascript',
                language: 'html',                    //指定代码编辑语言
                theme: 'vs-dark'                     //指定编辑框主题
            });
        });
    }

以下说完整的javascript代码,使用monaco-editor就下面几行代码就实现在线代码编辑功能。

<script>

    let code;
    var editor;
    let framename = "iframe_test";
    window.onload=function () 
    {
        code= window.localStorage["editcode"]
        if(code==null) code= '<HTML>this is test </HTML>';
        require.config({ paths: { vs: '../lib/monaco-editor/min/vs' } });
        require(['vs/editor/editor.main'], function () {
            editor = monaco.editor.create(document.getElementById('editcode'), {
                //value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
                value: code,
                // language: 'javascript',
                language: 'html',
                theme: 'vs-dark'
            });
        });
    }

        var i = 0;    
        var editorArray = [];
        function addIframe() {
             //let editor = editorlist["editcode"];
           
            if (editor == null) { console.error("编辑器没有初始化!"); return; }
            let curr_frame = document.getElementById(framename);
            var If = curr_frame != null ? curr_frame : document.createElement("iframe");
            //var If = document.createElement("iframe");
            If.style.height = "800px";
            If.style.width = "100%";
            If.id = framename;
            If.name = framename;
            if (curr_frame == null) document.all("iframewrapper").appendChild(If);
            var doc1 = window.frames[framename].document;
            doc1.close();
            let editorArray = [];
            editorArray.push(editor);
            let code= editorArray[0].getValue();
            doc1.write(code);
            window.localStorage["editcode"] = code;

        }
</script>

一个简单的在线代码编辑器就做好了,点击运行按钮,运行代码编辑器内容,看看效果吧!


完整的演示代码,移步https://github.com/sunaccess/ifand

文章来源:https://www.cnblogs.com/zzwen/p/15690031.html

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:用 WinUI 3 开发了一个摸鱼应用
下一篇:vs2019+windows服务+nancy+打包
评论列表

发表评论

评论内容
昵称:
关联文章

代码编辑使用
YES-CMS 内容管理系统 TinyMCE编辑演示
页面快排开发
WPF学习笔记(四):AvalonEdit 代码高亮编辑专题
OneNote安装代码高亮-NoteHightlight(2010-2013-2016)
C# MEF化开发
【C#】C#中使用GDAL3(三):Windows下编译驱动
TinyMCE开发之《设置Code标签》
页面快排配置支持图片上传
TinyMCE富文本编辑器 autoLink 配置,全角支持
YES-CMS内容管理系统扩展
使用WtmPlus低代码平台提高生产力
【gitblit复制URL】 修改URL复制方式Flash被浏览器禁用解决办法
YESWin Winform开发框架 代码生成器使用
数据库 red-gate SQLToolbelt 下载地址
AgGrid表格编辑功能
利用SelectPdf将网页生成PDF
编辑窗体中关联赋值操作
dotnet 使用 WpfAnalyzers 辅助分析 WPF 应用代码缺陷
插入代码页面报错

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码