Ace Code Editor Set Language Dynamically [closed]

谁说我不能喝 提交于 2020-01-24 04:17:07

问题


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()});
});

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()

but still no luck.


回答1:


you have a typo in the editor.session().setMode({ line

use editor.session.setMode("ace/mode/" + newMode) instead of it.

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




回答2:


This is correct editor.getSession().setMode("ace/mode/" + newMode);

But instead you type editor.session instead of editor.getSession()



来源:https://stackoverflow.com/questions/23278238/ace-code-editor-set-language-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!