Pergunta

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?

Foi útil?

Solução

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

Outras dicas

<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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top