Ace代码编辑器动态设置语言 [英] Ace Code Editor Set Language Dynamically

查看:315
本文介绍了Ace代码编辑器动态设置语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下拉列表选择语言来实现Ace代码编辑器。我的下拉有一个模式ID。我让编辑器正常工作,但我无法使用下拉按照我希望的方式更改语言。我当前的代码是

I'm attempting to implement Ace Code Editor with a drop down to select the language. My drop down has an id of mode. I have got the editor to work correctly, but I cannot change the language using the drop down as I would like it to do. My current code is

var editor = ace.edit("code");
var textarea = $('textarea[name="code"]').hide();
editor.setTheme("ace/theme/textmate");
editor.getSession().setMode("ace/mode/sql");
editor.getSession().setValue(textarea.val());
editor.getSession().on('change', function(){
textarea.val(editor.getSession().getValue());
});

$('#mode').on('change', function(){
var newMode = $("mode").val();
editor.session().setMode({
    path: "ace/mode/" + newMode,
    v: Date.now()});
});

如上所述,这成功启动了编辑器,但我无法从SQL更改语言,是最初的语言。我遇到了这个问题动态更新语法突出显示模式规则Ace编辑器
这就是为什么我已经包含

As above, this successfully launches the editor, however I can't change the language from SQL, which is the initial language. I came across this question Dynamically update syntax highlighting mode rules for the Ace Editor which is why I've included

v: Date.now()

但仍然没有运气。

推荐答案

你在 editor.session()中有拼写错误.setMode({

使用 editor.session.setMode(ace / mode /+ newMode)而不是它。

v:Date.now()需要禁用缓存,你很可能不需要。

v: Date.now() was needed to disable caching, you most likely do not need that.

这篇关于Ace代码编辑器动态设置语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆