欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fastadmin后台集成ace在线代码编辑器

程序员文章站 2022-05-26 18:13:16
...

ace包在这:https://download.csdn.net/download/weixin_39347356/74370374
官方Git:https://github.com/ajaxorg/ace/
放到对应页面对应js中的api bindevent中

 <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Content')}:</label>
        <div class="col-xs-12 col-sm-8" id="editor">
            <textarea id="content" class="form-control hidden" rows="5" name="row[content]" cols="50">{$row.content|htmlentities}</textarea>
        </div>
        <textarea id="c-content" class="form-control hidden" rows="5" name="row[content]" cols="50">{$row.content|htmlentities}</textarea>
    </div>
require(['ace/ace','ace/theme/monokai','ace/ext/language_tools','ace/mode/javascript',
                    'ace/mode/css','ace/mode/html','ace/mode/php','ace/mode/text'
], function (ace) {
     var type=$('#type').val();
     var editor = ace.edit("editor");
     editor.setTheme("ace/theme/monokai");
     if(type=='css'){
         editor.getSession().setMode("ace/mode/css");
     }else
     if(type=='js'){
         editor.getSession().setMode("ace/mode/javascript");
     }else {
     //if(type=='html'){
         editor.getSession().setMode("ace/mode/html");
         editor.getSession().setMode("ace/mode/php");
     }
     editor.setFontSize(14); //字体大小
     document.getElementById("editor").style.lineHeight="18px";//设置行高;
     editor.setOption("wrap", "free");//自动换行
     editor.setOptions({
         enableBasicAutocompletion: true,
         enableSnippets: true,
         enableLiveAutocompletion: true
     });
     editor.setShowPrintMargin(false);
     editor.getSession().setUseWorker(false);
     editor.getSession().setUseWrapMode(true); //支持代码折叠

     editor.getSession().on('change', function(){
         // 设置textarea的值
         $('#c-content').val(editor.getSession().getValue());
     });

     $('#bark').click(function(){
         //设置编辑器内容
         //editor.setValue($('#previous').val())
         editor.getSession().setValue($('#previous').val())
     })
 });