ACE 示例
<!DOCTYPE html> <html lang="en"> <head> <title>ACE in Action</title> <style type="text/css" media="screen"> #editor { position: absolute; top: 0; right: 0; bottom: 20px; left: 0; } </style> </head> <body> <div id="editor"></div> <script src="ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script> <script> var editor = ace.edit("editor"); /******** 设置主题 *****/ editor.setTheme("ace/theme/monokai"); /*** 设置编程语言模式 ***/ editor.getSession().setMode("ace/mode/java"); /** 通用操作 */ //设置编辑器中文本值 editor.setValue("the new text here\n\n\n"); console.log(editor.getValue()); //获取选中的文本 var selectContent = editor.session.getTextRange(editor.getSelectionRange()); console.log(selectContent); //获取当前光标行和列 console.log(editor.selection.getCursor()); //跳到指定行 参数为行号 editor.gotoLine(2); //在光标处插入 editor.insert("Something cool,Something cool"); //获取行数个数 console.log(editor.session.getLength()); //设置默认的标签大小 editor.getSession().setTabSize(4);//没发现有什么效果 //使用软标签 editor.getSession().setUseSoftTabs(true); //没发现有什么效果 //设置字体大小 document.getElementById('editor').style.fontSize='12px'; //切换字体换行 editor.getSession().setUseWrapMode(true); //不知道啥效果 //设置行高亮 editor.setHighlightActiveLine(false); //设置打印边距可见性 editor.setShowPrintMargin(true); //设置编辑器只读 editor.setReadOnly(false); // false to make it editable /*************** 触发调整大小 *********************/ //Ace只在窗口事件时调整自己的大小。 如果以另一种方式调整编辑器div的大小,并需要Ace调整大小,请使用以下命令: editor.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:要搜索的范围。 对于整个文档,将其设置为null regExp:搜索是否为正则表达式。 默认为false。 start:开始搜索的范围或光标位置 skipCurrent:是否在搜索中包括当前行。 默认为false。 */ //以下是如何执行替换 editor.find('Something'); editor.replace('Everything'); //以下是替换所有 //editor.replaceAll('bar'); //editor.replaceAll 要在 editor.find('needle', ...) 使用之后 /******** 监听事件 ******/ // 监听 onchange editor.getSession().on('change', function(e) { console.log("change happened" + e); // e.type, etc }); // 监听 selection change editor.getSession().selection.on('changeSelection', function(e) { console.log("changeSelection happened" + e); }); // 监听 cursor change editor.getSession().selection.on('changeCursor', function(e) { console.log("changeCursor happened" + e); }); /**** 添加新命令和键盘绑定 *****/ //将键绑定分配给自定义函数 editor.commands.addCommand({ name: 'myCommand', bindKey: {win: 'Ctrl-M', mac: 'Command-M'}, exec: function(editor) { console.log("键绑定成功"); console.log("编辑器的值为:" + editor.getValue()); }, readOnly: true // false if this command should not apply in readOnly mode }); </script> </body> </html>