ACE 示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>ACE in Action</title>
  5. <style type="text/css" media="screen">
  6. #editor {
  7. position: absolute;
  8. top: 0;
  9. right: 0;
  10. bottom: 20px;
  11. left: 0;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="editor"></div>
  17. <script src="ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
  18. <script>
  19. var editor = ace.edit("editor");
  20. /******** 设置主题 *****/
  21. editor.setTheme("ace/theme/monokai");
  22. /*** 设置编程语言模式 ***/
  23. editor.getSession().setMode("ace/mode/java");
  24. /** 通用操作 */
  25. //设置编辑器中文本值
  26. editor.setValue("the new text here\n\n\n");
  27. console.log(editor.getValue());
  28. //获取选中的文本
  29. var selectContent = editor.session.getTextRange(editor.getSelectionRange());
  30. console.log(selectContent);
  31. //获取当前光标行和列
  32. console.log(editor.selection.getCursor());
  33. //跳到指定行 参数为行号
  34. editor.gotoLine(2);
  35. //在光标处插入
  36. editor.insert("Something cool,Something cool");
  37. //获取行数个数
  38. console.log(editor.session.getLength());
  39. //设置默认的标签大小
  40. editor.getSession().setTabSize(4);//没发现有什么效果
  41. //使用软标签
  42. editor.getSession().setUseSoftTabs(true); //没发现有什么效果
  43. //设置字体大小
  44. document.getElementById('editor').style.fontSize='12px';
  45. //切换字体换行
  46. editor.getSession().setUseWrapMode(true); //不知道啥效果
  47. //设置行高亮
  48. editor.setHighlightActiveLine(false);
  49. //设置打印边距可见性
  50. editor.setShowPrintMargin(true);
  51. //设置编辑器只读
  52. editor.setReadOnly(false); // false to make it editable
  53. /*************** 触发调整大小 *********************/
  54. //Ace只在窗口事件时调整自己的大小。 如果以另一种方式调整编辑器div的大小,并需要Ace调整大小,请使用以下命令:
  55. editor.resize();
  56. /** 搜索 */
  57. editor.find('needle',{
  58. backwards: false,
  59. wrap: false,
  60. caseSensitive: false,
  61. wholeWord: false,
  62. regExp: false
  63. });
  64. editor.findNext();
  65. editor.findPrevious();
  66. /**
  67. * 以下选项可用于您的搜索参数:
  68. needle:您要查找的字符串或正则表达式
  69. backwards :是否从光标当前位置向后搜索。 默认为false。
  70. wrap:是否在搜索结束时将搜索回到开头。 默认为false。
  71. caseSensitive:搜索是否应区分大小写。 默认为false。
  72. wholeWord:搜索是否匹配整个单词。 默认为false。
  73. range:要搜索的范围。 对于整个文档,将其设置为null
  74. regExp:搜索是否为正则表达式。 默认为false。
  75. start:开始搜索的范围或光标位置
  76. skipCurrent:是否在搜索中包括当前行。 默认为false。
  77. */
  78. //以下是如何执行替换
  79. editor.find('Something');
  80. editor.replace('Everything');
  81. //以下是替换所有
  82. //editor.replaceAll('bar'); //editor.replaceAll 要在 editor.find('needle', ...) 使用之后
  83. /******** 监听事件 ******/
  84. // 监听 onchange
  85. editor.getSession().on('change', function(e) {
  86. console.log("change happened" + e);
  87. // e.type, etc
  88. });
  89. // 监听 selection change
  90. editor.getSession().selection.on('changeSelection', function(e) {
  91. console.log("changeSelection happened" + e);
  92. });
  93. // 监听 cursor change
  94. editor.getSession().selection.on('changeCursor', function(e) {
  95. console.log("changeCursor happened" + e);
  96. });
  97. /**** 添加新命令和键盘绑定 *****/
  98. //将键绑定分配给自定义函数
  99. editor.commands.addCommand({
  100. name: 'myCommand',
  101. bindKey: {win: 'Ctrl-M', mac: 'Command-M'},
  102. exec: function(editor) {
  103. console.log("键绑定成功");
  104. console.log("编辑器的值为:" + editor.getValue());
  105. },
  106. readOnly: true // false if this command should not apply in readOnly mode
  107. });
  108. </script>
  109. </body>
  110. </html>