ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python测试开发django -144.Ace Editor 在线编辑python代码

2021-09-28 23:34:39  阅读:248  来源: 互联网

标签:144 Ace ace python 编辑 editor 设置 true


前言

网页上想在线编辑代码,可以使用Ace Editor 在线编辑实现。比如我们想实现一个功能,在网页版上写python代码,能有python的语法自动补齐功能。

Ace Editor 在线编辑

ACE是一个开源的、独立的、基于浏览器的代码编辑器,可以嵌入到任何web页面或JavaScript应用程序中。ACE支持超过40种语言语法高亮,缩进,代码提示功能且具有大量的主题;并能够处理代码多达404万行的大型文档。ACE开发团队称,ACE在性能和功能上可以媲美本地代码编辑器(如SublimeText、TextMate和Vim等)。

Github地址:https://github.com/ajaxorg/ace
ace官网地址:https://ace.c9.io/
支持语言:python、java、javascript、json、jsp、markdown、mysql、nginx...等

导入js文件

需导入的js文件主要有2个:ace.js 和 ext-language_tools.js

方式1:用在线cdn
bootstrap 中文网提供的 cdn 服务;http://www.bootcdn.cn/

    <script src="http://cdn.bootcss.com/ace/1.2.4/ace.js"></script>
    <script src="http://cdn.bootcss.com/ace/1.2.4/ext-language_tools.js"></script>
 

或这个地址

    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript"</script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"</script>

方式2:下载到本地
从github下载资源到本地https://github.com/ajaxorg/ace.git

 <script src="/static/ace/src/ace.js"></script>
    <script src="/static/ace/src/ext-language_tools.js"></script>

简单使用

需注意的是<div id="editor"></div>容器需设置宽度和高度

    <style>
        /*必须给editor包裹元素设置宽高*/
     #editor{
         width:100%;
         height:800px;
     }
     </style>

完整html示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/static/ace/src/ace.js"></script>
    <script src="/static/ace/src/ext-language_tools.js"></script>
    <style>
        /*必须给editor包裹元素设置宽高*/
     #editor{
         width:100%;
         height:800px;
     }
     </style>
</head>

<body>
  <div id="editor"></div>
</body>
<script>
    //初始化id字符串(不加#)
    var editor = ace.edit('editor');
</script>
</html>

这样就可以得到一个最简单的在线编辑器了

添加主题和语言

设置字体大小,背景主题和语言设置为python

<script>
    //初始化id字符串(不加#)
    var editor = ace.edit('editor');
    //设置主题
    editor.setTheme("ace/theme/monokai");
    // 设置编辑语言
    editor.getSession().setMode("ace/mode/python")
    // 设置字体大小
    editor.setFontSize(28)
    
</script>

于是就可以得到这样效果

设置语法补齐

下一步需设置python代码语法自动补齐功能,设置setOptions属性。

<script>
    //初始化id字符串(不加#)
    var editor = ace.edit('editor');
    editor.setOptions({
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true
        });

    //设置主题
    editor.setTheme("ace/theme/monokai");
    // 设置编辑语言
    editor.getSession().setMode("ace/mode/python")
    // 设置字体大小
    editor.setFontSize(28)

</script>

其它功能

其它功能参考

<script>
    //初始化id字符串(不加#)
    var editor = ace.edit('editor');
    editor.setOptions({
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true

    });

    //设置主题
    editor.setTheme("ace/theme/monokai");
    // 设置编辑语言
    editor.getSession().setMode("ace/mode/python");
    // 设置字体大小
    editor.setFontSize(28);
    // 设置行高;
    document.getElementById("editor").style.lineHeight="40px";
    // 获取编辑内容
    // editor.getValue();
    // 移动光标至第0行,第0列
    editor.moveCursorTo(1, 0);

    //设置只读(true时只读,用于展示代码)
	editor.setReadOnly(false);
    //自动换行,设置为off关闭
	editor.setOption("wrap", "free");
    //5.设置编辑内容
    var editorValue='def function():\n' +
            '    return "hello"';
    editor.setValue(editorValue);
    //撤销:
    // editor.undo();
    //查找替换:
    editor.execCommand('replace');
    //编辑内容搜索
    //editor.execCommand('find');//与ctrl+f功能一致
    //自动换行 关闭时free换成off
    editor.setOption("wrap", "free");
    //获取编辑内容
    //editor.getValue();

</script>

标签:144,Ace,ace,python,编辑,editor,设置,true
来源: https://www.cnblogs.com/yoyoketang/p/15350649.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有