惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
罗磊的独立博客
爱范儿
爱范儿
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
C
CXSECURITY Database RSS Feed - CXSecurity.com
Spread Privacy
Spread Privacy
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
G
GRAHAM CLULEY
A
About on SuperTechFans
C
Cisco Blogs
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
Project Zero
Project Zero
V
V2EX
K
Kaspersky official blog
P
Privacy International News Feed
博客园 - 叶小钗
I
Intezer
T
Threatpost
The GitHub Blog
The GitHub Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 【当耐特】
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
The Cloudflare Blog
P
Palo Alto Networks Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tenable Blog

博客园 - 马会东

Llama3 在HumanEval中的表现 在 Apple Silicon Mac(M1、M2 或 M3)上运行最新 LLM 模型 Meta Llama 3 的分步指南 mamp nginx thinkphp5 配置方法 centos8 yum 安装rabbitmq dotnet new 命令 在mac上使用docker部署Mongo数据库 如何在Curl中使用Socks5代理 知识付费平台 快速开发框架(管理系统-持续更新) mysql 索引优化原则总结(limit where in like ) MySQL高级知识——Order By关键字优化 mysql索引的排列顺序 解决MySQL使用limit偏移量较大效率慢的问题 tar命令详解 systemctl命令列出所有服务 Git的撤销、修改和回退命令 使用git时显示untracked files(未监控)解决办法 MySQL中Case When用法详解 计算机指令-流水线和吞吐率
ACE Editor(代码编辑器) 入门教程
马会东 · 2023-04-27 · via 博客园 - 马会东

作者:Albert.Wen  添加时间:2022-10-05 00:34:16  修改时间:2023-04-27 09:49:52  分类:HTML/CSS/JS 编辑

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

1、拷贝所需的JS文件

我是把源码目录 src-min-noconflict 中的文件,拷贝到项目目录中,如:/static/ace-editor/js

2、引用JS文件

在一般情况下,我们需要引入的js库是两个:ace.jsext-language_tools.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

<!DOCTYPE html>

 <html>

     <head>

         <title>Demo of ACE Editor</title>

         <script src="/static/ace-editor/js/ace.js" type="text/javascript" charset="utf-8"></script>

         <script src="/static/ace-editor/js/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>

     </head>

     <body>

         <pre id="code" class="ace_editor" style="min-height:400px"><textarea class="ace_text-input">

 #include <cstdio>

 int main(){

     int n,m;

     scanf("%d %d",&n,&m);

     printf("%d",n+m);

     return 0;

 }

         </textarea></pre>

         <script>

             //初始化对象

             editor = ace.edit("code");

             //设置风格和语言(更多风格和语言,请到github上相应目录查看)

             theme = "clouds"

             language = "c_cpp"

             editor.setTheme("ace/theme/" + theme);

             editor.session.setMode("ace/mode/" + language);

             //字体大小

             editor.setFontSize(18);

             //设置只读(true时只读,用于展示代码)

             editor.setReadOnly(false);

             //自动换行,设置为off关闭

             editor.setOption("wrap", "free")

             //启用提示菜单

             ace.require("ace/ext/language_tools");

             editor.setOptions({

                     enableBasicAutocompletion: true,

                     enableSnippets: true,

                     enableLiveAutocompletion: true

                 });

         </script>

     </body>

 </html>

效果图(纯本机测试,Notepad++ + Firefox):

3、基本用法

去除中间的竖线:

1

editor.renderer.setShowGutter(false);

去除行号:

1

editor.setShowPrintMargin(false);

设置并获取内容:

1

2

3

editor.setValue("the new text here");

editor.session.setValue("the new text here");

editor.getValue();

设置主题:

1

editor.setTheme("ace/theme/xcode");

设置语言模式:

1

editor.session.setMode("ace/mode/sql");

启用提示菜单: 

1

2

3

4

5

6

7

ace.require("ace/ext/language_tools");

editor.setOptions({

    enableBasicAutocompletion: true,

    enableSnippets: true,

    enableLiveAutocompletion: true

});

撤销:

重做回退:

光标跳转到指定行:

查找替换:

1

editor.execCommand('replace');

自动换行:

1

editor.setOption("wrap", "free");

参考:

  1. ACE Editor在线代码编辑器简介及使用引导
  2. Ace editor中文文档
  3. ACE编辑器ace-editor笔记

工作项目中的一次应用

模板文件:aceEditor.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<textarea id="hid_${inputId!}" name="${inputName!}" class="hidden">${defaultValue!}</textarea>

<pre id="${inputId!}" class="ace_editor ${cssClass!}" style="min-height:${height!}"><textarea class="ace_text-input">${defaultValue!}</textarea></pre>

<script>

    $(function() {

        // ACE-Editor代码编辑器

        ace.require("ace/ext/language_tools");

        let ${inputId!} = ace.edit("${inputId!}");

        ${inputId!}.setTheme("ace/theme/${theme!}");

        ${inputId!}.session.setMode("ace/mode/${language}");

        ${inputId!}.setOptions({

            enableBasicAutocompletion : true,

            enableSnippets : true,

            enableLiveAutocompletion : true

        });

        // 通过change事件,捕获编辑器的最新内容

        ${inputId!}.getSession().on('change', function(e) {

            $('#hid_${inputId!}').val(${inputId!}.getValue());

        });

    });

</script>