Consider the following code snippets:

HTML

<input type="text" id="mode" >

<div id="editor"></div>

Javascript with jQuery and Ace Editor bundled

$('#mode').on('change', function() {
    createEditor($(this).val().toLowerCase());
});

function createEditor(mode) {
        var editor = ace.edit('editor');
        editor.renderer.setShowGutter(true);
        editor.getSession().setMode("ace/mode/" + mode);
}

What I am trying to achieve is to dynamically set the mode for the editor session. So when I input "javascript" ace loads mode-javascript.js .

But when there is no "mode" file - I want to fallback to mode-text.js.

Right now - If someone enters "hdsajdlasjdl" the requests returns a 404 of course.

Is this checking even possible with ace or do I have to pre-define which modes are supported?

有帮助吗?

解决方案

Ace doesn't provide a way to detect the 404 error, but you can set mode to text before setting it to non existent mode, this way if request returns 404 mode will remain text.

Still better way is to use built in list of all available modes https://github.com/ajaxorg/ace/blob/master/lib/ace/ext/modelist.js#L173

其他提示

<script src="ace/min-noconflict/ace.js"></script>
<script src="ace/min-noconflict/ext-modelist.js"></script>
<script>
var modelist = ace.require('ace/ext/modelist');
if(modelist.modesByName['hdsajdlasjdl'] == undefined) {
    console.log("mode doesn't exist");
}
</script>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top