javascript - Ace 编辑器,toUpperCase 函数有效,但 toggleCommentLines 无效
问题描述
给定一个带有 ace 编辑器块的网页(包含一些代码行)。在 pageLoad 上,我想自动大写并注释第 1 到 2 行。
我像这样简单地实例化 ace 编辑器:
let editor = ace.edit(editorElement); // editorElement is an html element containing some code lines
editor.setOptions({
maxLines: Infinity,
});
editor.getSession().setMode("ace/mode/python");
这是在 pageLoad 之后执行的代码:
require('ace-builds');
require('ace-builds/webpack-resolver'); // my project is managed by symfony 5 and webpack encore
let Range = require('ace-builds').Range;
editor.getSelection().setRange(new Range(0,0,2,0));
editor.toggleCommentLines(); // does nothing...why ?
editor.toUpperCase(); // its work !
// editor.clearSelection();
结果是:
第 1 行和第 2 行按预期使用大写,但未注释行,我不明白为什么......
我不会“只是”添加一个 '#' 字符,因为生成的 ace 编辑器的模式可能会改变。例如,模式可能是 'ace/mode/sql',并且 sql 中的注释不要使用 '#' 字符
谢谢你的帮助 :)
解决方案
在这里解决:)
设置模式需要一段时间,当我调用时它没有加载 editor.toggleCommentLines();
。下面的代码适用于我:
editor.session.setMode("ace/mode/python", function() {
editor.toggleCommentLines();
});
感谢ace git repo上的@nightwing :)
推荐阅读
- amazon-web-services - Aws s3 文件名未在回显函数中使用批处理文件打印
- swift - 来自多个值的唯一数组
- python - 使用 Twisted Web Client 发布具有多个参数的请求
- azure - 使用 Azure AD 的 Springboot+Oauth 客户端凭据授予流程
- html - 我怎样才能使一个 div 进入中心
- c - ffmpeg - 缺少 avformat 标头
- flutter - Flutter Media Query MediaQuery.of(context).size.height 没有给出准确的高度或宽度。从谷歌得到
- r - 在R中将变量条件分配给数据框
- git - 如何复制一个分支和其中的所有数据?
- javascript - React forwardRef - 类型 IntrinsicAttributes 上不存在属性