ACE 是一个开源的、独立的、基于浏览器的代码编辑器,能够嵌入到任何web页面或JavaScript应用程序中。ACE支持超过60种语言语法高亮,并可以处理代码多达400万行的大型文档。ACE开发团队称,ACE在性能和功能上能够媲美本地代码编辑器(如Sublime Text、TextMate和Vim等)。javascript
ACE是Mozilla Skywriter(之前称为Bespin)项目的继任者,并做为Cloud9的主要在线编辑器。java
1、特性web
2、使用正则表达式
一、引入 ace浏览器
二、设置主题session
三、设置程序语言模式编辑器
四、通常经常使用操做
设置、获取内容:函数
获取选择内容:性能
在光标处插入:字体
获取光标所在行或列:
跳转到行:
获取总行数:
设置默认制表符的大小:
使用软标签:
设置字体大小,这个其实不算API:
设置代码折叠:
设置高亮:
设置打印边距可见度:
设置编辑器只读:
五、触发尺寸缩放
编辑器默认自适应大小,若是要程序控制resize,使用以下方法:
六、搜索
editor.find('needle',{ backwards: false, wrap: false, caseSensitive: false, wholeWord: false, regExp: false }); editor.findNext(); editor.findPrevious();
下列选项可用于您的搜索参数:
needle: 要查找的字符串或正则表达式
backwards: 是否反向搜索,默认为false
wrap: 搜索到文档底部是否回到顶端,默认为false
caseSensitive: 是否匹配大小写搜索,默认为false
wholeWord: 是否匹配整个单词搜素,默认为false
range: 搜索范围,要搜素整个文档则设置为空
regExp: 搜索内容是不是正则表达式,默认为false
start: 搜索起始位置
skipCurrent: 是否不搜索当前行,默认为false
替换单个字符:
替换多个字符:
editor.replaceAll使用前须要先调用editor.find('needle', ...)
七、事件监听
监听改变事件:
editor.getSession().on('change', function(e) { // e.type, etc });
监听选择事件:
editor.getSession().selection.on('changeSelection', function(e) { });
监听光标移动:
editor.getSession().selection.on('changeCursor', function(e) { });
八、添加新命令、绑定按键
要指定键绑定到一个自定义函数:
editor.commands.addCommand({ name: 'myCommand', bindKey: {win: 'Ctrl-M', mac: 'Command-M'}, exec: function(editor) { //... }, readOnly: true // 若是不须要使用只读模式,这里设置false });
3、禁止复制代码
<script type="text/javascript"> editor.setReadOnly(true); editor.commands.addCommand({ name: 'myCommand', bindKey: {win: 'Ctrl-C', mac: 'Command-C'}, exec: function(editor) { layer.alert('禁止复制',{title:'提示'}) console.log(editor) }, readOnly: true // 若是不须要使用只读模式,这里设置false }); if (window.Event) document.captureEvents(Event.MOUSEUP); function nocontextmenu(){ event.cancelBubble = true event.returnValue = false; return false; } function norightclick(e){ if (window.Event){ if (e.which == 2 || e.which == 3) return false; } else if (event.button == 2 || event.button == 3){ event.cancelBubble = true event.returnValue = false; return false; } } document.oncontextmenu = nocontextmenu; // for IE5+ document.onmousedown = norightclick; // for all others </script>